]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/serial/metro-usb.c
Merge 3.6-rc6 into usb-next
[karo-tx-linux.git] / drivers / usb / serial / metro-usb.c
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 /* UNI-Directional mode commands for device configure */
56 #define UNI_CMD_OPEN    0x80
57 #define UNI_CMD_CLOSE   0xFF
58
59 inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
60 {
61         __u16 product_id = le16_to_cpu(
62                 port->serial->dev->descriptor.idProduct);
63
64         return product_id == FOCUS_PRODUCT_ID_UNI;
65 }
66
67 static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
68 {
69         int ret;
70         int actual_len;
71         u8 *buffer_cmd = NULL;
72
73         if (!metrousb_is_unidirectional_mode(port))
74                 return 0;
75
76         buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
77         if (!buffer_cmd)
78                 return -ENOMEM;
79
80         *buffer_cmd = cmd;
81
82         ret = usb_interrupt_msg(port->serial->dev,
83                 usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
84                 buffer_cmd, sizeof(cmd),
85                 &actual_len, USB_CTRL_SET_TIMEOUT);
86
87         kfree(buffer_cmd);
88
89         if (ret < 0)
90                 return ret;
91         else if (actual_len != sizeof(cmd))
92                 return -EIO;
93         return 0;
94 }
95
96 static void metrousb_read_int_callback(struct urb *urb)
97 {
98         struct usb_serial_port *port = urb->context;
99         struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
100         struct tty_struct *tty;
101         unsigned char *data = urb->transfer_buffer;
102         int throttled = 0;
103         int result = 0;
104         unsigned long flags = 0;
105
106         dev_dbg(&port->dev, "%s\n", __func__);
107
108         switch (urb->status) {
109         case 0:
110                 /* Success status, read from the port. */
111                 break;
112         case -ECONNRESET:
113         case -ENOENT:
114         case -ESHUTDOWN:
115                 /* urb has been terminated. */
116                 dev_dbg(&port->dev,
117                         "%s - urb shutting down, error code=%d\n",
118                         __func__, urb->status);
119                 return;
120         default:
121                 dev_dbg(&port->dev,
122                         "%s - non-zero urb received, error code=%d\n",
123                         __func__, urb->status);
124                 goto exit;
125         }
126
127
128         /* Set the data read from the usb port into the serial port buffer. */
129         tty = tty_port_tty_get(&port->port);
130         if (!tty) {
131                 dev_err(&port->dev, "%s - bad tty pointer - exiting\n",
132                         __func__);
133                 return;
134         }
135
136         if (tty && urb->actual_length) {
137                 /* Loop through the data copying each byte to the tty layer. */
138                 tty_insert_flip_string(tty, data, urb->actual_length);
139
140                 /* Force the data to the tty layer. */
141                 tty_flip_buffer_push(tty);
142         }
143         tty_kref_put(tty);
144
145         /* Set any port variables. */
146         spin_lock_irqsave(&metro_priv->lock, flags);
147         throttled = metro_priv->throttled;
148         spin_unlock_irqrestore(&metro_priv->lock, flags);
149
150         /* Continue trying to read if set. */
151         if (!throttled) {
152                 usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
153                                  usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
154                                  port->interrupt_in_urb->transfer_buffer,
155                                  port->interrupt_in_urb->transfer_buffer_length,
156                                  metrousb_read_int_callback, port, 1);
157
158                 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
159
160                 if (result)
161                         dev_err(&port->dev,
162                                 "%s - failed submitting interrupt in urb, error code=%d\n",
163                                 __func__, result);
164         }
165         return;
166
167 exit:
168         /* Try to resubmit the urb. */
169         result = usb_submit_urb(urb, GFP_ATOMIC);
170         if (result)
171                 dev_err(&port->dev,
172                         "%s - failed submitting interrupt in urb, error code=%d\n",
173                         __func__, result);
174 }
175
176 static void metrousb_write_int_callback(struct urb *urb)
177 {
178         struct usb_serial_port *port = urb->context;
179
180         dev_warn(&port->dev, "%s not implemented yet.\n",
181                 __func__);
182 }
183
184 static void metrousb_cleanup(struct usb_serial_port *port)
185 {
186         dev_dbg(&port->dev, "%s\n", __func__);
187
188         if (port->serial->dev) {
189                 /* Shutdown any interrupt in urbs. */
190                 if (port->interrupt_in_urb) {
191                         usb_unlink_urb(port->interrupt_in_urb);
192                         usb_kill_urb(port->interrupt_in_urb);
193                 }
194
195                 /* Send deactivate cmd to device */
196                 metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
197         }
198 }
199
200 static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
201 {
202         struct usb_serial *serial = port->serial;
203         struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
204         unsigned long flags = 0;
205         int result = 0;
206
207         dev_dbg(&port->dev, "%s\n", __func__);
208
209         /* Make sure the urb is initialized. */
210         if (!port->interrupt_in_urb) {
211                 dev_err(&port->dev, "%s - interrupt urb not initialized\n",
212                         __func__);
213                 return -ENODEV;
214         }
215
216         /* Set the private data information for the port. */
217         spin_lock_irqsave(&metro_priv->lock, flags);
218         metro_priv->control_state = 0;
219         metro_priv->throttled = 0;
220         spin_unlock_irqrestore(&metro_priv->lock, flags);
221
222         /* Clear the urb pipe. */
223         usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
224
225         /* Start reading from the device */
226         usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
227                           usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
228                            port->interrupt_in_urb->transfer_buffer,
229                            port->interrupt_in_urb->transfer_buffer_length,
230                            metrousb_read_int_callback, port, 1);
231         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
232
233         if (result) {
234                 dev_err(&port->dev,
235                         "%s - failed submitting interrupt in urb, error code=%d\n",
236                         __func__, result);
237                 goto exit;
238         }
239
240         /* Send activate cmd to device */
241         result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
242         if (result) {
243                 dev_err(&port->dev,
244                         "%s - failed to configure device for port number=%d, error code=%d\n",
245                         __func__, port->number, result);
246                 goto exit;
247         }
248
249         dev_dbg(&port->dev, "%s - port open\n", __func__);
250 exit:
251         return result;
252 }
253
254 static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
255 {
256         int retval = 0;
257         unsigned char mcr = METROUSB_MCR_NONE;
258
259         dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
260                 __func__, control_state);
261
262         /* Set the modem control value. */
263         if (control_state & TIOCM_DTR)
264                 mcr |= METROUSB_MCR_DTR;
265         if (control_state & TIOCM_RTS)
266                 mcr |= METROUSB_MCR_RTS;
267
268         /* Send the command to the usb port. */
269         retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
270                                 METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
271                                 control_state, 0, NULL, 0, WDR_TIMEOUT);
272         if (retval < 0)
273                 dev_err(&serial->dev->dev,
274                         "%s - set modem ctrl=0x%x failed, error code=%d\n",
275                         __func__, mcr, retval);
276
277         return retval;
278 }
279
280 static void metrousb_shutdown(struct usb_serial *serial)
281 {
282         int i = 0;
283
284         dev_dbg(&serial->dev->dev, "%s\n", __func__);
285
286         /* Stop reading and writing on all ports. */
287         for (i = 0; i < serial->num_ports; ++i) {
288                 /* Close any open urbs. */
289                 metrousb_cleanup(serial->port[i]);
290
291                 /* Free memory. */
292                 kfree(usb_get_serial_port_data(serial->port[i]));
293                 usb_set_serial_port_data(serial->port[i], NULL);
294
295                 dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
296                         __func__, serial->port[i]->number);
297         }
298 }
299
300 static int metrousb_startup(struct usb_serial *serial)
301 {
302         struct metrousb_private *metro_priv;
303         struct usb_serial_port *port;
304         int i = 0;
305
306         dev_dbg(&serial->dev->dev, "%s\n", __func__);
307
308         /* Loop through the serial ports setting up the private structures.
309          * Currently we only use one port. */
310         for (i = 0; i < serial->num_ports; ++i) {
311                 port = serial->port[i];
312
313                 /* Declare memory. */
314                 metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
315                 if (!metro_priv)
316                         return -ENOMEM;
317
318                 /* Initialize memory. */
319                 spin_lock_init(&metro_priv->lock);
320                 usb_set_serial_port_data(port, metro_priv);
321
322                 dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
323                         __func__, port->number);
324         }
325
326         return 0;
327 }
328
329 static void metrousb_throttle(struct tty_struct *tty)
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
335         dev_dbg(tty->dev, "%s\n", __func__);
336
337         /* Set the private information for the port to stop reading data. */
338         spin_lock_irqsave(&metro_priv->lock, flags);
339         metro_priv->throttled = 1;
340         spin_unlock_irqrestore(&metro_priv->lock, flags);
341 }
342
343 static int metrousb_tiocmget(struct tty_struct *tty)
344 {
345         unsigned long control_state = 0;
346         struct usb_serial_port *port = tty->driver_data;
347         struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
348         unsigned long flags = 0;
349
350         dev_dbg(tty->dev, "%s\n", __func__);
351
352         spin_lock_irqsave(&metro_priv->lock, flags);
353         control_state = metro_priv->control_state;
354         spin_unlock_irqrestore(&metro_priv->lock, flags);
355
356         return control_state;
357 }
358
359 static int metrousb_tiocmset(struct tty_struct *tty,
360                              unsigned int set, unsigned int clear)
361 {
362         struct usb_serial_port *port = tty->driver_data;
363         struct usb_serial *serial = port->serial;
364         struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
365         unsigned long flags = 0;
366         unsigned long control_state = 0;
367
368         dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
369
370         spin_lock_irqsave(&metro_priv->lock, flags);
371         control_state = metro_priv->control_state;
372
373         /* Set the RTS and DTR values. */
374         if (set & TIOCM_RTS)
375                 control_state |= TIOCM_RTS;
376         if (set & TIOCM_DTR)
377                 control_state |= TIOCM_DTR;
378         if (clear & TIOCM_RTS)
379                 control_state &= ~TIOCM_RTS;
380         if (clear & TIOCM_DTR)
381                 control_state &= ~TIOCM_DTR;
382
383         metro_priv->control_state = control_state;
384         spin_unlock_irqrestore(&metro_priv->lock, flags);
385         return metrousb_set_modem_ctrl(serial, control_state);
386 }
387
388 static void metrousb_unthrottle(struct tty_struct *tty)
389 {
390         struct usb_serial_port *port = tty->driver_data;
391         struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
392         unsigned long flags = 0;
393         int result = 0;
394
395         dev_dbg(tty->dev, "%s\n", __func__);
396
397         /* Set the private information for the port to resume reading data. */
398         spin_lock_irqsave(&metro_priv->lock, flags);
399         metro_priv->throttled = 0;
400         spin_unlock_irqrestore(&metro_priv->lock, flags);
401
402         /* Submit the urb to read from the port. */
403         port->interrupt_in_urb->dev = port->serial->dev;
404         result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
405         if (result)
406                 dev_err(tty->dev,
407                         "failed submitting interrupt in urb error code=%d\n",
408                         result);
409 }
410
411 static struct usb_serial_driver metrousb_device = {
412         .driver = {
413                 .owner =        THIS_MODULE,
414                 .name =         "metro-usb",
415         },
416         .description            = "Metrologic USB to Serial",
417         .id_table               = id_table,
418         .num_ports              = 1,
419         .open                   = metrousb_open,
420         .close                  = metrousb_cleanup,
421         .read_int_callback      = metrousb_read_int_callback,
422         .write_int_callback     = metrousb_write_int_callback,
423         .attach                 = metrousb_startup,
424         .release                = metrousb_shutdown,
425         .throttle               = metrousb_throttle,
426         .unthrottle             = metrousb_unthrottle,
427         .tiocmget               = metrousb_tiocmget,
428         .tiocmset               = metrousb_tiocmset,
429 };
430
431 static struct usb_serial_driver * const serial_drivers[] = {
432         &metrousb_device,
433         NULL,
434 };
435
436 module_usb_serial_driver(serial_drivers, id_table);
437
438 MODULE_LICENSE("GPL");
439 MODULE_AUTHOR("Philip Nicastro");
440 MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
441 MODULE_DESCRIPTION(DRIVER_DESC);