io_edgeport.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:96k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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