blob: 09456002bac017837a61eb810fea63d6cae29566 [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
Alan Cox03f0dbf2008-07-22 11:16:34 +0100196static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198
199/* local function prototypes */
200
201/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100202static void edge_interrupt_callback(struct urb *urb);
203static void edge_bulk_in_callback(struct urb *urb);
204static void edge_bulk_out_data_callback(struct urb *urb);
205static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700208static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100209static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100210static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
211 const unsigned char *buf, int count);
212static int edge_write_room(struct tty_struct *tty);
213static int edge_chars_in_buffer(struct tty_struct *tty);
214static void edge_throttle(struct tty_struct *tty);
215static void edge_unthrottle(struct tty_struct *tty);
216static void edge_set_termios(struct tty_struct *tty,
217 struct usb_serial_port *port,
218 struct ktermios *old_termios);
219static int edge_ioctl(struct tty_struct *tty, struct file *file,
220 unsigned int cmd, unsigned long arg);
221static void edge_break(struct tty_struct *tty, int break_state);
222static int edge_tiocmget(struct tty_struct *tty, struct file *file);
223static int edge_tiocmset(struct tty_struct *tty, struct file *file,
224 unsigned int set, unsigned int clear);
225static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400226static void edge_disconnect(struct usb_serial *serial);
227static void edge_release(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229#include "io_tables.h" /* all of the devices that this driver supports */
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alan Cox03f0dbf2008-07-22 11:16:34 +0100233static void process_rcvd_data(struct edgeport_serial *edge_serial,
234 unsigned char *buffer, __u16 bufferLength);
235static void process_rcvd_status(struct edgeport_serial *edge_serial,
236 __u8 byte2, __u8 byte3);
237static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
238 unsigned char *data, int length);
239static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
240static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
241 __u8 lsr, __u8 data);
242static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
243 __u8 param);
244static int calc_baud_rate_divisor(int baud_rate, int *divisor);
245static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
246 int baudRate);
247static void change_port_settings(struct tty_struct *tty,
248 struct edgeport_port *edge_port,
249 struct ktermios *old_termios);
250static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
251 __u8 regNum, __u8 regValue);
252static int write_cmd_usb(struct edgeport_port *edge_port,
253 unsigned char *buffer, int writeLength);
254static void send_more_port_data(struct edgeport_serial *edge_serial,
255 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Alan Cox03f0dbf2008-07-22 11:16:34 +0100257static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, const __u8 *data);
259static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, __u8 *data);
261static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, const __u8 *data);
263static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
264static void get_boot_desc(struct edgeport_serial *edge_serial);
265static void load_application_firmware(struct edgeport_serial *edge_serial);
266
267static void unicode_to_ascii(char *string, int buflen,
268 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270
Alan Cox03f0dbf2008-07-22 11:16:34 +0100271/* ************************************************************************ */
272/* ************************************************************************ */
273/* ************************************************************************ */
274/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/************************************************************************
277 * *
278 * update_edgeport_E2PROM() Compare current versions of *
279 * Boot ROM and Manufacture *
280 * Descriptors with versions *
281 * embedded in this driver *
282 * *
283 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100284static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 __u32 BootCurVer;
287 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530288 __u8 BootMajorVersion;
289 __u8 BootMinorVersion;
290 __u16 BootBuildNumber;
291 __u32 Bootaddr;
292 const struct ihex_binrec *rec;
293 const struct firmware *fw;
294 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int response;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100298 case EDGE_DOWNLOAD_FILE_I930:
299 fw_name = "edgeport/boot.fw";
300 break;
301 case EDGE_DOWNLOAD_FILE_80251:
302 fw_name = "edgeport/boot2.fw";
303 break;
304 default:
305 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530308 response = request_ihex_firmware(&fw, fw_name,
309 &edge_serial->serial->dev->dev);
310 if (response) {
311 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
312 fw_name, response);
313 return;
314 }
315
316 rec = (const struct ihex_binrec *)fw->data;
317 BootMajorVersion = rec->data[0];
318 BootMinorVersion = rec->data[1];
319 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
320
Alan Cox03f0dbf2008-07-22 11:16:34 +0100321 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
323 (edge_serial->boot_descriptor.MinorVersion << 16) +
324 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
325
326 BootNewVer = (BootMajorVersion << 24) +
327 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530328 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 dbg("Current Boot Image version %d.%d.%d",
331 edge_serial->boot_descriptor.MajorVersion,
332 edge_serial->boot_descriptor.MinorVersion,
333 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
334
335
336 if (BootNewVer > BootCurVer) {
337 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
338 edge_serial->boot_descriptor.MajorVersion,
339 edge_serial->boot_descriptor.MinorVersion,
340 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530341 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 dbg("Downloading new Boot Image");
344
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530345 for (rec = ihex_next_binrec(rec); rec;
346 rec = ihex_next_binrec(rec)) {
347 Bootaddr = be32_to_cpu(rec->addr);
348 response = rom_write(edge_serial->serial,
349 Bootaddr >> 16,
350 Bootaddr & 0xFFFF,
351 be16_to_cpu(rec->len),
352 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530354 dev_err(&edge_serial->serial->dev->dev,
355 "rom_write failed (%x, %x, %d)\n",
356 Bootaddr >> 16, Bootaddr & 0xFFFF,
357 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 }
360 }
361 } else {
362 dbg("Boot Image -- already up to date");
363 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530364 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367
368/************************************************************************
369 * *
370 * Get string descriptor from device *
371 * *
372 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100373static int get_string(struct usb_device *dev, int Id, char *string, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200375 struct usb_string_descriptor *StringDesc = NULL;
376 struct usb_string_descriptor *pStringDesc = NULL;
377 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alan Cox03f0dbf2008-07-22 11:16:34 +0100379 dbg("%s - USB String ID = %d", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200381 StringDesc = kmalloc(sizeof(*StringDesc), GFP_KERNEL);
382 if (!StringDesc)
383 goto free;
384 if (usb_get_descriptor(dev, USB_DT_STRING, Id, StringDesc, sizeof(*StringDesc)) <= 0)
385 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200387 pStringDesc = kmalloc(StringDesc->bLength, GFP_KERNEL);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100388 if (!pStringDesc)
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200389 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200391 if (usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc->bLength) <= 0)
392 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200394 unicode_to_ascii(string, buflen, pStringDesc->wData, pStringDesc->bLength/2);
395 ret = strlen(string);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800396 dbg("%s - USB String %s", __func__, string);
Dan Carpenterd0ef90b2009-12-31 17:42:55 +0200397free:
398 kfree(StringDesc);
399 kfree(pStringDesc);
400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403
404#if 0
405/************************************************************************
406 *
407 * Get string descriptor from device
408 *
409 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100410static int get_string_desc(struct usb_device *dev, int Id,
411 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct usb_string_descriptor StringDesc;
414 struct usb_string_descriptor *pStringDesc;
415
Alan Cox03f0dbf2008-07-22 11:16:34 +0100416 dbg("%s - USB String ID = %d", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alan Cox03f0dbf2008-07-22 11:16:34 +0100418 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
419 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Alan Cox03f0dbf2008-07-22 11:16:34 +0100422 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
423 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Alan Cox03f0dbf2008-07-22 11:16:34 +0100426 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
427 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree(pStringDesc);
429 return -1;
430 }
431
432 *pRetDesc = pStringDesc;
433 return 0;
434}
435#endif
436
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800437static void dump_product_info(struct edgeport_product_info *product_info)
438{
Alan Cox03f0dbf2008-07-22 11:16:34 +0100439 /* Dump Product Info structure */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800440 dbg("**Product Information:");
Alan Cox03f0dbf2008-07-22 11:16:34 +0100441 dbg(" ProductId %x", product_info->ProductId);
442 dbg(" NumPorts %d", product_info->NumPorts);
443 dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800444 dbg(" IsServer %d", product_info->IsServer);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100445 dbg(" IsRS232 %d", product_info->IsRS232);
446 dbg(" IsRS422 %d", product_info->IsRS422);
447 dbg(" IsRS485 %d", product_info->IsRS485);
448 dbg(" RomSize %d", product_info->RomSize);
449 dbg(" RamSize %d", product_info->RamSize);
450 dbg(" CpuRev %x", product_info->CpuRev);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800451 dbg(" BoardRev %x", product_info->BoardRev);
452 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
453 product_info->BootMinorVersion,
454 le16_to_cpu(product_info->BootBuildNumber));
Alan Cox03f0dbf2008-07-22 11:16:34 +0100455 dbg(" FirmwareMajorVersion %d.%d.%d",
456 product_info->FirmwareMajorVersion,
457 product_info->FirmwareMinorVersion,
458 le16_to_cpu(product_info->FirmwareBuildNumber));
459 dbg(" ManufactureDescDate %d/%d/%d",
460 product_info->ManufactureDescDate[0],
461 product_info->ManufactureDescDate[1],
462 product_info->ManufactureDescDate[2]+1900);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800463 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
464 dbg(" EpicVer %d", product_info->EpicVer);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static void get_product_info(struct edgeport_serial *edge_serial)
468{
469 struct edgeport_product_info *product_info = &edge_serial->product_info;
470
Alan Cox03f0dbf2008-07-22 11:16:34 +0100471 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Alan Cox03f0dbf2008-07-22 11:16:34 +0100473 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
474 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
475 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Alan Cox03f0dbf2008-07-22 11:16:34 +0100477 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
478 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
479 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
480 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alan Cox03f0dbf2008-07-22 11:16:34 +0100482 product_info->BootMajorVersion =
483 edge_serial->boot_descriptor.MajorVersion;
484 product_info->BootMinorVersion =
485 edge_serial->boot_descriptor.MinorVersion;
486 product_info->BootBuildNumber =
487 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alan Cox03f0dbf2008-07-22 11:16:34 +0100489 memcpy(product_info->ManufactureDescDate,
490 edge_serial->manuf_descriptor.DescDate,
491 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alan Cox03f0dbf2008-07-22 11:16:34 +0100493 /* check if this is 2nd generation hardware */
494 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
495 & ION_DEVICE_ID_80251_NETCHIP)
496 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
497 else
498 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
499
500 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100502 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
503 case ION_DEVICE_ID_EDGEPORT_4T:
504 case ION_DEVICE_ID_EDGEPORT_4:
505 case ION_DEVICE_ID_EDGEPORT_2:
506 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
507 case ION_DEVICE_ID_EDGEPORT_8:
508 case ION_DEVICE_ID_EDGEPORT_421:
509 case ION_DEVICE_ID_EDGEPORT_21:
510 case ION_DEVICE_ID_EDGEPORT_2_DIN:
511 case ION_DEVICE_ID_EDGEPORT_4_DIN:
512 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
513 product_info->IsRS232 = 1;
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alan Cox03f0dbf2008-07-22 11:16:34 +0100516 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
517 product_info->IsRS422 = 1;
518 product_info->IsRS485 = 1;
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Alan Cox03f0dbf2008-07-22 11:16:34 +0100521 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
522 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
523 product_info->IsRS422 = 1;
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800527 dump_product_info(product_info);
528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800530static int get_epic_descriptor(struct edgeport_serial *ep)
531{
532 int result;
533 struct usb_serial *serial = ep->serial;
534 struct edgeport_product_info *product_info = &ep->product_info;
535 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
536 struct edge_compatibility_bits *bits;
537
538 ep->is_epic = 0;
539 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
540 USB_REQUEST_ION_GET_EPIC_DESC,
541 0xC0, 0x00, 0x00,
542 &ep->epic_descriptor,
543 sizeof(struct edge_compatibility_descriptor),
544 300);
545
Harvey Harrison441b62c2008-03-03 16:08:34 -0800546 dbg("%s result = %d", __func__, result);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800547
548 if (result > 0) {
549 ep->is_epic = 1;
550 memset(product_info, 0, sizeof(struct edgeport_product_info));
551
Alan Cox03f0dbf2008-07-22 11:16:34 +0100552 product_info->NumPorts = epic->NumPorts;
553 product_info->ProdInfoVer = 0;
554 product_info->FirmwareMajorVersion = epic->MajorVersion;
555 product_info->FirmwareMinorVersion = epic->MinorVersion;
556 product_info->FirmwareBuildNumber = epic->BuildNumber;
557 product_info->iDownloadFile = epic->iDownloadFile;
558 product_info->EpicVer = epic->EpicVer;
559 product_info->Epic = epic->Supports;
560 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800561 dump_product_info(product_info);
562
563 bits = &ep->epic_descriptor.Supports;
564 dbg("**EPIC descriptor:");
565 dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
Alan Cox03f0dbf2008-07-22 11:16:34 +0100566 dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
567 dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
568 dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
569 dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
570 dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
571 dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
572 dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
573 dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
574 dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
575 dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
576 dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
577 dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800578 }
579
580 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583
584/************************************************************************/
585/************************************************************************/
586/* U S B C A L L B A C K F U N C T I O N S */
587/* U S B C A L L B A C K F U N C T I O N S */
588/************************************************************************/
589/************************************************************************/
590
591/*****************************************************************************
592 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100593 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * interrupt endpoint.
595 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100596static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Ming Leicdc97792008-02-24 18:41:47 +0800598 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct edgeport_port *edge_port;
600 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100601 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned char *data = urb->transfer_buffer;
603 int length = urb->actual_length;
604 int bytes_avail;
605 int position;
606 int txCredits;
607 int portNumber;
608 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700609 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Harvey Harrison441b62c2008-03-03 16:08:34 -0800611 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700613 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 case 0:
615 /* success */
616 break;
617 case -ECONNRESET:
618 case -ENOENT:
619 case -ESHUTDOWN:
620 /* this urb is terminated, clean up */
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700621 dbg("%s - urb shutting down with status: %d",
Alan Cox03f0dbf2008-07-22 11:16:34 +0100622 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return;
624 default:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100625 dbg("%s - nonzero urb status received: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto exit;
627 }
628
Alan Cox03f0dbf2008-07-22 11:16:34 +0100629 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (length) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100631 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
632 __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (length > 1) {
635 bytes_avail = data[0] | (data[1] << 8);
636 if (bytes_avail) {
637 spin_lock(&edge_serial->es_lock);
638 edge_serial->rxBytesAvail += bytes_avail;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800639 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 -0700640
641 if (edge_serial->rxBytesAvail > 0 &&
642 !edge_serial->read_in_progress) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800643 dbg("%s - posting a read", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100644 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Alan Cox03f0dbf2008-07-22 11:16:34 +0100646 /* we have pending bytes on the
647 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 edge_serial->read_urb->dev = edge_serial->serial->dev;
649 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
650 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800651 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 +0100652 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
655 spin_unlock(&edge_serial->es_lock);
656 }
657 }
658 /* grab the txcredits for the ports if available */
659 position = 2;
660 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100661 while ((position < length) &&
662 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 txCredits = data[position] | (data[position+1] << 8);
664 if (txCredits) {
665 port = edge_serial->serial->port[portNumber];
666 edge_port = usb_get_serial_port_data(port);
667 if (edge_port->open) {
668 spin_lock(&edge_port->ep_lock);
669 edge_port->txCredits += txCredits;
670 spin_unlock(&edge_port->ep_lock);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100671 dbg("%s - txcredits for port%d = %d",
672 __func__, portNumber,
673 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alan Cox03f0dbf2008-07-22 11:16:34 +0100675 /* tell the tty driver that something
676 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100677 tty = tty_port_tty_get(
678 &edge_port->port->port);
679 if (tty) {
680 tty_wakeup(tty);
681 tty_kref_put(tty);
682 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100683 /* Since we have more credit, check
684 if more data can be sent */
685 send_more_port_data(edge_serial,
686 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 }
689 position += 2;
690 ++portNumber;
691 }
692 }
693
694exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100695 result = usb_submit_urb(urb, GFP_ATOMIC);
696 if (result)
697 dev_err(&urb->dev->dev,
698 "%s - Error %d submitting control urb\n",
699 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702
703/*****************************************************************************
704 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100705 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * bulk in endpoint.
707 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100708static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Ming Leicdc97792008-02-24 18:41:47 +0800710 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700712 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700714 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Harvey Harrison441b62c2008-03-03 16:08:34 -0800716 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700718 if (status) {
719 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800720 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100721 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return;
723 }
724
725 if (urb->actual_length == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800726 dbg("%s - read bulk callback with no data", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100727 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return;
729 }
730
731 raw_data_length = urb->actual_length;
732
Alan Cox03f0dbf2008-07-22 11:16:34 +0100733 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
734 __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 spin_lock(&edge_serial->es_lock);
737
738 /* decrement our rxBytes available by the number that we just got */
739 edge_serial->rxBytesAvail -= raw_data_length;
740
Alan Cox03f0dbf2008-07-22 11:16:34 +0100741 dbg("%s - Received = %d, rxBytesAvail %d", __func__,
742 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Alan Cox03f0dbf2008-07-22 11:16:34 +0100744 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* check to see if there's any more data for us to read */
747 if (edge_serial->rxBytesAvail > 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800748 dbg("%s - posting a read", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 edge_serial->read_urb->dev = edge_serial->serial->dev;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700750 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
751 if (retval) {
752 dev_err(&urb->dev->dev,
753 "%s - usb_submit_urb(read bulk) failed, "
Harvey Harrison441b62c2008-03-03 16:08:34 -0800754 "retval = %d\n", __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100755 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100758 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 spin_unlock(&edge_serial->es_lock);
762}
763
764
765/*****************************************************************************
766 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100767 * this is the callback function for when we have finished sending
768 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100770static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Ming Leicdc97792008-02-24 18:41:47 +0800772 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700774 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Harvey Harrison441b62c2008-03-03 16:08:34 -0800776 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700778 if (status) {
779 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Alan Cox4a90f092008-10-13 10:39:46 +0100783 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100786 /* let the tty driver wakeup if it has a special
787 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 tty_wakeup(tty);
789 }
Alan Cox4a90f092008-10-13 10:39:46 +0100790 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Alan Cox03f0dbf2008-07-22 11:16:34 +0100792 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100793 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Alan Cox03f0dbf2008-07-22 11:16:34 +0100795 /* Check if more data needs to be sent */
796 send_more_port_data((struct edgeport_serial *)
797 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800
801/*****************************************************************************
802 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100803 * this is the callback function for when we have finished sending a
804 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100806static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Ming Leicdc97792008-02-24 18:41:47 +0800808 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct tty_struct *tty;
810 int status = urb->status;
811
Harvey Harrison441b62c2008-03-03 16:08:34 -0800812 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Oliver Neukum96c706e2007-03-15 15:27:17 +0100814 atomic_dec(&CmdUrbs);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100815 dbg("%s - FREE URB %p (outstanding %d)", __func__,
816 urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818
819 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700820 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100823 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (status) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100826 dbg("%s - nonzero write bulk status received: %d",
827 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return;
829 }
830
831 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100832 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* tell the tty driver that something has changed */
835 if (tty && edge_port->open)
836 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100837 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100840 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 wake_up(&edge_port->wait_command);
842}
843
844
845/*****************************************************************************
846 * Driver tty interface functions
847 *****************************************************************************/
848
849/*****************************************************************************
850 * SerialOpen
851 * this function is called by the tty driver when a port is opened
852 * If successful, we return 0
853 * Otherwise we return a negative error number.
854 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700855static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
858 struct usb_serial *serial;
859 struct edgeport_serial *edge_serial;
860 int response;
861
Harvey Harrison441b62c2008-03-03 16:08:34 -0800862 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 if (edge_port == NULL)
865 return -ENODEV;
866
Alan Cox03f0dbf2008-07-22 11:16:34 +0100867 /* see if we've set up our endpoint info yet (can't set it up
868 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 serial = port->serial;
870 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100871 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (edge_serial->interrupt_in_buffer == NULL) {
874 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100877 edge_serial->interrupt_in_buffer =
878 port0->interrupt_in_buffer;
879 edge_serial->interrupt_in_endpoint =
880 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
882 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100883 edge_serial->bulk_in_endpoint =
884 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100886 edge_serial->bulk_out_endpoint =
887 port0->bulk_out_endpointAddress;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* set up our interrupt urb */
890 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100891 serial->dev,
892 usb_rcvintpipe(serial->dev,
893 port0->interrupt_in_endpointAddress),
894 port0->interrupt_in_buffer,
895 edge_serial->interrupt_read_urb->transfer_buffer_length,
896 edge_interrupt_callback, edge_serial,
897 edge_serial->interrupt_read_urb->interval);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* set up our bulk in urb */
900 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100901 usb_rcvbulkpipe(serial->dev,
902 port0->bulk_in_endpointAddress),
903 port0->bulk_in_buffer,
904 edge_serial->read_urb->transfer_buffer_length,
905 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100906 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100909 * this interrupt will continue as long
910 * as the edgeport is connected */
911 response = usb_submit_urb(edge_serial->interrupt_read_urb,
912 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (response) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100914 dev_err(&port->dev,
915 "%s - Error %d submitting control urb\n",
916 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /* initialize our wait queues */
921 init_waitqueue_head(&edge_port->wait_open);
922 init_waitqueue_head(&edge_port->wait_chase);
923 init_waitqueue_head(&edge_port->delta_msr_wait);
924 init_waitqueue_head(&edge_port->wait_command);
925
926 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100927 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100930 edge_port->txCredits = 0; /* Can't send any data yet */
931 /* Must always set this bit to enable ints! */
932 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100933 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100936 edge_port->openPending = true;
937 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100938 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (response < 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100941 dev_err(&port->dev, "%s - error sending open port command\n",
942 __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100943 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -ENODEV;
945 }
946
947 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100948 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
949 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100951 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* open timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800953 dbg("%s - open timedout", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100954 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENODEV;
956 }
957
958 /* create the txfifo */
959 edge_port->txfifo.head = 0;
960 edge_port->txfifo.tail = 0;
961 edge_port->txfifo.count = 0;
962 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100963 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (!edge_port->txfifo.fifo) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800966 dbg("%s - no memory", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100967 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return -ENOMEM;
969 }
970
971 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100972 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100973 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!edge_port->write_urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800976 dbg("%s - no memory", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100977 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -ENOMEM;
979 }
980
Alan Cox03f0dbf2008-07-22 11:16:34 +0100981 dbg("%s(%d) - Initialize TX fifo to %d bytes",
982 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Harvey Harrison441b62c2008-03-03 16:08:34 -0800984 dbg("%s exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 return 0;
987}
988
989
990/************************************************************************
991 *
992 * block_until_chase_response
993 *
994 * This function will block the close until one of the following:
995 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800996 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
998 *
999 ************************************************************************/
1000static void block_until_chase_response(struct edgeport_port *edge_port)
1001{
1002 DEFINE_WAIT(wait);
1003 __u16 lastCredits;
1004 int timeout = 1*HZ;
1005 int loop = 10;
1006
1007 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001008 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 lastCredits = edge_port->txCredits;
1010
Alan Cox03f0dbf2008-07-22 11:16:34 +01001011 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001012 if (!edge_port->chaseResponsePending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001013 dbg("%s - Got Chase Response", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Alan Cox03f0dbf2008-07-22 11:16:34 +01001015 /* did we get all of our credit back? */
1016 if (edge_port->txCredits == edge_port->maxTxCredits) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001017 dbg("%s - Got all credits", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
1019 }
1020 }
1021
Alan Cox03f0dbf2008-07-22 11:16:34 +01001022 /* Block the thread for a while */
1023 prepare_to_wait(&edge_port->wait_chase, &wait,
1024 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 schedule_timeout(timeout);
1026 finish_wait(&edge_port->wait_chase, &wait);
1027
1028 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001029 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 loop--;
1031 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001032 edge_port->chaseResponsePending = false;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001033 dbg("%s - Chase TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return;
1035 }
1036 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001037 /* Reset timeout value back to 10 seconds */
1038 dbg("%s - Last %d, Current %d", __func__,
1039 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 loop = 10;
1041 }
1042 }
1043}
1044
1045
1046/************************************************************************
1047 *
1048 * block_until_tx_empty
1049 *
1050 * This function will block the close until one of the following:
1051 * 1. TX count are 0
1052 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001053 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *
1055 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001056static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 DEFINE_WAIT(wait);
1059 struct TxFifo *fifo = &edge_port->txfifo;
1060 __u32 lastCount;
1061 int timeout = HZ/10;
1062 int loop = 30;
1063
1064 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001065 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 lastCount = fifo->count;
1067
Alan Cox03f0dbf2008-07-22 11:16:34 +01001068 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (lastCount == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001070 dbg("%s - TX Buffer Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return;
1072 }
1073
Alan Cox03f0dbf2008-07-22 11:16:34 +01001074 /* Block the thread for a while */
1075 prepare_to_wait(&edge_port->wait_chase, &wait,
1076 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 schedule_timeout(timeout);
1078 finish_wait(&edge_port->wait_chase, &wait);
1079
Harvey Harrison441b62c2008-03-03 16:08:34 -08001080 dbg("%s wait", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001083 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 loop--;
1085 if (loop == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001086 dbg("%s - TIMEOUT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return;
1088 }
1089 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001090 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 loop = 30;
1092 }
1093 }
1094}
1095
1096
1097/*****************************************************************************
1098 * edge_close
1099 * this function is called by the tty driver when a port is closed
1100 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001101static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 struct edgeport_serial *edge_serial;
1104 struct edgeport_port *edge_port;
1105 int status;
1106
Harvey Harrison441b62c2008-03-03 16:08:34 -08001107 dbg("%s - port %d", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 edge_serial = usb_get_serial_data(port->serial);
1110 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001111 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001113
1114 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 block_until_tx_empty(edge_port);
1116
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001117 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001119 if ((!edge_serial->is_epic) ||
1120 ((edge_serial->is_epic) &&
1121 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1122 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001123 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Harvey Harrison441b62c2008-03-03 16:08:34 -08001125 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001126 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1127 if (status == 0)
1128 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001129 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001130 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001131 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001134 if ((!edge_serial->is_epic) ||
1135 ((edge_serial->is_epic) &&
1136 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1137 /* close the port */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001138 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001139 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Alan Cox03f0dbf2008-07-22 11:16:34 +01001142 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001143 edge_port->closePending = false;
1144 edge_port->open = false;
1145 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001147 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001150 /* if this urb had a transfer buffer already
1151 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001152 kfree(edge_port->write_urb->transfer_buffer);
1153 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 edge_port->write_urb = NULL;
1155 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001156 kfree(edge_port->txfifo.fifo);
1157 edge_port->txfifo.fifo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 dbg("%s exited", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/*****************************************************************************
1163 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001164 * this function is called by the tty driver when data should be written
1165 * to the port.
1166 * If successful, we return the number of bytes written, otherwise we
1167 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001169static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1170 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1173 struct TxFifo *fifo;
1174 int copySize;
1175 int bytesleft;
1176 int firsthalf;
1177 int secondhalf;
1178 unsigned long flags;
1179
Harvey Harrison441b62c2008-03-03 16:08:34 -08001180 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (edge_port == NULL)
1183 return -ENODEV;
1184
Alan Cox03f0dbf2008-07-22 11:16:34 +01001185 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 fifo = &edge_port->txfifo;
1187
1188 spin_lock_irqsave(&edge_port->ep_lock, flags);
1189
Alan Cox03f0dbf2008-07-22 11:16:34 +01001190 /* calculate number of bytes to put in fifo */
1191 copySize = min((unsigned int)count,
1192 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Alan Cox03f0dbf2008-07-22 11:16:34 +01001194 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
1195 __func__, port->number, count,
1196 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Alan Cox03f0dbf2008-07-22 11:16:34 +01001198 /* catch writes of 0 bytes which the tty driver likes to give us,
1199 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (copySize == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001201 dbg("%s - copySize = Zero", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto finish_write;
1203 }
1204
Alan Cox03f0dbf2008-07-22 11:16:34 +01001205 /* queue the data
1206 * since we can never overflow the buffer we do not have to check for a
1207 * full condition
1208 *
1209 * the copy is done is two parts -- first fill to the end of the buffer
1210 * then copy the reset from the start of the buffer
1211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001213 firsthalf = min(bytesleft, copySize);
1214 dbg("%s - copy %d bytes of %d into fifo ", __func__,
1215 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* now copy our data */
1218 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001219 usb_serial_debug_data(debug, &port->dev, __func__,
1220 firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Alan Cox03f0dbf2008-07-22 11:16:34 +01001222 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 fifo->head += firsthalf;
1224 fifo->count += firsthalf;
1225
Alan Cox03f0dbf2008-07-22 11:16:34 +01001226 /* wrap the index */
1227 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 secondhalf = copySize-firsthalf;
1231
1232 if (secondhalf) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001233 dbg("%s - copy rest of data %d", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001235 usb_serial_debug_data(debug, &port->dev, __func__,
1236 secondhalf, &fifo->fifo[fifo->head]);
1237 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 fifo->count += secondhalf;
1239 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001240 /* No need to check for wrap since we can not get to end of
1241 * the fifo in this part
1242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245finish_write:
1246 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1247
Alan Cox03f0dbf2008-07-22 11:16:34 +01001248 send_more_port_data((struct edgeport_serial *)
1249 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Alan Cox03f0dbf2008-07-22 11:16:34 +01001251 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
1252 copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Alan Cox03f0dbf2008-07-22 11:16:34 +01001254 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257
1258/************************************************************************
1259 *
1260 * send_more_port_data()
1261 *
1262 * This routine attempts to write additional UART transmit data
1263 * to a port over the USB bulk pipe. It is called (1) when new
1264 * data has been written to a port's TxBuffer from higher layers
1265 * (2) when the peripheral sends us additional TxCredits indicating
1266 * that it can accept more Tx data for a given port; and (3) when
1267 * a bulk write completes successfully and we want to see if we
1268 * can transmit more.
1269 *
1270 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001271static void send_more_port_data(struct edgeport_serial *edge_serial,
1272 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct TxFifo *fifo = &edge_port->txfifo;
1275 struct urb *urb;
1276 unsigned char *buffer;
1277 int status;
1278 int count;
1279 int bytesleft;
1280 int firsthalf;
1281 int secondhalf;
1282 unsigned long flags;
1283
Harvey Harrison441b62c2008-03-03 16:08:34 -08001284 dbg("%s(%d)", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 spin_lock_irqsave(&edge_port->ep_lock, flags);
1287
1288 if (edge_port->write_in_progress ||
1289 !edge_port->open ||
1290 (fifo->count == 0)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001291 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
1292 __func__, edge_port->port->number,
1293 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto exit_send;
1295 }
1296
Alan Cox03f0dbf2008-07-22 11:16:34 +01001297 /* since the amount of data in the fifo will always fit into the
1298 * edgeport buffer we do not need to check the write length
1299 *
1300 * Do we have enough credits for this port to make it worthwhile
1301 * to bother queueing a write. If it's too small, say a few bytes,
1302 * it's better to wait for more credits so we can do a larger write.
1303 */
1304 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1305 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
1306 __func__, edge_port->port->number, fifo->count,
1307 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 goto exit_send;
1309 }
1310
Alan Cox03f0dbf2008-07-22 11:16:34 +01001311 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001312 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alan Cox03f0dbf2008-07-22 11:16:34 +01001314 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 urb = edge_port->write_urb;
1316
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001317 /* make sure transfer buffer is freed */
1318 kfree(urb->transfer_buffer);
1319 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alan Cox03f0dbf2008-07-22 11:16:34 +01001321 /* build the data header for the buffer and port that we are about
1322 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001324 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (buffer == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001326 dev_err(&edge_port->port->dev,
1327 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001328 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto exit_send;
1330 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001331 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1332 - edge_port->port->serial->minor, count);
1333 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1334 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /* now copy our data */
1337 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001338 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1340 fifo->tail += firsthalf;
1341 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001342 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 secondhalf = count-firsthalf;
1346 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001347 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1348 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 fifo->tail += secondhalf;
1350 fifo->count -= secondhalf;
1351 }
1352
1353 if (count)
Alan Cox03f0dbf2008-07-22 11:16:34 +01001354 usb_serial_debug_data(debug, &edge_port->port->dev,
1355 __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001358 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1359 usb_sndbulkpipe(edge_serial->serial->dev,
1360 edge_serial->bulk_out_endpoint),
1361 buffer, count+2,
1362 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* decrement the number of credits we have by the number we just sent */
1365 edge_port->txCredits -= count;
1366 edge_port->icount.tx += count;
1367
1368 urb->dev = edge_serial->serial->dev;
1369 status = usb_submit_urb(urb, GFP_ATOMIC);
1370 if (status) {
1371 /* something went wrong */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001372 dev_err(&edge_port->port->dev,
1373 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1374 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001375 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* revert the credits as something bad happened. */
1378 edge_port->txCredits += count;
1379 edge_port->icount.tx -= count;
1380 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001381 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
1382 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384exit_send:
1385 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1386}
1387
1388
1389/*****************************************************************************
1390 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001391 * this function is called by the tty driver when it wants to know how
1392 * many bytes of data we can accept for a specific port. If successful,
1393 * we return the amount of room that we have for this port (the txCredits)
1394 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001396static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Alan Cox95da3102008-07-22 11:09:07 +01001398 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1400 int room;
1401 unsigned long flags;
1402
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001406 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001407 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Harvey Harrison441b62c2008-03-03 16:08:34 -08001410 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001413 dbg("%s - port not opened", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Alan Cox03f0dbf2008-07-22 11:16:34 +01001417 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 spin_lock_irqsave(&edge_port->ep_lock, flags);
1419 room = edge_port->txCredits - edge_port->txfifo.count;
1420 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1421
Harvey Harrison441b62c2008-03-03 16:08:34 -08001422 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return room;
1424}
1425
1426
1427/*****************************************************************************
1428 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001429 * this function is called by the tty driver when it wants to know how
1430 * many bytes of data we currently have outstanding in the port (data that
1431 * has been written, but hasn't made it out the port yet)
1432 * If successful, we return the number of bytes left to be written in the
1433 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * Otherwise we return a negative error number.
1435 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001436static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Alan Cox95da3102008-07-22 11:09:07 +01001438 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1440 int num_chars;
1441 unsigned long flags;
1442
Harvey Harrison441b62c2008-03-03 16:08:34 -08001443 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001446 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001447 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001451 dbg("%s - port not opened", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454
1455 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001456 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1457 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1459 if (num_chars) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001460 dbg("%s(port %d) - returns %d", __func__,
1461 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
1464 return num_chars;
1465}
1466
1467
1468/*****************************************************************************
1469 * SerialThrottle
1470 * this function is called by the tty driver when it wants to stop the data
1471 * being read from the port.
1472 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001473static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Alan Cox95da3102008-07-22 11:09:07 +01001475 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int status;
1478
Harvey Harrison441b62c2008-03-03 16:08:34 -08001479 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (edge_port == NULL)
1482 return;
1483
1484 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001485 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return;
1487 }
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /* if we are implementing XON/XOFF, send the stop character */
1490 if (I_IXOFF(tty)) {
1491 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001492 status = edge_write(tty, port, &stop_char, 1);
1493 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 /* if we are implementing RTS/CTS, toggle that line */
1498 if (tty->termios->c_cflag & CRTSCTS) {
1499 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001500 status = send_cmd_write_uart_register(edge_port, MCR,
1501 edge_port->shadowMCR);
1502 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
1506 return;
1507}
1508
1509
1510/*****************************************************************************
1511 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001512 * this function is called by the tty driver when it wants to resume the
1513 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001515static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Alan Cox95da3102008-07-22 11:09:07 +01001517 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 int status;
1520
Harvey Harrison441b62c2008-03-03 16:08:34 -08001521 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 if (edge_port == NULL)
1524 return;
1525
1526 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001527 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return;
1529 }
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* if we are implementing XON/XOFF, send the start character */
1532 if (I_IXOFF(tty)) {
1533 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001534 status = edge_write(tty, port, &start_char, 1);
1535 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* if we are implementing RTS/CTS, toggle that line */
1539 if (tty->termios->c_cflag & CRTSCTS) {
1540 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001541 send_cmd_write_uart_register(edge_port, MCR,
1542 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546
1547/*****************************************************************************
1548 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001549 * this function is called by the tty driver when it wants to change
1550 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001552static void edge_set_termios(struct tty_struct *tty,
1553 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 unsigned int cflag;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 cflag = tty->termios->c_cflag;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001559 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001561 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox6ce073b2007-10-18 01:24:25 -07001562 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Harvey Harrison441b62c2008-03-03 16:08:34 -08001564 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (edge_port == NULL)
1567 return;
1568
1569 if (!edge_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001570 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return;
1572 }
1573
1574 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001575 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578
1579/*****************************************************************************
1580 * get_lsr_info - get line status register info
1581 *
1582 * Purpose: Let user call ioctl() to get info when the UART physically
1583 * is emptied. On bus types like RS485, the transmitter must
1584 * release the bus after transmitting. This must be done when
1585 * the transmit shift register is empty, not be done when the
1586 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001587 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001589static int get_lsr_info(struct edgeport_port *edge_port,
1590 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 unsigned int result = 0;
1593 unsigned long flags;
1594
1595 spin_lock_irqsave(&edge_port->ep_lock, flags);
1596 if (edge_port->maxTxCredits == edge_port->txCredits &&
1597 edge_port->txfifo.count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001598 dbg("%s -- Empty", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 result = TIOCSER_TEMT;
1600 }
1601 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1602
1603 if (copy_to_user(value, &result, sizeof(int)))
1604 return -EFAULT;
1605 return 0;
1606}
1607
Alan Cox03f0dbf2008-07-22 11:16:34 +01001608static int edge_tiocmset(struct tty_struct *tty, struct file *file,
1609 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Alan Cox95da3102008-07-22 11:09:07 +01001611 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1613 unsigned int mcr;
1614
Harvey Harrison441b62c2008-03-03 16:08:34 -08001615 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 mcr = edge_port->shadowMCR;
1618 if (set & TIOCM_RTS)
1619 mcr |= MCR_RTS;
1620 if (set & TIOCM_DTR)
1621 mcr |= MCR_DTR;
1622 if (set & TIOCM_LOOP)
1623 mcr |= MCR_LOOPBACK;
1624
1625 if (clear & TIOCM_RTS)
1626 mcr &= ~MCR_RTS;
1627 if (clear & TIOCM_DTR)
1628 mcr &= ~MCR_DTR;
1629 if (clear & TIOCM_LOOP)
1630 mcr &= ~MCR_LOOPBACK;
1631
1632 edge_port->shadowMCR = mcr;
1633
1634 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1635
1636 return 0;
1637}
1638
Alan Cox95da3102008-07-22 11:09:07 +01001639static int edge_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Alan Cox95da3102008-07-22 11:09:07 +01001641 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1643 unsigned int result = 0;
1644 unsigned int msr;
1645 unsigned int mcr;
1646
Harvey Harrison441b62c2008-03-03 16:08:34 -08001647 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 msr = edge_port->shadowMSR;
1650 mcr = edge_port->shadowMCR;
1651 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1652 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1653 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1654 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1655 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1656 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1657
1658
Harvey Harrison441b62c2008-03-03 16:08:34 -08001659 dbg("%s -- %x", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 return result;
1662}
1663
Alan Cox03f0dbf2008-07-22 11:16:34 +01001664static int get_serial_info(struct edgeport_port *edge_port,
1665 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct serial_struct tmp;
1668
1669 if (!retinfo)
1670 return -EFAULT;
1671
1672 memset(&tmp, 0, sizeof(tmp));
1673
1674 tmp.type = PORT_16550A;
1675 tmp.line = edge_port->port->serial->minor;
1676 tmp.port = edge_port->port->number;
1677 tmp.irq = 0;
1678 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1679 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1680 tmp.baud_base = 9600;
1681 tmp.close_delay = 5*HZ;
1682 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1685 return -EFAULT;
1686 return 0;
1687}
1688
1689
1690
1691/*****************************************************************************
1692 * SerialIoctl
1693 * this function handles any ioctl calls to the driver
1694 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001695static int edge_ioctl(struct tty_struct *tty, struct file *file,
1696 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Alan Cox95da3102008-07-22 11:09:07 +01001698 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 DEFINE_WAIT(wait);
1700 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1701 struct async_icount cnow;
1702 struct async_icount cprev;
1703 struct serial_icounter_struct icount;
1704
Harvey Harrison441b62c2008-03-03 16:08:34 -08001705 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001708 case TIOCSERGETLSR:
1709 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1710 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Alan Cox03f0dbf2008-07-22 11:16:34 +01001712 case TIOCGSERIAL:
1713 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1714 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Alan Cox03f0dbf2008-07-22 11:16:34 +01001716 case TIOCMIWAIT:
1717 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1718 cprev = edge_port->icount;
1719 while (1) {
1720 prepare_to_wait(&edge_port->delta_msr_wait,
1721 &wait, TASK_INTERRUPTIBLE);
1722 schedule();
1723 finish_wait(&edge_port->delta_msr_wait, &wait);
1724 /* see if a signal did it */
1725 if (signal_pending(current))
1726 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001728 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1729 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1730 return -EIO; /* no change => error */
1731 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1732 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1733 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1734 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1735 return 0;
1736 }
1737 cprev = cnow;
1738 }
1739 /* NOTREACHED */
1740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Alan Cox03f0dbf2008-07-22 11:16:34 +01001742 case TIOCGICOUNT:
1743 cnow = edge_port->icount;
1744 memset(&icount, 0, sizeof(icount));
1745 icount.cts = cnow.cts;
1746 icount.dsr = cnow.dsr;
1747 icount.rng = cnow.rng;
1748 icount.dcd = cnow.dcd;
1749 icount.rx = cnow.rx;
1750 icount.tx = cnow.tx;
1751 icount.frame = cnow.frame;
1752 icount.overrun = cnow.overrun;
1753 icount.parity = cnow.parity;
1754 icount.brk = cnow.brk;
1755 icount.buf_overrun = cnow.buf_overrun;
1756
1757 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
1758 __func__, port->number, icount.rx, icount.tx);
1759 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1760 return -EFAULT;
1761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -ENOIOCTLCMD;
1764}
1765
1766
1767/*****************************************************************************
1768 * SerialBreak
1769 * this function sends a break to the port
1770 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001771static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Alan Cox95da3102008-07-22 11:09:07 +01001773 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001775 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int status;
1777
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001778 if ((!edge_serial->is_epic) ||
1779 ((edge_serial->is_epic) &&
1780 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1781 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001782 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Harvey Harrison441b62c2008-03-03 16:08:34 -08001784 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001785 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001786 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001787 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001788 block_until_chase_response(edge_port);
1789 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001790 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001794 if ((!edge_serial->is_epic) ||
1795 ((edge_serial->is_epic) &&
1796 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1797 if (break_state == -1) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001798 dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001799 status = send_iosp_ext_cmd(edge_port,
1800 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001801 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001802 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001803 status = send_iosp_ext_cmd(edge_port,
1804 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001805 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001806 if (status)
1807 dbg("%s - error sending break set/clear command.",
1808 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
1811 return;
1812}
1813
1814
1815/*****************************************************************************
1816 * process_rcvd_data
1817 * this function handles the data received on the bulk in pipe.
1818 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001819static void process_rcvd_data(struct edgeport_serial *edge_serial,
1820 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 struct usb_serial_port *port;
1823 struct edgeport_port *edge_port;
1824 struct tty_struct *tty;
1825 __u16 lastBufferLength;
1826 __u16 rxLen;
1827
Harvey Harrison441b62c2008-03-03 16:08:34 -08001828 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 lastBufferLength = bufferLength + 1;
1831
1832 while (bufferLength > 0) {
1833 /* failsafe incase we get a message that we don't understand */
1834 if (lastBufferLength == bufferLength) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001835 dbg("%s - stuck in loop, exiting it.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837 }
1838 lastBufferLength = bufferLength;
1839
1840 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001841 case EXPECT_HDR1:
1842 edge_serial->rxHeader1 = *buffer;
1843 ++buffer;
1844 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Alan Cox03f0dbf2008-07-22 11:16:34 +01001846 if (bufferLength == 0) {
1847 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001849 }
1850 /* otherwise, drop on through */
1851 case EXPECT_HDR2:
1852 edge_serial->rxHeader2 = *buffer;
1853 ++buffer;
1854 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Alan Cox03f0dbf2008-07-22 11:16:34 +01001856 dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
1857 edge_serial->rxHeader1, edge_serial->rxHeader2);
1858 /* Process depending on whether this header is
1859 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Alan Cox03f0dbf2008-07-22 11:16:34 +01001861 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1862 /* Decode this status header and go to
1863 * EXPECT_HDR1 (if we can process the status
1864 * with only 2 bytes), or go to EXPECT_HDR3 to
1865 * get the third byte. */
1866 edge_serial->rxPort =
1867 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1868 edge_serial->rxStatusCode =
1869 IOSP_GET_STATUS_CODE(
1870 edge_serial->rxHeader1);
1871
1872 if (!IOSP_STATUS_IS_2BYTE(
1873 edge_serial->rxStatusCode)) {
1874 /* This status needs additional bytes.
1875 * Save what we have and then wait for
1876 * more data.
1877 */
1878 edge_serial->rxStatusParam
1879 = edge_serial->rxHeader2;
1880 edge_serial->rxState = EXPECT_HDR3;
1881 break;
1882 }
1883 /* We have all the header bytes, process the
1884 status now */
1885 process_rcvd_status(edge_serial,
1886 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 edge_serial->rxState = EXPECT_HDR1;
1888 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001889 } else {
1890 edge_serial->rxPort =
1891 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1892 edge_serial->rxBytesRemaining =
1893 IOSP_GET_HDR_DATA_LEN(
1894 edge_serial->rxHeader1,
1895 edge_serial->rxHeader2);
1896 dbg("%s - Data for Port %u Len %u",
1897 __func__,
1898 edge_serial->rxPort,
1899 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Alan Cox03f0dbf2008-07-22 11:16:34 +01001901 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1902 * ASSERT(DevExt->RxBytesRemaining <
1903 * IOSP_MAX_DATA_LENGTH);
1904 */
1905
1906 if (bufferLength == 0) {
1907 edge_serial->rxState = EXPECT_DATA;
1908 break;
1909 }
1910 /* Else, drop through */
1911 }
1912 case EXPECT_DATA: /* Expect data */
1913 if (bufferLength < edge_serial->rxBytesRemaining) {
1914 rxLen = bufferLength;
1915 /* Expect data to start next buffer */
1916 edge_serial->rxState = EXPECT_DATA;
1917 } else {
1918 /* BufLen >= RxBytesRemaining */
1919 rxLen = edge_serial->rxBytesRemaining;
1920 /* Start another header next time */
1921 edge_serial->rxState = EXPECT_HDR1;
1922 }
1923
1924 bufferLength -= rxLen;
1925 edge_serial->rxBytesRemaining -= rxLen;
1926
1927 /* spit this data back into the tty driver if this
1928 port is open */
1929 if (rxLen) {
1930 port = edge_serial->serial->port[
1931 edge_serial->rxPort];
1932 edge_port = usb_get_serial_port_data(port);
1933 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001934 tty = tty_port_tty_get(
1935 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001936 if (tty) {
1937 dbg("%s - Sending %d bytes to TTY for port %d",
1938 __func__, rxLen, edge_serial->rxPort);
1939 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001940 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001941 }
1942 edge_port->icount.rx += rxLen;
1943 }
1944 buffer += rxLen;
1945 }
1946 break;
1947
1948 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1949 edge_serial->rxHeader3 = *buffer;
1950 ++buffer;
1951 --bufferLength;
1952
1953 /* We have all the header bytes, process the
1954 status now */
1955 process_rcvd_status(edge_serial,
1956 edge_serial->rxStatusParam,
1957 edge_serial->rxHeader3);
1958 edge_serial->rxState = EXPECT_HDR1;
1959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 }
1962}
1963
1964
1965/*****************************************************************************
1966 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001967 * this function handles the any status messages received on the
1968 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001970static void process_rcvd_status(struct edgeport_serial *edge_serial,
1971 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 struct usb_serial_port *port;
1974 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001975 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 __u8 code = edge_serial->rxStatusCode;
1977
1978 /* switch the port pointer to the one being currently talked about */
1979 port = edge_serial->serial->port[edge_serial->rxPort];
1980 edge_port = usb_get_serial_port_data(port);
1981 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001982 dev_err(&edge_serial->serial->dev->dev,
1983 "%s - edge_port == NULL for port %d\n",
1984 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return;
1986 }
1987
Harvey Harrison441b62c2008-03-03 16:08:34 -08001988 dbg("%s - port %d", __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (code == IOSP_EXT_STATUS) {
1991 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001992 case IOSP_EXT_STATUS_CHASE_RSP:
1993 /* we want to do EXT status regardless of port
1994 * open/closed */
1995 dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
1996 __func__, edge_serial->rxPort, byte3);
1997 /* Currently, the only EXT_STATUS is Chase, so process
1998 * here instead of one more call to one more subroutine
1999 * If/when more EXT_STATUS, there'll be more work to do
2000 * Also, we currently clear flag and close the port
2001 * regardless of content of above's Byte3.
2002 * We could choose to do something else when Byte3 says
2003 * Timeout on Chase from Edgeport, like wait longer in
2004 * block_until_chase_response, but for now we don't.
2005 */
2006 edge_port->chaseResponsePending = false;
2007 wake_up(&edge_port->wait_chase);
2008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alan Cox03f0dbf2008-07-22 11:16:34 +01002010 case IOSP_EXT_STATUS_RX_CHECK_RSP:
2011 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3);
2012 /* Port->RxCheckRsp = true; */
2013 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 }
2016
2017 if (code == IOSP_STATUS_OPEN_RSP) {
2018 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
2019 edge_port->maxTxCredits = edge_port->txCredits;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002020 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 +01002021 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Alan Cox03f0dbf2008-07-22 11:16:34 +01002023 /* send the current line settings to the port so we are
2024 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01002025 tty = tty_port_tty_get(&edge_port->port->port);
2026 if (tty) {
2027 change_port_settings(tty,
2028 edge_port, tty->termios);
2029 tty_kref_put(tty);
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002033 edge_port->openPending = false;
2034 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 wake_up(&edge_port->wait_open);
2036 return;
2037 }
2038
Alan Cox03f0dbf2008-07-22 11:16:34 +01002039 /* If port is closed, silently discard all rcvd status. We can
2040 * have cases where buffered status is received AFTER the close
2041 * port command is sent to the Edgeport.
2042 */
2043 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002047 /* Not currently sent by Edgeport */
2048 case IOSP_STATUS_LSR:
2049 dbg("%s - Port %u LSR Status = %02x",
2050 __func__, edge_serial->rxPort, byte2);
2051 handle_new_lsr(edge_port, false, byte2, 0);
2052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Alan Cox03f0dbf2008-07-22 11:16:34 +01002054 case IOSP_STATUS_LSR_DATA:
2055 dbg("%s - Port %u LSR Status = %02x, Data = %02x",
2056 __func__, edge_serial->rxPort, byte2, byte3);
2057 /* byte2 is LSR Register */
2058 /* byte3 is broken data byte */
2059 handle_new_lsr(edge_port, true, byte2, byte3);
2060 break;
2061 /*
2062 * case IOSP_EXT_4_STATUS:
2063 * dbg("%s - Port %u LSR Status = %02x Data = %02x",
2064 * __func__, edge_serial->rxPort, byte2, byte3);
2065 * break;
2066 */
2067 case IOSP_STATUS_MSR:
2068 dbg("%s - Port %u MSR Status = %02x",
2069 __func__, edge_serial->rxPort, byte2);
2070 /*
2071 * Process this new modem status and generate appropriate
2072 * events, etc, based on the new status. This routine
2073 * also saves the MSR in Port->ShadowMsr.
2074 */
2075 handle_new_msr(edge_port, byte2);
2076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078 default:
2079 dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
2080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return;
2083}
2084
2085
2086/*****************************************************************************
2087 * edge_tty_recv
2088 * this function passes data on to the tty flip buffer
2089 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002090static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2091 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 int cnt;
2094
2095 do {
Alan Cox33f0f882006-01-09 20:54:13 -08002096 cnt = tty_buffer_request_room(tty, length);
2097 if (cnt < length) {
2098 dev_err(dev, "%s - dropping data, %d bytes lost\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002099 __func__, length - cnt);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002100 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Alan Cox33f0f882006-01-09 20:54:13 -08002103 tty_insert_flip_string(tty, data, cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 data += cnt;
2105 length -= cnt;
2106 } while (length > 0);
2107
2108 tty_flip_buffer_push(tty);
2109}
2110
2111
2112/*****************************************************************************
2113 * handle_new_msr
2114 * this function handles any change to the msr register for a port.
2115 *****************************************************************************/
2116static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2117{
2118 struct async_icount *icount;
2119
Harvey Harrison441b62c2008-03-03 16:08:34 -08002120 dbg("%s %02x", __func__, newMsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Alan Cox03f0dbf2008-07-22 11:16:34 +01002122 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2123 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 icount = &edge_port->icount;
2125
2126 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002127 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002131 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002133 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 wake_up_interruptible(&edge_port->delta_msr_wait);
2136 }
2137
2138 /* Save the new modem status */
2139 edge_port->shadowMSR = newMsr & 0xf0;
2140
2141 return;
2142}
2143
2144
2145/*****************************************************************************
2146 * handle_new_lsr
2147 * this function handles any change to the lsr register for a port.
2148 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002149static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2150 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002152 __u8 newLsr = (__u8) (lsr & (__u8)
2153 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2154 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Harvey Harrison441b62c2008-03-03 16:08:34 -08002156 dbg("%s - %02x", __func__, newLsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 edge_port->shadowLSR = lsr;
2159
2160 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161 /*
2162 * Parity and Framing errors only count if they
2163 * occur exclusive of a break being
2164 * received.
2165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2167 }
2168
2169 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002170 if (lsrData) {
2171 struct tty_struct *tty =
2172 tty_port_tty_get(&edge_port->port->port);
2173 if (tty) {
2174 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2175 tty_kref_put(tty);
2176 }
2177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* update input line counters */
2179 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002180 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002182 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002184 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002186 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 return;
2190}
2191
2192
2193/****************************************************************************
2194 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002195 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * given address.
2197 * If successful returns the number of bytes written, otherwise it returns
2198 * a negative error number of the problem.
2199 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002200static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2201 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 int result;
2204 __u16 current_length;
2205 unsigned char *transfer_buffer;
2206
Harvey Harrison441b62c2008-03-03 16:08:34 -08002207 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Alan Cox03f0dbf2008-07-22 11:16:34 +01002209 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002211 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2212 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return -ENOMEM;
2214 }
2215
2216 /* need to split these writes up into 64 byte chunks */
2217 result = 0;
2218 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002219 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002221 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002223
2224/* dbg("%s - writing %x, %x, %d", __func__,
2225 extAddr, addr, current_length); */
2226 memcpy(transfer_buffer, data, current_length);
2227 result = usb_control_msg(serial->dev,
2228 usb_sndctrlpipe(serial->dev, 0),
2229 USB_REQUEST_ION_WRITE_RAM,
2230 0x40, addr, extAddr, transfer_buffer,
2231 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (result < 0)
2233 break;
2234 length -= current_length;
2235 addr += current_length;
2236 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Alan Cox03f0dbf2008-07-22 11:16:34 +01002239 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return result;
2241}
2242
2243
2244/****************************************************************************
2245 * rom_write
2246 * writes a number of bytes to the Edgeport device's ROM starting at the
2247 * given address.
2248 * If successful returns the number of bytes written, otherwise it returns
2249 * a negative error number of the problem.
2250 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002251static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2252 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 int result;
2255 __u16 current_length;
2256 unsigned char *transfer_buffer;
2257
Alan Cox03f0dbf2008-07-22 11:16:34 +01002258/* dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Alan Cox03f0dbf2008-07-22 11:16:34 +01002260 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002262 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2263 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return -ENOMEM;
2265 }
2266
2267 /* need to split these writes up into 64 byte chunks */
2268 result = 0;
2269 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002270 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002272 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002274/* dbg("%s - writing %x, %x, %d", __func__,
2275 extAddr, addr, current_length); */
2276 memcpy(transfer_buffer, data, current_length);
2277 result = usb_control_msg(serial->dev,
2278 usb_sndctrlpipe(serial->dev, 0),
2279 USB_REQUEST_ION_WRITE_ROM, 0x40,
2280 addr, extAddr,
2281 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (result < 0)
2283 break;
2284 length -= current_length;
2285 addr += current_length;
2286 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Alan Cox03f0dbf2008-07-22 11:16:34 +01002289 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return result;
2291}
2292
2293
2294/****************************************************************************
2295 * rom_read
2296 * reads a number of bytes from the Edgeport device starting at the given
2297 * address.
2298 * If successful returns the number of bytes read, otherwise it returns
2299 * a negative error number of the problem.
2300 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002301static int rom_read(struct usb_serial *serial, __u16 extAddr,
2302 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 int result;
2305 __u16 current_length;
2306 unsigned char *transfer_buffer;
2307
Harvey Harrison441b62c2008-03-03 16:08:34 -08002308 dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Alan Cox03f0dbf2008-07-22 11:16:34 +01002310 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002312 dev_err(&serial->dev->dev,
2313 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return -ENOMEM;
2315 }
2316
2317 /* need to split these reads up into 64 byte chunks */
2318 result = 0;
2319 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002320 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002322 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002324/* dbg("%s - %x, %x, %d", __func__,
2325 extAddr, addr, current_length); */
2326 result = usb_control_msg(serial->dev,
2327 usb_rcvctrlpipe(serial->dev, 0),
2328 USB_REQUEST_ION_READ_ROM,
2329 0xC0, addr, extAddr, transfer_buffer,
2330 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (result < 0)
2332 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002333 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 length -= current_length;
2335 addr += current_length;
2336 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Alan Cox03f0dbf2008-07-22 11:16:34 +01002339 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return result;
2341}
2342
2343
2344/****************************************************************************
2345 * send_iosp_ext_cmd
2346 * Is used to send a IOSP message to the Edgeport device
2347 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002348static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2349 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 unsigned char *buffer;
2352 unsigned char *currentCommand;
2353 int length = 0;
2354 int status = 0;
2355
Harvey Harrison441b62c2008-03-03 16:08:34 -08002356 dbg("%s - %d, %d", __func__, command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Alan Cox03f0dbf2008-07-22 11:16:34 +01002358 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002360 dev_err(&edge_port->port->dev,
2361 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return -ENOMEM;
2363 }
2364
2365 currentCommand = buffer;
2366
Alan Cox03f0dbf2008-07-22 11:16:34 +01002367 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2368 edge_port->port->number - edge_port->port->serial->minor,
2369 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Alan Cox03f0dbf2008-07-22 11:16:34 +01002371 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (status) {
2373 /* something bad happened, let's free up the memory */
2374 kfree(buffer);
2375 }
2376
2377 return status;
2378}
2379
2380
2381/*****************************************************************************
2382 * write_cmd_usb
2383 * this function writes the given buffer out to the bulk write endpoint.
2384 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002385static int write_cmd_usb(struct edgeport_port *edge_port,
2386 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002388 struct edgeport_serial *edge_serial =
2389 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 int status = 0;
2391 struct urb *urb;
2392 int timeout;
2393
Alan Cox03f0dbf2008-07-22 11:16:34 +01002394 usb_serial_debug_data(debug, &edge_port->port->dev,
2395 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002398 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (!urb)
2400 return -ENOMEM;
2401
Oliver Neukum96c706e2007-03-15 15:27:17 +01002402 atomic_inc(&CmdUrbs);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002403 dbg("%s - ALLOCATE URB %p (outstanding %d)",
2404 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Alan Cox03f0dbf2008-07-22 11:16:34 +01002406 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2407 usb_sndbulkpipe(edge_serial->serial->dev,
2408 edge_serial->bulk_out_endpoint),
2409 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002411 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 status = usb_submit_urb(urb, GFP_ATOMIC);
2413
2414 if (status) {
2415 /* something went wrong */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002416 dev_err(&edge_port->port->dev,
2417 "%s - usb_submit_urb(write command) failed, status = %d\n",
2418 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 usb_kill_urb(urb);
2420 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002421 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return status;
2423 }
2424
Alan Cox03f0dbf2008-07-22 11:16:34 +01002425 /* wait for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 timeout = COMMAND_TIMEOUT;
2427#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002428 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002430 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /* command timed out */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002432 dbg("%s - command timed out", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 status = -EINVAL;
2434 }
2435#endif
2436 return status;
2437}
2438
2439
2440/*****************************************************************************
2441 * send_cmd_write_baud_rate
2442 * this function sends the proper command to change the baud rate of the
2443 * specified port.
2444 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002445static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2446 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002448 struct edgeport_serial *edge_serial =
2449 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned char *cmdBuffer;
2451 unsigned char *currCmd;
2452 int cmdLen = 0;
2453 int divisor;
2454 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002455 unsigned char number =
2456 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Adam Kropelinbc71e472007-07-29 11:03:29 -04002458 if (edge_serial->is_epic &&
2459 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002460 dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
2461 edge_port->port->number, baudRate);
2462 return 0;
2463 }
2464
Alan Cox03f0dbf2008-07-22 11:16:34 +01002465 dbg("%s - port = %d, baud = %d", __func__,
2466 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Alan Cox03f0dbf2008-07-22 11:16:34 +01002468 status = calc_baud_rate_divisor(baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (status) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002470 dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
2471 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return status;
2473 }
2474
Alan Cox03f0dbf2008-07-22 11:16:34 +01002475 /* Alloc memory for the string of commands. */
2476 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (!cmdBuffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002478 dev_err(&edge_port->port->dev,
2479 "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return -ENOMEM;
2481 }
2482 currCmd = cmdBuffer;
2483
Alan Cox03f0dbf2008-07-22 11:16:34 +01002484 /* Enable access to divisor latch */
2485 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Alan Cox03f0dbf2008-07-22 11:16:34 +01002487 /* Write the divisor itself */
2488 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2489 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Alan Cox03f0dbf2008-07-22 11:16:34 +01002491 /* Restore original value to disable access to divisor latch */
2492 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2493 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Alan Cox03f0dbf2008-07-22 11:16:34 +01002495 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (status) {
2497 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002498 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
2501 return status;
2502}
2503
2504
2505/*****************************************************************************
2506 * calc_baud_rate_divisor
2507 * this function calculates the proper baud rate divisor for the specified
2508 * baud rate.
2509 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002510static int calc_baud_rate_divisor(int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 int i;
2513 __u16 custom;
2514
2515
Harvey Harrison441b62c2008-03-03 16:08:34 -08002516 dbg("%s - %d", __func__, baudrate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Tobias Klauser52950ed2005-12-11 16:20:08 +01002518 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002519 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 *divisor = divisor_table[i].Divisor;
2521 return 0;
2522 }
2523 }
2524
Alan Cox03f0dbf2008-07-22 11:16:34 +01002525 /* We have tried all of the standard baud rates
2526 * lets try to calculate the divisor for this baud rate
2527 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002529 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 custom = (__u16)((230400L + baudrate/2) / baudrate);
2531
2532 *divisor = custom;
2533
Harvey Harrison441b62c2008-03-03 16:08:34 -08002534 dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return 0;
2536 }
2537
2538 return -1;
2539}
2540
2541
2542/*****************************************************************************
2543 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002544 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002546static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2547 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002549 struct edgeport_serial *edge_serial =
2550 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 unsigned char *cmdBuffer;
2552 unsigned char *currCmd;
2553 unsigned long cmdLen = 0;
2554 int status;
2555
Alan Cox03f0dbf2008-07-22 11:16:34 +01002556 dbg("%s - write to %s register 0x%02x",
2557 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Adam Kropelinbc71e472007-07-29 11:03:29 -04002559 if (edge_serial->is_epic &&
2560 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2561 regNum == MCR) {
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02002562 dbg("SendCmdWriteUartReg - Not writing to MCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002563 return 0;
2564 }
2565
Adam Kropelinbc71e472007-07-29 11:03:29 -04002566 if (edge_serial->is_epic &&
2567 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2568 regNum == LCR) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002569 dbg("SendCmdWriteUartReg - Not writing to LCR Register");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002570 return 0;
2571 }
2572
Alan Cox03f0dbf2008-07-22 11:16:34 +01002573 /* Alloc memory for the string of commands. */
2574 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2575 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 currCmd = cmdBuffer;
2579
Alan Cox03f0dbf2008-07-22 11:16:34 +01002580 /* Build a cmd in the buffer to write the given register */
2581 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2582 edge_port->port->number - edge_port->port->serial->minor,
2583 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2586 if (status) {
2587 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002588 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
2591 return status;
2592}
2593
2594
2595/*****************************************************************************
2596 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002597 * This routine is called to set the UART on the device to match the
2598 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002600
2601static void change_port_settings(struct tty_struct *tty,
2602 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002604 struct edgeport_serial *edge_serial =
2605 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 int baud;
2607 unsigned cflag;
2608 __u8 mask = 0xff;
2609 __u8 lData;
2610 __u8 lParity;
2611 __u8 lStop;
2612 __u8 rxFlow;
2613 __u8 txFlow;
2614 int status;
2615
Harvey Harrison441b62c2008-03-03 16:08:34 -08002616 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002618 if (!edge_port->open &&
2619 !edge_port->openPending) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002620 dbg("%s - port not opened", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return;
2622 }
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 cflag = tty->termios->c_cflag;
2625
2626 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002627 case CS5:
2628 lData = LCR_BITS_5; mask = 0x1f;
2629 dbg("%s - data bits = 5", __func__);
2630 break;
2631 case CS6:
2632 lData = LCR_BITS_6; mask = 0x3f;
2633 dbg("%s - data bits = 6", __func__);
2634 break;
2635 case CS7:
2636 lData = LCR_BITS_7; mask = 0x7f;
2637 dbg("%s - data bits = 7", __func__);
2638 break;
2639 default:
2640 case CS8:
2641 lData = LCR_BITS_8;
2642 dbg("%s - data bits = 8", __func__);
2643 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645
2646 lParity = LCR_PAR_NONE;
2647 if (cflag & PARENB) {
2648 if (cflag & CMSPAR) {
2649 if (cflag & PARODD) {
2650 lParity = LCR_PAR_MARK;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002651 dbg("%s - parity = mark", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 } else {
2653 lParity = LCR_PAR_SPACE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002654 dbg("%s - parity = space", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 } else if (cflag & PARODD) {
2657 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002658 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 } else {
2660 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002661 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002664 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666
2667 if (cflag & CSTOPB) {
2668 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002669 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } else {
2671 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002672 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674
2675 /* figure out the flow control settings */
2676 rxFlow = txFlow = 0x00;
2677 if (cflag & CRTSCTS) {
2678 rxFlow |= IOSP_RX_FLOW_RTS;
2679 txFlow |= IOSP_TX_FLOW_CTS;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002680 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002682 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684
Alan Cox03f0dbf2008-07-22 11:16:34 +01002685 /* if we are implementing XON/XOFF, set the start and stop character
2686 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (I_IXOFF(tty) || I_IXON(tty)) {
2688 unsigned char stop_char = STOP_CHAR(tty);
2689 unsigned char start_char = START_CHAR(tty);
2690
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002691 if ((!edge_serial->is_epic) ||
2692 ((edge_serial->is_epic) &&
2693 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002694 send_iosp_ext_cmd(edge_port,
2695 IOSP_CMD_SET_XON_CHAR, start_char);
2696 send_iosp_ext_cmd(edge_port,
2697 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 /* if we are implementing INBOUND XON/XOFF */
2701 if (I_IXOFF(tty)) {
2702 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002703 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2704 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002706 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
2708
2709 /* if we are implementing OUTBOUND XON/XOFF */
2710 if (I_IXON(tty)) {
2711 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002712 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2713 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002715 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717 }
2718
2719 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002720 if ((!edge_serial->is_epic) ||
2721 ((edge_serial->is_epic) &&
2722 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2723 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2724 if ((!edge_serial->is_epic) ||
2725 ((edge_serial->is_epic) &&
2726 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2727 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729
2730 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2731 edge_port->shadowLCR |= (lData | lParity | lStop);
2732
2733 edge_port->validDataMask = mask;
2734
2735 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002736 status = send_cmd_write_uart_register(edge_port, LCR,
2737 edge_port->shadowLCR);
2738 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 /* set up the MCR register and send it to the EdgePort */
2742 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002743 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002745
2746 status = send_cmd_write_uart_register(edge_port, MCR,
2747 edge_port->shadowMCR);
2748 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* Determine divisor based on baud rate */
2752 baud = tty_get_baud_rate(tty);
2753 if (!baud) {
2754 /* pick a default, any default... */
2755 baud = 9600;
2756 }
2757
Harvey Harrison441b62c2008-03-03 16:08:34 -08002758 dbg("%s - baud rate = %d", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002759 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002760 if (status == -1) {
2761 /* Speed change was not possible - put back the old speed */
2762 baud = tty_termios_baud_rate(old_termios);
2763 tty_encode_baud_rate(tty, baud, baud);
2764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return;
2766}
2767
2768
2769/****************************************************************************
2770 * unicode_to_ascii
2771 * Turns a string from Unicode into ASCII.
2772 * Doesn't do a good job with any characters that are outside the normal
2773 * ASCII range, but it's only for debugging...
2774 * NOTE: expects the unicode in LE format
2775 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002776static void unicode_to_ascii(char *string, int buflen,
2777 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 int i;
2780
Pete Zaitcevad933752006-05-22 21:49:44 -07002781 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002783 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Pete Zaitcevad933752006-05-22 21:49:44 -07002785 for (i = 0; i < unicode_size; i++) {
2786 if (i >= buflen)
2787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002789 }
2790 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793
2794/****************************************************************************
2795 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002796 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 * structure.
2798 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002799static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
2801 int response;
2802
2803 dbg("getting manufacturer descriptor");
2804
Alan Cox03f0dbf2008-07-22 11:16:34 +01002805 response = rom_read(edge_serial->serial,
2806 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2807 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2808 EDGE_MANUF_DESC_LEN,
2809 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Alan Cox03f0dbf2008-07-22 11:16:34 +01002811 if (response < 1)
2812 dev_err(&edge_serial->serial->dev->dev,
2813 "error in getting manufacturer descriptor\n");
2814 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 char string[30];
2816 dbg("**Manufacturer Descriptor");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002817 dbg(" RomSize: %dK",
2818 edge_serial->manuf_descriptor.RomSize);
2819 dbg(" RamSize: %dK",
2820 edge_serial->manuf_descriptor.RamSize);
2821 dbg(" CpuRev: %d",
2822 edge_serial->manuf_descriptor.CpuRev);
2823 dbg(" BoardRev: %d",
2824 edge_serial->manuf_descriptor.BoardRev);
2825 dbg(" NumPorts: %d",
2826 edge_serial->manuf_descriptor.NumPorts);
2827 dbg(" DescDate: %d/%d/%d",
2828 edge_serial->manuf_descriptor.DescDate[0],
2829 edge_serial->manuf_descriptor.DescDate[1],
2830 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002831 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002832 edge_serial->manuf_descriptor.SerialNumber,
2833 edge_serial->manuf_descriptor.SerNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 dbg(" SerialNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002835 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002836 edge_serial->manuf_descriptor.AssemblyNumber,
2837 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 dbg(" AssemblyNumber: %s", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002839 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002840 edge_serial->manuf_descriptor.OemAssyNumber,
2841 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 dbg(" OemAssyNumber: %s", string);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002843 dbg(" UartType: %d",
2844 edge_serial->manuf_descriptor.UartType);
2845 dbg(" IonPid: %d",
2846 edge_serial->manuf_descriptor.IonPid);
2847 dbg(" IonConfig: %d",
2848 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850}
2851
2852
2853/****************************************************************************
2854 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002855 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * structure.
2857 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002858static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 int response;
2861
2862 dbg("getting boot descriptor");
2863
Alan Cox03f0dbf2008-07-22 11:16:34 +01002864 response = rom_read(edge_serial->serial,
2865 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2866 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2867 EDGE_BOOT_DESC_LEN,
2868 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Alan Cox03f0dbf2008-07-22 11:16:34 +01002870 if (response < 1)
2871 dev_err(&edge_serial->serial->dev->dev,
2872 "error in getting boot descriptor\n");
2873 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 dbg("**Boot Descriptor:");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002875 dbg(" BootCodeLength: %d",
2876 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2877 dbg(" MajorVersion: %d",
2878 edge_serial->boot_descriptor.MajorVersion);
2879 dbg(" MinorVersion: %d",
2880 edge_serial->boot_descriptor.MinorVersion);
2881 dbg(" BuildNumber: %d",
2882 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2883 dbg(" Capabilities: 0x%x",
2884 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2885 dbg(" UConfig0: %d",
2886 edge_serial->boot_descriptor.UConfig0);
2887 dbg(" UConfig1: %d",
2888 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890}
2891
2892
2893/****************************************************************************
2894 * load_application_firmware
2895 * This is called to load the application firmware to the device
2896 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002897static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302899 const struct ihex_binrec *rec;
2900 const struct firmware *fw;
2901 const char *fw_name;
2902 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302904 __u32 Operaddr;
2905 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 switch (edge_serial->product_info.iDownloadFile) {
2908 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302909 fw_info = "downloading firmware version (930)";
2910 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 break;
2912
2913 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302914 fw_info = "downloading firmware version (80251)";
2915 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 break;
2917
2918 case EDGE_DOWNLOAD_FILE_NONE:
2919 dbg ("No download file specified, skipping download\n");
2920 return;
2921
2922 default:
2923 return;
2924 }
2925
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302926 response = request_ihex_firmware(&fw, fw_name,
2927 &edge_serial->serial->dev->dev);
2928 if (response) {
2929 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
2930 fw_name, response);
2931 return;
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302934 rec = (const struct ihex_binrec *)fw->data;
2935 build = (rec->data[2] << 8) | rec->data[3];
2936
2937 dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
2938
2939 edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
2940 edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
2941 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2942
2943 for (rec = ihex_next_binrec(rec); rec;
2944 rec = ihex_next_binrec(rec)) {
2945 Operaddr = be32_to_cpu(rec->addr);
2946 response = sram_write(edge_serial->serial,
2947 Operaddr >> 16,
2948 Operaddr & 0xFFFF,
2949 be16_to_cpu(rec->len),
2950 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302952 dev_err(&edge_serial->serial->dev->dev,
2953 "sram_write failed (%x, %x, %d)\n",
2954 Operaddr >> 16, Operaddr & 0xFFFF,
2955 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 break;
2957 }
2958 }
2959
2960 dbg("sending exec_dl_code");
2961 response = usb_control_msg (edge_serial->serial->dev,
2962 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2963 USB_REQUEST_ION_EXEC_DL_CODE,
2964 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2965
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302966 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return;
2968}
2969
2970
2971/****************************************************************************
2972 * edge_startup
2973 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002974static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 struct edgeport_serial *edge_serial;
2977 struct edgeport_port *edge_port;
2978 struct usb_device *dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002979 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002980 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002981 bool interrupt_in_found;
2982 bool bulk_in_found;
2983 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002984 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2985 EDGE_COMPATIBILITY_MASK1,
2986 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 dev = serial->dev;
2989
2990 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002991 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002993 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return -ENOMEM;
2995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 spin_lock_init(&edge_serial->es_lock);
2997 edge_serial->serial = serial;
2998 usb_set_serial_data(serial, edge_serial);
2999
3000 /* get the name for the device from the device */
Pete Zaitcevad933752006-05-22 21:49:44 -07003001 i = get_string(dev, dev->descriptor.iManufacturer,
3002 &edge_serial->name[0], MAX_NAME_LEN+1);
3003 edge_serial->name[i++] = ' ';
3004 get_string(dev, dev->descriptor.iProduct,
3005 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
3008
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003009 /* Read the epic descriptor */
3010 if (get_epic_descriptor(edge_serial) <= 0) {
3011 /* memcpy descriptor to Supports structures */
3012 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
3013 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003015 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003016 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003018 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003019 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003020
3021 get_product_info(edge_serial);
3022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 /* set the number of ports from the manufacturing description */
3025 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003026 if ((!edge_serial->is_epic) &&
3027 (edge_serial->product_info.NumPorts != serial->num_ports)) {
3028 dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
3029 "vs. core thinking we have %d ports, email "
Joe Perches898eb712007-10-18 03:06:30 -07003030 "greg@kroah.com this information.\n",
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003031 edge_serial->product_info.NumPorts,
3032 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Harvey Harrison441b62c2008-03-03 16:08:34 -08003035 dbg("%s - time 1 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003037 /* If not an EPiC device */
3038 if (!edge_serial->is_epic) {
3039 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003040 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Harvey Harrison441b62c2008-03-03 16:08:34 -08003042 dbg("%s - time 2 %ld", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003044 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003045 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Harvey Harrison441b62c2008-03-03 16:08:34 -08003047 dbg("%s - time 3 %ld", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003048
3049 /* set the configuration to use #1 */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003050/* dbg("set_configuration 1"); */
3051/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003052 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303053 dbg(" FirmwareMajorVersion %d.%d.%d",
3054 edge_serial->product_info.FirmwareMajorVersion,
3055 edge_serial->product_info.FirmwareMinorVersion,
3056 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Alan Cox03f0dbf2008-07-22 11:16:34 +01003058 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 * as the structures aren't created yet. */
3060
3061 /* set up our port private structures */
3062 for (i = 0; i < serial->num_ports; ++i) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003063 edge_port = kmalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003065 dev_err(&serial->dev->dev, "%s - Out of memory\n",
3066 __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07003067 for (j = 0; j < i; ++j) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003068 kfree(usb_get_serial_port_data(serial->port[j]));
3069 usb_set_serial_port_data(serial->port[j],
3070 NULL);
Chris Lundbfd5df32006-06-03 13:58:19 -07003071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 usb_set_serial_data(serial, NULL);
3073 kfree(edge_serial);
3074 return -ENOMEM;
3075 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003076 memset(edge_port, 0, sizeof(struct edgeport_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 spin_lock_init(&edge_port->ep_lock);
3078 edge_port->port = serial->port[i];
3079 usb_set_serial_port_data(serial->port[i], edge_port);
3080 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003081
3082 response = 0;
3083
3084 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01003085 /* EPIC thing, set up our interrupt polling now and our read
3086 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003087 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01003088 for (i = 0; i < serial->interface->altsetting[0]
3089 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003090 struct usb_endpoint_descriptor *endpoint;
3091 int buffer_size;
3092
Alan Cox03f0dbf2008-07-22 11:16:34 +01003093 endpoint = &serial->interface->altsetting[0].
3094 endpoint[i].desc;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003095 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003096 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003097 (usb_endpoint_is_int_in(endpoint))) {
3098 /* we found a interrupt in endpoint */
3099 dbg("found interrupt in");
3100
3101 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003102 edge_serial->interrupt_read_urb =
3103 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003104 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003105 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003106 return -ENOMEM;
3107 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003108 edge_serial->interrupt_in_buffer =
3109 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003110 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003111 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003112 usb_free_urb(edge_serial->interrupt_read_urb);
3113 return -ENOMEM;
3114 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003115 edge_serial->interrupt_in_endpoint =
3116 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003117
3118 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003119 usb_fill_int_urb(
3120 edge_serial->interrupt_read_urb,
3121 dev,
3122 usb_rcvintpipe(dev,
3123 endpoint->bEndpointAddress),
3124 edge_serial->interrupt_in_buffer,
3125 buffer_size,
3126 edge_interrupt_callback,
3127 edge_serial,
3128 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003129
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003130 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003131 }
3132
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003133 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003134 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003135 /* we found a bulk in endpoint */
3136 dbg("found bulk in");
3137
3138 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003139 edge_serial->read_urb =
3140 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003141 if (!edge_serial->read_urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003142 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003143 return -ENOMEM;
3144 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003145 edge_serial->bulk_in_buffer =
3146 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003147 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003148 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003149 usb_free_urb(edge_serial->read_urb);
3150 return -ENOMEM;
3151 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003152 edge_serial->bulk_in_endpoint =
3153 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003154
3155 /* set up our bulk in urb */
3156 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003157 usb_rcvbulkpipe(dev,
3158 endpoint->bEndpointAddress),
3159 edge_serial->bulk_in_buffer,
3160 le16_to_cpu(endpoint->wMaxPacketSize),
3161 edge_bulk_in_callback,
3162 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003163 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003164 }
3165
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003166 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003167 (usb_endpoint_is_bulk_out(endpoint))) {
3168 /* we found a bulk out endpoint */
3169 dbg("found bulk out");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003170 edge_serial->bulk_out_endpoint =
3171 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003172 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003173 }
3174 }
3175
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003176 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003177 dev_err(&dev->dev, "Error - the proper endpoints "
3178 "were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003179 return -ENODEV;
3180 }
3181
3182 /* start interrupt read for this edgeport this interrupt will
3183 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003184 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3185 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003186 if (response)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003187 dev_err(&dev->dev,
3188 "%s - Error %d submitting control urb\n",
3189 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003190 }
3191 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
3194
3195/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003196 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * This function is called whenever the device is removed from the usb bus.
3198 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003199static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003201 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Harvey Harrison441b62c2008-03-03 16:08:34 -08003203 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003206 /* free up our endpoint stuff */
3207 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003208 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003209 usb_free_urb(edge_serial->interrupt_read_urb);
3210 kfree(edge_serial->interrupt_in_buffer);
3211
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003212 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003213 usb_free_urb(edge_serial->read_urb);
3214 kfree(edge_serial->bulk_in_buffer);
3215 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003216}
3217
3218
3219/****************************************************************************
3220 * edge_release
3221 * This function is called when the device structure is deallocated.
3222 ****************************************************************************/
3223static void edge_release(struct usb_serial *serial)
3224{
3225 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3226 int i;
3227
3228 dbg("%s", __func__);
3229
3230 for (i = 0; i < serial->num_ports; ++i)
3231 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003232
3233 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236
3237/****************************************************************************
3238 * edgeport_init
3239 * This is called by the module subsystem, or on startup to initialize us
3240 ****************************************************************************/
3241static int __init edgeport_init(void)
3242{
3243 int retval;
3244
3245 retval = usb_serial_register(&edgeport_2port_device);
3246 if (retval)
3247 goto failed_2port_device_register;
3248 retval = usb_serial_register(&edgeport_4port_device);
3249 if (retval)
3250 goto failed_4port_device_register;
3251 retval = usb_serial_register(&edgeport_8port_device);
3252 if (retval)
3253 goto failed_8port_device_register;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003254 retval = usb_serial_register(&epic_device);
3255 if (retval)
3256 goto failed_epic_device_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 retval = usb_register(&io_driver);
Alan Cox03f0dbf2008-07-22 11:16:34 +01003258 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto failed_usb_register;
Oliver Neukum96c706e2007-03-15 15:27:17 +01003260 atomic_set(&CmdUrbs, 0);
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07003261 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3262 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return 0;
3264
3265failed_usb_register:
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003266 usb_serial_deregister(&epic_device);
3267failed_epic_device_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 usb_serial_deregister(&edgeport_8port_device);
3269failed_8port_device_register:
3270 usb_serial_deregister(&edgeport_4port_device);
3271failed_4port_device_register:
3272 usb_serial_deregister(&edgeport_2port_device);
3273failed_2port_device_register:
3274 return retval;
3275}
3276
3277
3278/****************************************************************************
3279 * edgeport_exit
3280 * Called when the driver is about to be unloaded.
3281 ****************************************************************************/
3282static void __exit edgeport_exit (void)
3283{
Alan Cox03f0dbf2008-07-22 11:16:34 +01003284 usb_deregister(&io_driver);
3285 usb_serial_deregister(&edgeport_2port_device);
3286 usb_serial_deregister(&edgeport_4port_device);
3287 usb_serial_deregister(&edgeport_8port_device);
3288 usb_serial_deregister(&epic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
3291module_init(edgeport_init);
3292module_exit(edgeport_exit);
3293
3294/* Module information */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003295MODULE_AUTHOR(DRIVER_AUTHOR);
3296MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303298MODULE_FIRMWARE("edgeport/boot.fw");
3299MODULE_FIRMWARE("edgeport/boot2.fw");
3300MODULE_FIRMWARE("edgeport/down.fw");
3301MODULE_FIRMWARE("edgeport/down2.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303module_param(debug, bool, S_IRUGO | S_IWUSR);
3304MODULE_PARM_DESC(debug, "Debug enabled or not");