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