Commit | Line | Data |
---|---|---|
52af9545 BP |
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/init.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/tty.h> | |
12 | #include <linux/tty_driver.h> | |
13 | #include <linux/tty_flip.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/serial.h> | |
16 | #include <linux/usb.h> | |
17 | #include <linux/usb/serial.h> | |
79f203a2 | 18 | #include <linux/serial_reg.h> |
52af9545 BP |
19 | #include <linux/uaccess.h> |
20 | ||
21 | #define QT_OPEN_CLOSE_CHANNEL 0xca | |
22 | #define QT_SET_GET_DEVICE 0xc2 | |
23 | #define QT_SET_GET_REGISTER 0xc0 | |
24 | #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc | |
25 | #define QT_SET_ATF 0xcd | |
26 | #define QT_GET_SET_UART 0xc1 | |
27 | #define QT_TRANSFER_IN 0xc0 | |
28 | #define QT_HW_FLOW_CONTROL_MASK 0xc5 | |
29 | #define QT_SW_FLOW_CONTROL_MASK 0xc6 | |
30 | ||
52af9545 BP |
31 | #define SERIAL_MSR_MASK 0xf0 |
32 | ||
79f203a2 | 33 | #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) |
52af9545 | 34 | |
79f203a2 | 35 | #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) |
52af9545 BP |
36 | |
37 | #define MAX_BAUD_RATE 460800 | |
38 | ||
39 | #define ATC_DISABLED 0x00 | |
40 | #define DUPMODE_BITS 0xc0 | |
41 | #define RR_BITS 0x03 | |
42 | #define LOOPMODE_BITS 0x41 | |
43 | #define RS232_MODE 0x00 | |
44 | #define RTSCTS_TO_CONNECTOR 0x40 | |
45 | #define CLKS_X4 0x02 | |
46 | #define FULLPWRBIT 0x00000080 | |
47 | #define NEXT_BOARD_POWER_BIT 0x00000004 | |
48 | ||
90ab5ee9 | 49 | static bool debug; |
52af9545 BP |
50 | |
51 | /* Version Information */ | |
52 | #define DRIVER_VERSION "v0.1" | |
53 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" | |
54 | ||
55 | #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ | |
56 | #define QUATECH_SSU100 0xC020 /* SSU100 */ | |
57 | ||
58 | static const struct usb_device_id id_table[] = { | |
59 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, | |
60 | {} /* Terminating entry */ | |
61 | }; | |
62 | ||
63 | MODULE_DEVICE_TABLE(usb, id_table); | |
64 | ||
65 | ||
66 | static struct usb_driver ssu100_driver = { | |
67 | .name = "ssu100", | |
68 | .probe = usb_serial_probe, | |
69 | .disconnect = usb_serial_disconnect, | |
70 | .id_table = id_table, | |
71 | .suspend = usb_serial_suspend, | |
72 | .resume = usb_serial_resume, | |
52af9545 BP |
73 | .supports_autosuspend = 1, |
74 | }; | |
75 | ||
76 | struct ssu100_port_private { | |
17523058 | 77 | spinlock_t status_lock; |
52af9545 BP |
78 | u8 shadowLSR; |
79 | u8 shadowMSR; | |
80 | wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ | |
f81c83db | 81 | struct async_icount icount; |
52af9545 BP |
82 | }; |
83 | ||
84 | static void ssu100_release(struct usb_serial *serial) | |
85 | { | |
86 | struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); | |
87 | ||
88 | dbg("%s", __func__); | |
89 | kfree(priv); | |
90 | } | |
91 | ||
92 | static inline int ssu100_control_msg(struct usb_device *dev, | |
93 | u8 request, u16 data, u16 index) | |
94 | { | |
95 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
96 | request, 0x40, data, index, | |
97 | NULL, 0, 300); | |
98 | } | |
99 | ||
100 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) | |
101 | { | |
102 | u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); | |
103 | ||
104 | return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); | |
105 | } | |
106 | ||
107 | ||
108 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) | |
109 | { | |
110 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
111 | QT_SET_GET_DEVICE, 0xc0, 0, 0, | |
112 | data, 3, 300); | |
113 | } | |
114 | ||
115 | static inline int ssu100_getregister(struct usb_device *dev, | |
116 | unsigned short uart, | |
117 | unsigned short reg, | |
118 | u8 *data) | |
119 | { | |
120 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
121 | QT_SET_GET_REGISTER, 0xc0, reg, | |
122 | uart, data, sizeof(*data), 300); | |
123 | ||
124 | } | |
125 | ||
126 | ||
127 | static inline int ssu100_setregister(struct usb_device *dev, | |
128 | unsigned short uart, | |
556f1a0e | 129 | unsigned short reg, |
52af9545 BP |
130 | u16 data) |
131 | { | |
556f1a0e | 132 | u16 value = (data << 8) | reg; |
52af9545 BP |
133 | |
134 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
135 | QT_SET_GET_REGISTER, 0x40, value, uart, | |
136 | NULL, 0, 300); | |
137 | ||
138 | } | |
139 | ||
140 | #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) | |
141 | #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) | |
142 | ||
143 | /* these do not deal with device that have more than 1 port */ | |
144 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, | |
145 | unsigned int clear) | |
146 | { | |
147 | unsigned urb_value; | |
148 | int result; | |
149 | ||
150 | if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { | |
151 | dbg("%s - DTR|RTS not being set|cleared", __func__); | |
152 | return 0; /* no change */ | |
153 | } | |
154 | ||
155 | clear &= ~set; /* 'set' takes precedence over 'clear' */ | |
156 | urb_value = 0; | |
157 | if (set & TIOCM_DTR) | |
79f203a2 | 158 | urb_value |= UART_MCR_DTR; |
52af9545 | 159 | if (set & TIOCM_RTS) |
79f203a2 | 160 | urb_value |= UART_MCR_RTS; |
52af9545 | 161 | |
556f1a0e | 162 | result = ssu100_setregister(dev, 0, UART_MCR, urb_value); |
52af9545 BP |
163 | if (result < 0) |
164 | dbg("%s Error from MODEM_CTRL urb", __func__); | |
165 | ||
166 | return result; | |
167 | } | |
168 | ||
169 | static int ssu100_initdevice(struct usb_device *dev) | |
170 | { | |
171 | u8 *data; | |
172 | int result = 0; | |
173 | ||
174 | dbg("%s", __func__); | |
175 | ||
176 | data = kzalloc(3, GFP_KERNEL); | |
177 | if (!data) | |
178 | return -ENOMEM; | |
179 | ||
180 | result = ssu100_getdevice(dev, data); | |
181 | if (result < 0) { | |
182 | dbg("%s - get_device failed %i", __func__, result); | |
183 | goto out; | |
184 | } | |
185 | ||
186 | data[1] &= ~FULLPWRBIT; | |
187 | ||
188 | result = ssu100_setdevice(dev, data); | |
189 | if (result < 0) { | |
190 | dbg("%s - setdevice failed %i", __func__, result); | |
191 | goto out; | |
192 | } | |
193 | ||
194 | result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); | |
195 | if (result < 0) { | |
196 | dbg("%s - set prebuffer level failed %i", __func__, result); | |
197 | goto out; | |
198 | } | |
199 | ||
200 | result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); | |
201 | if (result < 0) { | |
202 | dbg("%s - set ATFprebuffer level failed %i", __func__, result); | |
203 | goto out; | |
204 | } | |
205 | ||
206 | result = ssu100_getdevice(dev, data); | |
207 | if (result < 0) { | |
208 | dbg("%s - get_device failed %i", __func__, result); | |
209 | goto out; | |
210 | } | |
211 | ||
212 | data[0] &= ~(RR_BITS | DUPMODE_BITS); | |
213 | data[0] |= CLKS_X4; | |
214 | data[1] &= ~(LOOPMODE_BITS); | |
215 | data[1] |= RS232_MODE; | |
216 | ||
217 | result = ssu100_setdevice(dev, data); | |
218 | if (result < 0) { | |
219 | dbg("%s - setdevice failed %i", __func__, result); | |
220 | goto out; | |
221 | } | |
222 | ||
223 | out: kfree(data); | |
224 | return result; | |
225 | ||
226 | } | |
227 | ||
228 | ||
229 | static void ssu100_set_termios(struct tty_struct *tty, | |
230 | struct usb_serial_port *port, | |
231 | struct ktermios *old_termios) | |
232 | { | |
233 | struct usb_device *dev = port->serial->dev; | |
234 | struct ktermios *termios = tty->termios; | |
235 | u16 baud, divisor, remainder; | |
236 | unsigned int cflag = termios->c_cflag; | |
237 | u16 urb_value = 0; /* will hold the new flags */ | |
238 | int result; | |
239 | ||
240 | dbg("%s", __func__); | |
241 | ||
242 | if (cflag & PARENB) { | |
243 | if (cflag & PARODD) | |
79f203a2 | 244 | urb_value |= UART_LCR_PARITY; |
52af9545 BP |
245 | else |
246 | urb_value |= SERIAL_EVEN_PARITY; | |
247 | } | |
248 | ||
249 | switch (cflag & CSIZE) { | |
250 | case CS5: | |
79f203a2 | 251 | urb_value |= UART_LCR_WLEN5; |
52af9545 BP |
252 | break; |
253 | case CS6: | |
79f203a2 | 254 | urb_value |= UART_LCR_WLEN6; |
52af9545 BP |
255 | break; |
256 | case CS7: | |
79f203a2 | 257 | urb_value |= UART_LCR_WLEN7; |
52af9545 BP |
258 | break; |
259 | default: | |
260 | case CS8: | |
79f203a2 | 261 | urb_value |= UART_LCR_WLEN8; |
52af9545 BP |
262 | break; |
263 | } | |
264 | ||
265 | baud = tty_get_baud_rate(tty); | |
266 | if (!baud) | |
267 | baud = 9600; | |
268 | ||
269 | dbg("%s - got baud = %d\n", __func__, baud); | |
270 | ||
271 | ||
272 | divisor = MAX_BAUD_RATE / baud; | |
273 | remainder = MAX_BAUD_RATE % baud; | |
274 | if (((remainder * 2) >= baud) && (baud != 110)) | |
275 | divisor++; | |
276 | ||
277 | urb_value = urb_value << 8; | |
278 | ||
279 | result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); | |
280 | if (result < 0) | |
281 | dbg("%s - set uart failed", __func__); | |
282 | ||
283 | if (cflag & CRTSCTS) | |
284 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | |
285 | SERIAL_CRTSCTS, 0); | |
286 | else | |
287 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | |
288 | 0, 0); | |
289 | if (result < 0) | |
290 | dbg("%s - set HW flow control failed", __func__); | |
291 | ||
292 | if (I_IXOFF(tty) || I_IXON(tty)) { | |
293 | u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); | |
294 | ||
295 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | |
296 | x, 0); | |
297 | } else | |
298 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | |
299 | 0, 0); | |
300 | ||
301 | if (result < 0) | |
302 | dbg("%s - set SW flow control failed", __func__); | |
303 | ||
304 | } | |
305 | ||
306 | ||
307 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) | |
308 | { | |
309 | struct usb_device *dev = port->serial->dev; | |
310 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | |
311 | u8 *data; | |
312 | int result; | |
17523058 | 313 | unsigned long flags; |
52af9545 BP |
314 | |
315 | dbg("%s - port %d", __func__, port->number); | |
316 | ||
317 | data = kzalloc(2, GFP_KERNEL); | |
318 | if (!data) | |
319 | return -ENOMEM; | |
320 | ||
321 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
322 | QT_OPEN_CLOSE_CHANNEL, | |
323 | QT_TRANSFER_IN, 0x01, | |
324 | 0, data, 2, 300); | |
325 | if (result < 0) { | |
326 | dbg("%s - open failed %i", __func__, result); | |
327 | kfree(data); | |
328 | return result; | |
329 | } | |
330 | ||
17523058 | 331 | spin_lock_irqsave(&priv->status_lock, flags); |
f81c83db BP |
332 | priv->shadowLSR = data[0]; |
333 | priv->shadowMSR = data[1]; | |
17523058 | 334 | spin_unlock_irqrestore(&priv->status_lock, flags); |
52af9545 BP |
335 | |
336 | kfree(data); | |
337 | ||
338 | /* set to 9600 */ | |
339 | result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); | |
340 | if (result < 0) | |
341 | dbg("%s - set uart failed", __func__); | |
342 | ||
343 | if (tty) | |
344 | ssu100_set_termios(tty, port, tty->termios); | |
345 | ||
346 | return usb_serial_generic_open(tty, port); | |
347 | } | |
348 | ||
349 | static void ssu100_close(struct usb_serial_port *port) | |
350 | { | |
351 | dbg("%s", __func__); | |
352 | usb_serial_generic_close(port); | |
353 | } | |
354 | ||
355 | static int get_serial_info(struct usb_serial_port *port, | |
356 | struct serial_struct __user *retinfo) | |
357 | { | |
358 | struct serial_struct tmp; | |
359 | ||
360 | if (!retinfo) | |
361 | return -EFAULT; | |
362 | ||
363 | memset(&tmp, 0, sizeof(tmp)); | |
364 | tmp.line = port->serial->minor; | |
365 | tmp.port = 0; | |
366 | tmp.irq = 0; | |
367 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | |
368 | tmp.xmit_fifo_size = port->bulk_out_size; | |
369 | tmp.baud_base = 9600; | |
370 | tmp.close_delay = 5*HZ; | |
371 | tmp.closing_wait = 30*HZ; | |
372 | ||
373 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) | |
374 | return -EFAULT; | |
375 | return 0; | |
376 | } | |
377 | ||
f81c83db BP |
378 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) |
379 | { | |
380 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | |
381 | struct async_icount prev, cur; | |
382 | unsigned long flags; | |
383 | ||
384 | spin_lock_irqsave(&priv->status_lock, flags); | |
385 | prev = priv->icount; | |
386 | spin_unlock_irqrestore(&priv->status_lock, flags); | |
387 | ||
388 | while (1) { | |
389 | wait_event_interruptible(priv->delta_msr_wait, | |
390 | ((priv->icount.rng != prev.rng) || | |
391 | (priv->icount.dsr != prev.dsr) || | |
392 | (priv->icount.dcd != prev.dcd) || | |
393 | (priv->icount.cts != prev.cts))); | |
394 | ||
395 | if (signal_pending(current)) | |
396 | return -ERESTARTSYS; | |
397 | ||
398 | spin_lock_irqsave(&priv->status_lock, flags); | |
399 | cur = priv->icount; | |
400 | spin_unlock_irqrestore(&priv->status_lock, flags); | |
401 | ||
402 | if ((prev.rng == cur.rng) && | |
403 | (prev.dsr == cur.dsr) && | |
404 | (prev.dcd == cur.dcd) && | |
405 | (prev.cts == cur.cts)) | |
406 | return -EIO; | |
407 | ||
408 | if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || | |
409 | (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || | |
410 | (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || | |
411 | (arg & TIOCM_CTS && (prev.cts != cur.cts))) | |
412 | return 0; | |
413 | } | |
414 | return 0; | |
415 | } | |
416 | ||
0bca1b91 AC |
417 | static int ssu100_get_icount(struct tty_struct *tty, |
418 | struct serial_icounter_struct *icount) | |
419 | { | |
420 | struct usb_serial_port *port = tty->driver_data; | |
421 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | |
422 | struct async_icount cnow = priv->icount; | |
423 | ||
424 | icount->cts = cnow.cts; | |
425 | icount->dsr = cnow.dsr; | |
426 | icount->rng = cnow.rng; | |
427 | icount->dcd = cnow.dcd; | |
428 | icount->rx = cnow.rx; | |
429 | icount->tx = cnow.tx; | |
430 | icount->frame = cnow.frame; | |
431 | icount->overrun = cnow.overrun; | |
432 | icount->parity = cnow.parity; | |
433 | icount->brk = cnow.brk; | |
434 | icount->buf_overrun = cnow.buf_overrun; | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
439 | ||
440 | ||
00a0d0d6 | 441 | static int ssu100_ioctl(struct tty_struct *tty, |
52af9545 BP |
442 | unsigned int cmd, unsigned long arg) |
443 | { | |
444 | struct usb_serial_port *port = tty->driver_data; | |
52af9545 BP |
445 | |
446 | dbg("%s cmd 0x%04x", __func__, cmd); | |
447 | ||
448 | switch (cmd) { | |
449 | case TIOCGSERIAL: | |
450 | return get_serial_info(port, | |
451 | (struct serial_struct __user *) arg); | |
452 | ||
453 | case TIOCMIWAIT: | |
f81c83db BP |
454 | return wait_modem_info(port, arg); |
455 | ||
52af9545 BP |
456 | default: |
457 | break; | |
458 | } | |
459 | ||
460 | dbg("%s arg not supported", __func__); | |
461 | ||
462 | return -ENOIOCTLCMD; | |
463 | } | |
464 | ||
52af9545 BP |
465 | static int ssu100_attach(struct usb_serial *serial) |
466 | { | |
467 | struct ssu100_port_private *priv; | |
468 | struct usb_serial_port *port = *serial->port; | |
469 | ||
470 | dbg("%s", __func__); | |
471 | ||
472 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
473 | if (!priv) { | |
474 | dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, | |
475 | sizeof(*priv)); | |
476 | return -ENOMEM; | |
477 | } | |
478 | ||
17523058 | 479 | spin_lock_init(&priv->status_lock); |
52af9545 BP |
480 | init_waitqueue_head(&priv->delta_msr_wait); |
481 | usb_set_serial_port_data(port, priv); | |
52af9545 BP |
482 | |
483 | return ssu100_initdevice(serial->dev); | |
484 | } | |
485 | ||
60b33c13 | 486 | static int ssu100_tiocmget(struct tty_struct *tty) |
52af9545 BP |
487 | { |
488 | struct usb_serial_port *port = tty->driver_data; | |
489 | struct usb_device *dev = port->serial->dev; | |
490 | u8 *d; | |
491 | int r; | |
492 | ||
493 | dbg("%s\n", __func__); | |
494 | ||
495 | d = kzalloc(2, GFP_KERNEL); | |
496 | if (!d) | |
497 | return -ENOMEM; | |
498 | ||
79f203a2 | 499 | r = ssu100_getregister(dev, 0, UART_MCR, d); |
52af9545 BP |
500 | if (r < 0) |
501 | goto mget_out; | |
502 | ||
79f203a2 | 503 | r = ssu100_getregister(dev, 0, UART_MSR, d+1); |
52af9545 BP |
504 | if (r < 0) |
505 | goto mget_out; | |
506 | ||
79f203a2 BP |
507 | r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | |
508 | (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | | |
509 | (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | | |
510 | (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | | |
511 | (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | | |
512 | (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); | |
52af9545 BP |
513 | |
514 | mget_out: | |
515 | kfree(d); | |
516 | return r; | |
517 | } | |
518 | ||
20b9d177 | 519 | static int ssu100_tiocmset(struct tty_struct *tty, |
52af9545 BP |
520 | unsigned int set, unsigned int clear) |
521 | { | |
522 | struct usb_serial_port *port = tty->driver_data; | |
523 | struct usb_device *dev = port->serial->dev; | |
524 | ||
525 | dbg("%s\n", __func__); | |
526 | return update_mctrl(dev, set, clear); | |
527 | } | |
528 | ||
529 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) | |
530 | { | |
531 | struct usb_device *dev = port->serial->dev; | |
532 | ||
533 | dbg("%s\n", __func__); | |
534 | ||
535 | mutex_lock(&port->serial->disc_mutex); | |
536 | if (!port->serial->disconnected) { | |
537 | /* Disable flow control */ | |
538 | if (!on && | |
556f1a0e | 539 | ssu100_setregister(dev, 0, UART_MCR, 0) < 0) |
52af9545 BP |
540 | dev_err(&port->dev, "error from flowcontrol urb\n"); |
541 | /* drop RTS and DTR */ | |
542 | if (on) | |
543 | set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | |
544 | else | |
545 | clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | |
546 | } | |
547 | mutex_unlock(&port->serial->disc_mutex); | |
548 | } | |
549 | ||
f81c83db BP |
550 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) |
551 | { | |
552 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | |
553 | unsigned long flags; | |
554 | ||
555 | spin_lock_irqsave(&priv->status_lock, flags); | |
556 | priv->shadowMSR = msr; | |
557 | spin_unlock_irqrestore(&priv->status_lock, flags); | |
558 | ||
559 | if (msr & UART_MSR_ANY_DELTA) { | |
560 | /* update input line counters */ | |
561 | if (msr & UART_MSR_DCTS) | |
562 | priv->icount.cts++; | |
563 | if (msr & UART_MSR_DDSR) | |
564 | priv->icount.dsr++; | |
565 | if (msr & UART_MSR_DDCD) | |
566 | priv->icount.dcd++; | |
567 | if (msr & UART_MSR_TERI) | |
568 | priv->icount.rng++; | |
569 | wake_up_interruptible(&priv->delta_msr_wait); | |
570 | } | |
571 | } | |
572 | ||
6b8f1ca5 BP |
573 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, |
574 | char *tty_flag) | |
f81c83db BP |
575 | { |
576 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); | |
577 | unsigned long flags; | |
578 | ||
579 | spin_lock_irqsave(&priv->status_lock, flags); | |
580 | priv->shadowLSR = lsr; | |
581 | spin_unlock_irqrestore(&priv->status_lock, flags); | |
582 | ||
6b8f1ca5 | 583 | *tty_flag = TTY_NORMAL; |
f81c83db | 584 | if (lsr & UART_LSR_BRK_ERROR_BITS) { |
6b8f1ca5 BP |
585 | /* we always want to update icount, but we only want to |
586 | * update tty_flag for one case */ | |
587 | if (lsr & UART_LSR_BI) { | |
f81c83db | 588 | priv->icount.brk++; |
6b8f1ca5 BP |
589 | *tty_flag = TTY_BREAK; |
590 | usb_serial_handle_break(port); | |
591 | } | |
592 | if (lsr & UART_LSR_PE) { | |
f81c83db | 593 | priv->icount.parity++; |
6b8f1ca5 BP |
594 | if (*tty_flag == TTY_NORMAL) |
595 | *tty_flag = TTY_PARITY; | |
596 | } | |
597 | if (lsr & UART_LSR_FE) { | |
598 | priv->icount.frame++; | |
599 | if (*tty_flag == TTY_NORMAL) | |
600 | *tty_flag = TTY_FRAME; | |
601 | } | |
602 | if (lsr & UART_LSR_OE){ | |
f81c83db | 603 | priv->icount.overrun++; |
6b8f1ca5 BP |
604 | if (*tty_flag == TTY_NORMAL) |
605 | *tty_flag = TTY_OVERRUN; | |
606 | } | |
f81c83db | 607 | } |
6b8f1ca5 | 608 | |
f81c83db BP |
609 | } |
610 | ||
f7043ecb BP |
611 | static int ssu100_process_packet(struct urb *urb, |
612 | struct tty_struct *tty) | |
52af9545 | 613 | { |
f7043ecb BP |
614 | struct usb_serial_port *port = urb->context; |
615 | char *packet = (char *)urb->transfer_buffer; | |
6b8f1ca5 | 616 | char flag = TTY_NORMAL; |
f7043ecb BP |
617 | u32 len = urb->actual_length; |
618 | int i; | |
52af9545 BP |
619 | char *ch; |
620 | ||
621 | dbg("%s - port %d", __func__, port->number); | |
622 | ||
9b2cef31 BP |
623 | if ((len >= 4) && |
624 | (packet[0] == 0x1b) && (packet[1] == 0x1b) && | |
52af9545 | 625 | ((packet[2] == 0x00) || (packet[2] == 0x01))) { |
6b8f1ca5 BP |
626 | if (packet[2] == 0x00) { |
627 | ssu100_update_lsr(port, packet[3], &flag); | |
628 | if (flag == TTY_OVERRUN) | |
629 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | |
630 | } | |
f81c83db BP |
631 | if (packet[2] == 0x01) |
632 | ssu100_update_msr(port, packet[3]); | |
52af9545 BP |
633 | |
634 | len -= 4; | |
635 | ch = packet + 4; | |
636 | } else | |
637 | ch = packet; | |
638 | ||
639 | if (!len) | |
640 | return 0; /* status only */ | |
641 | ||
642 | if (port->port.console && port->sysrq) { | |
643 | for (i = 0; i < len; i++, ch++) { | |
6ee9f4b4 | 644 | if (!usb_serial_handle_sysrq_char(port, *ch)) |
52af9545 BP |
645 | tty_insert_flip_char(tty, *ch, flag); |
646 | } | |
647 | } else | |
648 | tty_insert_flip_string_fixed_flag(tty, ch, flag, len); | |
649 | ||
650 | return len; | |
651 | } | |
652 | ||
653 | static void ssu100_process_read_urb(struct urb *urb) | |
654 | { | |
655 | struct usb_serial_port *port = urb->context; | |
52af9545 | 656 | struct tty_struct *tty; |
f7043ecb | 657 | int count; |
52af9545 BP |
658 | |
659 | dbg("%s", __func__); | |
660 | ||
661 | tty = tty_port_tty_get(&port->port); | |
662 | if (!tty) | |
663 | return; | |
664 | ||
f7043ecb | 665 | count = ssu100_process_packet(urb, tty); |
52af9545 BP |
666 | |
667 | if (count) | |
668 | tty_flip_buffer_push(tty); | |
669 | tty_kref_put(tty); | |
670 | } | |
671 | ||
52af9545 BP |
672 | static struct usb_serial_driver ssu100_device = { |
673 | .driver = { | |
674 | .owner = THIS_MODULE, | |
675 | .name = "ssu100", | |
676 | }, | |
677 | .description = DRIVER_DESC, | |
678 | .id_table = id_table, | |
52af9545 | 679 | .num_ports = 1, |
52af9545 BP |
680 | .open = ssu100_open, |
681 | .close = ssu100_close, | |
682 | .attach = ssu100_attach, | |
683 | .release = ssu100_release, | |
684 | .dtr_rts = ssu100_dtr_rts, | |
685 | .process_read_urb = ssu100_process_read_urb, | |
686 | .tiocmget = ssu100_tiocmget, | |
687 | .tiocmset = ssu100_tiocmset, | |
0bca1b91 | 688 | .get_icount = ssu100_get_icount, |
52af9545 BP |
689 | .ioctl = ssu100_ioctl, |
690 | .set_termios = ssu100_set_termios, | |
85dee135 | 691 | .disconnect = usb_serial_generic_disconnect, |
52af9545 BP |
692 | }; |
693 | ||
d860322f AS |
694 | static struct usb_serial_driver * const serial_drivers[] = { |
695 | &ssu100_device, NULL | |
696 | }; | |
697 | ||
3a674607 | 698 | module_usb_serial_driver(ssu100_driver, serial_drivers); |
52af9545 BP |
699 | |
700 | MODULE_DESCRIPTION(DRIVER_DESC); | |
701 | MODULE_LICENSE("GPL"); | |
702 | ||
703 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
704 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |