[PATCH] TTY layer buffering revamp
[deliverable/linux.git] / drivers / usb / serial / io_edgeport.c
1 /*
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 *
30 */
31
32 #include <linux/config.h>
33 #include <linux/kernel.h>
34 #include <linux/jiffies.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
39 #include <linux/tty_driver.h>
40 #include <linux/tty_flip.h>
41 #include <linux/module.h>
42 #include <linux/spinlock.h>
43 #include <linux/serial.h>
44 #include <linux/ioctl.h>
45 #include <linux/wait.h>
46 #include <asm/uaccess.h>
47 #include <linux/usb.h>
48 #include "usb-serial.h"
49 #include "io_edgeport.h"
50 #include "io_ionsp.h" /* info for the iosp messages */
51 #include "io_16654.h" /* 16654 UART defines */
52
53 /*
54 * Version Information
55 */
56 #define DRIVER_VERSION "v2.7"
57 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
58 #define DRIVER_DESC "Edgeport USB Serial Driver"
59
60 /* First, the latest boot code - for first generation edgeports */
61 #define IMAGE_ARRAY_NAME BootCodeImage_GEN1
62 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN1
63 #include "io_fw_boot.h" /* the bootloader firmware to download to a device, if it needs it */
64
65 /* for second generation edgeports */
66 #define IMAGE_ARRAY_NAME BootCodeImage_GEN2
67 #define IMAGE_VERSION_NAME BootCodeImageVersion_GEN2
68 #include "io_fw_boot2.h" /* the bootloader firmware to download to a device, if it needs it */
69
70 /* Then finally the main run-time operational code - for first generation edgeports */
71 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN1
72 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN1
73 #include "io_fw_down.h" /* Define array OperationalCodeImage[] */
74
75 /* for second generation edgeports */
76 #define IMAGE_ARRAY_NAME OperationalCodeImage_GEN2
77 #define IMAGE_VERSION_NAME OperationalCodeImageVersion_GEN2
78 #include "io_fw_down2.h" /* Define array OperationalCodeImage[] */
79
80 #define MAX_NAME_LEN 64
81
82 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
83 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
84 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
85
86 /* receive port state */
87 enum RXSTATE {
88 EXPECT_HDR1 = 0, /* Expect header byte 1 */
89 EXPECT_HDR2 = 1, /* Expect header byte 2 */
90 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
91 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
92 };
93
94
95 /* Transmit Fifo
96 * This Transmit queue is an extension of the edgeport Rx buffer.
97 * The maximum amount of data buffered in both the edgeport
98 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
99 */
100 struct TxFifo {
101 unsigned int head; /* index to head pointer (write) */
102 unsigned int tail; /* index to tail pointer (read) */
103 unsigned int count; /* Bytes in queue */
104 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
105 unsigned char *fifo; /* allocated Buffer */
106 };
107
108 /* This structure holds all of the local port information */
109 struct edgeport_port {
110 __u16 txCredits; /* our current credits for this port */
111 __u16 maxTxCredits; /* the max size of the port */
112
113 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
114 struct urb *write_urb; /* write URB for this port */
115 char write_in_progress; /* TRUE while a write URB is outstanding */
116 spinlock_t ep_lock;
117
118 __u8 shadowLCR; /* last LCR value received */
119 __u8 shadowMCR; /* last MCR value received */
120 __u8 shadowMSR; /* last MSR value received */
121 __u8 shadowLSR; /* last LSR value received */
122 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
123 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
124 __u8 validDataMask;
125 __u32 baudRate;
126
127 char open;
128 char openPending;
129 char commandPending;
130 char closePending;
131 char chaseResponsePending;
132
133 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
134 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
135 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
136 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
137
138 struct async_icount icount;
139 struct usb_serial_port *port; /* loop back to the owner of this object */
140 };
141
142
143 /* This structure holds all of the individual device information */
144 struct edgeport_serial {
145 char name[MAX_NAME_LEN+1]; /* string name of this device */
146
147 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
148 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
149 struct edgeport_product_info product_info; /* Product Info */
150
151 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
152 unsigned char * interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
153 struct urb * interrupt_read_urb; /* our interrupt urb */
154
155 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
156 unsigned char * bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
157 struct urb * read_urb; /* our bulk read urb */
158 int read_in_progress;
159 spinlock_t es_lock;
160
161 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
162
163 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
164
165 enum RXSTATE rxState; /* the current state of the bulk receive processor */
166 __u8 rxHeader1; /* receive header byte 1 */
167 __u8 rxHeader2; /* receive header byte 2 */
168 __u8 rxHeader3; /* receive header byte 3 */
169 __u8 rxPort; /* the port that we are currently receiving data for */
170 __u8 rxStatusCode; /* the receive status code */
171 __u8 rxStatusParam; /* the receive status paramater */
172 __s16 rxBytesRemaining; /* the number of port bytes left to read */
173 struct usb_serial *serial; /* loop back to the owner of this object */
174 };
175
176 /* baud rate information */
177 struct divisor_table_entry {
178 __u32 BaudRate;
179 __u16 Divisor;
180 };
181
182 //
183 // Define table of divisors for Rev A EdgePort/4 hardware
184 // These assume a 3.6864MHz crystal, the standard /16, and
185 // MCR.7 = 0.
186 //
187 static const struct divisor_table_entry divisor_table[] = {
188 { 50, 4608},
189 { 75, 3072},
190 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
191 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
192 { 150, 1536},
193 { 300, 768},
194 { 600, 384},
195 { 1200, 192},
196 { 1800, 128},
197 { 2400, 96},
198 { 4800, 48},
199 { 7200, 32},
200 { 9600, 24},
201 { 14400, 16},
202 { 19200, 12},
203 { 38400, 6},
204 { 57600, 4},
205 { 115200, 2},
206 { 230400, 1},
207 };
208
209 /* local variables */
210 static int debug;
211
212 static int low_latency = 1; /* tty low latency flag, on by default */
213
214 static int CmdUrbs = 0; /* Number of outstanding Command Write Urbs */
215
216
217 /* local function prototypes */
218
219 /* function prototypes for all URB callbacks */
220 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs);
221 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs);
222 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs);
223 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs);
224
225 /* function prototypes for the usbserial callbacks */
226 static int edge_open (struct usb_serial_port *port, struct file *filp);
227 static void edge_close (struct usb_serial_port *port, struct file *filp);
228 static int edge_write (struct usb_serial_port *port, const unsigned char *buf, int count);
229 static int edge_write_room (struct usb_serial_port *port);
230 static int edge_chars_in_buffer (struct usb_serial_port *port);
231 static void edge_throttle (struct usb_serial_port *port);
232 static void edge_unthrottle (struct usb_serial_port *port);
233 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios);
234 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
235 static void edge_break (struct usb_serial_port *port, int break_state);
236 static int edge_tiocmget (struct usb_serial_port *port, struct file *file);
237 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
238 static int edge_startup (struct usb_serial *serial);
239 static void edge_shutdown (struct usb_serial *serial);
240
241
242 #include "io_tables.h" /* all of the devices that this driver supports */
243
244 static struct usb_driver io_driver = {
245 .name = "io_edgeport",
246 .probe = usb_serial_probe,
247 .disconnect = usb_serial_disconnect,
248 .id_table = id_table_combined,
249 .no_dynamic_id = 1,
250 };
251
252 /* function prototypes for all of our local functions */
253 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char *buffer, __u16 bufferLength);
254 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3);
255 static void edge_tty_recv (struct device *dev, struct tty_struct *tty, unsigned char *data, int length);
256 static void handle_new_msr (struct edgeport_port *edge_port, __u8 newMsr);
257 static void handle_new_lsr (struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data);
258 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param);
259 static int calc_baud_rate_divisor (int baud_rate, int *divisor);
260 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate);
261 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios);
262 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue);
263 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int writeLength);
264 static void send_more_port_data (struct edgeport_serial *edge_serial, struct edgeport_port *edge_port);
265
266 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
267 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
268 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data);
269 static void get_manufacturing_desc (struct edgeport_serial *edge_serial);
270 static void get_boot_desc (struct edgeport_serial *edge_serial);
271 static void load_application_firmware (struct edgeport_serial *edge_serial);
272
273 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size);
274
275
276 // ************************************************************************
277 // ************************************************************************
278 // ************************************************************************
279 // ************************************************************************
280
281 /************************************************************************
282 * *
283 * update_edgeport_E2PROM() Compare current versions of *
284 * Boot ROM and Manufacture *
285 * Descriptors with versions *
286 * embedded in this driver *
287 * *
288 ************************************************************************/
289 static void update_edgeport_E2PROM (struct edgeport_serial *edge_serial)
290 {
291 __u32 BootCurVer;
292 __u32 BootNewVer;
293 __u8 BootMajorVersion;
294 __u8 BootMinorVersion;
295 __le16 BootBuildNumber;
296 __u8 *BootImage;
297 __u32 BootSize;
298 struct edge_firmware_image_record *record;
299 unsigned char *firmware;
300 int response;
301
302
303 switch (edge_serial->product_info.iDownloadFile) {
304 case EDGE_DOWNLOAD_FILE_I930:
305 BootMajorVersion = BootCodeImageVersion_GEN1.MajorVersion;
306 BootMinorVersion = BootCodeImageVersion_GEN1.MinorVersion;
307 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN1.BuildNumber);
308 BootImage = &BootCodeImage_GEN1[0];
309 BootSize = sizeof( BootCodeImage_GEN1 );
310 break;
311
312 case EDGE_DOWNLOAD_FILE_80251:
313 BootMajorVersion = BootCodeImageVersion_GEN2.MajorVersion;
314 BootMinorVersion = BootCodeImageVersion_GEN2.MinorVersion;
315 BootBuildNumber = cpu_to_le16(BootCodeImageVersion_GEN2.BuildNumber);
316 BootImage = &BootCodeImage_GEN2[0];
317 BootSize = sizeof( BootCodeImage_GEN2 );
318 break;
319
320 default:
321 return;
322 }
323
324 // Check Boot Image Version
325 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
326 (edge_serial->boot_descriptor.MinorVersion << 16) +
327 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
328
329 BootNewVer = (BootMajorVersion << 24) +
330 (BootMinorVersion << 16) +
331 le16_to_cpu(BootBuildNumber);
332
333 dbg("Current Boot Image version %d.%d.%d",
334 edge_serial->boot_descriptor.MajorVersion,
335 edge_serial->boot_descriptor.MinorVersion,
336 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
337
338
339 if (BootNewVer > BootCurVer) {
340 dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
341 edge_serial->boot_descriptor.MajorVersion,
342 edge_serial->boot_descriptor.MinorVersion,
343 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
344 BootMajorVersion,
345 BootMinorVersion,
346 le16_to_cpu(BootBuildNumber));
347
348
349 dbg("Downloading new Boot Image");
350
351 firmware = BootImage;
352
353 for (;;) {
354 record = (struct edge_firmware_image_record *)firmware;
355 response = rom_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
356 if (response < 0) {
357 dev_err(&edge_serial->serial->dev->dev, "rom_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
358 break;
359 }
360 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
361 if (firmware >= &BootImage[BootSize]) {
362 break;
363 }
364 }
365 } else {
366 dbg("Boot Image -- already up to date");
367 }
368 }
369
370
371 /************************************************************************
372 * *
373 * Get string descriptor from device *
374 * *
375 ************************************************************************/
376 static int get_string (struct usb_device *dev, int Id, char *string)
377 {
378 struct usb_string_descriptor StringDesc;
379 struct usb_string_descriptor *pStringDesc;
380
381 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
382
383 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
384 return 0;
385 }
386
387 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
388
389 if (!pStringDesc) {
390 return 0;
391 }
392
393 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
394 kfree(pStringDesc);
395 return 0;
396 }
397
398 unicode_to_ascii(string, pStringDesc->wData, pStringDesc->bLength/2-1);
399
400 kfree(pStringDesc);
401 return strlen(string);
402 }
403
404
405 #if 0
406 /************************************************************************
407 *
408 * Get string descriptor from device
409 *
410 ************************************************************************/
411 static int get_string_desc (struct usb_device *dev, int Id, struct usb_string_descriptor **pRetDesc)
412 {
413 struct usb_string_descriptor StringDesc;
414 struct usb_string_descriptor *pStringDesc;
415
416 dbg("%s - USB String ID = %d", __FUNCTION__, Id );
417
418 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, sizeof(StringDesc))) {
419 return 0;
420 }
421
422 pStringDesc = kmalloc (StringDesc.bLength, GFP_KERNEL);
423
424 if (!pStringDesc) {
425 return -1;
426 }
427
428 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, StringDesc.bLength )) {
429 kfree(pStringDesc);
430 return -1;
431 }
432
433 *pRetDesc = pStringDesc;
434 return 0;
435 }
436 #endif
437
438 static void get_product_info(struct edgeport_serial *edge_serial)
439 {
440 struct edgeport_product_info *product_info = &edge_serial->product_info;
441
442 memset (product_info, 0, sizeof(struct edgeport_product_info));
443
444 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
445 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
446 product_info->ProdInfoVer = 0;
447
448 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
449 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
450 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
451 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
452
453 product_info->BootMajorVersion = edge_serial->boot_descriptor.MajorVersion;
454 product_info->BootMinorVersion = edge_serial->boot_descriptor.MinorVersion;
455 product_info->BootBuildNumber = edge_serial->boot_descriptor.BuildNumber;
456
457 memcpy(product_info->ManufactureDescDate, edge_serial->manuf_descriptor.DescDate, sizeof(edge_serial->manuf_descriptor.DescDate));
458
459 // check if this is 2nd generation hardware
460 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ION_DEVICE_ID_80251_NETCHIP) {
461 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN2.MajorVersion;
462 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN2.MinorVersion;
463 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN2.BuildNumber);
464 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
465 } else {
466 product_info->FirmwareMajorVersion = OperationalCodeImageVersion_GEN1.MajorVersion;
467 product_info->FirmwareMinorVersion = OperationalCodeImageVersion_GEN1.MinorVersion;
468 product_info->FirmwareBuildNumber = cpu_to_le16(OperationalCodeImageVersion_GEN1.BuildNumber);
469 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
470 }
471
472 // Determine Product type and set appropriate flags
473 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
474 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
475 case ION_DEVICE_ID_EDGEPORT_4T:
476 case ION_DEVICE_ID_EDGEPORT_4:
477 case ION_DEVICE_ID_EDGEPORT_2:
478 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
479 case ION_DEVICE_ID_EDGEPORT_8:
480 case ION_DEVICE_ID_EDGEPORT_421:
481 case ION_DEVICE_ID_EDGEPORT_21:
482 case ION_DEVICE_ID_EDGEPORT_2_DIN:
483 case ION_DEVICE_ID_EDGEPORT_4_DIN:
484 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
485 product_info->IsRS232 = 1;
486 break;
487
488 case ION_DEVICE_ID_EDGEPORT_2I: // Edgeport/2 RS422/RS485
489 product_info->IsRS422 = 1;
490 product_info->IsRS485 = 1;
491 break;
492
493 case ION_DEVICE_ID_EDGEPORT_8I: // Edgeport/4 RS422
494 case ION_DEVICE_ID_EDGEPORT_4I: // Edgeport/4 RS422
495 product_info->IsRS422 = 1;
496 break;
497 }
498
499 // Dump Product Info structure
500 dbg("**Product Information:");
501 dbg(" ProductId %x", product_info->ProductId );
502 dbg(" NumPorts %d", product_info->NumPorts );
503 dbg(" ProdInfoVer %d", product_info->ProdInfoVer );
504 dbg(" IsServer %d", product_info->IsServer);
505 dbg(" IsRS232 %d", product_info->IsRS232 );
506 dbg(" IsRS422 %d", product_info->IsRS422 );
507 dbg(" IsRS485 %d", product_info->IsRS485 );
508 dbg(" RomSize %d", product_info->RomSize );
509 dbg(" RamSize %d", product_info->RamSize );
510 dbg(" CpuRev %x", product_info->CpuRev );
511 dbg(" BoardRev %x", product_info->BoardRev);
512 dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
513 product_info->BootMinorVersion,
514 le16_to_cpu(product_info->BootBuildNumber));
515 dbg(" FirmwareMajorVersion %d.%d.%d", product_info->FirmwareMajorVersion,
516 product_info->FirmwareMinorVersion,
517 le16_to_cpu(product_info->FirmwareBuildNumber));
518 dbg(" ManufactureDescDate %d/%d/%d", product_info->ManufactureDescDate[0],
519 product_info->ManufactureDescDate[1],
520 product_info->ManufactureDescDate[2]+1900);
521 dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
522
523 }
524
525
526 /************************************************************************/
527 /************************************************************************/
528 /* U S B C A L L B A C K F U N C T I O N S */
529 /* U S B C A L L B A C K F U N C T I O N S */
530 /************************************************************************/
531 /************************************************************************/
532
533 /*****************************************************************************
534 * edge_interrupt_callback
535 * this is the callback function for when we have received data on the
536 * interrupt endpoint.
537 *****************************************************************************/
538 static void edge_interrupt_callback (struct urb *urb, struct pt_regs *regs)
539 {
540 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
541 struct edgeport_port *edge_port;
542 struct usb_serial_port *port;
543 unsigned char *data = urb->transfer_buffer;
544 int length = urb->actual_length;
545 int bytes_avail;
546 int position;
547 int txCredits;
548 int portNumber;
549 int result;
550
551 dbg("%s", __FUNCTION__);
552
553 switch (urb->status) {
554 case 0:
555 /* success */
556 break;
557 case -ECONNRESET:
558 case -ENOENT:
559 case -ESHUTDOWN:
560 /* this urb is terminated, clean up */
561 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
562 return;
563 default:
564 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
565 goto exit;
566 }
567
568 // process this interrupt-read even if there are no ports open
569 if (length) {
570 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, length, data);
571
572 if (length > 1) {
573 bytes_avail = data[0] | (data[1] << 8);
574 if (bytes_avail) {
575 spin_lock(&edge_serial->es_lock);
576 edge_serial->rxBytesAvail += bytes_avail;
577 dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __FUNCTION__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
578
579 if (edge_serial->rxBytesAvail > 0 &&
580 !edge_serial->read_in_progress) {
581 dbg("%s - posting a read", __FUNCTION__);
582 edge_serial->read_in_progress = TRUE;
583
584 /* we have pending bytes on the bulk in pipe, send a request */
585 edge_serial->read_urb->dev = edge_serial->serial->dev;
586 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
587 if (result) {
588 dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __FUNCTION__, result);
589 edge_serial->read_in_progress = FALSE;
590 }
591 }
592 spin_unlock(&edge_serial->es_lock);
593 }
594 }
595 /* grab the txcredits for the ports if available */
596 position = 2;
597 portNumber = 0;
598 while ((position < length) && (portNumber < edge_serial->serial->num_ports)) {
599 txCredits = data[position] | (data[position+1] << 8);
600 if (txCredits) {
601 port = edge_serial->serial->port[portNumber];
602 edge_port = usb_get_serial_port_data(port);
603 if (edge_port->open) {
604 spin_lock(&edge_port->ep_lock);
605 edge_port->txCredits += txCredits;
606 spin_unlock(&edge_port->ep_lock);
607 dbg("%s - txcredits for port%d = %d", __FUNCTION__, portNumber, edge_port->txCredits);
608
609 /* tell the tty driver that something has changed */
610 if (edge_port->port->tty)
611 tty_wakeup(edge_port->port->tty);
612
613 // Since we have more credit, check if more data can be sent
614 send_more_port_data(edge_serial, edge_port);
615 }
616 }
617 position += 2;
618 ++portNumber;
619 }
620 }
621
622 exit:
623 result = usb_submit_urb (urb, GFP_ATOMIC);
624 if (result) {
625 dev_err(&urb->dev->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, result);
626 }
627 }
628
629
630 /*****************************************************************************
631 * edge_bulk_in_callback
632 * this is the callback function for when we have received data on the
633 * bulk in endpoint.
634 *****************************************************************************/
635 static void edge_bulk_in_callback (struct urb *urb, struct pt_regs *regs)
636 {
637 struct edgeport_serial *edge_serial = (struct edgeport_serial *)urb->context;
638 unsigned char *data = urb->transfer_buffer;
639 int status;
640 __u16 raw_data_length;
641
642 dbg("%s", __FUNCTION__);
643
644 if (urb->status) {
645 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
646 edge_serial->read_in_progress = FALSE;
647 return;
648 }
649
650 if (urb->actual_length == 0) {
651 dbg("%s - read bulk callback with no data", __FUNCTION__);
652 edge_serial->read_in_progress = FALSE;
653 return;
654 }
655
656 raw_data_length = urb->actual_length;
657
658 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev, __FUNCTION__, raw_data_length, data);
659
660 spin_lock(&edge_serial->es_lock);
661
662 /* decrement our rxBytes available by the number that we just got */
663 edge_serial->rxBytesAvail -= raw_data_length;
664
665 dbg("%s - Received = %d, rxBytesAvail %d", __FUNCTION__, raw_data_length, edge_serial->rxBytesAvail);
666
667 process_rcvd_data (edge_serial, data, urb->actual_length);
668
669 /* check to see if there's any more data for us to read */
670 if (edge_serial->rxBytesAvail > 0) {
671 dbg("%s - posting a read", __FUNCTION__);
672 edge_serial->read_urb->dev = edge_serial->serial->dev;
673 status = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
674 if (status) {
675 dev_err(&urb->dev->dev, "%s - usb_submit_urb(read bulk) failed, status = %d\n", __FUNCTION__, status);
676 edge_serial->read_in_progress = FALSE;
677 }
678 } else {
679 edge_serial->read_in_progress = FALSE;
680 }
681
682 spin_unlock(&edge_serial->es_lock);
683 }
684
685
686 /*****************************************************************************
687 * edge_bulk_out_data_callback
688 * this is the callback function for when we have finished sending serial data
689 * on the bulk out endpoint.
690 *****************************************************************************/
691 static void edge_bulk_out_data_callback (struct urb *urb, struct pt_regs *regs)
692 {
693 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
694 struct tty_struct *tty;
695
696 dbg("%s", __FUNCTION__);
697
698 if (urb->status) {
699 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
700 }
701
702 tty = edge_port->port->tty;
703
704 if (tty && edge_port->open) {
705 /* let the tty driver wakeup if it has a special write_wakeup function */
706 tty_wakeup(tty);
707 }
708
709 // Release the Write URB
710 edge_port->write_in_progress = FALSE;
711
712 // Check if more data needs to be sent
713 send_more_port_data((struct edgeport_serial *)(usb_get_serial_data(edge_port->port->serial)), edge_port);
714 }
715
716
717 /*****************************************************************************
718 * BulkOutCmdCallback
719 * this is the callback function for when we have finished sending a command
720 * on the bulk out endpoint.
721 *****************************************************************************/
722 static void edge_bulk_out_cmd_callback (struct urb *urb, struct pt_regs *regs)
723 {
724 struct edgeport_port *edge_port = (struct edgeport_port *)urb->context;
725 struct tty_struct *tty;
726 int status = urb->status;
727
728 dbg("%s", __FUNCTION__);
729
730 CmdUrbs--;
731 dbg("%s - FREE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
732
733
734 /* clean up the transfer buffer */
735 kfree(urb->transfer_buffer);
736
737 /* Free the command urb */
738 usb_free_urb (urb);
739
740 if (status) {
741 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, status);
742 return;
743 }
744
745 /* Get pointer to tty */
746 tty = edge_port->port->tty;
747
748 /* tell the tty driver that something has changed */
749 if (tty && edge_port->open)
750 tty_wakeup(tty);
751
752 /* we have completed the command */
753 edge_port->commandPending = FALSE;
754 wake_up(&edge_port->wait_command);
755 }
756
757
758 /*****************************************************************************
759 * Driver tty interface functions
760 *****************************************************************************/
761
762 /*****************************************************************************
763 * SerialOpen
764 * this function is called by the tty driver when a port is opened
765 * If successful, we return 0
766 * Otherwise we return a negative error number.
767 *****************************************************************************/
768 static int edge_open (struct usb_serial_port *port, struct file * filp)
769 {
770 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
771 struct usb_serial *serial;
772 struct edgeport_serial *edge_serial;
773 int response;
774
775 dbg("%s - port %d", __FUNCTION__, port->number);
776
777 if (edge_port == NULL)
778 return -ENODEV;
779
780 if (port->tty)
781 port->tty->low_latency = low_latency;
782
783 /* see if we've set up our endpoint info yet (can't set it up in edge_startup
784 as the structures were not set up at that time.) */
785 serial = port->serial;
786 edge_serial = usb_get_serial_data(serial);
787 if (edge_serial == NULL) {
788 return -ENODEV;
789 }
790 if (edge_serial->interrupt_in_buffer == NULL) {
791 struct usb_serial_port *port0 = serial->port[0];
792
793 /* not set up yet, so do it now */
794 edge_serial->interrupt_in_buffer = port0->interrupt_in_buffer;
795 edge_serial->interrupt_in_endpoint = port0->interrupt_in_endpointAddress;
796 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
797 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
798 edge_serial->bulk_in_endpoint = port0->bulk_in_endpointAddress;
799 edge_serial->read_urb = port0->read_urb;
800 edge_serial->bulk_out_endpoint = port0->bulk_out_endpointAddress;
801
802 /* set up our interrupt urb */
803 usb_fill_int_urb(edge_serial->interrupt_read_urb,
804 serial->dev,
805 usb_rcvintpipe(serial->dev,
806 port0->interrupt_in_endpointAddress),
807 port0->interrupt_in_buffer,
808 edge_serial->interrupt_read_urb->transfer_buffer_length,
809 edge_interrupt_callback, edge_serial,
810 edge_serial->interrupt_read_urb->interval);
811
812 /* set up our bulk in urb */
813 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
814 usb_rcvbulkpipe(serial->dev,
815 port0->bulk_in_endpointAddress),
816 port0->bulk_in_buffer,
817 edge_serial->read_urb->transfer_buffer_length,
818 edge_bulk_in_callback, edge_serial);
819 edge_serial->read_in_progress = FALSE;
820
821 /* start interrupt read for this edgeport
822 * this interrupt will continue as long as the edgeport is connected */
823 response = usb_submit_urb (edge_serial->interrupt_read_urb, GFP_KERNEL);
824 if (response) {
825 dev_err(&port->dev, "%s - Error %d submitting control urb\n", __FUNCTION__, response);
826 }
827 }
828
829 /* initialize our wait queues */
830 init_waitqueue_head(&edge_port->wait_open);
831 init_waitqueue_head(&edge_port->wait_chase);
832 init_waitqueue_head(&edge_port->delta_msr_wait);
833 init_waitqueue_head(&edge_port->wait_command);
834
835 /* initialize our icount structure */
836 memset (&(edge_port->icount), 0x00, sizeof(edge_port->icount));
837
838 /* initialize our port settings */
839 edge_port->txCredits = 0; /* Can't send any data yet */
840 edge_port->shadowMCR = MCR_MASTER_IE; /* Must always set this bit to enable ints! */
841 edge_port->chaseResponsePending = FALSE;
842
843 /* send a open port command */
844 edge_port->openPending = TRUE;
845 edge_port->open = FALSE;
846 response = send_iosp_ext_cmd (edge_port, IOSP_CMD_OPEN_PORT, 0);
847
848 if (response < 0) {
849 dev_err(&port->dev, "%s - error sending open port command\n", __FUNCTION__);
850 edge_port->openPending = FALSE;
851 return -ENODEV;
852 }
853
854 /* now wait for the port to be completely opened */
855 wait_event_timeout(edge_port->wait_open, (edge_port->openPending != TRUE), OPEN_TIMEOUT);
856
857 if (edge_port->open == FALSE) {
858 /* open timed out */
859 dbg("%s - open timedout", __FUNCTION__);
860 edge_port->openPending = FALSE;
861 return -ENODEV;
862 }
863
864 /* create the txfifo */
865 edge_port->txfifo.head = 0;
866 edge_port->txfifo.tail = 0;
867 edge_port->txfifo.count = 0;
868 edge_port->txfifo.size = edge_port->maxTxCredits;
869 edge_port->txfifo.fifo = kmalloc (edge_port->maxTxCredits, GFP_KERNEL);
870
871 if (!edge_port->txfifo.fifo) {
872 dbg("%s - no memory", __FUNCTION__);
873 edge_close (port, filp);
874 return -ENOMEM;
875 }
876
877 /* Allocate a URB for the write */
878 edge_port->write_urb = usb_alloc_urb (0, GFP_KERNEL);
879 edge_port->write_in_progress = FALSE;
880
881 if (!edge_port->write_urb) {
882 dbg("%s - no memory", __FUNCTION__);
883 edge_close (port, filp);
884 return -ENOMEM;
885 }
886
887 dbg("%s(%d) - Initialize TX fifo to %d bytes", __FUNCTION__, port->number, edge_port->maxTxCredits);
888
889 dbg("%s exited", __FUNCTION__);
890
891 return 0;
892 }
893
894
895 /************************************************************************
896 *
897 * block_until_chase_response
898 *
899 * This function will block the close until one of the following:
900 * 1. Response to our Chase comes from Edgeport
901 * 2. A timout of 10 seconds without activity has expired
902 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
903 *
904 ************************************************************************/
905 static void block_until_chase_response(struct edgeport_port *edge_port)
906 {
907 DEFINE_WAIT(wait);
908 __u16 lastCredits;
909 int timeout = 1*HZ;
910 int loop = 10;
911
912 while (1) {
913 // Save Last credits
914 lastCredits = edge_port->txCredits;
915
916 // Did we get our Chase response
917 if (edge_port->chaseResponsePending == FALSE) {
918 dbg("%s - Got Chase Response", __FUNCTION__);
919
920 // did we get all of our credit back?
921 if (edge_port->txCredits == edge_port->maxTxCredits ) {
922 dbg("%s - Got all credits", __FUNCTION__);
923 return;
924 }
925 }
926
927 // Block the thread for a while
928 prepare_to_wait(&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
929 schedule_timeout(timeout);
930 finish_wait(&edge_port->wait_chase, &wait);
931
932 if (lastCredits == edge_port->txCredits) {
933 // No activity.. count down.
934 loop--;
935 if (loop == 0) {
936 edge_port->chaseResponsePending = FALSE;
937 dbg("%s - Chase TIMEOUT", __FUNCTION__);
938 return;
939 }
940 } else {
941 // Reset timout value back to 10 seconds
942 dbg("%s - Last %d, Current %d", __FUNCTION__, lastCredits, edge_port->txCredits);
943 loop = 10;
944 }
945 }
946 }
947
948
949 /************************************************************************
950 *
951 * block_until_tx_empty
952 *
953 * This function will block the close until one of the following:
954 * 1. TX count are 0
955 * 2. The edgeport has stopped
956 * 3. A timout of 3 seconds without activity has expired
957 *
958 ************************************************************************/
959 static void block_until_tx_empty (struct edgeport_port *edge_port)
960 {
961 DEFINE_WAIT(wait);
962 struct TxFifo *fifo = &edge_port->txfifo;
963 __u32 lastCount;
964 int timeout = HZ/10;
965 int loop = 30;
966
967 while (1) {
968 // Save Last count
969 lastCount = fifo->count;
970
971 // Is the Edgeport Buffer empty?
972 if (lastCount == 0) {
973 dbg("%s - TX Buffer Empty", __FUNCTION__);
974 return;
975 }
976
977 // Block the thread for a while
978 prepare_to_wait (&edge_port->wait_chase, &wait, TASK_UNINTERRUPTIBLE);
979 schedule_timeout(timeout);
980 finish_wait(&edge_port->wait_chase, &wait);
981
982 dbg("%s wait", __FUNCTION__);
983
984 if (lastCount == fifo->count) {
985 // No activity.. count down.
986 loop--;
987 if (loop == 0) {
988 dbg("%s - TIMEOUT", __FUNCTION__);
989 return;
990 }
991 } else {
992 // Reset timout value back to seconds
993 loop = 30;
994 }
995 }
996 }
997
998
999 /*****************************************************************************
1000 * edge_close
1001 * this function is called by the tty driver when a port is closed
1002 *****************************************************************************/
1003 static void edge_close (struct usb_serial_port *port, struct file * filp)
1004 {
1005 struct edgeport_serial *edge_serial;
1006 struct edgeport_port *edge_port;
1007 int status;
1008
1009 dbg("%s - port %d", __FUNCTION__, port->number);
1010
1011 edge_serial = usb_get_serial_data(port->serial);
1012 edge_port = usb_get_serial_port_data(port);
1013 if ((edge_serial == NULL) || (edge_port == NULL))
1014 return;
1015
1016 // block until tx is empty
1017 block_until_tx_empty(edge_port);
1018
1019 edge_port->closePending = TRUE;
1020
1021 /* flush and chase */
1022 edge_port->chaseResponsePending = TRUE;
1023
1024 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1025 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1026 if (status == 0) {
1027 // block until chase finished
1028 block_until_chase_response(edge_port);
1029 } else {
1030 edge_port->chaseResponsePending = FALSE;
1031 }
1032
1033 /* close the port */
1034 dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __FUNCTION__);
1035 send_iosp_ext_cmd (edge_port, IOSP_CMD_CLOSE_PORT, 0);
1036
1037 //port->close = TRUE;
1038 edge_port->closePending = FALSE;
1039 edge_port->open = FALSE;
1040 edge_port->openPending = FALSE;
1041
1042 if (edge_port->write_urb) {
1043 usb_kill_urb(edge_port->write_urb);
1044 }
1045
1046 if (edge_port->write_urb) {
1047 /* if this urb had a transfer buffer already (old transfer) free it */
1048 kfree(edge_port->write_urb->transfer_buffer);
1049 usb_free_urb(edge_port->write_urb);
1050 edge_port->write_urb = NULL;
1051 }
1052 kfree(edge_port->txfifo.fifo);
1053 edge_port->txfifo.fifo = NULL;
1054
1055 dbg("%s exited", __FUNCTION__);
1056 }
1057
1058 /*****************************************************************************
1059 * SerialWrite
1060 * this function is called by the tty driver when data should be written to
1061 * the port.
1062 * If successful, we return the number of bytes written, otherwise we return
1063 * a negative error number.
1064 *****************************************************************************/
1065 static int edge_write (struct usb_serial_port *port, const unsigned char *data, int count)
1066 {
1067 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1068 struct TxFifo *fifo;
1069 int copySize;
1070 int bytesleft;
1071 int firsthalf;
1072 int secondhalf;
1073 unsigned long flags;
1074
1075 dbg("%s - port %d", __FUNCTION__, port->number);
1076
1077 if (edge_port == NULL)
1078 return -ENODEV;
1079
1080 // get a pointer to the Tx fifo
1081 fifo = &edge_port->txfifo;
1082
1083 spin_lock_irqsave(&edge_port->ep_lock, flags);
1084
1085 // calculate number of bytes to put in fifo
1086 copySize = min ((unsigned int)count, (edge_port->txCredits - fifo->count));
1087
1088 dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes", __FUNCTION__,
1089 port->number, count, edge_port->txCredits - fifo->count, copySize);
1090
1091 /* catch writes of 0 bytes which the tty driver likes to give us, and when txCredits is empty */
1092 if (copySize == 0) {
1093 dbg("%s - copySize = Zero", __FUNCTION__);
1094 goto finish_write;
1095 }
1096
1097 // queue the data
1098 // since we can never overflow the buffer we do not have to check for full condition
1099
1100 // the copy is done is two parts -- first fill to the end of the buffer
1101 // then copy the reset from the start of the buffer
1102
1103 bytesleft = fifo->size - fifo->head;
1104 firsthalf = min (bytesleft, copySize);
1105 dbg("%s - copy %d bytes of %d into fifo ", __FUNCTION__, firsthalf, bytesleft);
1106
1107 /* now copy our data */
1108 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1109 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, firsthalf, &fifo->fifo[fifo->head]);
1110
1111 // update the index and size
1112 fifo->head += firsthalf;
1113 fifo->count += firsthalf;
1114
1115 // wrap the index
1116 if (fifo->head == fifo->size) {
1117 fifo->head = 0;
1118 }
1119
1120 secondhalf = copySize-firsthalf;
1121
1122 if (secondhalf) {
1123 dbg("%s - copy rest of data %d", __FUNCTION__, secondhalf);
1124 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1125 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, secondhalf, &fifo->fifo[fifo->head]);
1126 // update the index and size
1127 fifo->count += secondhalf;
1128 fifo->head += secondhalf;
1129 // No need to check for wrap since we can not get to end of fifo in this part
1130 }
1131
1132 finish_write:
1133 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1134
1135 send_more_port_data((struct edgeport_serial *)usb_get_serial_data(port->serial), edge_port);
1136
1137 dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __FUNCTION__, copySize, edge_port->txCredits, fifo->count);
1138
1139 return copySize;
1140 }
1141
1142
1143 /************************************************************************
1144 *
1145 * send_more_port_data()
1146 *
1147 * This routine attempts to write additional UART transmit data
1148 * to a port over the USB bulk pipe. It is called (1) when new
1149 * data has been written to a port's TxBuffer from higher layers
1150 * (2) when the peripheral sends us additional TxCredits indicating
1151 * that it can accept more Tx data for a given port; and (3) when
1152 * a bulk write completes successfully and we want to see if we
1153 * can transmit more.
1154 *
1155 ************************************************************************/
1156 static void send_more_port_data(struct edgeport_serial *edge_serial, struct edgeport_port *edge_port)
1157 {
1158 struct TxFifo *fifo = &edge_port->txfifo;
1159 struct urb *urb;
1160 unsigned char *buffer;
1161 int status;
1162 int count;
1163 int bytesleft;
1164 int firsthalf;
1165 int secondhalf;
1166 unsigned long flags;
1167
1168 dbg("%s(%d)", __FUNCTION__, edge_port->port->number);
1169
1170 spin_lock_irqsave(&edge_port->ep_lock, flags);
1171
1172 if (edge_port->write_in_progress ||
1173 !edge_port->open ||
1174 (fifo->count == 0)) {
1175 dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->write_in_progress);
1176 goto exit_send;
1177 }
1178
1179 // since the amount of data in the fifo will always fit into the
1180 // edgeport buffer we do not need to check the write length
1181
1182 // Do we have enough credits for this port to make it worthwhile
1183 // to bother queueing a write. If it's too small, say a few bytes,
1184 // it's better to wait for more credits so we can do a larger
1185 // write.
1186 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits,EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1187 dbg("%s(%d) Not enough credit - fifo %d TxCredit %d", __FUNCTION__, edge_port->port->number, fifo->count, edge_port->txCredits );
1188 goto exit_send;
1189 }
1190
1191 // lock this write
1192 edge_port->write_in_progress = TRUE;
1193
1194 // get a pointer to the write_urb
1195 urb = edge_port->write_urb;
1196
1197 /* make sure transfer buffer is freed */
1198 kfree(urb->transfer_buffer);
1199 urb->transfer_buffer = NULL;
1200
1201 /* build the data header for the buffer and port that we are about to send out */
1202 count = fifo->count;
1203 buffer = kmalloc (count+2, GFP_ATOMIC);
1204 if (buffer == NULL) {
1205 dev_err(&edge_port->port->dev, "%s - no more kernel memory...\n", __FUNCTION__);
1206 edge_port->write_in_progress = FALSE;
1207 goto exit_send;
1208 }
1209 buffer[0] = IOSP_BUILD_DATA_HDR1 (edge_port->port->number - edge_port->port->serial->minor, count);
1210 buffer[1] = IOSP_BUILD_DATA_HDR2 (edge_port->port->number - edge_port->port->serial->minor, count);
1211
1212 /* now copy our data */
1213 bytesleft = fifo->size - fifo->tail;
1214 firsthalf = min (bytesleft, count);
1215 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1216 fifo->tail += firsthalf;
1217 fifo->count -= firsthalf;
1218 if (fifo->tail == fifo->size) {
1219 fifo->tail = 0;
1220 }
1221
1222 secondhalf = count-firsthalf;
1223 if (secondhalf) {
1224 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], secondhalf);
1225 fifo->tail += secondhalf;
1226 fifo->count -= secondhalf;
1227 }
1228
1229 if (count)
1230 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, count, &buffer[2]);
1231
1232 /* fill up the urb with all of our data and submit it */
1233 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
1234 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
1235 buffer, count+2, edge_bulk_out_data_callback, edge_port);
1236
1237 /* decrement the number of credits we have by the number we just sent */
1238 edge_port->txCredits -= count;
1239 edge_port->icount.tx += count;
1240
1241 urb->dev = edge_serial->serial->dev;
1242 status = usb_submit_urb(urb, GFP_ATOMIC);
1243 if (status) {
1244 /* something went wrong */
1245 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", __FUNCTION__, status);
1246 edge_port->write_in_progress = FALSE;
1247
1248 /* revert the credits as something bad happened. */
1249 edge_port->txCredits += count;
1250 edge_port->icount.tx -= count;
1251 }
1252 dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d", __FUNCTION__, count, edge_port->txCredits, fifo->count);
1253
1254 exit_send:
1255 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1256 }
1257
1258
1259 /*****************************************************************************
1260 * edge_write_room
1261 * this function is called by the tty driver when it wants to know how many
1262 * bytes of data we can accept for a specific port.
1263 * If successful, we return the amount of room that we have for this port
1264 * (the txCredits),
1265 * Otherwise we return a negative error number.
1266 *****************************************************************************/
1267 static int edge_write_room (struct usb_serial_port *port)
1268 {
1269 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1270 int room;
1271 unsigned long flags;
1272
1273 dbg("%s", __FUNCTION__);
1274
1275 if (edge_port == NULL)
1276 return -ENODEV;
1277 if (edge_port->closePending == TRUE)
1278 return -ENODEV;
1279
1280 dbg("%s - port %d", __FUNCTION__, port->number);
1281
1282 if (!edge_port->open) {
1283 dbg("%s - port not opened", __FUNCTION__);
1284 return -EINVAL;
1285 }
1286
1287 // total of both buffers is still txCredit
1288 spin_lock_irqsave(&edge_port->ep_lock, flags);
1289 room = edge_port->txCredits - edge_port->txfifo.count;
1290 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1291
1292 dbg("%s - returns %d", __FUNCTION__, room);
1293 return room;
1294 }
1295
1296
1297 /*****************************************************************************
1298 * edge_chars_in_buffer
1299 * this function is called by the tty driver when it wants to know how many
1300 * bytes of data we currently have outstanding in the port (data that has
1301 * been written, but hasn't made it out the port yet)
1302 * If successful, we return the number of bytes left to be written in the
1303 * system,
1304 * Otherwise we return a negative error number.
1305 *****************************************************************************/
1306 static int edge_chars_in_buffer (struct usb_serial_port *port)
1307 {
1308 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1309 int num_chars;
1310 unsigned long flags;
1311
1312 dbg("%s", __FUNCTION__);
1313
1314 if (edge_port == NULL)
1315 return -ENODEV;
1316 if (edge_port->closePending == TRUE)
1317 return -ENODEV;
1318
1319 if (!edge_port->open) {
1320 dbg("%s - port not opened", __FUNCTION__);
1321 return -EINVAL;
1322 }
1323
1324 spin_lock_irqsave(&edge_port->ep_lock, flags);
1325 num_chars = edge_port->maxTxCredits - edge_port->txCredits + edge_port->txfifo.count;
1326 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1327 if (num_chars) {
1328 dbg("%s(port %d) - returns %d", __FUNCTION__, port->number, num_chars);
1329 }
1330
1331 return num_chars;
1332 }
1333
1334
1335 /*****************************************************************************
1336 * SerialThrottle
1337 * this function is called by the tty driver when it wants to stop the data
1338 * being read from the port.
1339 *****************************************************************************/
1340 static void edge_throttle (struct usb_serial_port *port)
1341 {
1342 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1343 struct tty_struct *tty;
1344 int status;
1345
1346 dbg("%s - port %d", __FUNCTION__, port->number);
1347
1348 if (edge_port == NULL)
1349 return;
1350
1351 if (!edge_port->open) {
1352 dbg("%s - port not opened", __FUNCTION__);
1353 return;
1354 }
1355
1356 tty = port->tty;
1357 if (!tty) {
1358 dbg ("%s - no tty available", __FUNCTION__);
1359 return;
1360 }
1361
1362 /* if we are implementing XON/XOFF, send the stop character */
1363 if (I_IXOFF(tty)) {
1364 unsigned char stop_char = STOP_CHAR(tty);
1365 status = edge_write (port, &stop_char, 1);
1366 if (status <= 0) {
1367 return;
1368 }
1369 }
1370
1371 /* if we are implementing RTS/CTS, toggle that line */
1372 if (tty->termios->c_cflag & CRTSCTS) {
1373 edge_port->shadowMCR &= ~MCR_RTS;
1374 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1375 if (status != 0) {
1376 return;
1377 }
1378 }
1379
1380 return;
1381 }
1382
1383
1384 /*****************************************************************************
1385 * edge_unthrottle
1386 * this function is called by the tty driver when it wants to resume the data
1387 * being read from the port (called after SerialThrottle is called)
1388 *****************************************************************************/
1389 static void edge_unthrottle (struct usb_serial_port *port)
1390 {
1391 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1392 struct tty_struct *tty;
1393 int status;
1394
1395 dbg("%s - port %d", __FUNCTION__, port->number);
1396
1397 if (edge_port == NULL)
1398 return;
1399
1400 if (!edge_port->open) {
1401 dbg("%s - port not opened", __FUNCTION__);
1402 return;
1403 }
1404
1405 tty = port->tty;
1406 if (!tty) {
1407 dbg ("%s - no tty available", __FUNCTION__);
1408 return;
1409 }
1410
1411 /* if we are implementing XON/XOFF, send the start character */
1412 if (I_IXOFF(tty)) {
1413 unsigned char start_char = START_CHAR(tty);
1414 status = edge_write (port, &start_char, 1);
1415 if (status <= 0) {
1416 return;
1417 }
1418 }
1419
1420 /* if we are implementing RTS/CTS, toggle that line */
1421 if (tty->termios->c_cflag & CRTSCTS) {
1422 edge_port->shadowMCR |= MCR_RTS;
1423 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1424 if (status != 0) {
1425 return;
1426 }
1427 }
1428
1429 return;
1430 }
1431
1432
1433 /*****************************************************************************
1434 * SerialSetTermios
1435 * this function is called by the tty driver when it wants to change the termios structure
1436 *****************************************************************************/
1437 static void edge_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1438 {
1439 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1440 struct tty_struct *tty = port->tty;
1441 unsigned int cflag;
1442
1443 if (!port->tty || !port->tty->termios) {
1444 dbg ("%s - no tty or termios", __FUNCTION__);
1445 return;
1446 }
1447
1448 cflag = tty->termios->c_cflag;
1449 /* check that they really want us to change something */
1450 if (old_termios) {
1451 if (cflag == old_termios->c_cflag &&
1452 tty->termios->c_iflag == old_termios->c_iflag) {
1453 dbg("%s - nothing to change", __FUNCTION__);
1454 return;
1455 }
1456 }
1457
1458 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1459 tty->termios->c_cflag, tty->termios->c_iflag);
1460 if (old_termios) {
1461 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1462 old_termios->c_cflag, old_termios->c_iflag);
1463 }
1464
1465 dbg("%s - port %d", __FUNCTION__, port->number);
1466
1467 if (edge_port == NULL)
1468 return;
1469
1470 if (!edge_port->open) {
1471 dbg("%s - port not opened", __FUNCTION__);
1472 return;
1473 }
1474
1475 /* change the port settings to the new ones specified */
1476 change_port_settings (edge_port, old_termios);
1477
1478 return;
1479 }
1480
1481
1482 /*****************************************************************************
1483 * get_lsr_info - get line status register info
1484 *
1485 * Purpose: Let user call ioctl() to get info when the UART physically
1486 * is emptied. On bus types like RS485, the transmitter must
1487 * release the bus after transmitting. This must be done when
1488 * the transmit shift register is empty, not be done when the
1489 * transmit holding register is empty. This functionality
1490 * allows an RS485 driver to be written in user space.
1491 *****************************************************************************/
1492 static int get_lsr_info(struct edgeport_port *edge_port, unsigned int __user *value)
1493 {
1494 unsigned int result = 0;
1495 unsigned long flags;
1496
1497 spin_lock_irqsave(&edge_port->ep_lock, flags);
1498 if (edge_port->maxTxCredits == edge_port->txCredits &&
1499 edge_port->txfifo.count == 0) {
1500 dbg("%s -- Empty", __FUNCTION__);
1501 result = TIOCSER_TEMT;
1502 }
1503 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1504
1505 if (copy_to_user(value, &result, sizeof(int)))
1506 return -EFAULT;
1507 return 0;
1508 }
1509
1510 static int get_number_bytes_avail(struct edgeport_port *edge_port, unsigned int __user *value)
1511 {
1512 unsigned int result = 0;
1513 struct tty_struct *tty = edge_port->port->tty;
1514
1515 if (!tty)
1516 return -ENOIOCTLCMD;
1517
1518 result = tty->read_cnt;
1519
1520 dbg("%s(%d) = %d", __FUNCTION__, edge_port->port->number, result);
1521 if (copy_to_user(value, &result, sizeof(int)))
1522 return -EFAULT;
1523 //return 0;
1524 return -ENOIOCTLCMD;
1525 }
1526
1527 static int edge_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear)
1528 {
1529 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1530 unsigned int mcr;
1531
1532 dbg("%s - port %d", __FUNCTION__, port->number);
1533
1534 mcr = edge_port->shadowMCR;
1535 if (set & TIOCM_RTS)
1536 mcr |= MCR_RTS;
1537 if (set & TIOCM_DTR)
1538 mcr |= MCR_DTR;
1539 if (set & TIOCM_LOOP)
1540 mcr |= MCR_LOOPBACK;
1541
1542 if (clear & TIOCM_RTS)
1543 mcr &= ~MCR_RTS;
1544 if (clear & TIOCM_DTR)
1545 mcr &= ~MCR_DTR;
1546 if (clear & TIOCM_LOOP)
1547 mcr &= ~MCR_LOOPBACK;
1548
1549 edge_port->shadowMCR = mcr;
1550
1551 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1552
1553 return 0;
1554 }
1555
1556 static int edge_tiocmget(struct usb_serial_port *port, struct file *file)
1557 {
1558 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1559 unsigned int result = 0;
1560 unsigned int msr;
1561 unsigned int mcr;
1562
1563 dbg("%s - port %d", __FUNCTION__, port->number);
1564
1565 msr = edge_port->shadowMSR;
1566 mcr = edge_port->shadowMCR;
1567 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1568 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1569 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1570 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1571 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1572 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1573
1574
1575 dbg("%s -- %x", __FUNCTION__, result);
1576
1577 return result;
1578 }
1579
1580 static int get_serial_info(struct edgeport_port *edge_port, struct serial_struct __user *retinfo)
1581 {
1582 struct serial_struct tmp;
1583
1584 if (!retinfo)
1585 return -EFAULT;
1586
1587 memset(&tmp, 0, sizeof(tmp));
1588
1589 tmp.type = PORT_16550A;
1590 tmp.line = edge_port->port->serial->minor;
1591 tmp.port = edge_port->port->number;
1592 tmp.irq = 0;
1593 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1594 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1595 tmp.baud_base = 9600;
1596 tmp.close_delay = 5*HZ;
1597 tmp.closing_wait = 30*HZ;
1598 // tmp.custom_divisor = state->custom_divisor;
1599 // tmp.hub6 = state->hub6;
1600 // tmp.io_type = state->io_type;
1601
1602 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1603 return -EFAULT;
1604 return 0;
1605 }
1606
1607
1608
1609 /*****************************************************************************
1610 * SerialIoctl
1611 * this function handles any ioctl calls to the driver
1612 *****************************************************************************/
1613 static int edge_ioctl (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
1614 {
1615 DEFINE_WAIT(wait);
1616 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1617 struct async_icount cnow;
1618 struct async_icount cprev;
1619 struct serial_icounter_struct icount;
1620
1621 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1622
1623 switch (cmd) {
1624 // return number of bytes available
1625 case TIOCINQ:
1626 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1627 return get_number_bytes_avail(edge_port, (unsigned int __user *) arg);
1628 break;
1629
1630 case TIOCSERGETLSR:
1631 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1632 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1633 return 0;
1634
1635 case TIOCGSERIAL:
1636 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1637 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1638
1639 case TIOCSSERIAL:
1640 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1641 break;
1642
1643 case TIOCMIWAIT:
1644 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1645 cprev = edge_port->icount;
1646 while (1) {
1647 prepare_to_wait(&edge_port->delta_msr_wait, &wait, TASK_INTERRUPTIBLE);
1648 schedule();
1649 finish_wait(&edge_port->delta_msr_wait, &wait);
1650 /* see if a signal did it */
1651 if (signal_pending(current))
1652 return -ERESTARTSYS;
1653 cnow = edge_port->icount;
1654 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1655 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1656 return -EIO; /* no change => error */
1657 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1658 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1659 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1660 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1661 return 0;
1662 }
1663 cprev = cnow;
1664 }
1665 /* NOTREACHED */
1666 break;
1667
1668 case TIOCGICOUNT:
1669 cnow = edge_port->icount;
1670 memset(&icount, 0, sizeof(icount));
1671 icount.cts = cnow.cts;
1672 icount.dsr = cnow.dsr;
1673 icount.rng = cnow.rng;
1674 icount.dcd = cnow.dcd;
1675 icount.rx = cnow.rx;
1676 icount.tx = cnow.tx;
1677 icount.frame = cnow.frame;
1678 icount.overrun = cnow.overrun;
1679 icount.parity = cnow.parity;
1680 icount.brk = cnow.brk;
1681 icount.buf_overrun = cnow.buf_overrun;
1682
1683 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, icount.rx, icount.tx );
1684 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1685 return -EFAULT;
1686 return 0;
1687 }
1688
1689 return -ENOIOCTLCMD;
1690 }
1691
1692
1693 /*****************************************************************************
1694 * SerialBreak
1695 * this function sends a break to the port
1696 *****************************************************************************/
1697 static void edge_break (struct usb_serial_port *port, int break_state)
1698 {
1699 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1700 int status;
1701
1702 /* flush and chase */
1703 edge_port->chaseResponsePending = TRUE;
1704
1705 dbg("%s - Sending IOSP_CMD_CHASE_PORT", __FUNCTION__);
1706 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CHASE_PORT, 0);
1707 if (status == 0) {
1708 // block until chase finished
1709 block_until_chase_response(edge_port);
1710 } else {
1711 edge_port->chaseResponsePending = FALSE;
1712 }
1713
1714 if (break_state == -1) {
1715 dbg("%s - Sending IOSP_CMD_SET_BREAK", __FUNCTION__);
1716 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_BREAK, 0);
1717 } else {
1718 dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __FUNCTION__);
1719 status = send_iosp_ext_cmd (edge_port, IOSP_CMD_CLEAR_BREAK, 0);
1720 }
1721 if (status) {
1722 dbg("%s - error sending break set/clear command.", __FUNCTION__);
1723 }
1724
1725 return;
1726 }
1727
1728
1729 /*****************************************************************************
1730 * process_rcvd_data
1731 * this function handles the data received on the bulk in pipe.
1732 *****************************************************************************/
1733 static void process_rcvd_data (struct edgeport_serial *edge_serial, unsigned char * buffer, __u16 bufferLength)
1734 {
1735 struct usb_serial_port *port;
1736 struct edgeport_port *edge_port;
1737 struct tty_struct *tty;
1738 __u16 lastBufferLength;
1739 __u16 rxLen;
1740
1741 dbg("%s", __FUNCTION__);
1742
1743 lastBufferLength = bufferLength + 1;
1744
1745 while (bufferLength > 0) {
1746 /* failsafe incase we get a message that we don't understand */
1747 if (lastBufferLength == bufferLength) {
1748 dbg("%s - stuck in loop, exiting it.", __FUNCTION__);
1749 break;
1750 }
1751 lastBufferLength = bufferLength;
1752
1753 switch (edge_serial->rxState) {
1754 case EXPECT_HDR1:
1755 edge_serial->rxHeader1 = *buffer;
1756 ++buffer;
1757 --bufferLength;
1758
1759 if (bufferLength == 0) {
1760 edge_serial->rxState = EXPECT_HDR2;
1761 break;
1762 }
1763 /* otherwise, drop on through */
1764
1765 case EXPECT_HDR2:
1766 edge_serial->rxHeader2 = *buffer;
1767 ++buffer;
1768 --bufferLength;
1769
1770 dbg("%s - Hdr1=%02X Hdr2=%02X", __FUNCTION__, edge_serial->rxHeader1, edge_serial->rxHeader2);
1771
1772 // Process depending on whether this header is
1773 // data or status
1774
1775 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1776 // Decode this status header and goto EXPECT_HDR1 (if we
1777 // can process the status with only 2 bytes), or goto
1778 // EXPECT_HDR3 to get the third byte.
1779
1780 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1781 edge_serial->rxStatusCode = IOSP_GET_STATUS_CODE(edge_serial->rxHeader1);
1782
1783 if (!IOSP_STATUS_IS_2BYTE(edge_serial->rxStatusCode)) {
1784 // This status needs additional bytes. Save what we have
1785 // and then wait for more data.
1786 edge_serial->rxStatusParam = edge_serial->rxHeader2;
1787
1788 edge_serial->rxState = EXPECT_HDR3;
1789 break;
1790 }
1791
1792 // We have all the header bytes, process the status now
1793 process_rcvd_status (edge_serial, edge_serial->rxHeader2, 0);
1794 edge_serial->rxState = EXPECT_HDR1;
1795 break;
1796 } else {
1797 edge_serial->rxPort = IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1798 edge_serial->rxBytesRemaining = IOSP_GET_HDR_DATA_LEN(edge_serial->rxHeader1, edge_serial->rxHeader2);
1799
1800 dbg("%s - Data for Port %u Len %u", __FUNCTION__, edge_serial->rxPort, edge_serial->rxBytesRemaining);
1801
1802 //ASSERT( DevExt->RxPort < DevExt->NumPorts );
1803 //ASSERT( DevExt->RxBytesRemaining < IOSP_MAX_DATA_LENGTH );
1804
1805 if (bufferLength == 0 ) {
1806 edge_serial->rxState = EXPECT_DATA;
1807 break;
1808 }
1809 // Else, drop through
1810 }
1811
1812 case EXPECT_DATA: // Expect data
1813
1814 if (bufferLength < edge_serial->rxBytesRemaining) {
1815 rxLen = bufferLength;
1816 edge_serial->rxState = EXPECT_DATA; // Expect data to start next buffer
1817 } else {
1818 // BufLen >= RxBytesRemaining
1819 rxLen = edge_serial->rxBytesRemaining;
1820 edge_serial->rxState = EXPECT_HDR1; // Start another header next time
1821 }
1822
1823 bufferLength -= rxLen;
1824 edge_serial->rxBytesRemaining -= rxLen;
1825
1826 /* spit this data back into the tty driver if this port is open */
1827 if (rxLen) {
1828 port = edge_serial->serial->port[edge_serial->rxPort];
1829 edge_port = usb_get_serial_port_data(port);
1830 if (edge_port->open) {
1831 tty = edge_port->port->tty;
1832 if (tty) {
1833 dbg("%s - Sending %d bytes to TTY for port %d", __FUNCTION__, rxLen, edge_serial->rxPort);
1834 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
1835 }
1836 edge_port->icount.rx += rxLen;
1837 }
1838 buffer += rxLen;
1839 }
1840
1841 break;
1842
1843 case EXPECT_HDR3: // Expect 3rd byte of status header
1844 edge_serial->rxHeader3 = *buffer;
1845 ++buffer;
1846 --bufferLength;
1847
1848 // We have all the header bytes, process the status now
1849 process_rcvd_status (edge_serial, edge_serial->rxStatusParam, edge_serial->rxHeader3);
1850 edge_serial->rxState = EXPECT_HDR1;
1851 break;
1852
1853 }
1854 }
1855 }
1856
1857
1858 /*****************************************************************************
1859 * process_rcvd_status
1860 * this function handles the any status messages received on the bulk in pipe.
1861 *****************************************************************************/
1862 static void process_rcvd_status (struct edgeport_serial *edge_serial, __u8 byte2, __u8 byte3)
1863 {
1864 struct usb_serial_port *port;
1865 struct edgeport_port *edge_port;
1866 __u8 code = edge_serial->rxStatusCode;
1867
1868 /* switch the port pointer to the one being currently talked about */
1869 port = edge_serial->serial->port[edge_serial->rxPort];
1870 edge_port = usb_get_serial_port_data(port);
1871 if (edge_port == NULL) {
1872 dev_err(&edge_serial->serial->dev->dev, "%s - edge_port == NULL for port %d\n", __FUNCTION__, edge_serial->rxPort);
1873 return;
1874 }
1875
1876 dbg("%s - port %d", __FUNCTION__, edge_serial->rxPort);
1877
1878 if (code == IOSP_EXT_STATUS) {
1879 switch (byte2) {
1880 case IOSP_EXT_STATUS_CHASE_RSP:
1881 // we want to do EXT status regardless of port open/closed
1882 dbg("%s - Port %u EXT CHASE_RSP Data = %02x", __FUNCTION__, edge_serial->rxPort, byte3 );
1883 // Currently, the only EXT_STATUS is Chase, so process here instead of one more call
1884 // to one more subroutine. If/when more EXT_STATUS, there'll be more work to do.
1885 // Also, we currently clear flag and close the port regardless of content of above's Byte3.
1886 // We could choose to do something else when Byte3 says Timeout on Chase from Edgeport,
1887 // like wait longer in block_until_chase_response, but for now we don't.
1888 edge_port->chaseResponsePending = FALSE;
1889 wake_up (&edge_port->wait_chase);
1890 return;
1891
1892 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1893 dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __FUNCTION__, edge_serial->rxPort, byte3 );
1894 //Port->RxCheckRsp = TRUE;
1895 return;
1896 }
1897 }
1898
1899 if (code == IOSP_STATUS_OPEN_RSP) {
1900 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1901 edge_port->maxTxCredits = edge_port->txCredits;
1902 dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __FUNCTION__, edge_serial->rxPort, byte2, edge_port->txCredits);
1903 handle_new_msr (edge_port, byte2);
1904
1905 /* send the current line settings to the port so we are in sync with any further termios calls */
1906 if (edge_port->port->tty)
1907 change_port_settings (edge_port, edge_port->port->tty->termios);
1908
1909 /* we have completed the open */
1910 edge_port->openPending = FALSE;
1911 edge_port->open = TRUE;
1912 wake_up(&edge_port->wait_open);
1913 return;
1914 }
1915
1916 // If port is closed, silently discard all rcvd status. We can
1917 // have cases where buffered status is received AFTER the close
1918 // port command is sent to the Edgeport.
1919 if ((!edge_port->open ) || (edge_port->closePending)) {
1920 return;
1921 }
1922
1923 switch (code) {
1924 // Not currently sent by Edgeport
1925 case IOSP_STATUS_LSR:
1926 dbg("%s - Port %u LSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1927 handle_new_lsr (edge_port, FALSE, byte2, 0);
1928 break;
1929
1930 case IOSP_STATUS_LSR_DATA:
1931 dbg("%s - Port %u LSR Status = %02x, Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1932 // byte2 is LSR Register
1933 // byte3 is broken data byte
1934 handle_new_lsr (edge_port, TRUE, byte2, byte3);
1935 break;
1936 //
1937 // case IOSP_EXT_4_STATUS:
1938 // dbg("%s - Port %u LSR Status = %02x Data = %02x", __FUNCTION__, edge_serial->rxPort, byte2, byte3);
1939 // break;
1940 //
1941 case IOSP_STATUS_MSR:
1942 dbg("%s - Port %u MSR Status = %02x", __FUNCTION__, edge_serial->rxPort, byte2);
1943
1944 // Process this new modem status and generate appropriate
1945 // events, etc, based on the new status. This routine
1946 // also saves the MSR in Port->ShadowMsr.
1947 handle_new_msr(edge_port, byte2);
1948 break;
1949
1950 default:
1951 dbg("%s - Unrecognized IOSP status code %u\n", __FUNCTION__, code);
1952 break;
1953 }
1954
1955 return;
1956 }
1957
1958
1959 /*****************************************************************************
1960 * edge_tty_recv
1961 * this function passes data on to the tty flip buffer
1962 *****************************************************************************/
1963 static void edge_tty_recv(struct device *dev, struct tty_struct *tty, unsigned char *data, int length)
1964 {
1965 int cnt;
1966
1967 do {
1968 cnt = tty_buffer_request_room(tty, length);
1969 if (cnt < length) {
1970 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1971 __FUNCTION__, length - cnt);
1972 if(cnt == 0)
1973 break;
1974 }
1975 tty_insert_flip_string(tty, data, cnt);
1976 data += cnt;
1977 length -= cnt;
1978 } while (length > 0);
1979
1980 tty_flip_buffer_push(tty);
1981 }
1982
1983
1984 /*****************************************************************************
1985 * handle_new_msr
1986 * this function handles any change to the msr register for a port.
1987 *****************************************************************************/
1988 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1989 {
1990 struct async_icount *icount;
1991
1992 dbg("%s %02x", __FUNCTION__, newMsr);
1993
1994 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
1995 icount = &edge_port->icount;
1996
1997 /* update input line counters */
1998 if (newMsr & EDGEPORT_MSR_DELTA_CTS) {
1999 icount->cts++;
2000 }
2001 if (newMsr & EDGEPORT_MSR_DELTA_DSR) {
2002 icount->dsr++;
2003 }
2004 if (newMsr & EDGEPORT_MSR_DELTA_CD) {
2005 icount->dcd++;
2006 }
2007 if (newMsr & EDGEPORT_MSR_DELTA_RI) {
2008 icount->rng++;
2009 }
2010 wake_up_interruptible(&edge_port->delta_msr_wait);
2011 }
2012
2013 /* Save the new modem status */
2014 edge_port->shadowMSR = newMsr & 0xf0;
2015
2016 return;
2017 }
2018
2019
2020 /*****************************************************************************
2021 * handle_new_lsr
2022 * this function handles any change to the lsr register for a port.
2023 *****************************************************************************/
2024 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, __u8 lsr, __u8 data)
2025 {
2026 __u8 newLsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2027 struct async_icount *icount;
2028
2029 dbg("%s - %02x", __FUNCTION__, newLsr);
2030
2031 edge_port->shadowLSR = lsr;
2032
2033 if (newLsr & LSR_BREAK) {
2034 //
2035 // Parity and Framing errors only count if they
2036 // occur exclusive of a break being
2037 // received.
2038 //
2039 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2040 }
2041
2042 /* Place LSR data byte into Rx buffer */
2043 if (lsrData && edge_port->port->tty)
2044 edge_tty_recv(&edge_port->port->dev, edge_port->port->tty, &data, 1);
2045
2046 /* update input line counters */
2047 icount = &edge_port->icount;
2048 if (newLsr & LSR_BREAK) {
2049 icount->brk++;
2050 }
2051 if (newLsr & LSR_OVER_ERR) {
2052 icount->overrun++;
2053 }
2054 if (newLsr & LSR_PAR_ERR) {
2055 icount->parity++;
2056 }
2057 if (newLsr & LSR_FRM_ERR) {
2058 icount->frame++;
2059 }
2060
2061 return;
2062 }
2063
2064
2065 /****************************************************************************
2066 * sram_write
2067 * writes a number of bytes to the Edgeport device's sram starting at the
2068 * given address.
2069 * If successful returns the number of bytes written, otherwise it returns
2070 * a negative error number of the problem.
2071 ****************************************************************************/
2072 static int sram_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2073 {
2074 int result;
2075 __u16 current_length;
2076 unsigned char *transfer_buffer;
2077
2078 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2079
2080 transfer_buffer = kmalloc (64, GFP_KERNEL);
2081 if (!transfer_buffer) {
2082 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2083 return -ENOMEM;
2084 }
2085
2086 /* need to split these writes up into 64 byte chunks */
2087 result = 0;
2088 while (length > 0) {
2089 if (length > 64) {
2090 current_length = 64;
2091 } else {
2092 current_length = length;
2093 }
2094 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2095 memcpy (transfer_buffer, data, current_length);
2096 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_RAM,
2097 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2098 if (result < 0)
2099 break;
2100 length -= current_length;
2101 addr += current_length;
2102 data += current_length;
2103 }
2104
2105 kfree (transfer_buffer);
2106 return result;
2107 }
2108
2109
2110 /****************************************************************************
2111 * rom_write
2112 * writes a number of bytes to the Edgeport device's ROM starting at the
2113 * given address.
2114 * If successful returns the number of bytes written, otherwise it returns
2115 * a negative error number of the problem.
2116 ****************************************************************************/
2117 static int rom_write (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2118 {
2119 int result;
2120 __u16 current_length;
2121 unsigned char *transfer_buffer;
2122
2123 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2124
2125 transfer_buffer = kmalloc (64, GFP_KERNEL);
2126 if (!transfer_buffer) {
2127 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2128 return -ENOMEM;
2129 }
2130
2131 /* need to split these writes up into 64 byte chunks */
2132 result = 0;
2133 while (length > 0) {
2134 if (length > 64) {
2135 current_length = 64;
2136 } else {
2137 current_length = length;
2138 }
2139 // dbg("%s - writing %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2140 memcpy (transfer_buffer, data, current_length);
2141 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), USB_REQUEST_ION_WRITE_ROM,
2142 0x40, addr, extAddr, transfer_buffer, current_length, 300);
2143 if (result < 0)
2144 break;
2145 length -= current_length;
2146 addr += current_length;
2147 data += current_length;
2148 }
2149
2150 kfree (transfer_buffer);
2151 return result;
2152 }
2153
2154
2155 /****************************************************************************
2156 * rom_read
2157 * reads a number of bytes from the Edgeport device starting at the given
2158 * address.
2159 * If successful returns the number of bytes read, otherwise it returns
2160 * a negative error number of the problem.
2161 ****************************************************************************/
2162 static int rom_read (struct usb_serial *serial, __u16 extAddr, __u16 addr, __u16 length, __u8 *data)
2163 {
2164 int result;
2165 __u16 current_length;
2166 unsigned char *transfer_buffer;
2167
2168 dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, length);
2169
2170 transfer_buffer = kmalloc (64, GFP_KERNEL);
2171 if (!transfer_buffer) {
2172 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 64);
2173 return -ENOMEM;
2174 }
2175
2176 /* need to split these reads up into 64 byte chunks */
2177 result = 0;
2178 while (length > 0) {
2179 if (length > 64) {
2180 current_length = 64;
2181 } else {
2182 current_length = length;
2183 }
2184 // dbg("%s - %x, %x, %d", __FUNCTION__, extAddr, addr, current_length);
2185 result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), USB_REQUEST_ION_READ_ROM,
2186 0xC0, addr, extAddr, transfer_buffer, current_length, 300);
2187 if (result < 0)
2188 break;
2189 memcpy (data, transfer_buffer, current_length);
2190 length -= current_length;
2191 addr += current_length;
2192 data += current_length;
2193 }
2194
2195 kfree (transfer_buffer);
2196 return result;
2197 }
2198
2199
2200 /****************************************************************************
2201 * send_iosp_ext_cmd
2202 * Is used to send a IOSP message to the Edgeport device
2203 ****************************************************************************/
2204 static int send_iosp_ext_cmd (struct edgeport_port *edge_port, __u8 command, __u8 param)
2205 {
2206 unsigned char *buffer;
2207 unsigned char *currentCommand;
2208 int length = 0;
2209 int status = 0;
2210
2211 dbg("%s - %d, %d", __FUNCTION__, command, param);
2212
2213 buffer = kmalloc (10, GFP_ATOMIC);
2214 if (!buffer) {
2215 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 10);
2216 return -ENOMEM;
2217 }
2218
2219 currentCommand = buffer;
2220
2221 MAKE_CMD_EXT_CMD (&currentCommand, &length,
2222 edge_port->port->number - edge_port->port->serial->minor,
2223 command, param);
2224
2225 status = write_cmd_usb (edge_port, buffer, length);
2226 if (status) {
2227 /* something bad happened, let's free up the memory */
2228 kfree(buffer);
2229 }
2230
2231 return status;
2232 }
2233
2234
2235 /*****************************************************************************
2236 * write_cmd_usb
2237 * this function writes the given buffer out to the bulk write endpoint.
2238 *****************************************************************************/
2239 static int write_cmd_usb (struct edgeport_port *edge_port, unsigned char *buffer, int length)
2240 {
2241 struct edgeport_serial *edge_serial = usb_get_serial_data(edge_port->port->serial);
2242 int status = 0;
2243 struct urb *urb;
2244 int timeout;
2245
2246 usb_serial_debug_data(debug, &edge_port->port->dev, __FUNCTION__, length, buffer);
2247
2248 /* Allocate our next urb */
2249 urb = usb_alloc_urb (0, GFP_ATOMIC);
2250 if (!urb)
2251 return -ENOMEM;
2252
2253 CmdUrbs++;
2254 dbg("%s - ALLOCATE URB %p (outstanding %d)", __FUNCTION__, urb, CmdUrbs);
2255
2256 usb_fill_bulk_urb (urb, edge_serial->serial->dev,
2257 usb_sndbulkpipe(edge_serial->serial->dev, edge_serial->bulk_out_endpoint),
2258 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2259
2260 edge_port->commandPending = TRUE;
2261 status = usb_submit_urb(urb, GFP_ATOMIC);
2262
2263 if (status) {
2264 /* something went wrong */
2265 dev_err(&edge_port->port->dev, "%s - usb_submit_urb(write command) failed, status = %d\n", __FUNCTION__, status);
2266 usb_kill_urb(urb);
2267 usb_free_urb(urb);
2268 CmdUrbs--;
2269 return status;
2270 }
2271
2272 // wait for command to finish
2273 timeout = COMMAND_TIMEOUT;
2274 #if 0
2275 wait_event (&edge_port->wait_command, (edge_port->commandPending == FALSE));
2276
2277 if (edge_port->commandPending == TRUE) {
2278 /* command timed out */
2279 dbg("%s - command timed out", __FUNCTION__);
2280 status = -EINVAL;
2281 }
2282 #endif
2283 return status;
2284 }
2285
2286
2287 /*****************************************************************************
2288 * send_cmd_write_baud_rate
2289 * this function sends the proper command to change the baud rate of the
2290 * specified port.
2291 *****************************************************************************/
2292 static int send_cmd_write_baud_rate (struct edgeport_port *edge_port, int baudRate)
2293 {
2294 unsigned char *cmdBuffer;
2295 unsigned char *currCmd;
2296 int cmdLen = 0;
2297 int divisor;
2298 int status;
2299 unsigned char number = edge_port->port->number - edge_port->port->serial->minor;
2300
2301 dbg("%s - port = %d, baud = %d", __FUNCTION__, edge_port->port->number, baudRate);
2302
2303 status = calc_baud_rate_divisor (baudRate, &divisor);
2304 if (status) {
2305 dev_err(&edge_port->port->dev, "%s - bad baud rate\n", __FUNCTION__);
2306 return status;
2307 }
2308
2309 // Alloc memory for the string of commands.
2310 cmdBuffer = kmalloc (0x100, GFP_ATOMIC);
2311 if (!cmdBuffer) {
2312 dev_err(&edge_port->port->dev, "%s - kmalloc(%d) failed.\n", __FUNCTION__, 0x100);
2313 return -ENOMEM;
2314 }
2315 currCmd = cmdBuffer;
2316
2317 // Enable access to divisor latch
2318 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE );
2319
2320 // Write the divisor itself
2321 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLL, LOW8 (divisor) );
2322 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, DLM, HIGH8(divisor) );
2323
2324 // Restore original value to disable access to divisor latch
2325 MAKE_CMD_WRITE_REG( &currCmd, &cmdLen, number, LCR, edge_port->shadowLCR);
2326
2327 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen );
2328 if (status) {
2329 /* something bad happened, let's free up the memory */
2330 kfree (cmdBuffer);
2331 }
2332
2333 return status;
2334 }
2335
2336
2337 /*****************************************************************************
2338 * calc_baud_rate_divisor
2339 * this function calculates the proper baud rate divisor for the specified
2340 * baud rate.
2341 *****************************************************************************/
2342 static int calc_baud_rate_divisor (int baudrate, int *divisor)
2343 {
2344 int i;
2345 __u16 custom;
2346
2347
2348 dbg("%s - %d", __FUNCTION__, baudrate);
2349
2350 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2351 if ( divisor_table[i].BaudRate == baudrate ) {
2352 *divisor = divisor_table[i].Divisor;
2353 return 0;
2354 }
2355 }
2356
2357 // We have tried all of the standard baud rates
2358 // lets try to calculate the divisor for this baud rate
2359 // Make sure the baud rate is reasonable
2360 if (baudrate > 50 && baudrate < 230400) {
2361 // get divisor
2362 custom = (__u16)((230400L + baudrate/2) / baudrate);
2363
2364 *divisor = custom;
2365
2366 dbg("%s - Baud %d = %d\n", __FUNCTION__, baudrate, custom);
2367 return 0;
2368 }
2369
2370 return -1;
2371 }
2372
2373
2374 /*****************************************************************************
2375 * send_cmd_write_uart_register
2376 * this function builds up a uart register message and sends to to the device.
2377 *****************************************************************************/
2378 static int send_cmd_write_uart_register (struct edgeport_port *edge_port, __u8 regNum, __u8 regValue)
2379 {
2380 unsigned char *cmdBuffer;
2381 unsigned char *currCmd;
2382 unsigned long cmdLen = 0;
2383 int status;
2384
2385 dbg("%s - write to %s register 0x%02x", (regNum == MCR) ? "MCR" : "LCR", __FUNCTION__, regValue);
2386
2387 // Alloc memory for the string of commands.
2388 cmdBuffer = kmalloc (0x10, GFP_ATOMIC);
2389 if (cmdBuffer == NULL ) {
2390 return -ENOMEM;
2391 }
2392
2393 currCmd = cmdBuffer;
2394
2395 // Build a cmd in the buffer to write the given register
2396 MAKE_CMD_WRITE_REG (&currCmd, &cmdLen,
2397 edge_port->port->number - edge_port->port->serial->minor,
2398 regNum, regValue);
2399
2400 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2401 if (status) {
2402 /* something bad happened, let's free up the memory */
2403 kfree (cmdBuffer);
2404 }
2405
2406 return status;
2407 }
2408
2409
2410 /*****************************************************************************
2411 * change_port_settings
2412 * This routine is called to set the UART on the device to match the specified
2413 * new settings.
2414 *****************************************************************************/
2415 #ifndef CMSPAR
2416 #define CMSPAR 0
2417 #endif
2418 static void change_port_settings (struct edgeport_port *edge_port, struct termios *old_termios)
2419 {
2420 struct tty_struct *tty;
2421 int baud;
2422 unsigned cflag;
2423 __u8 mask = 0xff;
2424 __u8 lData;
2425 __u8 lParity;
2426 __u8 lStop;
2427 __u8 rxFlow;
2428 __u8 txFlow;
2429 int status;
2430
2431 dbg("%s - port %d", __FUNCTION__, edge_port->port->number);
2432
2433 if ((!edge_port->open) &&
2434 (!edge_port->openPending)) {
2435 dbg("%s - port not opened", __FUNCTION__);
2436 return;
2437 }
2438
2439 tty = edge_port->port->tty;
2440 if ((!tty) ||
2441 (!tty->termios)) {
2442 dbg("%s - no tty structures", __FUNCTION__);
2443 return;
2444 }
2445
2446 cflag = tty->termios->c_cflag;
2447
2448 switch (cflag & CSIZE) {
2449 case CS5: lData = LCR_BITS_5; mask = 0x1f; dbg("%s - data bits = 5", __FUNCTION__); break;
2450 case CS6: lData = LCR_BITS_6; mask = 0x3f; dbg("%s - data bits = 6", __FUNCTION__); break;
2451 case CS7: lData = LCR_BITS_7; mask = 0x7f; dbg("%s - data bits = 7", __FUNCTION__); break;
2452 default:
2453 case CS8: lData = LCR_BITS_8; dbg("%s - data bits = 8", __FUNCTION__); break;
2454 }
2455
2456 lParity = LCR_PAR_NONE;
2457 if (cflag & PARENB) {
2458 if (cflag & CMSPAR) {
2459 if (cflag & PARODD) {
2460 lParity = LCR_PAR_MARK;
2461 dbg("%s - parity = mark", __FUNCTION__);
2462 } else {
2463 lParity = LCR_PAR_SPACE;
2464 dbg("%s - parity = space", __FUNCTION__);
2465 }
2466 } else if (cflag & PARODD) {
2467 lParity = LCR_PAR_ODD;
2468 dbg("%s - parity = odd", __FUNCTION__);
2469 } else {
2470 lParity = LCR_PAR_EVEN;
2471 dbg("%s - parity = even", __FUNCTION__);
2472 }
2473 } else {
2474 dbg("%s - parity = none", __FUNCTION__);
2475 }
2476
2477 if (cflag & CSTOPB) {
2478 lStop = LCR_STOP_2;
2479 dbg("%s - stop bits = 2", __FUNCTION__);
2480 } else {
2481 lStop = LCR_STOP_1;
2482 dbg("%s - stop bits = 1", __FUNCTION__);
2483 }
2484
2485 /* figure out the flow control settings */
2486 rxFlow = txFlow = 0x00;
2487 if (cflag & CRTSCTS) {
2488 rxFlow |= IOSP_RX_FLOW_RTS;
2489 txFlow |= IOSP_TX_FLOW_CTS;
2490 dbg("%s - RTS/CTS is enabled", __FUNCTION__);
2491 } else {
2492 dbg("%s - RTS/CTS is disabled", __FUNCTION__);
2493 }
2494
2495 /* if we are implementing XON/XOFF, set the start and stop character in the device */
2496 if (I_IXOFF(tty) || I_IXON(tty)) {
2497 unsigned char stop_char = STOP_CHAR(tty);
2498 unsigned char start_char = START_CHAR(tty);
2499
2500 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XON_CHAR, start_char);
2501 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_XOFF_CHAR, stop_char);
2502
2503 /* if we are implementing INBOUND XON/XOFF */
2504 if (I_IXOFF(tty)) {
2505 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2506 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2507 } else {
2508 dbg("%s - INBOUND XON/XOFF is disabled", __FUNCTION__);
2509 }
2510
2511 /* if we are implementing OUTBOUND XON/XOFF */
2512 if (I_IXON(tty)) {
2513 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2514 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x", __FUNCTION__, start_char, stop_char);
2515 } else {
2516 dbg("%s - OUTBOUND XON/XOFF is disabled", __FUNCTION__);
2517 }
2518 }
2519
2520 /* Set flow control to the configured value */
2521 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2522 send_iosp_ext_cmd (edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2523
2524
2525 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2526 edge_port->shadowLCR |= (lData | lParity | lStop);
2527
2528 edge_port->validDataMask = mask;
2529
2530 /* Send the updated LCR value to the EdgePort */
2531 status = send_cmd_write_uart_register(edge_port, LCR, edge_port->shadowLCR);
2532 if (status != 0) {
2533 return;
2534 }
2535
2536 /* set up the MCR register and send it to the EdgePort */
2537 edge_port->shadowMCR = MCR_MASTER_IE;
2538 if (cflag & CBAUD) {
2539 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2540 }
2541 status = send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
2542 if (status != 0) {
2543 return;
2544 }
2545
2546 /* Determine divisor based on baud rate */
2547 baud = tty_get_baud_rate(tty);
2548 if (!baud) {
2549 /* pick a default, any default... */
2550 baud = 9600;
2551 }
2552
2553 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2554 status = send_cmd_write_baud_rate (edge_port, baud);
2555
2556 return;
2557 }
2558
2559
2560 /****************************************************************************
2561 * unicode_to_ascii
2562 * Turns a string from Unicode into ASCII.
2563 * Doesn't do a good job with any characters that are outside the normal
2564 * ASCII range, but it's only for debugging...
2565 * NOTE: expects the unicode in LE format
2566 ****************************************************************************/
2567 static void unicode_to_ascii (char *string, __le16 *unicode, int unicode_size)
2568 {
2569 int i;
2570
2571 if (unicode_size <= 0)
2572 return;
2573
2574 for (i = 0; i < unicode_size; ++i)
2575 string[i] = (char)(le16_to_cpu(unicode[i]));
2576 string[unicode_size] = 0x00;
2577 }
2578
2579
2580 /****************************************************************************
2581 * get_manufacturing_desc
2582 * reads in the manufacturing descriptor and stores it into the serial
2583 * structure.
2584 ****************************************************************************/
2585 static void get_manufacturing_desc (struct edgeport_serial *edge_serial)
2586 {
2587 int response;
2588
2589 dbg("getting manufacturer descriptor");
2590
2591 response = rom_read (edge_serial->serial, (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2592 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), EDGE_MANUF_DESC_LEN,
2593 (__u8 *)(&edge_serial->manuf_descriptor));
2594
2595 if (response < 1) {
2596 dev_err(&edge_serial->serial->dev->dev, "error in getting manufacturer descriptor\n");
2597 } else {
2598 char string[30];
2599 dbg("**Manufacturer Descriptor");
2600 dbg(" RomSize: %dK", edge_serial->manuf_descriptor.RomSize);
2601 dbg(" RamSize: %dK", edge_serial->manuf_descriptor.RamSize);
2602 dbg(" CpuRev: %d", edge_serial->manuf_descriptor.CpuRev);
2603 dbg(" BoardRev: %d", edge_serial->manuf_descriptor.BoardRev);
2604 dbg(" NumPorts: %d", edge_serial->manuf_descriptor.NumPorts);
2605 dbg(" DescDate: %d/%d/%d", edge_serial->manuf_descriptor.DescDate[0], edge_serial->manuf_descriptor.DescDate[1], edge_serial->manuf_descriptor.DescDate[2]+1900);
2606 unicode_to_ascii (string, edge_serial->manuf_descriptor.SerialNumber, edge_serial->manuf_descriptor.SerNumLength/2-1);
2607 dbg(" SerialNumber: %s", string);
2608 unicode_to_ascii (string, edge_serial->manuf_descriptor.AssemblyNumber, edge_serial->manuf_descriptor.AssemblyNumLength/2-1);
2609 dbg(" AssemblyNumber: %s", string);
2610 unicode_to_ascii (string, edge_serial->manuf_descriptor.OemAssyNumber, edge_serial->manuf_descriptor.OemAssyNumLength/2-1);
2611 dbg(" OemAssyNumber: %s", string);
2612 dbg(" UartType: %d", edge_serial->manuf_descriptor.UartType);
2613 dbg(" IonPid: %d", edge_serial->manuf_descriptor.IonPid);
2614 dbg(" IonConfig: %d", edge_serial->manuf_descriptor.IonConfig);
2615 }
2616 }
2617
2618
2619 /****************************************************************************
2620 * get_boot_desc
2621 * reads in the bootloader descriptor and stores it into the serial
2622 * structure.
2623 ****************************************************************************/
2624 static void get_boot_desc (struct edgeport_serial *edge_serial)
2625 {
2626 int response;
2627
2628 dbg("getting boot descriptor");
2629
2630 response = rom_read (edge_serial->serial, (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2631 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), EDGE_BOOT_DESC_LEN,
2632 (__u8 *)(&edge_serial->boot_descriptor));
2633
2634 if (response < 1) {
2635 dev_err(&edge_serial->serial->dev->dev, "error in getting boot descriptor\n");
2636 } else {
2637 dbg("**Boot Descriptor:");
2638 dbg(" BootCodeLength: %d", le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2639 dbg(" MajorVersion: %d", edge_serial->boot_descriptor.MajorVersion);
2640 dbg(" MinorVersion: %d", edge_serial->boot_descriptor.MinorVersion);
2641 dbg(" BuildNumber: %d", le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2642 dbg(" Capabilities: 0x%x", le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2643 dbg(" UConfig0: %d", edge_serial->boot_descriptor.UConfig0);
2644 dbg(" UConfig1: %d", edge_serial->boot_descriptor.UConfig1);
2645 }
2646 }
2647
2648
2649 /****************************************************************************
2650 * load_application_firmware
2651 * This is called to load the application firmware to the device
2652 ****************************************************************************/
2653 static void load_application_firmware (struct edgeport_serial *edge_serial)
2654 {
2655 struct edge_firmware_image_record *record;
2656 unsigned char *firmware;
2657 unsigned char *FirmwareImage;
2658 int ImageSize;
2659 int response;
2660
2661
2662 switch (edge_serial->product_info.iDownloadFile) {
2663 case EDGE_DOWNLOAD_FILE_I930:
2664 dbg("downloading firmware version (930) %d.%d.%d",
2665 OperationalCodeImageVersion_GEN1.MajorVersion,
2666 OperationalCodeImageVersion_GEN1.MinorVersion,
2667 OperationalCodeImageVersion_GEN1.BuildNumber);
2668 firmware = &OperationalCodeImage_GEN1[0];
2669 FirmwareImage = &OperationalCodeImage_GEN1[0];
2670 ImageSize = sizeof(OperationalCodeImage_GEN1);
2671 break;
2672
2673 case EDGE_DOWNLOAD_FILE_80251:
2674 dbg("downloading firmware version (80251) %d.%d.%d",
2675 OperationalCodeImageVersion_GEN2.MajorVersion,
2676 OperationalCodeImageVersion_GEN2.MinorVersion,
2677 OperationalCodeImageVersion_GEN2.BuildNumber);
2678 firmware = &OperationalCodeImage_GEN2[0];
2679 FirmwareImage = &OperationalCodeImage_GEN2[0];
2680 ImageSize = sizeof(OperationalCodeImage_GEN2);
2681 break;
2682
2683 case EDGE_DOWNLOAD_FILE_NONE:
2684 dbg ("No download file specified, skipping download\n");
2685 return;
2686
2687 default:
2688 return;
2689 }
2690
2691
2692 for (;;) {
2693 record = (struct edge_firmware_image_record *)firmware;
2694 response = sram_write (edge_serial->serial, le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len), &record->Data[0]);
2695 if (response < 0) {
2696 dev_err(&edge_serial->serial->dev->dev, "sram_write failed (%x, %x, %d)\n", le16_to_cpu(record->ExtAddr), le16_to_cpu(record->Addr), le16_to_cpu(record->Len));
2697 break;
2698 }
2699 firmware += sizeof (struct edge_firmware_image_record) + le16_to_cpu(record->Len);
2700 if (firmware >= &FirmwareImage[ImageSize]) {
2701 break;
2702 }
2703 }
2704
2705 dbg("sending exec_dl_code");
2706 response = usb_control_msg (edge_serial->serial->dev,
2707 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2708 USB_REQUEST_ION_EXEC_DL_CODE,
2709 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2710
2711 return;
2712 }
2713
2714
2715 /****************************************************************************
2716 * edge_startup
2717 ****************************************************************************/
2718 static int edge_startup (struct usb_serial *serial)
2719 {
2720 struct edgeport_serial *edge_serial;
2721 struct edgeport_port *edge_port;
2722 struct usb_device *dev;
2723 int i;
2724
2725 dev = serial->dev;
2726
2727 /* create our private serial structure */
2728 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL);
2729 if (edge_serial == NULL) {
2730 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2731 return -ENOMEM;
2732 }
2733 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2734 spin_lock_init(&edge_serial->es_lock);
2735 edge_serial->serial = serial;
2736 usb_set_serial_data(serial, edge_serial);
2737
2738 /* get the name for the device from the device */
2739 if ( (i = get_string(dev, dev->descriptor.iManufacturer, &edge_serial->name[0])) != 0) {
2740 edge_serial->name[i-1] = ' ';
2741 }
2742
2743 get_string(dev, dev->descriptor.iProduct, &edge_serial->name[i]);
2744
2745 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2746
2747 /* get the manufacturing descriptor for this device */
2748 get_manufacturing_desc (edge_serial);
2749
2750 /* get the boot descriptor */
2751 get_boot_desc (edge_serial);
2752
2753 get_product_info(edge_serial);
2754
2755 /* set the number of ports from the manufacturing description */
2756 /* serial->num_ports = serial->product_info.NumPorts; */
2757 if (edge_serial->product_info.NumPorts != serial->num_ports) {
2758 warn("%s - Device Reported %d serial ports vs core "
2759 "thinking we have %d ports, email greg@kroah.com this info.",
2760 __FUNCTION__, edge_serial->product_info.NumPorts,
2761 serial->num_ports);
2762 }
2763
2764 dbg("%s - time 1 %ld", __FUNCTION__, jiffies);
2765
2766 /* now load the application firmware into this device */
2767 load_application_firmware (edge_serial);
2768
2769 dbg("%s - time 2 %ld", __FUNCTION__, jiffies);
2770
2771 /* Check current Edgeport EEPROM and update if necessary */
2772 update_edgeport_E2PROM (edge_serial);
2773
2774 dbg("%s - time 3 %ld", __FUNCTION__, jiffies);
2775
2776 /* set the configuration to use #1 */
2777 // dbg("set_configuration 1");
2778 // usb_set_configuration (dev, 1);
2779
2780 /* we set up the pointers to the endpoints in the edge_open function,
2781 * as the structures aren't created yet. */
2782
2783 /* set up our port private structures */
2784 for (i = 0; i < serial->num_ports; ++i) {
2785 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL);
2786 if (edge_port == NULL) {
2787 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2788 usb_set_serial_data(serial, NULL);
2789 kfree(edge_serial);
2790 return -ENOMEM;
2791 }
2792 memset (edge_port, 0, sizeof(struct edgeport_port));
2793 spin_lock_init(&edge_port->ep_lock);
2794 edge_port->port = serial->port[i];
2795 usb_set_serial_port_data(serial->port[i], edge_port);
2796 }
2797
2798 return 0;
2799 }
2800
2801
2802 /****************************************************************************
2803 * edge_shutdown
2804 * This function is called whenever the device is removed from the usb bus.
2805 ****************************************************************************/
2806 static void edge_shutdown (struct usb_serial *serial)
2807 {
2808 int i;
2809
2810 dbg("%s", __FUNCTION__);
2811
2812 /* stop reads and writes on all ports */
2813 for (i=0; i < serial->num_ports; ++i) {
2814 kfree (usb_get_serial_port_data(serial->port[i]));
2815 usb_set_serial_port_data(serial->port[i], NULL);
2816 }
2817 kfree (usb_get_serial_data(serial));
2818 usb_set_serial_data(serial, NULL);
2819 }
2820
2821
2822 /****************************************************************************
2823 * edgeport_init
2824 * This is called by the module subsystem, or on startup to initialize us
2825 ****************************************************************************/
2826 static int __init edgeport_init(void)
2827 {
2828 int retval;
2829
2830 retval = usb_serial_register(&edgeport_2port_device);
2831 if (retval)
2832 goto failed_2port_device_register;
2833 retval = usb_serial_register(&edgeport_4port_device);
2834 if (retval)
2835 goto failed_4port_device_register;
2836 retval = usb_serial_register(&edgeport_8port_device);
2837 if (retval)
2838 goto failed_8port_device_register;
2839 retval = usb_register(&io_driver);
2840 if (retval)
2841 goto failed_usb_register;
2842 info(DRIVER_DESC " " DRIVER_VERSION);
2843 return 0;
2844
2845 failed_usb_register:
2846 usb_serial_deregister(&edgeport_8port_device);
2847 failed_8port_device_register:
2848 usb_serial_deregister(&edgeport_4port_device);
2849 failed_4port_device_register:
2850 usb_serial_deregister(&edgeport_2port_device);
2851 failed_2port_device_register:
2852 return retval;
2853 }
2854
2855
2856 /****************************************************************************
2857 * edgeport_exit
2858 * Called when the driver is about to be unloaded.
2859 ****************************************************************************/
2860 static void __exit edgeport_exit (void)
2861 {
2862 usb_deregister (&io_driver);
2863 usb_serial_deregister (&edgeport_2port_device);
2864 usb_serial_deregister (&edgeport_4port_device);
2865 usb_serial_deregister (&edgeport_8port_device);
2866 }
2867
2868 module_init(edgeport_init);
2869 module_exit(edgeport_exit);
2870
2871 /* Module information */
2872 MODULE_AUTHOR( DRIVER_AUTHOR );
2873 MODULE_DESCRIPTION( DRIVER_DESC );
2874 MODULE_LICENSE("GPL");
2875
2876 module_param(debug, bool, S_IRUGO | S_IWUSR);
2877 MODULE_PARM_DESC(debug, "Debug enabled or not");
2878
2879 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
2880 MODULE_PARM_DESC(low_latency, "Low latency enabled or not");
This page took 0.107748 seconds and 5 git commands to generate.