blob: 44e5208f7c61461be5ab21d1e3f8bc1bc2ded754 [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;
568 unsigned char *data = urb->transfer_buffer;
569 int length = urb->actual_length;
570 int bytes_avail;
571 int position;
572 int txCredits;
573 int portNumber;
574 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700575 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700577 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 case 0:
579 /* success */
580 break;
581 case -ECONNRESET:
582 case -ENOENT:
583 case -ESHUTDOWN:
584 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700585 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return;
587 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700588 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto exit;
590 }
591
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700592 dev = &edge_serial->serial->dev->dev;
593
Alan Cox03f0dbf2008-07-22 11:16:34 +0100594 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100596 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (length > 1) {
599 bytes_avail = data[0] | (data[1] << 8);
600 if (bytes_avail) {
601 spin_lock(&edge_serial->es_lock);
602 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700603 dev_dbg(dev,
604 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
605 __func__, bytes_avail,
606 edge_serial->rxBytesAvail,
607 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (edge_serial->rxBytesAvail > 0 &&
610 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700611 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100612 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Alan Cox03f0dbf2008-07-22 11:16:34 +0100614 /* we have pending bytes on the
615 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
617 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700618 dev_err(dev,
619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
620 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100621 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624 spin_unlock(&edge_serial->es_lock);
625 }
626 }
627 /* grab the txcredits for the ports if available */
628 position = 2;
629 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100630 while ((position < length) &&
631 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 txCredits = data[position] | (data[position+1] << 8);
633 if (txCredits) {
634 port = edge_serial->serial->port[portNumber];
635 edge_port = usb_get_serial_port_data(port);
636 if (edge_port->open) {
637 spin_lock(&edge_port->ep_lock);
638 edge_port->txCredits += txCredits;
639 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700640 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
641 __func__, portNumber,
642 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Alan Cox03f0dbf2008-07-22 11:16:34 +0100644 /* tell the tty driver that something
645 has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100646 tty_port_tty_wakeup(&edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100647 /* Since we have more credit, check
648 if more data can be sent */
649 send_more_port_data(edge_serial,
650 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652 }
653 position += 2;
654 ++portNumber;
655 }
656 }
657
658exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100659 result = usb_submit_urb(urb, GFP_ATOMIC);
660 if (result)
661 dev_err(&urb->dev->dev,
662 "%s - Error %d submitting control urb\n",
663 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666
667/*****************************************************************************
668 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100669 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * bulk in endpoint.
671 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100672static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Ming Leicdc97792008-02-24 18:41:47 +0800674 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700675 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700677 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700679 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700681 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700682 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
683 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100684 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 }
687
688 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700689 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
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
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700694 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 raw_data_length = urb->actual_length;
696
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100697 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 spin_lock(&edge_serial->es_lock);
700
701 /* decrement our rxBytes available by the number that we just got */
702 edge_serial->rxBytesAvail -= raw_data_length;
703
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700704 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
705 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Alan Cox03f0dbf2008-07-22 11:16:34 +0100707 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* check to see if there's any more data for us to read */
710 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700711 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700712 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
713 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700714 dev_err(dev,
715 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
716 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100717 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100720 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
723 spin_unlock(&edge_serial->es_lock);
724}
725
726
727/*****************************************************************************
728 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100729 * this is the callback function for when we have finished sending
730 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100732static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Ming Leicdc97792008-02-24 18:41:47 +0800734 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700735 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700737 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700738 dev_dbg(&urb->dev->dev,
739 "%s - nonzero write bulk status received: %d\n",
740 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100743 if (edge_port->open)
744 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alan Cox03f0dbf2008-07-22 11:16:34 +0100746 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100747 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Alan Cox03f0dbf2008-07-22 11:16:34 +0100749 /* Check if more data needs to be sent */
750 send_more_port_data((struct edgeport_serial *)
751 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754
755/*****************************************************************************
756 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100757 * this is the callback function for when we have finished sending a
758 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100760static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Ming Leicdc97792008-02-24 18:41:47 +0800762 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int status = urb->status;
764
Oliver Neukum96c706e2007-03-15 15:27:17 +0100765 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700766 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
767 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769
770 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700771 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100774 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700777 dev_dbg(&urb->dev->dev,
778 "%s - nonzero write bulk status received: %d\n",
779 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return;
781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* tell the tty driver that something has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100784 if (edge_port->open)
785 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100788 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 wake_up(&edge_port->wait_command);
790}
791
792
793/*****************************************************************************
794 * Driver tty interface functions
795 *****************************************************************************/
796
797/*****************************************************************************
798 * SerialOpen
799 * this function is called by the tty driver when a port is opened
800 * If successful, we return 0
801 * Otherwise we return a negative error number.
802 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700803static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700806 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct usb_serial *serial;
808 struct edgeport_serial *edge_serial;
809 int response;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (edge_port == NULL)
812 return -ENODEV;
813
Alan Cox03f0dbf2008-07-22 11:16:34 +0100814 /* see if we've set up our endpoint info yet (can't set it up
815 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 serial = port->serial;
817 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100818 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (edge_serial->interrupt_in_buffer == NULL) {
821 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100824 edge_serial->interrupt_in_buffer =
825 port0->interrupt_in_buffer;
826 edge_serial->interrupt_in_endpoint =
827 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
829 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100830 edge_serial->bulk_in_endpoint =
831 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100833 edge_serial->bulk_out_endpoint =
834 port0->bulk_out_endpointAddress;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* set up our interrupt urb */
837 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100838 serial->dev,
839 usb_rcvintpipe(serial->dev,
840 port0->interrupt_in_endpointAddress),
841 port0->interrupt_in_buffer,
842 edge_serial->interrupt_read_urb->transfer_buffer_length,
843 edge_interrupt_callback, edge_serial,
844 edge_serial->interrupt_read_urb->interval);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* set up our bulk in urb */
847 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100848 usb_rcvbulkpipe(serial->dev,
849 port0->bulk_in_endpointAddress),
850 port0->bulk_in_buffer,
851 edge_serial->read_urb->transfer_buffer_length,
852 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100853 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100856 * this interrupt will continue as long
857 * as the edgeport is connected */
858 response = usb_submit_urb(edge_serial->interrupt_read_urb,
859 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700861 dev_err(dev, "%s - Error %d submitting control urb\n",
862 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* initialize our wait queues */
867 init_waitqueue_head(&edge_port->wait_open);
868 init_waitqueue_head(&edge_port->wait_chase);
869 init_waitqueue_head(&edge_port->delta_msr_wait);
870 init_waitqueue_head(&edge_port->wait_command);
871
872 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100873 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100876 edge_port->txCredits = 0; /* Can't send any data yet */
877 /* Must always set this bit to enable ints! */
878 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100879 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100882 edge_port->openPending = true;
883 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100884 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700887 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100888 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -ENODEV;
890 }
891
892 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100893 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
894 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100896 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700898 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100899 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -ENODEV;
901 }
902
903 /* create the txfifo */
904 edge_port->txfifo.head = 0;
905 edge_port->txfifo.tail = 0;
906 edge_port->txfifo.count = 0;
907 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100908 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700911 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100912 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -ENOMEM;
914 }
915
916 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100917 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100918 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700921 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100922 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENOMEM;
924 }
925
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700926 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
927 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 return 0;
930}
931
932
933/************************************************************************
934 *
935 * block_until_chase_response
936 *
937 * This function will block the close until one of the following:
938 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800939 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
941 *
942 ************************************************************************/
943static void block_until_chase_response(struct edgeport_port *edge_port)
944{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700945 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 DEFINE_WAIT(wait);
947 __u16 lastCredits;
948 int timeout = 1*HZ;
949 int loop = 10;
950
951 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100952 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 lastCredits = edge_port->txCredits;
954
Alan Cox03f0dbf2008-07-22 11:16:34 +0100955 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100956 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700957 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Alan Cox03f0dbf2008-07-22 11:16:34 +0100959 /* did we get all of our credit back? */
960 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700961 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return;
963 }
964 }
965
Alan Cox03f0dbf2008-07-22 11:16:34 +0100966 /* Block the thread for a while */
967 prepare_to_wait(&edge_port->wait_chase, &wait,
968 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 schedule_timeout(timeout);
970 finish_wait(&edge_port->wait_chase, &wait);
971
972 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100973 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 loop--;
975 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100976 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700977 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 }
980 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100981 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700982 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100983 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 loop = 10;
985 }
986 }
987}
988
989
990/************************************************************************
991 *
992 * block_until_tx_empty
993 *
994 * This function will block the close until one of the following:
995 * 1. TX count are 0
996 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800997 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 *
999 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001000static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001002 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 DEFINE_WAIT(wait);
1004 struct TxFifo *fifo = &edge_port->txfifo;
1005 __u32 lastCount;
1006 int timeout = HZ/10;
1007 int loop = 30;
1008
1009 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001010 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 lastCount = fifo->count;
1012
Alan Cox03f0dbf2008-07-22 11:16:34 +01001013 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001015 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return;
1017 }
1018
Alan Cox03f0dbf2008-07-22 11:16:34 +01001019 /* Block the thread for a while */
1020 prepare_to_wait(&edge_port->wait_chase, &wait,
1021 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 schedule_timeout(timeout);
1023 finish_wait(&edge_port->wait_chase, &wait);
1024
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001025 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001028 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 loop--;
1030 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001031 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return;
1033 }
1034 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001035 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 loop = 30;
1037 }
1038 }
1039}
1040
1041
1042/*****************************************************************************
1043 * edge_close
1044 * this function is called by the tty driver when a port is closed
1045 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001046static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct edgeport_serial *edge_serial;
1049 struct edgeport_port *edge_port;
1050 int status;
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 edge_serial = usb_get_serial_data(port->serial);
1053 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001054 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001056
1057 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 block_until_tx_empty(edge_port);
1059
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001060 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001062 if ((!edge_serial->is_epic) ||
1063 ((edge_serial->is_epic) &&
1064 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1065 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001066 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001068 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001069 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1070 if (status == 0)
1071 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001072 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001073 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001074 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001077 if ((!edge_serial->is_epic) ||
1078 ((edge_serial->is_epic) &&
1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1080 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001081 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001082 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Alan Cox03f0dbf2008-07-22 11:16:34 +01001085 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001086 edge_port->closePending = false;
1087 edge_port->open = false;
1088 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001090 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001093 /* if this urb had a transfer buffer already
1094 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001095 kfree(edge_port->write_urb->transfer_buffer);
1096 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 edge_port->write_urb = NULL;
1098 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001099 kfree(edge_port->txfifo.fifo);
1100 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103/*****************************************************************************
1104 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001105 * this function is called by the tty driver when data should be written
1106 * to the port.
1107 * If successful, we return the number of bytes written, otherwise we
1108 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001110static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1111 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1114 struct TxFifo *fifo;
1115 int copySize;
1116 int bytesleft;
1117 int firsthalf;
1118 int secondhalf;
1119 unsigned long flags;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (edge_port == NULL)
1122 return -ENODEV;
1123
Alan Cox03f0dbf2008-07-22 11:16:34 +01001124 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 fifo = &edge_port->txfifo;
1126
1127 spin_lock_irqsave(&edge_port->ep_lock, flags);
1128
Alan Cox03f0dbf2008-07-22 11:16:34 +01001129 /* calculate number of bytes to put in fifo */
1130 copySize = min((unsigned int)count,
1131 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001133 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1134 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001135 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Alan Cox03f0dbf2008-07-22 11:16:34 +01001137 /* catch writes of 0 bytes which the tty driver likes to give us,
1138 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001140 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 goto finish_write;
1142 }
1143
Alan Cox03f0dbf2008-07-22 11:16:34 +01001144 /* queue the data
1145 * since we can never overflow the buffer we do not have to check for a
1146 * full condition
1147 *
1148 * the copy is done is two parts -- first fill to the end of the buffer
1149 * then copy the reset from the start of the buffer
1150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001152 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001153 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1154 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* now copy our data */
1157 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001158 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Alan Cox03f0dbf2008-07-22 11:16:34 +01001160 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 fifo->head += firsthalf;
1162 fifo->count += firsthalf;
1163
Alan Cox03f0dbf2008-07-22 11:16:34 +01001164 /* wrap the index */
1165 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 secondhalf = copySize-firsthalf;
1169
1170 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001171 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001173 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001174 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 fifo->count += secondhalf;
1176 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001177 /* No need to check for wrap since we can not get to end of
1178 * the fifo in this part
1179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182finish_write:
1183 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1184
Alan Cox03f0dbf2008-07-22 11:16:34 +01001185 send_more_port_data((struct edgeport_serial *)
1186 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001188 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1189 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Alan Cox03f0dbf2008-07-22 11:16:34 +01001191 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194
1195/************************************************************************
1196 *
1197 * send_more_port_data()
1198 *
1199 * This routine attempts to write additional UART transmit data
1200 * to a port over the USB bulk pipe. It is called (1) when new
1201 * data has been written to a port's TxBuffer from higher layers
1202 * (2) when the peripheral sends us additional TxCredits indicating
1203 * that it can accept more Tx data for a given port; and (3) when
1204 * a bulk write completes successfully and we want to see if we
1205 * can transmit more.
1206 *
1207 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001208static void send_more_port_data(struct edgeport_serial *edge_serial,
1209 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001212 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct urb *urb;
1214 unsigned char *buffer;
1215 int status;
1216 int count;
1217 int bytesleft;
1218 int firsthalf;
1219 int secondhalf;
1220 unsigned long flags;
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 spin_lock_irqsave(&edge_port->ep_lock, flags);
1223
1224 if (edge_port->write_in_progress ||
1225 !edge_port->open ||
1226 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001227 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1228 __func__, edge_port->port->number,
1229 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto exit_send;
1231 }
1232
Alan Cox03f0dbf2008-07-22 11:16:34 +01001233 /* since the amount of data in the fifo will always fit into the
1234 * edgeport buffer we do not need to check the write length
1235 *
1236 * Do we have enough credits for this port to make it worthwhile
1237 * to bother queueing a write. If it's too small, say a few bytes,
1238 * it's better to wait for more credits so we can do a larger write.
1239 */
1240 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 -07001241 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001242 __func__, edge_port->port->number, fifo->count,
1243 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto exit_send;
1245 }
1246
Alan Cox03f0dbf2008-07-22 11:16:34 +01001247 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001248 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Alan Cox03f0dbf2008-07-22 11:16:34 +01001250 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 urb = edge_port->write_urb;
1252
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001253 /* make sure transfer buffer is freed */
1254 kfree(urb->transfer_buffer);
1255 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Alan Cox03f0dbf2008-07-22 11:16:34 +01001257 /* build the data header for the buffer and port that we are about
1258 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001260 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001262 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001263 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001264 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto exit_send;
1266 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001267 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1268 - edge_port->port->serial->minor, count);
1269 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1270 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* now copy our data */
1273 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001274 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1276 fifo->tail += firsthalf;
1277 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001278 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 secondhalf = count-firsthalf;
1282 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001283 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1284 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 fifo->tail += secondhalf;
1286 fifo->count -= secondhalf;
1287 }
1288
1289 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001290 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001293 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1294 usb_sndbulkpipe(edge_serial->serial->dev,
1295 edge_serial->bulk_out_endpoint),
1296 buffer, count+2,
1297 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* decrement the number of credits we have by the number we just sent */
1300 edge_port->txCredits -= count;
1301 edge_port->icount.tx += count;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 status = usb_submit_urb(urb, GFP_ATOMIC);
1304 if (status) {
1305 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001306 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001307 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1308 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001309 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* revert the credits as something bad happened. */
1312 edge_port->txCredits += count;
1313 edge_port->icount.tx -= count;
1314 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001315 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1316 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318exit_send:
1319 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1320}
1321
1322
1323/*****************************************************************************
1324 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001325 * this function is called by the tty driver when it wants to know how
1326 * many bytes of data we can accept for a specific port. If successful,
1327 * we return the amount of room that we have for this port (the txCredits)
1328 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001330static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Alan Cox95da3102008-07-22 11:09:07 +01001332 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1334 int room;
1335 unsigned long flags;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001338 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001339 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001343 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
Alan Cox03f0dbf2008-07-22 11:16:34 +01001347 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 spin_lock_irqsave(&edge_port->ep_lock, flags);
1349 room = edge_port->txCredits - edge_port->txfifo.count;
1350 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1351
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001352 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return room;
1354}
1355
1356
1357/*****************************************************************************
1358 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001359 * this function is called by the tty driver when it wants to know how
1360 * many bytes of data we currently have outstanding in the port (data that
1361 * has been written, but hasn't made it out the port yet)
1362 * If successful, we return the number of bytes left to be written in the
1363 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * Otherwise we return a negative error number.
1365 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001366static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Alan Cox95da3102008-07-22 11:09:07 +01001368 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1370 int num_chars;
1371 unsigned long flags;
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001374 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001375 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001379 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001384 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1385 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1387 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001388 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1389 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 return num_chars;
1393}
1394
1395
1396/*****************************************************************************
1397 * SerialThrottle
1398 * this function is called by the tty driver when it wants to stop the data
1399 * being read from the port.
1400 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001401static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Alan Cox95da3102008-07-22 11:09:07 +01001403 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int status;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (edge_port == NULL)
1408 return;
1409
1410 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001411 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return;
1413 }
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* if we are implementing XON/XOFF, send the stop character */
1416 if (I_IXOFF(tty)) {
1417 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001418 status = edge_write(tty, port, &stop_char, 1);
1419 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
1423 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001424 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001426 status = send_cmd_write_uart_register(edge_port, MCR,
1427 edge_port->shadowMCR);
1428 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433
1434/*****************************************************************************
1435 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001436 * this function is called by the tty driver when it wants to resume the
1437 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001439static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Alan Cox95da3102008-07-22 11:09:07 +01001441 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int status;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (edge_port == NULL)
1446 return;
1447
1448 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001449 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return;
1451 }
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* if we are implementing XON/XOFF, send the start character */
1454 if (I_IXOFF(tty)) {
1455 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001456 status = edge_write(tty, port, &start_char, 1);
1457 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001461 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001463 send_cmd_write_uart_register(edge_port, MCR,
1464 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468
1469/*****************************************************************************
1470 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001471 * this function is called by the tty driver when it wants to change
1472 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001474static void edge_set_termios(struct tty_struct *tty,
1475 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 unsigned int cflag;
1479
Alan Coxadc8d742012-07-14 15:31:47 +01001480 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001481 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 -07001482 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 -07001483
1484 if (edge_port == NULL)
1485 return;
1486
1487 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001488 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490 }
1491
1492 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001493 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496
1497/*****************************************************************************
1498 * get_lsr_info - get line status register info
1499 *
1500 * Purpose: Let user call ioctl() to get info when the UART physically
1501 * is emptied. On bus types like RS485, the transmitter must
1502 * release the bus after transmitting. This must be done when
1503 * the transmit shift register is empty, not be done when the
1504 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001505 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001507static int get_lsr_info(struct edgeport_port *edge_port,
1508 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 unsigned int result = 0;
1511 unsigned long flags;
1512
1513 spin_lock_irqsave(&edge_port->ep_lock, flags);
1514 if (edge_port->maxTxCredits == edge_port->txCredits &&
1515 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001516 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 result = TIOCSER_TEMT;
1518 }
1519 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1520
1521 if (copy_to_user(value, &result, sizeof(int)))
1522 return -EFAULT;
1523 return 0;
1524}
1525
Alan Cox20b9d172011-02-14 16:26:50 +00001526static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001527 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Alan Cox95da3102008-07-22 11:09:07 +01001529 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1531 unsigned int mcr;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 mcr = edge_port->shadowMCR;
1534 if (set & TIOCM_RTS)
1535 mcr |= MCR_RTS;
1536 if (set & TIOCM_DTR)
1537 mcr |= MCR_DTR;
1538 if (set & TIOCM_LOOP)
1539 mcr |= MCR_LOOPBACK;
1540
1541 if (clear & TIOCM_RTS)
1542 mcr &= ~MCR_RTS;
1543 if (clear & TIOCM_DTR)
1544 mcr &= ~MCR_DTR;
1545 if (clear & TIOCM_LOOP)
1546 mcr &= ~MCR_LOOPBACK;
1547
1548 edge_port->shadowMCR = mcr;
1549
1550 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1551
1552 return 0;
1553}
1554
Alan Cox60b33c12011-02-14 16:26:14 +00001555static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Alan Cox95da3102008-07-22 11:09:07 +01001557 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1559 unsigned int result = 0;
1560 unsigned int msr;
1561 unsigned int mcr;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 msr = edge_port->shadowMSR;
1564 mcr = edge_port->shadowMCR;
1565 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1566 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1567 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1568 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1569 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1570 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return result;
1573}
1574
Alan Cox0bca1b92010-09-16 18:21:40 +01001575static int edge_get_icount(struct tty_struct *tty,
1576 struct serial_icounter_struct *icount)
1577{
1578 struct usb_serial_port *port = tty->driver_data;
1579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1580 struct async_icount cnow;
1581 cnow = edge_port->icount;
1582
1583 icount->cts = cnow.cts;
1584 icount->dsr = cnow.dsr;
1585 icount->rng = cnow.rng;
1586 icount->dcd = cnow.dcd;
1587 icount->rx = cnow.rx;
1588 icount->tx = cnow.tx;
1589 icount->frame = cnow.frame;
1590 icount->overrun = cnow.overrun;
1591 icount->parity = cnow.parity;
1592 icount->brk = cnow.brk;
1593 icount->buf_overrun = cnow.buf_overrun;
1594
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001595 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1596 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001597 return 0;
1598}
1599
Alan Cox03f0dbf2008-07-22 11:16:34 +01001600static int get_serial_info(struct edgeport_port *edge_port,
1601 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct serial_struct tmp;
1604
1605 if (!retinfo)
1606 return -EFAULT;
1607
1608 memset(&tmp, 0, sizeof(tmp));
1609
1610 tmp.type = PORT_16550A;
1611 tmp.line = edge_port->port->serial->minor;
1612 tmp.port = edge_port->port->number;
1613 tmp.irq = 0;
1614 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1615 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1616 tmp.baud_base = 9600;
1617 tmp.close_delay = 5*HZ;
1618 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1621 return -EFAULT;
1622 return 0;
1623}
1624
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626/*****************************************************************************
1627 * SerialIoctl
1628 * this function handles any ioctl calls to the driver
1629 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001630static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001631 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Alan Cox95da3102008-07-22 11:09:07 +01001633 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 DEFINE_WAIT(wait);
1635 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1636 struct async_icount cnow;
1637 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001639 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001642 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001643 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001644 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Alan Cox03f0dbf2008-07-22 11:16:34 +01001646 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001647 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001648 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Alan Cox03f0dbf2008-07-22 11:16:34 +01001650 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001651 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001652 cprev = edge_port->icount;
1653 while (1) {
1654 prepare_to_wait(&edge_port->delta_msr_wait,
1655 &wait, TASK_INTERRUPTIBLE);
1656 schedule();
1657 finish_wait(&edge_port->delta_msr_wait, &wait);
1658 /* see if a signal did it */
1659 if (signal_pending(current))
1660 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001662 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1663 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1664 return -EIO; /* no change => error */
1665 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1666 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1667 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1668 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1669 return 0;
1670 }
1671 cprev = cnow;
1672 }
1673 /* NOTREACHED */
1674 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return -ENOIOCTLCMD;
1678}
1679
1680
1681/*****************************************************************************
1682 * SerialBreak
1683 * this function sends a break to the port
1684 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001685static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Alan Cox95da3102008-07-22 11:09:07 +01001687 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001689 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int status;
1691
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001692 if ((!edge_serial->is_epic) ||
1693 ((edge_serial->is_epic) &&
1694 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1695 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001696 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001698 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001699 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001700 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001701 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001702 block_until_chase_response(edge_port);
1703 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001704 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001708 if ((!edge_serial->is_epic) ||
1709 ((edge_serial->is_epic) &&
1710 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1711 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001712 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001713 status = send_iosp_ext_cmd(edge_port,
1714 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001715 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001717 status = send_iosp_ext_cmd(edge_port,
1718 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001719 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001720 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001721 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001722 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726
1727/*****************************************************************************
1728 * process_rcvd_data
1729 * this function handles the data received on the bulk in pipe.
1730 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001731static void process_rcvd_data(struct edgeport_serial *edge_serial,
1732 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001734 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct usb_serial_port *port;
1736 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 __u16 lastBufferLength;
1738 __u16 rxLen;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 lastBufferLength = bufferLength + 1;
1741
1742 while (bufferLength > 0) {
1743 /* failsafe incase we get a message that we don't understand */
1744 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001745 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 break;
1747 }
1748 lastBufferLength = bufferLength;
1749
1750 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001751 case EXPECT_HDR1:
1752 edge_serial->rxHeader1 = *buffer;
1753 ++buffer;
1754 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Alan Cox03f0dbf2008-07-22 11:16:34 +01001756 if (bufferLength == 0) {
1757 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001759 }
1760 /* otherwise, drop on through */
1761 case EXPECT_HDR2:
1762 edge_serial->rxHeader2 = *buffer;
1763 ++buffer;
1764 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001766 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1767 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001768 /* Process depending on whether this header is
1769 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Alan Cox03f0dbf2008-07-22 11:16:34 +01001771 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1772 /* Decode this status header and go to
1773 * EXPECT_HDR1 (if we can process the status
1774 * with only 2 bytes), or go to EXPECT_HDR3 to
1775 * get the third byte. */
1776 edge_serial->rxPort =
1777 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1778 edge_serial->rxStatusCode =
1779 IOSP_GET_STATUS_CODE(
1780 edge_serial->rxHeader1);
1781
1782 if (!IOSP_STATUS_IS_2BYTE(
1783 edge_serial->rxStatusCode)) {
1784 /* This status needs additional bytes.
1785 * Save what we have and then wait for
1786 * more data.
1787 */
1788 edge_serial->rxStatusParam
1789 = edge_serial->rxHeader2;
1790 edge_serial->rxState = EXPECT_HDR3;
1791 break;
1792 }
1793 /* We have all the header bytes, process the
1794 status now */
1795 process_rcvd_status(edge_serial,
1796 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 edge_serial->rxState = EXPECT_HDR1;
1798 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001799 } else {
1800 edge_serial->rxPort =
1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802 edge_serial->rxBytesRemaining =
1803 IOSP_GET_HDR_DATA_LEN(
1804 edge_serial->rxHeader1,
1805 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001806 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1807 __func__,
1808 edge_serial->rxPort,
1809 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Alan Cox03f0dbf2008-07-22 11:16:34 +01001811 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1812 * ASSERT(DevExt->RxBytesRemaining <
1813 * IOSP_MAX_DATA_LENGTH);
1814 */
1815
1816 if (bufferLength == 0) {
1817 edge_serial->rxState = EXPECT_DATA;
1818 break;
1819 }
1820 /* Else, drop through */
1821 }
1822 case EXPECT_DATA: /* Expect data */
1823 if (bufferLength < edge_serial->rxBytesRemaining) {
1824 rxLen = bufferLength;
1825 /* Expect data to start next buffer */
1826 edge_serial->rxState = EXPECT_DATA;
1827 } else {
1828 /* BufLen >= RxBytesRemaining */
1829 rxLen = edge_serial->rxBytesRemaining;
1830 /* Start another header next time */
1831 edge_serial->rxState = EXPECT_HDR1;
1832 }
1833
1834 bufferLength -= rxLen;
1835 edge_serial->rxBytesRemaining -= rxLen;
1836
1837 /* spit this data back into the tty driver if this
1838 port is open */
1839 if (rxLen) {
1840 port = edge_serial->serial->port[
1841 edge_serial->rxPort];
1842 edge_port = usb_get_serial_port_data(port);
1843 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001844 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1845 __func__, rxLen,
1846 edge_serial->rxPort);
1847 edge_tty_recv(edge_port->port, buffer,
1848 rxLen);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001849 edge_port->icount.rx += rxLen;
1850 }
1851 buffer += rxLen;
1852 }
1853 break;
1854
1855 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1856 edge_serial->rxHeader3 = *buffer;
1857 ++buffer;
1858 --bufferLength;
1859
1860 /* We have all the header bytes, process the
1861 status now */
1862 process_rcvd_status(edge_serial,
1863 edge_serial->rxStatusParam,
1864 edge_serial->rxHeader3);
1865 edge_serial->rxState = EXPECT_HDR1;
1866 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 }
1869}
1870
1871
1872/*****************************************************************************
1873 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001874 * this function handles the any status messages received on the
1875 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001877static void process_rcvd_status(struct edgeport_serial *edge_serial,
1878 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
1880 struct usb_serial_port *port;
1881 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001882 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001883 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 __u8 code = edge_serial->rxStatusCode;
1885
1886 /* switch the port pointer to the one being currently talked about */
1887 port = edge_serial->serial->port[edge_serial->rxPort];
1888 edge_port = usb_get_serial_port_data(port);
1889 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001890 dev_err(&edge_serial->serial->dev->dev,
1891 "%s - edge_port == NULL for port %d\n",
1892 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return;
1894 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001895 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 if (code == IOSP_EXT_STATUS) {
1898 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001899 case IOSP_EXT_STATUS_CHASE_RSP:
1900 /* we want to do EXT status regardless of port
1901 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001902 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1903 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001904 /* Currently, the only EXT_STATUS is Chase, so process
1905 * here instead of one more call to one more subroutine
1906 * If/when more EXT_STATUS, there'll be more work to do
1907 * Also, we currently clear flag and close the port
1908 * regardless of content of above's Byte3.
1909 * We could choose to do something else when Byte3 says
1910 * Timeout on Chase from Edgeport, like wait longer in
1911 * block_until_chase_response, but for now we don't.
1912 */
1913 edge_port->chaseResponsePending = false;
1914 wake_up(&edge_port->wait_chase);
1915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Alan Cox03f0dbf2008-07-22 11:16:34 +01001917 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001918 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1919 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001920 /* Port->RxCheckRsp = true; */
1921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 }
1924
1925 if (code == IOSP_STATUS_OPEN_RSP) {
1926 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1927 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001928 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1929 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001930 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Alan Cox03f0dbf2008-07-22 11:16:34 +01001932 /* send the current line settings to the port so we are
1933 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001934 tty = tty_port_tty_get(&edge_port->port->port);
1935 if (tty) {
1936 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001937 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001938 tty_kref_put(tty);
1939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001942 edge_port->openPending = false;
1943 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 wake_up(&edge_port->wait_open);
1945 return;
1946 }
1947
Alan Cox03f0dbf2008-07-22 11:16:34 +01001948 /* If port is closed, silently discard all rcvd status. We can
1949 * have cases where buffered status is received AFTER the close
1950 * port command is sent to the Edgeport.
1951 */
1952 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001956 /* Not currently sent by Edgeport */
1957 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001958 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1959 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001960 handle_new_lsr(edge_port, false, byte2, 0);
1961 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Alan Cox03f0dbf2008-07-22 11:16:34 +01001963 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001964 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1965 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001966 /* byte2 is LSR Register */
1967 /* byte3 is broken data byte */
1968 handle_new_lsr(edge_port, true, byte2, byte3);
1969 break;
1970 /*
1971 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001972 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001973 * __func__, edge_serial->rxPort, byte2, byte3);
1974 * break;
1975 */
1976 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001977 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1978 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001979 /*
1980 * Process this new modem status and generate appropriate
1981 * events, etc, based on the new status. This routine
1982 * also saves the MSR in Port->ShadowMsr.
1983 */
1984 handle_new_msr(edge_port, byte2);
1985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Alan Cox03f0dbf2008-07-22 11:16:34 +01001987 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001988 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993
1994/*****************************************************************************
1995 * edge_tty_recv
1996 * this function passes data on to the tty flip buffer
1997 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01001998static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1999 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 int cnt;
2002
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002003 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00002004 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002005 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00002006 __func__, length - cnt);
2007 }
2008 data += cnt;
2009 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Jiri Slaby2e124b42013-01-03 15:53:06 +01002011 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014
2015/*****************************************************************************
2016 * handle_new_msr
2017 * this function handles any change to the msr register for a port.
2018 *****************************************************************************/
2019static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2020{
2021 struct async_icount *icount;
2022
Alan Cox03f0dbf2008-07-22 11:16:34 +01002023 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2024 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 icount = &edge_port->icount;
2026
2027 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002028 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002030 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002032 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002034 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 wake_up_interruptible(&edge_port->delta_msr_wait);
2037 }
2038
2039 /* Save the new modem status */
2040 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043
2044/*****************************************************************************
2045 * handle_new_lsr
2046 * this function handles any change to the lsr register for a port.
2047 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002048static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2049 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002051 __u8 newLsr = (__u8) (lsr & (__u8)
2052 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2053 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 edge_port->shadowLSR = lsr;
2056
2057 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002058 /*
2059 * Parity and Framing errors only count if they
2060 * occur exclusive of a break being
2061 * received.
2062 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2064 }
2065
2066 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002067 if (lsrData)
2068 edge_tty_recv(edge_port->port, &data, 1);
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* update input line counters */
2071 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002072 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002074 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002076 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
2082
2083/****************************************************************************
2084 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002085 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 * given address.
2087 * If successful returns the number of bytes written, otherwise it returns
2088 * a negative error number of the problem.
2089 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002090static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2091 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 int result;
2094 __u16 current_length;
2095 unsigned char *transfer_buffer;
2096
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002097 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Alan Cox03f0dbf2008-07-22 11:16:34 +01002099 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002101 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2102 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return -ENOMEM;
2104 }
2105
2106 /* need to split these writes up into 64 byte chunks */
2107 result = 0;
2108 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002109 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002111 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002113
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002114/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002115 memcpy(transfer_buffer, data, current_length);
2116 result = usb_control_msg(serial->dev,
2117 usb_sndctrlpipe(serial->dev, 0),
2118 USB_REQUEST_ION_WRITE_RAM,
2119 0x40, addr, extAddr, transfer_buffer,
2120 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (result < 0)
2122 break;
2123 length -= current_length;
2124 addr += current_length;
2125 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Alan Cox03f0dbf2008-07-22 11:16:34 +01002128 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return result;
2130}
2131
2132
2133/****************************************************************************
2134 * rom_write
2135 * writes a number of bytes to the Edgeport device's ROM starting at the
2136 * given address.
2137 * If successful returns the number of bytes written, otherwise it returns
2138 * a negative error number of the problem.
2139 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002140static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2141 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int result;
2144 __u16 current_length;
2145 unsigned char *transfer_buffer;
2146
Alan Cox03f0dbf2008-07-22 11:16:34 +01002147 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002149 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2150 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -ENOMEM;
2152 }
2153
2154 /* need to split these writes up into 64 byte chunks */
2155 result = 0;
2156 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002157 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002159 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161 memcpy(transfer_buffer, data, current_length);
2162 result = usb_control_msg(serial->dev,
2163 usb_sndctrlpipe(serial->dev, 0),
2164 USB_REQUEST_ION_WRITE_ROM, 0x40,
2165 addr, extAddr,
2166 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (result < 0)
2168 break;
2169 length -= current_length;
2170 addr += current_length;
2171 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Alan Cox03f0dbf2008-07-22 11:16:34 +01002174 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return result;
2176}
2177
2178
2179/****************************************************************************
2180 * rom_read
2181 * reads a number of bytes from the Edgeport device starting at the given
2182 * address.
2183 * If successful returns the number of bytes read, otherwise it returns
2184 * a negative error number of the problem.
2185 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002186static int rom_read(struct usb_serial *serial, __u16 extAddr,
2187 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 int result;
2190 __u16 current_length;
2191 unsigned char *transfer_buffer;
2192
Alan Cox03f0dbf2008-07-22 11:16:34 +01002193 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002195 dev_err(&serial->dev->dev,
2196 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return -ENOMEM;
2198 }
2199
2200 /* need to split these reads up into 64 byte chunks */
2201 result = 0;
2202 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002203 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002205 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002207 result = usb_control_msg(serial->dev,
2208 usb_rcvctrlpipe(serial->dev, 0),
2209 USB_REQUEST_ION_READ_ROM,
2210 0xC0, addr, extAddr, transfer_buffer,
2211 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (result < 0)
2213 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002214 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 length -= current_length;
2216 addr += current_length;
2217 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Alan Cox03f0dbf2008-07-22 11:16:34 +01002220 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return result;
2222}
2223
2224
2225/****************************************************************************
2226 * send_iosp_ext_cmd
2227 * Is used to send a IOSP message to the Edgeport device
2228 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002229static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2230 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 unsigned char *buffer;
2233 unsigned char *currentCommand;
2234 int length = 0;
2235 int status = 0;
2236
Alan Cox03f0dbf2008-07-22 11:16:34 +01002237 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002239 dev_err(&edge_port->port->dev,
2240 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return -ENOMEM;
2242 }
2243
2244 currentCommand = buffer;
2245
Alan Cox03f0dbf2008-07-22 11:16:34 +01002246 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2247 edge_port->port->number - edge_port->port->serial->minor,
2248 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Alan Cox03f0dbf2008-07-22 11:16:34 +01002250 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (status) {
2252 /* something bad happened, let's free up the memory */
2253 kfree(buffer);
2254 }
2255
2256 return status;
2257}
2258
2259
2260/*****************************************************************************
2261 * write_cmd_usb
2262 * this function writes the given buffer out to the bulk write endpoint.
2263 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002264static int write_cmd_usb(struct edgeport_port *edge_port,
2265 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002267 struct edgeport_serial *edge_serial =
2268 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002269 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int status = 0;
2271 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002273 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002276 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (!urb)
2278 return -ENOMEM;
2279
Oliver Neukum96c706e2007-03-15 15:27:17 +01002280 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002281 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2282 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Alan Cox03f0dbf2008-07-22 11:16:34 +01002284 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2285 usb_sndbulkpipe(edge_serial->serial->dev,
2286 edge_serial->bulk_out_endpoint),
2287 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002289 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 status = usb_submit_urb(urb, GFP_ATOMIC);
2291
2292 if (status) {
2293 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002294 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2295 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 usb_kill_urb(urb);
2297 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002298 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return status;
2300 }
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002303 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002305 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002307 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 status = -EINVAL;
2309 }
2310#endif
2311 return status;
2312}
2313
2314
2315/*****************************************************************************
2316 * send_cmd_write_baud_rate
2317 * this function sends the proper command to change the baud rate of the
2318 * specified port.
2319 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002320static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2321 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002323 struct edgeport_serial *edge_serial =
2324 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002325 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 unsigned char *cmdBuffer;
2327 unsigned char *currCmd;
2328 int cmdLen = 0;
2329 int divisor;
2330 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002331 unsigned char number =
2332 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Adam Kropelinbc71e472007-07-29 11:03:29 -04002334 if (edge_serial->is_epic &&
2335 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002336 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2337 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002338 return 0;
2339 }
2340
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002341 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2342 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002344 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002346 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return status;
2348 }
2349
Alan Cox03f0dbf2008-07-22 11:16:34 +01002350 /* Alloc memory for the string of commands. */
2351 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002353 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return -ENOMEM;
2355 }
2356 currCmd = cmdBuffer;
2357
Alan Cox03f0dbf2008-07-22 11:16:34 +01002358 /* Enable access to divisor latch */
2359 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Alan Cox03f0dbf2008-07-22 11:16:34 +01002361 /* Write the divisor itself */
2362 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2363 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Alan Cox03f0dbf2008-07-22 11:16:34 +01002365 /* Restore original value to disable access to divisor latch */
2366 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2367 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Alan Cox03f0dbf2008-07-22 11:16:34 +01002369 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (status) {
2371 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002372 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
2375 return status;
2376}
2377
2378
2379/*****************************************************************************
2380 * calc_baud_rate_divisor
2381 * this function calculates the proper baud rate divisor for the specified
2382 * baud rate.
2383 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002384static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 int i;
2387 __u16 custom;
2388
Tobias Klauser52950ed2005-12-11 16:20:08 +01002389 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002390 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 *divisor = divisor_table[i].Divisor;
2392 return 0;
2393 }
2394 }
2395
Alan Cox03f0dbf2008-07-22 11:16:34 +01002396 /* We have tried all of the standard baud rates
2397 * lets try to calculate the divisor for this baud rate
2398 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002400 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 custom = (__u16)((230400L + baudrate/2) / baudrate);
2402
2403 *divisor = custom;
2404
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002405 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return 0;
2407 }
2408
2409 return -1;
2410}
2411
2412
2413/*****************************************************************************
2414 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002415 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002417static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2418 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002420 struct edgeport_serial *edge_serial =
2421 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002422 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 unsigned char *cmdBuffer;
2424 unsigned char *currCmd;
2425 unsigned long cmdLen = 0;
2426 int status;
2427
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002428 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2429 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Adam Kropelinbc71e472007-07-29 11:03:29 -04002431 if (edge_serial->is_epic &&
2432 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2433 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002434 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002435 return 0;
2436 }
2437
Adam Kropelinbc71e472007-07-29 11:03:29 -04002438 if (edge_serial->is_epic &&
2439 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2440 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002441 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002442 return 0;
2443 }
2444
Alan Cox03f0dbf2008-07-22 11:16:34 +01002445 /* Alloc memory for the string of commands. */
2446 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2447 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 currCmd = cmdBuffer;
2451
Alan Cox03f0dbf2008-07-22 11:16:34 +01002452 /* Build a cmd in the buffer to write the given register */
2453 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2454 edge_port->port->number - edge_port->port->serial->minor,
2455 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2458 if (status) {
2459 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002460 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
2463 return status;
2464}
2465
2466
2467/*****************************************************************************
2468 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002469 * This routine is called to set the UART on the device to match the
2470 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002472
2473static void change_port_settings(struct tty_struct *tty,
2474 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002476 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002477 struct edgeport_serial *edge_serial =
2478 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int baud;
2480 unsigned cflag;
2481 __u8 mask = 0xff;
2482 __u8 lData;
2483 __u8 lParity;
2484 __u8 lStop;
2485 __u8 rxFlow;
2486 __u8 txFlow;
2487 int status;
2488
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002489 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002491 if (!edge_port->open &&
2492 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002493 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return;
2495 }
2496
Alan Coxadc8d742012-07-14 15:31:47 +01002497 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002500 case CS5:
2501 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002502 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002503 break;
2504 case CS6:
2505 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002506 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002507 break;
2508 case CS7:
2509 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002510 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002511 break;
2512 default:
2513 case CS8:
2514 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002515 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518
2519 lParity = LCR_PAR_NONE;
2520 if (cflag & PARENB) {
2521 if (cflag & CMSPAR) {
2522 if (cflag & PARODD) {
2523 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002524 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 } else {
2526 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002527 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529 } else if (cflag & PARODD) {
2530 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002531 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 } else {
2533 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002534 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002537 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539
2540 if (cflag & CSTOPB) {
2541 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002542 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } else {
2544 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002545 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
2548 /* figure out the flow control settings */
2549 rxFlow = txFlow = 0x00;
2550 if (cflag & CRTSCTS) {
2551 rxFlow |= IOSP_RX_FLOW_RTS;
2552 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002553 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002555 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
Alan Cox03f0dbf2008-07-22 11:16:34 +01002558 /* if we are implementing XON/XOFF, set the start and stop character
2559 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (I_IXOFF(tty) || I_IXON(tty)) {
2561 unsigned char stop_char = STOP_CHAR(tty);
2562 unsigned char start_char = START_CHAR(tty);
2563
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002564 if ((!edge_serial->is_epic) ||
2565 ((edge_serial->is_epic) &&
2566 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002567 send_iosp_ext_cmd(edge_port,
2568 IOSP_CMD_SET_XON_CHAR, start_char);
2569 send_iosp_ext_cmd(edge_port,
2570 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /* if we are implementing INBOUND XON/XOFF */
2574 if (I_IXOFF(tty)) {
2575 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002576 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2577 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002579 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
2582 /* if we are implementing OUTBOUND XON/XOFF */
2583 if (I_IXON(tty)) {
2584 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002585 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2586 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002588 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 }
2591
2592 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002593 if ((!edge_serial->is_epic) ||
2594 ((edge_serial->is_epic) &&
2595 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2596 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2597 if ((!edge_serial->is_epic) ||
2598 ((edge_serial->is_epic) &&
2599 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2600 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602
2603 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2604 edge_port->shadowLCR |= (lData | lParity | lStop);
2605
2606 edge_port->validDataMask = mask;
2607
2608 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002609 status = send_cmd_write_uart_register(edge_port, LCR,
2610 edge_port->shadowLCR);
2611 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 /* set up the MCR register and send it to the EdgePort */
2615 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002616 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002618
2619 status = send_cmd_write_uart_register(edge_port, MCR,
2620 edge_port->shadowMCR);
2621 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 /* Determine divisor based on baud rate */
2625 baud = tty_get_baud_rate(tty);
2626 if (!baud) {
2627 /* pick a default, any default... */
2628 baud = 9600;
2629 }
2630
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002631 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002632 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002633 if (status == -1) {
2634 /* Speed change was not possible - put back the old speed */
2635 baud = tty_termios_baud_rate(old_termios);
2636 tty_encode_baud_rate(tty, baud, baud);
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640
2641/****************************************************************************
2642 * unicode_to_ascii
2643 * Turns a string from Unicode into ASCII.
2644 * Doesn't do a good job with any characters that are outside the normal
2645 * ASCII range, but it's only for debugging...
2646 * NOTE: expects the unicode in LE format
2647 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002648static void unicode_to_ascii(char *string, int buflen,
2649 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 int i;
2652
Pete Zaitcevad933752006-05-22 21:49:44 -07002653 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002655 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Pete Zaitcevad933752006-05-22 21:49:44 -07002657 for (i = 0; i < unicode_size; i++) {
2658 if (i >= buflen)
2659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002661 }
2662 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665
2666/****************************************************************************
2667 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002668 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * structure.
2670 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002671static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002673 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 int response;
2675
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002676 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Alan Cox03f0dbf2008-07-22 11:16:34 +01002678 response = rom_read(edge_serial->serial,
2679 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2680 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2681 EDGE_MANUF_DESC_LEN,
2682 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Alan Cox03f0dbf2008-07-22 11:16:34 +01002684 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002685 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002686 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002688 dev_dbg(dev, "**Manufacturer Descriptor\n");
2689 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002690 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002691 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002692 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002693 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002694 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002695 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002696 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002697 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002698 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002699 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002700 edge_serial->manuf_descriptor.DescDate[0],
2701 edge_serial->manuf_descriptor.DescDate[1],
2702 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002703 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002704 edge_serial->manuf_descriptor.SerialNumber,
2705 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002706 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002707 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002708 edge_serial->manuf_descriptor.AssemblyNumber,
2709 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002710 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002711 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002712 edge_serial->manuf_descriptor.OemAssyNumber,
2713 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002714 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2715 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002716 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002718 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002719 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002720 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722}
2723
2724
2725/****************************************************************************
2726 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002727 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * structure.
2729 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002730static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002732 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 int response;
2734
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002735 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Alan Cox03f0dbf2008-07-22 11:16:34 +01002737 response = rom_read(edge_serial->serial,
2738 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2739 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2740 EDGE_BOOT_DESC_LEN,
2741 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Alan Cox03f0dbf2008-07-22 11:16:34 +01002743 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002744 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002745 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002746 dev_dbg(dev, "**Boot Descriptor:\n");
2747 dev_dbg(dev, " BootCodeLength: %d\n",
2748 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2749 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002750 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002751 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002752 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002753 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002754 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002755 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002756 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002757 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002758 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002759 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002760 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762}
2763
2764
2765/****************************************************************************
2766 * load_application_firmware
2767 * This is called to load the application firmware to the device
2768 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002769static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002771 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302772 const struct ihex_binrec *rec;
2773 const struct firmware *fw;
2774 const char *fw_name;
2775 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302777 __u32 Operaddr;
2778 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 switch (edge_serial->product_info.iDownloadFile) {
2781 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302782 fw_info = "downloading firmware version (930)";
2783 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 break;
2785
2786 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302787 fw_info = "downloading firmware version (80251)";
2788 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 break;
2790
2791 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002792 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return;
2794
2795 default:
2796 return;
2797 }
2798
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302799 response = request_ihex_firmware(&fw, fw_name,
2800 &edge_serial->serial->dev->dev);
2801 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002802 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302803 fw_name, response);
2804 return;
2805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302807 rec = (const struct ihex_binrec *)fw->data;
2808 build = (rec->data[2] << 8) | rec->data[3];
2809
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002810 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302811
Bjørn Mork271c1152011-01-17 14:19:37 +01002812 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2813 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302814 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2815
2816 for (rec = ihex_next_binrec(rec); rec;
2817 rec = ihex_next_binrec(rec)) {
2818 Operaddr = be32_to_cpu(rec->addr);
2819 response = sram_write(edge_serial->serial,
2820 Operaddr >> 16,
2821 Operaddr & 0xFFFF,
2822 be16_to_cpu(rec->len),
2823 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302825 dev_err(&edge_serial->serial->dev->dev,
2826 "sram_write failed (%x, %x, %d)\n",
2827 Operaddr >> 16, Operaddr & 0xFFFF,
2828 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 break;
2830 }
2831 }
2832
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002833 dev_dbg(dev, "sending exec_dl_code\n");
2834 response = usb_control_msg (edge_serial->serial->dev,
2835 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2836 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2838
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302839 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842
2843/****************************************************************************
2844 * edge_startup
2845 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002846static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002850 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002851 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002852 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002853 bool interrupt_in_found;
2854 bool bulk_in_found;
2855 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002856 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2857 EDGE_COMPATIBILITY_MASK1,
2858 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 dev = serial->dev;
2861
2862 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002863 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002865 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return -ENOMEM;
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 spin_lock_init(&edge_serial->es_lock);
2869 edge_serial->serial = serial;
2870 usb_set_serial_data(serial, edge_serial);
2871
2872 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002873 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002874 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002875 if (i < 0)
2876 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002877 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002878 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002879 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2882
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002883 /* Read the epic descriptor */
2884 if (get_epic_descriptor(edge_serial) <= 0) {
2885 /* memcpy descriptor to Supports structures */
2886 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2887 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002889 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002890 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002892 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002893 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002894
2895 get_product_info(edge_serial);
2896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /* set the number of ports from the manufacturing description */
2899 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002900 if ((!edge_serial->is_epic) &&
2901 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002902 dev_warn(ddev,
2903 "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 -08002904 edge_serial->product_info.NumPorts,
2905 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002908 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002910 /* If not an EPiC device */
2911 if (!edge_serial->is_epic) {
2912 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002913 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002915 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002917 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002918 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002920 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002921
2922 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002923/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002924/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002926 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302927 edge_serial->product_info.FirmwareMajorVersion,
2928 edge_serial->product_info.FirmwareMinorVersion,
2929 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Alan Cox03f0dbf2008-07-22 11:16:34 +01002931 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 * as the structures aren't created yet. */
2933
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002934 response = 0;
2935
2936 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002937 /* EPIC thing, set up our interrupt polling now and our read
2938 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002939 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002940 for (i = 0; i < serial->interface->altsetting[0]
2941 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002942 struct usb_endpoint_descriptor *endpoint;
2943 int buffer_size;
2944
Alan Cox03f0dbf2008-07-22 11:16:34 +01002945 endpoint = &serial->interface->altsetting[0].
2946 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002947 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002948 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002949 (usb_endpoint_is_int_in(endpoint))) {
2950 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002951 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002952
2953 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002954 edge_serial->interrupt_read_urb =
2955 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002956 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002957 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002958 return -ENOMEM;
2959 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002960 edge_serial->interrupt_in_buffer =
2961 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002962 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002963 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002964 usb_free_urb(edge_serial->interrupt_read_urb);
2965 return -ENOMEM;
2966 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002967 edge_serial->interrupt_in_endpoint =
2968 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002969
2970 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002971 usb_fill_int_urb(
2972 edge_serial->interrupt_read_urb,
2973 dev,
2974 usb_rcvintpipe(dev,
2975 endpoint->bEndpointAddress),
2976 edge_serial->interrupt_in_buffer,
2977 buffer_size,
2978 edge_interrupt_callback,
2979 edge_serial,
2980 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002981
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002982 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002983 }
2984
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002985 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01002986 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002988 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002989
2990 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002991 edge_serial->read_urb =
2992 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002993 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002994 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002995 return -ENOMEM;
2996 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002997 edge_serial->bulk_in_buffer =
2998 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002999 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003000 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001 usb_free_urb(edge_serial->read_urb);
3002 return -ENOMEM;
3003 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003004 edge_serial->bulk_in_endpoint =
3005 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003006
3007 /* set up our bulk in urb */
3008 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003009 usb_rcvbulkpipe(dev,
3010 endpoint->bEndpointAddress),
3011 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003012 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003013 edge_bulk_in_callback,
3014 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003015 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003016 }
3017
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003018 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003019 (usb_endpoint_is_bulk_out(endpoint))) {
3020 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003021 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003022 edge_serial->bulk_out_endpoint =
3023 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003024 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003025 }
3026 }
3027
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003028 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003029 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003030 return -ENODEV;
3031 }
3032
3033 /* start interrupt read for this edgeport this interrupt will
3034 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003035 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3036 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003037 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003038 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003039 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003040 }
3041 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
3044
3045/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003046 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 * This function is called whenever the device is removed from the usb bus.
3048 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003049static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003051 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003054 /* free up our endpoint stuff */
3055 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003056 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003057 usb_free_urb(edge_serial->interrupt_read_urb);
3058 kfree(edge_serial->interrupt_in_buffer);
3059
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003060 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003061 usb_free_urb(edge_serial->read_urb);
3062 kfree(edge_serial->bulk_in_buffer);
3063 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003064}
3065
3066
3067/****************************************************************************
3068 * edge_release
3069 * This function is called when the device structure is deallocated.
3070 ****************************************************************************/
3071static void edge_release(struct usb_serial *serial)
3072{
3073 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003074
3075 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003078static int edge_port_probe(struct usb_serial_port *port)
3079{
3080 struct edgeport_port *edge_port;
3081
3082 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3083 if (!edge_port)
3084 return -ENOMEM;
3085
3086 spin_lock_init(&edge_port->ep_lock);
3087 edge_port->port = port;
3088
3089 usb_set_serial_port_data(port, edge_port);
3090
3091 return 0;
3092}
3093
3094static int edge_port_remove(struct usb_serial_port *port)
3095{
3096 struct edgeport_port *edge_port;
3097
3098 edge_port = usb_get_serial_port_data(port);
3099 kfree(edge_port);
3100
3101 return 0;
3102}
3103
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003104module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Alan Cox03f0dbf2008-07-22 11:16:34 +01003106MODULE_AUTHOR(DRIVER_AUTHOR);
3107MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303109MODULE_FIRMWARE("edgeport/boot.fw");
3110MODULE_FIRMWARE("edgeport/boot2.fw");
3111MODULE_FIRMWARE("edgeport/down.fw");
3112MODULE_FIRMWARE("edgeport/down2.fw");