Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/usb/serial/ssu100.c

  1 /*
  2  * usb-serial driver for Quatech SSU-100
  3  *
  4  * based on ftdi_sio.c and the original serqt_usb.c from Quatech
  5  *
  6  */
  7 
  8 #include <linux/errno.h>
  9 #include <linux/slab.h>
 10 #include <linux/tty.h>
 11 #include <linux/tty_driver.h>
 12 #include <linux/tty_flip.h>
 13 #include <linux/module.h>
 14 #include <linux/serial.h>
 15 #include <linux/usb.h>
 16 #include <linux/usb/serial.h>
 17 #include <linux/serial_reg.h>
 18 #include <linux/uaccess.h>
 19 
 20 #define QT_OPEN_CLOSE_CHANNEL       0xca
 21 #define QT_SET_GET_DEVICE           0xc2
 22 #define QT_SET_GET_REGISTER         0xc0
 23 #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
 24 #define QT_SET_ATF                  0xcd
 25 #define QT_GET_SET_UART             0xc1
 26 #define QT_TRANSFER_IN              0xc0
 27 #define QT_HW_FLOW_CONTROL_MASK     0xc5
 28 #define QT_SW_FLOW_CONTROL_MASK     0xc6
 29 
 30 #define  SERIAL_MSR_MASK            0xf0
 31 
 32 #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
 33 
 34 #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
 35 
 36 #define  MAX_BAUD_RATE              460800
 37 
 38 #define ATC_DISABLED                0x00
 39 #define DUPMODE_BITS        0xc0
 40 #define RR_BITS             0x03
 41 #define LOOPMODE_BITS       0x41
 42 #define RS232_MODE          0x00
 43 #define RTSCTS_TO_CONNECTOR 0x40
 44 #define CLKS_X4             0x02
 45 #define FULLPWRBIT          0x00000080
 46 #define NEXT_BOARD_POWER_BIT        0x00000004
 47 
 48 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
 49 
 50 #define USB_VENDOR_ID_QUATECH   0x061d  /* Quatech VID */
 51 #define QUATECH_SSU100  0xC020  /* SSU100 */
 52 
 53 static const struct usb_device_id id_table[] = {
 54         {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
 55         {}                      /* Terminating entry */
 56 };
 57 MODULE_DEVICE_TABLE(usb, id_table);
 58 
 59 struct ssu100_port_private {
 60         spinlock_t status_lock;
 61         u8 shadowLSR;
 62         u8 shadowMSR;
 63 };
 64 
 65 static inline int ssu100_control_msg(struct usb_device *dev,
 66                                      u8 request, u16 data, u16 index)
 67 {
 68         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
 69                                request, 0x40, data, index,
 70                                NULL, 0, 300);
 71 }
 72 
 73 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
 74 {
 75         u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
 76 
 77         return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
 78 }
 79 
 80 
 81 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
 82 {
 83         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 84                                QT_SET_GET_DEVICE, 0xc0, 0, 0,
 85                                data, 3, 300);
 86 }
 87 
 88 static inline int ssu100_getregister(struct usb_device *dev,
 89                                      unsigned short uart,
 90                                      unsigned short reg,
 91                                      u8 *data)
 92 {
 93         return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 94                                QT_SET_GET_REGISTER, 0xc0, reg,
 95                                uart, data, sizeof(*data), 300);
 96 
 97 }
 98 
 99 
