0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #include <linux/kernel.h>
0025 #include <linux/errno.h>
0026 #include <linux/slab.h>
0027 #include <linux/tty.h>
0028 #include <linux/tty_driver.h>
0029 #include <linux/tty_flip.h>
0030 #include <linux/module.h>
0031 #include <linux/moduleparam.h>
0032 #include <linux/spinlock.h>
0033 #include <linux/usb.h>
0034 #include <linux/usb/serial.h>
0035 #include <linux/serial.h>
0036 #include <linux/kfifo.h>
0037 #include <linux/delay.h>
0038 #include <linux/uaccess.h>
0039 #include <asm/unaligned.h>
0040
0041 #include "cypress_m8.h"
0042
0043
0044 static bool stats;
0045 static int interval;
0046 static bool unstable_bauds;
0047
0048 #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
0049 #define DRIVER_DESC "Cypress USB to Serial Driver"
0050
0051
0052 #define CYPRESS_BUF_SIZE 1024
0053
0054 static const struct usb_device_id id_table_earthmate[] = {
0055 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
0056 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
0057 { }
0058 };
0059
0060 static const struct usb_device_id id_table_cyphidcomrs232[] = {
0061 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
0062 { USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
0063 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
0064 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
0065 { }
0066 };
0067
0068 static const struct usb_device_id id_table_nokiaca42v2[] = {
0069 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
0070 { }
0071 };
0072
0073 static const struct usb_device_id id_table_combined[] = {
0074 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
0075 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
0076 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
0077 { USB_DEVICE(VENDOR_ID_SAI, PRODUCT_ID_CYPHIDCOM) },
0078 { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
0079 { USB_DEVICE(VENDOR_ID_FRWD, PRODUCT_ID_CYPHIDCOM_FRWD) },
0080 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
0081 { }
0082 };
0083
0084 MODULE_DEVICE_TABLE(usb, id_table_combined);
0085
0086 enum packet_format {
0087 packet_format_1,
0088 packet_format_2
0089 };
0090
0091 struct cypress_private {
0092 spinlock_t lock;
0093 int chiptype;
0094 int bytes_in;
0095 int bytes_out;
0096 int cmd_count;
0097 int cmd_ctrl;
0098 struct kfifo write_fifo;
0099 int write_urb_in_use;
0100 int write_urb_interval;
0101 int read_urb_interval;
0102 int comm_is_ok;
0103 __u8 line_control;
0104 __u8 current_status;
0105 __u8 current_config;
0106 __u8 rx_flags;
0107 enum packet_format pkt_fmt;
0108 int get_cfg_unsafe;
0109 int baud_rate;
0110
0111 char prev_status;
0112 };
0113
0114
0115 static int cypress_earthmate_port_probe(struct usb_serial_port *port);
0116 static int cypress_hidcom_port_probe(struct usb_serial_port *port);
0117 static int cypress_ca42v2_port_probe(struct usb_serial_port *port);
0118 static void cypress_port_remove(struct usb_serial_port *port);
0119 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
0120 static void cypress_close(struct usb_serial_port *port);
0121 static void cypress_dtr_rts(struct usb_serial_port *port, int on);
0122 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
0123 const unsigned char *buf, int count);
0124 static void cypress_send(struct usb_serial_port *port);
0125 static unsigned int cypress_write_room(struct tty_struct *tty);
0126 static void cypress_earthmate_init_termios(struct tty_struct *tty);
0127 static void cypress_set_termios(struct tty_struct *tty,
0128 struct usb_serial_port *port, struct ktermios *old);
0129 static int cypress_tiocmget(struct tty_struct *tty);
0130 static int cypress_tiocmset(struct tty_struct *tty,
0131 unsigned int set, unsigned int clear);
0132 static unsigned int cypress_chars_in_buffer(struct tty_struct *tty);
0133 static void cypress_throttle(struct tty_struct *tty);
0134 static void cypress_unthrottle(struct tty_struct *tty);
0135 static void cypress_set_dead(struct usb_serial_port *port);
0136 static void cypress_read_int_callback(struct urb *urb);
0137 static void cypress_write_int_callback(struct urb *urb);
0138
0139 static struct usb_serial_driver cypress_earthmate_device = {
0140 .driver = {
0141 .owner = THIS_MODULE,
0142 .name = "earthmate",
0143 },
0144 .description = "DeLorme Earthmate USB",
0145 .id_table = id_table_earthmate,
0146 .num_ports = 1,
0147 .port_probe = cypress_earthmate_port_probe,
0148 .port_remove = cypress_port_remove,
0149 .open = cypress_open,
0150 .close = cypress_close,
0151 .dtr_rts = cypress_dtr_rts,
0152 .write = cypress_write,
0153 .write_room = cypress_write_room,
0154 .init_termios = cypress_earthmate_init_termios,
0155 .set_termios = cypress_set_termios,
0156 .tiocmget = cypress_tiocmget,
0157 .tiocmset = cypress_tiocmset,
0158 .tiocmiwait = usb_serial_generic_tiocmiwait,
0159 .chars_in_buffer = cypress_chars_in_buffer,
0160 .throttle = cypress_throttle,
0161 .unthrottle = cypress_unthrottle,
0162 .read_int_callback = cypress_read_int_callback,
0163 .write_int_callback = cypress_write_int_callback,
0164 };
0165
0166 static struct usb_serial_driver cypress_hidcom_device = {
0167 .driver = {
0168 .owner = THIS_MODULE,
0169 .name = "cyphidcom",
0170 },
0171 .description = "HID->COM RS232 Adapter",
0172 .id_table = id_table_cyphidcomrs232,
0173 .num_ports = 1,
0174 .port_probe = cypress_hidcom_port_probe,
0175 .port_remove = cypress_port_remove,
0176 .open = cypress_open,
0177 .close = cypress_close,
0178 .dtr_rts = cypress_dtr_rts,
0179 .write = cypress_write,
0180 .write_room = cypress_write_room,
0181 .set_termios = cypress_set_termios,
0182 .tiocmget = cypress_tiocmget,
0183 .tiocmset = cypress_tiocmset,
0184 .tiocmiwait = usb_serial_generic_tiocmiwait,
0185 .chars_in_buffer = cypress_chars_in_buffer,
0186 .throttle = cypress_throttle,
0187 .unthrottle = cypress_unthrottle,
0188 .read_int_callback = cypress_read_int_callback,
0189 .write_int_callback = cypress_write_int_callback,
0190 };
0191
0192 static struct usb_serial_driver cypress_ca42v2_device = {
0193 .driver = {
0194 .owner = THIS_MODULE,
0195 .name = "nokiaca42v2",
0196 },
0197 .description = "Nokia CA-42 V2 Adapter",
0198 .id_table = id_table_nokiaca42v2,
0199 .num_ports = 1,
0200 .port_probe = cypress_ca42v2_port_probe,
0201 .port_remove = cypress_port_remove,
0202 .open = cypress_open,
0203 .close = cypress_close,
0204 .dtr_rts = cypress_dtr_rts,
0205 .write = cypress_write,
0206 .write_room = cypress_write_room,
0207 .set_termios = cypress_set_termios,
0208 .tiocmget = cypress_tiocmget,
0209 .tiocmset = cypress_tiocmset,
0210 .tiocmiwait = usb_serial_generic_tiocmiwait,
0211 .chars_in_buffer = cypress_chars_in_buffer,
0212 .throttle = cypress_throttle,
0213 .unthrottle = cypress_unthrottle,
0214 .read_int_callback = cypress_read_int_callback,
0215 .write_int_callback = cypress_write_int_callback,
0216 };
0217
0218 static struct usb_serial_driver * const serial_drivers[] = {
0219 &cypress_earthmate_device, &cypress_hidcom_device,
0220 &cypress_ca42v2_device, NULL
0221 };
0222
0223
0224
0225
0226
0227
0228 static inline bool is_frwd(struct usb_device *dev)
0229 {
0230 return ((le16_to_cpu(dev->descriptor.idVendor) == VENDOR_ID_FRWD) &&
0231 (le16_to_cpu(dev->descriptor.idProduct) == PRODUCT_ID_CYPHIDCOM_FRWD));
0232 }
0233
0234 static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
0235 {
0236 struct cypress_private *priv;
0237 priv = usb_get_serial_port_data(port);
0238
0239 if (unstable_bauds)
0240 return new_rate;
0241
0242
0243 if (is_frwd(port->serial->dev))
0244 return new_rate;
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 if (port->serial->dev->speed == USB_SPEED_LOW) {
0256
0257
0258
0259
0260
0261
0262
0263 if (new_rate > 4800) {
0264 dev_dbg(&port->dev,
0265 "%s - failed setting baud rate, device incapable speed %d\n",
0266 __func__, new_rate);
0267 return -1;
0268 }
0269 }
0270 switch (priv->chiptype) {
0271 case CT_EARTHMATE:
0272 if (new_rate <= 600) {
0273
0274
0275
0276 dev_dbg(&port->dev,
0277 "%s - failed setting baud rate, unsupported speed of %d on Earthmate GPS\n",
0278 __func__, new_rate);
0279 return -1;
0280 }
0281 break;
0282 default:
0283 break;
0284 }
0285 return new_rate;
0286 }
0287
0288
0289
0290 static int cypress_serial_control(struct tty_struct *tty,
0291 struct usb_serial_port *port, speed_t baud_rate, int data_bits,
0292 int stop_bits, int parity_enable, int parity_type, int reset,
0293 int cypress_request_type)
0294 {
0295 int new_baudrate = 0, retval = 0, tries = 0;
0296 struct cypress_private *priv;
0297 struct device *dev = &port->dev;
0298 u8 *feature_buffer;
0299 const unsigned int feature_len = 5;
0300 unsigned long flags;
0301
0302 priv = usb_get_serial_port_data(port);
0303
0304 if (!priv->comm_is_ok)
0305 return -ENODEV;
0306
0307 feature_buffer = kcalloc(feature_len, sizeof(u8), GFP_KERNEL);
0308 if (!feature_buffer)
0309 return -ENOMEM;
0310
0311 switch (cypress_request_type) {
0312 case CYPRESS_SET_CONFIG:
0313
0314 new_baudrate = priv->baud_rate;
0315 if (baud_rate && baud_rate != priv->baud_rate) {
0316 dev_dbg(dev, "%s - baud rate is changing\n", __func__);
0317 retval = analyze_baud_rate(port, baud_rate);
0318 if (retval >= 0) {
0319 new_baudrate = retval;
0320 dev_dbg(dev, "%s - New baud rate set to %d\n",
0321 __func__, new_baudrate);
0322 }
0323 }
0324 dev_dbg(dev, "%s - baud rate is being sent as %d\n", __func__,
0325 new_baudrate);
0326
0327
0328 put_unaligned_le32(new_baudrate, feature_buffer);
0329 feature_buffer[4] |= data_bits - 5;
0330
0331 feature_buffer[4] |= (stop_bits << 3);
0332 feature_buffer[4] |= (parity_enable << 4);
0333 feature_buffer[4] |= (parity_type << 5);
0334
0335 feature_buffer[4] |= (reset << 7);
0336
0337 dev_dbg(dev, "%s - device is being sent this feature report:\n", __func__);
0338 dev_dbg(dev, "%s - %02X - %02X - %02X - %02X - %02X\n", __func__,
0339 feature_buffer[0], feature_buffer[1],
0340 feature_buffer[2], feature_buffer[3],
0341 feature_buffer[4]);
0342
0343 do {
0344 retval = usb_control_msg(port->serial->dev,
0345 usb_sndctrlpipe(port->serial->dev, 0),
0346 HID_REQ_SET_REPORT,
0347 USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
0348 0x0300, 0, feature_buffer,
0349 feature_len, 500);
0350
0351 if (tries++ >= 3)
0352 break;
0353
0354 } while (retval != feature_len &&
0355 retval != -ENODEV);
0356
0357 if (retval != feature_len) {
0358 dev_err(dev, "%s - failed sending serial line settings - %d\n",
0359 __func__, retval);
0360 cypress_set_dead(port);
0361 } else {
0362 spin_lock_irqsave(&priv->lock, flags);
0363 priv->baud_rate = new_baudrate;
0364 priv->current_config = feature_buffer[4];
0365 spin_unlock_irqrestore(&priv->lock, flags);
0366
0367 if (baud_rate)
0368 tty_encode_baud_rate(tty,
0369 new_baudrate, new_baudrate);
0370 }
0371 break;
0372 case CYPRESS_GET_CONFIG:
0373 if (priv->get_cfg_unsafe) {
0374
0375
0376
0377 retval = -ENOTTY;
0378 goto out;
0379 }
0380 dev_dbg(dev, "%s - retrieving serial line settings\n", __func__);
0381 do {
0382 retval = usb_control_msg(port->serial->dev,
0383 usb_rcvctrlpipe(port->serial->dev, 0),
0384 HID_REQ_GET_REPORT,
0385 USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
0386 0x0300, 0, feature_buffer,
0387 feature_len, 500);
0388
0389 if (tries++ >= 3)
0390 break;
0391 } while (retval != feature_len
0392 && retval != -ENODEV);
0393
0394 if (retval != feature_len) {
0395 dev_err(dev, "%s - failed to retrieve serial line settings - %d\n",
0396 __func__, retval);
0397 cypress_set_dead(port);
0398 goto out;
0399 } else {
0400 spin_lock_irqsave(&priv->lock, flags);
0401
0402
0403 priv->current_config = feature_buffer[4];
0404 priv->baud_rate = get_unaligned_le32(feature_buffer);
0405 spin_unlock_irqrestore(&priv->lock, flags);
0406 }
0407 }
0408 spin_lock_irqsave(&priv->lock, flags);
0409 ++priv->cmd_count;
0410 spin_unlock_irqrestore(&priv->lock, flags);
0411 out:
0412 kfree(feature_buffer);
0413 return retval;
0414 }
0415
0416
0417 static void cypress_set_dead(struct usb_serial_port *port)
0418 {
0419 struct cypress_private *priv = usb_get_serial_port_data(port);
0420 unsigned long flags;
0421
0422 spin_lock_irqsave(&priv->lock, flags);
0423 if (!priv->comm_is_ok) {
0424 spin_unlock_irqrestore(&priv->lock, flags);
0425 return;
0426 }
0427 priv->comm_is_ok = 0;
0428 spin_unlock_irqrestore(&priv->lock, flags);
0429
0430 dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
0431 "interval might be too short\n", port->port_number);
0432 }
0433
0434
0435
0436
0437
0438
0439
0440 static int cypress_generic_port_probe(struct usb_serial_port *port)
0441 {
0442 struct usb_serial *serial = port->serial;
0443 struct cypress_private *priv;
0444
0445 if (!port->interrupt_out_urb || !port->interrupt_in_urb) {
0446 dev_err(&port->dev, "required endpoint is missing\n");
0447 return -ENODEV;
0448 }
0449
0450 priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
0451 if (!priv)
0452 return -ENOMEM;
0453
0454 priv->comm_is_ok = !0;
0455 spin_lock_init(&priv->lock);
0456 if (kfifo_alloc(&priv->write_fifo, CYPRESS_BUF_SIZE, GFP_KERNEL)) {
0457 kfree(priv);
0458 return -ENOMEM;
0459 }
0460
0461
0462
0463
0464 if (!is_frwd(serial->dev))
0465 usb_reset_configuration(serial->dev);
0466
0467 priv->cmd_ctrl = 0;
0468 priv->line_control = 0;
0469 priv->rx_flags = 0;
0470
0471
0472
0473
0474
0475
0476 if (port->interrupt_out_size > 9)
0477 priv->pkt_fmt = packet_format_1;
0478 else
0479 priv->pkt_fmt = packet_format_2;
0480
0481 if (interval > 0) {
0482 priv->write_urb_interval = interval;
0483 priv->read_urb_interval = interval;
0484 dev_dbg(&port->dev, "%s - read & write intervals forced to %d\n",
0485 __func__, interval);
0486 } else {
0487 priv->write_urb_interval = port->interrupt_out_urb->interval;
0488 priv->read_urb_interval = port->interrupt_in_urb->interval;
0489 dev_dbg(&port->dev, "%s - intervals: read=%d write=%d\n",
0490 __func__, priv->read_urb_interval,
0491 priv->write_urb_interval);
0492 }
0493 usb_set_serial_port_data(port, priv);
0494
0495 port->port.drain_delay = 256;
0496
0497 return 0;
0498 }
0499
0500
0501 static int cypress_earthmate_port_probe(struct usb_serial_port *port)
0502 {
0503 struct usb_serial *serial = port->serial;
0504 struct cypress_private *priv;
0505 int ret;
0506
0507 ret = cypress_generic_port_probe(port);
0508 if (ret) {
0509 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
0510 return ret;
0511 }
0512
0513 priv = usb_get_serial_port_data(port);
0514 priv->chiptype = CT_EARTHMATE;
0515
0516
0517 priv->pkt_fmt = packet_format_1;
0518 if (serial->dev->descriptor.idProduct !=
0519 cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
0520
0521
0522
0523
0524 dev_dbg(&port->dev,
0525 "%s - Marking this device as unsafe for GET_CONFIG commands\n",
0526 __func__);
0527 priv->get_cfg_unsafe = !0;
0528 }
0529
0530 return 0;
0531 }
0532
0533 static int cypress_hidcom_port_probe(struct usb_serial_port *port)
0534 {
0535 struct cypress_private *priv;
0536 int ret;
0537
0538 ret = cypress_generic_port_probe(port);
0539 if (ret) {
0540 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
0541 return ret;
0542 }
0543
0544 priv = usb_get_serial_port_data(port);
0545 priv->chiptype = CT_CYPHIDCOM;
0546
0547 return 0;
0548 }
0549
0550 static int cypress_ca42v2_port_probe(struct usb_serial_port *port)
0551 {
0552 struct cypress_private *priv;
0553 int ret;
0554
0555 ret = cypress_generic_port_probe(port);
0556 if (ret) {
0557 dev_dbg(&port->dev, "%s - Failed setting up port\n", __func__);
0558 return ret;
0559 }
0560
0561 priv = usb_get_serial_port_data(port);
0562 priv->chiptype = CT_CA42V2;
0563
0564 return 0;
0565 }
0566
0567 static void cypress_port_remove(struct usb_serial_port *port)
0568 {
0569 struct cypress_private *priv;
0570
0571 priv = usb_get_serial_port_data(port);
0572
0573 kfifo_free(&priv->write_fifo);
0574 kfree(priv);
0575 }
0576
0577 static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
0578 {
0579 struct cypress_private *priv = usb_get_serial_port_data(port);
0580 struct usb_serial *serial = port->serial;
0581 unsigned long flags;
0582 int result = 0;
0583
0584 if (!priv->comm_is_ok)
0585 return -EIO;
0586
0587
0588 usb_clear_halt(serial->dev, 0x81);
0589 usb_clear_halt(serial->dev, 0x02);
0590
0591 spin_lock_irqsave(&priv->lock, flags);
0592
0593 priv->bytes_in = 0;
0594 priv->bytes_out = 0;
0595 priv->cmd_count = 0;
0596 priv->rx_flags = 0;
0597 spin_unlock_irqrestore(&priv->lock, flags);
0598
0599
0600 cypress_send(port);
0601
0602 if (tty)
0603 cypress_set_termios(tty, port, NULL);
0604
0605
0606 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
0607 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
0608 port->interrupt_in_urb->transfer_buffer,
0609 port->interrupt_in_urb->transfer_buffer_length,
0610 cypress_read_int_callback, port, priv->read_urb_interval);
0611 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0612
0613 if (result) {
0614 dev_err(&port->dev,
0615 "%s - failed submitting read urb, error %d\n",
0616 __func__, result);
0617 cypress_set_dead(port);
0618 }
0619
0620 return result;
0621 }
0622
0623 static void cypress_dtr_rts(struct usb_serial_port *port, int on)
0624 {
0625 struct cypress_private *priv = usb_get_serial_port_data(port);
0626
0627 spin_lock_irq(&priv->lock);
0628 if (on == 0)
0629 priv->line_control = 0;
0630 else
0631 priv->line_control = CONTROL_DTR | CONTROL_RTS;
0632 priv->cmd_ctrl = 1;
0633 spin_unlock_irq(&priv->lock);
0634 cypress_write(NULL, port, NULL, 0);
0635 }
0636
0637 static void cypress_close(struct usb_serial_port *port)
0638 {
0639 struct cypress_private *priv = usb_get_serial_port_data(port);
0640 unsigned long flags;
0641
0642 spin_lock_irqsave(&priv->lock, flags);
0643 kfifo_reset_out(&priv->write_fifo);
0644 spin_unlock_irqrestore(&priv->lock, flags);
0645
0646 dev_dbg(&port->dev, "%s - stopping urbs\n", __func__);
0647 usb_kill_urb(port->interrupt_in_urb);
0648 usb_kill_urb(port->interrupt_out_urb);
0649
0650 if (stats)
0651 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
0652 priv->bytes_in, priv->bytes_out, priv->cmd_count);
0653 }
0654
0655
0656 static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
0657 const unsigned char *buf, int count)
0658 {
0659 struct cypress_private *priv = usb_get_serial_port_data(port);
0660
0661 dev_dbg(&port->dev, "%s - %d bytes\n", __func__, count);
0662
0663
0664
0665
0666 if (priv->cmd_ctrl) {
0667 count = 0;
0668 goto finish;
0669 }
0670
0671 if (!count)
0672 return count;
0673
0674 count = kfifo_in_locked(&priv->write_fifo, buf, count, &priv->lock);
0675
0676 finish:
0677 cypress_send(port);
0678
0679 return count;
0680 }
0681
0682
0683 static void cypress_send(struct usb_serial_port *port)
0684 {
0685 int count = 0, result, offset, actual_size;
0686 struct cypress_private *priv = usb_get_serial_port_data(port);
0687 struct device *dev = &port->dev;
0688 unsigned long flags;
0689
0690 if (!priv->comm_is_ok)
0691 return;
0692
0693 dev_dbg(dev, "%s - interrupt out size is %d\n", __func__,
0694 port->interrupt_out_size);
0695
0696 spin_lock_irqsave(&priv->lock, flags);
0697 if (priv->write_urb_in_use) {
0698 dev_dbg(dev, "%s - can't write, urb in use\n", __func__);
0699 spin_unlock_irqrestore(&priv->lock, flags);
0700 return;
0701 }
0702 spin_unlock_irqrestore(&priv->lock, flags);
0703
0704
0705 memset(port->interrupt_out_urb->transfer_buffer, 0,
0706 port->interrupt_out_size);
0707
0708 spin_lock_irqsave(&priv->lock, flags);
0709 switch (priv->pkt_fmt) {
0710 default:
0711 case packet_format_1:
0712
0713 offset = 2;
0714 port->interrupt_out_buffer[0] = priv->line_control;
0715 break;
0716 case packet_format_2:
0717
0718 offset = 1;
0719 port->interrupt_out_buffer[0] = priv->line_control;
0720 break;
0721 }
0722
0723 if (priv->line_control & CONTROL_RESET)
0724 priv->line_control &= ~CONTROL_RESET;
0725
0726 if (priv->cmd_ctrl) {
0727 priv->cmd_count++;
0728 dev_dbg(dev, "%s - line control command being issued\n", __func__);
0729 spin_unlock_irqrestore(&priv->lock, flags);
0730 goto send;
0731 } else
0732 spin_unlock_irqrestore(&priv->lock, flags);
0733
0734 count = kfifo_out_locked(&priv->write_fifo,
0735 &port->interrupt_out_buffer[offset],
0736 port->interrupt_out_size - offset,
0737 &priv->lock);
0738 if (count == 0)
0739 return;
0740
0741 switch (priv->pkt_fmt) {
0742 default:
0743 case packet_format_1:
0744 port->interrupt_out_buffer[1] = count;
0745 break;
0746 case packet_format_2:
0747 port->interrupt_out_buffer[0] |= count;
0748 }
0749
0750 dev_dbg(dev, "%s - count is %d\n", __func__, count);
0751
0752 send:
0753 spin_lock_irqsave(&priv->lock, flags);
0754 priv->write_urb_in_use = 1;
0755 spin_unlock_irqrestore(&priv->lock, flags);
0756
0757 if (priv->cmd_ctrl)
0758 actual_size = 1;
0759 else
0760 actual_size = count +
0761 (priv->pkt_fmt == packet_format_1 ? 2 : 1);
0762
0763 usb_serial_debug_data(dev, __func__, port->interrupt_out_size,
0764 port->interrupt_out_urb->transfer_buffer);
0765
0766 usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
0767 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
0768 port->interrupt_out_buffer, actual_size,
0769 cypress_write_int_callback, port, priv->write_urb_interval);
0770 result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
0771 if (result) {
0772 dev_err_console(port,
0773 "%s - failed submitting write urb, error %d\n",
0774 __func__, result);
0775 priv->write_urb_in_use = 0;
0776 cypress_set_dead(port);
0777 }
0778
0779 spin_lock_irqsave(&priv->lock, flags);
0780 if (priv->cmd_ctrl)
0781 priv->cmd_ctrl = 0;
0782
0783
0784 priv->bytes_out += count;
0785 spin_unlock_irqrestore(&priv->lock, flags);
0786
0787 usb_serial_port_softint(port);
0788 }
0789
0790
0791
0792 static unsigned int cypress_write_room(struct tty_struct *tty)
0793 {
0794 struct usb_serial_port *port = tty->driver_data;
0795 struct cypress_private *priv = usb_get_serial_port_data(port);
0796 unsigned int room;
0797 unsigned long flags;
0798
0799 spin_lock_irqsave(&priv->lock, flags);
0800 room = kfifo_avail(&priv->write_fifo);
0801 spin_unlock_irqrestore(&priv->lock, flags);
0802
0803 dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
0804 return room;
0805 }
0806
0807
0808 static int cypress_tiocmget(struct tty_struct *tty)
0809 {
0810 struct usb_serial_port *port = tty->driver_data;
0811 struct cypress_private *priv = usb_get_serial_port_data(port);
0812 __u8 status, control;
0813 unsigned int result = 0;
0814 unsigned long flags;
0815
0816 spin_lock_irqsave(&priv->lock, flags);
0817 control = priv->line_control;
0818 status = priv->current_status;
0819 spin_unlock_irqrestore(&priv->lock, flags);
0820
0821 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
0822 | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
0823 | ((status & UART_CTS) ? TIOCM_CTS : 0)
0824 | ((status & UART_DSR) ? TIOCM_DSR : 0)
0825 | ((status & UART_RI) ? TIOCM_RI : 0)
0826 | ((status & UART_CD) ? TIOCM_CD : 0);
0827
0828 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
0829
0830 return result;
0831 }
0832
0833
0834 static int cypress_tiocmset(struct tty_struct *tty,
0835 unsigned int set, unsigned int clear)
0836 {
0837 struct usb_serial_port *port = tty->driver_data;
0838 struct cypress_private *priv = usb_get_serial_port_data(port);
0839 unsigned long flags;
0840
0841 spin_lock_irqsave(&priv->lock, flags);
0842 if (set & TIOCM_RTS)
0843 priv->line_control |= CONTROL_RTS;
0844 if (set & TIOCM_DTR)
0845 priv->line_control |= CONTROL_DTR;
0846 if (clear & TIOCM_RTS)
0847 priv->line_control &= ~CONTROL_RTS;
0848 if (clear & TIOCM_DTR)
0849 priv->line_control &= ~CONTROL_DTR;
0850 priv->cmd_ctrl = 1;
0851 spin_unlock_irqrestore(&priv->lock, flags);
0852
0853 return cypress_write(tty, port, NULL, 0);
0854 }
0855
0856 static void cypress_earthmate_init_termios(struct tty_struct *tty)
0857 {
0858 tty_encode_baud_rate(tty, 4800, 4800);
0859 }
0860
0861 static void cypress_set_termios(struct tty_struct *tty,
0862 struct usb_serial_port *port, struct ktermios *old_termios)
0863 {
0864 struct cypress_private *priv = usb_get_serial_port_data(port);
0865 struct device *dev = &port->dev;
0866 int data_bits, stop_bits, parity_type, parity_enable;
0867 unsigned int cflag;
0868 unsigned long flags;
0869 __u8 oldlines;
0870 int linechange = 0;
0871
0872
0873 tty->termios.c_cflag &= ~(CMSPAR|CRTSCTS);
0874
0875 cflag = tty->termios.c_cflag;
0876
0877
0878
0879
0880
0881 stop_bits = cflag & CSTOPB ? 1 : 0;
0882
0883 if (cflag & PARENB) {
0884 parity_enable = 1;
0885
0886 parity_type = cflag & PARODD ? 1 : 0;
0887 } else
0888 parity_enable = parity_type = 0;
0889
0890 data_bits = tty_get_char_size(cflag);
0891
0892 spin_lock_irqsave(&priv->lock, flags);
0893 oldlines = priv->line_control;
0894 if ((cflag & CBAUD) == B0) {
0895
0896 dev_dbg(dev, "%s - dropping the lines, baud rate 0bps\n", __func__);
0897 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
0898 } else
0899 priv->line_control = (CONTROL_DTR | CONTROL_RTS);
0900 spin_unlock_irqrestore(&priv->lock, flags);
0901
0902 dev_dbg(dev, "%s - sending %d stop_bits, %d parity_enable, %d parity_type, %d data_bits (+5)\n",
0903 __func__, stop_bits, parity_enable, parity_type, data_bits);
0904
0905 cypress_serial_control(tty, port, tty_get_baud_rate(tty),
0906 data_bits, stop_bits,
0907 parity_enable, parity_type,
0908 0, CYPRESS_SET_CONFIG);
0909
0910
0911
0912
0913 cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
0914
0915
0916
0917
0918 spin_lock_irqsave(&priv->lock, flags);
0919 if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
0920 dev_dbg(dev, "Using custom termios settings for a baud rate of 4800bps.\n");
0921
0922
0923 tty->termios.c_iflag
0924 &= ~(IGNBRK
0925 | BRKINT
0926 | PARMRK
0927 | ISTRIP
0928 | INLCR
0929 | IGNCR
0930 | ICRNL
0931 | IXON);
0932
0933 tty->termios.c_oflag
0934 &= ~OPOST;
0935
0936 tty->termios.c_lflag
0937 &= ~(ECHO
0938 | ECHONL
0939 | ICANON
0940
0941 | ISIG
0942
0943 | IEXTEN);
0944 }
0945
0946 linechange = (priv->line_control != oldlines);
0947 spin_unlock_irqrestore(&priv->lock, flags);
0948
0949
0950 if (linechange) {
0951 priv->cmd_ctrl = 1;
0952 cypress_write(tty, port, NULL, 0);
0953 }
0954 }
0955
0956
0957
0958 static unsigned int cypress_chars_in_buffer(struct tty_struct *tty)
0959 {
0960 struct usb_serial_port *port = tty->driver_data;
0961 struct cypress_private *priv = usb_get_serial_port_data(port);
0962 unsigned int chars;
0963 unsigned long flags;
0964
0965 spin_lock_irqsave(&priv->lock, flags);
0966 chars = kfifo_len(&priv->write_fifo);
0967 spin_unlock_irqrestore(&priv->lock, flags);
0968
0969 dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
0970 return chars;
0971 }
0972
0973
0974 static void cypress_throttle(struct tty_struct *tty)
0975 {
0976 struct usb_serial_port *port = tty->driver_data;
0977 struct cypress_private *priv = usb_get_serial_port_data(port);
0978
0979 spin_lock_irq(&priv->lock);
0980 priv->rx_flags = THROTTLED;
0981 spin_unlock_irq(&priv->lock);
0982 }
0983
0984
0985 static void cypress_unthrottle(struct tty_struct *tty)
0986 {
0987 struct usb_serial_port *port = tty->driver_data;
0988 struct cypress_private *priv = usb_get_serial_port_data(port);
0989 int actually_throttled, result;
0990
0991 spin_lock_irq(&priv->lock);
0992 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
0993 priv->rx_flags = 0;
0994 spin_unlock_irq(&priv->lock);
0995
0996 if (!priv->comm_is_ok)
0997 return;
0998
0999 if (actually_throttled) {
1000 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1001 if (result) {
1002 dev_err(&port->dev, "%s - failed submitting read urb, "
1003 "error %d\n", __func__, result);
1004 cypress_set_dead(port);
1005 }
1006 }
1007 }
1008
1009
1010 static void cypress_read_int_callback(struct urb *urb)
1011 {
1012 struct usb_serial_port *port = urb->context;
1013 struct cypress_private *priv = usb_get_serial_port_data(port);
1014 struct device *dev = &urb->dev->dev;
1015 struct tty_struct *tty;
1016 unsigned char *data = urb->transfer_buffer;
1017 unsigned long flags;
1018 char tty_flag = TTY_NORMAL;
1019 int bytes = 0;
1020 int result;
1021 int i = 0;
1022 int status = urb->status;
1023
1024 switch (status) {
1025 case 0:
1026 break;
1027 case -ECONNRESET:
1028 case -ENOENT:
1029 case -ESHUTDOWN:
1030
1031 return;
1032 case -EPIPE:
1033
1034 fallthrough;
1035 default:
1036
1037 dev_err(dev, "%s - unexpected nonzero read status received: %d\n",
1038 __func__, status);
1039 cypress_set_dead(port);
1040 return;
1041 }
1042
1043 spin_lock_irqsave(&priv->lock, flags);
1044 if (priv->rx_flags & THROTTLED) {
1045 dev_dbg(dev, "%s - now throttling\n", __func__);
1046 priv->rx_flags |= ACTUALLY_THROTTLED;
1047 spin_unlock_irqrestore(&priv->lock, flags);
1048 return;
1049 }
1050 spin_unlock_irqrestore(&priv->lock, flags);
1051
1052 tty = tty_port_tty_get(&port->port);
1053 if (!tty) {
1054 dev_dbg(dev, "%s - bad tty pointer - exiting\n", __func__);
1055 return;
1056 }
1057
1058 spin_lock_irqsave(&priv->lock, flags);
1059 result = urb->actual_length;
1060 switch (priv->pkt_fmt) {
1061 default:
1062 case packet_format_1:
1063
1064 priv->current_status = data[0] & 0xF8;
1065 bytes = data[1] + 2;
1066 i = 2;
1067 break;
1068 case packet_format_2:
1069
1070 priv->current_status = data[0] & 0xF8;
1071 bytes = (data[0] & 0x07) + 1;
1072 i = 1;
1073 break;
1074 }
1075 spin_unlock_irqrestore(&priv->lock, flags);
1076 if (result < bytes) {
1077 dev_dbg(dev,
1078 "%s - wrong packet size - received %d bytes but packet said %d bytes\n",
1079 __func__, result, bytes);
1080 goto continue_read;
1081 }
1082
1083 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
1084
1085 spin_lock_irqsave(&priv->lock, flags);
1086
1087 if (priv->current_status != priv->prev_status) {
1088 u8 delta = priv->current_status ^ priv->prev_status;
1089
1090 if (delta & UART_MSR_MASK) {
1091 if (delta & UART_CTS)
1092 port->icount.cts++;
1093 if (delta & UART_DSR)
1094 port->icount.dsr++;
1095 if (delta & UART_RI)
1096 port->icount.rng++;
1097 if (delta & UART_CD)
1098 port->icount.dcd++;
1099
1100 wake_up_interruptible(&port->port.delta_msr_wait);
1101 }
1102
1103 priv->prev_status = priv->current_status;
1104 }
1105 spin_unlock_irqrestore(&priv->lock, flags);
1106
1107
1108
1109 if (tty && !C_CLOCAL(tty) && !(priv->current_status & UART_CD)) {
1110 dev_dbg(dev, "%s - calling hangup\n", __func__);
1111 tty_hangup(tty);
1112 goto continue_read;
1113 }
1114
1115
1116
1117
1118
1119 spin_lock_irqsave(&priv->lock, flags);
1120 if (priv->current_status & CYP_ERROR) {
1121 spin_unlock_irqrestore(&priv->lock, flags);
1122 tty_flag = TTY_PARITY;
1123 dev_dbg(dev, "%s - Parity Error detected\n", __func__);
1124 } else
1125 spin_unlock_irqrestore(&priv->lock, flags);
1126
1127
1128 if (bytes > i) {
1129 tty_insert_flip_string_fixed_flag(&port->port, data + i,
1130 tty_flag, bytes - i);
1131 tty_flip_buffer_push(&port->port);
1132 }
1133
1134 spin_lock_irqsave(&priv->lock, flags);
1135
1136 priv->bytes_in += bytes;
1137 spin_unlock_irqrestore(&priv->lock, flags);
1138
1139 continue_read:
1140 tty_kref_put(tty);
1141
1142
1143
1144 if (priv->comm_is_ok) {
1145 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
1146 usb_rcvintpipe(port->serial->dev,
1147 port->interrupt_in_endpointAddress),
1148 port->interrupt_in_urb->transfer_buffer,
1149 port->interrupt_in_urb->transfer_buffer_length,
1150 cypress_read_int_callback, port,
1151 priv->read_urb_interval);
1152 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1153 if (result && result != -EPERM) {
1154 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
1155 __func__, result);
1156 cypress_set_dead(port);
1157 }
1158 }
1159 }
1160
1161
1162 static void cypress_write_int_callback(struct urb *urb)
1163 {
1164 struct usb_serial_port *port = urb->context;
1165 struct cypress_private *priv = usb_get_serial_port_data(port);
1166 struct device *dev = &urb->dev->dev;
1167 int status = urb->status;
1168
1169 switch (status) {
1170 case 0:
1171
1172 break;
1173 case -ECONNRESET:
1174 case -ENOENT:
1175 case -ESHUTDOWN:
1176
1177 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
1178 __func__, status);
1179 priv->write_urb_in_use = 0;
1180 return;
1181 case -EPIPE:
1182
1183 fallthrough;
1184 default:
1185 dev_err(dev, "%s - unexpected nonzero write status received: %d\n",
1186 __func__, status);
1187 cypress_set_dead(port);
1188 break;
1189 }
1190 priv->write_urb_in_use = 0;
1191
1192
1193 cypress_send(port);
1194 }
1195
1196 module_usb_serial_driver(serial_drivers, id_table_combined);
1197
1198 MODULE_AUTHOR(DRIVER_AUTHOR);
1199 MODULE_DESCRIPTION(DRIVER_DESC);
1200 MODULE_LICENSE("GPL");
1201
1202 module_param(stats, bool, 0644);
1203 MODULE_PARM_DESC(stats, "Enable statistics or not");
1204 module_param(interval, int, 0644);
1205 MODULE_PARM_DESC(interval, "Overrides interrupt interval");
1206 module_param(unstable_bauds, bool, 0644);
1207 MODULE_PARM_DESC(unstable_bauds, "Allow unstable baud rates");