blob: b0415e7542c4a10278f56e3525160e8114110b0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
3 *
4 * Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is largely derived from the Belkin USB Serial Adapter Driver
12 * (see belkin_sa.[ch]). All of the information about the device was acquired
13 * by using SniffUSB on Windows98. For technical details see mct_u232.h.
14 *
15 * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
16 * do the reverse engineering and how to write a USB serial device driver.
17 *
18 * TO BE DONE, TO BE CHECKED:
19 * DTR/RTS signal handling may be incomplete or incorrect. I have mainly
20 * implemented what I have seen with SniffUSB or found in belkin_sa.c.
21 * For further TODOs check also belkin_sa.c.
22 *
23 * TEST STATUS:
24 * Basic tests have been performed with minicom/zmodem transfers and
25 * modem dialing under Linux 2.4.0-test10 (for me it works fine).
26 *
27 * 04-Nov-2003 Bill Marr <marr at flex dot com>
28 * - Mimic Windows driver by sending 2 USB 'device request' messages
29 * following normal 'baud rate change' message. This allows data to be
30 * transmitted to RS-232 devices which don't assert the 'CTS' signal.
31 *
32 * 10-Nov-2001 Wolfgang Grandegger
33 * - Fixed an endianess problem with the baudrate selection for PowerPC.
34 *
35 * 06-Dec-2001 Martin Hamilton <martinh@gnu.org>
36 * Added support for the Belkin F5U109 DB9 adaptor
37 *
38 * 30-May-2001 Greg Kroah-Hartman
39 * switched from using spinlock to a semaphore, which fixes lots of problems.
40 *
41 * 04-May-2001 Stelian Pop
42 * - Set the maximum bulk output size for Sitecom U232-P25 model to 16 bytes
43 * instead of the device reported 32 (using 32 bytes causes many data
44 * loss, Windows driver uses 16 too).
45 *
46 * 02-May-2001 Stelian Pop
47 * - Fixed the baud calculation for Sitecom U232-P25 model
48 *
49 * 08-Apr-2001 gb
50 * - Identify version on module load.
51 *
52 * 06-Jan-2001 Cornel Ciocirlan
53 * - Added support for Sitecom U232-P25 model (Product Id 0x0230)
54 * - Added support for D-Link DU-H3SP USB BAY (Product Id 0x0200)
55 *
56 * 29-Nov-2000 Greg Kroah-Hartman
57 * - Added device id table to fit with 2.4.0-test11 structure.
58 * - took out DEAL_WITH_TWO_INT_IN_ENDPOINTS #define as it's not needed
59 * (lots of things will change if/when the usb-serial core changes to
60 * handle these issues.
61 *
62 * 27-Nov-2000 Wolfgang Grandegger
63 * A version for kernel 2.4.0-test10 released to the Linux community
64 * (via linux-usb-devel).
65 */
66
67#include <linux/config.h>
68#include <linux/kernel.h>
69#include <linux/errno.h>
70#include <linux/init.h>
71#include <linux/slab.h>
72#include <linux/tty.h>
73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h>
75#include <linux/module.h>
76#include <linux/spinlock.h>
77#include <asm/uaccess.h>
78#include <linux/usb.h>
79#include "usb-serial.h"
80#include "mct_u232.h"
81
82/*
83 * Version Information
84 */
85#define DRIVER_VERSION "z2.0" /* Linux in-kernel version */
86#define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
87#define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
88
89static int debug;
90
91/*
92 * Function prototypes
93 */
94static int mct_u232_startup (struct usb_serial *serial);
95static void mct_u232_shutdown (struct usb_serial *serial);
96static int mct_u232_open (struct usb_serial_port *port,
97 struct file *filp);
98static void mct_u232_close (struct usb_serial_port *port,
99 struct file *filp);
100static void mct_u232_read_int_callback (struct urb *urb, struct pt_regs *regs);
101static void mct_u232_set_termios (struct usb_serial_port *port,
102 struct termios * old);
103static int mct_u232_ioctl (struct usb_serial_port *port,
104 struct file * file,
105 unsigned int cmd,
106 unsigned long arg);
107static void mct_u232_break_ctl (struct usb_serial_port *port,
108 int break_state );
109static int mct_u232_tiocmget (struct usb_serial_port *port,
110 struct file *file);
111static int mct_u232_tiocmset (struct usb_serial_port *port,
112 struct file *file, unsigned int set,
113 unsigned int clear);
114/*
115 * All of the device info needed for the MCT USB-RS232 converter.
116 */
117static struct usb_device_id id_table_combined [] = {
118 { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
119 { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
120 { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
121 { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
122 { } /* Terminating entry */
123};
124
125MODULE_DEVICE_TABLE (usb, id_table_combined);
126
127static struct usb_driver mct_u232_driver = {
128 .owner = THIS_MODULE,
129 .name = "mct_u232",
130 .probe = usb_serial_probe,
131 .disconnect = usb_serial_disconnect,
132 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800133 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700136static struct usb_serial_driver mct_u232_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700137 .driver = {
138 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700139 .name = "mct_u232",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700140 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700141 .description = "MCT U232",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .id_table = id_table_combined,
143 .num_interrupt_in = 2,
144 .num_bulk_in = 0,
145 .num_bulk_out = 1,
146 .num_ports = 1,
147 .open = mct_u232_open,
148 .close = mct_u232_close,
149 .read_int_callback = mct_u232_read_int_callback,
150 .ioctl = mct_u232_ioctl,
151 .set_termios = mct_u232_set_termios,
152 .break_ctl = mct_u232_break_ctl,
153 .tiocmget = mct_u232_tiocmget,
154 .tiocmset = mct_u232_tiocmset,
155 .attach = mct_u232_startup,
156 .shutdown = mct_u232_shutdown,
157};
158
159
160struct mct_u232_private {
161 spinlock_t lock;
162 unsigned int control_state; /* Modem Line Setting (TIOCM) */
163 unsigned char last_lcr; /* Line Control Register */
164 unsigned char last_lsr; /* Line Status Register */
165 unsigned char last_msr; /* Modem Status Register */
166};
167
168/*
169 * Handle vendor specific USB requests
170 */
171
172#define WDR_TIMEOUT 5000 /* default urb timeout */
173
174/*
175 * Later day 2.6.0-test kernels have new baud rates like B230400 which
176 * we do not know how to support. We ignore them for the moment.
177 * XXX Rate-limit the error message, it's user triggerable.
178 */
179static int mct_u232_calculate_baud_rate(struct usb_serial *serial, int value)
180{
181 if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
182 || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
183 switch (value) {
184 case B300: return 0x01;
185 case B600: return 0x02; /* this one not tested */
186 case B1200: return 0x03;
187 case B2400: return 0x04;
188 case B4800: return 0x06;
189 case B9600: return 0x08;
190 case B19200: return 0x09;
191 case B38400: return 0x0a;
192 case B57600: return 0x0b;
193 case B115200: return 0x0c;
194 default:
195 err("MCT USB-RS232: unsupported baudrate request 0x%x,"
196 " using default of B9600", value);
197 return 0x08;
198 }
199 } else {
200 switch (value) {
201 case B300: value = 300; break;
202 case B600: value = 600; break;
203 case B1200: value = 1200; break;
204 case B2400: value = 2400; break;
205 case B4800: value = 4800; break;
206 case B9600: value = 9600; break;
207 case B19200: value = 19200; break;
208 case B38400: value = 38400; break;
209 case B57600: value = 57600; break;
210 case B115200: value = 115200; break;
211 default:
212 err("MCT USB-RS232: unsupported baudrate request 0x%x,"
213 " using default of B9600", value);
214 value = 9600;
215 }
216 return 115200/value;
217 }
218}
219
220static int mct_u232_set_baud_rate(struct usb_serial *serial, int value)
221{
222 __le32 divisor;
223 int rc;
224 unsigned char zero_byte = 0;
225
226 divisor = cpu_to_le32(mct_u232_calculate_baud_rate(serial, value));
227
228 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
229 MCT_U232_SET_BAUD_RATE_REQUEST,
230 MCT_U232_SET_REQUEST_TYPE,
231 0, 0, &divisor, MCT_U232_SET_BAUD_RATE_SIZE,
232 WDR_TIMEOUT);
233 if (rc < 0)
234 err("Set BAUD RATE %d failed (error = %d)", value, rc);
235 dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor);
236
237 /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
238 always sends two extra USB 'device request' messages after the
239 'baud rate change' message. The actual functionality of the
240 request codes in these messages is not fully understood but these
241 particular codes are never seen in any operation besides a baud
242 rate change. Both of these messages send a single byte of data
243 whose value is always zero. The second of these two extra messages
244 is required in order for data to be properly written to an RS-232
245 device which does not assert the 'CTS' signal. */
246
247 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
248 MCT_U232_SET_UNKNOWN1_REQUEST,
249 MCT_U232_SET_REQUEST_TYPE,
250 0, 0, &zero_byte, MCT_U232_SET_UNKNOWN1_SIZE,
251 WDR_TIMEOUT);
252 if (rc < 0)
253 err("Sending USB device request code %d failed (error = %d)",
254 MCT_U232_SET_UNKNOWN1_REQUEST, rc);
255
256 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
257 MCT_U232_SET_UNKNOWN2_REQUEST,
258 MCT_U232_SET_REQUEST_TYPE,
259 0, 0, &zero_byte, MCT_U232_SET_UNKNOWN2_SIZE,
260 WDR_TIMEOUT);
261 if (rc < 0)
262 err("Sending USB device request code %d failed (error = %d)",
263 MCT_U232_SET_UNKNOWN2_REQUEST, rc);
264
265 return rc;
266} /* mct_u232_set_baud_rate */
267
268static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr)
269{
270 int rc;
271 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
272 MCT_U232_SET_LINE_CTRL_REQUEST,
273 MCT_U232_SET_REQUEST_TYPE,
274 0, 0, &lcr, MCT_U232_SET_LINE_CTRL_SIZE,
275 WDR_TIMEOUT);
276 if (rc < 0)
277 err("Set LINE CTRL 0x%x failed (error = %d)", lcr, rc);
278 dbg("set_line_ctrl: 0x%x", lcr);
279 return rc;
280} /* mct_u232_set_line_ctrl */
281
282static int mct_u232_set_modem_ctrl(struct usb_serial *serial,
283 unsigned int control_state)
284{
285 int rc;
286 unsigned char mcr = MCT_U232_MCR_NONE;
287
288 if (control_state & TIOCM_DTR)
289 mcr |= MCT_U232_MCR_DTR;
290 if (control_state & TIOCM_RTS)
291 mcr |= MCT_U232_MCR_RTS;
292
293 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
294 MCT_U232_SET_MODEM_CTRL_REQUEST,
295 MCT_U232_SET_REQUEST_TYPE,
296 0, 0, &mcr, MCT_U232_SET_MODEM_CTRL_SIZE,
297 WDR_TIMEOUT);
298 if (rc < 0)
299 err("Set MODEM CTRL 0x%x failed (error = %d)", mcr, rc);
300 dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr);
301
302 return rc;
303} /* mct_u232_set_modem_ctrl */
304
305static int mct_u232_get_modem_stat(struct usb_serial *serial, unsigned char *msr)
306{
307 int rc;
308 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
309 MCT_U232_GET_MODEM_STAT_REQUEST,
310 MCT_U232_GET_REQUEST_TYPE,
311 0, 0, msr, MCT_U232_GET_MODEM_STAT_SIZE,
312 WDR_TIMEOUT);
313 if (rc < 0) {
314 err("Get MODEM STATus failed (error = %d)", rc);
315 *msr = 0;
316 }
317 dbg("get_modem_stat: 0x%x", *msr);
318 return rc;
319} /* mct_u232_get_modem_stat */
320
321static void mct_u232_msr_to_state(unsigned int *control_state, unsigned char msr)
322{
323 /* Translate Control Line states */
324 if (msr & MCT_U232_MSR_DSR)
325 *control_state |= TIOCM_DSR;
326 else
327 *control_state &= ~TIOCM_DSR;
328 if (msr & MCT_U232_MSR_CTS)
329 *control_state |= TIOCM_CTS;
330 else
331 *control_state &= ~TIOCM_CTS;
332 if (msr & MCT_U232_MSR_RI)
333 *control_state |= TIOCM_RI;
334 else
335 *control_state &= ~TIOCM_RI;
336 if (msr & MCT_U232_MSR_CD)
337 *control_state |= TIOCM_CD;
338 else
339 *control_state &= ~TIOCM_CD;
340 dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state);
341} /* mct_u232_msr_to_state */
342
343/*
344 * Driver's tty interface functions
345 */
346
347static int mct_u232_startup (struct usb_serial *serial)
348{
349 struct mct_u232_private *priv;
350 struct usb_serial_port *port, *rport;
351
352 priv = kmalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
353 if (!priv)
354 return -ENOMEM;
355 memset(priv, 0, sizeof(struct mct_u232_private));
356 spin_lock_init(&priv->lock);
357 usb_set_serial_port_data(serial->port[0], priv);
358
359 init_waitqueue_head(&serial->port[0]->write_wait);
360
361 /* Puh, that's dirty */
362 port = serial->port[0];
363 rport = serial->port[1];
364 if (port->read_urb) {
365 /* No unlinking, it wasn't submitted yet. */
366 usb_free_urb(port->read_urb);
367 }
368 port->read_urb = rport->interrupt_in_urb;
369 rport->interrupt_in_urb = NULL;
370 port->read_urb->context = port;
371
372 return (0);
373} /* mct_u232_startup */
374
375
376static void mct_u232_shutdown (struct usb_serial *serial)
377{
378 struct mct_u232_private *priv;
379 int i;
380
381 dbg("%s", __FUNCTION__);
382
383 for (i=0; i < serial->num_ports; ++i) {
384 /* My special items, the standard routines free my urbs */
385 priv = usb_get_serial_port_data(serial->port[i]);
386 if (priv) {
387 usb_set_serial_port_data(serial->port[i], NULL);
388 kfree(priv);
389 }
390 }
391} /* mct_u232_shutdown */
392
393static int mct_u232_open (struct usb_serial_port *port, struct file *filp)
394{
395 struct usb_serial *serial = port->serial;
396 struct mct_u232_private *priv = usb_get_serial_port_data(port);
397 int retval = 0;
398 unsigned int control_state;
399 unsigned long flags;
400 unsigned char last_lcr;
401 unsigned char last_msr;
402
403 dbg("%s port %d", __FUNCTION__, port->number);
404
405 /* Compensate for a hardware bug: although the Sitecom U232-P25
406 * device reports a maximum output packet size of 32 bytes,
407 * it seems to be able to accept only 16 bytes (and that's what
408 * SniffUSB says too...)
409 */
410 if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID)
411 port->bulk_out_size = 16;
412
413 /* Do a defined restart: the normal serial device seems to
414 * always turn on DTR and RTS here, so do the same. I'm not
415 * sure if this is really necessary. But it should not harm
416 * either.
417 */
418 spin_lock_irqsave(&priv->lock, flags);
419 if (port->tty->termios->c_cflag & CBAUD)
420 priv->control_state = TIOCM_DTR | TIOCM_RTS;
421 else
422 priv->control_state = 0;
423
424 priv->last_lcr = (MCT_U232_DATA_BITS_8 |
425 MCT_U232_PARITY_NONE |
426 MCT_U232_STOP_BITS_1);
427 control_state = priv->control_state;
428 last_lcr = priv->last_lcr;
429 spin_unlock_irqrestore(&priv->lock, flags);
430 mct_u232_set_modem_ctrl(serial, control_state);
431 mct_u232_set_line_ctrl(serial, last_lcr);
432
433 /* Read modem status and update control state */
434 mct_u232_get_modem_stat(serial, &last_msr);
435 spin_lock_irqsave(&priv->lock, flags);
436 priv->last_msr = last_msr;
437 mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
438 spin_unlock_irqrestore(&priv->lock, flags);
439
440 port->read_urb->dev = port->serial->dev;
441 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
442 if (retval) {
443 err("usb_submit_urb(read bulk) failed pipe 0x%x err %d",
444 port->read_urb->pipe, retval);
445 goto exit;
446 }
447
448 port->interrupt_in_urb->dev = port->serial->dev;
449 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
450 if (retval)
451 err(" usb_submit_urb(read int) failed pipe 0x%x err %d",
452 port->interrupt_in_urb->pipe, retval);
453
454exit:
455 return 0;
456} /* mct_u232_open */
457
458
459static void mct_u232_close (struct usb_serial_port *port, struct file *filp)
460{
461 dbg("%s port %d", __FUNCTION__, port->number);
462
463 if (port->serial->dev) {
464 /* shutdown our urbs */
465 usb_kill_urb(port->write_urb);
466 usb_kill_urb(port->read_urb);
467 usb_kill_urb(port->interrupt_in_urb);
468 }
469} /* mct_u232_close */
470
471
472static void mct_u232_read_int_callback (struct urb *urb, struct pt_regs *regs)
473{
474 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
475 struct mct_u232_private *priv = usb_get_serial_port_data(port);
476 struct usb_serial *serial = port->serial;
477 struct tty_struct *tty;
478 unsigned char *data = urb->transfer_buffer;
479 int status;
480 unsigned long flags;
481
482 switch (urb->status) {
483 case 0:
484 /* success */
485 break;
486 case -ECONNRESET:
487 case -ENOENT:
488 case -ESHUTDOWN:
489 /* this urb is terminated, clean up */
490 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
491 return;
492 default:
493 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
494 goto exit;
495 }
496
497 if (!serial) {
498 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
499 return;
500 }
501
502 dbg("%s - port %d", __FUNCTION__, port->number);
503 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
504
505 /*
506 * Work-a-round: handle the 'usual' bulk-in pipe here
507 */
508 if (urb->transfer_buffer_length > 2) {
509 int i;
510 tty = port->tty;
511 if (urb->actual_length) {
512 for (i = 0; i < urb->actual_length ; ++i) {
513 tty_insert_flip_char(tty, data[i], 0);
514 }
515 tty_flip_buffer_push(tty);
516 }
517 goto exit;
518 }
519
520 /*
521 * The interrupt-in pipe signals exceptional conditions (modem line
522 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
523 */
524 spin_lock_irqsave(&priv->lock, flags);
525 priv->last_msr = data[MCT_U232_MSR_INDEX];
526
527 /* Record Control Line states */
528 mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
529
530#if 0
531 /* Not yet handled. See belin_sa.c for further information */
532 /* Now to report any errors */
533 priv->last_lsr = data[MCT_U232_LSR_INDEX];
534 /*
535 * fill in the flip buffer here, but I do not know the relation
536 * to the current/next receive buffer or characters. I need
537 * to look in to this before committing any code.
538 */
539 if (priv->last_lsr & MCT_U232_LSR_ERR) {
540 tty = port->tty;
541 /* Overrun Error */
542 if (priv->last_lsr & MCT_U232_LSR_OE) {
543 }
544 /* Parity Error */
545 if (priv->last_lsr & MCT_U232_LSR_PE) {
546 }
547 /* Framing Error */
548 if (priv->last_lsr & MCT_U232_LSR_FE) {
549 }
550 /* Break Indicator */
551 if (priv->last_lsr & MCT_U232_LSR_BI) {
552 }
553 }
554#endif
555 spin_unlock_irqrestore(&priv->lock, flags);
556exit:
557 status = usb_submit_urb (urb, GFP_ATOMIC);
558 if (status)
559 err ("%s - usb_submit_urb failed with result %d",
560 __FUNCTION__, status);
561} /* mct_u232_read_int_callback */
562
563static void mct_u232_set_termios (struct usb_serial_port *port,
564 struct termios *old_termios)
565{
566 struct usb_serial *serial = port->serial;
567 struct mct_u232_private *priv = usb_get_serial_port_data(port);
568 unsigned int iflag = port->tty->termios->c_iflag;
569 unsigned int cflag = port->tty->termios->c_cflag;
570 unsigned int old_cflag = old_termios->c_cflag;
571 unsigned long flags;
572 unsigned int control_state, new_state;
573 unsigned char last_lcr;
574
575 /* get a local copy of the current port settings */
576 spin_lock_irqsave(&priv->lock, flags);
577 control_state = priv->control_state;
578 spin_unlock_irqrestore(&priv->lock, flags);
579 last_lcr = 0;
580
581 /*
582 * Update baud rate.
583 * Do not attempt to cache old rates and skip settings,
584 * disconnects screw such tricks up completely.
585 * Premature optimization is the root of all evil.
586 */
587
588 /* reassert DTR and (maybe) RTS on transition from B0 */
589 if ((old_cflag & CBAUD) == B0) {
590 dbg("%s: baud was B0", __FUNCTION__);
591 control_state |= TIOCM_DTR;
592 /* don't set RTS if using hardware flow control */
593 if (!(old_cflag & CRTSCTS)) {
594 control_state |= TIOCM_RTS;
595 }
596 mct_u232_set_modem_ctrl(serial, control_state);
597 }
598
599 mct_u232_set_baud_rate(serial, cflag & CBAUD);
600
601 if ((cflag & CBAUD) == B0 ) {
602 dbg("%s: baud is B0", __FUNCTION__);
603 /* Drop RTS and DTR */
604 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
605 mct_u232_set_modem_ctrl(serial, control_state);
606 }
607
608 /*
609 * Update line control register (LCR)
610 */
611
612 /* set the parity */
613 if (cflag & PARENB)
614 last_lcr |= (cflag & PARODD) ?
615 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
616 else
617 last_lcr |= MCT_U232_PARITY_NONE;
618
619 /* set the number of data bits */
620 switch (cflag & CSIZE) {
621 case CS5:
622 last_lcr |= MCT_U232_DATA_BITS_5; break;
623 case CS6:
624 last_lcr |= MCT_U232_DATA_BITS_6; break;
625 case CS7:
626 last_lcr |= MCT_U232_DATA_BITS_7; break;
627 case CS8:
628 last_lcr |= MCT_U232_DATA_BITS_8; break;
629 default:
630 err("CSIZE was not CS5-CS8, using default of 8");
631 last_lcr |= MCT_U232_DATA_BITS_8;
632 break;
633 }
634
635 /* set the number of stop bits */
636 last_lcr |= (cflag & CSTOPB) ?
637 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
638
639 mct_u232_set_line_ctrl(serial, last_lcr);
640
641 /*
642 * Set flow control: well, I do not really now how to handle DTR/RTS.
643 * Just do what we have seen with SniffUSB on Win98.
644 */
645 /* Drop DTR/RTS if no flow control otherwise assert */
646 new_state = control_state;
647 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
648 new_state |= TIOCM_DTR | TIOCM_RTS;
649 else
650 new_state &= ~(TIOCM_DTR | TIOCM_RTS);
651 if (new_state != control_state) {
652 mct_u232_set_modem_ctrl(serial, new_state);
653 control_state = new_state;
654 }
655
656 /* save off the modified port settings */
657 spin_lock_irqsave(&priv->lock, flags);
658 priv->control_state = control_state;
659 priv->last_lcr = last_lcr;
660 spin_unlock_irqrestore(&priv->lock, flags);
661} /* mct_u232_set_termios */
662
663static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
664{
665 struct usb_serial *serial = port->serial;
666 struct mct_u232_private *priv = usb_get_serial_port_data(port);
667 unsigned char lcr;
668 unsigned long flags;
669
670 dbg("%sstate=%d", __FUNCTION__, break_state);
671
672 spin_lock_irqsave(&priv->lock, flags);
673 lcr = priv->last_lcr;
674 spin_unlock_irqrestore(&priv->lock, flags);
675
676 if (break_state)
677 lcr |= MCT_U232_SET_BREAK;
678
679 mct_u232_set_line_ctrl(serial, lcr);
680} /* mct_u232_break_ctl */
681
682
683static int mct_u232_tiocmget (struct usb_serial_port *port, struct file *file)
684{
685 struct mct_u232_private *priv = usb_get_serial_port_data(port);
686 unsigned int control_state;
687 unsigned long flags;
688
689 dbg("%s", __FUNCTION__);
690
691 spin_lock_irqsave(&priv->lock, flags);
692 control_state = priv->control_state;
693 spin_unlock_irqrestore(&priv->lock, flags);
694
695 return control_state;
696}
697
698static int mct_u232_tiocmset (struct usb_serial_port *port, struct file *file,
699 unsigned int set, unsigned int clear)
700{
701 struct usb_serial *serial = port->serial;
702 struct mct_u232_private *priv = usb_get_serial_port_data(port);
703 unsigned int control_state;
704 unsigned long flags;
705
706 dbg("%s", __FUNCTION__);
707
708 spin_lock_irqsave(&priv->lock, flags);
709 control_state = priv->control_state;
710
711 if (set & TIOCM_RTS)
712 control_state |= TIOCM_RTS;
713 if (set & TIOCM_DTR)
714 control_state |= TIOCM_DTR;
715 if (clear & TIOCM_RTS)
716 control_state &= ~TIOCM_RTS;
717 if (clear & TIOCM_DTR)
718 control_state &= ~TIOCM_DTR;
719
720 priv->control_state = control_state;
721 spin_unlock_irqrestore(&priv->lock, flags);
722 return mct_u232_set_modem_ctrl(serial, control_state);
723}
724
725static int mct_u232_ioctl (struct usb_serial_port *port, struct file * file,
726 unsigned int cmd, unsigned long arg)
727{
728 dbg("%scmd=0x%x", __FUNCTION__, cmd);
729
730 /* Based on code from acm.c and others */
731 switch (cmd) {
732 case TIOCMIWAIT:
733 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
734 /* TODO */
735 return( 0 );
736
737 case TIOCGICOUNT:
738 /* return count of modemline transitions */
739 /* TODO */
740 return 0;
741
742 default:
743 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
744 return(-ENOIOCTLCMD);
745 break;
746 }
747 return 0;
748} /* mct_u232_ioctl */
749
750
751static int __init mct_u232_init (void)
752{
753 int retval;
754 retval = usb_serial_register(&mct_u232_device);
755 if (retval)
756 goto failed_usb_serial_register;
757 retval = usb_register(&mct_u232_driver);
758 if (retval)
759 goto failed_usb_register;
760 info(DRIVER_DESC " " DRIVER_VERSION);
761 return 0;
762failed_usb_register:
763 usb_serial_deregister(&mct_u232_device);
764failed_usb_serial_register:
765 return retval;
766}
767
768
769static void __exit mct_u232_exit (void)
770{
771 usb_deregister (&mct_u232_driver);
772 usb_serial_deregister (&mct_u232_device);
773}
774
775
776module_init (mct_u232_init);
777module_exit(mct_u232_exit);
778
779MODULE_AUTHOR( DRIVER_AUTHOR );
780MODULE_DESCRIPTION( DRIVER_DESC );
781MODULE_LICENSE("GPL");
782
783module_param(debug, bool, S_IRUGO | S_IWUSR);
784MODULE_PARM_DESC(debug, "Debug enabled or not");