blob: 1a826973017b30fd5758670fdd4347b2d4bbf7b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Supports the following devices:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
25 *
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
29 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/jiffies.h>
34#include <linux/errno.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_driver.h>
39#include <linux/tty_flip.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/serial.h>
43#include <linux/ioctl.h>
44#include <linux/wait.h>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053045#include <linux/firmware.h>
46#include <linux/ihex.h>
Alan Cox03f0dbf2008-07-22 11:16:34 +010047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
54/*
55 * Version Information
56 */
57#define DRIVER_VERSION "v2.7"
58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59#define DRIVER_DESC "Edgeport USB Serial Driver"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_NAME_LEN 64
62
63#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
66
67/* receive port state */
68enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010069 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75
Alan Cox03f0dbf2008-07-22 11:16:34 +010076/* Transmit Fifo
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
80 */
81struct TxFifo {
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
87};
88
89/* This structure holds all of the local port information */
90struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
93
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010096 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spinlock_t ep_lock;
98
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
105 __u8 validDataMask;
106 __u32 baudRate;
107
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100108 bool open;
109 bool openPending;
110 bool commandPending;
111 bool closePending;
112 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
118
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
121};
122
123
124/* This structure holds all of the individual device information */
125struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700126 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100135 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100139 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100141 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spinlock_t es_lock;
143
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
145
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
147
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
157};
158
159/* baud rate information */
160struct divisor_table_entry {
161 __u32 BaudRate;
162 __u16 Divisor;
163};
164
Alan Cox03f0dbf2008-07-22 11:16:34 +0100165/*
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
168 * MCR.7 = 0.
169 */
170
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100171static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100172 { 50, 4608},
173 { 75, 3072},
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { 150, 1536},
177 { 300, 768},
178 { 600, 384},
179 { 1200, 192},
180 { 1800, 128},
181 { 2400, 96},
182 { 4800, 48},
183 { 7200, 32},
184 { 9600, 24},
185 { 14400, 16},
186 { 19200, 12},
187 { 38400, 6},
188 { 57600, 4},
189 { 115200, 2},
190 { 230400, 1},
191};
192
193/* local variables */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030194static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800196/* Number of outstanding Command Write Urbs */
197static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199
200/* local function prototypes */
201
202/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100203static void edge_interrupt_callback(struct urb *urb);
204static void edge_bulk_in_callback(struct urb *urb);
205static void edge_bulk_out_data_callback(struct urb *urb);
206static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700209static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100210static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100211static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
212 const unsigned char *buf, int count);
213static int edge_write_room(struct tty_struct *tty);
214static int edge_chars_in_buffer(struct tty_struct *tty);
215static void edge_throttle(struct tty_struct *tty);
216static void edge_unthrottle(struct tty_struct *tty);
217static void edge_set_termios(struct tty_struct *tty,
218 struct usb_serial_port *port,
219 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000220static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100221 unsigned int cmd, unsigned long arg);
222static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000223static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000224static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100225 unsigned int set, unsigned int clear);
Alan Cox0bca1b92010-09-16 18:21:40 +0100226static int edge_get_icount(struct tty_struct *tty,
227 struct serial_icounter_struct *icount);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100228static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400229static void edge_disconnect(struct usb_serial *serial);
230static void edge_release(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#include "io_tables.h" /* all of the devices that this driver supports */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Alan Cox03f0dbf2008-07-22 11:16:34 +0100236static void process_rcvd_data(struct edgeport_serial *edge_serial,
237 unsigned char *buffer, __u16 bufferLength);
238static void process_rcvd_status(struct edgeport_serial *edge_serial,
239 __u8 byte2, __u8 byte3);
240static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241 unsigned char *data, int length);
242static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
243static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
244 __u8 lsr, __u8 data);
245static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
246 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700247static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100248static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
249 int baudRate);
250static void change_port_settings(struct tty_struct *tty,
251 struct edgeport_port *edge_port,
252 struct ktermios *old_termios);
253static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
254 __u8 regNum, __u8 regValue);
255static int write_cmd_usb(struct edgeport_port *edge_port,
256 unsigned char *buffer, int writeLength);
257static void send_more_port_data(struct edgeport_serial *edge_serial,
258 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Alan Cox03f0dbf2008-07-22 11:16:34 +0100260static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
261 __u16 length, const __u8 *data);
262static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
263 __u16 length, __u8 *data);
264static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
265 __u16 length, const __u8 *data);
266static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
267static void get_boot_desc(struct edgeport_serial *edge_serial);
268static void load_application_firmware(struct edgeport_serial *edge_serial);
269
270static void unicode_to_ascii(char *string, int buflen,
271 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273
Alan Cox03f0dbf2008-07-22 11:16:34 +0100274/* ************************************************************************ */
275/* ************************************************************************ */
276/* ************************************************************************ */
277/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/************************************************************************
280 * *
281 * update_edgeport_E2PROM() Compare current versions of *
282 * Boot ROM and Manufacture *
283 * Descriptors with versions *
284 * embedded in this driver *
285 * *
286 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100287static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700289 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 __u32 BootCurVer;
291 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530292 __u8 BootMajorVersion;
293 __u8 BootMinorVersion;
294 __u16 BootBuildNumber;
295 __u32 Bootaddr;
296 const struct ihex_binrec *rec;
297 const struct firmware *fw;
298 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int response;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100302 case EDGE_DOWNLOAD_FILE_I930:
303 fw_name = "edgeport/boot.fw";
304 break;
305 case EDGE_DOWNLOAD_FILE_80251:
306 fw_name = "edgeport/boot2.fw";
307 break;
308 default:
309 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530312 response = request_ihex_firmware(&fw, fw_name,
313 &edge_serial->serial->dev->dev);
314 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700315 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530316 fw_name, response);
317 return;
318 }
319
320 rec = (const struct ihex_binrec *)fw->data;
321 BootMajorVersion = rec->data[0];
322 BootMinorVersion = rec->data[1];
323 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
324
Alan Cox03f0dbf2008-07-22 11:16:34 +0100325 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
327 (edge_serial->boot_descriptor.MinorVersion << 16) +
328 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
329
330 BootNewVer = (BootMajorVersion << 24) +
331 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530332 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700334 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 edge_serial->boot_descriptor.MajorVersion,
336 edge_serial->boot_descriptor.MinorVersion,
337 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
338
339
340 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700341 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 edge_serial->boot_descriptor.MajorVersion,
343 edge_serial->boot_descriptor.MinorVersion,
344 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530345 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700347 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530349 for (rec = ihex_next_binrec(rec); rec;
350 rec = ihex_next_binrec(rec)) {
351 Bootaddr = be32_to_cpu(rec->addr);
352 response = rom_write(edge_serial->serial,
353 Bootaddr >> 16,
354 Bootaddr & 0xFFFF,
355 be16_to_cpu(rec->len),
356 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530358 dev_err(&edge_serial->serial->dev->dev,
359 "rom_write failed (%x, %x, %d)\n",
360 Bootaddr >> 16, Bootaddr & 0xFFFF,
361 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 }
364 }
365 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700366 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530368 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#if 0
372/************************************************************************
373 *
374 * Get string descriptor from device
375 *
376 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100377static int get_string_desc(struct usb_device *dev, int Id,
378 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct usb_string_descriptor StringDesc;
381 struct usb_string_descriptor *pStringDesc;
382
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700383 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Alan Cox03f0dbf2008-07-22 11:16:34 +0100385 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
386 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alan Cox03f0dbf2008-07-22 11:16:34 +0100389 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
390 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Alan Cox03f0dbf2008-07-22 11:16:34 +0100393 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
394 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 kfree(pStringDesc);
396 return -1;
397 }
398
399 *pRetDesc = pStringDesc;
400 return 0;
401}
402#endif
403
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700404static void dump_product_info(struct edgeport_serial *edge_serial,
405 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800406{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700407 struct device *dev = &edge_serial->serial->dev->dev;
408
Alan Cox03f0dbf2008-07-22 11:16:34 +0100409 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700410 dev_dbg(dev, "**Product Information:\n");
411 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
412 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
413 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
414 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
415 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
416 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
417 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
418 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
419 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
420 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
421 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
422 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
423 product_info->BootMajorVersion,
424 product_info->BootMinorVersion,
425 le16_to_cpu(product_info->BootBuildNumber));
426 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
427 product_info->FirmwareMajorVersion,
428 product_info->FirmwareMinorVersion,
429 le16_to_cpu(product_info->FirmwareBuildNumber));
430 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
431 product_info->ManufactureDescDate[0],
432 product_info->ManufactureDescDate[1],
433 product_info->ManufactureDescDate[2]+1900);
434 dev_dbg(dev, " iDownloadFile 0x%x\n",
435 product_info->iDownloadFile);
436 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static void get_product_info(struct edgeport_serial *edge_serial)
440{
441 struct edgeport_product_info *product_info = &edge_serial->product_info;
442
Alan Cox03f0dbf2008-07-22 11:16:34 +0100443 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Cox03f0dbf2008-07-22 11:16:34 +0100445 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
446 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
447 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Cox03f0dbf2008-07-22 11:16:34 +0100449 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
450 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
451 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
452 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Alan Cox03f0dbf2008-07-22 11:16:34 +0100454 product_info->BootMajorVersion =
455 edge_serial->boot_descriptor.MajorVersion;
456 product_info->BootMinorVersion =
457 edge_serial->boot_descriptor.MinorVersion;
458 product_info->BootBuildNumber =
459 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alan Cox03f0dbf2008-07-22 11:16:34 +0100461 memcpy(product_info->ManufactureDescDate,
462 edge_serial->manuf_descriptor.DescDate,
463 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alan Cox03f0dbf2008-07-22 11:16:34 +0100465 /* check if this is 2nd generation hardware */
466 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
467 & ION_DEVICE_ID_80251_NETCHIP)
468 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
469 else
470 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700471
Alan Cox03f0dbf2008-07-22 11:16:34 +0100472 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100474 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475 case ION_DEVICE_ID_EDGEPORT_4T:
476 case ION_DEVICE_ID_EDGEPORT_4:
477 case ION_DEVICE_ID_EDGEPORT_2:
478 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479 case ION_DEVICE_ID_EDGEPORT_8:
480 case ION_DEVICE_ID_EDGEPORT_421:
481 case ION_DEVICE_ID_EDGEPORT_21:
482 case ION_DEVICE_ID_EDGEPORT_2_DIN:
483 case ION_DEVICE_ID_EDGEPORT_4_DIN:
484 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485 product_info->IsRS232 = 1;
486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alan Cox03f0dbf2008-07-22 11:16:34 +0100488 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
489 product_info->IsRS422 = 1;
490 product_info->IsRS485 = 1;
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alan Cox03f0dbf2008-07-22 11:16:34 +0100493 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
494 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
495 product_info->IsRS422 = 1;
496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700499 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800502static int get_epic_descriptor(struct edgeport_serial *ep)
503{
504 int result;
505 struct usb_serial *serial = ep->serial;
506 struct edgeport_product_info *product_info = &ep->product_info;
507 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
508 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700509 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800510
511 ep->is_epic = 0;
512 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
513 USB_REQUEST_ION_GET_EPIC_DESC,
514 0xC0, 0x00, 0x00,
515 &ep->epic_descriptor,
516 sizeof(struct edge_compatibility_descriptor),
517 300);
518
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800519 if (result > 0) {
520 ep->is_epic = 1;
521 memset(product_info, 0, sizeof(struct edgeport_product_info));
522
Alan Cox03f0dbf2008-07-22 11:16:34 +0100523 product_info->NumPorts = epic->NumPorts;
524 product_info->ProdInfoVer = 0;
525 product_info->FirmwareMajorVersion = epic->MajorVersion;
526 product_info->FirmwareMinorVersion = epic->MinorVersion;
527 product_info->FirmwareBuildNumber = epic->BuildNumber;
528 product_info->iDownloadFile = epic->iDownloadFile;
529 product_info->EpicVer = epic->EpicVer;
530 product_info->Epic = epic->Supports;
531 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700532 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800533
534 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700535 dev_dbg(dev, "**EPIC descriptor:\n");
536 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
543 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
544 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
545 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
546 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
547 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
548 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800549 }
550
551 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554
555/************************************************************************/
556/************************************************************************/
557/* U S B C A L L B A C K F U N C T I O N S */
558/* U S B C A L L B A C K F U N C T I O N S */
559/************************************************************************/
560/************************************************************************/
561
562/*****************************************************************************
563 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100564 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * interrupt endpoint.
566 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100567static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700569 struct edgeport_serial *edge_serial = urb->context;
570 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct edgeport_port *edge_port;
572 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100573 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unsigned char *data = urb->transfer_buffer;
575 int length = urb->actual_length;
576 int bytes_avail;
577 int position;
578 int txCredits;
579 int portNumber;
580 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700581 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700583 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 case 0:
585 /* success */
586 break;
587 case -ECONNRESET:
588 case -ENOENT:
589 case -ESHUTDOWN:
590 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700591 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700594 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto exit;
596 }
597
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700598 dev = &edge_serial->serial->dev->dev;
599
Alan Cox03f0dbf2008-07-22 11:16:34 +0100600 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (length) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700602 usb_serial_debug_data(debug, dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (length > 1) {
605 bytes_avail = data[0] | (data[1] << 8);
606 if (bytes_avail) {
607 spin_lock(&edge_serial->es_lock);
608 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700609 dev_dbg(dev,
610 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
611 __func__, bytes_avail,
612 edge_serial->rxBytesAvail,
613 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (edge_serial->rxBytesAvail > 0 &&
616 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700617 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100618 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Alan Cox03f0dbf2008-07-22 11:16:34 +0100620 /* we have pending bytes on the
621 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
623 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700624 dev_err(dev,
625 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
626 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100627 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
630 spin_unlock(&edge_serial->es_lock);
631 }
632 }
633 /* grab the txcredits for the ports if available */
634 position = 2;
635 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100636 while ((position < length) &&
637 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 txCredits = data[position] | (data[position+1] << 8);
639 if (txCredits) {
640 port = edge_serial->serial->port[portNumber];
641 edge_port = usb_get_serial_port_data(port);
642 if (edge_port->open) {
643 spin_lock(&edge_port->ep_lock);
644 edge_port->txCredits += txCredits;
645 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700646 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
647 __func__, portNumber,
648 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Alan Cox03f0dbf2008-07-22 11:16:34 +0100650 /* tell the tty driver that something
651 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100652 tty = tty_port_tty_get(
653 &edge_port->port->port);
654 if (tty) {
655 tty_wakeup(tty);
656 tty_kref_put(tty);
657 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100658 /* Since we have more credit, check
659 if more data can be sent */
660 send_more_port_data(edge_serial,
661 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 }
664 position += 2;
665 ++portNumber;
666 }
667 }
668
669exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100670 result = usb_submit_urb(urb, GFP_ATOMIC);
671 if (result)
672 dev_err(&urb->dev->dev,
673 "%s - Error %d submitting control urb\n",
674 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677
678/*****************************************************************************
679 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100680 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * bulk in endpoint.
682 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100683static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Ming Leicdc97792008-02-24 18:41:47 +0800685 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700686 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700688 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700690 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700692 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700693 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
694 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100695 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return;
697 }
698
699 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700700 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100701 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return;
703 }
704
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700705 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 raw_data_length = urb->actual_length;
707
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700708 usb_serial_debug_data(debug, dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 spin_lock(&edge_serial->es_lock);
711
712 /* decrement our rxBytes available by the number that we just got */
713 edge_serial->rxBytesAvail -= raw_data_length;
714
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700715 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
716 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Alan Cox03f0dbf2008-07-22 11:16:34 +0100718 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* check to see if there's any more data for us to read */
721 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700722 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700723 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
724 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700725 dev_err(dev,
726 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
727 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100728 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100731 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
734 spin_unlock(&edge_serial->es_lock);
735}
736
737
738/*****************************************************************************
739 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100740 * this is the callback function for when we have finished sending
741 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100743static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Ming Leicdc97792008-02-24 18:41:47 +0800745 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700747 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700749 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700750 dev_dbg(&urb->dev->dev,
751 "%s - nonzero write bulk status received: %d\n",
752 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Alan Cox4a90f092008-10-13 10:39:46 +0100755 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100758 /* let the tty driver wakeup if it has a special
759 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 tty_wakeup(tty);
761 }
Alan Cox4a90f092008-10-13 10:39:46 +0100762 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Alan Cox03f0dbf2008-07-22 11:16:34 +0100764 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100765 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Alan Cox03f0dbf2008-07-22 11:16:34 +0100767 /* Check if more data needs to be sent */
768 send_more_port_data((struct edgeport_serial *)
769 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772
773/*****************************************************************************
774 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100775 * this is the callback function for when we have finished sending a
776 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100778static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Ming Leicdc97792008-02-24 18:41:47 +0800780 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct tty_struct *tty;
782 int status = urb->status;
783
Oliver Neukum96c706e2007-03-15 15:27:17 +0100784 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700785 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
786 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788
789 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700790 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100793 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700796 dev_dbg(&urb->dev->dev,
797 "%s - nonzero write bulk status received: %d\n",
798 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return;
800 }
801
802 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100803 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* tell the tty driver that something has changed */
806 if (tty && edge_port->open)
807 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100808 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100811 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 wake_up(&edge_port->wait_command);
813}
814
815
816/*****************************************************************************
817 * Driver tty interface functions
818 *****************************************************************************/
819
820/*****************************************************************************
821 * SerialOpen
822 * this function is called by the tty driver when a port is opened
823 * If successful, we return 0
824 * Otherwise we return a negative error number.
825 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700826static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700829 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct usb_serial *serial;
831 struct edgeport_serial *edge_serial;
832 int response;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (edge_port == NULL)
835 return -ENODEV;
836
Alan Cox03f0dbf2008-07-22 11:16:34 +0100837 /* see if we've set up our endpoint info yet (can't set it up
838 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 serial = port->serial;
840 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100841 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (edge_serial->interrupt_in_buffer == NULL) {
844 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100847 edge_serial->interrupt_in_buffer =
848 port0->interrupt_in_buffer;
849 edge_serial->interrupt_in_endpoint =
850 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
852 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100853 edge_serial->bulk_in_endpoint =
854 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100856 edge_serial->bulk_out_endpoint =
857 port0->bulk_out_endpointAddress;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* set up our interrupt urb */
860 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100861 serial->dev,
862 usb_rcvintpipe(serial->dev,
863 port0->interrupt_in_endpointAddress),
864 port0->interrupt_in_buffer,
865 edge_serial->interrupt_read_urb->transfer_buffer_length,
866 edge_interrupt_callback, edge_serial,
867 edge_serial->interrupt_read_urb->interval);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* set up our bulk in urb */
870 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100871 usb_rcvbulkpipe(serial->dev,
872 port0->bulk_in_endpointAddress),
873 port0->bulk_in_buffer,
874 edge_serial->read_urb->transfer_buffer_length,
875 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100876 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100879 * this interrupt will continue as long
880 * as the edgeport is connected */
881 response = usb_submit_urb(edge_serial->interrupt_read_urb,
882 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700884 dev_err(dev, "%s - Error %d submitting control urb\n",
885 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* initialize our wait queues */
890 init_waitqueue_head(&edge_port->wait_open);
891 init_waitqueue_head(&edge_port->wait_chase);
892 init_waitqueue_head(&edge_port->delta_msr_wait);
893 init_waitqueue_head(&edge_port->wait_command);
894
895 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100896 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100899 edge_port->txCredits = 0; /* Can't send any data yet */
900 /* Must always set this bit to enable ints! */
901 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100902 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100905 edge_port->openPending = true;
906 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100907 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700910 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100911 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -ENODEV;
913 }
914
915 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100916 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
917 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100919 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700921 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100922 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENODEV;
924 }
925
926 /* create the txfifo */
927 edge_port->txfifo.head = 0;
928 edge_port->txfifo.tail = 0;
929 edge_port->txfifo.count = 0;
930 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100931 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700934 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100935 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -ENOMEM;
937 }
938
939 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100940 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100941 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700944 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100945 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -ENOMEM;
947 }
948
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700949 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
950 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 return 0;
953}
954
955
956/************************************************************************
957 *
958 * block_until_chase_response
959 *
960 * This function will block the close until one of the following:
961 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800962 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
964 *
965 ************************************************************************/
966static void block_until_chase_response(struct edgeport_port *edge_port)
967{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700968 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 DEFINE_WAIT(wait);
970 __u16 lastCredits;
971 int timeout = 1*HZ;
972 int loop = 10;
973
974 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100975 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 lastCredits = edge_port->txCredits;
977
Alan Cox03f0dbf2008-07-22 11:16:34 +0100978 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100979 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700980 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Alan Cox03f0dbf2008-07-22 11:16:34 +0100982 /* did we get all of our credit back? */
983 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700984 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return;
986 }
987 }
988
Alan Cox03f0dbf2008-07-22 11:16:34 +0100989 /* Block the thread for a while */
990 prepare_to_wait(&edge_port->wait_chase, &wait,
991 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 schedule_timeout(timeout);
993 finish_wait(&edge_port->wait_chase, &wait);
994
995 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100996 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 loop--;
998 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100999 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001000 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return;
1002 }
1003 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001004 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001005 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001006 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 loop = 10;
1008 }
1009 }
1010}
1011
1012
1013/************************************************************************
1014 *
1015 * block_until_tx_empty
1016 *
1017 * This function will block the close until one of the following:
1018 * 1. TX count are 0
1019 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001020 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *
1022 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001023static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001025 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 DEFINE_WAIT(wait);
1027 struct TxFifo *fifo = &edge_port->txfifo;
1028 __u32 lastCount;
1029 int timeout = HZ/10;
1030 int loop = 30;
1031
1032 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001033 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 lastCount = fifo->count;
1035
Alan Cox03f0dbf2008-07-22 11:16:34 +01001036 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001038 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return;
1040 }
1041
Alan Cox03f0dbf2008-07-22 11:16:34 +01001042 /* Block the thread for a while */
1043 prepare_to_wait(&edge_port->wait_chase, &wait,
1044 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 schedule_timeout(timeout);
1046 finish_wait(&edge_port->wait_chase, &wait);
1047
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001048 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001051 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 loop--;
1053 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001054 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return;
1056 }
1057 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001058 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 loop = 30;
1060 }
1061 }
1062}
1063
1064
1065/*****************************************************************************
1066 * edge_close
1067 * this function is called by the tty driver when a port is closed
1068 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001069static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct edgeport_serial *edge_serial;
1072 struct edgeport_port *edge_port;
1073 int status;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 edge_serial = usb_get_serial_data(port->serial);
1076 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001077 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001079
1080 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 block_until_tx_empty(edge_port);
1082
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001083 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001085 if ((!edge_serial->is_epic) ||
1086 ((edge_serial->is_epic) &&
1087 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1088 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001089 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001091 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001092 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1093 if (status == 0)
1094 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001095 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001096 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001097 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001100 if ((!edge_serial->is_epic) ||
1101 ((edge_serial->is_epic) &&
1102 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1103 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001104 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001105 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Alan Cox03f0dbf2008-07-22 11:16:34 +01001108 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001109 edge_port->closePending = false;
1110 edge_port->open = false;
1111 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001113 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001116 /* if this urb had a transfer buffer already
1117 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001118 kfree(edge_port->write_urb->transfer_buffer);
1119 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 edge_port->write_urb = NULL;
1121 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001122 kfree(edge_port->txfifo.fifo);
1123 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126/*****************************************************************************
1127 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001128 * this function is called by the tty driver when data should be written
1129 * to the port.
1130 * If successful, we return the number of bytes written, otherwise we
1131 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001133static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1134 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1137 struct TxFifo *fifo;
1138 int copySize;
1139 int bytesleft;
1140 int firsthalf;
1141 int secondhalf;
1142 unsigned long flags;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (edge_port == NULL)
1145 return -ENODEV;
1146
Alan Cox03f0dbf2008-07-22 11:16:34 +01001147 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 fifo = &edge_port->txfifo;
1149
1150 spin_lock_irqsave(&edge_port->ep_lock, flags);
1151
Alan Cox03f0dbf2008-07-22 11:16:34 +01001152 /* calculate number of bytes to put in fifo */
1153 copySize = min((unsigned int)count,
1154 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001156 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1157 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001158 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Alan Cox03f0dbf2008-07-22 11:16:34 +01001160 /* catch writes of 0 bytes which the tty driver likes to give us,
1161 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001163 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto finish_write;
1165 }
1166
Alan Cox03f0dbf2008-07-22 11:16:34 +01001167 /* queue the data
1168 * since we can never overflow the buffer we do not have to check for a
1169 * full condition
1170 *
1171 * the copy is done is two parts -- first fill to the end of the buffer
1172 * then copy the reset from the start of the buffer
1173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001175 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001176 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1177 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* now copy our data */
1180 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001181 usb_serial_debug_data(debug, &port->dev, __func__,
1182 firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Alan Cox03f0dbf2008-07-22 11:16:34 +01001184 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 fifo->head += firsthalf;
1186 fifo->count += firsthalf;
1187
Alan Cox03f0dbf2008-07-22 11:16:34 +01001188 /* wrap the index */
1189 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 secondhalf = copySize-firsthalf;
1193
1194 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001195 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001197 usb_serial_debug_data(debug, &port->dev, __func__,
1198 secondhalf, &fifo->fifo[fifo->head]);
1199 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 fifo->count += secondhalf;
1201 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001202 /* No need to check for wrap since we can not get to end of
1203 * the fifo in this part
1204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207finish_write:
1208 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1209
Alan Cox03f0dbf2008-07-22 11:16:34 +01001210 send_more_port_data((struct edgeport_serial *)
1211 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001213 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1214 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Alan Cox03f0dbf2008-07-22 11:16:34 +01001216 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219
1220/************************************************************************
1221 *
1222 * send_more_port_data()
1223 *
1224 * This routine attempts to write additional UART transmit data
1225 * to a port over the USB bulk pipe. It is called (1) when new
1226 * data has been written to a port's TxBuffer from higher layers
1227 * (2) when the peripheral sends us additional TxCredits indicating
1228 * that it can accept more Tx data for a given port; and (3) when
1229 * a bulk write completes successfully and we want to see if we
1230 * can transmit more.
1231 *
1232 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001233static void send_more_port_data(struct edgeport_serial *edge_serial,
1234 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001237 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct urb *urb;
1239 unsigned char *buffer;
1240 int status;
1241 int count;
1242 int bytesleft;
1243 int firsthalf;
1244 int secondhalf;
1245 unsigned long flags;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 spin_lock_irqsave(&edge_port->ep_lock, flags);
1248
1249 if (edge_port->write_in_progress ||
1250 !edge_port->open ||
1251 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001252 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1253 __func__, edge_port->port->number,
1254 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 goto exit_send;
1256 }
1257
Alan Cox03f0dbf2008-07-22 11:16:34 +01001258 /* since the amount of data in the fifo will always fit into the
1259 * edgeport buffer we do not need to check the write length
1260 *
1261 * Do we have enough credits for this port to make it worthwhile
1262 * to bother queueing a write. If it's too small, say a few bytes,
1263 * it's better to wait for more credits so we can do a larger write.
1264 */
1265 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 -07001266 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001267 __func__, edge_port->port->number, fifo->count,
1268 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto exit_send;
1270 }
1271
Alan Cox03f0dbf2008-07-22 11:16:34 +01001272 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001273 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Alan Cox03f0dbf2008-07-22 11:16:34 +01001275 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 urb = edge_port->write_urb;
1277
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001278 /* make sure transfer buffer is freed */
1279 kfree(urb->transfer_buffer);
1280 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Alan Cox03f0dbf2008-07-22 11:16:34 +01001282 /* build the data header for the buffer and port that we are about
1283 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001285 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001287 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001288 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001289 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto exit_send;
1291 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001292 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1293 - edge_port->port->serial->minor, count);
1294 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1295 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* now copy our data */
1298 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001299 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1301 fifo->tail += firsthalf;
1302 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001303 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 secondhalf = count-firsthalf;
1307 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001308 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1309 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 fifo->tail += secondhalf;
1311 fifo->count -= secondhalf;
1312 }
1313
1314 if (count)
Alan Cox03f0dbf2008-07-22 11:16:34 +01001315 usb_serial_debug_data(debug, &edge_port->port->dev,
1316 __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001319 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1320 usb_sndbulkpipe(edge_serial->serial->dev,
1321 edge_serial->bulk_out_endpoint),
1322 buffer, count+2,
1323 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* decrement the number of credits we have by the number we just sent */
1326 edge_port->txCredits -= count;
1327 edge_port->icount.tx += count;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 status = usb_submit_urb(urb, GFP_ATOMIC);
1330 if (status) {
1331 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001332 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001333 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1334 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001335 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* revert the credits as something bad happened. */
1338 edge_port->txCredits += count;
1339 edge_port->icount.tx -= count;
1340 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001341 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1342 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344exit_send:
1345 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1346}
1347
1348
1349/*****************************************************************************
1350 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001351 * this function is called by the tty driver when it wants to know how
1352 * many bytes of data we can accept for a specific port. If successful,
1353 * we return the amount of room that we have for this port (the txCredits)
1354 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001356static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Alan Cox95da3102008-07-22 11:09:07 +01001358 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1360 int room;
1361 unsigned long flags;
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001364 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001365 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001369 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Alan Cox03f0dbf2008-07-22 11:16:34 +01001373 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_lock_irqsave(&edge_port->ep_lock, flags);
1375 room = edge_port->txCredits - edge_port->txfifo.count;
1376 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1377
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001378 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return room;
1380}
1381
1382
1383/*****************************************************************************
1384 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001385 * this function is called by the tty driver when it wants to know how
1386 * many bytes of data we currently have outstanding in the port (data that
1387 * has been written, but hasn't made it out the port yet)
1388 * If successful, we return the number of bytes left to be written in the
1389 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * Otherwise we return a negative error number.
1391 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001392static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Alan Cox95da3102008-07-22 11:09:07 +01001394 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1396 int num_chars;
1397 unsigned long flags;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001400 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001401 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001405 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
1409 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001410 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1411 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1413 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001414 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1415 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 return num_chars;
1419}
1420
1421
1422/*****************************************************************************
1423 * SerialThrottle
1424 * this function is called by the tty driver when it wants to stop the data
1425 * being read from the port.
1426 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001427static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Alan Cox95da3102008-07-22 11:09:07 +01001429 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int status;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (edge_port == NULL)
1434 return;
1435
1436 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001437 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return;
1439 }
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* if we are implementing XON/XOFF, send the stop character */
1442 if (I_IXOFF(tty)) {
1443 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001444 status = edge_write(tty, port, &stop_char, 1);
1445 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
1449 /* if we are implementing RTS/CTS, toggle that line */
1450 if (tty->termios->c_cflag & CRTSCTS) {
1451 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001452 status = send_cmd_write_uart_register(edge_port, MCR,
1453 edge_port->shadowMCR);
1454 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
1459
1460/*****************************************************************************
1461 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001462 * this function is called by the tty driver when it wants to resume the
1463 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001465static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Alan Cox95da3102008-07-22 11:09:07 +01001467 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int status;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (edge_port == NULL)
1472 return;
1473
1474 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001475 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return;
1477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 /* if we are implementing XON/XOFF, send the start character */
1480 if (I_IXOFF(tty)) {
1481 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001482 status = edge_write(tty, port, &start_char, 1);
1483 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* if we are implementing RTS/CTS, toggle that line */
1487 if (tty->termios->c_cflag & CRTSCTS) {
1488 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001489 send_cmd_write_uart_register(edge_port, MCR,
1490 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494
1495/*****************************************************************************
1496 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001497 * this function is called by the tty driver when it wants to change
1498 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001500static void edge_set_termios(struct tty_struct *tty,
1501 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 unsigned int cflag;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 cflag = tty->termios->c_cflag;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001507 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
1508 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 -07001509
1510 if (edge_port == NULL)
1511 return;
1512
1513 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001514 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return;
1516 }
1517
1518 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001519 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522
1523/*****************************************************************************
1524 * get_lsr_info - get line status register info
1525 *
1526 * Purpose: Let user call ioctl() to get info when the UART physically
1527 * is emptied. On bus types like RS485, the transmitter must
1528 * release the bus after transmitting. This must be done when
1529 * the transmit shift register is empty, not be done when the
1530 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001531 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001533static int get_lsr_info(struct edgeport_port *edge_port,
1534 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 unsigned int result = 0;
1537 unsigned long flags;
1538
1539 spin_lock_irqsave(&edge_port->ep_lock, flags);
1540 if (edge_port->maxTxCredits == edge_port->txCredits &&
1541 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001542 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 result = TIOCSER_TEMT;
1544 }
1545 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1546
1547 if (copy_to_user(value, &result, sizeof(int)))
1548 return -EFAULT;
1549 return 0;
1550}
1551
Alan Cox20b9d172011-02-14 16:26:50 +00001552static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001553 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Alan Cox95da3102008-07-22 11:09:07 +01001555 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1557 unsigned int mcr;
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 mcr = edge_port->shadowMCR;
1560 if (set & TIOCM_RTS)
1561 mcr |= MCR_RTS;
1562 if (set & TIOCM_DTR)
1563 mcr |= MCR_DTR;
1564 if (set & TIOCM_LOOP)
1565 mcr |= MCR_LOOPBACK;
1566
1567 if (clear & TIOCM_RTS)
1568 mcr &= ~MCR_RTS;
1569 if (clear & TIOCM_DTR)
1570 mcr &= ~MCR_DTR;
1571 if (clear & TIOCM_LOOP)
1572 mcr &= ~MCR_LOOPBACK;
1573
1574 edge_port->shadowMCR = mcr;
1575
1576 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1577
1578 return 0;
1579}
1580
Alan Cox60b33c12011-02-14 16:26:14 +00001581static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Alan Cox95da3102008-07-22 11:09:07 +01001583 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1585 unsigned int result = 0;
1586 unsigned int msr;
1587 unsigned int mcr;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 msr = edge_port->shadowMSR;
1590 mcr = edge_port->shadowMCR;
1591 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1592 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1593 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1594 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1595 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1596 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return result;
1599}
1600
Alan Cox0bca1b92010-09-16 18:21:40 +01001601static int edge_get_icount(struct tty_struct *tty,
1602 struct serial_icounter_struct *icount)
1603{
1604 struct usb_serial_port *port = tty->driver_data;
1605 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1606 struct async_icount cnow;
1607 cnow = edge_port->icount;
1608
1609 icount->cts = cnow.cts;
1610 icount->dsr = cnow.dsr;
1611 icount->rng = cnow.rng;
1612 icount->dcd = cnow.dcd;
1613 icount->rx = cnow.rx;
1614 icount->tx = cnow.tx;
1615 icount->frame = cnow.frame;
1616 icount->overrun = cnow.overrun;
1617 icount->parity = cnow.parity;
1618 icount->brk = cnow.brk;
1619 icount->buf_overrun = cnow.buf_overrun;
1620
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001621 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1622 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001623 return 0;
1624}
1625
Alan Cox03f0dbf2008-07-22 11:16:34 +01001626static int get_serial_info(struct edgeport_port *edge_port,
1627 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct serial_struct tmp;
1630
1631 if (!retinfo)
1632 return -EFAULT;
1633
1634 memset(&tmp, 0, sizeof(tmp));
1635
1636 tmp.type = PORT_16550A;
1637 tmp.line = edge_port->port->serial->minor;
1638 tmp.port = edge_port->port->number;
1639 tmp.irq = 0;
1640 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1641 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1642 tmp.baud_base = 9600;
1643 tmp.close_delay = 5*HZ;
1644 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1647 return -EFAULT;
1648 return 0;
1649}
1650
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652/*****************************************************************************
1653 * SerialIoctl
1654 * this function handles any ioctl calls to the driver
1655 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001656static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001657 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Alan Cox95da3102008-07-22 11:09:07 +01001659 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 DEFINE_WAIT(wait);
1661 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1662 struct async_icount cnow;
1663 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001665 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001668 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001669 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001670 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Alan Cox03f0dbf2008-07-22 11:16:34 +01001672 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001673 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001674 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Alan Cox03f0dbf2008-07-22 11:16:34 +01001676 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001677 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001678 cprev = edge_port->icount;
1679 while (1) {
1680 prepare_to_wait(&edge_port->delta_msr_wait,
1681 &wait, TASK_INTERRUPTIBLE);
1682 schedule();
1683 finish_wait(&edge_port->delta_msr_wait, &wait);
1684 /* see if a signal did it */
1685 if (signal_pending(current))
1686 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001688 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1689 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1690 return -EIO; /* no change => error */
1691 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1692 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1693 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1694 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1695 return 0;
1696 }
1697 cprev = cnow;
1698 }
1699 /* NOTREACHED */
1700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return -ENOIOCTLCMD;
1704}
1705
1706
1707/*****************************************************************************
1708 * SerialBreak
1709 * this function sends a break to the port
1710 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001711static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Alan Cox95da3102008-07-22 11:09:07 +01001713 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001715 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int status;
1717
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001718 if ((!edge_serial->is_epic) ||
1719 ((edge_serial->is_epic) &&
1720 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1721 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001722 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001724 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001725 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001726 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001727 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001728 block_until_chase_response(edge_port);
1729 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001730 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001734 if ((!edge_serial->is_epic) ||
1735 ((edge_serial->is_epic) &&
1736 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1737 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001738 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001739 status = send_iosp_ext_cmd(edge_port,
1740 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001741 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001742 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001743 status = send_iosp_ext_cmd(edge_port,
1744 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001745 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001746 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001747 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001748 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752
1753/*****************************************************************************
1754 * process_rcvd_data
1755 * this function handles the data received on the bulk in pipe.
1756 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001757static void process_rcvd_data(struct edgeport_serial *edge_serial,
1758 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001760 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct usb_serial_port *port;
1762 struct edgeport_port *edge_port;
1763 struct tty_struct *tty;
1764 __u16 lastBufferLength;
1765 __u16 rxLen;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 lastBufferLength = bufferLength + 1;
1768
1769 while (bufferLength > 0) {
1770 /* failsafe incase we get a message that we don't understand */
1771 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001772 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
1774 }
1775 lastBufferLength = bufferLength;
1776
1777 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001778 case EXPECT_HDR1:
1779 edge_serial->rxHeader1 = *buffer;
1780 ++buffer;
1781 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Alan Cox03f0dbf2008-07-22 11:16:34 +01001783 if (bufferLength == 0) {
1784 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001786 }
1787 /* otherwise, drop on through */
1788 case EXPECT_HDR2:
1789 edge_serial->rxHeader2 = *buffer;
1790 ++buffer;
1791 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001793 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1794 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001795 /* Process depending on whether this header is
1796 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Alan Cox03f0dbf2008-07-22 11:16:34 +01001798 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1799 /* Decode this status header and go to
1800 * EXPECT_HDR1 (if we can process the status
1801 * with only 2 bytes), or go to EXPECT_HDR3 to
1802 * get the third byte. */
1803 edge_serial->rxPort =
1804 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1805 edge_serial->rxStatusCode =
1806 IOSP_GET_STATUS_CODE(
1807 edge_serial->rxHeader1);
1808
1809 if (!IOSP_STATUS_IS_2BYTE(
1810 edge_serial->rxStatusCode)) {
1811 /* This status needs additional bytes.
1812 * Save what we have and then wait for
1813 * more data.
1814 */
1815 edge_serial->rxStatusParam
1816 = edge_serial->rxHeader2;
1817 edge_serial->rxState = EXPECT_HDR3;
1818 break;
1819 }
1820 /* We have all the header bytes, process the
1821 status now */
1822 process_rcvd_status(edge_serial,
1823 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 edge_serial->rxState = EXPECT_HDR1;
1825 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001826 } else {
1827 edge_serial->rxPort =
1828 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1829 edge_serial->rxBytesRemaining =
1830 IOSP_GET_HDR_DATA_LEN(
1831 edge_serial->rxHeader1,
1832 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001833 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1834 __func__,
1835 edge_serial->rxPort,
1836 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Alan Cox03f0dbf2008-07-22 11:16:34 +01001838 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1839 * ASSERT(DevExt->RxBytesRemaining <
1840 * IOSP_MAX_DATA_LENGTH);
1841 */
1842
1843 if (bufferLength == 0) {
1844 edge_serial->rxState = EXPECT_DATA;
1845 break;
1846 }
1847 /* Else, drop through */
1848 }
1849 case EXPECT_DATA: /* Expect data */
1850 if (bufferLength < edge_serial->rxBytesRemaining) {
1851 rxLen = bufferLength;
1852 /* Expect data to start next buffer */
1853 edge_serial->rxState = EXPECT_DATA;
1854 } else {
1855 /* BufLen >= RxBytesRemaining */
1856 rxLen = edge_serial->rxBytesRemaining;
1857 /* Start another header next time */
1858 edge_serial->rxState = EXPECT_HDR1;
1859 }
1860
1861 bufferLength -= rxLen;
1862 edge_serial->rxBytesRemaining -= rxLen;
1863
1864 /* spit this data back into the tty driver if this
1865 port is open */
1866 if (rxLen) {
1867 port = edge_serial->serial->port[
1868 edge_serial->rxPort];
1869 edge_port = usb_get_serial_port_data(port);
1870 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001871 tty = tty_port_tty_get(
1872 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001873 if (tty) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001874 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001875 __func__, rxLen, edge_serial->rxPort);
1876 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001877 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001878 }
1879 edge_port->icount.rx += rxLen;
1880 }
1881 buffer += rxLen;
1882 }
1883 break;
1884
1885 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1886 edge_serial->rxHeader3 = *buffer;
1887 ++buffer;
1888 --bufferLength;
1889
1890 /* We have all the header bytes, process the
1891 status now */
1892 process_rcvd_status(edge_serial,
1893 edge_serial->rxStatusParam,
1894 edge_serial->rxHeader3);
1895 edge_serial->rxState = EXPECT_HDR1;
1896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 }
1899}
1900
1901
1902/*****************************************************************************
1903 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001904 * this function handles the any status messages received on the
1905 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001907static void process_rcvd_status(struct edgeport_serial *edge_serial,
1908 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct usb_serial_port *port;
1911 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001912 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001913 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 __u8 code = edge_serial->rxStatusCode;
1915
1916 /* switch the port pointer to the one being currently talked about */
1917 port = edge_serial->serial->port[edge_serial->rxPort];
1918 edge_port = usb_get_serial_port_data(port);
1919 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001920 dev_err(&edge_serial->serial->dev->dev,
1921 "%s - edge_port == NULL for port %d\n",
1922 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return;
1924 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001925 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (code == IOSP_EXT_STATUS) {
1928 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001929 case IOSP_EXT_STATUS_CHASE_RSP:
1930 /* we want to do EXT status regardless of port
1931 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001932 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1933 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001934 /* Currently, the only EXT_STATUS is Chase, so process
1935 * here instead of one more call to one more subroutine
1936 * If/when more EXT_STATUS, there'll be more work to do
1937 * Also, we currently clear flag and close the port
1938 * regardless of content of above's Byte3.
1939 * We could choose to do something else when Byte3 says
1940 * Timeout on Chase from Edgeport, like wait longer in
1941 * block_until_chase_response, but for now we don't.
1942 */
1943 edge_port->chaseResponsePending = false;
1944 wake_up(&edge_port->wait_chase);
1945 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Alan Cox03f0dbf2008-07-22 11:16:34 +01001947 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001948 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1949 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001950 /* Port->RxCheckRsp = true; */
1951 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 }
1954
1955 if (code == IOSP_STATUS_OPEN_RSP) {
1956 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1957 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001958 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1959 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001960 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Alan Cox03f0dbf2008-07-22 11:16:34 +01001962 /* send the current line settings to the port so we are
1963 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001964 tty = tty_port_tty_get(&edge_port->port->port);
1965 if (tty) {
1966 change_port_settings(tty,
1967 edge_port, tty->termios);
1968 tty_kref_put(tty);
1969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001972 edge_port->openPending = false;
1973 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 wake_up(&edge_port->wait_open);
1975 return;
1976 }
1977
Alan Cox03f0dbf2008-07-22 11:16:34 +01001978 /* If port is closed, silently discard all rcvd status. We can
1979 * have cases where buffered status is received AFTER the close
1980 * port command is sent to the Edgeport.
1981 */
1982 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001986 /* Not currently sent by Edgeport */
1987 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001988 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1989 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001990 handle_new_lsr(edge_port, false, byte2, 0);
1991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Alan Cox03f0dbf2008-07-22 11:16:34 +01001993 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001994 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1995 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001996 /* byte2 is LSR Register */
1997 /* byte3 is broken data byte */
1998 handle_new_lsr(edge_port, true, byte2, byte3);
1999 break;
2000 /*
2001 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002002 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002003 * __func__, edge_serial->rxPort, byte2, byte3);
2004 * break;
2005 */
2006 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002007 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2008 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002009 /*
2010 * Process this new modem status and generate appropriate
2011 * events, etc, based on the new status. This routine
2012 * also saves the MSR in Port->ShadowMsr.
2013 */
2014 handle_new_msr(edge_port, byte2);
2015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Alan Cox03f0dbf2008-07-22 11:16:34 +01002017 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002018 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023
2024/*****************************************************************************
2025 * edge_tty_recv
2026 * this function passes data on to the tty flip buffer
2027 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002028static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2029 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 int cnt;
2032
Alan Coxa108bfc2010-02-18 16:44:01 +00002033 cnt = tty_insert_flip_string(tty, data, length);
2034 if (cnt < length) {
2035 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2036 __func__, length - cnt);
2037 }
2038 data += cnt;
2039 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 tty_flip_buffer_push(tty);
2042}
2043
2044
2045/*****************************************************************************
2046 * handle_new_msr
2047 * this function handles any change to the msr register for a port.
2048 *****************************************************************************/
2049static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2050{
2051 struct async_icount *icount;
2052
Alan Cox03f0dbf2008-07-22 11:16:34 +01002053 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2054 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 icount = &edge_port->icount;
2056
2057 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002058 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002060 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002062 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002064 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 wake_up_interruptible(&edge_port->delta_msr_wait);
2067 }
2068
2069 /* Save the new modem status */
2070 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073
2074/*****************************************************************************
2075 * handle_new_lsr
2076 * this function handles any change to the lsr register for a port.
2077 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2079 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002081 __u8 newLsr = (__u8) (lsr & (__u8)
2082 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2083 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 edge_port->shadowLSR = lsr;
2086
2087 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002088 /*
2089 * Parity and Framing errors only count if they
2090 * occur exclusive of a break being
2091 * received.
2092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2094 }
2095
2096 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002097 if (lsrData) {
2098 struct tty_struct *tty =
2099 tty_port_tty_get(&edge_port->port->port);
2100 if (tty) {
2101 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2102 tty_kref_put(tty);
2103 }
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /* update input line counters */
2106 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002107 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002109 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002111 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002113 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
2117
2118/****************************************************************************
2119 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002120 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * given address.
2122 * If successful returns the number of bytes written, otherwise it returns
2123 * a negative error number of the problem.
2124 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002125static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2126 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int result;
2129 __u16 current_length;
2130 unsigned char *transfer_buffer;
2131
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002132 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Alan Cox03f0dbf2008-07-22 11:16:34 +01002134 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002136 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2137 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -ENOMEM;
2139 }
2140
2141 /* need to split these writes up into 64 byte chunks */
2142 result = 0;
2143 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002144 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002148
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002149/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002150 memcpy(transfer_buffer, data, current_length);
2151 result = usb_control_msg(serial->dev,
2152 usb_sndctrlpipe(serial->dev, 0),
2153 USB_REQUEST_ION_WRITE_RAM,
2154 0x40, addr, extAddr, transfer_buffer,
2155 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (result < 0)
2157 break;
2158 length -= current_length;
2159 addr += current_length;
2160 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Alan Cox03f0dbf2008-07-22 11:16:34 +01002163 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return result;
2165}
2166
2167
2168/****************************************************************************
2169 * rom_write
2170 * writes a number of bytes to the Edgeport device's ROM starting at the
2171 * given address.
2172 * If successful returns the number of bytes written, otherwise it returns
2173 * a negative error number of the problem.
2174 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002175static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2176 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 int result;
2179 __u16 current_length;
2180 unsigned char *transfer_buffer;
2181
Alan Cox03f0dbf2008-07-22 11:16:34 +01002182 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002184 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2185 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -ENOMEM;
2187 }
2188
2189 /* need to split these writes up into 64 byte chunks */
2190 result = 0;
2191 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002192 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002194 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002196 memcpy(transfer_buffer, data, current_length);
2197 result = usb_control_msg(serial->dev,
2198 usb_sndctrlpipe(serial->dev, 0),
2199 USB_REQUEST_ION_WRITE_ROM, 0x40,
2200 addr, extAddr,
2201 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (result < 0)
2203 break;
2204 length -= current_length;
2205 addr += current_length;
2206 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Alan Cox03f0dbf2008-07-22 11:16:34 +01002209 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return result;
2211}
2212
2213
2214/****************************************************************************
2215 * rom_read
2216 * reads a number of bytes from the Edgeport device starting at the given
2217 * address.
2218 * If successful returns the number of bytes read, otherwise it returns
2219 * a negative error number of the problem.
2220 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002221static int rom_read(struct usb_serial *serial, __u16 extAddr,
2222 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 int result;
2225 __u16 current_length;
2226 unsigned char *transfer_buffer;
2227
Alan Cox03f0dbf2008-07-22 11:16:34 +01002228 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002230 dev_err(&serial->dev->dev,
2231 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return -ENOMEM;
2233 }
2234
2235 /* need to split these reads up into 64 byte chunks */
2236 result = 0;
2237 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002238 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002240 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002242 result = usb_control_msg(serial->dev,
2243 usb_rcvctrlpipe(serial->dev, 0),
2244 USB_REQUEST_ION_READ_ROM,
2245 0xC0, addr, extAddr, transfer_buffer,
2246 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (result < 0)
2248 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002249 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 length -= current_length;
2251 addr += current_length;
2252 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Alan Cox03f0dbf2008-07-22 11:16:34 +01002255 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return result;
2257}
2258
2259
2260/****************************************************************************
2261 * send_iosp_ext_cmd
2262 * Is used to send a IOSP message to the Edgeport device
2263 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002264static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2265 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 unsigned char *buffer;
2268 unsigned char *currentCommand;
2269 int length = 0;
2270 int status = 0;
2271
Alan Cox03f0dbf2008-07-22 11:16:34 +01002272 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002274 dev_err(&edge_port->port->dev,
2275 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -ENOMEM;
2277 }
2278
2279 currentCommand = buffer;
2280
Alan Cox03f0dbf2008-07-22 11:16:34 +01002281 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2282 edge_port->port->number - edge_port->port->serial->minor,
2283 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Alan Cox03f0dbf2008-07-22 11:16:34 +01002285 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (status) {
2287 /* something bad happened, let's free up the memory */
2288 kfree(buffer);
2289 }
2290
2291 return status;
2292}
2293
2294
2295/*****************************************************************************
2296 * write_cmd_usb
2297 * this function writes the given buffer out to the bulk write endpoint.
2298 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002299static int write_cmd_usb(struct edgeport_port *edge_port,
2300 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002302 struct edgeport_serial *edge_serial =
2303 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002304 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 int status = 0;
2306 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002308 usb_serial_debug_data(debug, dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002311 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (!urb)
2313 return -ENOMEM;
2314
Oliver Neukum96c706e2007-03-15 15:27:17 +01002315 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002316 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2317 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Alan Cox03f0dbf2008-07-22 11:16:34 +01002319 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2320 usb_sndbulkpipe(edge_serial->serial->dev,
2321 edge_serial->bulk_out_endpoint),
2322 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002324 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 status = usb_submit_urb(urb, GFP_ATOMIC);
2326
2327 if (status) {
2328 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002329 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2330 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 usb_kill_urb(urb);
2332 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002333 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return status;
2335 }
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002338 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002340 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002342 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 status = -EINVAL;
2344 }
2345#endif
2346 return status;
2347}
2348
2349
2350/*****************************************************************************
2351 * send_cmd_write_baud_rate
2352 * this function sends the proper command to change the baud rate of the
2353 * specified port.
2354 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002355static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2356 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002358 struct edgeport_serial *edge_serial =
2359 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002360 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 unsigned char *cmdBuffer;
2362 unsigned char *currCmd;
2363 int cmdLen = 0;
2364 int divisor;
2365 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002366 unsigned char number =
2367 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Adam Kropelinbc71e472007-07-29 11:03:29 -04002369 if (edge_serial->is_epic &&
2370 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002371 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2372 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002373 return 0;
2374 }
2375
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002376 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2377 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002379 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002381 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return status;
2383 }
2384
Alan Cox03f0dbf2008-07-22 11:16:34 +01002385 /* Alloc memory for the string of commands. */
2386 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002388 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -ENOMEM;
2390 }
2391 currCmd = cmdBuffer;
2392
Alan Cox03f0dbf2008-07-22 11:16:34 +01002393 /* Enable access to divisor latch */
2394 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Alan Cox03f0dbf2008-07-22 11:16:34 +01002396 /* Write the divisor itself */
2397 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2398 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Alan Cox03f0dbf2008-07-22 11:16:34 +01002400 /* Restore original value to disable access to divisor latch */
2401 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2402 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Alan Cox03f0dbf2008-07-22 11:16:34 +01002404 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (status) {
2406 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002407 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409
2410 return status;
2411}
2412
2413
2414/*****************************************************************************
2415 * calc_baud_rate_divisor
2416 * this function calculates the proper baud rate divisor for the specified
2417 * baud rate.
2418 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002419static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 int i;
2422 __u16 custom;
2423
Tobias Klauser52950ed2005-12-11 16:20:08 +01002424 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002425 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 *divisor = divisor_table[i].Divisor;
2427 return 0;
2428 }
2429 }
2430
Alan Cox03f0dbf2008-07-22 11:16:34 +01002431 /* We have tried all of the standard baud rates
2432 * lets try to calculate the divisor for this baud rate
2433 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002435 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 custom = (__u16)((230400L + baudrate/2) / baudrate);
2437
2438 *divisor = custom;
2439
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002440 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
2442 }
2443
2444 return -1;
2445}
2446
2447
2448/*****************************************************************************
2449 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002450 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002452static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2453 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002455 struct edgeport_serial *edge_serial =
2456 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002457 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 unsigned char *cmdBuffer;
2459 unsigned char *currCmd;
2460 unsigned long cmdLen = 0;
2461 int status;
2462
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002463 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2464 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Adam Kropelinbc71e472007-07-29 11:03:29 -04002466 if (edge_serial->is_epic &&
2467 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2468 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002469 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002470 return 0;
2471 }
2472
Adam Kropelinbc71e472007-07-29 11:03:29 -04002473 if (edge_serial->is_epic &&
2474 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2475 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002476 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002477 return 0;
2478 }
2479
Alan Cox03f0dbf2008-07-22 11:16:34 +01002480 /* Alloc memory for the string of commands. */
2481 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2482 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 currCmd = cmdBuffer;
2486
Alan Cox03f0dbf2008-07-22 11:16:34 +01002487 /* Build a cmd in the buffer to write the given register */
2488 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2489 edge_port->port->number - edge_port->port->serial->minor,
2490 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2493 if (status) {
2494 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002495 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497
2498 return status;
2499}
2500
2501
2502/*****************************************************************************
2503 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002504 * This routine is called to set the UART on the device to match the
2505 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002507
2508static void change_port_settings(struct tty_struct *tty,
2509 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002511 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002512 struct edgeport_serial *edge_serial =
2513 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int baud;
2515 unsigned cflag;
2516 __u8 mask = 0xff;
2517 __u8 lData;
2518 __u8 lParity;
2519 __u8 lStop;
2520 __u8 rxFlow;
2521 __u8 txFlow;
2522 int status;
2523
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002524 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002526 if (!edge_port->open &&
2527 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002528 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return;
2530 }
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 cflag = tty->termios->c_cflag;
2533
2534 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002535 case CS5:
2536 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002537 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002538 break;
2539 case CS6:
2540 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002541 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002542 break;
2543 case CS7:
2544 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002545 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002546 break;
2547 default:
2548 case CS8:
2549 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002550 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
2554 lParity = LCR_PAR_NONE;
2555 if (cflag & PARENB) {
2556 if (cflag & CMSPAR) {
2557 if (cflag & PARODD) {
2558 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002559 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 } else {
2561 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002562 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 } else if (cflag & PARODD) {
2565 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002566 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 } else {
2568 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002569 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002572 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
2575 if (cflag & CSTOPB) {
2576 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002577 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 } else {
2579 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002580 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
2583 /* figure out the flow control settings */
2584 rxFlow = txFlow = 0x00;
2585 if (cflag & CRTSCTS) {
2586 rxFlow |= IOSP_RX_FLOW_RTS;
2587 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002588 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002590 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592
Alan Cox03f0dbf2008-07-22 11:16:34 +01002593 /* if we are implementing XON/XOFF, set the start and stop character
2594 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (I_IXOFF(tty) || I_IXON(tty)) {
2596 unsigned char stop_char = STOP_CHAR(tty);
2597 unsigned char start_char = START_CHAR(tty);
2598
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002599 if ((!edge_serial->is_epic) ||
2600 ((edge_serial->is_epic) &&
2601 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002602 send_iosp_ext_cmd(edge_port,
2603 IOSP_CMD_SET_XON_CHAR, start_char);
2604 send_iosp_ext_cmd(edge_port,
2605 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 /* if we are implementing INBOUND XON/XOFF */
2609 if (I_IXOFF(tty)) {
2610 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002611 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2612 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002614 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616
2617 /* if we are implementing OUTBOUND XON/XOFF */
2618 if (I_IXON(tty)) {
2619 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002620 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2621 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002623 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625 }
2626
2627 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002628 if ((!edge_serial->is_epic) ||
2629 ((edge_serial->is_epic) &&
2630 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2631 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2632 if ((!edge_serial->is_epic) ||
2633 ((edge_serial->is_epic) &&
2634 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2635 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637
2638 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2639 edge_port->shadowLCR |= (lData | lParity | lStop);
2640
2641 edge_port->validDataMask = mask;
2642
2643 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002644 status = send_cmd_write_uart_register(edge_port, LCR,
2645 edge_port->shadowLCR);
2646 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 /* set up the MCR register and send it to the EdgePort */
2650 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002651 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002653
2654 status = send_cmd_write_uart_register(edge_port, MCR,
2655 edge_port->shadowMCR);
2656 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 /* Determine divisor based on baud rate */
2660 baud = tty_get_baud_rate(tty);
2661 if (!baud) {
2662 /* pick a default, any default... */
2663 baud = 9600;
2664 }
2665
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002666 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002667 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002668 if (status == -1) {
2669 /* Speed change was not possible - put back the old speed */
2670 baud = tty_termios_baud_rate(old_termios);
2671 tty_encode_baud_rate(tty, baud, baud);
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
2675
2676/****************************************************************************
2677 * unicode_to_ascii
2678 * Turns a string from Unicode into ASCII.
2679 * Doesn't do a good job with any characters that are outside the normal
2680 * ASCII range, but it's only for debugging...
2681 * NOTE: expects the unicode in LE format
2682 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002683static void unicode_to_ascii(char *string, int buflen,
2684 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 int i;
2687
Pete Zaitcevad933752006-05-22 21:49:44 -07002688 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002690 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Pete Zaitcevad933752006-05-22 21:49:44 -07002692 for (i = 0; i < unicode_size; i++) {
2693 if (i >= buflen)
2694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002696 }
2697 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700
2701/****************************************************************************
2702 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002703 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 * structure.
2705 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002706static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002708 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 int response;
2710
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002711 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Alan Cox03f0dbf2008-07-22 11:16:34 +01002713 response = rom_read(edge_serial->serial,
2714 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2715 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2716 EDGE_MANUF_DESC_LEN,
2717 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Alan Cox03f0dbf2008-07-22 11:16:34 +01002719 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002720 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002721 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002723 dev_dbg(dev, "**Manufacturer Descriptor\n");
2724 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002725 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002726 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002727 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002728 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002729 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002730 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002731 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002732 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002733 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002734 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002735 edge_serial->manuf_descriptor.DescDate[0],
2736 edge_serial->manuf_descriptor.DescDate[1],
2737 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002738 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002739 edge_serial->manuf_descriptor.SerialNumber,
2740 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002741 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002742 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002743 edge_serial->manuf_descriptor.AssemblyNumber,
2744 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002745 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002746 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002747 edge_serial->manuf_descriptor.OemAssyNumber,
2748 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002749 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2750 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002751 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002752 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002753 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002754 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002755 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757}
2758
2759
2760/****************************************************************************
2761 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002762 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 * structure.
2764 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002765static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002767 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 int response;
2769
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002770 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Alan Cox03f0dbf2008-07-22 11:16:34 +01002772 response = rom_read(edge_serial->serial,
2773 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2774 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2775 EDGE_BOOT_DESC_LEN,
2776 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Alan Cox03f0dbf2008-07-22 11:16:34 +01002778 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002779 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002780 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002781 dev_dbg(dev, "**Boot Descriptor:\n");
2782 dev_dbg(dev, " BootCodeLength: %d\n",
2783 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2784 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002785 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002786 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002787 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002788 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002789 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002790 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002791 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002792 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002793 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002794 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002795 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797}
2798
2799
2800/****************************************************************************
2801 * load_application_firmware
2802 * This is called to load the application firmware to the device
2803 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002804static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002806 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302807 const struct ihex_binrec *rec;
2808 const struct firmware *fw;
2809 const char *fw_name;
2810 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302812 __u32 Operaddr;
2813 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 switch (edge_serial->product_info.iDownloadFile) {
2816 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302817 fw_info = "downloading firmware version (930)";
2818 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 break;
2820
2821 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302822 fw_info = "downloading firmware version (80251)";
2823 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 break;
2825
2826 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002827 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return;
2829
2830 default:
2831 return;
2832 }
2833
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302834 response = request_ihex_firmware(&fw, fw_name,
2835 &edge_serial->serial->dev->dev);
2836 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002837 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302838 fw_name, response);
2839 return;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302842 rec = (const struct ihex_binrec *)fw->data;
2843 build = (rec->data[2] << 8) | rec->data[3];
2844
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002845 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302846
Bjørn Mork271c1152011-01-17 14:19:37 +01002847 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2848 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302849 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2850
2851 for (rec = ihex_next_binrec(rec); rec;
2852 rec = ihex_next_binrec(rec)) {
2853 Operaddr = be32_to_cpu(rec->addr);
2854 response = sram_write(edge_serial->serial,
2855 Operaddr >> 16,
2856 Operaddr & 0xFFFF,
2857 be16_to_cpu(rec->len),
2858 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302860 dev_err(&edge_serial->serial->dev->dev,
2861 "sram_write failed (%x, %x, %d)\n",
2862 Operaddr >> 16, Operaddr & 0xFFFF,
2863 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865 }
2866 }
2867
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002868 dev_dbg(dev, "sending exec_dl_code\n");
2869 response = usb_control_msg (edge_serial->serial->dev,
2870 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2871 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2873
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302874 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877
2878/****************************************************************************
2879 * edge_startup
2880 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002881static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct edgeport_serial *edge_serial;
2884 struct edgeport_port *edge_port;
2885 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002886 struct device *ddev = &serial->dev->dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002887 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002888 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002889 bool interrupt_in_found;
2890 bool bulk_in_found;
2891 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002892 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2893 EDGE_COMPATIBILITY_MASK1,
2894 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 dev = serial->dev;
2897
2898 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002899 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002901 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return -ENOMEM;
2903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 spin_lock_init(&edge_serial->es_lock);
2905 edge_serial->serial = serial;
2906 usb_set_serial_data(serial, edge_serial);
2907
2908 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002909 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002910 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002911 if (i < 0)
2912 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002913 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002914 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002915 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2918
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002919 /* Read the epic descriptor */
2920 if (get_epic_descriptor(edge_serial) <= 0) {
2921 /* memcpy descriptor to Supports structures */
2922 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2923 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002926 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002928 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002929 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002930
2931 get_product_info(edge_serial);
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 /* set the number of ports from the manufacturing description */
2935 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002936 if ((!edge_serial->is_epic) &&
2937 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002938 dev_warn(ddev,
2939 "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 -08002940 edge_serial->product_info.NumPorts,
2941 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
2943
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002944 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002946 /* If not an EPiC device */
2947 if (!edge_serial->is_epic) {
2948 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002949 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002951 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002953 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002954 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002956 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002957
2958 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002959/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002960/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002961 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002962 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302963 edge_serial->product_info.FirmwareMajorVersion,
2964 edge_serial->product_info.FirmwareMinorVersion,
2965 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Alan Cox03f0dbf2008-07-22 11:16:34 +01002967 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 * as the structures aren't created yet. */
2969
2970 /* set up our port private structures */
2971 for (i = 0; i < serial->num_ports; ++i) {
Julia Lawall1ac93a32010-05-13 22:00:40 +02002972 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (edge_port == NULL) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002974 dev_err(ddev, "%s - Out of memory\n", __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07002975 for (j = 0; j < i; ++j) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002976 kfree(usb_get_serial_port_data(serial->port[j]));
2977 usb_set_serial_port_data(serial->port[j],
2978 NULL);
Chris Lundbfd5df32006-06-03 13:58:19 -07002979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 usb_set_serial_data(serial, NULL);
2981 kfree(edge_serial);
2982 return -ENOMEM;
2983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 spin_lock_init(&edge_port->ep_lock);
2985 edge_port->port = serial->port[i];
2986 usb_set_serial_port_data(serial->port[i], edge_port);
2987 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002988
2989 response = 0;
2990
2991 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002992 /* EPIC thing, set up our interrupt polling now and our read
2993 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002994 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002995 for (i = 0; i < serial->interface->altsetting[0]
2996 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002997 struct usb_endpoint_descriptor *endpoint;
2998 int buffer_size;
2999
Alan Cox03f0dbf2008-07-22 11:16:34 +01003000 endpoint = &serial->interface->altsetting[0].
3001 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003002 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003003 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003004 (usb_endpoint_is_int_in(endpoint))) {
3005 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003006 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003007
3008 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003009 edge_serial->interrupt_read_urb =
3010 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003011 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003012 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003013 return -ENOMEM;
3014 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003015 edge_serial->interrupt_in_buffer =
3016 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003017 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003018 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003019 usb_free_urb(edge_serial->interrupt_read_urb);
3020 return -ENOMEM;
3021 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003022 edge_serial->interrupt_in_endpoint =
3023 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003024
3025 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003026 usb_fill_int_urb(
3027 edge_serial->interrupt_read_urb,
3028 dev,
3029 usb_rcvintpipe(dev,
3030 endpoint->bEndpointAddress),
3031 edge_serial->interrupt_in_buffer,
3032 buffer_size,
3033 edge_interrupt_callback,
3034 edge_serial,
3035 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003036
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003037 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003038 }
3039
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003040 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003041 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003042 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003043 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003044
3045 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003046 edge_serial->read_urb =
3047 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003048 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003049 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 return -ENOMEM;
3051 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003052 edge_serial->bulk_in_buffer =
3053 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003054 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003055 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003056 usb_free_urb(edge_serial->read_urb);
3057 return -ENOMEM;
3058 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003059 edge_serial->bulk_in_endpoint =
3060 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003061
3062 /* set up our bulk in urb */
3063 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003064 usb_rcvbulkpipe(dev,
3065 endpoint->bEndpointAddress),
3066 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003067 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003068 edge_bulk_in_callback,
3069 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003070 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003071 }
3072
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003073 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003074 (usb_endpoint_is_bulk_out(endpoint))) {
3075 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003076 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003077 edge_serial->bulk_out_endpoint =
3078 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003079 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003080 }
3081 }
3082
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003083 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003084 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003085 return -ENODEV;
3086 }
3087
3088 /* start interrupt read for this edgeport this interrupt will
3089 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003090 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3091 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003092 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003093 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003094 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003095 }
3096 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099
3100/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003101 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 * This function is called whenever the device is removed from the usb bus.
3103 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003104static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003106 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003109 /* free up our endpoint stuff */
3110 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003111 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003112 usb_free_urb(edge_serial->interrupt_read_urb);
3113 kfree(edge_serial->interrupt_in_buffer);
3114
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003115 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003116 usb_free_urb(edge_serial->read_urb);
3117 kfree(edge_serial->bulk_in_buffer);
3118 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003119}
3120
3121
3122/****************************************************************************
3123 * edge_release
3124 * This function is called when the device structure is deallocated.
3125 ****************************************************************************/
3126static void edge_release(struct usb_serial *serial)
3127{
3128 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3129 int i;
3130
Alan Sternf9c99bb2009-06-02 11:53:55 -04003131 for (i = 0; i < serial->num_ports; ++i)
3132 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003133
3134 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003137module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Alan Cox03f0dbf2008-07-22 11:16:34 +01003139MODULE_AUTHOR(DRIVER_AUTHOR);
3140MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303142MODULE_FIRMWARE("edgeport/boot.fw");
3143MODULE_FIRMWARE("edgeport/boot2.fw");
3144MODULE_FIRMWARE("edgeport/down.fw");
3145MODULE_FIRMWARE("edgeport/down2.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147module_param(debug, bool, S_IRUGO | S_IWUSR);
3148MODULE_PARM_DESC(debug, "Debug enabled or not");