blob: 06159fd037779ad38d2e37b21b3b2e3f644984c6 [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-Hartman59d33f22012-09-18 09:58:57 +0100602 usb_serial_debug_data(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-Hartman59d33f22012-09-18 09:58:57 +0100708 usb_serial_debug_data(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);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001181 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Alan Cox03f0dbf2008-07-22 11:16:34 +01001183 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 fifo->head += firsthalf;
1185 fifo->count += firsthalf;
1186
Alan Cox03f0dbf2008-07-22 11:16:34 +01001187 /* wrap the index */
1188 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 secondhalf = copySize-firsthalf;
1192
1193 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001194 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001196 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001197 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 fifo->count += secondhalf;
1199 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001200 /* No need to check for wrap since we can not get to end of
1201 * the fifo in this part
1202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205finish_write:
1206 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1207
Alan Cox03f0dbf2008-07-22 11:16:34 +01001208 send_more_port_data((struct edgeport_serial *)
1209 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001211 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1212 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Alan Cox03f0dbf2008-07-22 11:16:34 +01001214 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217
1218/************************************************************************
1219 *
1220 * send_more_port_data()
1221 *
1222 * This routine attempts to write additional UART transmit data
1223 * to a port over the USB bulk pipe. It is called (1) when new
1224 * data has been written to a port's TxBuffer from higher layers
1225 * (2) when the peripheral sends us additional TxCredits indicating
1226 * that it can accept more Tx data for a given port; and (3) when
1227 * a bulk write completes successfully and we want to see if we
1228 * can transmit more.
1229 *
1230 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001231static void send_more_port_data(struct edgeport_serial *edge_serial,
1232 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001235 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct urb *urb;
1237 unsigned char *buffer;
1238 int status;
1239 int count;
1240 int bytesleft;
1241 int firsthalf;
1242 int secondhalf;
1243 unsigned long flags;
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 spin_lock_irqsave(&edge_port->ep_lock, flags);
1246
1247 if (edge_port->write_in_progress ||
1248 !edge_port->open ||
1249 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001250 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1251 __func__, edge_port->port->number,
1252 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto exit_send;
1254 }
1255
Alan Cox03f0dbf2008-07-22 11:16:34 +01001256 /* since the amount of data in the fifo will always fit into the
1257 * edgeport buffer we do not need to check the write length
1258 *
1259 * Do we have enough credits for this port to make it worthwhile
1260 * to bother queueing a write. If it's too small, say a few bytes,
1261 * it's better to wait for more credits so we can do a larger write.
1262 */
1263 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 -07001264 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001265 __func__, edge_port->port->number, fifo->count,
1266 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto exit_send;
1268 }
1269
Alan Cox03f0dbf2008-07-22 11:16:34 +01001270 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001271 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Alan Cox03f0dbf2008-07-22 11:16:34 +01001273 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 urb = edge_port->write_urb;
1275
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001276 /* make sure transfer buffer is freed */
1277 kfree(urb->transfer_buffer);
1278 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Alan Cox03f0dbf2008-07-22 11:16:34 +01001280 /* build the data header for the buffer and port that we are about
1281 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001283 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001285 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001286 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001287 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto exit_send;
1289 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001290 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1291 - edge_port->port->serial->minor, count);
1292 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1293 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* now copy our data */
1296 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001297 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1299 fifo->tail += firsthalf;
1300 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001301 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 secondhalf = count-firsthalf;
1305 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001306 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1307 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 fifo->tail += secondhalf;
1309 fifo->count -= secondhalf;
1310 }
1311
1312 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001313 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001316 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1317 usb_sndbulkpipe(edge_serial->serial->dev,
1318 edge_serial->bulk_out_endpoint),
1319 buffer, count+2,
1320 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /* decrement the number of credits we have by the number we just sent */
1323 edge_port->txCredits -= count;
1324 edge_port->icount.tx += count;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 status = usb_submit_urb(urb, GFP_ATOMIC);
1327 if (status) {
1328 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001329 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001330 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1331 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001332 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* revert the credits as something bad happened. */
1335 edge_port->txCredits += count;
1336 edge_port->icount.tx -= count;
1337 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001338 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1339 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341exit_send:
1342 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1343}
1344
1345
1346/*****************************************************************************
1347 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001348 * this function is called by the tty driver when it wants to know how
1349 * many bytes of data we can accept for a specific port. If successful,
1350 * we return the amount of room that we have for this port (the txCredits)
1351 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001353static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Alan Cox95da3102008-07-22 11:09:07 +01001355 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1357 int room;
1358 unsigned long flags;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001361 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001362 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001366 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Alan Cox03f0dbf2008-07-22 11:16:34 +01001370 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_lock_irqsave(&edge_port->ep_lock, flags);
1372 room = edge_port->txCredits - edge_port->txfifo.count;
1373 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1374
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001375 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return room;
1377}
1378
1379
1380/*****************************************************************************
1381 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001382 * this function is called by the tty driver when it wants to know how
1383 * many bytes of data we currently have outstanding in the port (data that
1384 * has been written, but hasn't made it out the port yet)
1385 * If successful, we return the number of bytes left to be written in the
1386 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * Otherwise we return a negative error number.
1388 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001389static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Alan Cox95da3102008-07-22 11:09:07 +01001391 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1393 int num_chars;
1394 unsigned long flags;
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001397 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001398 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001402 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
1406 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001407 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1408 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1410 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001411 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1412 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 return num_chars;
1416}
1417
1418
1419/*****************************************************************************
1420 * SerialThrottle
1421 * this function is called by the tty driver when it wants to stop the data
1422 * being read from the port.
1423 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001424static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Alan Cox95da3102008-07-22 11:09:07 +01001426 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int status;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (edge_port == NULL)
1431 return;
1432
1433 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001434 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return;
1436 }
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /* if we are implementing XON/XOFF, send the stop character */
1439 if (I_IXOFF(tty)) {
1440 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001441 status = edge_write(tty, port, &stop_char, 1);
1442 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
1446 /* if we are implementing RTS/CTS, toggle that line */
1447 if (tty->termios->c_cflag & CRTSCTS) {
1448 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001449 status = send_cmd_write_uart_register(edge_port, MCR,
1450 edge_port->shadowMCR);
1451 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456
1457/*****************************************************************************
1458 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001459 * this function is called by the tty driver when it wants to resume the
1460 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001462static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Alan Cox95da3102008-07-22 11:09:07 +01001464 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 int status;
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (edge_port == NULL)
1469 return;
1470
1471 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001472 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return;
1474 }
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* if we are implementing XON/XOFF, send the start character */
1477 if (I_IXOFF(tty)) {
1478 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001479 status = edge_write(tty, port, &start_char, 1);
1480 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* if we are implementing RTS/CTS, toggle that line */
1484 if (tty->termios->c_cflag & CRTSCTS) {
1485 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001486 send_cmd_write_uart_register(edge_port, MCR,
1487 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491
1492/*****************************************************************************
1493 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001494 * this function is called by the tty driver when it wants to change
1495 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001497static void edge_set_termios(struct tty_struct *tty,
1498 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 unsigned int cflag;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 cflag = tty->termios->c_cflag;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001504 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
1505 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 -07001506
1507 if (edge_port == NULL)
1508 return;
1509
1510 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001511 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return;
1513 }
1514
1515 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001516 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519
1520/*****************************************************************************
1521 * get_lsr_info - get line status register info
1522 *
1523 * Purpose: Let user call ioctl() to get info when the UART physically
1524 * is emptied. On bus types like RS485, the transmitter must
1525 * release the bus after transmitting. This must be done when
1526 * the transmit shift register is empty, not be done when the
1527 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001528 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001530static int get_lsr_info(struct edgeport_port *edge_port,
1531 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 unsigned int result = 0;
1534 unsigned long flags;
1535
1536 spin_lock_irqsave(&edge_port->ep_lock, flags);
1537 if (edge_port->maxTxCredits == edge_port->txCredits &&
1538 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001539 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 result = TIOCSER_TEMT;
1541 }
1542 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1543
1544 if (copy_to_user(value, &result, sizeof(int)))
1545 return -EFAULT;
1546 return 0;
1547}
1548
Alan Cox20b9d172011-02-14 16:26:50 +00001549static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001550 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Alan Cox95da3102008-07-22 11:09:07 +01001552 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1554 unsigned int mcr;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 mcr = edge_port->shadowMCR;
1557 if (set & TIOCM_RTS)
1558 mcr |= MCR_RTS;
1559 if (set & TIOCM_DTR)
1560 mcr |= MCR_DTR;
1561 if (set & TIOCM_LOOP)
1562 mcr |= MCR_LOOPBACK;
1563
1564 if (clear & TIOCM_RTS)
1565 mcr &= ~MCR_RTS;
1566 if (clear & TIOCM_DTR)
1567 mcr &= ~MCR_DTR;
1568 if (clear & TIOCM_LOOP)
1569 mcr &= ~MCR_LOOPBACK;
1570
1571 edge_port->shadowMCR = mcr;
1572
1573 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1574
1575 return 0;
1576}
1577
Alan Cox60b33c12011-02-14 16:26:14 +00001578static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Alan Cox95da3102008-07-22 11:09:07 +01001580 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1582 unsigned int result = 0;
1583 unsigned int msr;
1584 unsigned int mcr;
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 msr = edge_port->shadowMSR;
1587 mcr = edge_port->shadowMCR;
1588 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1589 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1590 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1591 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1592 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1593 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return result;
1596}
1597
Alan Cox0bca1b92010-09-16 18:21:40 +01001598static int edge_get_icount(struct tty_struct *tty,
1599 struct serial_icounter_struct *icount)
1600{
1601 struct usb_serial_port *port = tty->driver_data;
1602 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1603 struct async_icount cnow;
1604 cnow = edge_port->icount;
1605
1606 icount->cts = cnow.cts;
1607 icount->dsr = cnow.dsr;
1608 icount->rng = cnow.rng;
1609 icount->dcd = cnow.dcd;
1610 icount->rx = cnow.rx;
1611 icount->tx = cnow.tx;
1612 icount->frame = cnow.frame;
1613 icount->overrun = cnow.overrun;
1614 icount->parity = cnow.parity;
1615 icount->brk = cnow.brk;
1616 icount->buf_overrun = cnow.buf_overrun;
1617
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001618 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1619 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001620 return 0;
1621}
1622
Alan Cox03f0dbf2008-07-22 11:16:34 +01001623static int get_serial_info(struct edgeport_port *edge_port,
1624 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 struct serial_struct tmp;
1627
1628 if (!retinfo)
1629 return -EFAULT;
1630
1631 memset(&tmp, 0, sizeof(tmp));
1632
1633 tmp.type = PORT_16550A;
1634 tmp.line = edge_port->port->serial->minor;
1635 tmp.port = edge_port->port->number;
1636 tmp.irq = 0;
1637 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1638 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1639 tmp.baud_base = 9600;
1640 tmp.close_delay = 5*HZ;
1641 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1644 return -EFAULT;
1645 return 0;
1646}
1647
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/*****************************************************************************
1650 * SerialIoctl
1651 * this function handles any ioctl calls to the driver
1652 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001653static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001654 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Alan Cox95da3102008-07-22 11:09:07 +01001656 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 DEFINE_WAIT(wait);
1658 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1659 struct async_icount cnow;
1660 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001662 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001665 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001666 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001667 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Alan Cox03f0dbf2008-07-22 11:16:34 +01001669 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001670 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001671 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Alan Cox03f0dbf2008-07-22 11:16:34 +01001673 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001674 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001675 cprev = edge_port->icount;
1676 while (1) {
1677 prepare_to_wait(&edge_port->delta_msr_wait,
1678 &wait, TASK_INTERRUPTIBLE);
1679 schedule();
1680 finish_wait(&edge_port->delta_msr_wait, &wait);
1681 /* see if a signal did it */
1682 if (signal_pending(current))
1683 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001685 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1686 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1687 return -EIO; /* no change => error */
1688 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1689 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1690 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1691 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1692 return 0;
1693 }
1694 cprev = cnow;
1695 }
1696 /* NOTREACHED */
1697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -ENOIOCTLCMD;
1701}
1702
1703
1704/*****************************************************************************
1705 * SerialBreak
1706 * this function sends a break to the port
1707 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001708static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Alan Cox95da3102008-07-22 11:09:07 +01001710 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001712 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int status;
1714
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001715 if ((!edge_serial->is_epic) ||
1716 ((edge_serial->is_epic) &&
1717 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1718 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001719 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001721 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001722 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001723 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001724 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001725 block_until_chase_response(edge_port);
1726 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001727 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001731 if ((!edge_serial->is_epic) ||
1732 ((edge_serial->is_epic) &&
1733 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1734 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001735 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001736 status = send_iosp_ext_cmd(edge_port,
1737 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001738 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001739 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001740 status = send_iosp_ext_cmd(edge_port,
1741 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001742 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001743 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001744 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001745 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749
1750/*****************************************************************************
1751 * process_rcvd_data
1752 * this function handles the data received on the bulk in pipe.
1753 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001754static void process_rcvd_data(struct edgeport_serial *edge_serial,
1755 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001757 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 struct usb_serial_port *port;
1759 struct edgeport_port *edge_port;
1760 struct tty_struct *tty;
1761 __u16 lastBufferLength;
1762 __u16 rxLen;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 lastBufferLength = bufferLength + 1;
1765
1766 while (bufferLength > 0) {
1767 /* failsafe incase we get a message that we don't understand */
1768 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001769 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 break;
1771 }
1772 lastBufferLength = bufferLength;
1773
1774 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001775 case EXPECT_HDR1:
1776 edge_serial->rxHeader1 = *buffer;
1777 ++buffer;
1778 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Alan Cox03f0dbf2008-07-22 11:16:34 +01001780 if (bufferLength == 0) {
1781 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001783 }
1784 /* otherwise, drop on through */
1785 case EXPECT_HDR2:
1786 edge_serial->rxHeader2 = *buffer;
1787 ++buffer;
1788 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001790 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1791 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001792 /* Process depending on whether this header is
1793 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Alan Cox03f0dbf2008-07-22 11:16:34 +01001795 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1796 /* Decode this status header and go to
1797 * EXPECT_HDR1 (if we can process the status
1798 * with only 2 bytes), or go to EXPECT_HDR3 to
1799 * get the third byte. */
1800 edge_serial->rxPort =
1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1802 edge_serial->rxStatusCode =
1803 IOSP_GET_STATUS_CODE(
1804 edge_serial->rxHeader1);
1805
1806 if (!IOSP_STATUS_IS_2BYTE(
1807 edge_serial->rxStatusCode)) {
1808 /* This status needs additional bytes.
1809 * Save what we have and then wait for
1810 * more data.
1811 */
1812 edge_serial->rxStatusParam
1813 = edge_serial->rxHeader2;
1814 edge_serial->rxState = EXPECT_HDR3;
1815 break;
1816 }
1817 /* We have all the header bytes, process the
1818 status now */
1819 process_rcvd_status(edge_serial,
1820 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 edge_serial->rxState = EXPECT_HDR1;
1822 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001823 } else {
1824 edge_serial->rxPort =
1825 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1826 edge_serial->rxBytesRemaining =
1827 IOSP_GET_HDR_DATA_LEN(
1828 edge_serial->rxHeader1,
1829 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001830 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1831 __func__,
1832 edge_serial->rxPort,
1833 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Alan Cox03f0dbf2008-07-22 11:16:34 +01001835 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1836 * ASSERT(DevExt->RxBytesRemaining <
1837 * IOSP_MAX_DATA_LENGTH);
1838 */
1839
1840 if (bufferLength == 0) {
1841 edge_serial->rxState = EXPECT_DATA;
1842 break;
1843 }
1844 /* Else, drop through */
1845 }
1846 case EXPECT_DATA: /* Expect data */
1847 if (bufferLength < edge_serial->rxBytesRemaining) {
1848 rxLen = bufferLength;
1849 /* Expect data to start next buffer */
1850 edge_serial->rxState = EXPECT_DATA;
1851 } else {
1852 /* BufLen >= RxBytesRemaining */
1853 rxLen = edge_serial->rxBytesRemaining;
1854 /* Start another header next time */
1855 edge_serial->rxState = EXPECT_HDR1;
1856 }
1857
1858 bufferLength -= rxLen;
1859 edge_serial->rxBytesRemaining -= rxLen;
1860
1861 /* spit this data back into the tty driver if this
1862 port is open */
1863 if (rxLen) {
1864 port = edge_serial->serial->port[
1865 edge_serial->rxPort];
1866 edge_port = usb_get_serial_port_data(port);
1867 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001868 tty = tty_port_tty_get(
1869 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001870 if (tty) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001871 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001872 __func__, rxLen, edge_serial->rxPort);
1873 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001874 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001875 }
1876 edge_port->icount.rx += rxLen;
1877 }
1878 buffer += rxLen;
1879 }
1880 break;
1881
1882 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1883 edge_serial->rxHeader3 = *buffer;
1884 ++buffer;
1885 --bufferLength;
1886
1887 /* We have all the header bytes, process the
1888 status now */
1889 process_rcvd_status(edge_serial,
1890 edge_serial->rxStatusParam,
1891 edge_serial->rxHeader3);
1892 edge_serial->rxState = EXPECT_HDR1;
1893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 }
1896}
1897
1898
1899/*****************************************************************************
1900 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001901 * this function handles the any status messages received on the
1902 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001904static void process_rcvd_status(struct edgeport_serial *edge_serial,
1905 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct usb_serial_port *port;
1908 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001909 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001910 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 __u8 code = edge_serial->rxStatusCode;
1912
1913 /* switch the port pointer to the one being currently talked about */
1914 port = edge_serial->serial->port[edge_serial->rxPort];
1915 edge_port = usb_get_serial_port_data(port);
1916 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001917 dev_err(&edge_serial->serial->dev->dev,
1918 "%s - edge_port == NULL for port %d\n",
1919 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return;
1921 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001922 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (code == IOSP_EXT_STATUS) {
1925 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001926 case IOSP_EXT_STATUS_CHASE_RSP:
1927 /* we want to do EXT status regardless of port
1928 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001929 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1930 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001931 /* Currently, the only EXT_STATUS is Chase, so process
1932 * here instead of one more call to one more subroutine
1933 * If/when more EXT_STATUS, there'll be more work to do
1934 * Also, we currently clear flag and close the port
1935 * regardless of content of above's Byte3.
1936 * We could choose to do something else when Byte3 says
1937 * Timeout on Chase from Edgeport, like wait longer in
1938 * block_until_chase_response, but for now we don't.
1939 */
1940 edge_port->chaseResponsePending = false;
1941 wake_up(&edge_port->wait_chase);
1942 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Alan Cox03f0dbf2008-07-22 11:16:34 +01001944 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001945 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1946 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001947 /* Port->RxCheckRsp = true; */
1948 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 }
1951
1952 if (code == IOSP_STATUS_OPEN_RSP) {
1953 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1954 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001955 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1956 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001957 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Alan Cox03f0dbf2008-07-22 11:16:34 +01001959 /* send the current line settings to the port so we are
1960 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001961 tty = tty_port_tty_get(&edge_port->port->port);
1962 if (tty) {
1963 change_port_settings(tty,
1964 edge_port, tty->termios);
1965 tty_kref_put(tty);
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001969 edge_port->openPending = false;
1970 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 wake_up(&edge_port->wait_open);
1972 return;
1973 }
1974
Alan Cox03f0dbf2008-07-22 11:16:34 +01001975 /* If port is closed, silently discard all rcvd status. We can
1976 * have cases where buffered status is received AFTER the close
1977 * port command is sent to the Edgeport.
1978 */
1979 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001983 /* Not currently sent by Edgeport */
1984 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001985 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1986 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001987 handle_new_lsr(edge_port, false, byte2, 0);
1988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Alan Cox03f0dbf2008-07-22 11:16:34 +01001990 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001991 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1992 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001993 /* byte2 is LSR Register */
1994 /* byte3 is broken data byte */
1995 handle_new_lsr(edge_port, true, byte2, byte3);
1996 break;
1997 /*
1998 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001999 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002000 * __func__, edge_serial->rxPort, byte2, byte3);
2001 * break;
2002 */
2003 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002004 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2005 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002006 /*
2007 * Process this new modem status and generate appropriate
2008 * events, etc, based on the new status. This routine
2009 * also saves the MSR in Port->ShadowMsr.
2010 */
2011 handle_new_msr(edge_port, byte2);
2012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Alan Cox03f0dbf2008-07-22 11:16:34 +01002014 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002015 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
2020
2021/*****************************************************************************
2022 * edge_tty_recv
2023 * this function passes data on to the tty flip buffer
2024 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002025static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2026 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 int cnt;
2029
Alan Coxa108bfc2010-02-18 16:44:01 +00002030 cnt = tty_insert_flip_string(tty, data, length);
2031 if (cnt < length) {
2032 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2033 __func__, length - cnt);
2034 }
2035 data += cnt;
2036 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 tty_flip_buffer_push(tty);
2039}
2040
2041
2042/*****************************************************************************
2043 * handle_new_msr
2044 * this function handles any change to the msr register for a port.
2045 *****************************************************************************/
2046static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2047{
2048 struct async_icount *icount;
2049
Alan Cox03f0dbf2008-07-22 11:16:34 +01002050 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2051 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 icount = &edge_port->icount;
2053
2054 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002055 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002057 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002059 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002061 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 wake_up_interruptible(&edge_port->delta_msr_wait);
2064 }
2065
2066 /* Save the new modem status */
2067 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
2070
2071/*****************************************************************************
2072 * handle_new_lsr
2073 * this function handles any change to the lsr register for a port.
2074 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002075static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2076 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078 __u8 newLsr = (__u8) (lsr & (__u8)
2079 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2080 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 edge_port->shadowLSR = lsr;
2083
2084 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002085 /*
2086 * Parity and Framing errors only count if they
2087 * occur exclusive of a break being
2088 * received.
2089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2091 }
2092
2093 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002094 if (lsrData) {
2095 struct tty_struct *tty =
2096 tty_port_tty_get(&edge_port->port->port);
2097 if (tty) {
2098 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2099 tty_kref_put(tty);
2100 }
2101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* update input line counters */
2103 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002104 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002106 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002108 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002110 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114
2115/****************************************************************************
2116 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002117 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * given address.
2119 * If successful returns the number of bytes written, otherwise it returns
2120 * a negative error number of the problem.
2121 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002122static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2123 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 int result;
2126 __u16 current_length;
2127 unsigned char *transfer_buffer;
2128
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002129 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Alan Cox03f0dbf2008-07-22 11:16:34 +01002131 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002133 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2134 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return -ENOMEM;
2136 }
2137
2138 /* need to split these writes up into 64 byte chunks */
2139 result = 0;
2140 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002141 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002143 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002145
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002146/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002147 memcpy(transfer_buffer, data, current_length);
2148 result = usb_control_msg(serial->dev,
2149 usb_sndctrlpipe(serial->dev, 0),
2150 USB_REQUEST_ION_WRITE_RAM,
2151 0x40, addr, extAddr, transfer_buffer,
2152 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (result < 0)
2154 break;
2155 length -= current_length;
2156 addr += current_length;
2157 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Alan Cox03f0dbf2008-07-22 11:16:34 +01002160 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return result;
2162}
2163
2164
2165/****************************************************************************
2166 * rom_write
2167 * writes a number of bytes to the Edgeport device's ROM starting at the
2168 * given address.
2169 * If successful returns the number of bytes written, otherwise it returns
2170 * a negative error number of the problem.
2171 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002172static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2173 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 int result;
2176 __u16 current_length;
2177 unsigned char *transfer_buffer;
2178
Alan Cox03f0dbf2008-07-22 11:16:34 +01002179 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002181 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2182 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return -ENOMEM;
2184 }
2185
2186 /* need to split these writes up into 64 byte chunks */
2187 result = 0;
2188 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002189 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002191 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002193 memcpy(transfer_buffer, data, current_length);
2194 result = usb_control_msg(serial->dev,
2195 usb_sndctrlpipe(serial->dev, 0),
2196 USB_REQUEST_ION_WRITE_ROM, 0x40,
2197 addr, extAddr,
2198 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (result < 0)
2200 break;
2201 length -= current_length;
2202 addr += current_length;
2203 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Alan Cox03f0dbf2008-07-22 11:16:34 +01002206 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return result;
2208}
2209
2210
2211/****************************************************************************
2212 * rom_read
2213 * reads a number of bytes from the Edgeport device starting at the given
2214 * address.
2215 * If successful returns the number of bytes read, otherwise it returns
2216 * a negative error number of the problem.
2217 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002218static int rom_read(struct usb_serial *serial, __u16 extAddr,
2219 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 int result;
2222 __u16 current_length;
2223 unsigned char *transfer_buffer;
2224
Alan Cox03f0dbf2008-07-22 11:16:34 +01002225 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002227 dev_err(&serial->dev->dev,
2228 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return -ENOMEM;
2230 }
2231
2232 /* need to split these reads up into 64 byte chunks */
2233 result = 0;
2234 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002235 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002237 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002239 result = usb_control_msg(serial->dev,
2240 usb_rcvctrlpipe(serial->dev, 0),
2241 USB_REQUEST_ION_READ_ROM,
2242 0xC0, addr, extAddr, transfer_buffer,
2243 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (result < 0)
2245 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002246 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 length -= current_length;
2248 addr += current_length;
2249 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Alan Cox03f0dbf2008-07-22 11:16:34 +01002252 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return result;
2254}
2255
2256
2257/****************************************************************************
2258 * send_iosp_ext_cmd
2259 * Is used to send a IOSP message to the Edgeport device
2260 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002261static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2262 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 unsigned char *buffer;
2265 unsigned char *currentCommand;
2266 int length = 0;
2267 int status = 0;
2268
Alan Cox03f0dbf2008-07-22 11:16:34 +01002269 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002271 dev_err(&edge_port->port->dev,
2272 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return -ENOMEM;
2274 }
2275
2276 currentCommand = buffer;
2277
Alan Cox03f0dbf2008-07-22 11:16:34 +01002278 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2279 edge_port->port->number - edge_port->port->serial->minor,
2280 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Alan Cox03f0dbf2008-07-22 11:16:34 +01002282 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (status) {
2284 /* something bad happened, let's free up the memory */
2285 kfree(buffer);
2286 }
2287
2288 return status;
2289}
2290
2291
2292/*****************************************************************************
2293 * write_cmd_usb
2294 * this function writes the given buffer out to the bulk write endpoint.
2295 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002296static int write_cmd_usb(struct edgeport_port *edge_port,
2297 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002299 struct edgeport_serial *edge_serial =
2300 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002301 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int status = 0;
2303 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002305 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002308 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (!urb)
2310 return -ENOMEM;
2311
Oliver Neukum96c706e2007-03-15 15:27:17 +01002312 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002313 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2314 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Alan Cox03f0dbf2008-07-22 11:16:34 +01002316 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2317 usb_sndbulkpipe(edge_serial->serial->dev,
2318 edge_serial->bulk_out_endpoint),
2319 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002321 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 status = usb_submit_urb(urb, GFP_ATOMIC);
2323
2324 if (status) {
2325 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002326 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2327 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 usb_kill_urb(urb);
2329 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002330 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return status;
2332 }
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002335 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002337 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002339 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 status = -EINVAL;
2341 }
2342#endif
2343 return status;
2344}
2345
2346
2347/*****************************************************************************
2348 * send_cmd_write_baud_rate
2349 * this function sends the proper command to change the baud rate of the
2350 * specified port.
2351 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002352static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2353 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002355 struct edgeport_serial *edge_serial =
2356 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002357 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 unsigned char *cmdBuffer;
2359 unsigned char *currCmd;
2360 int cmdLen = 0;
2361 int divisor;
2362 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002363 unsigned char number =
2364 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Adam Kropelinbc71e472007-07-29 11:03:29 -04002366 if (edge_serial->is_epic &&
2367 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002368 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2369 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002370 return 0;
2371 }
2372
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002373 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2374 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002376 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002378 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return status;
2380 }
2381
Alan Cox03f0dbf2008-07-22 11:16:34 +01002382 /* Alloc memory for the string of commands. */
2383 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002385 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return -ENOMEM;
2387 }
2388 currCmd = cmdBuffer;
2389
Alan Cox03f0dbf2008-07-22 11:16:34 +01002390 /* Enable access to divisor latch */
2391 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Alan Cox03f0dbf2008-07-22 11:16:34 +01002393 /* Write the divisor itself */
2394 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2395 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Alan Cox03f0dbf2008-07-22 11:16:34 +01002397 /* Restore original value to disable access to divisor latch */
2398 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2399 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Alan Cox03f0dbf2008-07-22 11:16:34 +01002401 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (status) {
2403 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002404 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406
2407 return status;
2408}
2409
2410
2411/*****************************************************************************
2412 * calc_baud_rate_divisor
2413 * this function calculates the proper baud rate divisor for the specified
2414 * baud rate.
2415 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002416static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 int i;
2419 __u16 custom;
2420
Tobias Klauser52950ed2005-12-11 16:20:08 +01002421 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002422 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *divisor = divisor_table[i].Divisor;
2424 return 0;
2425 }
2426 }
2427
Alan Cox03f0dbf2008-07-22 11:16:34 +01002428 /* We have tried all of the standard baud rates
2429 * lets try to calculate the divisor for this baud rate
2430 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002432 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 custom = (__u16)((230400L + baudrate/2) / baudrate);
2434
2435 *divisor = custom;
2436
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002437 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return 0;
2439 }
2440
2441 return -1;
2442}
2443
2444
2445/*****************************************************************************
2446 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002447 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002449static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2450 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002452 struct edgeport_serial *edge_serial =
2453 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002454 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 unsigned char *cmdBuffer;
2456 unsigned char *currCmd;
2457 unsigned long cmdLen = 0;
2458 int status;
2459
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002460 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2461 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Adam Kropelinbc71e472007-07-29 11:03:29 -04002463 if (edge_serial->is_epic &&
2464 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2465 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002466 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002467 return 0;
2468 }
2469
Adam Kropelinbc71e472007-07-29 11:03:29 -04002470 if (edge_serial->is_epic &&
2471 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2472 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002473 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002474 return 0;
2475 }
2476
Alan Cox03f0dbf2008-07-22 11:16:34 +01002477 /* Alloc memory for the string of commands. */
2478 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2479 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 currCmd = cmdBuffer;
2483
Alan Cox03f0dbf2008-07-22 11:16:34 +01002484 /* Build a cmd in the buffer to write the given register */
2485 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2486 edge_port->port->number - edge_port->port->serial->minor,
2487 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2490 if (status) {
2491 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002492 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
2495 return status;
2496}
2497
2498
2499/*****************************************************************************
2500 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002501 * This routine is called to set the UART on the device to match the
2502 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002504
2505static void change_port_settings(struct tty_struct *tty,
2506 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002508 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002509 struct edgeport_serial *edge_serial =
2510 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 int baud;
2512 unsigned cflag;
2513 __u8 mask = 0xff;
2514 __u8 lData;
2515 __u8 lParity;
2516 __u8 lStop;
2517 __u8 rxFlow;
2518 __u8 txFlow;
2519 int status;
2520
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002521 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002523 if (!edge_port->open &&
2524 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002525 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return;
2527 }
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 cflag = tty->termios->c_cflag;
2530
2531 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002532 case CS5:
2533 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002534 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002535 break;
2536 case CS6:
2537 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002538 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002539 break;
2540 case CS7:
2541 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002542 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002543 break;
2544 default:
2545 case CS8:
2546 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002547 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
2551 lParity = LCR_PAR_NONE;
2552 if (cflag & PARENB) {
2553 if (cflag & CMSPAR) {
2554 if (cflag & PARODD) {
2555 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002556 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 } else {
2558 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002559 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 } else if (cflag & PARODD) {
2562 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002563 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 } else {
2565 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002566 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002569 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571
2572 if (cflag & CSTOPB) {
2573 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002574 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 } else {
2576 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002577 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
2580 /* figure out the flow control settings */
2581 rxFlow = txFlow = 0x00;
2582 if (cflag & CRTSCTS) {
2583 rxFlow |= IOSP_RX_FLOW_RTS;
2584 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002585 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002587 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589
Alan Cox03f0dbf2008-07-22 11:16:34 +01002590 /* if we are implementing XON/XOFF, set the start and stop character
2591 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (I_IXOFF(tty) || I_IXON(tty)) {
2593 unsigned char stop_char = STOP_CHAR(tty);
2594 unsigned char start_char = START_CHAR(tty);
2595
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002596 if ((!edge_serial->is_epic) ||
2597 ((edge_serial->is_epic) &&
2598 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002599 send_iosp_ext_cmd(edge_port,
2600 IOSP_CMD_SET_XON_CHAR, start_char);
2601 send_iosp_ext_cmd(edge_port,
2602 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /* if we are implementing INBOUND XON/XOFF */
2606 if (I_IXOFF(tty)) {
2607 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002608 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2609 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002611 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613
2614 /* if we are implementing OUTBOUND XON/XOFF */
2615 if (I_IXON(tty)) {
2616 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002617 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2618 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002620 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622 }
2623
2624 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002625 if ((!edge_serial->is_epic) ||
2626 ((edge_serial->is_epic) &&
2627 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2628 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2629 if ((!edge_serial->is_epic) ||
2630 ((edge_serial->is_epic) &&
2631 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2632 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634
2635 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2636 edge_port->shadowLCR |= (lData | lParity | lStop);
2637
2638 edge_port->validDataMask = mask;
2639
2640 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002641 status = send_cmd_write_uart_register(edge_port, LCR,
2642 edge_port->shadowLCR);
2643 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 /* set up the MCR register and send it to the EdgePort */
2647 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002648 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002650
2651 status = send_cmd_write_uart_register(edge_port, MCR,
2652 edge_port->shadowMCR);
2653 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 /* Determine divisor based on baud rate */
2657 baud = tty_get_baud_rate(tty);
2658 if (!baud) {
2659 /* pick a default, any default... */
2660 baud = 9600;
2661 }
2662
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002663 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002664 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002665 if (status == -1) {
2666 /* Speed change was not possible - put back the old speed */
2667 baud = tty_termios_baud_rate(old_termios);
2668 tty_encode_baud_rate(tty, baud, baud);
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672
2673/****************************************************************************
2674 * unicode_to_ascii
2675 * Turns a string from Unicode into ASCII.
2676 * Doesn't do a good job with any characters that are outside the normal
2677 * ASCII range, but it's only for debugging...
2678 * NOTE: expects the unicode in LE format
2679 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002680static void unicode_to_ascii(char *string, int buflen,
2681 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
2683 int i;
2684
Pete Zaitcevad933752006-05-22 21:49:44 -07002685 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002687 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Pete Zaitcevad933752006-05-22 21:49:44 -07002689 for (i = 0; i < unicode_size; i++) {
2690 if (i >= buflen)
2691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002693 }
2694 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697
2698/****************************************************************************
2699 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002700 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * structure.
2702 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002703static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002705 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int response;
2707
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002708 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Alan Cox03f0dbf2008-07-22 11:16:34 +01002710 response = rom_read(edge_serial->serial,
2711 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2712 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2713 EDGE_MANUF_DESC_LEN,
2714 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Alan Cox03f0dbf2008-07-22 11:16:34 +01002716 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002718 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002720 dev_dbg(dev, "**Manufacturer Descriptor\n");
2721 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002722 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002723 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002724 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002725 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002726 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002727 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002728 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002729 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002730 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002731 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002732 edge_serial->manuf_descriptor.DescDate[0],
2733 edge_serial->manuf_descriptor.DescDate[1],
2734 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002735 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002736 edge_serial->manuf_descriptor.SerialNumber,
2737 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002738 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002739 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002740 edge_serial->manuf_descriptor.AssemblyNumber,
2741 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002742 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002743 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002744 edge_serial->manuf_descriptor.OemAssyNumber,
2745 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002746 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2747 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002748 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002749 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002750 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002751 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002752 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754}
2755
2756
2757/****************************************************************************
2758 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002759 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 * structure.
2761 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002762static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002764 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 int response;
2766
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002767 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Alan Cox03f0dbf2008-07-22 11:16:34 +01002769 response = rom_read(edge_serial->serial,
2770 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2771 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2772 EDGE_BOOT_DESC_LEN,
2773 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Alan Cox03f0dbf2008-07-22 11:16:34 +01002775 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002776 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002777 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002778 dev_dbg(dev, "**Boot Descriptor:\n");
2779 dev_dbg(dev, " BootCodeLength: %d\n",
2780 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2781 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002782 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002783 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002784 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002785 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002786 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002787 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002788 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002789 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002790 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002791 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002792 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
2794}
2795
2796
2797/****************************************************************************
2798 * load_application_firmware
2799 * This is called to load the application firmware to the device
2800 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002801static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002803 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302804 const struct ihex_binrec *rec;
2805 const struct firmware *fw;
2806 const char *fw_name;
2807 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302809 __u32 Operaddr;
2810 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 switch (edge_serial->product_info.iDownloadFile) {
2813 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302814 fw_info = "downloading firmware version (930)";
2815 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 break;
2817
2818 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302819 fw_info = "downloading firmware version (80251)";
2820 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 break;
2822
2823 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002824 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return;
2826
2827 default:
2828 return;
2829 }
2830
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302831 response = request_ihex_firmware(&fw, fw_name,
2832 &edge_serial->serial->dev->dev);
2833 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002834 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302835 fw_name, response);
2836 return;
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302839 rec = (const struct ihex_binrec *)fw->data;
2840 build = (rec->data[2] << 8) | rec->data[3];
2841
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002842 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302843
Bjørn Mork271c1152011-01-17 14:19:37 +01002844 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2845 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302846 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2847
2848 for (rec = ihex_next_binrec(rec); rec;
2849 rec = ihex_next_binrec(rec)) {
2850 Operaddr = be32_to_cpu(rec->addr);
2851 response = sram_write(edge_serial->serial,
2852 Operaddr >> 16,
2853 Operaddr & 0xFFFF,
2854 be16_to_cpu(rec->len),
2855 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302857 dev_err(&edge_serial->serial->dev->dev,
2858 "sram_write failed (%x, %x, %d)\n",
2859 Operaddr >> 16, Operaddr & 0xFFFF,
2860 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862 }
2863 }
2864
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002865 dev_dbg(dev, "sending exec_dl_code\n");
2866 response = usb_control_msg (edge_serial->serial->dev,
2867 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2868 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2870
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302871 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
2874
2875/****************************************************************************
2876 * edge_startup
2877 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002878static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
2880 struct edgeport_serial *edge_serial;
2881 struct edgeport_port *edge_port;
2882 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002883 struct device *ddev = &serial->dev->dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002884 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002885 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002886 bool interrupt_in_found;
2887 bool bulk_in_found;
2888 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002889 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2890 EDGE_COMPATIBILITY_MASK1,
2891 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 dev = serial->dev;
2894
2895 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002896 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002898 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return -ENOMEM;
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 spin_lock_init(&edge_serial->es_lock);
2902 edge_serial->serial = serial;
2903 usb_set_serial_data(serial, edge_serial);
2904
2905 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002906 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002907 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002908 if (i < 0)
2909 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002910 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002911 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002912 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2915
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002916 /* Read the epic descriptor */
2917 if (get_epic_descriptor(edge_serial) <= 0) {
2918 /* memcpy descriptor to Supports structures */
2919 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2920 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002922 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002923 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002926 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002927
2928 get_product_info(edge_serial);
2929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 /* set the number of ports from the manufacturing description */
2932 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002933 if ((!edge_serial->is_epic) &&
2934 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002935 dev_warn(ddev,
2936 "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 -08002937 edge_serial->product_info.NumPorts,
2938 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
2940
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002941 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002943 /* If not an EPiC device */
2944 if (!edge_serial->is_epic) {
2945 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002946 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002948 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002950 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002951 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002953 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002954
2955 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002956/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002957/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002958 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002959 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302960 edge_serial->product_info.FirmwareMajorVersion,
2961 edge_serial->product_info.FirmwareMinorVersion,
2962 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Alan Cox03f0dbf2008-07-22 11:16:34 +01002964 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * as the structures aren't created yet. */
2966
2967 /* set up our port private structures */
2968 for (i = 0; i < serial->num_ports; ++i) {
Julia Lawall1ac93a32010-05-13 22:00:40 +02002969 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (edge_port == NULL) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002971 dev_err(ddev, "%s - Out of memory\n", __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07002972 for (j = 0; j < i; ++j) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002973 kfree(usb_get_serial_port_data(serial->port[j]));
2974 usb_set_serial_port_data(serial->port[j],
2975 NULL);
Chris Lundbfd5df32006-06-03 13:58:19 -07002976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 usb_set_serial_data(serial, NULL);
2978 kfree(edge_serial);
2979 return -ENOMEM;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 spin_lock_init(&edge_port->ep_lock);
2982 edge_port->port = serial->port[i];
2983 usb_set_serial_port_data(serial->port[i], edge_port);
2984 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002985
2986 response = 0;
2987
2988 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002989 /* EPIC thing, set up our interrupt polling now and our read
2990 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002991 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002992 for (i = 0; i < serial->interface->altsetting[0]
2993 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002994 struct usb_endpoint_descriptor *endpoint;
2995 int buffer_size;
2996
Alan Cox03f0dbf2008-07-22 11:16:34 +01002997 endpoint = &serial->interface->altsetting[0].
2998 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002999 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003000 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001 (usb_endpoint_is_int_in(endpoint))) {
3002 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003003 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003004
3005 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003006 edge_serial->interrupt_read_urb =
3007 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003008 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003009 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003010 return -ENOMEM;
3011 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003012 edge_serial->interrupt_in_buffer =
3013 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003014 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003015 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003016 usb_free_urb(edge_serial->interrupt_read_urb);
3017 return -ENOMEM;
3018 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003019 edge_serial->interrupt_in_endpoint =
3020 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003021
3022 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003023 usb_fill_int_urb(
3024 edge_serial->interrupt_read_urb,
3025 dev,
3026 usb_rcvintpipe(dev,
3027 endpoint->bEndpointAddress),
3028 edge_serial->interrupt_in_buffer,
3029 buffer_size,
3030 edge_interrupt_callback,
3031 edge_serial,
3032 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003033
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003034 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003035 }
3036
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003037 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003038 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003039 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003040 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003041
3042 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003043 edge_serial->read_urb =
3044 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003045 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003046 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003047 return -ENOMEM;
3048 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003049 edge_serial->bulk_in_buffer =
3050 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003051 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003052 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003053 usb_free_urb(edge_serial->read_urb);
3054 return -ENOMEM;
3055 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003056 edge_serial->bulk_in_endpoint =
3057 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003058
3059 /* set up our bulk in urb */
3060 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003061 usb_rcvbulkpipe(dev,
3062 endpoint->bEndpointAddress),
3063 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003064 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003065 edge_bulk_in_callback,
3066 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003067 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003068 }
3069
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003070 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003071 (usb_endpoint_is_bulk_out(endpoint))) {
3072 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003073 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003074 edge_serial->bulk_out_endpoint =
3075 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003076 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003077 }
3078 }
3079
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003080 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003081 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003082 return -ENODEV;
3083 }
3084
3085 /* start interrupt read for this edgeport this interrupt will
3086 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003087 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3088 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003089 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003090 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003091 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003092 }
3093 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
3096
3097/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003098 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 * This function is called whenever the device is removed from the usb bus.
3100 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003101static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003103 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003106 /* free up our endpoint stuff */
3107 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003108 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003109 usb_free_urb(edge_serial->interrupt_read_urb);
3110 kfree(edge_serial->interrupt_in_buffer);
3111
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003112 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003113 usb_free_urb(edge_serial->read_urb);
3114 kfree(edge_serial->bulk_in_buffer);
3115 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003116}
3117
3118
3119/****************************************************************************
3120 * edge_release
3121 * This function is called when the device structure is deallocated.
3122 ****************************************************************************/
3123static void edge_release(struct usb_serial *serial)
3124{
3125 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3126 int i;
3127
Alan Sternf9c99bb2009-06-02 11:53:55 -04003128 for (i = 0; i < serial->num_ports; ++i)
3129 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003130
3131 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132}
3133
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003134module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Alan Cox03f0dbf2008-07-22 11:16:34 +01003136MODULE_AUTHOR(DRIVER_AUTHOR);
3137MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303139MODULE_FIRMWARE("edgeport/boot.fw");
3140MODULE_FIRMWARE("edgeport/boot2.fw");
3141MODULE_FIRMWARE("edgeport/down.fw");
3142MODULE_FIRMWARE("edgeport/down2.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144module_param(debug, bool, S_IRUGO | S_IWUSR);
3145MODULE_PARM_DESC(debug, "Debug enabled or not");