xref: /linux/drivers/usb/serial/metro-usb.c (revision 26b0d14106954ae46d2f4f7eec3481828a210f7d)
1 /*
2   Some of this code is credited to Linux USB open source files that are
3   distributed with Linux.
4 
5   Copyright:	2007 Metrologic Instruments. All rights reserved.
6   Copyright:	2011 Azimut Ltd. <http://azimutrzn.ru/>
7 */
8 
9 #include <linux/kernel.h>
10 #include <linux/init.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_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/moduleparam.h>
19 #include <linux/spinlock.h>
20 #include <linux/uaccess.h>
21 #include <linux/usb/serial.h>
22 
23 /* Version Information */
24 #define DRIVER_VERSION "v1.2.0.0"
25 #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
26 
27 /* Product information. */
28 #define FOCUS_VENDOR_ID			0x0C2E
29 #define FOCUS_PRODUCT_ID_BI		0x0720
30 #define FOCUS_PRODUCT_ID_UNI		0x0700
31 
32 #define METROUSB_SET_REQUEST_TYPE	0x40
33 #define METROUSB_SET_MODEM_CTRL_REQUEST	10
34 #define METROUSB_SET_BREAK_REQUEST	0x40
35 #define METROUSB_MCR_NONE		0x08	/* Deactivate DTR and RTS. */
36 #define METROUSB_MCR_RTS		0x0a	/* Activate RTS. */
37 #define METROUSB_MCR_DTR		0x09	/* Activate DTR. */
38 #define WDR_TIMEOUT			5000	/* default urb timeout. */
39 
40 /* Private data structure. */
41 struct metrousb_private {
42 	spinlock_t lock;
43 	int throttled;
44 	unsigned long control_state;
45 };
46 
47 /* Device table list. */
48 static struct usb_device_id id_table[] = {
49 	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
50 	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
51 	{ }, /* Terminating entry. */
52 };
53 MODULE_DEVICE_TABLE(usb, id_table);
54 
55 /* Input parameter constants. */
56 static bool debug;
57 
58 /* UNI-Directional mode commands for device configure */
59 #define UNI_CMD_OPEN	0x80
60 #define UNI_CMD_CLOSE	0xFF
61 
62 inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
63 {
64 	__u16 product_id = le16_to_cpu(
65 		port->serial->dev->descriptor.idProduct);
66 
67 	return product_id == FOCUS_PRODUCT_ID_UNI;
68 }
69 
70 static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
71 {
72 	int ret;
73 	int actual_len;
74 	u8 *buffer_cmd = NULL;
75 
76 	if (!metrousb_is_unidirectional_mode(port))
77 		return 0;
78 
79 	buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
80 	if (!buffer_cmd)
81 		return -ENOMEM;
82 
83 	*buffer_cmd = cmd;
84 
85 	ret = usb_interrupt_msg(port->serial->dev,
86 		usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
87 		buffer_cmd, sizeof(cmd),
88 		&actual_len, USB_CTRL_SET_TIMEOUT);
89 
90 	kfree(buffer_cmd);
91 
92 	if (ret < 0)
93 		return ret;
94 	else if (actual_len != sizeof(cmd))
95 		return -EIO;
96 	return 0;
97 }
98 
99 static void metrousb_read_int_callback(struct urb *urb)
100 {
101 	struct usb_serial_port *port = urb->context;
102 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
103 	struct tty_struct *tty;
104 	unsigned char *data = urb->transfer_buffer;
105 	int throttled = 0;
106 	int result = 0;
107 	unsigned long flags = 0;
108 
109 	dev_dbg(&port->dev, "%s\n", __func__);
110 
111 	switch (urb->status) {
112 	case 0:
113 		/* Success status, read from the port. */
114 		break;
115 	case -ECONNRESET:
116 	case -ENOENT:
117 	case -ESHUTDOWN:
118 		/* urb has been terminated. */
119 		dev_dbg(&port->dev,
120 			"%s - urb shutting down, error code=%d\n",
121 			__func__, urb->status);
122 		return;
123 	default:
124 		dev_dbg(&port->dev,
125 			"%s - non-zero urb received, error code=%d\n",
126 			__func__, urb->status);
127 		goto exit;
128 	}
129 
130 
131 	/* Set the data read from the usb port into the serial port buffer. */
132 	tty = tty_port_tty_get(&port->port);
133 	if (!tty) {
134 		dev_err(&port->dev, "%s - bad tty pointer - exiting\n",
135 			__func__);
136 		return;
137 	}
138 
139 	if (tty && urb->actual_length) {
140 		/* Loop through the data copying each byte to the tty layer. */
141 		tty_insert_flip_string(tty, data, urb->actual_length);
142 
143 		/* Force the data to the tty layer. */
144 		tty_flip_buffer_push(tty);
145 	}
146 	tty_kref_put(tty);
147 
148 	/* Set any port variables. */
149 	spin_lock_irqsave(&metro_priv->lock, flags);
150 	throttled = metro_priv->throttled;
151 	spin_unlock_irqrestore(&metro_priv->lock, flags);
152 
153 	/* Continue trying to read if set. */
154 	if (!throttled) {
155 		usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
156 				 usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
157 				 port->interrupt_in_urb->transfer_buffer,
158 				 port->interrupt_in_urb->transfer_buffer_length,
159 				 metrousb_read_int_callback, port, 1);
160 
161 		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
162 
163 		if (result)
164 			dev_err(&port->dev,
165 				"%s - failed submitting interrupt in urb, error code=%d\n",
166 				__func__, result);
167 	}
168 	return;
169 
170 exit:
171 	/* Try to resubmit the urb. */
172 	result = usb_submit_urb(urb, GFP_ATOMIC);
173 	if (result)
174 		dev_err(&port->dev,
175 			"%s - failed submitting interrupt in urb, error code=%d\n",
176 			__func__, result);
177 }
178 
179 static void metrousb_write_int_callback(struct urb *urb)
180 {
181 	struct usb_serial_port *port = urb->context;
182 
183 	dev_warn(&port->dev, "%s not implemented yet.\n",
184 		__func__);
185 }
186 
187 static void metrousb_cleanup(struct usb_serial_port *port)
188 {
189 	dev_dbg(&port->dev, "%s\n", __func__);
190 
191 	if (port->serial->dev) {
192 		/* Shutdown any interrupt in urbs. */
193 		if (port->interrupt_in_urb) {
194 			usb_unlink_urb(port->interrupt_in_urb);
195 			usb_kill_urb(port->interrupt_in_urb);
196 		}
197 
198 		/* Send deactivate cmd to device */
199 		metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
200 	}
201 }
202 
203 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
204 {
205 	struct usb_serial *serial = port->serial;
206 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
207 	unsigned long flags = 0;
208 	int result = 0;
209 
210 	dev_dbg(&port->dev, "%s\n", __func__);
211 
212 	/* Make sure the urb is initialized. */
213 	if (!port->interrupt_in_urb) {
214 		dev_err(&port->dev, "%s - interrupt urb not initialized\n",
215 			__func__);
216 		return -ENODEV;
217 	}
218 
219 	/* Set the private data information for the port. */
220 	spin_lock_irqsave(&metro_priv->lock, flags);
221 	metro_priv->control_state = 0;
222 	metro_priv->throttled = 0;
223 	spin_unlock_irqrestore(&metro_priv->lock, flags);
224 
225 	/*
226 	 * Force low_latency on so that our tty_push actually forces the data
227 	 * through, otherwise it is scheduled, and with high data rates (like
228 	 * with OHCI) data can get lost.
229 	 */
230 	if (tty)
231 		tty->low_latency = 1;
232 
233 	/* Clear the urb pipe. */
234 	usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
235 
236 	/* Start reading from the device */
237 	usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
238 			  usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
239 			   port->interrupt_in_urb->transfer_buffer,
240 			   port->interrupt_in_urb->transfer_buffer_length,
241 			   metrousb_read_int_callback, port, 1);
242 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
243 
244 	if (result) {
245 		dev_err(&port->dev,
246 			"%s - failed submitting interrupt in urb, error code=%d\n",
247 			__func__, result);
248 		goto exit;
249 	}
250 
251 	/* Send activate cmd to device */
252 	result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
253 	if (result) {
254 		dev_err(&port->dev,
255 			"%s - failed to configure device for port number=%d, error code=%d\n",
256 			__func__, port->number, result);
257 		goto exit;
258 	}
259 
260 	dev_dbg(&port->dev, "%s - port open\n", __func__);
261 exit:
262 	return result;
263 }
264 
265 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
266 {
267 	int retval = 0;
268 	unsigned char mcr = METROUSB_MCR_NONE;
269 
270 	dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
271 		__func__, control_state);
272 
273 	/* Set the modem control value. */
274 	if (control_state & TIOCM_DTR)
275 		mcr |= METROUSB_MCR_DTR;
276 	if (control_state & TIOCM_RTS)
277 		mcr |= METROUSB_MCR_RTS;
278 
279 	/* Send the command to the usb port. */
280 	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
281 				METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
282 				control_state, 0, NULL, 0, WDR_TIMEOUT);
283 	if (retval < 0)
284 		dev_err(&serial->dev->dev,
285 			"%s - set modem ctrl=0x%x failed, error code=%d\n",
286 			__func__, mcr, retval);
287 
288 	return retval;
289 }
290 
291 static void metrousb_shutdown(struct usb_serial *serial)
292 {
293 	int i = 0;
294 
295 	dev_dbg(&serial->dev->dev, "%s\n", __func__);
296 
297 	/* Stop reading and writing on all ports. */
298 	for (i = 0; i < serial->num_ports; ++i) {
299 		/* Close any open urbs. */
300 		metrousb_cleanup(serial->port[i]);
301 
302 		/* Free memory. */
303 		kfree(usb_get_serial_port_data(serial->port[i]));
304 		usb_set_serial_port_data(serial->port[i], NULL);
305 
306 		dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
307 			__func__, serial->port[i]->number);
308 	}
309 }
310 
311 static int metrousb_startup(struct usb_serial *serial)
312 {
313 	struct metrousb_private *metro_priv;
314 	struct usb_serial_port *port;
315 	int i = 0;
316 
317 	dev_dbg(&serial->dev->dev, "%s\n", __func__);
318 
319 	/* Loop through the serial ports setting up the private structures.
320 	 * Currently we only use one port. */
321 	for (i = 0; i < serial->num_ports; ++i) {
322 		port = serial->port[i];
323 
324 		/* Declare memory. */
325 		metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
326 		if (!metro_priv)
327 			return -ENOMEM;
328 
329 		/* Initialize memory. */
330 		spin_lock_init(&metro_priv->lock);
331 		usb_set_serial_port_data(port, metro_priv);
332 
333 		dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
334 			__func__, port->number);
335 	}
336 
337 	return 0;
338 }
339 
340 static void metrousb_throttle(struct tty_struct *tty)
341 {
342 	struct usb_serial_port *port = tty->driver_data;
343 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
344 	unsigned long flags = 0;
345 
346 	dev_dbg(tty->dev, "%s\n", __func__);
347 
348 	/* Set the private information for the port to stop reading data. */
349 	spin_lock_irqsave(&metro_priv->lock, flags);
350 	metro_priv->throttled = 1;
351 	spin_unlock_irqrestore(&metro_priv->lock, flags);
352 }
353 
354 static int metrousb_tiocmget(struct tty_struct *tty)
355 {
356 	unsigned long control_state = 0;
357 	struct usb_serial_port *port = tty->driver_data;
358 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
359 	unsigned long flags = 0;
360 
361 	dev_dbg(tty->dev, "%s\n", __func__);
362 
363 	spin_lock_irqsave(&metro_priv->lock, flags);
364 	control_state = metro_priv->control_state;
365 	spin_unlock_irqrestore(&metro_priv->lock, flags);
366 
367 	return control_state;
368 }
369 
370 static int metrousb_tiocmset(struct tty_struct *tty,
371 			     unsigned int set, unsigned int clear)
372 {
373 	struct usb_serial_port *port = tty->driver_data;
374 	struct usb_serial *serial = port->serial;
375 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
376 	unsigned long flags = 0;
377 	unsigned long control_state = 0;
378 
379 	dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
380 
381 	spin_lock_irqsave(&metro_priv->lock, flags);
382 	control_state = metro_priv->control_state;
383 
384 	/* Set the RTS and DTR values. */
385 	if (set & TIOCM_RTS)
386 		control_state |= TIOCM_RTS;
387 	if (set & TIOCM_DTR)
388 		control_state |= TIOCM_DTR;
389 	if (clear & TIOCM_RTS)
390 		control_state &= ~TIOCM_RTS;
391 	if (clear & TIOCM_DTR)
392 		control_state &= ~TIOCM_DTR;
393 
394 	metro_priv->control_state = control_state;
395 	spin_unlock_irqrestore(&metro_priv->lock, flags);
396 	return metrousb_set_modem_ctrl(serial, control_state);
397 }
398 
399 static void metrousb_unthrottle(struct tty_struct *tty)
400 {
401 	struct usb_serial_port *port = tty->driver_data;
402 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
403 	unsigned long flags = 0;
404 	int result = 0;
405 
406 	dev_dbg(tty->dev, "%s\n", __func__);
407 
408 	/* Set the private information for the port to resume reading data. */
409 	spin_lock_irqsave(&metro_priv->lock, flags);
410 	metro_priv->throttled = 0;
411 	spin_unlock_irqrestore(&metro_priv->lock, flags);
412 
413 	/* Submit the urb to read from the port. */
414 	port->interrupt_in_urb->dev = port->serial->dev;
415 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
416 	if (result)
417 		dev_err(tty->dev,
418 			"failed submitting interrupt in urb error code=%d\n",
419 			result);
420 }
421 
422 static struct usb_serial_driver metrousb_device = {
423 	.driver = {
424 		.owner =	THIS_MODULE,
425 		.name =		"metro-usb",
426 	},
427 	.description		= "Metrologic USB to Serial",
428 	.id_table		= id_table,
429 	.num_ports		= 1,
430 	.open			= metrousb_open,
431 	.close			= metrousb_cleanup,
432 	.read_int_callback	= metrousb_read_int_callback,
433 	.write_int_callback	= metrousb_write_int_callback,
434 	.attach			= metrousb_startup,
435 	.release		= metrousb_shutdown,
436 	.throttle		= metrousb_throttle,
437 	.unthrottle		= metrousb_unthrottle,
438 	.tiocmget		= metrousb_tiocmget,
439 	.tiocmset		= metrousb_tiocmset,
440 };
441 
442 static struct usb_serial_driver * const serial_drivers[] = {
443 	&metrousb_device,
444 	NULL,
445 };
446 
447 module_usb_serial_driver(serial_drivers, id_table);
448 
449 MODULE_LICENSE("GPL");
450 MODULE_AUTHOR("Philip Nicastro");
451 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
452 MODULE_DESCRIPTION(DRIVER_DESC);
453 
454 /* Module input parameters */
455 module_param(debug, bool, S_IRUGO | S_IWUSR);
456 MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");
457