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