blob: cc84da8dbb8495bc10bff34bcafd89a5960ec65e [file] [log] [blame]
Aleksey Babahin43d186f2012-03-08 13:18:43 -08001/*
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -08002 Some of this code is credited to Linux USB open source files that are
3 distributed with Linux.
Aleksey Babahin43d186f2012-03-08 13:18:43 -08004
5 Copyright: 2007 Metrologic Instruments. All rights reserved.
6 Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
Aleksey Babahin43d186f2012-03-08 13:18:43 -08007*/
8
9#include <linux/kernel.h>
Aleksey Babahin43d186f2012-03-08 13:18:43 -080010#include <linux/tty.h>
11#include <linux/module.h>
12#include <linux/usb.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15#include <linux/tty_driver.h>
16#include <linux/tty_flip.h>
17#include <linux/moduleparam.h>
18#include <linux/spinlock.h>
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080019#include <linux/uaccess.h>
Aleksey Babahin43d186f2012-03-08 13:18:43 -080020#include <linux/usb/serial.h>
21
Aleksey Babahin43d186f2012-03-08 13:18:43 -080022#define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
23
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080024/* Product information. */
25#define FOCUS_VENDOR_ID 0x0C2E
Aleksey Babahin810ec782012-03-20 00:46:31 +040026#define FOCUS_PRODUCT_ID_BI 0x0720
27#define FOCUS_PRODUCT_ID_UNI 0x0700
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080028
29#define METROUSB_SET_REQUEST_TYPE 0x40
30#define METROUSB_SET_MODEM_CTRL_REQUEST 10
31#define METROUSB_SET_BREAK_REQUEST 0x40
32#define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
33#define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
34#define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080035#define WDR_TIMEOUT 5000 /* default urb timeout. */
Greg Kroah-Hartman159d4d82012-03-08 13:42:41 -080036
37/* Private data structure. */
38struct metrousb_private {
39 spinlock_t lock;
40 int throttled;
41 unsigned long control_state;
42};
43
Aleksey Babahin43d186f2012-03-08 13:18:43 -080044/* Device table list. */
Johan Hovold5c6b98d2013-12-29 19:22:54 +010045static const struct usb_device_id id_table[] = {
Aleksey Babahin810ec782012-03-20 00:46:31 +040046 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
Aleksey Babahin43d186f2012-03-08 13:18:43 -080047 { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
Aleksey Babahin43d186f2012-03-08 13:18:43 -080048 { }, /* Terminating entry. */
49};
50MODULE_DEVICE_TABLE(usb, id_table);
51
Aleksey Babahin70457782012-03-20 00:46:33 +040052/* UNI-Directional mode commands for device configure */
53#define UNI_CMD_OPEN 0x80
54#define UNI_CMD_CLOSE 0xFF
55
Rashika Kheria8f24c492013-12-19 15:43:17 +053056static inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
Aleksey Babahin70457782012-03-20 00:46:33 +040057{
58 __u16 product_id = le16_to_cpu(
59 port->serial->dev->descriptor.idProduct);
60
61 return product_id == FOCUS_PRODUCT_ID_UNI;
62}
63
64static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
65{
66 int ret;
67 int actual_len;
68 u8 *buffer_cmd = NULL;
69
70 if (!metrousb_is_unidirectional_mode(port))
71 return 0;
72
73 buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
74 if (!buffer_cmd)
75 return -ENOMEM;
76
77 *buffer_cmd = cmd;
78
79 ret = usb_interrupt_msg(port->serial->dev,
80 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
81 buffer_cmd, sizeof(cmd),
82 &actual_len, USB_CTRL_SET_TIMEOUT);
83
84 kfree(buffer_cmd);
85
86 if (ret < 0)
87 return ret;
88 else if (actual_len != sizeof(cmd))
89 return -EIO;
90 return 0;
91}
92
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -080093static void metrousb_read_int_callback(struct urb *urb)
Aleksey Babahin43d186f2012-03-08 13:18:43 -080094{
Greg Kroah-Hartman8111e4e2012-03-08 14:00:11 -080095 struct usb_serial_port *port = urb->context;
Aleksey Babahin43d186f2012-03-08 13:18:43 -080096 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
Aleksey Babahin43d186f2012-03-08 13:18:43 -080097 unsigned char *data = urb->transfer_buffer;
98 int throttled = 0;
99 int result = 0;
100 unsigned long flags = 0;
101
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800102 dev_dbg(&port->dev, "%s\n", __func__);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800103
104 switch (urb->status) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800105 case 0:
106 /* Success status, read from the port. */
107 break;
108 case -ECONNRESET:
109 case -ENOENT:
110 case -ESHUTDOWN:
111 /* urb has been terminated. */
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800112 dev_dbg(&port->dev,
113 "%s - urb shutting down, error code=%d\n",
Aleksey Babahinbd2c09b2012-03-20 00:46:35 +0400114 __func__, urb->status);
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800115 return;
116 default:
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800117 dev_dbg(&port->dev,
118 "%s - non-zero urb received, error code=%d\n",
Aleksey Babahinbd2c09b2012-03-20 00:46:35 +0400119 __func__, urb->status);
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800120 goto exit;
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800121 }
122
123
124 /* Set the data read from the usb port into the serial port buffer. */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100125 if (urb->actual_length) {
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800126 /* Loop through the data copying each byte to the tty layer. */
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100127 tty_insert_flip_string(&port->port, data, urb->actual_length);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800128
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800129 /* Force the data to the tty layer. */
Jiri Slaby2e124b42013-01-03 15:53:06 +0100130 tty_flip_buffer_push(&port->port);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800131 }
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800132
133 /* Set any port variables. */
134 spin_lock_irqsave(&metro_priv->lock, flags);
135 throttled = metro_priv->throttled;
136 spin_unlock_irqrestore(&metro_priv->lock, flags);
137
Johan Hovoldacfe2762017-02-08 13:49:19 +0100138 if (throttled)
139 return;
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800140exit:
141 /* Try to resubmit the urb. */
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800142 result = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800143 if (result)
Aleksey Babahin91fbecf2012-03-20 00:46:34 +0400144 dev_err(&port->dev,
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800145 "%s - failed submitting interrupt in urb, error code=%d\n",
146 __func__, result);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800147}
148
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800149static void metrousb_cleanup(struct usb_serial_port *port)
150{
Johan Hovold2ee44fb2012-10-25 10:29:00 +0200151 usb_kill_urb(port->interrupt_in_urb);
Aleksey Babahin70457782012-03-20 00:46:33 +0400152
Johan Hovold5813f282013-03-21 12:37:40 +0100153 metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800154}
155
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800156static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
157{
158 struct usb_serial *serial = port->serial;
159 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
160 unsigned long flags = 0;
161 int result = 0;
162
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800163 /* Make sure the urb is initialized. */
164 if (!port->interrupt_in_urb) {
Aleksey Babahin91fbecf2012-03-20 00:46:34 +0400165 dev_err(&port->dev, "%s - interrupt urb not initialized\n",
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800166 __func__);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800167 return -ENODEV;
168 }
169
170 /* Set the private data information for the port. */
171 spin_lock_irqsave(&metro_priv->lock, flags);
172 metro_priv->control_state = 0;
173 metro_priv->throttled = 0;
174 spin_unlock_irqrestore(&metro_priv->lock, flags);
175
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800176 /* Clear the urb pipe. */
177 usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
178
179 /* Start reading from the device */
180 usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
181 usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
182 port->interrupt_in_urb->transfer_buffer,
183 port->interrupt_in_urb->transfer_buffer_length,
184 metrousb_read_int_callback, port, 1);
185 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
186
187 if (result) {
Aleksey Babahin91fbecf2012-03-20 00:46:34 +0400188 dev_err(&port->dev,
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800189 "%s - failed submitting interrupt in urb, error code=%d\n",
190 __func__, result);
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800191 goto exit;
192 }
193
Aleksey Babahin70457782012-03-20 00:46:33 +0400194 /* Send activate cmd to device */
195 result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
196 if (result) {
197 dev_err(&port->dev,
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700198 "%s - failed to configure device, error code=%d\n",
199 __func__, result);
Aleksey Babahin70457782012-03-20 00:46:33 +0400200 goto exit;
201 }
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800202exit:
203 return result;
204}
205
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800206static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
207{
208 int retval = 0;
209 unsigned char mcr = METROUSB_MCR_NONE;
210
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800211 dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
212 __func__, control_state);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800213
214 /* Set the modem control value. */
215 if (control_state & TIOCM_DTR)
216 mcr |= METROUSB_MCR_DTR;
217 if (control_state & TIOCM_RTS)
218 mcr |= METROUSB_MCR_RTS;
219
220 /* Send the command to the usb port. */
221 retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
222 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
223 control_state, 0, NULL, 0, WDR_TIMEOUT);
224 if (retval < 0)
Aleksey Babahin91fbecf2012-03-20 00:46:34 +0400225 dev_err(&serial->dev->dev,
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800226 "%s - set modem ctrl=0x%x failed, error code=%d\n",
227 __func__, mcr, retval);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800228
229 return retval;
230}
231
Johan Hovold50dde862012-10-25 10:28:59 +0200232static int metrousb_port_probe(struct usb_serial_port *port)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800233{
234 struct metrousb_private *metro_priv;
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800235
Johan Hovold50dde862012-10-25 10:28:59 +0200236 metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
237 if (!metro_priv)
238 return -ENOMEM;
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800239
Johan Hovold50dde862012-10-25 10:28:59 +0200240 spin_lock_init(&metro_priv->lock);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800241
Johan Hovold50dde862012-10-25 10:28:59 +0200242 usb_set_serial_port_data(port, metro_priv);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800243
Johan Hovold50dde862012-10-25 10:28:59 +0200244 return 0;
245}
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800246
Johan Hovold50dde862012-10-25 10:28:59 +0200247static int metrousb_port_remove(struct usb_serial_port *port)
248{
249 struct metrousb_private *metro_priv;
250
251 metro_priv = usb_get_serial_port_data(port);
252 kfree(metro_priv);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800253
254 return 0;
255}
256
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800257static void metrousb_throttle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800258{
259 struct usb_serial_port *port = tty->driver_data;
260 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
261 unsigned long flags = 0;
262
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800263 /* Set the private information for the port to stop reading data. */
264 spin_lock_irqsave(&metro_priv->lock, flags);
265 metro_priv->throttled = 1;
266 spin_unlock_irqrestore(&metro_priv->lock, flags);
267}
268
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800269static int metrousb_tiocmget(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800270{
271 unsigned long control_state = 0;
272 struct usb_serial_port *port = tty->driver_data;
273 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
274 unsigned long flags = 0;
275
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800276 spin_lock_irqsave(&metro_priv->lock, flags);
277 control_state = metro_priv->control_state;
278 spin_unlock_irqrestore(&metro_priv->lock, flags);
279
280 return control_state;
281}
282
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800283static int metrousb_tiocmset(struct tty_struct *tty,
284 unsigned int set, unsigned int clear)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800285{
286 struct usb_serial_port *port = tty->driver_data;
287 struct usb_serial *serial = port->serial;
288 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
289 unsigned long flags = 0;
290 unsigned long control_state = 0;
291
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800292 dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800293
294 spin_lock_irqsave(&metro_priv->lock, flags);
295 control_state = metro_priv->control_state;
296
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800297 /* Set the RTS and DTR values. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800298 if (set & TIOCM_RTS)
299 control_state |= TIOCM_RTS;
300 if (set & TIOCM_DTR)
301 control_state |= TIOCM_DTR;
302 if (clear & TIOCM_RTS)
303 control_state &= ~TIOCM_RTS;
304 if (clear & TIOCM_DTR)
305 control_state &= ~TIOCM_DTR;
306
307 metro_priv->control_state = control_state;
308 spin_unlock_irqrestore(&metro_priv->lock, flags);
309 return metrousb_set_modem_ctrl(serial, control_state);
310}
311
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800312static void metrousb_unthrottle(struct tty_struct *tty)
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800313{
314 struct usb_serial_port *port = tty->driver_data;
315 struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
316 unsigned long flags = 0;
317 int result = 0;
318
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800319 /* Set the private information for the port to resume reading data. */
320 spin_lock_irqsave(&metro_priv->lock, flags);
321 metro_priv->throttled = 0;
322 spin_unlock_irqrestore(&metro_priv->lock, flags);
323
324 /* Submit the urb to read from the port. */
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800325 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800326 if (result)
Aleksey Babahin91fbecf2012-03-20 00:46:34 +0400327 dev_err(tty->dev,
Greg Kroah-Hartman5db51b52012-03-08 14:16:12 -0800328 "failed submitting interrupt in urb error code=%d\n",
329 result);
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800330}
331
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800332static struct usb_serial_driver metrousb_device = {
333 .driver = {
334 .owner = THIS_MODULE,
335 .name = "metro-usb",
336 },
Aleksey Babahine2dd3af2012-03-20 00:46:37 +0400337 .description = "Metrologic USB to Serial",
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800338 .id_table = id_table,
339 .num_ports = 1,
340 .open = metrousb_open,
341 .close = metrousb_cleanup,
342 .read_int_callback = metrousb_read_int_callback,
Johan Hovold50dde862012-10-25 10:28:59 +0200343 .port_probe = metrousb_port_probe,
344 .port_remove = metrousb_port_remove,
Greg Kroah-Hartman9fbd1642012-03-08 13:55:41 -0800345 .throttle = metrousb_throttle,
346 .unthrottle = metrousb_unthrottle,
347 .tiocmget = metrousb_tiocmget,
348 .tiocmset = metrousb_tiocmset,
349};
350
351static struct usb_serial_driver * const serial_drivers[] = {
352 &metrousb_device,
353 NULL,
354};
355
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700356module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman1935e352012-03-08 13:39:53 -0800357
Aleksey Babahin43d186f2012-03-08 13:18:43 -0800358MODULE_LICENSE("GPL");
Greg Kroah-Hartmand4cbd6e2012-03-08 13:50:54 -0800359MODULE_AUTHOR("Philip Nicastro");
360MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
361MODULE_DESCRIPTION(DRIVER_DESC);