io_edgeport.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:96k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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