blob: e85c8c0d1ad95bd967e1c5dc894648e8b32ac275 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Supports the following devices:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
25 *
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
29 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/jiffies.h>
34#include <linux/errno.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_driver.h>
39#include <linux/tty_flip.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/serial.h>
43#include <linux/ioctl.h>
44#include <linux/wait.h>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053045#include <linux/firmware.h>
46#include <linux/ihex.h>
Alan Cox03f0dbf2008-07-22 11:16:34 +010047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
54/*
55 * Version Information
56 */
57#define DRIVER_VERSION "v2.7"
58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59#define DRIVER_DESC "Edgeport USB Serial Driver"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_NAME_LEN 64
62
63#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
66
67/* receive port state */
68enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010069 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75
Alan Cox03f0dbf2008-07-22 11:16:34 +010076/* Transmit Fifo
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
80 */
81struct TxFifo {
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
87};
88
89/* This structure holds all of the local port information */
90struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
93
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010096 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spinlock_t ep_lock;
98
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
105 __u8 validDataMask;
106 __u32 baudRate;
107
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100108 bool open;
109 bool openPending;
110 bool commandPending;
111 bool closePending;
112 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
118
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
121};
122
123
124/* This structure holds all of the individual device information */
125struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700126 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100135 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100139 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100141 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spinlock_t es_lock;
143
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
145
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
147
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
157};
158
159/* baud rate information */
160struct divisor_table_entry {
161 __u32 BaudRate;
162 __u16 Divisor;
163};
164
Alan Cox03f0dbf2008-07-22 11:16:34 +0100165/*
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
168 * MCR.7 = 0.
169 */
170
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100171static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100172 { 50, 4608},
173 { 75, 3072},
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { 150, 1536},
177 { 300, 768},
178 { 600, 384},
179 { 1200, 192},
180 { 1800, 128},
181 { 2400, 96},
182 { 4800, 48},
183 { 7200, 32},
184 { 9600, 24},
185 { 14400, 16},
186 { 19200, 12},
187 { 38400, 6},
188 { 57600, 4},
189 { 115200, 2},
190 { 230400, 1},
191};
192
193/* local variables */
194static int debug;
195
196static int low_latency = 1; /* tty low latency flag, on by default */
197
Alan Cox03f0dbf2008-07-22 11:16:34 +0100198static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200
201/* local function prototypes */
202
203/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100204static void edge_interrupt_callback(struct urb *urb);
205static void edge_bulk_in_callback(struct urb *urb);
206static void edge_bulk_out_data_callback(struct urb *urb);
207static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/* function prototypes for the usbserial callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100210static int edge_open(struct tty_struct *tty, struct usb_serial_port *port,
211 struct file *filp);
212static void edge_close(struct tty_struct *tty, struct usb_serial_port *port,
213 struct file *filp);
214static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
215 const unsigned char *buf, int count);
216static int edge_write_room(struct tty_struct *tty);
217static int edge_chars_in_buffer(struct tty_struct *tty);
218static void edge_throttle(struct tty_struct *tty);
219static void edge_unthrottle(struct tty_struct *tty);
220static void edge_set_termios(struct tty_struct *tty,
221 struct usb_serial_port *port,
222 struct ktermios *old_termios);
223static int edge_ioctl(struct tty_struct *tty, struct file *file,
224 unsigned int cmd, unsigned long arg);
225static void edge_break(struct tty_struct *tty, int break_state);
226static int edge_tiocmget(struct tty_struct *tty, struct file *file);
227static int edge_tiocmset(struct tty_struct *tty, struct file *file,
228 unsigned int set, unsigned int clear);
229static int edge_startup(struct usb_serial *serial);
230static void edge_shutdown(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232#include "io_tables.h" /* all of the devices that this driver supports */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Alan Cox03f0dbf2008-07-22 11:16:34 +0100236static void process_rcvd_data(struct edgeport_serial *edge_serial,
237 unsigned char *buffer, __u16 bufferLength);
238static void process_rcvd_status(struct edgeport_serial *edge_serial,
239 __u8 byte2, __u8 byte3);
240static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
241 unsigned char *data, int length);
242static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
243static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
244 __u8 lsr, __u8 data);
245static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
246 __u8 param);
247static int calc_baud_rate_divisor(int baud_rate, int *divisor);
248static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
249 int baudRate);
250static void change_port_settings(struct tty_struct *tty,
251 struct edgeport_port *edge_port,
252 struct ktermios *old_termios);
253static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
254 __u8 regNum, __u8 regValue);
255static int write_cmd_usb(struct edgeport_port *edge_port,
256 unsigned char *buffer, int writeLength);
257static void send_more_port_data(struct edgeport_serial *edge_serial,
258 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Alan Cox03f0dbf2008-07-22 11:16:34 +0100260static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
261 __u16 length, const __u8 *data);
262static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
263 __u16 length, __u8 *data);
264static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
265 __u16 length, const __u8 *data);
266static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
267static void get_boot_desc(struct edgeport_serial *edge_serial);
268static void load_application_firmware(struct edgeport_serial *edge_serial);
269
270static void unicode_to_ascii(char *string, int buflen,
271 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273
Alan Cox03f0dbf2008-07-22 11:16:34 +0100274/* ************************************************************************ */
275/* ************************************************************************ */
276/* ************************************************************************ */
277/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/************************************************************************
280 * *
281 * update_edgeport_E2PROM() Compare current versions of *
282 * Boot ROM and Manufacture *
283 * Descriptors with versions *
284 * embedded in this driver *
285 * *
286 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100287static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 __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) {
314 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
315 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
333 dbg("Current Boot Image version %d.%d.%d",
334 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) {
340 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
341 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
346 dbg("Downloading new Boot Image");
347
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 {
365 dbg("Boot Image -- already up to date");
366 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530367 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370
371/************************************************************************
372 * *
373 * Get string descriptor from device *
374 * *
375 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100376static int get_string(struct usb_device *dev, int Id, char *string, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct usb_string_descriptor StringDesc;
379 struct usb_string_descriptor *pStringDesc;
380
Alan Cox03f0dbf2008-07-22 11:16:34 +0100381 dbg("%s - USB String ID = %d", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Alan Cox03f0dbf2008-07-22 11:16:34 +0100383 if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
384 &StringDesc, sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Cox03f0dbf2008-07-22 11:16:34 +0100387 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
388 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Alan Cox03f0dbf2008-07-22 11:16:34 +0100391 if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
392 pStringDesc, StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(pStringDesc);
394 return 0;
395 }
396
Alan Cox03f0dbf2008-07-22 11:16:34 +0100397 unicode_to_ascii(string, buflen,
398 pStringDesc->wData, pStringDesc->bLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 kfree(pStringDesc);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800401 dbg("%s - USB String %s", __func__, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return strlen(string);
403}
404
405
406#if 0
407/************************************************************************
408 *
409 * Get string descriptor from device
410 *
411 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100412static int get_string_desc(struct usb_device *dev, int Id,
413 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct usb_string_descriptor StringDesc;
416 struct usb_string_descriptor *pStringDesc;
417
Alan Cox03f0dbf2008-07-22 11:16:34 +0100418 dbg("%s - USB String ID = %d", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Alan Cox03f0dbf2008-07-22 11:16:34 +0100420 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
421 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Alan Cox03f0dbf2008-07-22 11:16:34 +0100424 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
425 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alan Cox03f0dbf2008-07-22 11:16:34 +0100428 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
429 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 kfree(pStringDesc);
431 return -1;
432 }
433
434 *pRetDesc = pStringDesc;
435 return 0;
436}
437#endif
438
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800439static void dump_product_info(struct edgeport_product_info *product_info)
440{
Alan Cox03f0dbf2008-07-22 11:16:34 +0100441 /* Dump Product Info structure */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800442 dbg("**Product Information:");
Alan Cox03f0dbf2008-07-22 11:16:34 +0100443 dbg(" ProductId %x", product_info->ProductId);
444 dbg(" NumPorts %d", product_info->NumPorts);
445 dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800446 dbg(" IsServer %d", product_info->IsServer);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100447 dbg(" IsRS232 %d", product_info->IsRS232);
448 dbg(" IsRS422 %d", product_info->IsRS422);
449 dbg(" IsRS485 %d", product_info->IsRS485);
450 dbg(" RomSize %d", product_info->RomSize);
451 dbg(" RamSize %d", product_info->RamSize);
452 dbg(" CpuRev %x", product_info->CpuRev);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800453 dbg(" BoardRev %x", product_info->BoardRev);
454 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
455 product_info->BootMinorVersion,
456 le16_to_cpu(product_info->BootBuildNumber));
Alan Cox03f0dbf2008-07-22 11:16:34 +0100457 dbg(" FirmwareMajorVersion %d.%d.%d",
458 product_info->FirmwareMajorVersion,
459 product_info->FirmwareMinorVersion,
460 le16_to_cpu(product_info->FirmwareBuildNumber));
461 dbg(" ManufactureDescDate %d/%d/%d",
462 product_info->ManufactureDescDate[0],
463 product_info->ManufactureDescDate[1],
464 product_info->ManufactureDescDate[2]+1900);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800465 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
466 dbg(" EpicVer %d", product_info->EpicVer);
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static void get_product_info(struct edgeport_serial *edge_serial)
470{
471 struct edgeport_product_info *product_info = &edge_serial->product_info;
472
Alan Cox03f0dbf2008-07-22 11:16:34 +0100473 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alan Cox03f0dbf2008-07-22 11:16:34 +0100475 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
476 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
477 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alan Cox03f0dbf2008-07-22 11:16:34 +0100479 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
480 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
481 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
482 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Alan Cox03f0dbf2008-07-22 11:16:34 +0100484 product_info->BootMajorVersion =
485 edge_serial->boot_descriptor.MajorVersion;
486 product_info->BootMinorVersion =
487 edge_serial->boot_descriptor.MinorVersion;
488 product_info->BootBuildNumber =
489 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alan Cox03f0dbf2008-07-22 11:16:34 +0100491 memcpy(product_info->ManufactureDescDate,
492 edge_serial->manuf_descriptor.DescDate,
493 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alan Cox03f0dbf2008-07-22 11:16:34 +0100495 /* check if this is 2nd generation hardware */
496 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
497 & ION_DEVICE_ID_80251_NETCHIP)
498 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
499 else
500 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
501
502 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100504 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
505 case ION_DEVICE_ID_EDGEPORT_4T:
506 case ION_DEVICE_ID_EDGEPORT_4:
507 case ION_DEVICE_ID_EDGEPORT_2:
508 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
509 case ION_DEVICE_ID_EDGEPORT_8:
510 case ION_DEVICE_ID_EDGEPORT_421:
511 case ION_DEVICE_ID_EDGEPORT_21:
512 case ION_DEVICE_ID_EDGEPORT_2_DIN:
513 case ION_DEVICE_ID_EDGEPORT_4_DIN:
514 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
515 product_info->IsRS232 = 1;
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Alan Cox03f0dbf2008-07-22 11:16:34 +0100518 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
519 product_info->IsRS422 = 1;
520 product_info->IsRS485 = 1;
521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alan Cox03f0dbf2008-07-22 11:16:34 +0100523 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
524 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
525 product_info->IsRS422 = 1;
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800529 dump_product_info(product_info);
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800532static int get_epic_descriptor(struct edgeport_serial *ep)
533{
534 int result;
535 struct usb_serial *serial = ep->serial;
536 struct edgeport_product_info *product_info = &ep->product_info;
537 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
538 struct edge_compatibility_bits *bits;
539
540 ep->is_epic = 0;
541 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
542 USB_REQUEST_ION_GET_EPIC_DESC,
543 0xC0, 0x00, 0x00,
544 &ep->epic_descriptor,
545 sizeof(struct edge_compatibility_descriptor),
546 300);
547
Harvey Harrison441b62c2008-03-03 16:08:34 -0800548 dbg("%s result = %d", __func__, result);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800549
550 if (result > 0) {
551 ep->is_epic = 1;
552 memset(product_info, 0, sizeof(struct edgeport_product_info));
553
Alan Cox03f0dbf2008-07-22 11:16:34 +0100554 product_info->NumPorts = epic->NumPorts;
555 product_info->ProdInfoVer = 0;
556 product_info->FirmwareMajorVersion = epic->MajorVersion;
557 product_info->FirmwareMinorVersion = epic->MinorVersion;
558 product_info->FirmwareBuildNumber = epic->BuildNumber;
559 product_info->iDownloadFile = epic->iDownloadFile;
560 product_info->EpicVer = epic->EpicVer;
561 product_info->Epic = epic->Supports;
562 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800563 dump_product_info(product_info);
564
565 bits = &ep->epic_descriptor.Supports;
566 dbg("**EPIC descriptor:");
567 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
Alan Cox03f0dbf2008-07-22 11:16:34 +0100568 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
569 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
570 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
571 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
572 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
573 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
574 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
575 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
576 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
577 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
578 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
579 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800580 }
581
582 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585
586/************************************************************************/
587/************************************************************************/
588/* U S B C A L L B A C K F U N C T I O N S */
589/* U S B C A L L B A C K F U N C T I O N S */
590/************************************************************************/
591/************************************************************************/
592
593/*****************************************************************************
594 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100595 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * interrupt endpoint.
597 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100598static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Ming Leicdc97792008-02-24 18:41:47 +0800600 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct edgeport_port *edge_port;
602 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100603 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned char *data = urb->transfer_buffer;
605 int length = urb->actual_length;
606 int bytes_avail;
607 int position;
608 int txCredits;
609 int portNumber;
610 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700611 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700615 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 case 0:
617 /* success */
618 break;
619 case -ECONNRESET:
620 case -ENOENT:
621 case -ESHUTDOWN:
622 /* this urb is terminated, clean up */
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700623 dbg("%s - urb shutting down with status: %d",
Alan Cox03f0dbf2008-07-22 11:16:34 +0100624 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return;
626 default:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100627 dbg("%s - nonzero urb status received: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto exit;
629 }
630
Alan Cox03f0dbf2008-07-22 11:16:34 +0100631 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (length) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100633 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
634 __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (length > 1) {
637 bytes_avail = data[0] | (data[1] << 8);
638 if (bytes_avail) {
639 spin_lock(&edge_serial->es_lock);
640 edge_serial->rxBytesAvail += bytes_avail;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800641 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (edge_serial->rxBytesAvail > 0 &&
644 !edge_serial->read_in_progress) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800645 dbg("%s - posting a read", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100646 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Alan Cox03f0dbf2008-07-22 11:16:34 +0100648 /* we have pending bytes on the
649 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 edge_serial->read_urb->dev = edge_serial->serial->dev;
651 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
652 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100654 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 }
657 spin_unlock(&edge_serial->es_lock);
658 }
659 }
660 /* grab the txcredits for the ports if available */
661 position = 2;
662 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100663 while ((position < length) &&
664 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 txCredits = data[position] | (data[position+1] << 8);
666 if (txCredits) {
667 port = edge_serial->serial->port[portNumber];
668 edge_port = usb_get_serial_port_data(port);
669 if (edge_port->open) {
670 spin_lock(&edge_port->ep_lock);
671 edge_port->txCredits += txCredits;
672 spin_unlock(&edge_port->ep_lock);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100673 dbg("%s - txcredits for port%d = %d",
674 __func__, portNumber,
675 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alan Cox03f0dbf2008-07-22 11:16:34 +0100677 /* tell the tty driver that something
678 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100679 tty = tty_port_tty_get(
680 &edge_port->port->port);
681 if (tty) {
682 tty_wakeup(tty);
683 tty_kref_put(tty);
684 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100685 /* Since we have more credit, check
686 if more data can be sent */
687 send_more_port_data(edge_serial,
688 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 }
691 position += 2;
692 ++portNumber;
693 }
694 }
695
696exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100697 result = usb_submit_urb(urb, GFP_ATOMIC);
698 if (result)
699 dev_err(&urb->dev->dev,
700 "%s - Error %d submitting control urb\n",
701 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704
705/*****************************************************************************
706 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100707 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * bulk in endpoint.
709 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100710static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Ming Leicdc97792008-02-24 18:41:47 +0800712 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700714 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700716 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Harvey Harrison441b62c2008-03-03 16:08:34 -0800718 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700720 if (status) {
721 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800722 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100723 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return;
725 }
726
727 if (urb->actual_length == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800728 dbg("%s - read bulk callback with no data", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100729 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731 }
732
733 raw_data_length = urb->actual_length;
734
Alan Cox03f0dbf2008-07-22 11:16:34 +0100735 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
736 __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 spin_lock(&edge_serial->es_lock);
739
740 /* decrement our rxBytes available by the number that we just got */
741 edge_serial->rxBytesAvail -= raw_data_length;
742
Alan Cox03f0dbf2008-07-22 11:16:34 +0100743 dbg("%s - Received = %d, rxBytesAvail %d", __func__,
744 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Alan Cox03f0dbf2008-07-22 11:16:34 +0100746 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* check to see if there's any more data for us to read */
749 if (edge_serial->rxBytesAvail > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800750 dbg("%s - posting a read", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 edge_serial->read_urb->dev = edge_serial->serial->dev;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700752 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
753 if (retval) {
754 dev_err(&urb->dev->dev,
755 "%s - usb_submit_urb(read bulk) failed, "
Harvey Harrison441b62c2008-03-03 16:08:34 -0800756 "retval = %d\n", __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100757 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100760 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 spin_unlock(&edge_serial->es_lock);
764}
765
766
767/*****************************************************************************
768 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100769 * this is the callback function for when we have finished sending
770 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100772static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Ming Leicdc97792008-02-24 18:41:47 +0800774 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700776 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Harvey Harrison441b62c2008-03-03 16:08:34 -0800778 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700780 if (status) {
781 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800782 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Alan Cox4a90f092008-10-13 10:39:46 +0100785 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100788 /* let the tty driver wakeup if it has a special
789 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 tty_wakeup(tty);
791 }
Alan Cox4a90f092008-10-13 10:39:46 +0100792 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Alan Cox03f0dbf2008-07-22 11:16:34 +0100794 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100795 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Alan Cox03f0dbf2008-07-22 11:16:34 +0100797 /* Check if more data needs to be sent */
798 send_more_port_data((struct edgeport_serial *)
799 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802
803/*****************************************************************************
804 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100805 * this is the callback function for when we have finished sending a
806 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100808static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Ming Leicdc97792008-02-24 18:41:47 +0800810 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct tty_struct *tty;
812 int status = urb->status;
813
Harvey Harrison441b62c2008-03-03 16:08:34 -0800814 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Oliver Neukum96c706e2007-03-15 15:27:17 +0100816 atomic_dec(&CmdUrbs);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100817 dbg("%s - FREE URB %p (outstanding %d)", __func__,
818 urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820
821 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700822 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100825 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (status) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100828 dbg("%s - nonzero write bulk status received: %d",
829 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return;
831 }
832
833 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100834 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* tell the tty driver that something has changed */
837 if (tty && edge_port->open)
838 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100839 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100842 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 wake_up(&edge_port->wait_command);
844}
845
846
847/*****************************************************************************
848 * Driver tty interface functions
849 *****************************************************************************/
850
851/*****************************************************************************
852 * SerialOpen
853 * this function is called by the tty driver when a port is opened
854 * If successful, we return 0
855 * Otherwise we return a negative error number.
856 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +0100857static int edge_open(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100858 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
861 struct usb_serial *serial;
862 struct edgeport_serial *edge_serial;
863 int response;
864
Harvey Harrison441b62c2008-03-03 16:08:34 -0800865 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (edge_port == NULL)
868 return -ENODEV;
869
Alan Cox95da3102008-07-22 11:09:07 +0100870 if (tty)
871 tty->low_latency = low_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Alan Cox03f0dbf2008-07-22 11:16:34 +0100873 /* see if we've set up our endpoint info yet (can't set it up
874 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 serial = port->serial;
876 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100877 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (edge_serial->interrupt_in_buffer == NULL) {
880 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100883 edge_serial->interrupt_in_buffer =
884 port0->interrupt_in_buffer;
885 edge_serial->interrupt_in_endpoint =
886 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
888 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100889 edge_serial->bulk_in_endpoint =
890 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100892 edge_serial->bulk_out_endpoint =
893 port0->bulk_out_endpointAddress;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* set up our interrupt urb */
896 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100897 serial->dev,
898 usb_rcvintpipe(serial->dev,
899 port0->interrupt_in_endpointAddress),
900 port0->interrupt_in_buffer,
901 edge_serial->interrupt_read_urb->transfer_buffer_length,
902 edge_interrupt_callback, edge_serial,
903 edge_serial->interrupt_read_urb->interval);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* set up our bulk in urb */
906 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100907 usb_rcvbulkpipe(serial->dev,
908 port0->bulk_in_endpointAddress),
909 port0->bulk_in_buffer,
910 edge_serial->read_urb->transfer_buffer_length,
911 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100912 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100915 * this interrupt will continue as long
916 * as the edgeport is connected */
917 response = usb_submit_urb(edge_serial->interrupt_read_urb,
918 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (response) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100920 dev_err(&port->dev,
921 "%s - Error %d submitting control urb\n",
922 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* initialize our wait queues */
927 init_waitqueue_head(&edge_port->wait_open);
928 init_waitqueue_head(&edge_port->wait_chase);
929 init_waitqueue_head(&edge_port->delta_msr_wait);
930 init_waitqueue_head(&edge_port->wait_command);
931
932 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100933 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100936 edge_port->txCredits = 0; /* Can't send any data yet */
937 /* Must always set this bit to enable ints! */
938 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100939 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100942 edge_port->openPending = true;
943 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100944 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (response < 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100947 dev_err(&port->dev, "%s - error sending open port command\n",
948 __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100949 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -ENODEV;
951 }
952
953 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100954 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
955 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100957 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* open timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800959 dbg("%s - open timedout", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100960 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ENODEV;
962 }
963
964 /* create the txfifo */
965 edge_port->txfifo.head = 0;
966 edge_port->txfifo.tail = 0;
967 edge_port->txfifo.count = 0;
968 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100969 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (!edge_port->txfifo.fifo) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800972 dbg("%s - no memory", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100973 edge_close(tty, port, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -ENOMEM;
975 }
976
977 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100978 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100979 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (!edge_port->write_urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800982 dbg("%s - no memory", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100983 edge_close(tty, port, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return -ENOMEM;
985 }
986
Alan Cox03f0dbf2008-07-22 11:16:34 +0100987 dbg("%s(%d) - Initialize TX fifo to %d bytes",
988 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Harvey Harrison441b62c2008-03-03 16:08:34 -0800990 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return 0;
993}
994
995
996/************************************************************************
997 *
998 * block_until_chase_response
999 *
1000 * This function will block the close until one of the following:
1001 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001002 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
1004 *
1005 ************************************************************************/
1006static void block_until_chase_response(struct edgeport_port *edge_port)
1007{
1008 DEFINE_WAIT(wait);
1009 __u16 lastCredits;
1010 int timeout = 1*HZ;
1011 int loop = 10;
1012
1013 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001014 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 lastCredits = edge_port->txCredits;
1016
Alan Cox03f0dbf2008-07-22 11:16:34 +01001017 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001018 if (!edge_port->chaseResponsePending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001019 dbg("%s - Got Chase Response", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Alan Cox03f0dbf2008-07-22 11:16:34 +01001021 /* did we get all of our credit back? */
1022 if (edge_port->txCredits == edge_port->maxTxCredits) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001023 dbg("%s - Got all credits", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026 }
1027
Alan Cox03f0dbf2008-07-22 11:16:34 +01001028 /* Block the thread for a while */
1029 prepare_to_wait(&edge_port->wait_chase, &wait,
1030 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 schedule_timeout(timeout);
1032 finish_wait(&edge_port->wait_chase, &wait);
1033
1034 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001035 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 loop--;
1037 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001038 edge_port->chaseResponsePending = false;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001039 dbg("%s - Chase TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return;
1041 }
1042 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001043 /* Reset timeout value back to 10 seconds */
1044 dbg("%s - Last %d, Current %d", __func__,
1045 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 loop = 10;
1047 }
1048 }
1049}
1050
1051
1052/************************************************************************
1053 *
1054 * block_until_tx_empty
1055 *
1056 * This function will block the close until one of the following:
1057 * 1. TX count are 0
1058 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001059 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
1061 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001062static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 DEFINE_WAIT(wait);
1065 struct TxFifo *fifo = &edge_port->txfifo;
1066 __u32 lastCount;
1067 int timeout = HZ/10;
1068 int loop = 30;
1069
1070 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001071 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 lastCount = fifo->count;
1073
Alan Cox03f0dbf2008-07-22 11:16:34 +01001074 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (lastCount == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001076 dbg("%s - TX Buffer Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return;
1078 }
1079
Alan Cox03f0dbf2008-07-22 11:16:34 +01001080 /* Block the thread for a while */
1081 prepare_to_wait(&edge_port->wait_chase, &wait,
1082 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 schedule_timeout(timeout);
1084 finish_wait(&edge_port->wait_chase, &wait);
1085
Harvey Harrison441b62c2008-03-03 16:08:34 -08001086 dbg("%s wait", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001089 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 loop--;
1091 if (loop == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001092 dbg("%s - TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return;
1094 }
1095 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001096 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 loop = 30;
1098 }
1099 }
1100}
1101
1102
1103/*****************************************************************************
1104 * edge_close
1105 * this function is called by the tty driver when a port is closed
1106 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001107static void edge_close(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001108 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 struct edgeport_serial *edge_serial;
1111 struct edgeport_port *edge_port;
1112 int status;
1113
Harvey Harrison441b62c2008-03-03 16:08:34 -08001114 dbg("%s - port %d", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 edge_serial = usb_get_serial_data(port->serial);
1117 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001118 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001120
1121 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 block_until_tx_empty(edge_port);
1123
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001124 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001126 if ((!edge_serial->is_epic) ||
1127 ((edge_serial->is_epic) &&
1128 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1129 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001130 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Harvey Harrison441b62c2008-03-03 16:08:34 -08001132 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001133 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1134 if (status == 0)
1135 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001136 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001137 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001138 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001141 if ((!edge_serial->is_epic) ||
1142 ((edge_serial->is_epic) &&
1143 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1144 /* close the port */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001145 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001146 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Alan Cox03f0dbf2008-07-22 11:16:34 +01001149 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001150 edge_port->closePending = false;
1151 edge_port->open = false;
1152 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001154 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001157 /* if this urb had a transfer buffer already
1158 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001159 kfree(edge_port->write_urb->transfer_buffer);
1160 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 edge_port->write_urb = NULL;
1162 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001163 kfree(edge_port->txfifo.fifo);
1164 edge_port->txfifo.fifo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Harvey Harrison441b62c2008-03-03 16:08:34 -08001166 dbg("%s exited", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169/*****************************************************************************
1170 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001171 * this function is called by the tty driver when data should be written
1172 * to the port.
1173 * If successful, we return the number of bytes written, otherwise we
1174 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001176static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1177 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1180 struct TxFifo *fifo;
1181 int copySize;
1182 int bytesleft;
1183 int firsthalf;
1184 int secondhalf;
1185 unsigned long flags;
1186
Harvey Harrison441b62c2008-03-03 16:08:34 -08001187 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (edge_port == NULL)
1190 return -ENODEV;
1191
Alan Cox03f0dbf2008-07-22 11:16:34 +01001192 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 fifo = &edge_port->txfifo;
1194
1195 spin_lock_irqsave(&edge_port->ep_lock, flags);
1196
Alan Cox03f0dbf2008-07-22 11:16:34 +01001197 /* calculate number of bytes to put in fifo */
1198 copySize = min((unsigned int)count,
1199 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alan Cox03f0dbf2008-07-22 11:16:34 +01001201 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
1202 __func__, port->number, count,
1203 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Alan Cox03f0dbf2008-07-22 11:16:34 +01001205 /* catch writes of 0 bytes which the tty driver likes to give us,
1206 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (copySize == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001208 dbg("%s - copySize = Zero", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 goto finish_write;
1210 }
1211
Alan Cox03f0dbf2008-07-22 11:16:34 +01001212 /* queue the data
1213 * since we can never overflow the buffer we do not have to check for a
1214 * full condition
1215 *
1216 * the copy is done is two parts -- first fill to the end of the buffer
1217 * then copy the reset from the start of the buffer
1218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001220 firsthalf = min(bytesleft, copySize);
1221 dbg("%s - copy %d bytes of %d into fifo ", __func__,
1222 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* now copy our data */
1225 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001226 usb_serial_debug_data(debug, &port->dev, __func__,
1227 firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Alan Cox03f0dbf2008-07-22 11:16:34 +01001229 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 fifo->head += firsthalf;
1231 fifo->count += firsthalf;
1232
Alan Cox03f0dbf2008-07-22 11:16:34 +01001233 /* wrap the index */
1234 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 secondhalf = copySize-firsthalf;
1238
1239 if (secondhalf) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001240 dbg("%s - copy rest of data %d", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001242 usb_serial_debug_data(debug, &port->dev, __func__,
1243 secondhalf, &fifo->fifo[fifo->head]);
1244 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 fifo->count += secondhalf;
1246 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001247 /* No need to check for wrap since we can not get to end of
1248 * the fifo in this part
1249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
1252finish_write:
1253 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1254
Alan Cox03f0dbf2008-07-22 11:16:34 +01001255 send_more_port_data((struct edgeport_serial *)
1256 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alan Cox03f0dbf2008-07-22 11:16:34 +01001258 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
1259 copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Alan Cox03f0dbf2008-07-22 11:16:34 +01001261 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264
1265/************************************************************************
1266 *
1267 * send_more_port_data()
1268 *
1269 * This routine attempts to write additional UART transmit data
1270 * to a port over the USB bulk pipe. It is called (1) when new
1271 * data has been written to a port's TxBuffer from higher layers
1272 * (2) when the peripheral sends us additional TxCredits indicating
1273 * that it can accept more Tx data for a given port; and (3) when
1274 * a bulk write completes successfully and we want to see if we
1275 * can transmit more.
1276 *
1277 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001278static void send_more_port_data(struct edgeport_serial *edge_serial,
1279 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct TxFifo *fifo = &edge_port->txfifo;
1282 struct urb *urb;
1283 unsigned char *buffer;
1284 int status;
1285 int count;
1286 int bytesleft;
1287 int firsthalf;
1288 int secondhalf;
1289 unsigned long flags;
1290
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 dbg("%s(%d)", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 spin_lock_irqsave(&edge_port->ep_lock, flags);
1294
1295 if (edge_port->write_in_progress ||
1296 !edge_port->open ||
1297 (fifo->count == 0)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001298 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
1299 __func__, edge_port->port->number,
1300 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto exit_send;
1302 }
1303
Alan Cox03f0dbf2008-07-22 11:16:34 +01001304 /* since the amount of data in the fifo will always fit into the
1305 * edgeport buffer we do not need to check the write length
1306 *
1307 * Do we have enough credits for this port to make it worthwhile
1308 * to bother queueing a write. If it's too small, say a few bytes,
1309 * it's better to wait for more credits so we can do a larger write.
1310 */
1311 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1312 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
1313 __func__, edge_port->port->number, fifo->count,
1314 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 goto exit_send;
1316 }
1317
Alan Cox03f0dbf2008-07-22 11:16:34 +01001318 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001319 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alan Cox03f0dbf2008-07-22 11:16:34 +01001321 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 urb = edge_port->write_urb;
1323
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001324 /* make sure transfer buffer is freed */
1325 kfree(urb->transfer_buffer);
1326 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alan Cox03f0dbf2008-07-22 11:16:34 +01001328 /* build the data header for the buffer and port that we are about
1329 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001331 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (buffer == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001333 dev_err(&edge_port->port->dev,
1334 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001335 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto exit_send;
1337 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001338 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1339 - edge_port->port->serial->minor, count);
1340 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1341 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* now copy our data */
1344 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001345 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1347 fifo->tail += firsthalf;
1348 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001349 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 secondhalf = count-firsthalf;
1353 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001354 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1355 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 fifo->tail += secondhalf;
1357 fifo->count -= secondhalf;
1358 }
1359
1360 if (count)
Alan Cox03f0dbf2008-07-22 11:16:34 +01001361 usb_serial_debug_data(debug, &edge_port->port->dev,
1362 __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001365 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1366 usb_sndbulkpipe(edge_serial->serial->dev,
1367 edge_serial->bulk_out_endpoint),
1368 buffer, count+2,
1369 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* decrement the number of credits we have by the number we just sent */
1372 edge_port->txCredits -= count;
1373 edge_port->icount.tx += count;
1374
1375 urb->dev = edge_serial->serial->dev;
1376 status = usb_submit_urb(urb, GFP_ATOMIC);
1377 if (status) {
1378 /* something went wrong */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001379 dev_err(&edge_port->port->dev,
1380 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1381 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001382 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* revert the credits as something bad happened. */
1385 edge_port->txCredits += count;
1386 edge_port->icount.tx -= count;
1387 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001388 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
1389 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391exit_send:
1392 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1393}
1394
1395
1396/*****************************************************************************
1397 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001398 * this function is called by the tty driver when it wants to know how
1399 * many bytes of data we can accept for a specific port. If successful,
1400 * we return the amount of room that we have for this port (the txCredits)
1401 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001403static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Alan Cox95da3102008-07-22 11:09:07 +01001405 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1407 int room;
1408 unsigned long flags;
1409
Harvey Harrison441b62c2008-03-03 16:08:34 -08001410 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001413 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001414 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Harvey Harrison441b62c2008-03-03 16:08:34 -08001417 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001420 dbg("%s - port not opened", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Alan Cox03f0dbf2008-07-22 11:16:34 +01001424 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 spin_lock_irqsave(&edge_port->ep_lock, flags);
1426 room = edge_port->txCredits - edge_port->txfifo.count;
1427 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1428
Harvey Harrison441b62c2008-03-03 16:08:34 -08001429 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return room;
1431}
1432
1433
1434/*****************************************************************************
1435 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001436 * this function is called by the tty driver when it wants to know how
1437 * many bytes of data we currently have outstanding in the port (data that
1438 * has been written, but hasn't made it out the port yet)
1439 * If successful, we return the number of bytes left to be written in the
1440 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * Otherwise we return a negative error number.
1442 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001443static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Alan Cox95da3102008-07-22 11:09:07 +01001445 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1447 int num_chars;
1448 unsigned long flags;
1449
Harvey Harrison441b62c2008-03-03 16:08:34 -08001450 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001453 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001454 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001458 dbg("%s - port not opened", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
1462 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001463 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1464 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1466 if (num_chars) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001467 dbg("%s(port %d) - returns %d", __func__,
1468 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 return num_chars;
1472}
1473
1474
1475/*****************************************************************************
1476 * SerialThrottle
1477 * this function is called by the tty driver when it wants to stop the data
1478 * being read from the port.
1479 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001480static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Alan Cox95da3102008-07-22 11:09:07 +01001482 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int status;
1485
Harvey Harrison441b62c2008-03-03 16:08:34 -08001486 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (edge_port == NULL)
1489 return;
1490
1491 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001492 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return;
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* if we are implementing XON/XOFF, send the stop character */
1497 if (I_IXOFF(tty)) {
1498 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001499 status = edge_write(tty, port, &stop_char, 1);
1500 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503
1504 /* if we are implementing RTS/CTS, toggle that line */
1505 if (tty->termios->c_cflag & CRTSCTS) {
1506 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001507 status = send_cmd_write_uart_register(edge_port, MCR,
1508 edge_port->shadowMCR);
1509 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
1513 return;
1514}
1515
1516
1517/*****************************************************************************
1518 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001519 * this function is called by the tty driver when it wants to resume the
1520 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001522static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Alan Cox95da3102008-07-22 11:09:07 +01001524 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 int status;
1527
Harvey Harrison441b62c2008-03-03 16:08:34 -08001528 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (edge_port == NULL)
1531 return;
1532
1533 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001534 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
1536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* if we are implementing XON/XOFF, send the start character */
1539 if (I_IXOFF(tty)) {
1540 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001541 status = edge_write(tty, port, &start_char, 1);
1542 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* if we are implementing RTS/CTS, toggle that line */
1546 if (tty->termios->c_cflag & CRTSCTS) {
1547 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001548 send_cmd_write_uart_register(edge_port, MCR,
1549 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553
1554/*****************************************************************************
1555 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001556 * this function is called by the tty driver when it wants to change
1557 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001559static void edge_set_termios(struct tty_struct *tty,
1560 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 unsigned int cflag;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 cflag = tty->termios->c_cflag;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001566 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001568 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox6ce073b2007-10-18 01:24:25 -07001569 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Harvey Harrison441b62c2008-03-03 16:08:34 -08001571 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (edge_port == NULL)
1574 return;
1575
1576 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001577 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return;
1579 }
1580
1581 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001582 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585
1586/*****************************************************************************
1587 * get_lsr_info - get line status register info
1588 *
1589 * Purpose: Let user call ioctl() to get info when the UART physically
1590 * is emptied. On bus types like RS485, the transmitter must
1591 * release the bus after transmitting. This must be done when
1592 * the transmit shift register is empty, not be done when the
1593 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001594 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001596static int get_lsr_info(struct edgeport_port *edge_port,
1597 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 unsigned int result = 0;
1600 unsigned long flags;
1601
1602 spin_lock_irqsave(&edge_port->ep_lock, flags);
1603 if (edge_port->maxTxCredits == edge_port->txCredits &&
1604 edge_port->txfifo.count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001605 dbg("%s -- Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 result = TIOCSER_TEMT;
1607 }
1608 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1609
1610 if (copy_to_user(value, &result, sizeof(int)))
1611 return -EFAULT;
1612 return 0;
1613}
1614
Alan Cox03f0dbf2008-07-22 11:16:34 +01001615static int edge_tiocmset(struct tty_struct *tty, struct file *file,
1616 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Alan Cox95da3102008-07-22 11:09:07 +01001618 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1620 unsigned int mcr;
1621
Harvey Harrison441b62c2008-03-03 16:08:34 -08001622 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 mcr = edge_port->shadowMCR;
1625 if (set & TIOCM_RTS)
1626 mcr |= MCR_RTS;
1627 if (set & TIOCM_DTR)
1628 mcr |= MCR_DTR;
1629 if (set & TIOCM_LOOP)
1630 mcr |= MCR_LOOPBACK;
1631
1632 if (clear & TIOCM_RTS)
1633 mcr &= ~MCR_RTS;
1634 if (clear & TIOCM_DTR)
1635 mcr &= ~MCR_DTR;
1636 if (clear & TIOCM_LOOP)
1637 mcr &= ~MCR_LOOPBACK;
1638
1639 edge_port->shadowMCR = mcr;
1640
1641 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1642
1643 return 0;
1644}
1645
Alan Cox95da3102008-07-22 11:09:07 +01001646static int edge_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Alan Cox95da3102008-07-22 11:09:07 +01001648 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1650 unsigned int result = 0;
1651 unsigned int msr;
1652 unsigned int mcr;
1653
Harvey Harrison441b62c2008-03-03 16:08:34 -08001654 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 msr = edge_port->shadowMSR;
1657 mcr = edge_port->shadowMCR;
1658 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1659 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1660 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1661 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1662 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1663 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1664
1665
Harvey Harrison441b62c2008-03-03 16:08:34 -08001666 dbg("%s -- %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 return result;
1669}
1670
Alan Cox03f0dbf2008-07-22 11:16:34 +01001671static int get_serial_info(struct edgeport_port *edge_port,
1672 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct serial_struct tmp;
1675
1676 if (!retinfo)
1677 return -EFAULT;
1678
1679 memset(&tmp, 0, sizeof(tmp));
1680
1681 tmp.type = PORT_16550A;
1682 tmp.line = edge_port->port->serial->minor;
1683 tmp.port = edge_port->port->number;
1684 tmp.irq = 0;
1685 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1686 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1687 tmp.baud_base = 9600;
1688 tmp.close_delay = 5*HZ;
1689 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1692 return -EFAULT;
1693 return 0;
1694}
1695
1696
1697
1698/*****************************************************************************
1699 * SerialIoctl
1700 * this function handles any ioctl calls to the driver
1701 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001702static int edge_ioctl(struct tty_struct *tty, struct file *file,
1703 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Alan Cox95da3102008-07-22 11:09:07 +01001705 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 DEFINE_WAIT(wait);
1707 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1708 struct async_icount cnow;
1709 struct async_icount cprev;
1710 struct serial_icounter_struct icount;
1711
Harvey Harrison441b62c2008-03-03 16:08:34 -08001712 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001715 case TIOCSERGETLSR:
1716 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1717 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Alan Cox03f0dbf2008-07-22 11:16:34 +01001719 case TIOCGSERIAL:
1720 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1721 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Alan Cox03f0dbf2008-07-22 11:16:34 +01001723 case TIOCMIWAIT:
1724 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1725 cprev = edge_port->icount;
1726 while (1) {
1727 prepare_to_wait(&edge_port->delta_msr_wait,
1728 &wait, TASK_INTERRUPTIBLE);
1729 schedule();
1730 finish_wait(&edge_port->delta_msr_wait, &wait);
1731 /* see if a signal did it */
1732 if (signal_pending(current))
1733 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001735 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1736 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1737 return -EIO; /* no change => error */
1738 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1739 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1740 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1741 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1742 return 0;
1743 }
1744 cprev = cnow;
1745 }
1746 /* NOTREACHED */
1747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Alan Cox03f0dbf2008-07-22 11:16:34 +01001749 case TIOCGICOUNT:
1750 cnow = edge_port->icount;
1751 memset(&icount, 0, sizeof(icount));
1752 icount.cts = cnow.cts;
1753 icount.dsr = cnow.dsr;
1754 icount.rng = cnow.rng;
1755 icount.dcd = cnow.dcd;
1756 icount.rx = cnow.rx;
1757 icount.tx = cnow.tx;
1758 icount.frame = cnow.frame;
1759 icount.overrun = cnow.overrun;
1760 icount.parity = cnow.parity;
1761 icount.brk = cnow.brk;
1762 icount.buf_overrun = cnow.buf_overrun;
1763
1764 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
1765 __func__, port->number, icount.rx, icount.tx);
1766 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1767 return -EFAULT;
1768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return -ENOIOCTLCMD;
1771}
1772
1773
1774/*****************************************************************************
1775 * SerialBreak
1776 * this function sends a break to the port
1777 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001778static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Alan Cox95da3102008-07-22 11:09:07 +01001780 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001782 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int status;
1784
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001785 if ((!edge_serial->is_epic) ||
1786 ((edge_serial->is_epic) &&
1787 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1788 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001789 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Harvey Harrison441b62c2008-03-03 16:08:34 -08001791 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001792 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001793 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001794 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001795 block_until_chase_response(edge_port);
1796 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001797 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001801 if ((!edge_serial->is_epic) ||
1802 ((edge_serial->is_epic) &&
1803 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1804 if (break_state == -1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001805 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001806 status = send_iosp_ext_cmd(edge_port,
1807 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001808 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001809 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001810 status = send_iosp_ext_cmd(edge_port,
1811 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001812 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001813 if (status)
1814 dbg("%s - error sending break set/clear command.",
1815 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817
1818 return;
1819}
1820
1821
1822/*****************************************************************************
1823 * process_rcvd_data
1824 * this function handles the data received on the bulk in pipe.
1825 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001826static void process_rcvd_data(struct edgeport_serial *edge_serial,
1827 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 struct usb_serial_port *port;
1830 struct edgeport_port *edge_port;
1831 struct tty_struct *tty;
1832 __u16 lastBufferLength;
1833 __u16 rxLen;
1834
Harvey Harrison441b62c2008-03-03 16:08:34 -08001835 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 lastBufferLength = bufferLength + 1;
1838
1839 while (bufferLength > 0) {
1840 /* failsafe incase we get a message that we don't understand */
1841 if (lastBufferLength == bufferLength) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001842 dbg("%s - stuck in loop, exiting it.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844 }
1845 lastBufferLength = bufferLength;
1846
1847 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001848 case EXPECT_HDR1:
1849 edge_serial->rxHeader1 = *buffer;
1850 ++buffer;
1851 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Alan Cox03f0dbf2008-07-22 11:16:34 +01001853 if (bufferLength == 0) {
1854 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001856 }
1857 /* otherwise, drop on through */
1858 case EXPECT_HDR2:
1859 edge_serial->rxHeader2 = *buffer;
1860 ++buffer;
1861 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Alan Cox03f0dbf2008-07-22 11:16:34 +01001863 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
1864 edge_serial->rxHeader1, edge_serial->rxHeader2);
1865 /* Process depending on whether this header is
1866 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Alan Cox03f0dbf2008-07-22 11:16:34 +01001868 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1869 /* Decode this status header and go to
1870 * EXPECT_HDR1 (if we can process the status
1871 * with only 2 bytes), or go to EXPECT_HDR3 to
1872 * get the third byte. */
1873 edge_serial->rxPort =
1874 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1875 edge_serial->rxStatusCode =
1876 IOSP_GET_STATUS_CODE(
1877 edge_serial->rxHeader1);
1878
1879 if (!IOSP_STATUS_IS_2BYTE(
1880 edge_serial->rxStatusCode)) {
1881 /* This status needs additional bytes.
1882 * Save what we have and then wait for
1883 * more data.
1884 */
1885 edge_serial->rxStatusParam
1886 = edge_serial->rxHeader2;
1887 edge_serial->rxState = EXPECT_HDR3;
1888 break;
1889 }
1890 /* We have all the header bytes, process the
1891 status now */
1892 process_rcvd_status(edge_serial,
1893 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 edge_serial->rxState = EXPECT_HDR1;
1895 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001896 } else {
1897 edge_serial->rxPort =
1898 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1899 edge_serial->rxBytesRemaining =
1900 IOSP_GET_HDR_DATA_LEN(
1901 edge_serial->rxHeader1,
1902 edge_serial->rxHeader2);
1903 dbg("%s - Data for Port %u Len %u",
1904 __func__,
1905 edge_serial->rxPort,
1906 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Alan Cox03f0dbf2008-07-22 11:16:34 +01001908 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1909 * ASSERT(DevExt->RxBytesRemaining <
1910 * IOSP_MAX_DATA_LENGTH);
1911 */
1912
1913 if (bufferLength == 0) {
1914 edge_serial->rxState = EXPECT_DATA;
1915 break;
1916 }
1917 /* Else, drop through */
1918 }
1919 case EXPECT_DATA: /* Expect data */
1920 if (bufferLength < edge_serial->rxBytesRemaining) {
1921 rxLen = bufferLength;
1922 /* Expect data to start next buffer */
1923 edge_serial->rxState = EXPECT_DATA;
1924 } else {
1925 /* BufLen >= RxBytesRemaining */
1926 rxLen = edge_serial->rxBytesRemaining;
1927 /* Start another header next time */
1928 edge_serial->rxState = EXPECT_HDR1;
1929 }
1930
1931 bufferLength -= rxLen;
1932 edge_serial->rxBytesRemaining -= rxLen;
1933
1934 /* spit this data back into the tty driver if this
1935 port is open */
1936 if (rxLen) {
1937 port = edge_serial->serial->port[
1938 edge_serial->rxPort];
1939 edge_port = usb_get_serial_port_data(port);
1940 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001941 tty = tty_port_tty_get(
1942 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001943 if (tty) {
1944 dbg("%s - Sending %d bytes to TTY for port %d",
1945 __func__, rxLen, edge_serial->rxPort);
1946 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001947 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001948 }
1949 edge_port->icount.rx += rxLen;
1950 }
1951 buffer += rxLen;
1952 }
1953 break;
1954
1955 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1956 edge_serial->rxHeader3 = *buffer;
1957 ++buffer;
1958 --bufferLength;
1959
1960 /* We have all the header bytes, process the
1961 status now */
1962 process_rcvd_status(edge_serial,
1963 edge_serial->rxStatusParam,
1964 edge_serial->rxHeader3);
1965 edge_serial->rxState = EXPECT_HDR1;
1966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 }
1969}
1970
1971
1972/*****************************************************************************
1973 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001974 * this function handles the any status messages received on the
1975 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001977static void process_rcvd_status(struct edgeport_serial *edge_serial,
1978 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct usb_serial_port *port;
1981 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001982 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 __u8 code = edge_serial->rxStatusCode;
1984
1985 /* switch the port pointer to the one being currently talked about */
1986 port = edge_serial->serial->port[edge_serial->rxPort];
1987 edge_port = usb_get_serial_port_data(port);
1988 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001989 dev_err(&edge_serial->serial->dev->dev,
1990 "%s - edge_port == NULL for port %d\n",
1991 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return;
1993 }
1994
Harvey Harrison441b62c2008-03-03 16:08:34 -08001995 dbg("%s - port %d", __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 if (code == IOSP_EXT_STATUS) {
1998 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001999 case IOSP_EXT_STATUS_CHASE_RSP:
2000 /* we want to do EXT status regardless of port
2001 * open/closed */
2002 dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
2003 __func__, edge_serial->rxPort, byte3);
2004 /* Currently, the only EXT_STATUS is Chase, so process
2005 * here instead of one more call to one more subroutine
2006 * If/when more EXT_STATUS, there'll be more work to do
2007 * Also, we currently clear flag and close the port
2008 * regardless of content of above's Byte3.
2009 * We could choose to do something else when Byte3 says
2010 * Timeout on Chase from Edgeport, like wait longer in
2011 * block_until_chase_response, but for now we don't.
2012 */
2013 edge_port->chaseResponsePending = false;
2014 wake_up(&edge_port->wait_chase);
2015 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Alan Cox03f0dbf2008-07-22 11:16:34 +01002017 case IOSP_EXT_STATUS_RX_CHECK_RSP:
2018 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3);
2019 /* Port->RxCheckRsp = true; */
2020 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 }
2023
2024 if (code == IOSP_STATUS_OPEN_RSP) {
2025 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2026 edge_port->maxTxCredits = edge_port->txCredits;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002027 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002028 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Alan Cox03f0dbf2008-07-22 11:16:34 +01002030 /* send the current line settings to the port so we are
2031 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01002032 tty = tty_port_tty_get(&edge_port->port->port);
2033 if (tty) {
2034 change_port_settings(tty,
2035 edge_port, tty->termios);
2036 tty_kref_put(tty);
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002040 edge_port->openPending = false;
2041 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 wake_up(&edge_port->wait_open);
2043 return;
2044 }
2045
Alan Cox03f0dbf2008-07-22 11:16:34 +01002046 /* If port is closed, silently discard all rcvd status. We can
2047 * have cases where buffered status is received AFTER the close
2048 * port command is sent to the Edgeport.
2049 */
2050 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002054 /* Not currently sent by Edgeport */
2055 case IOSP_STATUS_LSR:
2056 dbg("%s - Port %u LSR Status = %02x",
2057 __func__, edge_serial->rxPort, byte2);
2058 handle_new_lsr(edge_port, false, byte2, 0);
2059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Alan Cox03f0dbf2008-07-22 11:16:34 +01002061 case IOSP_STATUS_LSR_DATA:
2062 dbg("%s - Port %u LSR Status = %02x, Data = %02x",
2063 __func__, edge_serial->rxPort, byte2, byte3);
2064 /* byte2 is LSR Register */
2065 /* byte3 is broken data byte */
2066 handle_new_lsr(edge_port, true, byte2, byte3);
2067 break;
2068 /*
2069 * case IOSP_EXT_4_STATUS:
2070 * dbg("%s - Port %u LSR Status = %02x Data = %02x",
2071 * __func__, edge_serial->rxPort, byte2, byte3);
2072 * break;
2073 */
2074 case IOSP_STATUS_MSR:
2075 dbg("%s - Port %u MSR Status = %02x",
2076 __func__, edge_serial->rxPort, byte2);
2077 /*
2078 * Process this new modem status and generate appropriate
2079 * events, etc, based on the new status. This routine
2080 * also saves the MSR in Port->ShadowMsr.
2081 */
2082 handle_new_msr(edge_port, byte2);
2083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Alan Cox03f0dbf2008-07-22 11:16:34 +01002085 default:
2086 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
2087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return;
2090}
2091
2092
2093/*****************************************************************************
2094 * edge_tty_recv
2095 * this function passes data on to the tty flip buffer
2096 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002097static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2098 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 int cnt;
2101
2102 do {
Alan Cox33f0f882006-01-09 20:54:13 -08002103 cnt = tty_buffer_request_room(tty, length);
2104 if (cnt < length) {
2105 dev_err(dev, "%s - dropping data, %d bytes lost\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002106 __func__, length - cnt);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002107 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Alan Cox33f0f882006-01-09 20:54:13 -08002110 tty_insert_flip_string(tty, data, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 data += cnt;
2112 length -= cnt;
2113 } while (length > 0);
2114
2115 tty_flip_buffer_push(tty);
2116}
2117
2118
2119/*****************************************************************************
2120 * handle_new_msr
2121 * this function handles any change to the msr register for a port.
2122 *****************************************************************************/
2123static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2124{
2125 struct async_icount *icount;
2126
Harvey Harrison441b62c2008-03-03 16:08:34 -08002127 dbg("%s %02x", __func__, newMsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2130 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 icount = &edge_port->icount;
2132
2133 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002134 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002136 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002138 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002140 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 wake_up_interruptible(&edge_port->delta_msr_wait);
2143 }
2144
2145 /* Save the new modem status */
2146 edge_port->shadowMSR = newMsr & 0xf0;
2147
2148 return;
2149}
2150
2151
2152/*****************************************************************************
2153 * handle_new_lsr
2154 * this function handles any change to the lsr register for a port.
2155 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002156static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2157 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002159 __u8 newLsr = (__u8) (lsr & (__u8)
2160 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2161 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Harvey Harrison441b62c2008-03-03 16:08:34 -08002163 dbg("%s - %02x", __func__, newLsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 edge_port->shadowLSR = lsr;
2166
2167 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002168 /*
2169 * Parity and Framing errors only count if they
2170 * occur exclusive of a break being
2171 * received.
2172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2174 }
2175
2176 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002177 if (lsrData) {
2178 struct tty_struct *tty =
2179 tty_port_tty_get(&edge_port->port->port);
2180 if (tty) {
2181 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2182 tty_kref_put(tty);
2183 }
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* update input line counters */
2186 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002187 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002189 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002191 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002193 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 return;
2197}
2198
2199
2200/****************************************************************************
2201 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002202 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * given address.
2204 * If successful returns the number of bytes written, otherwise it returns
2205 * a negative error number of the problem.
2206 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002207static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2208 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 int result;
2211 __u16 current_length;
2212 unsigned char *transfer_buffer;
2213
Harvey Harrison441b62c2008-03-03 16:08:34 -08002214 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Alan Cox03f0dbf2008-07-22 11:16:34 +01002216 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002218 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2219 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return -ENOMEM;
2221 }
2222
2223 /* need to split these writes up into 64 byte chunks */
2224 result = 0;
2225 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002226 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002228 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002230
2231/* dbg("%s - writing %x, %x, %d", __func__,
2232 extAddr, addr, current_length); */
2233 memcpy(transfer_buffer, data, current_length);
2234 result = usb_control_msg(serial->dev,
2235 usb_sndctrlpipe(serial->dev, 0),
2236 USB_REQUEST_ION_WRITE_RAM,
2237 0x40, addr, extAddr, transfer_buffer,
2238 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (result < 0)
2240 break;
2241 length -= current_length;
2242 addr += current_length;
2243 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Alan Cox03f0dbf2008-07-22 11:16:34 +01002246 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return result;
2248}
2249
2250
2251/****************************************************************************
2252 * rom_write
2253 * writes a number of bytes to the Edgeport device's ROM starting at the
2254 * given address.
2255 * If successful returns the number of bytes written, otherwise it returns
2256 * a negative error number of the problem.
2257 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002258static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2259 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 int result;
2262 __u16 current_length;
2263 unsigned char *transfer_buffer;
2264
Alan Cox03f0dbf2008-07-22 11:16:34 +01002265/* dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Alan Cox03f0dbf2008-07-22 11:16:34 +01002267 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002269 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2270 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return -ENOMEM;
2272 }
2273
2274 /* need to split these writes up into 64 byte chunks */
2275 result = 0;
2276 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002277 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002279 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002281/* dbg("%s - writing %x, %x, %d", __func__,
2282 extAddr, addr, current_length); */
2283 memcpy(transfer_buffer, data, current_length);
2284 result = usb_control_msg(serial->dev,
2285 usb_sndctrlpipe(serial->dev, 0),
2286 USB_REQUEST_ION_WRITE_ROM, 0x40,
2287 addr, extAddr,
2288 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (result < 0)
2290 break;
2291 length -= current_length;
2292 addr += current_length;
2293 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Alan Cox03f0dbf2008-07-22 11:16:34 +01002296 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return result;
2298}
2299
2300
2301/****************************************************************************
2302 * rom_read
2303 * reads a number of bytes from the Edgeport device starting at the given
2304 * address.
2305 * If successful returns the number of bytes read, otherwise it returns
2306 * a negative error number of the problem.
2307 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002308static int rom_read(struct usb_serial *serial, __u16 extAddr,
2309 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 int result;
2312 __u16 current_length;
2313 unsigned char *transfer_buffer;
2314
Harvey Harrison441b62c2008-03-03 16:08:34 -08002315 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Alan Cox03f0dbf2008-07-22 11:16:34 +01002317 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002319 dev_err(&serial->dev->dev,
2320 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return -ENOMEM;
2322 }
2323
2324 /* need to split these reads up into 64 byte chunks */
2325 result = 0;
2326 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002327 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002329 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002331/* dbg("%s - %x, %x, %d", __func__,
2332 extAddr, addr, current_length); */
2333 result = usb_control_msg(serial->dev,
2334 usb_rcvctrlpipe(serial->dev, 0),
2335 USB_REQUEST_ION_READ_ROM,
2336 0xC0, addr, extAddr, transfer_buffer,
2337 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (result < 0)
2339 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002340 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 length -= current_length;
2342 addr += current_length;
2343 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Alan Cox03f0dbf2008-07-22 11:16:34 +01002346 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return result;
2348}
2349
2350
2351/****************************************************************************
2352 * send_iosp_ext_cmd
2353 * Is used to send a IOSP message to the Edgeport device
2354 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002355static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2356 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 unsigned char *buffer;
2359 unsigned char *currentCommand;
2360 int length = 0;
2361 int status = 0;
2362
Harvey Harrison441b62c2008-03-03 16:08:34 -08002363 dbg("%s - %d, %d", __func__, command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Alan Cox03f0dbf2008-07-22 11:16:34 +01002365 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002367 dev_err(&edge_port->port->dev,
2368 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -ENOMEM;
2370 }
2371
2372 currentCommand = buffer;
2373
Alan Cox03f0dbf2008-07-22 11:16:34 +01002374 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2375 edge_port->port->number - edge_port->port->serial->minor,
2376 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alan Cox03f0dbf2008-07-22 11:16:34 +01002378 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (status) {
2380 /* something bad happened, let's free up the memory */
2381 kfree(buffer);
2382 }
2383
2384 return status;
2385}
2386
2387
2388/*****************************************************************************
2389 * write_cmd_usb
2390 * this function writes the given buffer out to the bulk write endpoint.
2391 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002392static int write_cmd_usb(struct edgeport_port *edge_port,
2393 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002395 struct edgeport_serial *edge_serial =
2396 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int status = 0;
2398 struct urb *urb;
2399 int timeout;
2400
Alan Cox03f0dbf2008-07-22 11:16:34 +01002401 usb_serial_debug_data(debug, &edge_port->port->dev,
2402 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002405 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (!urb)
2407 return -ENOMEM;
2408
Oliver Neukum96c706e2007-03-15 15:27:17 +01002409 atomic_inc(&CmdUrbs);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002410 dbg("%s - ALLOCATE URB %p (outstanding %d)",
2411 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Alan Cox03f0dbf2008-07-22 11:16:34 +01002413 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2414 usb_sndbulkpipe(edge_serial->serial->dev,
2415 edge_serial->bulk_out_endpoint),
2416 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002418 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 status = usb_submit_urb(urb, GFP_ATOMIC);
2420
2421 if (status) {
2422 /* something went wrong */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002423 dev_err(&edge_port->port->dev,
2424 "%s - usb_submit_urb(write command) failed, status = %d\n",
2425 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 usb_kill_urb(urb);
2427 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002428 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return status;
2430 }
2431
Alan Cox03f0dbf2008-07-22 11:16:34 +01002432 /* wait for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 timeout = COMMAND_TIMEOUT;
2434#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002435 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002437 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* command timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002439 dbg("%s - command timed out", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 status = -EINVAL;
2441 }
2442#endif
2443 return status;
2444}
2445
2446
2447/*****************************************************************************
2448 * send_cmd_write_baud_rate
2449 * this function sends the proper command to change the baud rate of the
2450 * specified port.
2451 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002452static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2453 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002455 struct edgeport_serial *edge_serial =
2456 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 unsigned char *cmdBuffer;
2458 unsigned char *currCmd;
2459 int cmdLen = 0;
2460 int divisor;
2461 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002462 unsigned char number =
2463 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Adam Kropelinbc71e472007-07-29 11:03:29 -04002465 if (edge_serial->is_epic &&
2466 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002467 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2468 edge_port->port->number, baudRate);
2469 return 0;
2470 }
2471
Alan Cox03f0dbf2008-07-22 11:16:34 +01002472 dbg("%s - port = %d, baud = %d", __func__,
2473 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Alan Cox03f0dbf2008-07-22 11:16:34 +01002475 status = calc_baud_rate_divisor(baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (status) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002477 dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
2478 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return status;
2480 }
2481
Alan Cox03f0dbf2008-07-22 11:16:34 +01002482 /* Alloc memory for the string of commands. */
2483 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (!cmdBuffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002485 dev_err(&edge_port->port->dev,
2486 "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return -ENOMEM;
2488 }
2489 currCmd = cmdBuffer;
2490
Alan Cox03f0dbf2008-07-22 11:16:34 +01002491 /* Enable access to divisor latch */
2492 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Alan Cox03f0dbf2008-07-22 11:16:34 +01002494 /* Write the divisor itself */
2495 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2496 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Alan Cox03f0dbf2008-07-22 11:16:34 +01002498 /* Restore original value to disable access to divisor latch */
2499 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2500 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Alan Cox03f0dbf2008-07-22 11:16:34 +01002502 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (status) {
2504 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002505 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
2508 return status;
2509}
2510
2511
2512/*****************************************************************************
2513 * calc_baud_rate_divisor
2514 * this function calculates the proper baud rate divisor for the specified
2515 * baud rate.
2516 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002517static int calc_baud_rate_divisor(int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
2519 int i;
2520 __u16 custom;
2521
2522
Harvey Harrison441b62c2008-03-03 16:08:34 -08002523 dbg("%s - %d", __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Tobias Klauser52950ed2005-12-11 16:20:08 +01002525 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002526 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 *divisor = divisor_table[i].Divisor;
2528 return 0;
2529 }
2530 }
2531
Alan Cox03f0dbf2008-07-22 11:16:34 +01002532 /* We have tried all of the standard baud rates
2533 * lets try to calculate the divisor for this baud rate
2534 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002536 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 custom = (__u16)((230400L + baudrate/2) / baudrate);
2538
2539 *divisor = custom;
2540
Harvey Harrison441b62c2008-03-03 16:08:34 -08002541 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return 0;
2543 }
2544
2545 return -1;
2546}
2547
2548
2549/*****************************************************************************
2550 * send_cmd_write_uart_register
Alan Cox03f0dbf2008-07-22 11:16:34 +01002551 * this function builds up a uart register message and sends to to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002553static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2554 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002556 struct edgeport_serial *edge_serial =
2557 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 unsigned char *cmdBuffer;
2559 unsigned char *currCmd;
2560 unsigned long cmdLen = 0;
2561 int status;
2562
Alan Cox03f0dbf2008-07-22 11:16:34 +01002563 dbg("%s - write to %s register 0x%02x",
2564 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Adam Kropelinbc71e472007-07-29 11:03:29 -04002566 if (edge_serial->is_epic &&
2567 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2568 regNum == MCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002569 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002570 return 0;
2571 }
2572
Adam Kropelinbc71e472007-07-29 11:03:29 -04002573 if (edge_serial->is_epic &&
2574 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2575 regNum == LCR) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002576 dbg("SendCmdWriteUartReg - Not writing to LCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002577 return 0;
2578 }
2579
Alan Cox03f0dbf2008-07-22 11:16:34 +01002580 /* Alloc memory for the string of commands. */
2581 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2582 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 currCmd = cmdBuffer;
2586
Alan Cox03f0dbf2008-07-22 11:16:34 +01002587 /* Build a cmd in the buffer to write the given register */
2588 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2589 edge_port->port->number - edge_port->port->serial->minor,
2590 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2593 if (status) {
2594 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002595 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597
2598 return status;
2599}
2600
2601
2602/*****************************************************************************
2603 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002604 * This routine is called to set the UART on the device to match the
2605 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002607
2608static void change_port_settings(struct tty_struct *tty,
2609 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002611 struct edgeport_serial *edge_serial =
2612 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 int baud;
2614 unsigned cflag;
2615 __u8 mask = 0xff;
2616 __u8 lData;
2617 __u8 lParity;
2618 __u8 lStop;
2619 __u8 rxFlow;
2620 __u8 txFlow;
2621 int status;
2622
Harvey Harrison441b62c2008-03-03 16:08:34 -08002623 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002625 if (!edge_port->open &&
2626 !edge_port->openPending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002627 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return;
2629 }
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 cflag = tty->termios->c_cflag;
2632
2633 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002634 case CS5:
2635 lData = LCR_BITS_5; mask = 0x1f;
2636 dbg("%s - data bits = 5", __func__);
2637 break;
2638 case CS6:
2639 lData = LCR_BITS_6; mask = 0x3f;
2640 dbg("%s - data bits = 6", __func__);
2641 break;
2642 case CS7:
2643 lData = LCR_BITS_7; mask = 0x7f;
2644 dbg("%s - data bits = 7", __func__);
2645 break;
2646 default:
2647 case CS8:
2648 lData = LCR_BITS_8;
2649 dbg("%s - data bits = 8", __func__);
2650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652
2653 lParity = LCR_PAR_NONE;
2654 if (cflag & PARENB) {
2655 if (cflag & CMSPAR) {
2656 if (cflag & PARODD) {
2657 lParity = LCR_PAR_MARK;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002658 dbg("%s - parity = mark", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 } else {
2660 lParity = LCR_PAR_SPACE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002661 dbg("%s - parity = space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 } else if (cflag & PARODD) {
2664 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002665 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 } else {
2667 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002668 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002671 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 }
2673
2674 if (cflag & CSTOPB) {
2675 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002676 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 } else {
2678 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002679 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
2681
2682 /* figure out the flow control settings */
2683 rxFlow = txFlow = 0x00;
2684 if (cflag & CRTSCTS) {
2685 rxFlow |= IOSP_RX_FLOW_RTS;
2686 txFlow |= IOSP_TX_FLOW_CTS;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002687 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002689 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
2691
Alan Cox03f0dbf2008-07-22 11:16:34 +01002692 /* if we are implementing XON/XOFF, set the start and stop character
2693 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (I_IXOFF(tty) || I_IXON(tty)) {
2695 unsigned char stop_char = STOP_CHAR(tty);
2696 unsigned char start_char = START_CHAR(tty);
2697
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002698 if ((!edge_serial->is_epic) ||
2699 ((edge_serial->is_epic) &&
2700 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002701 send_iosp_ext_cmd(edge_port,
2702 IOSP_CMD_SET_XON_CHAR, start_char);
2703 send_iosp_ext_cmd(edge_port,
2704 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 /* if we are implementing INBOUND XON/XOFF */
2708 if (I_IXOFF(tty)) {
2709 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002710 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2711 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002713 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715
2716 /* if we are implementing OUTBOUND XON/XOFF */
2717 if (I_IXON(tty)) {
2718 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002719 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2720 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002722 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 }
2725
2726 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002727 if ((!edge_serial->is_epic) ||
2728 ((edge_serial->is_epic) &&
2729 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2730 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2731 if ((!edge_serial->is_epic) ||
2732 ((edge_serial->is_epic) &&
2733 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2734 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736
2737 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2738 edge_port->shadowLCR |= (lData | lParity | lStop);
2739
2740 edge_port->validDataMask = mask;
2741
2742 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002743 status = send_cmd_write_uart_register(edge_port, LCR,
2744 edge_port->shadowLCR);
2745 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 /* set up the MCR register and send it to the EdgePort */
2749 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002750 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002752
2753 status = send_cmd_write_uart_register(edge_port, MCR,
2754 edge_port->shadowMCR);
2755 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 /* Determine divisor based on baud rate */
2759 baud = tty_get_baud_rate(tty);
2760 if (!baud) {
2761 /* pick a default, any default... */
2762 baud = 9600;
2763 }
2764
Harvey Harrison441b62c2008-03-03 16:08:34 -08002765 dbg("%s - baud rate = %d", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002766 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002767 if (status == -1) {
2768 /* Speed change was not possible - put back the old speed */
2769 baud = tty_termios_baud_rate(old_termios);
2770 tty_encode_baud_rate(tty, baud, baud);
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return;
2773}
2774
2775
2776/****************************************************************************
2777 * unicode_to_ascii
2778 * Turns a string from Unicode into ASCII.
2779 * Doesn't do a good job with any characters that are outside the normal
2780 * ASCII range, but it's only for debugging...
2781 * NOTE: expects the unicode in LE format
2782 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002783static void unicode_to_ascii(char *string, int buflen,
2784 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
2786 int i;
2787
Pete Zaitcevad933752006-05-22 21:49:44 -07002788 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002790 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Pete Zaitcevad933752006-05-22 21:49:44 -07002792 for (i = 0; i < unicode_size; i++) {
2793 if (i >= buflen)
2794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002796 }
2797 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800
2801/****************************************************************************
2802 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002803 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * structure.
2805 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002806static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 int response;
2809
2810 dbg("getting manufacturer descriptor");
2811
Alan Cox03f0dbf2008-07-22 11:16:34 +01002812 response = rom_read(edge_serial->serial,
2813 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2814 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2815 EDGE_MANUF_DESC_LEN,
2816 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Alan Cox03f0dbf2008-07-22 11:16:34 +01002818 if (response < 1)
2819 dev_err(&edge_serial->serial->dev->dev,
2820 "error in getting manufacturer descriptor\n");
2821 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 char string[30];
2823 dbg("**Manufacturer Descriptor");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002824 dbg(" RomSize: %dK",
2825 edge_serial->manuf_descriptor.RomSize);
2826 dbg(" RamSize: %dK",
2827 edge_serial->manuf_descriptor.RamSize);
2828 dbg(" CpuRev: %d",
2829 edge_serial->manuf_descriptor.CpuRev);
2830 dbg(" BoardRev: %d",
2831 edge_serial->manuf_descriptor.BoardRev);
2832 dbg(" NumPorts: %d",
2833 edge_serial->manuf_descriptor.NumPorts);
2834 dbg(" DescDate: %d/%d/%d",
2835 edge_serial->manuf_descriptor.DescDate[0],
2836 edge_serial->manuf_descriptor.DescDate[1],
2837 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002838 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002839 edge_serial->manuf_descriptor.SerialNumber,
2840 edge_serial->manuf_descriptor.SerNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 dbg(" SerialNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002842 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002843 edge_serial->manuf_descriptor.AssemblyNumber,
2844 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 dbg(" AssemblyNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002846 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002847 edge_serial->manuf_descriptor.OemAssyNumber,
2848 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 dbg(" OemAssyNumber: %s", string);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002850 dbg(" UartType: %d",
2851 edge_serial->manuf_descriptor.UartType);
2852 dbg(" IonPid: %d",
2853 edge_serial->manuf_descriptor.IonPid);
2854 dbg(" IonConfig: %d",
2855 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857}
2858
2859
2860/****************************************************************************
2861 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002862 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 * structure.
2864 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002865static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 int response;
2868
2869 dbg("getting boot descriptor");
2870
Alan Cox03f0dbf2008-07-22 11:16:34 +01002871 response = rom_read(edge_serial->serial,
2872 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2873 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2874 EDGE_BOOT_DESC_LEN,
2875 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Alan Cox03f0dbf2008-07-22 11:16:34 +01002877 if (response < 1)
2878 dev_err(&edge_serial->serial->dev->dev,
2879 "error in getting boot descriptor\n");
2880 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 dbg("**Boot Descriptor:");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002882 dbg(" BootCodeLength: %d",
2883 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2884 dbg(" MajorVersion: %d",
2885 edge_serial->boot_descriptor.MajorVersion);
2886 dbg(" MinorVersion: %d",
2887 edge_serial->boot_descriptor.MinorVersion);
2888 dbg(" BuildNumber: %d",
2889 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2890 dbg(" Capabilities: 0x%x",
2891 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2892 dbg(" UConfig0: %d",
2893 edge_serial->boot_descriptor.UConfig0);
2894 dbg(" UConfig1: %d",
2895 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
2897}
2898
2899
2900/****************************************************************************
2901 * load_application_firmware
2902 * This is called to load the application firmware to the device
2903 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002904static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302906 const struct ihex_binrec *rec;
2907 const struct firmware *fw;
2908 const char *fw_name;
2909 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302911 __u32 Operaddr;
2912 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 switch (edge_serial->product_info.iDownloadFile) {
2915 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302916 fw_info = "downloading firmware version (930)";
2917 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 break;
2919
2920 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302921 fw_info = "downloading firmware version (80251)";
2922 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 break;
2924
2925 case EDGE_DOWNLOAD_FILE_NONE:
2926 dbg ("No download file specified, skipping download\n");
2927 return;
2928
2929 default:
2930 return;
2931 }
2932
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302933 response = request_ihex_firmware(&fw, fw_name,
2934 &edge_serial->serial->dev->dev);
2935 if (response) {
2936 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2937 fw_name, response);
2938 return;
2939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302941 rec = (const struct ihex_binrec *)fw->data;
2942 build = (rec->data[2] << 8) | rec->data[3];
2943
2944 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2945
2946 edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
2947 edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
2948 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2949
2950 for (rec = ihex_next_binrec(rec); rec;
2951 rec = ihex_next_binrec(rec)) {
2952 Operaddr = be32_to_cpu(rec->addr);
2953 response = sram_write(edge_serial->serial,
2954 Operaddr >> 16,
2955 Operaddr & 0xFFFF,
2956 be16_to_cpu(rec->len),
2957 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302959 dev_err(&edge_serial->serial->dev->dev,
2960 "sram_write failed (%x, %x, %d)\n",
2961 Operaddr >> 16, Operaddr & 0xFFFF,
2962 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 break;
2964 }
2965 }
2966
2967 dbg("sending exec_dl_code");
2968 response = usb_control_msg (edge_serial->serial->dev,
2969 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2970 USB_REQUEST_ION_EXEC_DL_CODE,
2971 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2972
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302973 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return;
2975}
2976
2977
2978/****************************************************************************
2979 * edge_startup
2980 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002981static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct edgeport_serial *edge_serial;
2984 struct edgeport_port *edge_port;
2985 struct usb_device *dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002986 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002988 bool interrupt_in_found;
2989 bool bulk_in_found;
2990 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002991 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2992 EDGE_COMPATIBILITY_MASK1,
2993 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 dev = serial->dev;
2996
2997 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002998 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08003000 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return -ENOMEM;
3002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 spin_lock_init(&edge_serial->es_lock);
3004 edge_serial->serial = serial;
3005 usb_set_serial_data(serial, edge_serial);
3006
3007 /* get the name for the device from the device */
Pete Zaitcevad933752006-05-22 21:49:44 -07003008 i = get_string(dev, dev->descriptor.iManufacturer,
3009 &edge_serial->name[0], MAX_NAME_LEN+1);
3010 edge_serial->name[i++] = ' ';
3011 get_string(dev, dev->descriptor.iProduct,
3012 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
3015
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003016 /* Read the epic descriptor */
3017 if (get_epic_descriptor(edge_serial) <= 0) {
3018 /* memcpy descriptor to Supports structures */
3019 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
3020 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003022 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003023 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003025 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003026 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003027
3028 get_product_info(edge_serial);
3029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /* set the number of ports from the manufacturing description */
3032 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003033 if ((!edge_serial->is_epic) &&
3034 (edge_serial->product_info.NumPorts != serial->num_ports)) {
3035 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
3036 "vs. core thinking we have %d ports, email "
Joe Perches898eb712007-10-18 03:06:30 -07003037 "greg@kroah.com this information.\n",
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003038 edge_serial->product_info.NumPorts,
3039 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041
Harvey Harrison441b62c2008-03-03 16:08:34 -08003042 dbg("%s - time 1 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003044 /* If not an EPiC device */
3045 if (!edge_serial->is_epic) {
3046 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003047 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Harvey Harrison441b62c2008-03-03 16:08:34 -08003049 dbg("%s - time 2 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003051 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003052 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Harvey Harrison441b62c2008-03-03 16:08:34 -08003054 dbg("%s - time 3 %ld", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003055
3056 /* set the configuration to use #1 */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003057/* dbg("set_configuration 1"); */
3058/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003059 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303060 dbg(" FirmwareMajorVersion %d.%d.%d",
3061 edge_serial->product_info.FirmwareMajorVersion,
3062 edge_serial->product_info.FirmwareMinorVersion,
3063 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Alan Cox03f0dbf2008-07-22 11:16:34 +01003065 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 * as the structures aren't created yet. */
3067
3068 /* set up our port private structures */
3069 for (i = 0; i < serial->num_ports; ++i) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003070 edge_port = kmalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003072 dev_err(&serial->dev->dev, "%s - Out of memory\n",
3073 __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07003074 for (j = 0; j < i; ++j) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003075 kfree(usb_get_serial_port_data(serial->port[j]));
3076 usb_set_serial_port_data(serial->port[j],
3077 NULL);
Chris Lundbfd5df32006-06-03 13:58:19 -07003078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 usb_set_serial_data(serial, NULL);
3080 kfree(edge_serial);
3081 return -ENOMEM;
3082 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003083 memset(edge_port, 0, sizeof(struct edgeport_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 spin_lock_init(&edge_port->ep_lock);
3085 edge_port->port = serial->port[i];
3086 usb_set_serial_port_data(serial->port[i], edge_port);
3087 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003088
3089 response = 0;
3090
3091 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003092 /* EPIC thing, set up our interrupt polling now and our read
3093 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003094 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01003095 for (i = 0; i < serial->interface->altsetting[0]
3096 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003097 struct usb_endpoint_descriptor *endpoint;
3098 int buffer_size;
3099
Alan Cox03f0dbf2008-07-22 11:16:34 +01003100 endpoint = &serial->interface->altsetting[0].
3101 endpoint[i].desc;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003102 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003103 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003104 (usb_endpoint_is_int_in(endpoint))) {
3105 /* we found a interrupt in endpoint */
3106 dbg("found interrupt in");
3107
3108 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003109 edge_serial->interrupt_read_urb =
3110 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003111 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003112 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003113 return -ENOMEM;
3114 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003115 edge_serial->interrupt_in_buffer =
3116 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003117 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003118 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003119 usb_free_urb(edge_serial->interrupt_read_urb);
3120 return -ENOMEM;
3121 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003122 edge_serial->interrupt_in_endpoint =
3123 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003124
3125 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003126 usb_fill_int_urb(
3127 edge_serial->interrupt_read_urb,
3128 dev,
3129 usb_rcvintpipe(dev,
3130 endpoint->bEndpointAddress),
3131 edge_serial->interrupt_in_buffer,
3132 buffer_size,
3133 edge_interrupt_callback,
3134 edge_serial,
3135 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003136
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003137 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003138 }
3139
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003140 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003141 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003142 /* we found a bulk in endpoint */
3143 dbg("found bulk in");
3144
3145 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003146 edge_serial->read_urb =
3147 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003148 if (!edge_serial->read_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003149 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003150 return -ENOMEM;
3151 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003152 edge_serial->bulk_in_buffer =
3153 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003154 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003155 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003156 usb_free_urb(edge_serial->read_urb);
3157 return -ENOMEM;
3158 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003159 edge_serial->bulk_in_endpoint =
3160 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003161
3162 /* set up our bulk in urb */
3163 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003164 usb_rcvbulkpipe(dev,
3165 endpoint->bEndpointAddress),
3166 edge_serial->bulk_in_buffer,
3167 le16_to_cpu(endpoint->wMaxPacketSize),
3168 edge_bulk_in_callback,
3169 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003170 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003171 }
3172
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003173 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003174 (usb_endpoint_is_bulk_out(endpoint))) {
3175 /* we found a bulk out endpoint */
3176 dbg("found bulk out");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003177 edge_serial->bulk_out_endpoint =
3178 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003179 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003180 }
3181 }
3182
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003183 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003184 dev_err(&dev->dev, "Error - the proper endpoints "
3185 "were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003186 return -ENODEV;
3187 }
3188
3189 /* start interrupt read for this edgeport this interrupt will
3190 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003191 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3192 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003193 if (response)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003194 dev_err(&dev->dev,
3195 "%s - Error %d submitting control urb\n",
3196 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003197 }
3198 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
3201
3202/****************************************************************************
3203 * edge_shutdown
3204 * This function is called whenever the device is removed from the usb bus.
3205 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01003206static void edge_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003208 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 int i;
3210
Harvey Harrison441b62c2008-03-03 16:08:34 -08003211 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213 /* stop reads and writes on all ports */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003214 for (i = 0; i < serial->num_ports; ++i) {
3215 kfree(usb_get_serial_port_data(serial->port[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 usb_set_serial_port_data(serial->port[i], NULL);
3217 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003218 /* free up our endpoint stuff */
3219 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003220 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003221 usb_free_urb(edge_serial->interrupt_read_urb);
3222 kfree(edge_serial->interrupt_in_buffer);
3223
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003224 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003225 usb_free_urb(edge_serial->read_urb);
3226 kfree(edge_serial->bulk_in_buffer);
3227 }
3228
3229 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 usb_set_serial_data(serial, NULL);
3231}
3232
3233
3234/****************************************************************************
3235 * edgeport_init
3236 * This is called by the module subsystem, or on startup to initialize us
3237 ****************************************************************************/
3238static int __init edgeport_init(void)
3239{
3240 int retval;
3241
3242 retval = usb_serial_register(&edgeport_2port_device);
3243 if (retval)
3244 goto failed_2port_device_register;
3245 retval = usb_serial_register(&edgeport_4port_device);
3246 if (retval)
3247 goto failed_4port_device_register;
3248 retval = usb_serial_register(&edgeport_8port_device);
3249 if (retval)
3250 goto failed_8port_device_register;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003251 retval = usb_serial_register(&epic_device);
3252 if (retval)
3253 goto failed_epic_device_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 retval = usb_register(&io_driver);
Alan Cox03f0dbf2008-07-22 11:16:34 +01003255 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 goto failed_usb_register;
Oliver Neukum96c706e2007-03-15 15:27:17 +01003257 atomic_set(&CmdUrbs, 0);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07003258 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3259 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 return 0;
3261
3262failed_usb_register:
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003263 usb_serial_deregister(&epic_device);
3264failed_epic_device_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 usb_serial_deregister(&edgeport_8port_device);
3266failed_8port_device_register:
3267 usb_serial_deregister(&edgeport_4port_device);
3268failed_4port_device_register:
3269 usb_serial_deregister(&edgeport_2port_device);
3270failed_2port_device_register:
3271 return retval;
3272}
3273
3274
3275/****************************************************************************
3276 * edgeport_exit
3277 * Called when the driver is about to be unloaded.
3278 ****************************************************************************/
3279static void __exit edgeport_exit (void)
3280{
Alan Cox03f0dbf2008-07-22 11:16:34 +01003281 usb_deregister(&io_driver);
3282 usb_serial_deregister(&edgeport_2port_device);
3283 usb_serial_deregister(&edgeport_4port_device);
3284 usb_serial_deregister(&edgeport_8port_device);
3285 usb_serial_deregister(&epic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
3288module_init(edgeport_init);
3289module_exit(edgeport_exit);
3290
3291/* Module information */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003292MODULE_AUTHOR(DRIVER_AUTHOR);
3293MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303295MODULE_FIRMWARE("edgeport/boot.fw");
3296MODULE_FIRMWARE("edgeport/boot2.fw");
3297MODULE_FIRMWARE("edgeport/down.fw");
3298MODULE_FIRMWARE("edgeport/down2.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300module_param(debug, bool, S_IRUGO | S_IWUSR);
3301MODULE_PARM_DESC(debug, "Debug enabled or not");
3302
3303module_param(low_latency, bool, S_IRUGO | S_IWUSR);
3304MODULE_PARM_DESC(low_latency, "Low latency enabled or not");