100 static inline int ssu100_setregister(struct usb_device *dev,
101                                      unsigned short uart,
102                                      unsigned short reg,
103                                      u16 data)
104 {
105         u16 value = (data << 8) | reg;
106 
107         return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
108                                QT_SET_GET_REGISTER, 0x40, value, uart,
109                                NULL, 0, 300);
110 
111 }
112 
113 #define set_mctrl(dev, set)             update_mctrl((dev), (set), 0)
114 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
115 
116 /* these do not deal with device that have more than 1 port */
117 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
118                                unsigned int clear)
119 {
120         unsigned urb_value;
121         int result;
122 
123         if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
124                 dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
125                 return 0;       /* no change */
126         }
127 
128         clear &= ~set;  /* 'set' takes precedence over 'clear' */
129         urb_value = 0;
130         if (set & TIOCM_DTR)
131                 urb_value |= UART_MCR_DTR;
132         if (set & TIOCM_RTS)
133                 urb_value |= UART_MCR_RTS;
134 
135         result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
136         if (result < 0)
137                 dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
138 
139         return result;
140 }
141 
142 static int ssu100_initdevice(struct usb_device *dev)
143 {
144         u8 *data;
145         int result = 0;
146 
147         data = kzalloc(3, GFP_KERNEL);
148         if (!data)
149                 return -ENOMEM;
150 
151         result = ssu100_getdevice(dev, data);
152         if (result < 0) {
153                 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
154                 goto out;
155         }
156 
157         data[1] &= ~FULLPWRBIT;
158 
159         result = ssu100_setdevice(dev, data);
160         if (result < 0) {
161                 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
162                 goto out;
163         }
164 
165         result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
166         if (result < 0) {
167                 dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
168                 goto out;
169         }
170 
171         result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
172         if (result < 0) {
173                 dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
174                 goto out;
175         }
176 
177         result = ssu100_getdevice(dev, data);
178         if (result < 0) {
179                 dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
180                 goto out;
181         }
182 
183         data[0] &= ~(RR_BITS | DUPMODE_BITS);
184         data[0] |= CLKS_X4;
185         data[1] &= ~(LOOPMODE_BITS);
186         data[1] |= RS232_MODE;
187 
188         result = ssu100_setdevice(dev, data);
189         if (result < 0) {
190                 dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
191                 goto out;
192         }
193 
194 out:    kfree(data);
195         return result;
196 
197 }
198 
199 
200 static void ssu100_set_termios(struct tty_struct *tty,
201                                struct usb_serial_port *port,
202                                struct ktermios *old_termios)
203 {
204         struct usb_device *dev = port->serial->dev;
205         struct ktermios *termios = &tty->termios;
206         u16 baud, divisor, remainder;
207         unsigned int cflag = termios->c_cflag;
208         u16 urb_value = 0; /* will hold the new flags */
209         int result;
210 
211         if (cflag & PARENB) {
212                 if (cflag & PARODD)
213                         urb_value |= UART_LCR_PARITY;
214                 else
215                         urb_value |= SERIAL_EVEN_PARITY;
216         }
217 
218         switch (cflag & CSIZE) {
219         case CS5:
220                 urb_value |= UART_LCR_WLEN5;
221                 break;
222         case CS6:
223                 urb_value |= UART_LCR_WLEN6;
224                 break;
225         case CS7:
226                 urb_value |= UART_LCR_WLEN7;
227                 break;
228         default:
229         case CS8:
230                 urb_value |= UART_LCR_WLEN8;
231                 break;
232         }
233 
234         baud = tty_get_baud_rate(tty);
235         if (!baud)
236                 baud = 9600;
237 
238         dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
239 
240 
241         divisor = MAX_BAUD_RATE / baud;
242         remainder = MAX_BAUD_RATE % baud;
243         if (((remainder * 2) >= baud) && (baud != 110))
244                 divisor++;
245 
246         urb_value = urb_value << 8;
247 
248         result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
249         if (result < 0)
250                 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
251 
252         if (cflag & CRTSCTS)
253                 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
254                                             SERIAL_CRTSCTS, 0);
255         else
256                 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
257                                             0, 0);
258         if (result < 0)
259                 dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
260 
261         if (I_IXOFF(tty) || I_IXON(tty)) {
262                 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
263 
264                 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
265                                             x, 0);
266         } else
267                 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
268                                             0, 0);
269 
270         if (result < 0)
271                 dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
272 
273 }
274 
275 
276 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
277 {
278         struct usb_device *dev = port->serial->dev;
279         struct ssu100_port_private *priv = usb_get_serial_port_data(port);
280         u8 *data;
281         int result;
282         unsigned long flags;
283 
284         data = kzalloc(2, GFP_KERNEL);
285         if (!data)
286                 return -ENOMEM;
287 
288         result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
289                                  QT_OPEN_CLOSE_CHANNEL,
290                                  QT_TRANSFER_IN, 0x01,
291                                  0, data, 2, 300);
292         if (result < 0) {
293                 dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
294                 kfree(data);
295                 return result;
296         }
297 
298         spin_lock_irqsave(&priv->status_lock, flags);
299         priv->shadowLSR = data[0];
300         priv->shadowMSR = data[1];
301         spin_unlock_irqrestore(&priv->status_lock, flags);
302 
303         kfree(data);
304 
305 /* set to 9600 */
306         result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
307         if (result < 0)
308                 dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
309 
310         if (tty)
311                 ssu100_set_termios(tty, port, &tty->termios);
312 
313         return usb_serial_generic_open(tty, port);
314 }
315 
316 static int get_serial_info(struct usb_serial_port *port,
317                            struct serial_struct __user *retinfo)
318 {
319         struct serial_struct tmp;
320 
321         memset(&tmp, 0, sizeof(tmp));
322         tmp.line                = port->minor;
323         tmp.port                = 0;
324         tmp.irq                 = 0;
325         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
326         tmp.xmit_fifo_size      = port->bulk_out_size;
327         tmp.baud_base           = 9600;
328         tmp.close_delay         = 5*HZ;
329         tmp.closing_wait        = 30*HZ;
330 
331         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
332                 return -EFAULT;
333         return 0;
334 }
335 
336 static int ssu100_ioctl(struct tty_struct *tty,
337                     unsigned int cmd, unsigned long arg)
338 {
339         struct usb_serial_port *port = tty->driver_data;
340 
341         switch (cmd) {
342         case TIOCGSERIAL:
343                 return get_serial_info(port,
344                                        (struct serial_struct __user *) arg);
345         default:
346                 break;
347         }
348 
349         return -ENOIOCTLCMD;
350 }
351 
352 static int ssu100_attach(struct usb_serial *serial)
353 {
354         return ssu100_initdevice(serial->dev);
355 }
356 
357 static int ssu100_port_probe(struct usb_serial_port *port)
358 {
359         struct ssu100_port_private *priv;
360 
361         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
362         if (!priv)
363                 return -ENOMEM;
364 
365         spin_lock_init(&priv->status_lock);
366 
367         usb_set_serial_port_data(port, priv);
368 
369         return 0;
370 }
371 
372 static int ssu100_port_remove(struct usb_serial_port *port)
373 {
374         struct ssu100_port_private *priv;
375 
376         priv = usb_get_serial_port_data(port);
377         kfree(priv);
378 
379         return 0;
380 }
381 
382 static int ssu100_tiocmget(struct tty_struct *tty)
383 {
384         struct usb_serial_port *port = tty->driver_data;
385         struct usb_device *dev = port->serial->dev;
386         u8 *d;
387         int r;
388 
389         d = kzalloc(2, GFP_KERNEL);
390         if (!d)
391                 return -ENOMEM;
392 
393         r = ssu100_getregister(dev, 0, UART_MCR, d);
394         if (r < 0)
395                 goto mget_out;
396 
397         r = ssu100_getregister(dev, 0, UART_MSR, d+1);
398         if (r < 0)
399                 goto mget_out;
400 
401         r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
402                 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
403                 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
404                 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
405                 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
406                 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
407 
408 mget_out:
409         kfree(d);
410         return r;
411 }
412 
413 static int ssu100_tiocmset(struct tty_struct *tty,
414                            unsigned int set, unsigned int clear)
415 {
416         struct usb_serial_port *port = tty->driver_data;
417         struct usb_device *dev = port->serial->dev;
418 
419         return update_mctrl(dev, set, clear);
420 }
421 
422 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
423 {
424         struct usb_device *dev = port->serial->dev;
425 
426         /* Disable flow control */
427         if (!on) {
428                 if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
429                         dev_err(&port->dev, "error from flowcontrol urb\n");
430         }
431         /* drop RTS and DTR */
432         if (on)
433                 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
434         else
435                 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
436 }
437 
438 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
439 {
440         struct ssu100_port_private *priv = usb_get_serial_port_data(port);
441         unsigned long flags;
442 
443         spin_lock_irqsave(&priv->status_lock, flags);
444         priv->shadowMSR = msr;
445         spin_unlock_irqrestore(&priv->status_lock, flags);
446 
447         if (msr & UART_MSR_ANY_DELTA) {
448                 /* update input line counters */
449                 if (msr & UART_MSR_DCTS)
450                         port->icount.cts++;
451                 if (msr & UART_MSR_DDSR)
452                         port->icount.dsr++;
453                 if (msr & UART_MSR_DDCD)
454                         port->icount.dcd++;
455                 if (msr & UART_MSR_TERI)
456                         port->icount.rng++;
457                 wake_up_interruptible(&port->port.delta_msr_wait);
458         }
459 }
460 
461 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
462                               char *tty_flag)
463 {
464         struct ssu100_port_private *priv = usb_get_serial_port_data(port);
465         unsigned long flags;
466 
467         spin_lock_irqsave(&priv->status_lock, flags);
468         priv->shadowLSR = lsr;
469         spin_unlock_irqrestore(&priv->status_lock, flags);
470 
471         *tty_flag = TTY_NORMAL;
472         if (lsr & UART_LSR_BRK_ERROR_BITS) {
473                 /* we always want to update icount, but we only want to
474                  * update tty_flag for one case */
475                 if (lsr & UART_LSR_BI) {
476                         port->icount.brk++;
477                         *tty_flag = TTY_BREAK;
478                         usb_serial_handle_break(port);
479                 }
480                 if (lsr & UART_LSR_PE) {
481                         port->icount.parity++;
482                         if (*tty_flag == TTY_NORMAL)
483                                 *tty_flag = TTY_PARITY;
484                 }
485                 if (lsr & UART_LSR_FE) {
486                         port->icount.frame++;
487                         if (*tty_flag == TTY_NORMAL)
488                                 *tty_flag = TTY_FRAME;
489                 }
490                 if (lsr & UART_LSR_OE) {
491                         port->icount.overrun++;
492                         tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
493                 }
494         }
495 
496 }
497 
498 static void ssu100_process_read_urb(struct urb *urb)
499 {
500         struct usb_serial_port *port = urb->context;
501         char *packet = (char *)urb->transfer_buffer;
502         char flag = TTY_NORMAL;
503         u32 len = urb->actual_length;
504         int i;
505         char *ch;
506 
507         if ((len >= 4) &&
508             (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
509             ((packet[2] == 0x00) || (packet[2] == 0x01))) {
510                 if (packet[2] == 0x00)
511                         ssu100_update_lsr(port, packet[3], &flag);
512                 if (packet[2] == 0x01)
513                         ssu100_update_msr(port, packet[3]);
514 
515                 len -= 4;
516                 ch = packet + 4;
517         } else
518                 ch = packet;
519 
520         if (!len)
521                 return; /* status only */
522 
523         if (port->port.console && port->sysrq) {
524                 for (i = 0; i < len; i++, ch++) {
525                         if (!usb_serial_handle_sysrq_char(port, *ch))
526                                 tty_insert_flip_char(&port->port, *ch, flag);
527                 }
528         } else
529                 tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
530 
531         tty_flip_buffer_push(&port->port);
532 }
533 
534 static struct usb_serial_driver ssu100_device = {
535         .driver = {
536                 .owner = THIS_MODULE,
537                 .name = "ssu100",
538         },
539         .description         = DRIVER_DESC,
540         .id_table            = id_table,
541         .num_ports           = 1,
542         .open                = ssu100_open,
543         .attach              = ssu100_attach,
544         .port_probe          = ssu100_port_probe,
545         .port_remove         = ssu100_port_remove,
546         .dtr_rts             = ssu100_dtr_rts,
547         .process_read_urb    = ssu100_process_read_urb,
548         .tiocmget            = ssu100_tiocmget,
549         .tiocmset            = ssu100_tiocmset,
550         .tiocmiwait          = usb_serial_generic_tiocmiwait,
551         .get_icount          = usb_serial_generic_get_icount,
552         .ioctl               = ssu100_ioctl,
553         .set_termios         = ssu100_set_termios,
554 };
555 
556 static struct usb_serial_driver * const serial_drivers[] = {
557         &ssu100_device, NULL
558 };
559 
560 module_usb_serial_driver(serial_drivers, id_table);
561 
562 MODULE_DESCRIPTION(DRIVER_DESC);
563 MODULE_LICENSE("GPL");
564 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us