0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/tty.h>
0012 #include <linux/module.h>
0013 #include <linux/usb.h>
0014 #include <linux/errno.h>
0015 #include <linux/slab.h>
0016 #include <linux/tty_driver.h>
0017 #include <linux/tty_flip.h>
0018 #include <linux/moduleparam.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/usb/serial.h>
0022
0023 #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
0024
0025
0026 #define FOCUS_VENDOR_ID 0x0C2E
0027 #define FOCUS_PRODUCT_ID_BI 0x0720
0028 #define FOCUS_PRODUCT_ID_UNI 0x0700
0029
0030 #define METROUSB_SET_REQUEST_TYPE 0x40
0031 #define METROUSB_SET_MODEM_CTRL_REQUEST 10
0032 #define METROUSB_SET_BREAK_REQUEST 0x40
0033 #define METROUSB_MCR_NONE 0x08
0034 #define METROUSB_MCR_RTS 0x0a
0035 #define METROUSB_MCR_DTR 0x09
0036 #define WDR_TIMEOUT 5000
0037
0038
0039 struct metrousb_private {
0040 spinlock_t lock;
0041 int throttled;
0042 unsigned long control_state;
0043 };
0044
0045
0046 static const struct usb_device_id id_table[] = {
0047 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
0048 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
0049 { USB_DEVICE_INTERFACE_CLASS(0x0c2e, 0x0730, 0xff) },
0050 { },
0051 };
0052 MODULE_DEVICE_TABLE(usb, id_table);
0053
0054
0055 #define UNI_CMD_OPEN 0x80
0056 #define UNI_CMD_CLOSE 0xFF
0057
0058 static int metrousb_is_unidirectional_mode(struct usb_serial *serial)
0059 {
0060 u16 product_id = le16_to_cpu(serial->dev->descriptor.idProduct);
0061
0062 return product_id == FOCUS_PRODUCT_ID_UNI;
0063 }
0064
0065 static int metrousb_calc_num_ports(struct usb_serial *serial,
0066 struct usb_serial_endpoints *epds)
0067 {
0068 if (metrousb_is_unidirectional_mode(serial)) {
0069 if (epds->num_interrupt_out == 0) {
0070 dev_err(&serial->interface->dev, "interrupt-out endpoint missing\n");
0071 return -ENODEV;
0072 }
0073 }
0074
0075 return 1;
0076 }
0077
0078 static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
0079 {
0080 int ret;
0081 int actual_len;
0082 u8 *buffer_cmd = NULL;
0083
0084 if (!metrousb_is_unidirectional_mode(port->serial))
0085 return 0;
0086
0087 buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
0088 if (!buffer_cmd)
0089 return -ENOMEM;
0090
0091 *buffer_cmd = cmd;
0092
0093 ret = usb_interrupt_msg(port->serial->dev,
0094 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
0095 buffer_cmd, sizeof(cmd),
0096 &actual_len, USB_CTRL_SET_TIMEOUT);
0097
0098 kfree(buffer_cmd);
0099
0100 if (ret < 0)
0101 return ret;
0102 else if (actual_len != sizeof(cmd))
0103 return -EIO;
0104 return 0;
0105 }
0106
0107 static void metrousb_read_int_callback(struct urb *urb)
0108 {
0109 struct usb_serial_port *port = urb->context;
0110 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0111 unsigned char *data = urb->transfer_buffer;
0112 unsigned long flags;
0113 int throttled = 0;
0114 int result = 0;
0115
0116 dev_dbg(&port->dev, "%s\n", __func__);
0117
0118 switch (urb->status) {
0119 case 0:
0120
0121 break;
0122 case -ECONNRESET:
0123 case -ENOENT:
0124 case -ESHUTDOWN:
0125
0126 dev_dbg(&port->dev,
0127 "%s - urb shutting down, error code=%d\n",
0128 __func__, urb->status);
0129 return;
0130 default:
0131 dev_dbg(&port->dev,
0132 "%s - non-zero urb received, error code=%d\n",
0133 __func__, urb->status);
0134 goto exit;
0135 }
0136
0137
0138
0139 if (urb->actual_length) {
0140
0141 tty_insert_flip_string(&port->port, data, urb->actual_length);
0142
0143
0144 tty_flip_buffer_push(&port->port);
0145 }
0146
0147
0148 spin_lock_irqsave(&metro_priv->lock, flags);
0149 throttled = metro_priv->throttled;
0150 spin_unlock_irqrestore(&metro_priv->lock, flags);
0151
0152 if (throttled)
0153 return;
0154 exit:
0155
0156 result = usb_submit_urb(urb, GFP_ATOMIC);
0157 if (result)
0158 dev_err(&port->dev,
0159 "%s - failed submitting interrupt in urb, error code=%d\n",
0160 __func__, result);
0161 }
0162
0163 static void metrousb_cleanup(struct usb_serial_port *port)
0164 {
0165 usb_kill_urb(port->interrupt_in_urb);
0166
0167 metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
0168 }
0169
0170 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
0171 {
0172 struct usb_serial *serial = port->serial;
0173 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0174 unsigned long flags;
0175 int result = 0;
0176
0177
0178 spin_lock_irqsave(&metro_priv->lock, flags);
0179 metro_priv->control_state = 0;
0180 metro_priv->throttled = 0;
0181 spin_unlock_irqrestore(&metro_priv->lock, flags);
0182
0183
0184 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
0185
0186
0187 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
0188 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
0189 port->interrupt_in_urb->transfer_buffer,
0190 port->interrupt_in_urb->transfer_buffer_length,
0191 metrousb_read_int_callback, port, 1);
0192 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0193
0194 if (result) {
0195 dev_err(&port->dev,
0196 "%s - failed submitting interrupt in urb, error code=%d\n",
0197 __func__, result);
0198 return result;
0199 }
0200
0201
0202 result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
0203 if (result) {
0204 dev_err(&port->dev,
0205 "%s - failed to configure device, error code=%d\n",
0206 __func__, result);
0207 goto err_kill_urb;
0208 }
0209
0210 return 0;
0211
0212 err_kill_urb:
0213 usb_kill_urb(port->interrupt_in_urb);
0214
0215 return result;
0216 }
0217
0218 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
0219 {
0220 int retval = 0;
0221 unsigned char mcr = METROUSB_MCR_NONE;
0222
0223 dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
0224 __func__, control_state);
0225
0226
0227 if (control_state & TIOCM_DTR)
0228 mcr |= METROUSB_MCR_DTR;
0229 if (control_state & TIOCM_RTS)
0230 mcr |= METROUSB_MCR_RTS;
0231
0232
0233 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
0234 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
0235 control_state, 0, NULL, 0, WDR_TIMEOUT);
0236 if (retval < 0)
0237 dev_err(&serial->dev->dev,
0238 "%s - set modem ctrl=0x%x failed, error code=%d\n",
0239 __func__, mcr, retval);
0240
0241 return retval;
0242 }
0243
0244 static int metrousb_port_probe(struct usb_serial_port *port)
0245 {
0246 struct metrousb_private *metro_priv;
0247
0248 metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
0249 if (!metro_priv)
0250 return -ENOMEM;
0251
0252 spin_lock_init(&metro_priv->lock);
0253
0254 usb_set_serial_port_data(port, metro_priv);
0255
0256 return 0;
0257 }
0258
0259 static void metrousb_port_remove(struct usb_serial_port *port)
0260 {
0261 struct metrousb_private *metro_priv;
0262
0263 metro_priv = usb_get_serial_port_data(port);
0264 kfree(metro_priv);
0265 }
0266
0267 static void metrousb_throttle(struct tty_struct *tty)
0268 {
0269 struct usb_serial_port *port = tty->driver_data;
0270 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0271 unsigned long flags;
0272
0273
0274 spin_lock_irqsave(&metro_priv->lock, flags);
0275 metro_priv->throttled = 1;
0276 spin_unlock_irqrestore(&metro_priv->lock, flags);
0277 }
0278
0279 static int metrousb_tiocmget(struct tty_struct *tty)
0280 {
0281 unsigned long control_state = 0;
0282 struct usb_serial_port *port = tty->driver_data;
0283 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0284 unsigned long flags;
0285
0286 spin_lock_irqsave(&metro_priv->lock, flags);
0287 control_state = metro_priv->control_state;
0288 spin_unlock_irqrestore(&metro_priv->lock, flags);
0289
0290 return control_state;
0291 }
0292
0293 static int metrousb_tiocmset(struct tty_struct *tty,
0294 unsigned int set, unsigned int clear)
0295 {
0296 struct usb_serial_port *port = tty->driver_data;
0297 struct usb_serial *serial = port->serial;
0298 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0299 unsigned long flags;
0300 unsigned long control_state = 0;
0301
0302 dev_dbg(&port->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
0303
0304 spin_lock_irqsave(&metro_priv->lock, flags);
0305 control_state = metro_priv->control_state;
0306
0307
0308 if (set & TIOCM_RTS)
0309 control_state |= TIOCM_RTS;
0310 if (set & TIOCM_DTR)
0311 control_state |= TIOCM_DTR;
0312 if (clear & TIOCM_RTS)
0313 control_state &= ~TIOCM_RTS;
0314 if (clear & TIOCM_DTR)
0315 control_state &= ~TIOCM_DTR;
0316
0317 metro_priv->control_state = control_state;
0318 spin_unlock_irqrestore(&metro_priv->lock, flags);
0319 return metrousb_set_modem_ctrl(serial, control_state);
0320 }
0321
0322 static void metrousb_unthrottle(struct tty_struct *tty)
0323 {
0324 struct usb_serial_port *port = tty->driver_data;
0325 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
0326 unsigned long flags;
0327 int result = 0;
0328
0329
0330 spin_lock_irqsave(&metro_priv->lock, flags);
0331 metro_priv->throttled = 0;
0332 spin_unlock_irqrestore(&metro_priv->lock, flags);
0333
0334
0335 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
0336 if (result)
0337 dev_err(&port->dev,
0338 "failed submitting interrupt in urb error code=%d\n",
0339 result);
0340 }
0341
0342 static struct usb_serial_driver metrousb_device = {
0343 .driver = {
0344 .owner = THIS_MODULE,
0345 .name = "metro-usb",
0346 },
0347 .description = "Metrologic USB to Serial",
0348 .id_table = id_table,
0349 .num_interrupt_in = 1,
0350 .calc_num_ports = metrousb_calc_num_ports,
0351 .open = metrousb_open,
0352 .close = metrousb_cleanup,
0353 .read_int_callback = metrousb_read_int_callback,
0354 .port_probe = metrousb_port_probe,
0355 .port_remove = metrousb_port_remove,
0356 .throttle = metrousb_throttle,
0357 .unthrottle = metrousb_unthrottle,
0358 .tiocmget = metrousb_tiocmget,
0359 .tiocmset = metrousb_tiocmset,
0360 };
0361
0362 static struct usb_serial_driver * const serial_drivers[] = {
0363 &metrousb_device,
0364 NULL,
0365 };
0366
0367 module_usb_serial_driver(serial_drivers, id_table);
0368
0369 MODULE_LICENSE("GPL v2");
0370 MODULE_AUTHOR("Philip Nicastro");
0371 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
0372 MODULE_DESCRIPTION(DRIVER_DESC);