blob: 5acc0d13864a400344a012400f20bc989fa23574 [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
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800193/* Number of outstanding Command Write Urbs */
194static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196
197/* local function prototypes */
198
199/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100200static void edge_interrupt_callback(struct urb *urb);
201static void edge_bulk_in_callback(struct urb *urb);
202static void edge_bulk_out_data_callback(struct urb *urb);
203static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700206static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100207static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100208static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
209 const unsigned char *buf, int count);
210static int edge_write_room(struct tty_struct *tty);
211static int edge_chars_in_buffer(struct tty_struct *tty);
212static void edge_throttle(struct tty_struct *tty);
213static void edge_unthrottle(struct tty_struct *tty);
214static void edge_set_termios(struct tty_struct *tty,
215 struct usb_serial_port *port,
216 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000217static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100218 unsigned int cmd, unsigned long arg);
219static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000220static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000221static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100222 unsigned int set, unsigned int clear);
Alan Cox0bca1b92010-09-16 18:21:40 +0100223static int edge_get_icount(struct tty_struct *tty,
224 struct serial_icounter_struct *icount);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100225static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400226static void edge_disconnect(struct usb_serial *serial);
227static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200228static int edge_port_probe(struct usb_serial_port *port);
229static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231#include "io_tables.h" /* all of the devices that this driver supports */
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Cox03f0dbf2008-07-22 11:16:34 +0100235static void process_rcvd_data(struct edgeport_serial *edge_serial,
236 unsigned char *buffer, __u16 bufferLength);
237static void process_rcvd_status(struct edgeport_serial *edge_serial,
238 __u8 byte2, __u8 byte3);
239static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
240 unsigned char *data, int length);
241static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
242static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
243 __u8 lsr, __u8 data);
244static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
245 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700246static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100247static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
248 int baudRate);
249static void change_port_settings(struct tty_struct *tty,
250 struct edgeport_port *edge_port,
251 struct ktermios *old_termios);
252static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
253 __u8 regNum, __u8 regValue);
254static int write_cmd_usb(struct edgeport_port *edge_port,
255 unsigned char *buffer, int writeLength);
256static void send_more_port_data(struct edgeport_serial *edge_serial,
257 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Alan Cox03f0dbf2008-07-22 11:16:34 +0100259static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data);
261static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, __u8 *data);
263static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
264 __u16 length, const __u8 *data);
265static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
266static void get_boot_desc(struct edgeport_serial *edge_serial);
267static void load_application_firmware(struct edgeport_serial *edge_serial);
268
269static void unicode_to_ascii(char *string, int buflen,
270 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272
Alan Cox03f0dbf2008-07-22 11:16:34 +0100273/* ************************************************************************ */
274/* ************************************************************************ */
275/* ************************************************************************ */
276/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/************************************************************************
279 * *
280 * update_edgeport_E2PROM() Compare current versions of *
281 * Boot ROM and Manufacture *
282 * Descriptors with versions *
283 * embedded in this driver *
284 * *
285 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100286static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700288 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 __u32 BootCurVer;
290 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530291 __u8 BootMajorVersion;
292 __u8 BootMinorVersion;
293 __u16 BootBuildNumber;
294 __u32 Bootaddr;
295 const struct ihex_binrec *rec;
296 const struct firmware *fw;
297 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int response;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100301 case EDGE_DOWNLOAD_FILE_I930:
302 fw_name = "edgeport/boot.fw";
303 break;
304 case EDGE_DOWNLOAD_FILE_80251:
305 fw_name = "edgeport/boot2.fw";
306 break;
307 default:
308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530311 response = request_ihex_firmware(&fw, fw_name,
312 &edge_serial->serial->dev->dev);
313 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700314 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530315 fw_name, response);
316 return;
317 }
318
319 rec = (const struct ihex_binrec *)fw->data;
320 BootMajorVersion = rec->data[0];
321 BootMinorVersion = rec->data[1];
322 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
323
Alan Cox03f0dbf2008-07-22 11:16:34 +0100324 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
326 (edge_serial->boot_descriptor.MinorVersion << 16) +
327 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
328
329 BootNewVer = (BootMajorVersion << 24) +
330 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530331 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700333 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 edge_serial->boot_descriptor.MajorVersion,
335 edge_serial->boot_descriptor.MinorVersion,
336 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
337
338
339 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700340 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 edge_serial->boot_descriptor.MajorVersion,
342 edge_serial->boot_descriptor.MinorVersion,
343 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530344 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700346 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530348 for (rec = ihex_next_binrec(rec); rec;
349 rec = ihex_next_binrec(rec)) {
350 Bootaddr = be32_to_cpu(rec->addr);
351 response = rom_write(edge_serial->serial,
352 Bootaddr >> 16,
353 Bootaddr & 0xFFFF,
354 be16_to_cpu(rec->len),
355 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530357 dev_err(&edge_serial->serial->dev->dev,
358 "rom_write failed (%x, %x, %d)\n",
359 Bootaddr >> 16, Bootaddr & 0xFFFF,
360 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 }
363 }
364 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700365 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530367 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#if 0
371/************************************************************************
372 *
373 * Get string descriptor from device
374 *
375 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100376static int get_string_desc(struct usb_device *dev, int Id,
377 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct usb_string_descriptor StringDesc;
380 struct usb_string_descriptor *pStringDesc;
381
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700382 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Alan Cox03f0dbf2008-07-22 11:16:34 +0100384 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
385 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Alan Cox03f0dbf2008-07-22 11:16:34 +0100388 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
389 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Alan Cox03f0dbf2008-07-22 11:16:34 +0100392 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
393 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 kfree(pStringDesc);
395 return -1;
396 }
397
398 *pRetDesc = pStringDesc;
399 return 0;
400}
401#endif
402
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700403static void dump_product_info(struct edgeport_serial *edge_serial,
404 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800405{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700406 struct device *dev = &edge_serial->serial->dev->dev;
407
Alan Cox03f0dbf2008-07-22 11:16:34 +0100408 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700409 dev_dbg(dev, "**Product Information:\n");
410 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
411 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
412 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
413 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
414 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
415 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
416 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
417 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
418 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
419 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
420 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
421 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
422 product_info->BootMajorVersion,
423 product_info->BootMinorVersion,
424 le16_to_cpu(product_info->BootBuildNumber));
425 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
426 product_info->FirmwareMajorVersion,
427 product_info->FirmwareMinorVersion,
428 le16_to_cpu(product_info->FirmwareBuildNumber));
429 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
430 product_info->ManufactureDescDate[0],
431 product_info->ManufactureDescDate[1],
432 product_info->ManufactureDescDate[2]+1900);
433 dev_dbg(dev, " iDownloadFile 0x%x\n",
434 product_info->iDownloadFile);
435 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static void get_product_info(struct edgeport_serial *edge_serial)
439{
440 struct edgeport_product_info *product_info = &edge_serial->product_info;
441
Alan Cox03f0dbf2008-07-22 11:16:34 +0100442 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alan Cox03f0dbf2008-07-22 11:16:34 +0100444 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
445 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
446 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alan Cox03f0dbf2008-07-22 11:16:34 +0100448 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
449 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
450 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
451 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Cox03f0dbf2008-07-22 11:16:34 +0100453 product_info->BootMajorVersion =
454 edge_serial->boot_descriptor.MajorVersion;
455 product_info->BootMinorVersion =
456 edge_serial->boot_descriptor.MinorVersion;
457 product_info->BootBuildNumber =
458 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Alan Cox03f0dbf2008-07-22 11:16:34 +0100460 memcpy(product_info->ManufactureDescDate,
461 edge_serial->manuf_descriptor.DescDate,
462 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Alan Cox03f0dbf2008-07-22 11:16:34 +0100464 /* check if this is 2nd generation hardware */
465 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
466 & ION_DEVICE_ID_80251_NETCHIP)
467 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
468 else
469 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700470
Alan Cox03f0dbf2008-07-22 11:16:34 +0100471 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100473 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
474 case ION_DEVICE_ID_EDGEPORT_4T:
475 case ION_DEVICE_ID_EDGEPORT_4:
476 case ION_DEVICE_ID_EDGEPORT_2:
477 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
478 case ION_DEVICE_ID_EDGEPORT_8:
479 case ION_DEVICE_ID_EDGEPORT_421:
480 case ION_DEVICE_ID_EDGEPORT_21:
481 case ION_DEVICE_ID_EDGEPORT_2_DIN:
482 case ION_DEVICE_ID_EDGEPORT_4_DIN:
483 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
484 product_info->IsRS232 = 1;
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Alan Cox03f0dbf2008-07-22 11:16:34 +0100487 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
488 product_info->IsRS422 = 1;
489 product_info->IsRS485 = 1;
490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alan Cox03f0dbf2008-07-22 11:16:34 +0100492 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
493 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
494 product_info->IsRS422 = 1;
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700498 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800501static int get_epic_descriptor(struct edgeport_serial *ep)
502{
503 int result;
504 struct usb_serial *serial = ep->serial;
505 struct edgeport_product_info *product_info = &ep->product_info;
506 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
507 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700508 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800509
510 ep->is_epic = 0;
511 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
512 USB_REQUEST_ION_GET_EPIC_DESC,
513 0xC0, 0x00, 0x00,
514 &ep->epic_descriptor,
515 sizeof(struct edge_compatibility_descriptor),
516 300);
517
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800518 if (result > 0) {
519 ep->is_epic = 1;
520 memset(product_info, 0, sizeof(struct edgeport_product_info));
521
Alan Cox03f0dbf2008-07-22 11:16:34 +0100522 product_info->NumPorts = epic->NumPorts;
523 product_info->ProdInfoVer = 0;
524 product_info->FirmwareMajorVersion = epic->MajorVersion;
525 product_info->FirmwareMinorVersion = epic->MinorVersion;
526 product_info->FirmwareBuildNumber = epic->BuildNumber;
527 product_info->iDownloadFile = epic->iDownloadFile;
528 product_info->EpicVer = epic->EpicVer;
529 product_info->Epic = epic->Supports;
530 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700531 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800532
533 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700534 dev_dbg(dev, "**EPIC descriptor:\n");
535 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
543 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
544 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
545 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
546 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
547 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800548 }
549
550 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553
554/************************************************************************/
555/************************************************************************/
556/* U S B C A L L B A C K F U N C T I O N S */
557/* U S B C A L L B A C K F U N C T I O N S */
558/************************************************************************/
559/************************************************************************/
560
561/*****************************************************************************
562 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100563 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * interrupt endpoint.
565 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100566static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700568 struct edgeport_serial *edge_serial = urb->context;
569 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct edgeport_port *edge_port;
571 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100572 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 unsigned char *data = urb->transfer_buffer;
574 int length = urb->actual_length;
575 int bytes_avail;
576 int position;
577 int txCredits;
578 int portNumber;
579 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700580 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700582 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 case 0:
584 /* success */
585 break;
586 case -ECONNRESET:
587 case -ENOENT:
588 case -ESHUTDOWN:
589 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700590 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return;
592 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700593 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto exit;
595 }
596
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700597 dev = &edge_serial->serial->dev->dev;
598
Alan Cox03f0dbf2008-07-22 11:16:34 +0100599 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100601 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (length > 1) {
604 bytes_avail = data[0] | (data[1] << 8);
605 if (bytes_avail) {
606 spin_lock(&edge_serial->es_lock);
607 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700608 dev_dbg(dev,
609 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
610 __func__, bytes_avail,
611 edge_serial->rxBytesAvail,
612 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (edge_serial->rxBytesAvail > 0 &&
615 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700616 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100617 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Alan Cox03f0dbf2008-07-22 11:16:34 +0100619 /* we have pending bytes on the
620 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
622 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700623 dev_err(dev,
624 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
625 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100626 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 }
629 spin_unlock(&edge_serial->es_lock);
630 }
631 }
632 /* grab the txcredits for the ports if available */
633 position = 2;
634 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100635 while ((position < length) &&
636 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 txCredits = data[position] | (data[position+1] << 8);
638 if (txCredits) {
639 port = edge_serial->serial->port[portNumber];
640 edge_port = usb_get_serial_port_data(port);
641 if (edge_port->open) {
642 spin_lock(&edge_port->ep_lock);
643 edge_port->txCredits += txCredits;
644 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700645 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
646 __func__, portNumber,
647 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Alan Cox03f0dbf2008-07-22 11:16:34 +0100649 /* tell the tty driver that something
650 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100651 tty = tty_port_tty_get(
652 &edge_port->port->port);
653 if (tty) {
654 tty_wakeup(tty);
655 tty_kref_put(tty);
656 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100657 /* Since we have more credit, check
658 if more data can be sent */
659 send_more_port_data(edge_serial,
660 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 }
663 position += 2;
664 ++portNumber;
665 }
666 }
667
668exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100669 result = usb_submit_urb(urb, GFP_ATOMIC);
670 if (result)
671 dev_err(&urb->dev->dev,
672 "%s - Error %d submitting control urb\n",
673 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676
677/*****************************************************************************
678 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100679 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * bulk in endpoint.
681 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100682static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Ming Leicdc97792008-02-24 18:41:47 +0800684 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700685 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700687 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700689 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700691 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700692 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
693 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100694 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return;
696 }
697
698 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700699 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100700 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
702 }
703
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700704 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 raw_data_length = urb->actual_length;
706
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100707 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 spin_lock(&edge_serial->es_lock);
710
711 /* decrement our rxBytes available by the number that we just got */
712 edge_serial->rxBytesAvail -= raw_data_length;
713
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700714 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
715 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Alan Cox03f0dbf2008-07-22 11:16:34 +0100717 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* check to see if there's any more data for us to read */
720 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700721 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700722 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
723 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700724 dev_err(dev,
725 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
726 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100727 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100730 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 spin_unlock(&edge_serial->es_lock);
734}
735
736
737/*****************************************************************************
738 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100739 * this is the callback function for when we have finished sending
740 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100742static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Ming Leicdc97792008-02-24 18:41:47 +0800744 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700746 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700748 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700749 dev_dbg(&urb->dev->dev,
750 "%s - nonzero write bulk status received: %d\n",
751 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Alan Cox4a90f092008-10-13 10:39:46 +0100754 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100757 /* let the tty driver wakeup if it has a special
758 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 tty_wakeup(tty);
760 }
Alan Cox4a90f092008-10-13 10:39:46 +0100761 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Alan Cox03f0dbf2008-07-22 11:16:34 +0100763 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100764 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Alan Cox03f0dbf2008-07-22 11:16:34 +0100766 /* Check if more data needs to be sent */
767 send_more_port_data((struct edgeport_serial *)
768 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771
772/*****************************************************************************
773 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100774 * this is the callback function for when we have finished sending a
775 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100777static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Ming Leicdc97792008-02-24 18:41:47 +0800779 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct tty_struct *tty;
781 int status = urb->status;
782
Oliver Neukum96c706e2007-03-15 15:27:17 +0100783 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700784 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
785 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787
788 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700789 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100792 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700795 dev_dbg(&urb->dev->dev,
796 "%s - nonzero write bulk status received: %d\n",
797 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
799 }
800
801 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100802 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* tell the tty driver that something has changed */
805 if (tty && edge_port->open)
806 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100807 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100810 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 wake_up(&edge_port->wait_command);
812}
813
814
815/*****************************************************************************
816 * Driver tty interface functions
817 *****************************************************************************/
818
819/*****************************************************************************
820 * SerialOpen
821 * this function is called by the tty driver when a port is opened
822 * If successful, we return 0
823 * Otherwise we return a negative error number.
824 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700825static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700828 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct usb_serial *serial;
830 struct edgeport_serial *edge_serial;
831 int response;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (edge_port == NULL)
834 return -ENODEV;
835
Alan Cox03f0dbf2008-07-22 11:16:34 +0100836 /* see if we've set up our endpoint info yet (can't set it up
837 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 serial = port->serial;
839 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100840 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (edge_serial->interrupt_in_buffer == NULL) {
843 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100846 edge_serial->interrupt_in_buffer =
847 port0->interrupt_in_buffer;
848 edge_serial->interrupt_in_endpoint =
849 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
851 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100852 edge_serial->bulk_in_endpoint =
853 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100855 edge_serial->bulk_out_endpoint =
856 port0->bulk_out_endpointAddress;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* set up our interrupt urb */
859 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100860 serial->dev,
861 usb_rcvintpipe(serial->dev,
862 port0->interrupt_in_endpointAddress),
863 port0->interrupt_in_buffer,
864 edge_serial->interrupt_read_urb->transfer_buffer_length,
865 edge_interrupt_callback, edge_serial,
866 edge_serial->interrupt_read_urb->interval);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* set up our bulk in urb */
869 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100870 usb_rcvbulkpipe(serial->dev,
871 port0->bulk_in_endpointAddress),
872 port0->bulk_in_buffer,
873 edge_serial->read_urb->transfer_buffer_length,
874 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100875 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100878 * this interrupt will continue as long
879 * as the edgeport is connected */
880 response = usb_submit_urb(edge_serial->interrupt_read_urb,
881 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700883 dev_err(dev, "%s - Error %d submitting control urb\n",
884 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* initialize our wait queues */
889 init_waitqueue_head(&edge_port->wait_open);
890 init_waitqueue_head(&edge_port->wait_chase);
891 init_waitqueue_head(&edge_port->delta_msr_wait);
892 init_waitqueue_head(&edge_port->wait_command);
893
894 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100895 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100898 edge_port->txCredits = 0; /* Can't send any data yet */
899 /* Must always set this bit to enable ints! */
900 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100901 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100904 edge_port->openPending = true;
905 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100906 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700909 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100910 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENODEV;
912 }
913
914 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100915 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
916 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100918 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700920 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100921 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -ENODEV;
923 }
924
925 /* create the txfifo */
926 edge_port->txfifo.head = 0;
927 edge_port->txfifo.tail = 0;
928 edge_port->txfifo.count = 0;
929 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100930 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700933 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100934 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENOMEM;
936 }
937
938 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100939 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100940 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700943 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100944 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -ENOMEM;
946 }
947
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700948 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
949 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 return 0;
952}
953
954
955/************************************************************************
956 *
957 * block_until_chase_response
958 *
959 * This function will block the close until one of the following:
960 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800961 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
963 *
964 ************************************************************************/
965static void block_until_chase_response(struct edgeport_port *edge_port)
966{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700967 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 DEFINE_WAIT(wait);
969 __u16 lastCredits;
970 int timeout = 1*HZ;
971 int loop = 10;
972
973 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100974 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 lastCredits = edge_port->txCredits;
976
Alan Cox03f0dbf2008-07-22 11:16:34 +0100977 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100978 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700979 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Alan Cox03f0dbf2008-07-22 11:16:34 +0100981 /* did we get all of our credit back? */
982 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700983 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return;
985 }
986 }
987
Alan Cox03f0dbf2008-07-22 11:16:34 +0100988 /* Block the thread for a while */
989 prepare_to_wait(&edge_port->wait_chase, &wait,
990 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 schedule_timeout(timeout);
992 finish_wait(&edge_port->wait_chase, &wait);
993
994 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100995 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 loop--;
997 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100998 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700999 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return;
1001 }
1002 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001003 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001004 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001005 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 loop = 10;
1007 }
1008 }
1009}
1010
1011
1012/************************************************************************
1013 *
1014 * block_until_tx_empty
1015 *
1016 * This function will block the close until one of the following:
1017 * 1. TX count are 0
1018 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001019 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 *
1021 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001022static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001024 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 DEFINE_WAIT(wait);
1026 struct TxFifo *fifo = &edge_port->txfifo;
1027 __u32 lastCount;
1028 int timeout = HZ/10;
1029 int loop = 30;
1030
1031 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001032 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 lastCount = fifo->count;
1034
Alan Cox03f0dbf2008-07-22 11:16:34 +01001035 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001037 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039 }
1040
Alan Cox03f0dbf2008-07-22 11:16:34 +01001041 /* Block the thread for a while */
1042 prepare_to_wait(&edge_port->wait_chase, &wait,
1043 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 schedule_timeout(timeout);
1045 finish_wait(&edge_port->wait_chase, &wait);
1046
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001047 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001050 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 loop--;
1052 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001053 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return;
1055 }
1056 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001057 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 loop = 30;
1059 }
1060 }
1061}
1062
1063
1064/*****************************************************************************
1065 * edge_close
1066 * this function is called by the tty driver when a port is closed
1067 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001068static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 struct edgeport_serial *edge_serial;
1071 struct edgeport_port *edge_port;
1072 int status;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 edge_serial = usb_get_serial_data(port->serial);
1075 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001076 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001078
1079 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 block_until_tx_empty(edge_port);
1081
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001082 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001084 if ((!edge_serial->is_epic) ||
1085 ((edge_serial->is_epic) &&
1086 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1087 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001088 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001090 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001091 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1092 if (status == 0)
1093 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001094 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001095 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001096 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001099 if ((!edge_serial->is_epic) ||
1100 ((edge_serial->is_epic) &&
1101 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1102 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001103 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001104 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Alan Cox03f0dbf2008-07-22 11:16:34 +01001107 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001108 edge_port->closePending = false;
1109 edge_port->open = false;
1110 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001112 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001115 /* if this urb had a transfer buffer already
1116 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001117 kfree(edge_port->write_urb->transfer_buffer);
1118 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 edge_port->write_urb = NULL;
1120 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001121 kfree(edge_port->txfifo.fifo);
1122 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125/*****************************************************************************
1126 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001127 * this function is called by the tty driver when data should be written
1128 * to the port.
1129 * If successful, we return the number of bytes written, otherwise we
1130 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001132static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1133 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1136 struct TxFifo *fifo;
1137 int copySize;
1138 int bytesleft;
1139 int firsthalf;
1140 int secondhalf;
1141 unsigned long flags;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (edge_port == NULL)
1144 return -ENODEV;
1145
Alan Cox03f0dbf2008-07-22 11:16:34 +01001146 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 fifo = &edge_port->txfifo;
1148
1149 spin_lock_irqsave(&edge_port->ep_lock, flags);
1150
Alan Cox03f0dbf2008-07-22 11:16:34 +01001151 /* calculate number of bytes to put in fifo */
1152 copySize = min((unsigned int)count,
1153 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001155 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1156 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001157 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Alan Cox03f0dbf2008-07-22 11:16:34 +01001159 /* catch writes of 0 bytes which the tty driver likes to give us,
1160 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001162 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto finish_write;
1164 }
1165
Alan Cox03f0dbf2008-07-22 11:16:34 +01001166 /* queue the data
1167 * since we can never overflow the buffer we do not have to check for a
1168 * full condition
1169 *
1170 * the copy is done is two parts -- first fill to the end of the buffer
1171 * then copy the reset from the start of the buffer
1172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001174 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001175 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1176 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* now copy our data */
1179 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001180 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Alan Cox03f0dbf2008-07-22 11:16:34 +01001182 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 fifo->head += firsthalf;
1184 fifo->count += firsthalf;
1185
Alan Cox03f0dbf2008-07-22 11:16:34 +01001186 /* wrap the index */
1187 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 secondhalf = copySize-firsthalf;
1191
1192 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001193 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001195 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001196 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 fifo->count += secondhalf;
1198 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001199 /* No need to check for wrap since we can not get to end of
1200 * the fifo in this part
1201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
1204finish_write:
1205 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1206
Alan Cox03f0dbf2008-07-22 11:16:34 +01001207 send_more_port_data((struct edgeport_serial *)
1208 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001210 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1211 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alan Cox03f0dbf2008-07-22 11:16:34 +01001213 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216
1217/************************************************************************
1218 *
1219 * send_more_port_data()
1220 *
1221 * This routine attempts to write additional UART transmit data
1222 * to a port over the USB bulk pipe. It is called (1) when new
1223 * data has been written to a port's TxBuffer from higher layers
1224 * (2) when the peripheral sends us additional TxCredits indicating
1225 * that it can accept more Tx data for a given port; and (3) when
1226 * a bulk write completes successfully and we want to see if we
1227 * can transmit more.
1228 *
1229 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001230static void send_more_port_data(struct edgeport_serial *edge_serial,
1231 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001234 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct urb *urb;
1236 unsigned char *buffer;
1237 int status;
1238 int count;
1239 int bytesleft;
1240 int firsthalf;
1241 int secondhalf;
1242 unsigned long flags;
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_lock_irqsave(&edge_port->ep_lock, flags);
1245
1246 if (edge_port->write_in_progress ||
1247 !edge_port->open ||
1248 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001249 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1250 __func__, edge_port->port->number,
1251 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 goto exit_send;
1253 }
1254
Alan Cox03f0dbf2008-07-22 11:16:34 +01001255 /* since the amount of data in the fifo will always fit into the
1256 * edgeport buffer we do not need to check the write length
1257 *
1258 * Do we have enough credits for this port to make it worthwhile
1259 * to bother queueing a write. If it's too small, say a few bytes,
1260 * it's better to wait for more credits so we can do a larger write.
1261 */
1262 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 -07001263 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001264 __func__, edge_port->port->number, fifo->count,
1265 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 goto exit_send;
1267 }
1268
Alan Cox03f0dbf2008-07-22 11:16:34 +01001269 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001270 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Alan Cox03f0dbf2008-07-22 11:16:34 +01001272 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 urb = edge_port->write_urb;
1274
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001275 /* make sure transfer buffer is freed */
1276 kfree(urb->transfer_buffer);
1277 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Alan Cox03f0dbf2008-07-22 11:16:34 +01001279 /* build the data header for the buffer and port that we are about
1280 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001282 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001284 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001285 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001286 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto exit_send;
1288 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001289 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1290 - edge_port->port->serial->minor, count);
1291 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1292 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 /* now copy our data */
1295 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001296 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1298 fifo->tail += firsthalf;
1299 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001300 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 secondhalf = count-firsthalf;
1304 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001305 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1306 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 fifo->tail += secondhalf;
1308 fifo->count -= secondhalf;
1309 }
1310
1311 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001312 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001315 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1316 usb_sndbulkpipe(edge_serial->serial->dev,
1317 edge_serial->bulk_out_endpoint),
1318 buffer, count+2,
1319 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* decrement the number of credits we have by the number we just sent */
1322 edge_port->txCredits -= count;
1323 edge_port->icount.tx += count;
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 status = usb_submit_urb(urb, GFP_ATOMIC);
1326 if (status) {
1327 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001328 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001329 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1330 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001331 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* revert the credits as something bad happened. */
1334 edge_port->txCredits += count;
1335 edge_port->icount.tx -= count;
1336 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001337 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1338 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340exit_send:
1341 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1342}
1343
1344
1345/*****************************************************************************
1346 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001347 * this function is called by the tty driver when it wants to know how
1348 * many bytes of data we can accept for a specific port. If successful,
1349 * we return the amount of room that we have for this port (the txCredits)
1350 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001352static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Alan Cox95da3102008-07-22 11:09:07 +01001354 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1356 int room;
1357 unsigned long flags;
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001360 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001361 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001365 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Alan Cox03f0dbf2008-07-22 11:16:34 +01001369 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 spin_lock_irqsave(&edge_port->ep_lock, flags);
1371 room = edge_port->txCredits - edge_port->txfifo.count;
1372 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1373
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001374 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return room;
1376}
1377
1378
1379/*****************************************************************************
1380 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001381 * this function is called by the tty driver when it wants to know how
1382 * many bytes of data we currently have outstanding in the port (data that
1383 * has been written, but hasn't made it out the port yet)
1384 * If successful, we return the number of bytes left to be written in the
1385 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * Otherwise we return a negative error number.
1387 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001388static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Alan Cox95da3102008-07-22 11:09:07 +01001390 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1392 int num_chars;
1393 unsigned long flags;
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001396 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001397 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001401 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
1405 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001406 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1407 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1409 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001410 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1411 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
1414 return num_chars;
1415}
1416
1417
1418/*****************************************************************************
1419 * SerialThrottle
1420 * this function is called by the tty driver when it wants to stop the data
1421 * being read from the port.
1422 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001423static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Alan Cox95da3102008-07-22 11:09:07 +01001425 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int status;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (edge_port == NULL)
1430 return;
1431
1432 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001433 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return;
1435 }
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* if we are implementing XON/XOFF, send the stop character */
1438 if (I_IXOFF(tty)) {
1439 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001440 status = edge_write(tty, port, &stop_char, 1);
1441 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
1445 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001446 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001448 status = send_cmd_write_uart_register(edge_port, MCR,
1449 edge_port->shadowMCR);
1450 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455
1456/*****************************************************************************
1457 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001458 * this function is called by the tty driver when it wants to resume the
1459 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001461static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Alan Cox95da3102008-07-22 11:09:07 +01001463 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int status;
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (edge_port == NULL)
1468 return;
1469
1470 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001471 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return;
1473 }
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* if we are implementing XON/XOFF, send the start character */
1476 if (I_IXOFF(tty)) {
1477 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001478 status = edge_write(tty, port, &start_char, 1);
1479 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001483 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001485 send_cmd_write_uart_register(edge_port, MCR,
1486 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490
1491/*****************************************************************************
1492 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001493 * this function is called by the tty driver when it wants to change
1494 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001496static void edge_set_termios(struct tty_struct *tty,
1497 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 unsigned int cflag;
1501
Alan Coxadc8d742012-07-14 15:31:47 +01001502 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001503 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001504 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 -07001505
1506 if (edge_port == NULL)
1507 return;
1508
1509 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001510 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return;
1512 }
1513
1514 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001515 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518
1519/*****************************************************************************
1520 * get_lsr_info - get line status register info
1521 *
1522 * Purpose: Let user call ioctl() to get info when the UART physically
1523 * is emptied. On bus types like RS485, the transmitter must
1524 * release the bus after transmitting. This must be done when
1525 * the transmit shift register is empty, not be done when the
1526 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001527 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001529static int get_lsr_info(struct edgeport_port *edge_port,
1530 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 unsigned int result = 0;
1533 unsigned long flags;
1534
1535 spin_lock_irqsave(&edge_port->ep_lock, flags);
1536 if (edge_port->maxTxCredits == edge_port->txCredits &&
1537 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001538 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 result = TIOCSER_TEMT;
1540 }
1541 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1542
1543 if (copy_to_user(value, &result, sizeof(int)))
1544 return -EFAULT;
1545 return 0;
1546}
1547
Alan Cox20b9d172011-02-14 16:26:50 +00001548static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001549 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Alan Cox95da3102008-07-22 11:09:07 +01001551 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1553 unsigned int mcr;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 mcr = edge_port->shadowMCR;
1556 if (set & TIOCM_RTS)
1557 mcr |= MCR_RTS;
1558 if (set & TIOCM_DTR)
1559 mcr |= MCR_DTR;
1560 if (set & TIOCM_LOOP)
1561 mcr |= MCR_LOOPBACK;
1562
1563 if (clear & TIOCM_RTS)
1564 mcr &= ~MCR_RTS;
1565 if (clear & TIOCM_DTR)
1566 mcr &= ~MCR_DTR;
1567 if (clear & TIOCM_LOOP)
1568 mcr &= ~MCR_LOOPBACK;
1569
1570 edge_port->shadowMCR = mcr;
1571
1572 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1573
1574 return 0;
1575}
1576
Alan Cox60b33c12011-02-14 16:26:14 +00001577static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Alan Cox95da3102008-07-22 11:09:07 +01001579 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1581 unsigned int result = 0;
1582 unsigned int msr;
1583 unsigned int mcr;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 msr = edge_port->shadowMSR;
1586 mcr = edge_port->shadowMCR;
1587 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1588 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1589 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1590 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1591 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1592 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return result;
1595}
1596
Alan Cox0bca1b92010-09-16 18:21:40 +01001597static int edge_get_icount(struct tty_struct *tty,
1598 struct serial_icounter_struct *icount)
1599{
1600 struct usb_serial_port *port = tty->driver_data;
1601 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1602 struct async_icount cnow;
1603 cnow = edge_port->icount;
1604
1605 icount->cts = cnow.cts;
1606 icount->dsr = cnow.dsr;
1607 icount->rng = cnow.rng;
1608 icount->dcd = cnow.dcd;
1609 icount->rx = cnow.rx;
1610 icount->tx = cnow.tx;
1611 icount->frame = cnow.frame;
1612 icount->overrun = cnow.overrun;
1613 icount->parity = cnow.parity;
1614 icount->brk = cnow.brk;
1615 icount->buf_overrun = cnow.buf_overrun;
1616
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001617 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1618 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001619 return 0;
1620}
1621
Alan Cox03f0dbf2008-07-22 11:16:34 +01001622static int get_serial_info(struct edgeport_port *edge_port,
1623 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct serial_struct tmp;
1626
1627 if (!retinfo)
1628 return -EFAULT;
1629
1630 memset(&tmp, 0, sizeof(tmp));
1631
1632 tmp.type = PORT_16550A;
1633 tmp.line = edge_port->port->serial->minor;
1634 tmp.port = edge_port->port->number;
1635 tmp.irq = 0;
1636 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1637 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1638 tmp.baud_base = 9600;
1639 tmp.close_delay = 5*HZ;
1640 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1643 return -EFAULT;
1644 return 0;
1645}
1646
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648/*****************************************************************************
1649 * SerialIoctl
1650 * this function handles any ioctl calls to the driver
1651 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001652static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001653 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Alan Cox95da3102008-07-22 11:09:07 +01001655 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 DEFINE_WAIT(wait);
1657 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1658 struct async_icount cnow;
1659 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001661 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001664 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001665 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001666 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Alan Cox03f0dbf2008-07-22 11:16:34 +01001668 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001669 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001670 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Alan Cox03f0dbf2008-07-22 11:16:34 +01001672 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001673 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001674 cprev = edge_port->icount;
1675 while (1) {
1676 prepare_to_wait(&edge_port->delta_msr_wait,
1677 &wait, TASK_INTERRUPTIBLE);
1678 schedule();
1679 finish_wait(&edge_port->delta_msr_wait, &wait);
1680 /* see if a signal did it */
1681 if (signal_pending(current))
1682 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001684 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1685 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1686 return -EIO; /* no change => error */
1687 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1688 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1689 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1690 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1691 return 0;
1692 }
1693 cprev = cnow;
1694 }
1695 /* NOTREACHED */
1696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return -ENOIOCTLCMD;
1700}
1701
1702
1703/*****************************************************************************
1704 * SerialBreak
1705 * this function sends a break to the port
1706 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001707static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Alan Cox95da3102008-07-22 11:09:07 +01001709 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001711 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int status;
1713
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001714 if ((!edge_serial->is_epic) ||
1715 ((edge_serial->is_epic) &&
1716 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1717 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001718 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001720 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001721 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001722 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001723 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001724 block_until_chase_response(edge_port);
1725 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001726 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001730 if ((!edge_serial->is_epic) ||
1731 ((edge_serial->is_epic) &&
1732 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1733 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001734 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001735 status = send_iosp_ext_cmd(edge_port,
1736 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001737 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001738 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001739 status = send_iosp_ext_cmd(edge_port,
1740 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001741 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001742 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001743 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001744 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748
1749/*****************************************************************************
1750 * process_rcvd_data
1751 * this function handles the data received on the bulk in pipe.
1752 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001753static void process_rcvd_data(struct edgeport_serial *edge_serial,
1754 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001756 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct usb_serial_port *port;
1758 struct edgeport_port *edge_port;
1759 struct tty_struct *tty;
1760 __u16 lastBufferLength;
1761 __u16 rxLen;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 lastBufferLength = bufferLength + 1;
1764
1765 while (bufferLength > 0) {
1766 /* failsafe incase we get a message that we don't understand */
1767 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001768 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 break;
1770 }
1771 lastBufferLength = bufferLength;
1772
1773 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001774 case EXPECT_HDR1:
1775 edge_serial->rxHeader1 = *buffer;
1776 ++buffer;
1777 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Alan Cox03f0dbf2008-07-22 11:16:34 +01001779 if (bufferLength == 0) {
1780 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001782 }
1783 /* otherwise, drop on through */
1784 case EXPECT_HDR2:
1785 edge_serial->rxHeader2 = *buffer;
1786 ++buffer;
1787 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001789 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1790 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001791 /* Process depending on whether this header is
1792 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Alan Cox03f0dbf2008-07-22 11:16:34 +01001794 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1795 /* Decode this status header and go to
1796 * EXPECT_HDR1 (if we can process the status
1797 * with only 2 bytes), or go to EXPECT_HDR3 to
1798 * get the third byte. */
1799 edge_serial->rxPort =
1800 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1801 edge_serial->rxStatusCode =
1802 IOSP_GET_STATUS_CODE(
1803 edge_serial->rxHeader1);
1804
1805 if (!IOSP_STATUS_IS_2BYTE(
1806 edge_serial->rxStatusCode)) {
1807 /* This status needs additional bytes.
1808 * Save what we have and then wait for
1809 * more data.
1810 */
1811 edge_serial->rxStatusParam
1812 = edge_serial->rxHeader2;
1813 edge_serial->rxState = EXPECT_HDR3;
1814 break;
1815 }
1816 /* We have all the header bytes, process the
1817 status now */
1818 process_rcvd_status(edge_serial,
1819 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 edge_serial->rxState = EXPECT_HDR1;
1821 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001822 } else {
1823 edge_serial->rxPort =
1824 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1825 edge_serial->rxBytesRemaining =
1826 IOSP_GET_HDR_DATA_LEN(
1827 edge_serial->rxHeader1,
1828 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001829 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1830 __func__,
1831 edge_serial->rxPort,
1832 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Alan Cox03f0dbf2008-07-22 11:16:34 +01001834 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1835 * ASSERT(DevExt->RxBytesRemaining <
1836 * IOSP_MAX_DATA_LENGTH);
1837 */
1838
1839 if (bufferLength == 0) {
1840 edge_serial->rxState = EXPECT_DATA;
1841 break;
1842 }
1843 /* Else, drop through */
1844 }
1845 case EXPECT_DATA: /* Expect data */
1846 if (bufferLength < edge_serial->rxBytesRemaining) {
1847 rxLen = bufferLength;
1848 /* Expect data to start next buffer */
1849 edge_serial->rxState = EXPECT_DATA;
1850 } else {
1851 /* BufLen >= RxBytesRemaining */
1852 rxLen = edge_serial->rxBytesRemaining;
1853 /* Start another header next time */
1854 edge_serial->rxState = EXPECT_HDR1;
1855 }
1856
1857 bufferLength -= rxLen;
1858 edge_serial->rxBytesRemaining -= rxLen;
1859
1860 /* spit this data back into the tty driver if this
1861 port is open */
1862 if (rxLen) {
1863 port = edge_serial->serial->port[
1864 edge_serial->rxPort];
1865 edge_port = usb_get_serial_port_data(port);
1866 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001867 tty = tty_port_tty_get(
1868 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001869 if (tty) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001870 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001871 __func__, rxLen, edge_serial->rxPort);
1872 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001873 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001874 }
1875 edge_port->icount.rx += rxLen;
1876 }
1877 buffer += rxLen;
1878 }
1879 break;
1880
1881 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1882 edge_serial->rxHeader3 = *buffer;
1883 ++buffer;
1884 --bufferLength;
1885
1886 /* We have all the header bytes, process the
1887 status now */
1888 process_rcvd_status(edge_serial,
1889 edge_serial->rxStatusParam,
1890 edge_serial->rxHeader3);
1891 edge_serial->rxState = EXPECT_HDR1;
1892 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 }
1895}
1896
1897
1898/*****************************************************************************
1899 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001900 * this function handles the any status messages received on the
1901 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001903static void process_rcvd_status(struct edgeport_serial *edge_serial,
1904 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct usb_serial_port *port;
1907 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001908 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001909 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 __u8 code = edge_serial->rxStatusCode;
1911
1912 /* switch the port pointer to the one being currently talked about */
1913 port = edge_serial->serial->port[edge_serial->rxPort];
1914 edge_port = usb_get_serial_port_data(port);
1915 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001916 dev_err(&edge_serial->serial->dev->dev,
1917 "%s - edge_port == NULL for port %d\n",
1918 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return;
1920 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001921 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (code == IOSP_EXT_STATUS) {
1924 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001925 case IOSP_EXT_STATUS_CHASE_RSP:
1926 /* we want to do EXT status regardless of port
1927 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001928 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1929 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001930 /* Currently, the only EXT_STATUS is Chase, so process
1931 * here instead of one more call to one more subroutine
1932 * If/when more EXT_STATUS, there'll be more work to do
1933 * Also, we currently clear flag and close the port
1934 * regardless of content of above's Byte3.
1935 * We could choose to do something else when Byte3 says
1936 * Timeout on Chase from Edgeport, like wait longer in
1937 * block_until_chase_response, but for now we don't.
1938 */
1939 edge_port->chaseResponsePending = false;
1940 wake_up(&edge_port->wait_chase);
1941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Alan Cox03f0dbf2008-07-22 11:16:34 +01001943 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001944 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1945 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001946 /* Port->RxCheckRsp = true; */
1947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 }
1950
1951 if (code == IOSP_STATUS_OPEN_RSP) {
1952 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1953 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001954 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1955 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001956 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Alan Cox03f0dbf2008-07-22 11:16:34 +01001958 /* send the current line settings to the port so we are
1959 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001960 tty = tty_port_tty_get(&edge_port->port->port);
1961 if (tty) {
1962 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001963 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001964 tty_kref_put(tty);
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001968 edge_port->openPending = false;
1969 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 wake_up(&edge_port->wait_open);
1971 return;
1972 }
1973
Alan Cox03f0dbf2008-07-22 11:16:34 +01001974 /* If port is closed, silently discard all rcvd status. We can
1975 * have cases where buffered status is received AFTER the close
1976 * port command is sent to the Edgeport.
1977 */
1978 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001982 /* Not currently sent by Edgeport */
1983 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001984 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1985 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001986 handle_new_lsr(edge_port, false, byte2, 0);
1987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Alan Cox03f0dbf2008-07-22 11:16:34 +01001989 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001990 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1991 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001992 /* byte2 is LSR Register */
1993 /* byte3 is broken data byte */
1994 handle_new_lsr(edge_port, true, byte2, byte3);
1995 break;
1996 /*
1997 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001998 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001999 * __func__, edge_serial->rxPort, byte2, byte3);
2000 * break;
2001 */
2002 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002003 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2004 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002005 /*
2006 * Process this new modem status and generate appropriate
2007 * events, etc, based on the new status. This routine
2008 * also saves the MSR in Port->ShadowMsr.
2009 */
2010 handle_new_msr(edge_port, byte2);
2011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Alan Cox03f0dbf2008-07-22 11:16:34 +01002013 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002014 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019
2020/*****************************************************************************
2021 * edge_tty_recv
2022 * this function passes data on to the tty flip buffer
2023 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002024static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2025 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 int cnt;
2028
Alan Coxa108bfc2010-02-18 16:44:01 +00002029 cnt = tty_insert_flip_string(tty, data, length);
2030 if (cnt < length) {
2031 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2032 __func__, length - cnt);
2033 }
2034 data += cnt;
2035 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 tty_flip_buffer_push(tty);
2038}
2039
2040
2041/*****************************************************************************
2042 * handle_new_msr
2043 * this function handles any change to the msr register for a port.
2044 *****************************************************************************/
2045static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2046{
2047 struct async_icount *icount;
2048
Alan Cox03f0dbf2008-07-22 11:16:34 +01002049 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2050 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 icount = &edge_port->icount;
2052
2053 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002054 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002056 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002058 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002060 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 wake_up_interruptible(&edge_port->delta_msr_wait);
2063 }
2064
2065 /* Save the new modem status */
2066 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069
2070/*****************************************************************************
2071 * handle_new_lsr
2072 * this function handles any change to the lsr register for a port.
2073 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002074static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2075 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002077 __u8 newLsr = (__u8) (lsr & (__u8)
2078 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2079 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 edge_port->shadowLSR = lsr;
2082
2083 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002084 /*
2085 * Parity and Framing errors only count if they
2086 * occur exclusive of a break being
2087 * received.
2088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2090 }
2091
2092 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002093 if (lsrData) {
2094 struct tty_struct *tty =
2095 tty_port_tty_get(&edge_port->port->port);
2096 if (tty) {
2097 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2098 tty_kref_put(tty);
2099 }
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /* update input line counters */
2102 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002103 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002105 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002107 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002109 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113
2114/****************************************************************************
2115 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002116 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * given address.
2118 * If successful returns the number of bytes written, otherwise it returns
2119 * a negative error number of the problem.
2120 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002121static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2122 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 int result;
2125 __u16 current_length;
2126 unsigned char *transfer_buffer;
2127
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002128 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Alan Cox03f0dbf2008-07-22 11:16:34 +01002130 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002132 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2133 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return -ENOMEM;
2135 }
2136
2137 /* need to split these writes up into 64 byte chunks */
2138 result = 0;
2139 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002140 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002142 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002144
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002145/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 memcpy(transfer_buffer, data, current_length);
2147 result = usb_control_msg(serial->dev,
2148 usb_sndctrlpipe(serial->dev, 0),
2149 USB_REQUEST_ION_WRITE_RAM,
2150 0x40, addr, extAddr, transfer_buffer,
2151 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (result < 0)
2153 break;
2154 length -= current_length;
2155 addr += current_length;
2156 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Alan Cox03f0dbf2008-07-22 11:16:34 +01002159 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return result;
2161}
2162
2163
2164/****************************************************************************
2165 * rom_write
2166 * writes a number of bytes to the Edgeport device's ROM starting at the
2167 * given address.
2168 * If successful returns the number of bytes written, otherwise it returns
2169 * a negative error number of the problem.
2170 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002171static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2172 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 int result;
2175 __u16 current_length;
2176 unsigned char *transfer_buffer;
2177
Alan Cox03f0dbf2008-07-22 11:16:34 +01002178 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002180 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2181 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -ENOMEM;
2183 }
2184
2185 /* need to split these writes up into 64 byte chunks */
2186 result = 0;
2187 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002188 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002190 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002192 memcpy(transfer_buffer, data, current_length);
2193 result = usb_control_msg(serial->dev,
2194 usb_sndctrlpipe(serial->dev, 0),
2195 USB_REQUEST_ION_WRITE_ROM, 0x40,
2196 addr, extAddr,
2197 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (result < 0)
2199 break;
2200 length -= current_length;
2201 addr += current_length;
2202 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Alan Cox03f0dbf2008-07-22 11:16:34 +01002205 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return result;
2207}
2208
2209
2210/****************************************************************************
2211 * rom_read
2212 * reads a number of bytes from the Edgeport device starting at the given
2213 * address.
2214 * If successful returns the number of bytes read, otherwise it returns
2215 * a negative error number of the problem.
2216 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002217static int rom_read(struct usb_serial *serial, __u16 extAddr,
2218 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 int result;
2221 __u16 current_length;
2222 unsigned char *transfer_buffer;
2223
Alan Cox03f0dbf2008-07-22 11:16:34 +01002224 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002226 dev_err(&serial->dev->dev,
2227 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return -ENOMEM;
2229 }
2230
2231 /* need to split these reads up into 64 byte chunks */
2232 result = 0;
2233 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002236 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002238 result = usb_control_msg(serial->dev,
2239 usb_rcvctrlpipe(serial->dev, 0),
2240 USB_REQUEST_ION_READ_ROM,
2241 0xC0, addr, extAddr, transfer_buffer,
2242 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (result < 0)
2244 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002245 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 length -= current_length;
2247 addr += current_length;
2248 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Alan Cox03f0dbf2008-07-22 11:16:34 +01002251 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return result;
2253}
2254
2255
2256/****************************************************************************
2257 * send_iosp_ext_cmd
2258 * Is used to send a IOSP message to the Edgeport device
2259 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002260static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2261 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 unsigned char *buffer;
2264 unsigned char *currentCommand;
2265 int length = 0;
2266 int status = 0;
2267
Alan Cox03f0dbf2008-07-22 11:16:34 +01002268 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002270 dev_err(&edge_port->port->dev,
2271 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -ENOMEM;
2273 }
2274
2275 currentCommand = buffer;
2276
Alan Cox03f0dbf2008-07-22 11:16:34 +01002277 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2278 edge_port->port->number - edge_port->port->serial->minor,
2279 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Alan Cox03f0dbf2008-07-22 11:16:34 +01002281 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (status) {
2283 /* something bad happened, let's free up the memory */
2284 kfree(buffer);
2285 }
2286
2287 return status;
2288}
2289
2290
2291/*****************************************************************************
2292 * write_cmd_usb
2293 * this function writes the given buffer out to the bulk write endpoint.
2294 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002295static int write_cmd_usb(struct edgeport_port *edge_port,
2296 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002298 struct edgeport_serial *edge_serial =
2299 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002300 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 int status = 0;
2302 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002304 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002307 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (!urb)
2309 return -ENOMEM;
2310
Oliver Neukum96c706e2007-03-15 15:27:17 +01002311 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002312 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2313 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Alan Cox03f0dbf2008-07-22 11:16:34 +01002315 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2316 usb_sndbulkpipe(edge_serial->serial->dev,
2317 edge_serial->bulk_out_endpoint),
2318 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002320 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 status = usb_submit_urb(urb, GFP_ATOMIC);
2322
2323 if (status) {
2324 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002325 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2326 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 usb_kill_urb(urb);
2328 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002329 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return status;
2331 }
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002334 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002336 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002338 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 status = -EINVAL;
2340 }
2341#endif
2342 return status;
2343}
2344
2345
2346/*****************************************************************************
2347 * send_cmd_write_baud_rate
2348 * this function sends the proper command to change the baud rate of the
2349 * specified port.
2350 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002351static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2352 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002354 struct edgeport_serial *edge_serial =
2355 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002356 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 unsigned char *cmdBuffer;
2358 unsigned char *currCmd;
2359 int cmdLen = 0;
2360 int divisor;
2361 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002362 unsigned char number =
2363 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Adam Kropelinbc71e472007-07-29 11:03:29 -04002365 if (edge_serial->is_epic &&
2366 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002367 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2368 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002369 return 0;
2370 }
2371
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002372 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2373 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002375 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002377 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return status;
2379 }
2380
Alan Cox03f0dbf2008-07-22 11:16:34 +01002381 /* Alloc memory for the string of commands. */
2382 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002384 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return -ENOMEM;
2386 }
2387 currCmd = cmdBuffer;
2388
Alan Cox03f0dbf2008-07-22 11:16:34 +01002389 /* Enable access to divisor latch */
2390 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Alan Cox03f0dbf2008-07-22 11:16:34 +01002392 /* Write the divisor itself */
2393 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2394 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Alan Cox03f0dbf2008-07-22 11:16:34 +01002396 /* Restore original value to disable access to divisor latch */
2397 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2398 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Alan Cox03f0dbf2008-07-22 11:16:34 +01002400 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (status) {
2402 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002403 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
2406 return status;
2407}
2408
2409
2410/*****************************************************************************
2411 * calc_baud_rate_divisor
2412 * this function calculates the proper baud rate divisor for the specified
2413 * baud rate.
2414 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002415static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 int i;
2418 __u16 custom;
2419
Tobias Klauser52950ed2005-12-11 16:20:08 +01002420 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002421 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 *divisor = divisor_table[i].Divisor;
2423 return 0;
2424 }
2425 }
2426
Alan Cox03f0dbf2008-07-22 11:16:34 +01002427 /* We have tried all of the standard baud rates
2428 * lets try to calculate the divisor for this baud rate
2429 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002431 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 custom = (__u16)((230400L + baudrate/2) / baudrate);
2433
2434 *divisor = custom;
2435
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002436 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 0;
2438 }
2439
2440 return -1;
2441}
2442
2443
2444/*****************************************************************************
2445 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002446 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002448static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2449 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002451 struct edgeport_serial *edge_serial =
2452 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002453 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 unsigned char *cmdBuffer;
2455 unsigned char *currCmd;
2456 unsigned long cmdLen = 0;
2457 int status;
2458
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002459 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2460 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Adam Kropelinbc71e472007-07-29 11:03:29 -04002462 if (edge_serial->is_epic &&
2463 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2464 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002465 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002466 return 0;
2467 }
2468
Adam Kropelinbc71e472007-07-29 11:03:29 -04002469 if (edge_serial->is_epic &&
2470 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2471 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002472 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002473 return 0;
2474 }
2475
Alan Cox03f0dbf2008-07-22 11:16:34 +01002476 /* Alloc memory for the string of commands. */
2477 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2478 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 currCmd = cmdBuffer;
2482
Alan Cox03f0dbf2008-07-22 11:16:34 +01002483 /* Build a cmd in the buffer to write the given register */
2484 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2485 edge_port->port->number - edge_port->port->serial->minor,
2486 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2489 if (status) {
2490 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002491 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493
2494 return status;
2495}
2496
2497
2498/*****************************************************************************
2499 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002500 * This routine is called to set the UART on the device to match the
2501 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002503
2504static void change_port_settings(struct tty_struct *tty,
2505 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002507 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002508 struct edgeport_serial *edge_serial =
2509 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 int baud;
2511 unsigned cflag;
2512 __u8 mask = 0xff;
2513 __u8 lData;
2514 __u8 lParity;
2515 __u8 lStop;
2516 __u8 rxFlow;
2517 __u8 txFlow;
2518 int status;
2519
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002520 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002522 if (!edge_port->open &&
2523 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002524 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return;
2526 }
2527
Alan Coxadc8d742012-07-14 15:31:47 +01002528 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002531 case CS5:
2532 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002533 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002534 break;
2535 case CS6:
2536 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002537 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002538 break;
2539 case CS7:
2540 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002541 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002542 break;
2543 default:
2544 case CS8:
2545 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002546 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549
2550 lParity = LCR_PAR_NONE;
2551 if (cflag & PARENB) {
2552 if (cflag & CMSPAR) {
2553 if (cflag & PARODD) {
2554 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002555 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 } else {
2557 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002558 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560 } else if (cflag & PARODD) {
2561 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002562 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 } else {
2564 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002565 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002568 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570
2571 if (cflag & CSTOPB) {
2572 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002573 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 } else {
2575 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002576 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578
2579 /* figure out the flow control settings */
2580 rxFlow = txFlow = 0x00;
2581 if (cflag & CRTSCTS) {
2582 rxFlow |= IOSP_RX_FLOW_RTS;
2583 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002584 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002586 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588
Alan Cox03f0dbf2008-07-22 11:16:34 +01002589 /* if we are implementing XON/XOFF, set the start and stop character
2590 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (I_IXOFF(tty) || I_IXON(tty)) {
2592 unsigned char stop_char = STOP_CHAR(tty);
2593 unsigned char start_char = START_CHAR(tty);
2594
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002595 if ((!edge_serial->is_epic) ||
2596 ((edge_serial->is_epic) &&
2597 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002598 send_iosp_ext_cmd(edge_port,
2599 IOSP_CMD_SET_XON_CHAR, start_char);
2600 send_iosp_ext_cmd(edge_port,
2601 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 /* if we are implementing INBOUND XON/XOFF */
2605 if (I_IXOFF(tty)) {
2606 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002607 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2608 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002610 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612
2613 /* if we are implementing OUTBOUND XON/XOFF */
2614 if (I_IXON(tty)) {
2615 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002616 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2617 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002619 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 }
2622
2623 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002624 if ((!edge_serial->is_epic) ||
2625 ((edge_serial->is_epic) &&
2626 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2627 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2628 if ((!edge_serial->is_epic) ||
2629 ((edge_serial->is_epic) &&
2630 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2631 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633
2634 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2635 edge_port->shadowLCR |= (lData | lParity | lStop);
2636
2637 edge_port->validDataMask = mask;
2638
2639 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002640 status = send_cmd_write_uart_register(edge_port, LCR,
2641 edge_port->shadowLCR);
2642 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645 /* set up the MCR register and send it to the EdgePort */
2646 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002647 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002649
2650 status = send_cmd_write_uart_register(edge_port, MCR,
2651 edge_port->shadowMCR);
2652 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 /* Determine divisor based on baud rate */
2656 baud = tty_get_baud_rate(tty);
2657 if (!baud) {
2658 /* pick a default, any default... */
2659 baud = 9600;
2660 }
2661
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002662 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002663 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002664 if (status == -1) {
2665 /* Speed change was not possible - put back the old speed */
2666 baud = tty_termios_baud_rate(old_termios);
2667 tty_encode_baud_rate(tty, baud, baud);
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671
2672/****************************************************************************
2673 * unicode_to_ascii
2674 * Turns a string from Unicode into ASCII.
2675 * Doesn't do a good job with any characters that are outside the normal
2676 * ASCII range, but it's only for debugging...
2677 * NOTE: expects the unicode in LE format
2678 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002679static void unicode_to_ascii(char *string, int buflen,
2680 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 int i;
2683
Pete Zaitcevad933752006-05-22 21:49:44 -07002684 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002686 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Pete Zaitcevad933752006-05-22 21:49:44 -07002688 for (i = 0; i < unicode_size; i++) {
2689 if (i >= buflen)
2690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002692 }
2693 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696
2697/****************************************************************************
2698 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002699 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * structure.
2701 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002702static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002704 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 int response;
2706
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002707 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Alan Cox03f0dbf2008-07-22 11:16:34 +01002709 response = rom_read(edge_serial->serial,
2710 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2711 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2712 EDGE_MANUF_DESC_LEN,
2713 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Alan Cox03f0dbf2008-07-22 11:16:34 +01002715 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002716 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002717 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002719 dev_dbg(dev, "**Manufacturer Descriptor\n");
2720 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002721 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002722 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002723 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002724 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002725 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002726 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002727 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002728 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002729 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002730 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002731 edge_serial->manuf_descriptor.DescDate[0],
2732 edge_serial->manuf_descriptor.DescDate[1],
2733 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002734 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002735 edge_serial->manuf_descriptor.SerialNumber,
2736 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002737 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002738 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002739 edge_serial->manuf_descriptor.AssemblyNumber,
2740 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002741 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002742 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002743 edge_serial->manuf_descriptor.OemAssyNumber,
2744 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002745 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2746 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002747 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002748 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002749 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002750 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002751 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753}
2754
2755
2756/****************************************************************************
2757 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002758 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 * structure.
2760 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002761static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002763 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 int response;
2765
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002766 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Alan Cox03f0dbf2008-07-22 11:16:34 +01002768 response = rom_read(edge_serial->serial,
2769 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2770 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2771 EDGE_BOOT_DESC_LEN,
2772 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Alan Cox03f0dbf2008-07-22 11:16:34 +01002774 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002775 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002776 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002777 dev_dbg(dev, "**Boot Descriptor:\n");
2778 dev_dbg(dev, " BootCodeLength: %d\n",
2779 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2780 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002781 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002782 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002783 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002784 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002785 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002786 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002787 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002788 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002789 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002790 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002791 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793}
2794
2795
2796/****************************************************************************
2797 * load_application_firmware
2798 * This is called to load the application firmware to the device
2799 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002800static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002802 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302803 const struct ihex_binrec *rec;
2804 const struct firmware *fw;
2805 const char *fw_name;
2806 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302808 __u32 Operaddr;
2809 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 switch (edge_serial->product_info.iDownloadFile) {
2812 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302813 fw_info = "downloading firmware version (930)";
2814 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816
2817 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302818 fw_info = "downloading firmware version (80251)";
2819 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 break;
2821
2822 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002823 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return;
2825
2826 default:
2827 return;
2828 }
2829
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302830 response = request_ihex_firmware(&fw, fw_name,
2831 &edge_serial->serial->dev->dev);
2832 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002833 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302834 fw_name, response);
2835 return;
2836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302838 rec = (const struct ihex_binrec *)fw->data;
2839 build = (rec->data[2] << 8) | rec->data[3];
2840
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002841 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302842
Bjørn Mork271c1152011-01-17 14:19:37 +01002843 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2844 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302845 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2846
2847 for (rec = ihex_next_binrec(rec); rec;
2848 rec = ihex_next_binrec(rec)) {
2849 Operaddr = be32_to_cpu(rec->addr);
2850 response = sram_write(edge_serial->serial,
2851 Operaddr >> 16,
2852 Operaddr & 0xFFFF,
2853 be16_to_cpu(rec->len),
2854 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302856 dev_err(&edge_serial->serial->dev->dev,
2857 "sram_write failed (%x, %x, %d)\n",
2858 Operaddr >> 16, Operaddr & 0xFFFF,
2859 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 break;
2861 }
2862 }
2863
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002864 dev_dbg(dev, "sending exec_dl_code\n");
2865 response = usb_control_msg (edge_serial->serial->dev,
2866 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2867 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2869
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302870 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873
2874/****************************************************************************
2875 * edge_startup
2876 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002877static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
2879 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002881 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002882 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002883 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002884 bool interrupt_in_found;
2885 bool bulk_in_found;
2886 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002887 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2888 EDGE_COMPATIBILITY_MASK1,
2889 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 dev = serial->dev;
2892
2893 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002894 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002896 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return -ENOMEM;
2898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 spin_lock_init(&edge_serial->es_lock);
2900 edge_serial->serial = serial;
2901 usb_set_serial_data(serial, edge_serial);
2902
2903 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002904 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002905 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002906 if (i < 0)
2907 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002908 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002909 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002910 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2913
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002914 /* Read the epic descriptor */
2915 if (get_epic_descriptor(edge_serial) <= 0) {
2916 /* memcpy descriptor to Supports structures */
2917 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2918 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002920 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002921 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002923 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002924 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925
2926 get_product_info(edge_serial);
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 /* set the number of ports from the manufacturing description */
2930 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002931 if ((!edge_serial->is_epic) &&
2932 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002933 dev_warn(ddev,
2934 "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 -08002935 edge_serial->product_info.NumPorts,
2936 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002939 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002941 /* If not an EPiC device */
2942 if (!edge_serial->is_epic) {
2943 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002944 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002946 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002948 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002949 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002951 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002952
2953 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002954/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002955/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002956 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002957 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302958 edge_serial->product_info.FirmwareMajorVersion,
2959 edge_serial->product_info.FirmwareMinorVersion,
2960 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Alan Cox03f0dbf2008-07-22 11:16:34 +01002962 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 * as the structures aren't created yet. */
2964
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002965 response = 0;
2966
2967 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002968 /* EPIC thing, set up our interrupt polling now and our read
2969 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002970 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002971 for (i = 0; i < serial->interface->altsetting[0]
2972 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002973 struct usb_endpoint_descriptor *endpoint;
2974 int buffer_size;
2975
Alan Cox03f0dbf2008-07-22 11:16:34 +01002976 endpoint = &serial->interface->altsetting[0].
2977 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002978 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002979 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002980 (usb_endpoint_is_int_in(endpoint))) {
2981 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002982 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002983
2984 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002985 edge_serial->interrupt_read_urb =
2986 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002988 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002989 return -ENOMEM;
2990 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002991 edge_serial->interrupt_in_buffer =
2992 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002993 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002994 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002995 usb_free_urb(edge_serial->interrupt_read_urb);
2996 return -ENOMEM;
2997 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002998 edge_serial->interrupt_in_endpoint =
2999 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003000
3001 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003002 usb_fill_int_urb(
3003 edge_serial->interrupt_read_urb,
3004 dev,
3005 usb_rcvintpipe(dev,
3006 endpoint->bEndpointAddress),
3007 edge_serial->interrupt_in_buffer,
3008 buffer_size,
3009 edge_interrupt_callback,
3010 edge_serial,
3011 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003012
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003013 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003014 }
3015
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003016 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003017 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003018 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003019 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003020
3021 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003022 edge_serial->read_urb =
3023 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003024 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003025 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003026 return -ENOMEM;
3027 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003028 edge_serial->bulk_in_buffer =
3029 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003030 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003031 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003032 usb_free_urb(edge_serial->read_urb);
3033 return -ENOMEM;
3034 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003035 edge_serial->bulk_in_endpoint =
3036 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003037
3038 /* set up our bulk in urb */
3039 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003040 usb_rcvbulkpipe(dev,
3041 endpoint->bEndpointAddress),
3042 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003043 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003044 edge_bulk_in_callback,
3045 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003046 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003047 }
3048
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003049 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 (usb_endpoint_is_bulk_out(endpoint))) {
3051 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003052 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003053 edge_serial->bulk_out_endpoint =
3054 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003055 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003056 }
3057 }
3058
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003059 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003060 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003061 return -ENODEV;
3062 }
3063
3064 /* start interrupt read for this edgeport this interrupt will
3065 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003066 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3067 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003068 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003069 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003070 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003071 }
3072 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075
3076/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003077 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 * This function is called whenever the device is removed from the usb bus.
3079 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003080static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003082 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003085 /* free up our endpoint stuff */
3086 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003087 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003088 usb_free_urb(edge_serial->interrupt_read_urb);
3089 kfree(edge_serial->interrupt_in_buffer);
3090
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003091 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003092 usb_free_urb(edge_serial->read_urb);
3093 kfree(edge_serial->bulk_in_buffer);
3094 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003095}
3096
3097
3098/****************************************************************************
3099 * edge_release
3100 * This function is called when the device structure is deallocated.
3101 ****************************************************************************/
3102static void edge_release(struct usb_serial *serial)
3103{
3104 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003105
3106 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003109static int edge_port_probe(struct usb_serial_port *port)
3110{
3111 struct edgeport_port *edge_port;
3112
3113 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3114 if (!edge_port)
3115 return -ENOMEM;
3116
3117 spin_lock_init(&edge_port->ep_lock);
3118 edge_port->port = port;
3119
3120 usb_set_serial_port_data(port, edge_port);
3121
3122 return 0;
3123}
3124
3125static int edge_port_remove(struct usb_serial_port *port)
3126{
3127 struct edgeport_port *edge_port;
3128
3129 edge_port = usb_get_serial_port_data(port);
3130 kfree(edge_port);
3131
3132 return 0;
3133}
3134
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003135module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Alan Cox03f0dbf2008-07-22 11:16:34 +01003137MODULE_AUTHOR(DRIVER_AUTHOR);
3138MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303140MODULE_FIRMWARE("edgeport/boot.fw");
3141MODULE_FIRMWARE("edgeport/boot2.fw");
3142MODULE_FIRMWARE("edgeport/down.fw");
3143MODULE_FIRMWARE("edgeport/down2.fw");