1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 Some of this code is credited to Linux USB open source files that are 4 distributed with Linux. 5 6 Copyright: 2007 Metrologic Instruments. All rights reserved. 7 Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/tty.h> 12 #include <linux/module.h> 13 #include <linux/usb.h> 14 #include <linux/errno.h> 15 #include <linux/slab.h> 16 #include <linux/tty_flip.h> 17 #include <linux/spinlock.h> 18 #include <linux/usb/serial.h> 19 20 #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver" 21 22 /* Product information. */ 23 #define FOCUS_VENDOR_ID 0x0C2E 24 #define FOCUS_PRODUCT_ID_BI 0x0720 25 #define FOCUS_PRODUCT_ID_UNI 0x0700 26 27 #define METROUSB_SET_REQUEST_TYPE 0x40 28 #define METROUSB_SET_MODEM_CTRL_REQUEST 10 29 #define METROUSB_SET_BREAK_REQUEST 0x40 30 #define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */ 31 #define METROUSB_MCR_RTS 0x0a /* Activate RTS. */ 32 #define METROUSB_MCR_DTR 0x09 /* Activate DTR. */ 33 #define WDR_TIMEOUT 5000 /* default urb timeout. */ 34 35 /* Private data structure. */ 36 struct metrousb_private { 37 spinlock_t lock; 38 int throttled; 39 unsigned long control_state; 40 }; 41 42 /* Device table list. */ 43 static const struct usb_device_id id_table[] = { 44 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) }, 45 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) }, 46 { USB_DEVICE_INTERFACE_CLASS(0x0c2e, 0x0730, 0xff) }, /* MS7820 */ 47 { }, /* Terminating entry. */ 48 }; 49 MODULE_DEVICE_TABLE(usb, id_table); 50 51 /* UNI-Directional mode commands for device configure */ 52 #define UNI_CMD_OPEN 0x80 53 #define UNI_CMD_CLOSE 0xFF 54 55 static int metrousb_is_unidirectional_mode(struct usb_serial *serial) 56 { 57 u16 product_id = le16_to_cpu(serial->dev->descriptor.idProduct); 58 59 return product_id == FOCUS_PRODUCT_ID_UNI; 60 } 61 62 static int metrousb_calc_num_ports(struct usb_serial *serial, 63 struct usb_serial_endpoints *epds) 64 { 65 if (metrousb_is_unidirectional_mode(serial)) { 66 if (epds->num_interrupt_out == 0) { 67 dev_err(&serial->interface->dev, "interrupt-out endpoint missing\n"); 68 return -ENODEV; 69 } 70 } 71 72 return 1; 73 } 74 75 static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port) 76 { 77 int ret; 78 int actual_len; 79 u8 *buffer_cmd = NULL; 80 81 if (!metrousb_is_unidirectional_mode(port->serial)) 82 return 0; 83 84 buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL); 85 if (!buffer_cmd) 86 return -ENOMEM; 87 88 *buffer_cmd = cmd; 89 90 ret = usb_interrupt_msg(port->serial->dev, 91 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress), 92 buffer_cmd, sizeof(cmd), 93 &actual_len, USB_CTRL_SET_TIMEOUT); 94 95 kfree(buffer_cmd); 96 97 if (ret < 0) 98 return ret; 99 else if (actual_len != sizeof(cmd)) 100 return -EIO; 101 return 0; 102 } 103 104 static void metrousb_read_int_callback(struct urb *urb) 105 { 106 struct usb_serial_port *port = urb->context; 107 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 108 unsigned char *data = urb->transfer_buffer; 109 unsigned long flags; 110 int throttled = 0; 111 int result = 0; 112 113 dev_dbg(&port->dev, "%s\n", __func__); 114 115 switch (urb->status) { 116 case 0: 117 /* Success status, read from the port. */ 118 break; 119 case -ECONNRESET: 120 case -ENOENT: 121 case -ESHUTDOWN: 122 /* urb has been terminated. */ 123 dev_dbg(&port->dev, 124 "%s - urb shutting down, error code=%d\n", 125 __func__, urb->status); 126 return; 127 default: 128 dev_dbg(&port->dev, 129 "%s - non-zero urb received, error code=%d\n", 130 __func__, urb->status); 131 goto exit; 132 } 133 134 135 /* Set the data read from the usb port into the serial port buffer. */ 136 if (urb->actual_length) { 137 /* Loop through the data copying each byte to the tty layer. */ 138 tty_insert_flip_string(&port->port, data, urb->actual_length); 139 140 /* Force the data to the tty layer. */ 141 tty_flip_buffer_push(&port->port); 142 } 143 144 /* Set any port variables. */ 145 spin_lock_irqsave(&metro_priv->lock, flags); 146 throttled = metro_priv->throttled; 147 spin_unlock_irqrestore(&metro_priv->lock, flags); 148 149 if (throttled) 150 return; 151 exit: 152 /* Try to resubmit the urb. */ 153 result = usb_submit_urb(urb, GFP_ATOMIC); 154 if (result) 155 dev_err(&port->dev, 156 "%s - failed submitting interrupt in urb, error code=%d\n", 157 __func__, result); 158 } 159 160 static void metrousb_cleanup(struct usb_serial_port *port) 161 { 162 usb_kill_urb(port->interrupt_in_urb); 163 164 metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port); 165 } 166 167 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port) 168 { 169 struct usb_serial *serial = port->serial; 170 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 171 unsigned long flags; 172 int result = 0; 173 174 /* Set the private data information for the port. */ 175 spin_lock_irqsave(&metro_priv->lock, flags); 176 metro_priv->control_state = 0; 177 metro_priv->throttled = 0; 178 spin_unlock_irqrestore(&metro_priv->lock, flags); 179 180 /* Clear the urb pipe. */ 181 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe); 182 183 /* Start reading from the device */ 184 usb_fill_int_urb(port->interrupt_in_urb, serial->dev, 185 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress), 186 port->interrupt_in_urb->transfer_buffer, 187 port->interrupt_in_urb->transfer_buffer_length, 188 metrousb_read_int_callback, port, 1); 189 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 190 191 if (result) { 192 dev_err(&port->dev, 193 "%s - failed submitting interrupt in urb, error code=%d\n", 194 __func__, result); 195 return result; 196 } 197 198 /* Send activate cmd to device */ 199 result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port); 200 if (result) { 201 dev_err(&port->dev, 202 "%s - failed to configure device, error code=%d\n", 203 __func__, result); 204 goto err_kill_urb; 205 } 206 207 return 0; 208 209 err_kill_urb: 210 usb_kill_urb(port->interrupt_in_urb); 211 212 return result; 213 } 214 215 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state) 216 { 217 int retval = 0; 218 unsigned char mcr = METROUSB_MCR_NONE; 219 220 dev_dbg(&serial->dev->dev, "%s - control state = %d\n", 221 __func__, control_state); 222 223 /* Set the modem control value. */ 224 if (control_state & TIOCM_DTR) 225 mcr |= METROUSB_MCR_DTR; 226 if (control_state & TIOCM_RTS) 227 mcr |= METROUSB_MCR_RTS; 228 229 /* Send the command to the usb port. */ 230 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 231 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST, 232 control_state, 0, NULL, 0, WDR_TIMEOUT); 233 if (retval < 0) 234 dev_err(&serial->dev->dev, 235 "%s - set modem ctrl=0x%x failed, error code=%d\n", 236 __func__, mcr, retval); 237 238 return retval; 239 } 240 241 static int metrousb_port_probe(struct usb_serial_port *port) 242 { 243 struct metrousb_private *metro_priv; 244 245 metro_priv = kzalloc_obj(*metro_priv); 246 if (!metro_priv) 247 return -ENOMEM; 248 249 spin_lock_init(&metro_priv->lock); 250 251 usb_set_serial_port_data(port, metro_priv); 252 253 return 0; 254 } 255 256 static void metrousb_port_remove(struct usb_serial_port *port) 257 { 258 struct metrousb_private *metro_priv; 259 260 metro_priv = usb_get_serial_port_data(port); 261 kfree(metro_priv); 262 } 263 264 static void metrousb_throttle(struct tty_struct *tty) 265 { 266 struct usb_serial_port *port = tty->driver_data; 267 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 268 unsigned long flags; 269 270 /* Set the private information for the port to stop reading data. */ 271 spin_lock_irqsave(&metro_priv->lock, flags); 272 metro_priv->throttled = 1; 273 spin_unlock_irqrestore(&metro_priv->lock, flags); 274 } 275 276 static int metrousb_tiocmget(struct tty_struct *tty) 277 { 278 unsigned long control_state = 0; 279 struct usb_serial_port *port = tty->driver_data; 280 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 281 unsigned long flags; 282 283 spin_lock_irqsave(&metro_priv->lock, flags); 284 control_state = metro_priv->control_state; 285 spin_unlock_irqrestore(&metro_priv->lock, flags); 286 287 return control_state; 288 } 289 290 static int metrousb_tiocmset(struct tty_struct *tty, 291 unsigned int set, unsigned int clear) 292 { 293 struct usb_serial_port *port = tty->driver_data; 294 struct usb_serial *serial = port->serial; 295 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 296 unsigned long flags; 297 unsigned long control_state = 0; 298 299 dev_dbg(&port->dev, "%s - set=%d, clear=%d\n", __func__, set, clear); 300 301 spin_lock_irqsave(&metro_priv->lock, flags); 302 control_state = metro_priv->control_state; 303 304 /* Set the RTS and DTR values. */ 305 if (set & TIOCM_RTS) 306 control_state |= TIOCM_RTS; 307 if (set & TIOCM_DTR) 308 control_state |= TIOCM_DTR; 309 if (clear & TIOCM_RTS) 310 control_state &= ~TIOCM_RTS; 311 if (clear & TIOCM_DTR) 312 control_state &= ~TIOCM_DTR; 313 314 metro_priv->control_state = control_state; 315 spin_unlock_irqrestore(&metro_priv->lock, flags); 316 return metrousb_set_modem_ctrl(serial, control_state); 317 } 318 319 static void metrousb_unthrottle(struct tty_struct *tty) 320 { 321 struct usb_serial_port *port = tty->driver_data; 322 struct metrousb_private *metro_priv = usb_get_serial_port_data(port); 323 unsigned long flags; 324 int result = 0; 325 326 /* Set the private information for the port to resume reading data. */ 327 spin_lock_irqsave(&metro_priv->lock, flags); 328 metro_priv->throttled = 0; 329 spin_unlock_irqrestore(&metro_priv->lock, flags); 330 331 /* Submit the urb to read from the port. */ 332 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 333 if (result) 334 dev_err(&port->dev, 335 "failed submitting interrupt in urb error code=%d\n", 336 result); 337 } 338 339 static struct usb_serial_driver metrousb_device = { 340 .driver = { 341 .name = "metro-usb", 342 }, 343 .description = "Metrologic USB to Serial", 344 .id_table = id_table, 345 .num_interrupt_in = 1, 346 .calc_num_ports = metrousb_calc_num_ports, 347 .open = metrousb_open, 348 .close = metrousb_cleanup, 349 .read_int_callback = metrousb_read_int_callback, 350 .port_probe = metrousb_port_probe, 351 .port_remove = metrousb_port_remove, 352 .throttle = metrousb_throttle, 353 .unthrottle = metrousb_unthrottle, 354 .tiocmget = metrousb_tiocmget, 355 .tiocmset = metrousb_tiocmset, 356 }; 357 358 static struct usb_serial_driver * const serial_drivers[] = { 359 &metrousb_device, 360 NULL, 361 }; 362 363 module_usb_serial_driver(serial_drivers, id_table); 364 365 MODULE_LICENSE("GPL v2"); 366 MODULE_AUTHOR("Philip Nicastro"); 367 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>"); 368 MODULE_DESCRIPTION(DRIVER_DESC); 369