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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.   Keyspan USB to Serial Converter driver
  3.  
  4.   (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
  5.   (C) Copyright (C) 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.   See http://misc.nu/hugh/keyspan.html for more information.
  12.   
  13.   Code in this driver inspired by and in a number of places taken
  14.   from Brian Warner's original Keyspan-PDA driver.
  15.   This driver has been put together with the support of Innosys, Inc.
  16.   and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  17.   Thanks Guys :)
  18.   
  19.   Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  20.   of much nicer and/or completely new code and (perhaps most uniquely)
  21.   having the patience to sit down and explain why and where he'd changed
  22.   stuff. 
  23.   
  24.   Tip 'o the hat to IBM (and previously Linuxcare :) for supporting 
  25.   staff in their work on open source projects.
  26.   Change History
  27.     Wed Apr 25 12:00:00 PST 2002 (Keyspan)
  28.       Started with Hugh Blemings' code dated Jan 17, 2002.  All adapters
  29.       now supported (including QI and QW).  Modified port open, port
  30.       close, and send setup() logic to fix various data and endpoint
  31.       synchronization bugs and device LED status bugs.  Changed keyspan_
  32.       write_room() to accurately return transmit buffer availability.
  33.       Changed forwardingLength from 1 to 16 for all adapters.
  34.     Fri Oct 12 16:45:00 EST 2001
  35.       Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
  36.     Mon Oct  8 14:29:00 EST 2001 hugh
  37.       Fixed bug that prevented mulitport devices operating correctly
  38.       if they weren't the first unit attached.
  39.     Sat Oct  6 12:31:21 EST 2001 hugh
  40.       Added support for USA-28XA and -28XB, misc cleanups, break support
  41.       for usa26 based models thanks to David Gibson.
  42.     Thu May 31 11:56:42 PDT 2001 gkh
  43.       switched from using spinlock to a semaphore
  44.    
  45.     (04/08/2001) gb
  46. Identify version on module load.
  47.    
  48.     (11/01/2000) Adam J. Richter
  49. usb_device_id table support.
  50.    
  51.     Tue Oct 10 23:15:33 EST 2000 Hugh
  52.       Merged Paul's changes with my USA-49W mods.  Work in progress
  53.       still...
  54.   
  55.     Wed Jul 19 14:00:42 EST 2000 gkh
  56.       Added module_init and module_exit functions to handle the fact that
  57.       this driver is a loadable module now.
  58.  
  59.     Tue Jul 18 16:14:52 EST 2000 Hugh
  60.       Basic character input/output for USA-19 now mostly works,
  61.       fixed at 9600 baud for the moment.
  62.     Sat Jul  8 11:11:48 EST 2000 Hugh
  63.       First public release - nothing works except the firmware upload.
  64.       Tested on PPC and x86 architectures, seems to behave...
  65. */
  66. #include <linux/config.h>
  67. #include <linux/kernel.h>
  68. #include <linux/errno.h>
  69. #include <linux/init.h>
  70. #include <linux/slab.h>
  71. #include <linux/tty.h>
  72. #include <linux/tty_driver.h>
  73. #include <linux/tty_flip.h>
  74. #include <linux/module.h>
  75. #include <linux/spinlock.h>
  76. #include <asm/uaccess.h>
  77. #ifdef CONFIG_USB_SERIAL_DEBUG
  78. static int debug = 1;
  79. #define DEBUG
  80. #else
  81. static int debug;
  82. #undef DEBUG
  83. #endif
  84. #include <linux/usb.h>
  85. #include "usb-serial.h"
  86. #include "keyspan.h"
  87. /*
  88.  * Version Information
  89.  */
  90. #define DRIVER_VERSION "v1.1.3"
  91. #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
  92. #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
  93. #define INSTAT_BUFLEN 32
  94. #define GLOCONT_BUFLEN 64
  95. /* Per device and per port private data */
  96. struct keyspan_serial_private {
  97. /* number of active ports */
  98. atomic_t active_count;
  99. const struct keyspan_device_details *device_details;
  100. struct urb *instat_urb;
  101. char instat_buf[INSTAT_BUFLEN];
  102. /* XXX this one probably will need a lock */
  103. struct urb *glocont_urb;
  104. char glocont_buf[GLOCONT_BUFLEN];
  105. };
  106. struct keyspan_port_private {
  107. /* Keep track of which input & output endpoints to use */
  108. int in_flip;
  109. int out_flip;
  110. /* Keep duplicate of device details in each port
  111.    structure as well - simplifies some of the
  112.    callback functions etc. */
  113. const struct keyspan_device_details *device_details;
  114. /* Input endpoints and buffer for this port */
  115. struct urb *in_urbs[2];
  116. char in_buffer[2][64];
  117. /* Output endpoints and buffer for this port */
  118. struct urb *out_urbs[2];
  119. char out_buffer[2][64];
  120. /* Input ack endpoint */
  121. struct urb *inack_urb;
  122. char inack_buffer[1];
  123. /* Output control endpoint */
  124. struct urb *outcont_urb;
  125. char outcont_buffer[64];
  126. /* Settings for the port */
  127. int baud;
  128. int old_baud;
  129. unsigned int cflag;
  130. enum {flow_none, flow_cts, flow_xon} flow_control;
  131. int rts_state; /* Handshaking pins (outputs) */
  132. int dtr_state;
  133. int cts_state; /* Handshaking pins (inputs) */
  134. int dsr_state;
  135. int dcd_state;
  136. int ri_state;
  137. int break_on;
  138. unsigned long tx_start_time[2];
  139. int resend_cont; /* need to resend control packet */
  140. };
  141. /* Include Keyspan message headers.  All current Keyspan Adapters
  142.    make use of one of three message formats which are referred
  143.    to as USA-26, USA-28 and USA-49 by Keyspan and within this driver. */
  144. #include "keyspan_usa26msg.h"
  145. #include "keyspan_usa28msg.h"
  146. #include "keyspan_usa49msg.h"
  147. /* Functions used by new usb-serial code. */
  148. static int __init keyspan_init (void)
  149. {
  150. usb_serial_register (&keyspan_pre_device);
  151. usb_serial_register (&keyspan_1port_device);
  152. usb_serial_register (&keyspan_2port_device);
  153. usb_serial_register (&keyspan_4port_device);
  154. info(DRIVER_VERSION ":" DRIVER_DESC);
  155. return 0;
  156. }
  157. static void __exit keyspan_exit (void)
  158. {
  159. usb_serial_deregister (&keyspan_pre_device);
  160. usb_serial_deregister (&keyspan_1port_device);
  161. usb_serial_deregister (&keyspan_2port_device);
  162. usb_serial_deregister (&keyspan_4port_device);
  163. }
  164. module_init(keyspan_init);
  165. module_exit(keyspan_exit);
  166. static void keyspan_rx_throttle (struct usb_serial_port *port)
  167. {
  168. dbg("%s - port %d", __FUNCTION__, port->number);
  169. }
  170. static void keyspan_rx_unthrottle (struct usb_serial_port *port)
  171. {
  172. dbg("%s - port %d", __FUNCTION__, port->number);
  173. }
  174. static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
  175. {
  176. struct keyspan_port_private  *p_priv;
  177.   dbg("%s", __FUNCTION__);
  178. p_priv = (struct keyspan_port_private *)port->private;
  179. if (break_state == -1)
  180. p_priv->break_on = 1;
  181. else
  182. p_priv->break_on = 0;
  183. keyspan_send_setup(port, 0);
  184. }
  185. static void keyspan_set_termios (struct usb_serial_port *port, 
  186.      struct termios *old_termios)
  187. {
  188. int baud_rate, device_port;
  189. struct keyspan_port_private  *p_priv;
  190. const struct keyspan_device_details *d_details;
  191. unsigned int  cflag;
  192. dbg("%s", __FUNCTION__); 
  193. p_priv = (struct keyspan_port_private *)(port->private);
  194. d_details = p_priv->device_details;
  195. cflag = port->tty->termios->c_cflag;
  196. device_port = port->number - port->serial->minor;
  197. /* Baud rate calculation takes baud rate as an integer
  198.    so other rates can be generated if desired. */
  199. baud_rate = tty_get_baud_rate(port->tty);
  200. /* If no match or invalid, don't change */
  201. if (baud_rate >= 0
  202.     && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
  203. NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
  204. /* FIXME - more to do here to ensure rate changes cleanly */
  205. p_priv->baud = baud_rate;
  206. }
  207. /* set CTS/RTS handshake etc. */
  208. p_priv->cflag = cflag;
  209. p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
  210. keyspan_send_setup(port, 0);
  211. }
  212. static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
  213.      unsigned int cmd, unsigned long arg)
  214. {
  215. unsigned int value, set;
  216. struct keyspan_port_private  *p_priv;
  217. p_priv = (struct keyspan_port_private *)(port->private);
  218. switch (cmd) {
  219. case TIOCMGET:
  220. value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
  221. ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
  222. ((p_priv->cts_state) ? TIOCM_CTS : 0) |
  223. ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
  224. ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
  225. ((p_priv->ri_state) ? TIOCM_RNG : 0); 
  226. if (put_user(value, (unsigned int *) arg))
  227. return -EFAULT;
  228. return 0;
  229. case TIOCMSET:
  230. if (get_user(value, (unsigned int *) arg))
  231. return -EFAULT;
  232. p_priv->rts_state = ((value & TIOCM_RTS) ? 1 : 0);
  233. p_priv->dtr_state = ((value & TIOCM_DTR) ? 1 : 0);
  234. keyspan_send_setup(port, 0);
  235. return 0;
  236. case TIOCMBIS:
  237. case TIOCMBIC:
  238. if (get_user(value, (unsigned int *) arg))
  239. return -EFAULT;
  240. set = (cmd == TIOCMBIS);
  241. if (value & TIOCM_RTS)
  242. p_priv->rts_state = set;
  243. if (value & TIOCM_DTR)
  244. p_priv->dtr_state = set;
  245. keyspan_send_setup(port, 0);
  246. return 0;
  247. }
  248. return -ENOIOCTLCMD;
  249. }
  250. /* Write function is generic for the three protocols used
  251.    with only a minor change for usa49 required */
  252. static int keyspan_write(struct usb_serial_port *port, int from_user, 
  253.  const unsigned char *buf, int count)
  254. {
  255. struct keyspan_port_private  *p_priv;
  256. const struct keyspan_device_details *d_details;
  257. int flip;
  258. int  left, todo;
  259. struct urb *this_urb;
  260. int  err;
  261. p_priv = (struct keyspan_port_private *)(port->private);
  262. d_details = p_priv->device_details;
  263. dbg("%s - for port %d (%d chars [%x]), flip=%d",
  264.     __FUNCTION__, port->number, count, buf[0], p_priv->out_flip);
  265. for (left = count; left > 0; left -= todo) {
  266. todo = left;
  267. if (todo > 63)
  268. todo = 63;
  269. flip = p_priv->out_flip;
  270. /* Check we have a valid urb/endpoint before we use it... */
  271. if ((this_urb = p_priv->out_urbs[flip]) == 0) {
  272. /* no bulk out, so return 0 bytes written */
  273. dbg("%s - no output urb :(", __FUNCTION__);
  274. return count;
  275. }
  276. dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
  277. if (this_urb->status == -EINPROGRESS) {
  278. if (this_urb->transfer_flags & USB_ASYNC_UNLINK)
  279. break;
  280. if (jiffies - p_priv->tx_start_time[flip] < 10 * HZ)
  281. break;
  282. this_urb->transfer_flags |= USB_ASYNC_UNLINK;
  283. usb_unlink_urb(this_urb);
  284. break;
  285. }
  286. /* First byte in buffer is "last flag" - unused so
  287.    for now so set to zero */
  288. ((char *)this_urb->transfer_buffer)[0] = 0;
  289. if (from_user) {
  290. if (copy_from_user(this_urb->transfer_buffer + 1, buf, todo))
  291. return -EFAULT;
  292. } else {
  293. memcpy (this_urb->transfer_buffer + 1, buf, todo);
  294. }
  295. buf += todo;
  296. /* send the data out the bulk port */
  297. this_urb->transfer_buffer_length = todo + 1;
  298. this_urb->transfer_flags &= ~USB_ASYNC_UNLINK;
  299. this_urb->dev = port->serial->dev;
  300. if ((err = usb_submit_urb(this_urb)) != 0) {
  301. dbg("usb_submit_urb(write bulk) failed (%d)", err);
  302. }
  303. p_priv->tx_start_time[flip] = jiffies;
  304. /* Flip for next time if usa26 or usa28 interface
  305.    (not used on usa49) */
  306. p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
  307. }
  308. return count - left;
  309. }
  310. static void usa26_indat_callback(struct urb *urb)
  311. {
  312. int i, err;
  313. int endpoint;
  314. struct usb_serial_port *port;
  315. struct tty_struct *tty;
  316. unsigned char  *data = urb->transfer_buffer;
  317. dbg ("%s", __FUNCTION__); 
  318. endpoint = usb_pipeendpoint(urb->pipe);
  319. if (urb->status) {
  320. dbg("%s - nonzero status: %x on endpoint %d.",
  321.     __FUNCTION__, urb->status, endpoint);
  322. return;
  323. }
  324. port = (struct usb_serial_port *) urb->context;
  325. tty = port->tty;
  326. if (urb->actual_length) {
  327. /* 0x80 bit is error flag */
  328. if ((data[0] & 0x80) == 0) {
  329. /* no error on any byte */
  330. for (i = 1; i < urb->actual_length ; ++i) {
  331. tty_insert_flip_char(tty, data[i], 0);
  332. }
  333. } else {
  334. /* some bytes had errors, every byte has status */
  335. dbg("%s - RX error!!!!", __FUNCTION__);
  336. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  337. int stat = data[i], flag = 0;
  338. if (stat & RXERROR_OVERRUN)
  339. flag |= TTY_OVERRUN;
  340. if (stat & RXERROR_FRAMING)
  341. flag |= TTY_FRAME;
  342. if (stat & RXERROR_PARITY)
  343. flag |= TTY_PARITY;
  344. /* XXX should handle break (0x10) */
  345. tty_insert_flip_char(tty, data[i+1], flag);
  346. }
  347. }
  348. tty_flip_buffer_push(tty);
  349. }
  350. /* Resubmit urb so we continue receiving */
  351. urb->dev = port->serial->dev;
  352. if (port->open_count)
  353. if ((err = usb_submit_urb(urb)) != 0) {
  354. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  355. }
  356. return;
  357. }
  358. /* Outdat handling is common for usa26, usa28 and usa49 messages */
  359. static void usa2x_outdat_callback(struct urb *urb)
  360. {
  361. struct usb_serial_port *port;
  362. struct keyspan_port_private *p_priv;
  363. port = (struct usb_serial_port *) urb->context;
  364. p_priv = (struct keyspan_port_private *)(port->private);
  365. dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]); 
  366. if (port->open_count) {
  367. queue_task(&port->tqueue, &tq_immediate);
  368. mark_bh(IMMEDIATE_BH);
  369. }
  370. }
  371. static void usa26_inack_callback(struct urb *urb)
  372. {
  373. dbg ("%s", __FUNCTION__); 
  374. }
  375. static void usa26_outcont_callback(struct urb *urb)
  376. {
  377. struct usb_serial_port *port;
  378. struct keyspan_port_private *p_priv;
  379. port = (struct usb_serial_port *) urb->context;
  380. p_priv = (struct keyspan_port_private *)(port->private);
  381. if (p_priv->resend_cont) {
  382. dbg ("%s - sending setup", __FUNCTION__); 
  383. keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
  384. }
  385. }
  386. static void usa26_instat_callback(struct urb *urb)
  387. {
  388. unsigned char  *data = urb->transfer_buffer;
  389. struct keyspan_usa26_portStatusMessage *msg;
  390. struct usb_serial *serial;
  391. struct usb_serial_port *port;
  392. struct keyspan_port_private   *p_priv;
  393. int old_dcd_state, err;
  394. serial = (struct usb_serial *) urb->context;
  395. if (urb->status) {
  396. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  397. return;
  398. }
  399. if (urb->actual_length != 9) {
  400. dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
  401. goto exit;
  402. }
  403. msg = (struct keyspan_usa26_portStatusMessage *)data;
  404. #if 0
  405. dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
  406.     __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
  407.     msg->_txXoff, msg->rxEnabled, msg->controlResponse);
  408. #endif
  409. /* Now do something useful with the data */
  410. /* Check port number from message and retrieve private data */
  411. if (msg->port >= serial->num_ports) {
  412. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
  413. goto exit;
  414. }
  415. port = &serial->port[msg->port];
  416. p_priv = (struct keyspan_port_private *)(port->private);
  417. /* Update handshaking pin state information */
  418. old_dcd_state = p_priv->dcd_state;
  419. p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
  420. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  421. p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
  422. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  423. if (port->tty && !C_CLOCAL(port->tty)
  424.     && old_dcd_state != p_priv->dcd_state) {
  425. if (old_dcd_state)
  426. tty_hangup(port->tty);
  427. /*  else */
  428. /* wake_up_interruptible(&p_priv->open_wait); */
  429. }
  430. /* Resubmit urb so we continue receiving */
  431. urb->dev = serial->dev;
  432. if ((err = usb_submit_urb(urb)) != 0) {
  433. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  434. }
  435. exit:
  436. }
  437. static void usa26_glocont_callback(struct urb *urb)
  438. {
  439. dbg ("%s", __FUNCTION__);
  440. }
  441. static void     usa28_indat_callback(struct urb *urb)
  442. {
  443. int                     i, err;
  444. struct usb_serial_port  *port;
  445. struct tty_struct       *tty;
  446. unsigned char           *data;
  447. struct keyspan_port_private             *p_priv;
  448. dbg ("%s", __FUNCTION__);
  449. port = (struct usb_serial_port *) urb->context;
  450. p_priv = (struct keyspan_port_private *)(port->private);
  451. data = urb->transfer_buffer;
  452. if (urb != p_priv->in_urbs[p_priv->in_flip])
  453. return;
  454. do {
  455. if (urb->status) {
  456. dbg("%s - nonzero status: %x on endpoint %d.",
  457.     __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
  458. return;
  459. }
  460. port = (struct usb_serial_port *) urb->context;
  461. p_priv = (struct keyspan_port_private *)(port->private);
  462. data = urb->transfer_buffer;
  463. tty = port->tty;
  464. if (urb->actual_length) {
  465. for (i = 0; i < urb->actual_length ; ++i) {
  466. tty_insert_flip_char(tty, data[i], 0);
  467. }
  468. tty_flip_buffer_push(tty);
  469. }
  470. /* Resubmit urb so we continue receiving */
  471. urb->dev = port->serial->dev;
  472. if (port->open_count)
  473. if ((err = usb_submit_urb(urb)) != 0) {
  474. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  475. }
  476. p_priv->in_flip ^= 1;
  477. urb = p_priv->in_urbs[p_priv->in_flip];
  478. } while (urb->status != -EINPROGRESS);
  479. }
  480. static void usa28_inack_callback(struct urb *urb)
  481. {
  482. dbg ("%s", __FUNCTION__);
  483. }
  484. static void usa28_outcont_callback(struct urb *urb)
  485. {
  486. struct usb_serial_port *port;
  487. struct keyspan_port_private *p_priv;
  488. port = (struct usb_serial_port *) urb->context;
  489. p_priv = (struct keyspan_port_private *)(port->private);
  490. if (p_priv->resend_cont) {
  491. dbg ("%s - sending setup", __FUNCTION__);
  492. keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
  493. }
  494. }
  495. static void usa28_instat_callback(struct urb *urb)
  496. {
  497. int err;
  498. unsigned char  *data = urb->transfer_buffer;
  499. struct keyspan_usa28_portStatusMessage *msg;
  500. struct usb_serial *serial;
  501. struct usb_serial_port *port;
  502. struct keyspan_port_private   *p_priv;
  503. int old_dcd_state;
  504. serial = (struct usb_serial *) urb->context;
  505. if (urb->status) {
  506. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  507. return;
  508. }
  509. if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
  510. dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
  511. goto exit;
  512. }
  513. /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
  514.     data[0], data[1], data[2], data[3], data[4], data[5],
  515.     data[6], data[7], data[8], data[9], data[10], data[11]);*/
  516. /* Now do something useful with the data */
  517. msg = (struct keyspan_usa28_portStatusMessage *)data;
  518. /* Check port number from message and retrieve private data */
  519. if (msg->port >= serial->num_ports) {
  520. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
  521. goto exit;
  522. }
  523. port = &serial->port[msg->port];
  524. p_priv = (struct keyspan_port_private *)(port->private);
  525. /* Update handshaking pin state information */
  526. old_dcd_state = p_priv->dcd_state;
  527. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  528. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  529. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  530. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  531. if (port->tty && !C_CLOCAL(port->tty)
  532.     && old_dcd_state != p_priv->dcd_state) {
  533. if (old_dcd_state)
  534. tty_hangup(port->tty);
  535. /*  else */
  536. /* wake_up_interruptible(&p_priv->open_wait); */
  537. }
  538. /* Resubmit urb so we continue receiving */
  539. urb->dev = serial->dev;
  540. if ((err = usb_submit_urb(urb)) != 0) {
  541. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  542. }
  543. exit:
  544. }
  545. static void usa28_glocont_callback(struct urb *urb)
  546. {
  547. dbg ("%s", __FUNCTION__);
  548. }
  549. static void usa49_glocont_callback(struct urb *urb)
  550. {
  551. struct usb_serial *serial;
  552. struct usb_serial_port *port;
  553. struct keyspan_port_private *p_priv;
  554. int i;
  555. dbg ("%s", __FUNCTION__);
  556. serial = (struct usb_serial *) urb->context;
  557. for (i = 0; i < serial->num_ports; ++i) {
  558. port = &serial->port[i];
  559. p_priv = (struct keyspan_port_private *)(port->private);
  560. if (p_priv->resend_cont) {
  561. dbg ("%s - sending setup", __FUNCTION__); 
  562. keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
  563. break;
  564. }
  565. }
  566. }
  567. /* This is actually called glostat in the Keyspan
  568.    doco */
  569. static void usa49_instat_callback(struct urb *urb)
  570. {
  571. int err;
  572. unsigned char  *data = urb->transfer_buffer;
  573. struct keyspan_usa49_portStatusMessage *msg;
  574. struct usb_serial *serial;
  575. struct usb_serial_port *port;
  576. struct keyspan_port_private   *p_priv;
  577. int old_dcd_state;
  578. dbg ("%s", __FUNCTION__);
  579. serial = (struct usb_serial *) urb->context;
  580. if (urb->status) {
  581. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  582. return;
  583. }
  584. if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
  585. dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
  586. goto exit;
  587. }
  588. /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__, 
  589.     data[0], data[1], data[2], data[3], data[4], data[5],
  590.     data[6], data[7], data[8], data[9], data[10]);*/
  591. /* Now do something useful with the data */
  592. msg = (struct keyspan_usa49_portStatusMessage *)data;
  593. /* Check port number from message and retrieve private data */
  594. if (msg->portNumber >= serial->num_ports) {
  595. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
  596. goto exit;
  597. }
  598. port = &serial->port[msg->portNumber];
  599. p_priv = (struct keyspan_port_private *)(port->private);
  600. /* Update handshaking pin state information */
  601. old_dcd_state = p_priv->dcd_state;
  602. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  603. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  604. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  605. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  606. if (port->tty && !C_CLOCAL(port->tty)
  607.     && old_dcd_state != p_priv->dcd_state) {
  608. if (old_dcd_state)
  609. tty_hangup(port->tty);
  610. /*  else */
  611. /* wake_up_interruptible(&p_priv->open_wait); */
  612. }
  613. /* Resubmit urb so we continue receiving */
  614. urb->dev = serial->dev;
  615. if ((err = usb_submit_urb(urb)) != 0) {
  616. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  617. }
  618. exit:
  619. }
  620. static void usa49_inack_callback(struct urb *urb)
  621. {
  622. dbg ("%s", __FUNCTION__);
  623. }
  624. static void usa49_indat_callback(struct urb *urb)
  625. {
  626. int i, err;
  627. int endpoint;
  628. struct usb_serial_port *port;
  629. struct tty_struct *tty;
  630. unsigned char  *data = urb->transfer_buffer;
  631. dbg ("%s", __FUNCTION__);
  632. endpoint = usb_pipeendpoint(urb->pipe);
  633. if (urb->status) {
  634. dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
  635.     urb->status, endpoint);
  636. return;
  637. }
  638. port = (struct usb_serial_port *) urb->context;
  639. tty = port->tty;
  640. if (urb->actual_length) {
  641. /* 0x80 bit is error flag */
  642. if ((data[0] & 0x80) == 0) {
  643. /* no error on any byte */
  644. for (i = 1; i < urb->actual_length ; ++i) {
  645. tty_insert_flip_char(tty, data[i], 0);
  646. }
  647. } else {
  648. /* some bytes had errors, every byte has status */
  649. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  650. int stat = data[i], flag = 0;
  651. if (stat & RXERROR_OVERRUN)
  652. flag |= TTY_OVERRUN;
  653. if (stat & RXERROR_FRAMING)
  654. flag |= TTY_FRAME;
  655. if (stat & RXERROR_PARITY)
  656. flag |= TTY_PARITY;
  657. /* XXX should handle break (0x10) */
  658. tty_insert_flip_char(tty, data[i+1], flag);
  659. }
  660. }
  661. tty_flip_buffer_push(tty);
  662. }
  663. /* Resubmit urb so we continue receiving */
  664. urb->dev = port->serial->dev;
  665. if (port->open_count)
  666. if ((err = usb_submit_urb(urb)) != 0) {
  667. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  668. }
  669. }
  670. /* not used, usa-49 doesn't have per-port control endpoints */
  671. static void usa49_outcont_callback(struct urb *urb)
  672. {
  673. dbg ("%s", __FUNCTION__);
  674. }
  675. static int keyspan_write_room (struct usb_serial_port *port)
  676. {
  677. struct keyspan_port_private *p_priv;
  678. const struct keyspan_device_details *d_details;
  679. int flip;
  680. struct urb *this_urb;
  681. dbg("%s", __FUNCTION__);
  682. p_priv = (struct keyspan_port_private *)(port->private);
  683. d_details = p_priv->device_details;
  684. flip = p_priv->out_flip;
  685. /* Check both endpoints to see if any are available. */
  686. if ((this_urb = p_priv->out_urbs[flip]) != 0) {
  687. if (this_urb->status != -EINPROGRESS)
  688. return (63);
  689. flip = (flip + 1) & d_details->outdat_endp_flip;        
  690. if ((this_urb = p_priv->out_urbs[flip]) != 0) 
  691. if (this_urb->status != -EINPROGRESS)
  692. return (63);
  693. }
  694. return (0);
  695. }
  696. static int keyspan_chars_in_buffer (struct usb_serial_port *port)
  697. {
  698. return (0);
  699. }
  700. static int keyspan_open (struct usb_serial_port *port, struct file *filp)
  701. {
  702. struct keyspan_port_private  *p_priv;
  703. struct keyspan_serial_private  *s_priv;
  704. struct usb_serial  *serial = port->serial;
  705. const struct keyspan_device_details *d_details;
  706. int i, err;
  707. struct urb *urb;
  708. s_priv = (struct keyspan_serial_private *)(serial->private);
  709. p_priv = (struct keyspan_port_private *)(port->private);
  710. d_details = s_priv->device_details;
  711. dbg("%s - port%d.", __FUNCTION__, port->number); 
  712. p_priv = (struct keyspan_port_private *)(port->private);
  713. /* Set some sane defaults */
  714. p_priv->rts_state = 1;
  715. p_priv->dtr_state = 1;
  716. p_priv->out_flip = 0;
  717. p_priv->in_flip = 0;
  718. /* Reset low level data toggle and start reading from endpoints */
  719. for (i = 0; i < 2; i++) {
  720. if ((urb = p_priv->in_urbs[i]) == NULL)
  721. continue;
  722. urb->dev = serial->dev;
  723. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0);
  724. if ((err = usb_submit_urb(urb)) != 0) {
  725. dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
  726. }
  727. }
  728. /* Reset low level data toggle on out endpoints */
  729. for (i = 0; i < 2; i++) {
  730. if ((urb = p_priv->out_urbs[i]) == NULL)
  731. continue;
  732. urb->dev = serial->dev;
  733. /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
  734. }
  735. keyspan_send_setup(port, 1);
  736. //mdelay(100);
  737. keyspan_set_termios(port, NULL);
  738. return (0);
  739. }
  740. static inline void stop_urb(struct urb *urb)
  741. {
  742. if (urb && urb->status == -EINPROGRESS) {
  743. urb->transfer_flags &= ~USB_ASYNC_UNLINK;
  744. usb_unlink_urb(urb);
  745. }
  746. }
  747. static void keyspan_close(struct usb_serial_port *port, struct file *filp)
  748. {
  749. int i;
  750. struct usb_serial *serial;
  751. struct keyspan_serial_private  *s_priv;
  752. struct keyspan_port_private  *p_priv;
  753. serial = get_usb_serial (port, __FUNCTION__);
  754. if (!serial)
  755. return;
  756. dbg("%s", __FUNCTION__);
  757. s_priv = (struct keyspan_serial_private *)(serial->private);
  758. p_priv = (struct keyspan_port_private *)(port->private);
  759. p_priv->rts_state = 0;
  760. p_priv->dtr_state = 0;
  761. if (serial->dev) {
  762. keyspan_send_setup(port, 2);
  763. /* pilot-xfer seems to work best with this delay */
  764. mdelay(100);
  765. keyspan_set_termios(port, NULL);
  766. }
  767. /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
  768. dbg("%s - urb in progress", __FUNCTION__);
  769. }*/
  770. p_priv->out_flip = 0;
  771. p_priv->in_flip = 0;
  772. if (serial->dev) {
  773. /* Stop reading/writing urbs */
  774. stop_urb(p_priv->inack_urb);
  775. /* stop_urb(p_priv->outcont_urb); */
  776. for (i = 0; i < 2; i++) {
  777. stop_urb(p_priv->in_urbs[i]);
  778. stop_urb(p_priv->out_urbs[i]);
  779. }
  780. }
  781. port->tty = 0;
  782. }
  783. /* download the firmware to a pre-renumeration device */
  784. static int keyspan_fake_startup (struct usb_serial *serial)
  785. {
  786. int  response;
  787. const struct ezusb_hex_record  *record;
  788. char *fw_name;
  789. dbg("Keyspan startup version %04x product %04x",
  790.     serial->dev->descriptor.bcdDevice,
  791.     serial->dev->descriptor.idProduct); 
  792. if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
  793. dbg("Firmware already loaded.  Quitting.");
  794. return(1);
  795. }
  796. /* Select firmware image on the basis of idProduct */
  797. switch (serial->dev->descriptor.idProduct) {
  798. case keyspan_usa28_pre_product_id:
  799. record = &keyspan_usa28_firmware[0];
  800. fw_name = "USA28";
  801. break;
  802. case keyspan_usa28x_pre_product_id:
  803. record = &keyspan_usa28x_firmware[0];
  804. fw_name = "USA28X";
  805. break;
  806. case keyspan_usa28xa_pre_product_id:
  807. record = &keyspan_usa28xa_firmware[0];
  808. fw_name = "USA28XA";
  809. break;
  810. case keyspan_usa28xb_pre_product_id:
  811. record = &keyspan_usa28xb_firmware[0];
  812. fw_name = "USA28XB";
  813. break;
  814. case keyspan_usa19_pre_product_id:
  815. record = &keyspan_usa19_firmware[0];
  816. fw_name = "USA19";
  817. break;
  818.      
  819. case keyspan_usa19qi_pre_product_id:
  820. record = &keyspan_usa19qi_firmware[0];
  821. fw_name = "USA19QI";
  822. break;
  823.      
  824. case keyspan_usa19qw_pre_product_id:
  825. record = &keyspan_usa19qw_firmware[0];
  826. fw_name = "USA19QI";
  827. break;
  828.      
  829. case keyspan_usa18x_pre_product_id:
  830. record = &keyspan_usa18x_firmware[0];
  831. fw_name = "USA18X";
  832. break;
  833.      
  834. case keyspan_usa19w_pre_product_id:
  835. record = &keyspan_usa19w_firmware[0];
  836. fw_name = "USA19W";
  837. break;
  838. case keyspan_usa49w_pre_product_id:
  839. record = &keyspan_usa49w_firmware[0];
  840. fw_name = "USA49W";
  841. break;
  842. default:
  843. record = NULL;
  844. fw_name = "Unknown";
  845. break;
  846. }
  847. if (record == NULL) {
  848. err("Required keyspan firmware image (%s) unavailable.", fw_name);
  849. return(1);
  850. }
  851. dbg("Uploading Keyspan %s firmware.", fw_name);
  852. /* download the firmware image */
  853. response = ezusb_set_reset(serial, 1);
  854. while(record->address != 0xffff) {
  855. response = ezusb_writememory(serial, record->address,
  856.      (unsigned char *)record->data,
  857.      record->data_size, 0xa0);
  858. if (response < 0) {
  859. err("ezusb_writememory failed for Keyspan"
  860.     "firmware (%d %04X %p %d)",
  861.     response, 
  862.     record->address, record->data, record->data_size);
  863. break;
  864. }
  865. record++;
  866. }
  867. /* bring device out of reset. Renumeration will occur in a
  868.    moment and the new device will bind to the real driver */
  869. response = ezusb_set_reset(serial, 0);
  870. /* we don't want this device to have a driver assigned to it. */
  871. return (1);
  872. }
  873. /* Helper functions used by keyspan_setup_urbs */
  874. static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
  875.       int dir, void *ctx, char *buf, int len,
  876.       void (*callback)(struct urb *))
  877. {
  878. struct urb *urb;
  879. if (endpoint == -1)
  880. return NULL; /* endpoint not needed */
  881. dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
  882. urb = usb_alloc_urb(0); /* No ISO */
  883. if (urb == NULL) {
  884. dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
  885. return NULL;
  886. }
  887. /* Fill URB using supplied data. */
  888. FILL_BULK_URB(urb, serial->dev,
  889.       usb_sndbulkpipe(serial->dev, endpoint) | dir,
  890.       buf, len, callback, ctx);
  891. return urb;
  892. }
  893. static struct callbacks {
  894. void (*instat_callback)(struct urb *);
  895. void (*glocont_callback)(struct urb *);
  896. void (*indat_callback)(struct urb *);
  897. void (*outdat_callback)(struct urb *);
  898. void (*inack_callback)(struct urb *);
  899. void (*outcont_callback)(struct urb *);
  900. } keyspan_callbacks[] = {
  901. {
  902. /* msg_usa26 callbacks */
  903. .instat_callback = usa26_instat_callback,
  904. .glocont_callback = usa26_glocont_callback,
  905. .indat_callback = usa26_indat_callback,
  906. .outdat_callback = usa2x_outdat_callback,
  907. .inack_callback = usa26_inack_callback,
  908. .outcont_callback = usa26_outcont_callback,
  909. }, {
  910. /* msg_usa28 callbacks */
  911. .instat_callback = usa28_instat_callback,
  912. .glocont_callback = usa28_glocont_callback,
  913. .indat_callback = usa28_indat_callback,
  914. .outdat_callback = usa2x_outdat_callback,
  915. .inack_callback = usa28_inack_callback,
  916. .outcont_callback = usa28_outcont_callback,
  917. }, {
  918. /* msg_usa49 callbacks */
  919. .instat_callback = usa49_instat_callback,
  920. .glocont_callback = usa49_glocont_callback,
  921. .indat_callback = usa49_indat_callback,
  922. .outdat_callback = usa2x_outdat_callback,
  923. .inack_callback = usa49_inack_callback,
  924. .outcont_callback = usa49_outcont_callback,
  925. }
  926. };
  927. /* Generic setup urbs function that uses
  928.    data in device_details */
  929. static void keyspan_setup_urbs(struct usb_serial *serial)
  930. {
  931. int i, j;
  932. struct keyspan_serial_private  *s_priv;
  933. const struct keyspan_device_details *d_details;
  934. struct usb_serial_port *port;
  935. struct keyspan_port_private *p_priv;
  936. struct callbacks *cback;
  937. int endp;
  938. dbg ("%s", __FUNCTION__);
  939. s_priv = (struct keyspan_serial_private *)(serial->private);
  940. d_details = s_priv->device_details;
  941. /* Setup values for the various callback routines */
  942. cback = &keyspan_callbacks[d_details->msg_format];
  943. /* Allocate and set up urbs for each one that is in use, 
  944.    starting with instat endpoints */
  945. s_priv->instat_urb = keyspan_setup_urb
  946. (serial, d_details->instat_endpoint, USB_DIR_IN,
  947.  serial, s_priv->instat_buf, INSTAT_BUFLEN,
  948.  cback->instat_callback);
  949. s_priv->glocont_urb = keyspan_setup_urb
  950. (serial, d_details->glocont_endpoint, USB_DIR_OUT,
  951.  serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
  952.  cback->glocont_callback);
  953. /* Setup endpoints for each port specific thing */
  954. for (i = 0; i < d_details->num_ports; i ++) {
  955. port = &serial->port[i];
  956. p_priv = (struct keyspan_port_private *)(port->private);
  957. /* Do indat endpoints first, once for each flip */
  958. endp = d_details->indat_endpoints[i];
  959. for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
  960. p_priv->in_urbs[j] = keyspan_setup_urb
  961. (serial, endp, USB_DIR_IN, port,
  962.  p_priv->in_buffer[j], 64,
  963.  cback->indat_callback);
  964. }
  965. for (; j < 2; ++j)
  966. p_priv->in_urbs[j] = NULL;
  967. /* outdat endpoints also have flip */
  968. endp = d_details->outdat_endpoints[i];
  969. for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
  970. p_priv->out_urbs[j] = keyspan_setup_urb
  971. (serial, endp, USB_DIR_OUT, port,
  972.  p_priv->out_buffer[j], 64,
  973.  cback->outdat_callback);
  974. }
  975. for (; j < 2; ++j)
  976. p_priv->out_urbs[j] = NULL;
  977. /* inack endpoint */
  978. p_priv->inack_urb = keyspan_setup_urb
  979. (serial, d_details->inack_endpoints[i], USB_DIR_IN,
  980.  port, p_priv->inack_buffer, 1, cback->inack_callback);
  981. /* outcont endpoint */
  982. p_priv->outcont_urb = keyspan_setup_urb
  983. (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
  984.  port, p_priv->outcont_buffer, 64,
  985.  cback->outcont_callback);
  986. }
  987. }
  988. /* usa19 function doesn't require prescaler */
  989. static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  990.    u8 *rate_low, u8 *prescaler, int portnum)
  991. {
  992. u32  b16, /* baud rate times 16 (actual rate used internally) */
  993. div, /* divisor */
  994. cnt; /* inverse of divisor (programmed into 8051) */
  995. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  996. /* prevent divide by zero...  */
  997. if( (b16 = (baud_rate * 16L)) == 0) {
  998. return (KEYSPAN_INVALID_BAUD_RATE);
  999. }
  1000. /* Any "standard" rate over 57k6 is marginal on the USA-19
  1001.    as we run out of divisor resolution. */
  1002. if (baud_rate > 57600) {
  1003. return (KEYSPAN_INVALID_BAUD_RATE);
  1004. }
  1005. /* calculate the divisor and the counter (its inverse) */
  1006. if( (div = (baudclk / b16)) == 0) {
  1007. return (KEYSPAN_INVALID_BAUD_RATE);
  1008. }
  1009. else {
  1010. cnt = 0 - div;
  1011. }
  1012. if(div > 0xffff) {
  1013. return (KEYSPAN_INVALID_BAUD_RATE);
  1014. }
  1015. /* return the counter values if non-null */
  1016. if (rate_low) {
  1017. *rate_low = (u8) (cnt & 0xff);
  1018. }
  1019. if (rate_hi) {
  1020. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1021. }
  1022. if (rate_low && rate_hi) {
  1023. dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
  1024. }
  1025. return (KEYSPAN_BAUD_RATE_OK);
  1026. }
  1027. static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1028.     u8 *rate_low, u8 *prescaler, int portnum)
  1029. {
  1030. u32  b16, /* baud rate times 16 (actual rate used internally) */
  1031. clk, /* clock with 13/8 prescaler */
  1032. div, /* divisor using 13/8 prescaler */
  1033. res, /* resulting baud rate using 13/8 prescaler */
  1034. diff, /* error using 13/8 prescaler */
  1035. smallest_diff;
  1036. u8 best_prescaler;
  1037. int i;
  1038. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1039. /* prevent divide by zero */
  1040. if( (b16 = baud_rate * 16L) == 0) {
  1041. return (KEYSPAN_INVALID_BAUD_RATE);
  1042. }
  1043. /* Calculate prescaler by trying them all and looking
  1044.    for best fit */
  1045. /* start with largest possible difference */
  1046. smallest_diff = 0xffffffff;
  1047. /* 0 is an invalid prescaler, used as a flag */
  1048. best_prescaler = 0;
  1049. for(i = 8; i <= 0xff; ++i) {
  1050. clk = (baudclk * 8) / (u32) i;
  1051. if( (div = clk / b16) == 0) {
  1052. continue;
  1053. }
  1054. res = clk / div;
  1055. diff= (res > b16) ? (res-b16) : (b16-res);
  1056. if(diff < smallest_diff) {
  1057. best_prescaler = i;
  1058. smallest_diff = diff;
  1059. }
  1060. }
  1061. if(best_prescaler == 0) {
  1062. return (KEYSPAN_INVALID_BAUD_RATE);
  1063. }
  1064. clk = (baudclk * 8) / (u32) best_prescaler;
  1065. div = clk / b16;
  1066. /* return the divisor and prescaler if non-null */
  1067. if (rate_low) {
  1068. *rate_low = (u8) (div & 0xff);
  1069. }
  1070. if (rate_hi) {
  1071. *rate_hi = (u8) ((div >> 8) & 0xff);
  1072. }
  1073. if (prescaler) {
  1074. *prescaler = best_prescaler;
  1075. /*  dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
  1076. }
  1077. return (KEYSPAN_BAUD_RATE_OK);
  1078. }
  1079. /* USA-28 supports different maximum baud rates on each port */
  1080. static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1081.     u8 *rate_low, u8 *prescaler, int portnum)
  1082. {
  1083. u32  b16, /* baud rate times 16 (actual rate used internally) */
  1084. div, /* divisor */
  1085. cnt; /* inverse of divisor (programmed into 8051) */
  1086. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1087. /* prevent divide by zero */
  1088. if ((b16 = baud_rate * 16L) == 0)
  1089. return (KEYSPAN_INVALID_BAUD_RATE);
  1090. /* calculate the divisor and the counter (its inverse) */
  1091. if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
  1092. return (KEYSPAN_INVALID_BAUD_RATE);
  1093. }
  1094. else {
  1095. cnt = 0 - div;
  1096. }
  1097. /* check for out of range, based on portnum, 
  1098.    and return result */
  1099. if(portnum == 0) {
  1100. if(div > 0xffff)
  1101. return (KEYSPAN_INVALID_BAUD_RATE);
  1102. }
  1103. else {
  1104. if(portnum == 1) {
  1105. if(div > 0xff) {
  1106. return (KEYSPAN_INVALID_BAUD_RATE);
  1107. }
  1108. }
  1109. else {
  1110. return (KEYSPAN_INVALID_BAUD_RATE);
  1111. }
  1112. }
  1113. /* return the counter values if not NULL
  1114.    (port 1 will ignore retHi) */
  1115. if (rate_low) {
  1116. *rate_low = (u8) (cnt & 0xff);
  1117. }
  1118. if (rate_hi) {
  1119. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1120. }
  1121. dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
  1122. return (KEYSPAN_BAUD_RATE_OK);
  1123. }
  1124. static int keyspan_usa26_send_setup(struct usb_serial *serial,
  1125.     struct usb_serial_port *port,
  1126.     int reset_port)
  1127. {
  1128. struct keyspan_usa26_portControlMessage msg;
  1129. struct keyspan_serial_private  *s_priv;
  1130. struct keyspan_port_private  *p_priv;
  1131. const struct keyspan_device_details *d_details;
  1132. int  outcont_urb;
  1133. struct urb *this_urb;
  1134. int  device_port, err;
  1135. dbg ("%s reset=%d", __FUNCTION__, reset_port); 
  1136. s_priv = (struct keyspan_serial_private *)(serial->private);
  1137. p_priv = (struct keyspan_port_private *)(port->private);
  1138. d_details = s_priv->device_details;
  1139. device_port = port->number - port->serial->minor;
  1140. outcont_urb = d_details->outcont_endpoints[port->number];
  1141. this_urb = p_priv->outcont_urb;
  1142. dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
  1143. /* Make sure we have an urb then send the message */
  1144. if (this_urb == NULL) {
  1145. dbg("%s - oops no urb.", __FUNCTION__);
  1146. return -1;
  1147. }
  1148. /* Save reset port val for resend.
  1149. Don't overwrite resend for close condition. */
  1150. if (p_priv->resend_cont != 3)
  1151. p_priv->resend_cont = reset_port + 1;
  1152. if (this_urb->status == -EINPROGRESS) {
  1153. /*  dbg ("%s - already writing", __FUNCTION__); */
  1154. return(-1);
  1155. }
  1156. memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
  1157. /* Only set baud rate if it's changed */
  1158. if (p_priv->old_baud != p_priv->baud) {
  1159. p_priv->old_baud = p_priv->baud;
  1160. msg.setClocking = 0xff;
  1161. if (d_details->calculate_baud_rate
  1162.     (p_priv->baud, d_details->baudclk, &msg.baudHi,
  1163.      &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1164. dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
  1165.     p_priv->baud);
  1166. msg.baudLo = 0;
  1167. msg.baudHi = 125; /* Values for 9600 baud */
  1168. msg.prescaler = 10;
  1169. }
  1170. msg.setPrescaler = 0xff;
  1171. }
  1172. msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
  1173. switch (p_priv->cflag & CSIZE) {
  1174. case CS5:
  1175. msg.lcr |= USA_DATABITS_5;
  1176. break;
  1177. case CS6:
  1178. msg.lcr |= USA_DATABITS_6;
  1179. break;
  1180. case CS7:
  1181. msg.lcr |= USA_DATABITS_7;
  1182. break;
  1183. case CS8:
  1184. msg.lcr |= USA_DATABITS_8;
  1185. break;
  1186. }
  1187. if (p_priv->cflag & PARENB) {
  1188. /* note USA_PARITY_NONE == 0 */
  1189. msg.lcr |= (p_priv->cflag & PARODD)?
  1190. USA_PARITY_ODD: USA_PARITY_EVEN;
  1191. }
  1192. msg.setLcr = 0xff;
  1193. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1194. msg.xonFlowControl = 0;
  1195. msg.setFlowControl = 0xff;
  1196. msg.forwardingLength = 16;
  1197. msg.xonChar = 17;
  1198. msg.xoffChar = 19;
  1199. /* Opening port */
  1200. if (reset_port == 1) {
  1201. msg._txOn = 1;
  1202. msg._txOff = 0;
  1203. msg.txFlush = 0;
  1204. msg.txBreak = 0;
  1205. msg.rxOn = 1;
  1206. msg.rxOff = 0;
  1207. msg.rxFlush = 1;
  1208. msg.rxForward = 0;
  1209. msg.returnStatus = 0;
  1210. msg.resetDataToggle = 0xff;
  1211. }
  1212. /* Closing port */
  1213. else if (reset_port == 2) {
  1214. msg._txOn = 0;
  1215. msg._txOff = 1;
  1216. msg.txFlush = 0;
  1217. msg.txBreak = 0;
  1218. msg.rxOn = 0;
  1219. msg.rxOff = 1;
  1220. msg.rxFlush = 1;
  1221. msg.rxForward = 0;
  1222. msg.returnStatus = 0;
  1223. msg.resetDataToggle = 0;
  1224. }
  1225. /* Sending intermediate configs */
  1226. else {
  1227. msg._txOn = (! p_priv->break_on);
  1228. msg._txOff = 0;
  1229. msg.txFlush = 0;
  1230. msg.txBreak = (p_priv->break_on);
  1231. msg.rxOn = 0;
  1232. msg.rxOff = 0;
  1233. msg.rxFlush = 0;
  1234. msg.rxForward = 0;
  1235. msg.returnStatus = 0;
  1236. msg.resetDataToggle = 0x0;
  1237. }
  1238. /* Do handshaking outputs */
  1239. msg.setTxTriState_setRts = 0xff;
  1240. msg.txTriState_rts = p_priv->rts_state;
  1241. msg.setHskoa_setDtr = 0xff;
  1242. msg.hskoa_dtr = p_priv->dtr_state;
  1243. p_priv->resend_cont = 0;
  1244. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1245. /* send the data out the device on control endpoint */
  1246. this_urb->transfer_buffer_length = sizeof(msg);
  1247. this_urb->dev = serial->dev;
  1248. if ((err = usb_submit_urb(this_urb)) != 0) {
  1249. dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
  1250. }
  1251. #if 0
  1252. else {
  1253. dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
  1254.     outcont_urb, this_urb->transfer_buffer_length,
  1255.     usb_pipeendpoint(this_urb->pipe));
  1256. }
  1257. #endif
  1258. return (0);
  1259. }
  1260. static int keyspan_usa28_send_setup(struct usb_serial *serial,
  1261.     struct usb_serial_port *port,
  1262.     int reset_port)
  1263. {
  1264. struct keyspan_usa28_portControlMessage msg;
  1265. struct keyspan_serial_private   *s_priv;
  1266. struct keyspan_port_private  *p_priv;
  1267. const struct keyspan_device_details *d_details;
  1268. struct urb *this_urb;
  1269. int  device_port, err;
  1270. dbg ("%s", __FUNCTION__);
  1271. s_priv = (struct keyspan_serial_private *)(serial->private);
  1272. p_priv = (struct keyspan_port_private *)(port->private);
  1273. d_details = s_priv->device_details;
  1274. device_port = port->number - port->serial->minor;
  1275. /* only do something if we have a bulk out endpoint */
  1276. if ((this_urb = p_priv->outcont_urb) == NULL) {
  1277. dbg("%s - oops no urb.", __FUNCTION__);
  1278. return -1;
  1279. }
  1280. /* Save reset port val for resend.
  1281.    Don't overwrite resend for close condition. */
  1282. if (p_priv->resend_cont != 3)
  1283. p_priv->resend_cont = reset_port + 1;
  1284. if (this_urb->status == -EINPROGRESS) {
  1285. dbg ("%s already writing", __FUNCTION__);
  1286. return(-1);
  1287. }
  1288. memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
  1289. msg.setBaudRate = 1;
  1290. if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
  1291. &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1292. dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
  1293. msg.baudLo = 0xff;
  1294. msg.baudHi = 0xb2; /* Values for 9600 baud */
  1295. }
  1296. /* If parity is enabled, we must calculate it ourselves. */
  1297. msg.parity = 0; /* XXX for now */
  1298. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1299. msg.xonFlowControl = 0;
  1300. /* Do handshaking outputs, DTR is inverted relative to RTS */
  1301. msg.rts = p_priv->rts_state;
  1302. msg.dtr = p_priv->dtr_state;
  1303. msg.forwardingLength = 16;
  1304. msg.forwardMs = 10;
  1305. msg.breakThreshold = 45;
  1306. msg.xonChar = 17;
  1307. msg.xoffChar = 19;
  1308. /*msg.returnStatus = 1;
  1309. msg.resetDataToggle = 0xff;*/
  1310. /* Opening port */
  1311. if (reset_port == 1) {
  1312. msg._txOn = 1;
  1313. msg._txOff = 0;
  1314. msg.txFlush = 0;
  1315. msg.txForceXoff = 0;
  1316. msg.txBreak = 0;
  1317. msg.rxOn = 1;
  1318. msg.rxOff = 0;
  1319. msg.rxFlush = 1;
  1320. msg.rxForward = 0;
  1321. msg.returnStatus = 0;
  1322. msg.resetDataToggle = 0xff;
  1323. }
  1324. /* Closing port */
  1325. else if (reset_port == 2) {
  1326. msg._txOn = 0;
  1327. msg._txOff = 1;
  1328. msg.txFlush = 0;
  1329. msg.txForceXoff = 0;
  1330. msg.txBreak = 0;
  1331. msg.rxOn = 0;
  1332. msg.rxOff = 1;
  1333. msg.rxFlush = 1;
  1334. msg.rxForward = 0;
  1335. msg.returnStatus = 0;
  1336. msg.resetDataToggle = 0;
  1337. }
  1338. /* Sending intermediate configs */
  1339. else {
  1340. msg._txOn = (! p_priv->break_on);
  1341. msg._txOff = 0;
  1342. msg.txFlush = 0;
  1343. msg.txForceXoff = 0;
  1344. msg.txBreak = (p_priv->break_on);
  1345. msg.rxOn = 0;
  1346. msg.rxOff = 0;
  1347. msg.rxFlush = 0;
  1348. msg.rxForward = 0;
  1349. msg.returnStatus = 0;
  1350. msg.resetDataToggle = 0x0;
  1351. }
  1352. p_priv->resend_cont = 0;
  1353. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1354. /* send the data out the device on control endpoint */
  1355. this_urb->transfer_buffer_length = sizeof(msg);
  1356. this_urb->dev = serial->dev;
  1357. if ((err = usb_submit_urb(this_urb)) != 0) {
  1358. dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
  1359. }
  1360. #if 0
  1361. else {
  1362. dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
  1363.     this_urb->transfer_buffer_length);
  1364. }
  1365. #endif
  1366. return (0);
  1367. }
  1368. static int keyspan_usa49_send_setup(struct usb_serial *serial,
  1369.     struct usb_serial_port *port,
  1370.     int reset_port)
  1371. {
  1372. struct keyspan_usa49_portControlMessage msg;
  1373. struct keyspan_serial_private  *s_priv;
  1374. struct keyspan_port_private  *p_priv;
  1375. const struct keyspan_device_details *d_details;
  1376. int  glocont_urb;
  1377. struct urb *this_urb;
  1378. int  err, device_port;
  1379. dbg ("%s", __FUNCTION__);
  1380. s_priv = (struct keyspan_serial_private *)(serial->private);
  1381. p_priv = (struct keyspan_port_private *)(port->private);
  1382. d_details = s_priv->device_details;
  1383. glocont_urb = d_details->glocont_endpoint;
  1384. this_urb = s_priv->glocont_urb;
  1385. /* Work out which port within the device is being setup */
  1386. device_port = port->number - port->serial->minor;
  1387. dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
  1388. /* Make sure we have an urb then send the message */
  1389. if (this_urb == NULL) {
  1390. dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
  1391. return -1;
  1392. }
  1393. /* Save reset port val for resend.
  1394.    Don't overwrite resend for close condition. */
  1395. if (p_priv->resend_cont != 3)
  1396. p_priv->resend_cont = reset_port + 1;
  1397. if (this_urb->status == -EINPROGRESS) {
  1398. /*  dbg ("%s - already writing", __FUNCTION__); */
  1399. return(-1);
  1400. }
  1401. memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
  1402. /*msg.portNumber = port->number;*/
  1403. msg.portNumber = device_port;
  1404. /* Only set baud rate if it's changed */
  1405. if (p_priv->old_baud != p_priv->baud) {
  1406. p_priv->old_baud = p_priv->baud;
  1407. msg.setClocking = 0xff;
  1408. if (d_details->calculate_baud_rate
  1409.     (p_priv->baud, d_details->baudclk, &msg.baudHi,
  1410.      &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1411. dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
  1412.     p_priv->baud);
  1413. msg.baudLo = 0;
  1414. msg.baudHi = 125; /* Values for 9600 baud */
  1415. msg.prescaler = 10;
  1416. }
  1417. //msg.setPrescaler = 0xff;
  1418. }
  1419. msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
  1420. switch (p_priv->cflag & CSIZE) {
  1421. case CS5:
  1422. msg.lcr |= USA_DATABITS_5;
  1423. break;
  1424. case CS6:
  1425. msg.lcr |= USA_DATABITS_6;
  1426. break;
  1427. case CS7:
  1428. msg.lcr |= USA_DATABITS_7;
  1429. break;
  1430. case CS8:
  1431. msg.lcr |= USA_DATABITS_8;
  1432. break;
  1433. }
  1434. if (p_priv->cflag & PARENB) {
  1435. /* note USA_PARITY_NONE == 0 */
  1436. msg.lcr |= (p_priv->cflag & PARODD)?
  1437. USA_PARITY_ODD: USA_PARITY_EVEN;
  1438. }
  1439. msg.setLcr = 0xff;
  1440. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1441. msg.xonFlowControl = 0;
  1442. msg.setFlowControl = 0xff;
  1443. msg.forwardingLength = 16;
  1444. msg.xonChar = 17;
  1445. msg.xoffChar = 19;
  1446. /* Opening port */ 
  1447. if (reset_port == 1) {
  1448. msg._txOn = 1;
  1449. msg._txOff = 0;
  1450. msg.txFlush = 0;
  1451. msg.txBreak = 0;
  1452. msg.rxOn = 1;
  1453. msg.rxOff = 0;
  1454. msg.rxFlush = 1;
  1455. msg.rxForward = 0;
  1456. msg.returnStatus = 0;
  1457. msg.resetDataToggle = 0xff;
  1458. msg.enablePort = 1;
  1459. msg.disablePort = 0;
  1460. }
  1461. /* Closing port */
  1462. else if (reset_port == 2) {
  1463. msg._txOn = 0;
  1464. msg._txOff = 1;
  1465. msg.txFlush = 0;
  1466. msg.txBreak = 0;
  1467. msg.rxOn = 0;
  1468. msg.rxOff = 1;
  1469. msg.rxFlush = 1;
  1470. msg.rxForward = 0;
  1471. msg.returnStatus = 0;
  1472. msg.resetDataToggle = 0;
  1473. msg.enablePort = 0;
  1474. msg.disablePort = 1;
  1475. }
  1476. /* Sending intermediate configs */
  1477. else {
  1478. msg._txOn = (! p_priv->break_on);
  1479. msg._txOff = 0;
  1480. msg.txFlush = 0;
  1481. msg.txBreak = (p_priv->break_on);
  1482. msg.rxOn = 0;
  1483. msg.rxOff = 0;
  1484. msg.rxFlush = 0;
  1485. msg.rxForward = 0;
  1486. msg.returnStatus = 0;
  1487. msg.resetDataToggle = 0x0;
  1488. msg.enablePort = 0;
  1489. msg.disablePort = 0;
  1490. }
  1491. /* Do handshaking outputs */
  1492. msg.setRts = 0xff;
  1493. msg.rts = p_priv->rts_state;
  1494. msg.setDtr = 0xff;
  1495. msg.dtr = p_priv->dtr_state;
  1496. p_priv->resend_cont = 0;
  1497. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1498. /* send the data out the device on control endpoint */
  1499. this_urb->transfer_buffer_length = sizeof(msg);
  1500. this_urb->dev = serial->dev;
  1501. if ((err = usb_submit_urb(this_urb)) != 0) {
  1502. dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
  1503. }
  1504. #if 0
  1505. else {
  1506. dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
  1507.     outcont_urb, this_urb->transfer_buffer_length,
  1508.     usb_pipeendpoint(this_urb->pipe));
  1509. }
  1510. #endif
  1511. return (0);
  1512. }
  1513. static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
  1514. {
  1515. struct usb_serial *serial = port->serial;
  1516. struct keyspan_serial_private *s_priv;
  1517. const struct keyspan_device_details *d_details;
  1518. dbg ("%s", __FUNCTION__);
  1519. s_priv = (struct keyspan_serial_private *)(serial->private);
  1520. d_details = s_priv->device_details;
  1521. switch (d_details->msg_format) {
  1522. case msg_usa26:
  1523. keyspan_usa26_send_setup(serial, port, reset_port);
  1524. break;
  1525. case msg_usa28:
  1526. keyspan_usa28_send_setup(serial, port, reset_port);
  1527. break;
  1528. case msg_usa49:
  1529. keyspan_usa49_send_setup(serial, port, reset_port);
  1530. break;
  1531. }
  1532. }
  1533. /* Gets called by the "real" driver (ie once firmware is loaded
  1534.    and renumeration has taken place. */
  1535. static int keyspan_startup (struct usb_serial *serial)
  1536. {
  1537. int i, err;
  1538. struct usb_serial_port *port;
  1539. struct keyspan_serial_private  *s_priv;
  1540. struct keyspan_port_private *p_priv;
  1541. const struct keyspan_device_details *d_details;
  1542. dbg("%s", __FUNCTION__);
  1543. for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
  1544. if (d_details->product_id == serial->dev->descriptor.idProduct)
  1545. break;
  1546. if (d_details == NULL) {
  1547. err("%s - unknown product id %x", __FUNCTION__, serial->dev->descriptor.idProduct);
  1548. return 1;
  1549. }
  1550. /* Setup private data for serial driver */
  1551. serial->private = kmalloc(sizeof(struct keyspan_serial_private),
  1552.   GFP_KERNEL);
  1553. if (!serial->private) {
  1554. dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
  1555. return (1);
  1556. }
  1557. memset(serial->private, 0, sizeof(struct keyspan_serial_private));
  1558. s_priv = (struct keyspan_serial_private *)(serial->private);
  1559. s_priv->device_details = d_details;
  1560. /* Now setup per port private data */
  1561. for (i = 0; i < serial->num_ports; i++) {
  1562. port = &serial->port[i];
  1563. port->private = kmalloc(sizeof(struct keyspan_port_private),
  1564. GFP_KERNEL);
  1565. if (!port->private) {
  1566. dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
  1567. return (1);
  1568. }
  1569. memset(port->private, 0, sizeof(struct keyspan_port_private));
  1570. p_priv = (struct keyspan_port_private *)(port->private);
  1571. p_priv->device_details = d_details;
  1572. }
  1573. keyspan_setup_urbs(serial);
  1574. s_priv->instat_urb->dev = serial->dev;
  1575. if ((err = usb_submit_urb(s_priv->instat_urb)) != 0) {
  1576. dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
  1577. }
  1578. return (0);
  1579. }
  1580. static void keyspan_shutdown (struct usb_serial *serial)
  1581. {
  1582. int i, j;
  1583. struct usb_serial_port *port;
  1584. struct keyspan_serial_private  *s_priv;
  1585. struct keyspan_port_private *p_priv;
  1586. dbg("%s", __FUNCTION__);
  1587. s_priv = (struct keyspan_serial_private *)(serial->private);
  1588. /* Stop reading/writing urbs */
  1589. stop_urb(s_priv->instat_urb);
  1590. stop_urb(s_priv->glocont_urb);
  1591. for (i = 0; i < serial->num_ports; ++i) {
  1592. port = &serial->port[i];
  1593. p_priv = (struct keyspan_port_private *)(port->private);
  1594. stop_urb(p_priv->inack_urb);
  1595. stop_urb(p_priv->outcont_urb);
  1596. for (j = 0; j < 2; j++) {
  1597. stop_urb(p_priv->in_urbs[j]);
  1598. stop_urb(p_priv->out_urbs[j]);
  1599. }
  1600. }
  1601. /* Now free them */
  1602. if (s_priv->instat_urb)
  1603. usb_free_urb(s_priv->instat_urb);
  1604. if (s_priv->glocont_urb)
  1605. usb_free_urb(s_priv->glocont_urb);
  1606. for (i = 0; i < serial->num_ports; ++i) {
  1607. port = &serial->port[i];
  1608. p_priv = (struct keyspan_port_private *)(port->private);
  1609. if (p_priv->inack_urb)
  1610. usb_free_urb(p_priv->inack_urb);
  1611. if (p_priv->outcont_urb)
  1612. usb_free_urb(p_priv->outcont_urb);
  1613. for (j = 0; j < 2; j++) {
  1614. if (p_priv->in_urbs[j])
  1615. usb_free_urb(p_priv->in_urbs[j]);
  1616. if (p_priv->out_urbs[j])
  1617. usb_free_urb(p_priv->out_urbs[j]);
  1618. }
  1619. }
  1620. /*  dbg("Freeing serial->private."); */
  1621. kfree(serial->private);
  1622. /*  dbg("Freeing port->private."); */
  1623. /* Now free per port private data */
  1624. for (i = 0; i < serial->num_ports; i++) {
  1625. port = &serial->port[i];
  1626. kfree(port->private);
  1627. }
  1628. }
  1629. MODULE_AUTHOR( DRIVER_AUTHOR );
  1630. MODULE_DESCRIPTION( DRIVER_DESC );
  1631. MODULE_LICENSE("GPL");
  1632. MODULE_PARM(debug, "i");
  1633. MODULE_PARM_DESC(debug, "Debug enabled or not");