| .. | .. |
|---|
| 50 | 50 | static int cp210x_port_probe(struct usb_serial_port *); |
|---|
| 51 | 51 | static int cp210x_port_remove(struct usb_serial_port *); |
|---|
| 52 | 52 | static void cp210x_dtr_rts(struct usb_serial_port *p, int on); |
|---|
| 53 | +static void cp210x_process_read_urb(struct urb *urb); |
|---|
| 54 | +static void cp210x_enable_event_mode(struct usb_serial_port *port); |
|---|
| 55 | +static void cp210x_disable_event_mode(struct usb_serial_port *port); |
|---|
| 53 | 56 | |
|---|
| 54 | 57 | static const struct usb_device_id id_table[] = { |
|---|
| 55 | 58 | { USB_DEVICE(0x0404, 0x034C) }, /* NCR Retail IO Box */ |
|---|
| .. | .. |
|---|
| 131 | 134 | { USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */ |
|---|
| 132 | 135 | { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */ |
|---|
| 133 | 136 | { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */ |
|---|
| 137 | + { USB_DEVICE(0x10C4, 0x8414) }, /* Decagon USB Cable Adapter */ |
|---|
| 134 | 138 | { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */ |
|---|
| 135 | 139 | { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */ |
|---|
| 136 | 140 | { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */ |
|---|
| .. | .. |
|---|
| 195 | 199 | { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */ |
|---|
| 196 | 200 | { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */ |
|---|
| 197 | 201 | { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */ |
|---|
| 202 | + { USB_DEVICE(0x17A8, 0x0011) }, /* Kamstrup 444 MHz RF sniffer */ |
|---|
| 203 | + { USB_DEVICE(0x17A8, 0x0013) }, /* Kamstrup 870 MHz RF sniffer */ |
|---|
| 204 | + { USB_DEVICE(0x17A8, 0x0101) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Int Ant) */ |
|---|
| 205 | + { USB_DEVICE(0x17A8, 0x0102) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Ext Ant) */ |
|---|
| 198 | 206 | { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */ |
|---|
| 199 | 207 | { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */ |
|---|
| 200 | 208 | { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */ |
|---|
| .. | .. |
|---|
| 257 | 265 | u8 gpio_input; |
|---|
| 258 | 266 | #endif |
|---|
| 259 | 267 | u8 partnum; |
|---|
| 268 | + speed_t min_speed; |
|---|
| 260 | 269 | speed_t max_speed; |
|---|
| 261 | 270 | bool use_actual_rate; |
|---|
| 271 | + bool no_event_mode; |
|---|
| 272 | +}; |
|---|
| 273 | + |
|---|
| 274 | +enum cp210x_event_state { |
|---|
| 275 | + ES_DATA, |
|---|
| 276 | + ES_ESCAPE, |
|---|
| 277 | + ES_LSR, |
|---|
| 278 | + ES_LSR_DATA_0, |
|---|
| 279 | + ES_LSR_DATA_1, |
|---|
| 280 | + ES_MSR |
|---|
| 262 | 281 | }; |
|---|
| 263 | 282 | |
|---|
| 264 | 283 | struct cp210x_port_private { |
|---|
| 265 | | - __u8 bInterfaceNumber; |
|---|
| 284 | + u8 bInterfaceNumber; |
|---|
| 266 | 285 | bool has_swapped_line_ctl; |
|---|
| 286 | + bool event_mode; |
|---|
| 287 | + enum cp210x_event_state event_state; |
|---|
| 288 | + u8 lsr; |
|---|
| 267 | 289 | }; |
|---|
| 268 | 290 | |
|---|
| 269 | 291 | static struct usb_serial_driver cp210x_device = { |
|---|
| .. | .. |
|---|
| 284 | 306 | .unthrottle = usb_serial_generic_unthrottle, |
|---|
| 285 | 307 | .tiocmget = cp210x_tiocmget, |
|---|
| 286 | 308 | .tiocmset = cp210x_tiocmset, |
|---|
| 309 | + .get_icount = usb_serial_generic_get_icount, |
|---|
| 287 | 310 | .attach = cp210x_attach, |
|---|
| 288 | 311 | .disconnect = cp210x_disconnect, |
|---|
| 289 | 312 | .release = cp210x_release, |
|---|
| 290 | 313 | .port_probe = cp210x_port_probe, |
|---|
| 291 | 314 | .port_remove = cp210x_port_remove, |
|---|
| 292 | | - .dtr_rts = cp210x_dtr_rts |
|---|
| 315 | + .dtr_rts = cp210x_dtr_rts, |
|---|
| 316 | + .process_read_urb = cp210x_process_read_urb, |
|---|
| 293 | 317 | }; |
|---|
| 294 | 318 | |
|---|
| 295 | 319 | static struct usb_serial_driver * const serial_drivers[] = { |
|---|
| .. | .. |
|---|
| 411 | 435 | */ |
|---|
| 412 | 436 | #define PURGE_ALL 0x000f |
|---|
| 413 | 437 | |
|---|
| 438 | +/* CP210X_EMBED_EVENTS */ |
|---|
| 439 | +#define CP210X_ESCCHAR 0xec |
|---|
| 440 | + |
|---|
| 441 | +#define CP210X_LSR_OVERRUN BIT(1) |
|---|
| 442 | +#define CP210X_LSR_PARITY BIT(2) |
|---|
| 443 | +#define CP210X_LSR_FRAME BIT(3) |
|---|
| 444 | +#define CP210X_LSR_BREAK BIT(4) |
|---|
| 445 | + |
|---|
| 446 | + |
|---|
| 414 | 447 | /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */ |
|---|
| 415 | 448 | struct cp210x_flow_ctl { |
|---|
| 416 | 449 | __le32 ulControlHandshake; |
|---|
| 417 | 450 | __le32 ulFlowReplace; |
|---|
| 418 | 451 | __le32 ulXonLimit; |
|---|
| 419 | 452 | __le32 ulXoffLimit; |
|---|
| 420 | | -} __packed; |
|---|
| 453 | +}; |
|---|
| 421 | 454 | |
|---|
| 422 | 455 | /* cp210x_flow_ctl::ulControlHandshake */ |
|---|
| 423 | 456 | #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0) |
|---|
| .. | .. |
|---|
| 451 | 484 | struct cp210x_pin_mode { |
|---|
| 452 | 485 | u8 eci; |
|---|
| 453 | 486 | u8 sci; |
|---|
| 454 | | -} __packed; |
|---|
| 487 | +}; |
|---|
| 455 | 488 | |
|---|
| 456 | 489 | #define CP210X_PIN_MODE_MODEM 0 |
|---|
| 457 | 490 | #define CP210X_PIN_MODE_GPIO BIT(0) |
|---|
| 458 | 491 | |
|---|
| 459 | 492 | /* |
|---|
| 460 | | - * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes. |
|---|
| 461 | | - * Structure needs padding due to unused/unspecified bytes. |
|---|
| 493 | + * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes |
|---|
| 494 | + * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes. |
|---|
| 462 | 495 | */ |
|---|
| 463 | | -struct cp210x_config { |
|---|
| 496 | +struct cp210x_dual_port_config { |
|---|
| 464 | 497 | __le16 gpio_mode; |
|---|
| 465 | 498 | u8 __pad0[2]; |
|---|
| 466 | 499 | __le16 reset_state; |
|---|
| .. | .. |
|---|
| 471 | 504 | u8 device_cfg; |
|---|
| 472 | 505 | } __packed; |
|---|
| 473 | 506 | |
|---|
| 507 | +/* |
|---|
| 508 | + * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes |
|---|
| 509 | + * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes. |
|---|
| 510 | + */ |
|---|
| 511 | +struct cp210x_single_port_config { |
|---|
| 512 | + __le16 gpio_mode; |
|---|
| 513 | + u8 __pad0[2]; |
|---|
| 514 | + __le16 reset_state; |
|---|
| 515 | + u8 __pad1[4]; |
|---|
| 516 | + __le16 suspend_state; |
|---|
| 517 | + u8 device_cfg; |
|---|
| 518 | +} __packed; |
|---|
| 519 | + |
|---|
| 474 | 520 | /* GPIO modes */ |
|---|
| 475 | 521 | #define CP210X_SCI_GPIO_MODE_OFFSET 9 |
|---|
| 476 | 522 | #define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9) |
|---|
| .. | .. |
|---|
| 478 | 524 | #define CP210X_ECI_GPIO_MODE_OFFSET 2 |
|---|
| 479 | 525 | #define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2) |
|---|
| 480 | 526 | |
|---|
| 527 | +#define CP210X_GPIO_MODE_OFFSET 8 |
|---|
| 528 | +#define CP210X_GPIO_MODE_MASK GENMASK(11, 8) |
|---|
| 529 | + |
|---|
| 481 | 530 | /* CP2105 port configuration values */ |
|---|
| 482 | 531 | #define CP2105_GPIO0_TXLED_MODE BIT(0) |
|---|
| 483 | 532 | #define CP2105_GPIO1_RXLED_MODE BIT(1) |
|---|
| 484 | 533 | #define CP2105_GPIO1_RS485_MODE BIT(2) |
|---|
| 534 | + |
|---|
| 535 | +/* CP2104 port configuration values */ |
|---|
| 536 | +#define CP2104_GPIO0_TXLED_MODE BIT(0) |
|---|
| 537 | +#define CP2104_GPIO1_RXLED_MODE BIT(1) |
|---|
| 538 | +#define CP2104_GPIO2_RS485_MODE BIT(2) |
|---|
| 485 | 539 | |
|---|
| 486 | 540 | /* CP2102N configuration array indices */ |
|---|
| 487 | 541 | #define CP210X_2NCONFIG_CONFIG_VERSION_IDX 2 |
|---|
| .. | .. |
|---|
| 499 | 553 | struct cp210x_gpio_write { |
|---|
| 500 | 554 | u8 mask; |
|---|
| 501 | 555 | u8 state; |
|---|
| 502 | | -} __packed; |
|---|
| 556 | +}; |
|---|
| 503 | 557 | |
|---|
| 504 | 558 | /* |
|---|
| 505 | 559 | * Helper to get interface number when we only have struct usb_serial. |
|---|
| .. | .. |
|---|
| 802 | 856 | |
|---|
| 803 | 857 | static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port) |
|---|
| 804 | 858 | { |
|---|
| 859 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 805 | 860 | int result; |
|---|
| 806 | 861 | |
|---|
| 807 | 862 | result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE); |
|---|
| .. | .. |
|---|
| 813 | 868 | /* Configure the termios structure */ |
|---|
| 814 | 869 | cp210x_get_termios(tty, port); |
|---|
| 815 | 870 | |
|---|
| 816 | | - /* The baud rate must be initialised on cp2104 */ |
|---|
| 817 | | - if (tty) |
|---|
| 871 | + if (tty) { |
|---|
| 872 | + /* The baud rate must be initialised on cp2104 */ |
|---|
| 818 | 873 | cp210x_change_speed(tty, port, NULL); |
|---|
| 819 | 874 | |
|---|
| 820 | | - return usb_serial_generic_open(tty, port); |
|---|
| 875 | + if (I_INPCK(tty)) |
|---|
| 876 | + cp210x_enable_event_mode(port); |
|---|
| 877 | + } |
|---|
| 878 | + |
|---|
| 879 | + result = usb_serial_generic_open(tty, port); |
|---|
| 880 | + if (result) |
|---|
| 881 | + goto err_disable; |
|---|
| 882 | + |
|---|
| 883 | + return 0; |
|---|
| 884 | + |
|---|
| 885 | +err_disable: |
|---|
| 886 | + cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE); |
|---|
| 887 | + port_priv->event_mode = false; |
|---|
| 888 | + |
|---|
| 889 | + return result; |
|---|
| 821 | 890 | } |
|---|
| 822 | 891 | |
|---|
| 823 | 892 | static void cp210x_close(struct usb_serial_port *port) |
|---|
| 824 | 893 | { |
|---|
| 894 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 895 | + |
|---|
| 825 | 896 | usb_serial_generic_close(port); |
|---|
| 826 | 897 | |
|---|
| 827 | 898 | /* Clear both queues; cp2108 needs this to avoid an occasional hang */ |
|---|
| 828 | 899 | cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL); |
|---|
| 829 | 900 | |
|---|
| 830 | 901 | cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE); |
|---|
| 902 | + |
|---|
| 903 | + /* Disabling the interface disables event-insertion mode. */ |
|---|
| 904 | + port_priv->event_mode = false; |
|---|
| 905 | +} |
|---|
| 906 | + |
|---|
| 907 | +static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag) |
|---|
| 908 | +{ |
|---|
| 909 | + if (lsr & CP210X_LSR_BREAK) { |
|---|
| 910 | + port->icount.brk++; |
|---|
| 911 | + *flag = TTY_BREAK; |
|---|
| 912 | + } else if (lsr & CP210X_LSR_PARITY) { |
|---|
| 913 | + port->icount.parity++; |
|---|
| 914 | + *flag = TTY_PARITY; |
|---|
| 915 | + } else if (lsr & CP210X_LSR_FRAME) { |
|---|
| 916 | + port->icount.frame++; |
|---|
| 917 | + *flag = TTY_FRAME; |
|---|
| 918 | + } |
|---|
| 919 | + |
|---|
| 920 | + if (lsr & CP210X_LSR_OVERRUN) { |
|---|
| 921 | + port->icount.overrun++; |
|---|
| 922 | + tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); |
|---|
| 923 | + } |
|---|
| 924 | +} |
|---|
| 925 | + |
|---|
| 926 | +static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag) |
|---|
| 927 | +{ |
|---|
| 928 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 929 | + |
|---|
| 930 | + switch (port_priv->event_state) { |
|---|
| 931 | + case ES_DATA: |
|---|
| 932 | + if (*ch == CP210X_ESCCHAR) { |
|---|
| 933 | + port_priv->event_state = ES_ESCAPE; |
|---|
| 934 | + break; |
|---|
| 935 | + } |
|---|
| 936 | + return false; |
|---|
| 937 | + case ES_ESCAPE: |
|---|
| 938 | + switch (*ch) { |
|---|
| 939 | + case 0: |
|---|
| 940 | + dev_dbg(&port->dev, "%s - escape char\n", __func__); |
|---|
| 941 | + *ch = CP210X_ESCCHAR; |
|---|
| 942 | + port_priv->event_state = ES_DATA; |
|---|
| 943 | + return false; |
|---|
| 944 | + case 1: |
|---|
| 945 | + port_priv->event_state = ES_LSR_DATA_0; |
|---|
| 946 | + break; |
|---|
| 947 | + case 2: |
|---|
| 948 | + port_priv->event_state = ES_LSR; |
|---|
| 949 | + break; |
|---|
| 950 | + case 3: |
|---|
| 951 | + port_priv->event_state = ES_MSR; |
|---|
| 952 | + break; |
|---|
| 953 | + default: |
|---|
| 954 | + dev_err(&port->dev, "malformed event 0x%02x\n", *ch); |
|---|
| 955 | + port_priv->event_state = ES_DATA; |
|---|
| 956 | + break; |
|---|
| 957 | + } |
|---|
| 958 | + break; |
|---|
| 959 | + case ES_LSR_DATA_0: |
|---|
| 960 | + port_priv->lsr = *ch; |
|---|
| 961 | + port_priv->event_state = ES_LSR_DATA_1; |
|---|
| 962 | + break; |
|---|
| 963 | + case ES_LSR_DATA_1: |
|---|
| 964 | + dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n", |
|---|
| 965 | + __func__, port_priv->lsr, *ch); |
|---|
| 966 | + cp210x_process_lsr(port, port_priv->lsr, flag); |
|---|
| 967 | + port_priv->event_state = ES_DATA; |
|---|
| 968 | + return false; |
|---|
| 969 | + case ES_LSR: |
|---|
| 970 | + dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch); |
|---|
| 971 | + port_priv->lsr = *ch; |
|---|
| 972 | + cp210x_process_lsr(port, port_priv->lsr, flag); |
|---|
| 973 | + port_priv->event_state = ES_DATA; |
|---|
| 974 | + break; |
|---|
| 975 | + case ES_MSR: |
|---|
| 976 | + dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch); |
|---|
| 977 | + /* unimplemented */ |
|---|
| 978 | + port_priv->event_state = ES_DATA; |
|---|
| 979 | + break; |
|---|
| 980 | + } |
|---|
| 981 | + |
|---|
| 982 | + return true; |
|---|
| 983 | +} |
|---|
| 984 | + |
|---|
| 985 | +static void cp210x_process_read_urb(struct urb *urb) |
|---|
| 986 | +{ |
|---|
| 987 | + struct usb_serial_port *port = urb->context; |
|---|
| 988 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 989 | + unsigned char *ch = urb->transfer_buffer; |
|---|
| 990 | + char flag; |
|---|
| 991 | + int i; |
|---|
| 992 | + |
|---|
| 993 | + if (!urb->actual_length) |
|---|
| 994 | + return; |
|---|
| 995 | + |
|---|
| 996 | + if (port_priv->event_mode) { |
|---|
| 997 | + for (i = 0; i < urb->actual_length; i++, ch++) { |
|---|
| 998 | + flag = TTY_NORMAL; |
|---|
| 999 | + |
|---|
| 1000 | + if (cp210x_process_char(port, ch, &flag)) |
|---|
| 1001 | + continue; |
|---|
| 1002 | + |
|---|
| 1003 | + tty_insert_flip_char(&port->port, *ch, flag); |
|---|
| 1004 | + } |
|---|
| 1005 | + } else { |
|---|
| 1006 | + tty_insert_flip_string(&port->port, ch, urb->actual_length); |
|---|
| 1007 | + } |
|---|
| 1008 | + tty_flip_buffer_push(&port->port); |
|---|
| 831 | 1009 | } |
|---|
| 832 | 1010 | |
|---|
| 833 | 1011 | /* |
|---|
| .. | .. |
|---|
| 1088 | 1266 | return cp210x_an205_table1[i].rate; |
|---|
| 1089 | 1267 | } |
|---|
| 1090 | 1268 | |
|---|
| 1091 | | -static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud) |
|---|
| 1269 | +static speed_t cp210x_get_actual_rate(speed_t baud) |
|---|
| 1092 | 1270 | { |
|---|
| 1093 | | - struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 1094 | 1271 | unsigned int prescale = 1; |
|---|
| 1095 | 1272 | unsigned int div; |
|---|
| 1096 | | - |
|---|
| 1097 | | - baud = clamp(baud, 300u, priv->max_speed); |
|---|
| 1098 | 1273 | |
|---|
| 1099 | 1274 | if (baud <= 365) |
|---|
| 1100 | 1275 | prescale = 4; |
|---|
| .. | .. |
|---|
| 1138 | 1313 | struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 1139 | 1314 | u32 baud; |
|---|
| 1140 | 1315 | |
|---|
| 1141 | | - baud = tty->termios.c_ospeed; |
|---|
| 1142 | | - |
|---|
| 1143 | 1316 | /* |
|---|
| 1144 | 1317 | * This maps the requested rate to the actual rate, a valid rate on |
|---|
| 1145 | 1318 | * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed]. |
|---|
| 1146 | 1319 | * |
|---|
| 1147 | 1320 | * NOTE: B0 is not implemented. |
|---|
| 1148 | 1321 | */ |
|---|
| 1322 | + baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed); |
|---|
| 1323 | + |
|---|
| 1149 | 1324 | if (priv->use_actual_rate) |
|---|
| 1150 | | - baud = cp210x_get_actual_rate(serial, baud); |
|---|
| 1325 | + baud = cp210x_get_actual_rate(baud); |
|---|
| 1151 | 1326 | else if (baud < 1000000) |
|---|
| 1152 | 1327 | baud = cp210x_get_an205_rate(baud); |
|---|
| 1153 | | - else if (baud > priv->max_speed) |
|---|
| 1154 | | - baud = priv->max_speed; |
|---|
| 1155 | 1328 | |
|---|
| 1156 | 1329 | dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); |
|---|
| 1157 | 1330 | if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) { |
|---|
| .. | .. |
|---|
| 1163 | 1336 | } |
|---|
| 1164 | 1337 | |
|---|
| 1165 | 1338 | tty_encode_baud_rate(tty, baud, baud); |
|---|
| 1339 | +} |
|---|
| 1340 | + |
|---|
| 1341 | +static void cp210x_enable_event_mode(struct usb_serial_port *port) |
|---|
| 1342 | +{ |
|---|
| 1343 | + struct cp210x_serial_private *priv = usb_get_serial_data(port->serial); |
|---|
| 1344 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 1345 | + int ret; |
|---|
| 1346 | + |
|---|
| 1347 | + if (port_priv->event_mode) |
|---|
| 1348 | + return; |
|---|
| 1349 | + |
|---|
| 1350 | + if (priv->no_event_mode) |
|---|
| 1351 | + return; |
|---|
| 1352 | + |
|---|
| 1353 | + port_priv->event_state = ES_DATA; |
|---|
| 1354 | + port_priv->event_mode = true; |
|---|
| 1355 | + |
|---|
| 1356 | + ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR); |
|---|
| 1357 | + if (ret) { |
|---|
| 1358 | + dev_err(&port->dev, "failed to enable events: %d\n", ret); |
|---|
| 1359 | + port_priv->event_mode = false; |
|---|
| 1360 | + } |
|---|
| 1361 | +} |
|---|
| 1362 | + |
|---|
| 1363 | +static void cp210x_disable_event_mode(struct usb_serial_port *port) |
|---|
| 1364 | +{ |
|---|
| 1365 | + struct cp210x_port_private *port_priv = usb_get_serial_port_data(port); |
|---|
| 1366 | + int ret; |
|---|
| 1367 | + |
|---|
| 1368 | + if (!port_priv->event_mode) |
|---|
| 1369 | + return; |
|---|
| 1370 | + |
|---|
| 1371 | + ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0); |
|---|
| 1372 | + if (ret) { |
|---|
| 1373 | + dev_err(&port->dev, "failed to disable events: %d\n", ret); |
|---|
| 1374 | + return; |
|---|
| 1375 | + } |
|---|
| 1376 | + |
|---|
| 1377 | + port_priv->event_mode = false; |
|---|
| 1166 | 1378 | } |
|---|
| 1167 | 1379 | |
|---|
| 1168 | 1380 | static void cp210x_set_termios(struct tty_struct *tty, |
|---|
| .. | .. |
|---|
| 1287 | 1499 | sizeof(flow_ctl)); |
|---|
| 1288 | 1500 | } |
|---|
| 1289 | 1501 | |
|---|
| 1502 | + /* |
|---|
| 1503 | + * Enable event-insertion mode only if input parity checking is |
|---|
| 1504 | + * enabled for now. |
|---|
| 1505 | + */ |
|---|
| 1506 | + if (I_INPCK(tty)) |
|---|
| 1507 | + cp210x_enable_event_mode(port); |
|---|
| 1508 | + else |
|---|
| 1509 | + cp210x_disable_event_mode(port); |
|---|
| 1290 | 1510 | } |
|---|
| 1291 | 1511 | |
|---|
| 1292 | 1512 | static int cp210x_tiocmset(struct tty_struct *tty, |
|---|
| .. | .. |
|---|
| 1518 | 1738 | { |
|---|
| 1519 | 1739 | struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 1520 | 1740 | struct cp210x_pin_mode mode; |
|---|
| 1521 | | - struct cp210x_config config; |
|---|
| 1741 | + struct cp210x_dual_port_config config; |
|---|
| 1522 | 1742 | u8 intf_num = cp210x_interface_num(serial); |
|---|
| 1523 | 1743 | u8 iface_config; |
|---|
| 1524 | 1744 | int result; |
|---|
| .. | .. |
|---|
| 1579 | 1799 | return 0; |
|---|
| 1580 | 1800 | } |
|---|
| 1581 | 1801 | |
|---|
| 1802 | +static int cp2104_gpioconf_init(struct usb_serial *serial) |
|---|
| 1803 | +{ |
|---|
| 1804 | + struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 1805 | + struct cp210x_single_port_config config; |
|---|
| 1806 | + u8 iface_config; |
|---|
| 1807 | + u8 gpio_latch; |
|---|
| 1808 | + int result; |
|---|
| 1809 | + u8 i; |
|---|
| 1810 | + |
|---|
| 1811 | + result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST, |
|---|
| 1812 | + CP210X_GET_PORTCONFIG, &config, |
|---|
| 1813 | + sizeof(config)); |
|---|
| 1814 | + if (result < 0) |
|---|
| 1815 | + return result; |
|---|
| 1816 | + |
|---|
| 1817 | + priv->gc.ngpio = 4; |
|---|
| 1818 | + |
|---|
| 1819 | + iface_config = config.device_cfg; |
|---|
| 1820 | + priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) & |
|---|
| 1821 | + CP210X_GPIO_MODE_MASK) >> |
|---|
| 1822 | + CP210X_GPIO_MODE_OFFSET); |
|---|
| 1823 | + gpio_latch = (u8)((le16_to_cpu(config.reset_state) & |
|---|
| 1824 | + CP210X_GPIO_MODE_MASK) >> |
|---|
| 1825 | + CP210X_GPIO_MODE_OFFSET); |
|---|
| 1826 | + |
|---|
| 1827 | + /* mark all pins which are not in GPIO mode */ |
|---|
| 1828 | + if (iface_config & CP2104_GPIO0_TXLED_MODE) /* GPIO 0 */ |
|---|
| 1829 | + priv->gpio_altfunc |= BIT(0); |
|---|
| 1830 | + if (iface_config & CP2104_GPIO1_RXLED_MODE) /* GPIO 1 */ |
|---|
| 1831 | + priv->gpio_altfunc |= BIT(1); |
|---|
| 1832 | + if (iface_config & CP2104_GPIO2_RS485_MODE) /* GPIO 2 */ |
|---|
| 1833 | + priv->gpio_altfunc |= BIT(2); |
|---|
| 1834 | + |
|---|
| 1835 | + /* |
|---|
| 1836 | + * Like CP2102N, CP2104 has also no strict input and output pin |
|---|
| 1837 | + * modes. |
|---|
| 1838 | + * Do the same input mode emulation as CP2102N. |
|---|
| 1839 | + */ |
|---|
| 1840 | + for (i = 0; i < priv->gc.ngpio; ++i) { |
|---|
| 1841 | + /* |
|---|
| 1842 | + * Set direction to "input" iff pin is open-drain and reset |
|---|
| 1843 | + * value is 1. |
|---|
| 1844 | + */ |
|---|
| 1845 | + if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i))) |
|---|
| 1846 | + priv->gpio_input |= BIT(i); |
|---|
| 1847 | + } |
|---|
| 1848 | + |
|---|
| 1849 | + return 0; |
|---|
| 1850 | +} |
|---|
| 1851 | + |
|---|
| 1582 | 1852 | static int cp2102n_gpioconf_init(struct usb_serial *serial) |
|---|
| 1583 | 1853 | { |
|---|
| 1584 | 1854 | struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| .. | .. |
|---|
| 1624 | 1894 | if (config_version != 0x01) |
|---|
| 1625 | 1895 | return -ENOTSUPP; |
|---|
| 1626 | 1896 | |
|---|
| 1627 | | - /* |
|---|
| 1628 | | - * We only support 4 GPIOs even on the QFN28 package, because |
|---|
| 1629 | | - * config locations of GPIOs 4-6 determined using reverse |
|---|
| 1630 | | - * engineering revealed conflicting offsets with other |
|---|
| 1631 | | - * documented functions. So we'll just play it safe for now. |
|---|
| 1632 | | - */ |
|---|
| 1633 | 1897 | priv->gc.ngpio = 4; |
|---|
| 1634 | 1898 | |
|---|
| 1635 | 1899 | /* |
|---|
| .. | .. |
|---|
| 1654 | 1918 | priv->gpio_altfunc |= BIT(3); |
|---|
| 1655 | 1919 | } else { |
|---|
| 1656 | 1920 | priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f; |
|---|
| 1921 | + } |
|---|
| 1922 | + |
|---|
| 1923 | + if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) { |
|---|
| 1924 | + /* |
|---|
| 1925 | + * For the QFN28 package, GPIO4-6 are controlled by |
|---|
| 1926 | + * the low three bits of the mode/latch fields. |
|---|
| 1927 | + * Contrary to the document linked above, the bits for |
|---|
| 1928 | + * the SUSPEND pins are elsewhere. No alternate |
|---|
| 1929 | + * function is available for these pins. |
|---|
| 1930 | + */ |
|---|
| 1931 | + priv->gc.ngpio = 7; |
|---|
| 1932 | + gpio_latch |= (gpio_rst_latch & 7) << 4; |
|---|
| 1933 | + priv->gpio_pushpull |= (gpio_pushpull & 7) << 4; |
|---|
| 1657 | 1934 | } |
|---|
| 1658 | 1935 | |
|---|
| 1659 | 1936 | /* |
|---|
| .. | .. |
|---|
| 1682 | 1959 | int result; |
|---|
| 1683 | 1960 | |
|---|
| 1684 | 1961 | switch (priv->partnum) { |
|---|
| 1962 | + case CP210X_PARTNUM_CP2104: |
|---|
| 1963 | + result = cp2104_gpioconf_init(serial); |
|---|
| 1964 | + break; |
|---|
| 1685 | 1965 | case CP210X_PARTNUM_CP2105: |
|---|
| 1686 | 1966 | result = cp2105_gpioconf_init(serial); |
|---|
| 1687 | 1967 | break; |
|---|
| .. | .. |
|---|
| 1778 | 2058 | { |
|---|
| 1779 | 2059 | struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 1780 | 2060 | bool use_actual_rate = false; |
|---|
| 2061 | + speed_t min = 300; |
|---|
| 1781 | 2062 | speed_t max; |
|---|
| 1782 | 2063 | |
|---|
| 1783 | 2064 | switch (priv->partnum) { |
|---|
| .. | .. |
|---|
| 1800 | 2081 | use_actual_rate = true; |
|---|
| 1801 | 2082 | max = 2000000; /* ECI */ |
|---|
| 1802 | 2083 | } else { |
|---|
| 2084 | + min = 2400; |
|---|
| 1803 | 2085 | max = 921600; /* SCI */ |
|---|
| 1804 | 2086 | } |
|---|
| 1805 | 2087 | break; |
|---|
| .. | .. |
|---|
| 1814 | 2096 | break; |
|---|
| 1815 | 2097 | } |
|---|
| 1816 | 2098 | |
|---|
| 2099 | + priv->min_speed = min; |
|---|
| 1817 | 2100 | priv->max_speed = max; |
|---|
| 1818 | 2101 | priv->use_actual_rate = use_actual_rate; |
|---|
| 2102 | +} |
|---|
| 2103 | + |
|---|
| 2104 | +static void cp2102_determine_quirks(struct usb_serial *serial) |
|---|
| 2105 | +{ |
|---|
| 2106 | + struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 2107 | + u8 *buf; |
|---|
| 2108 | + int ret; |
|---|
| 2109 | + |
|---|
| 2110 | + buf = kmalloc(2, GFP_KERNEL); |
|---|
| 2111 | + if (!buf) |
|---|
| 2112 | + return; |
|---|
| 2113 | + /* |
|---|
| 2114 | + * Some (possibly counterfeit) CP2102 do not support event-insertion |
|---|
| 2115 | + * mode and respond differently to malformed vendor requests. |
|---|
| 2116 | + * Specifically, they return one instead of two bytes when sent a |
|---|
| 2117 | + * two-byte part-number request. |
|---|
| 2118 | + */ |
|---|
| 2119 | + ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
|---|
| 2120 | + CP210X_VENDOR_SPECIFIC, REQTYPE_DEVICE_TO_HOST, |
|---|
| 2121 | + CP210X_GET_PARTNUM, 0, buf, 2, USB_CTRL_GET_TIMEOUT); |
|---|
| 2122 | + if (ret == 1) { |
|---|
| 2123 | + dev_dbg(&serial->interface->dev, |
|---|
| 2124 | + "device does not support event-insertion mode\n"); |
|---|
| 2125 | + priv->no_event_mode = true; |
|---|
| 2126 | + } |
|---|
| 2127 | + |
|---|
| 2128 | + kfree(buf); |
|---|
| 2129 | +} |
|---|
| 2130 | + |
|---|
| 2131 | +static void cp210x_determine_quirks(struct usb_serial *serial) |
|---|
| 2132 | +{ |
|---|
| 2133 | + struct cp210x_serial_private *priv = usb_get_serial_data(serial); |
|---|
| 2134 | + |
|---|
| 2135 | + switch (priv->partnum) { |
|---|
| 2136 | + case CP210X_PARTNUM_CP2102: |
|---|
| 2137 | + cp2102_determine_quirks(serial); |
|---|
| 2138 | + break; |
|---|
| 2139 | + default: |
|---|
| 2140 | + break; |
|---|
| 2141 | + } |
|---|
| 1819 | 2142 | } |
|---|
| 1820 | 2143 | |
|---|
| 1821 | 2144 | static int cp210x_attach(struct usb_serial *serial) |
|---|
| .. | .. |
|---|
| 1838 | 2161 | |
|---|
| 1839 | 2162 | usb_set_serial_data(serial, priv); |
|---|
| 1840 | 2163 | |
|---|
| 2164 | + cp210x_determine_quirks(serial); |
|---|
| 1841 | 2165 | cp210x_init_max_speed(serial); |
|---|
| 1842 | 2166 | |
|---|
| 1843 | 2167 | result = cp210x_gpio_init(serial); |
|---|