Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Digi AccelePort USB-4 and USB-2 Serial Converters | |
3 | * | |
4 | * Copyright 2000 by Digi International | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's | |
12 | * usb-serial driver. | |
13 | * | |
14 | * Peter Berger (pberger@brimson.com) | |
15 | * Al Borchers (borchers@steinerpoint.com) | |
1da177e4 LT |
16 | */ |
17 | ||
1da177e4 LT |
18 | #include <linux/kernel.h> |
19 | #include <linux/errno.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/tty.h> | |
23 | #include <linux/tty_driver.h> | |
24 | #include <linux/tty_flip.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/spinlock.h> | |
27 | #include <linux/workqueue.h> | |
41ad427d | 28 | #include <linux/uaccess.h> |
1da177e4 LT |
29 | #include <linux/usb.h> |
30 | #include <linux/wait.h> | |
a969888c | 31 | #include <linux/usb/serial.h> |
1da177e4 LT |
32 | |
33 | /* Defines */ | |
34 | ||
35 | /* | |
36 | * Version Information | |
37 | */ | |
38 | #define DRIVER_VERSION "v1.80.1.2" | |
39 | #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>" | |
40 | #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver" | |
41 | ||
42 | /* port output buffer length -- must be <= transfer buffer length - 2 */ | |
43 | /* so we can be sure to send the full buffer in one urb */ | |
44 | #define DIGI_OUT_BUF_SIZE 8 | |
45 | ||
46 | /* port input buffer length -- must be >= transfer buffer length - 3 */ | |
47 | /* so we can be sure to hold at least one full buffer from one urb */ | |
48 | #define DIGI_IN_BUF_SIZE 64 | |
49 | ||
50 | /* retry timeout while sleeping */ | |
51 | #define DIGI_RETRY_TIMEOUT (HZ/10) | |
52 | ||
53 | /* timeout while waiting for tty output to drain in close */ | |
54 | /* this delay is used twice in close, so the total delay could */ | |
55 | /* be twice this value */ | |
56 | #define DIGI_CLOSE_TIMEOUT (5*HZ) | |
57 | ||
58 | ||
59 | /* AccelePort USB Defines */ | |
60 | ||
61 | /* ids */ | |
62 | #define DIGI_VENDOR_ID 0x05c5 | |
63 | #define DIGI_2_ID 0x0002 /* USB-2 */ | |
64 | #define DIGI_4_ID 0x0004 /* USB-4 */ | |
65 | ||
66 | /* commands | |
67 | * "INB": can be used on the in-band endpoint | |
68 | * "OOB": can be used on the out-of-band endpoint | |
69 | */ | |
70 | #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */ | |
71 | #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */ | |
72 | #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */ | |
73 | #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */ | |
74 | #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */ | |
75 | #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */ | |
76 | #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */ | |
77 | #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */ | |
78 | #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */ | |
79 | #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */ | |
80 | #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */ | |
81 | #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */ | |
82 | #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */ | |
83 | #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */ | |
84 | #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */ | |
85 | #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */ | |
86 | #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */ | |
87 | #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */ | |
88 | #define DIGI_CMD_SEND_DATA 18 /* INB */ | |
89 | #define DIGI_CMD_RECEIVE_DATA 19 /* INB */ | |
90 | #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */ | |
91 | #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */ | |
92 | ||
93 | /* baud rates */ | |
94 | #define DIGI_BAUD_50 0 | |
95 | #define DIGI_BAUD_75 1 | |
96 | #define DIGI_BAUD_110 2 | |
97 | #define DIGI_BAUD_150 3 | |
98 | #define DIGI_BAUD_200 4 | |
99 | #define DIGI_BAUD_300 5 | |
100 | #define DIGI_BAUD_600 6 | |
101 | #define DIGI_BAUD_1200 7 | |
102 | #define DIGI_BAUD_1800 8 | |
103 | #define DIGI_BAUD_2400 9 | |
104 | #define DIGI_BAUD_4800 10 | |
105 | #define DIGI_BAUD_7200 11 | |
106 | #define DIGI_BAUD_9600 12 | |
107 | #define DIGI_BAUD_14400 13 | |
108 | #define DIGI_BAUD_19200 14 | |
109 | #define DIGI_BAUD_28800 15 | |
110 | #define DIGI_BAUD_38400 16 | |
111 | #define DIGI_BAUD_57600 17 | |
112 | #define DIGI_BAUD_76800 18 | |
113 | #define DIGI_BAUD_115200 19 | |
114 | #define DIGI_BAUD_153600 20 | |
115 | #define DIGI_BAUD_230400 21 | |
116 | #define DIGI_BAUD_460800 22 | |
117 | ||
118 | /* arguments */ | |
119 | #define DIGI_WORD_SIZE_5 0 | |
120 | #define DIGI_WORD_SIZE_6 1 | |
121 | #define DIGI_WORD_SIZE_7 2 | |
122 | #define DIGI_WORD_SIZE_8 3 | |
123 | ||
124 | #define DIGI_PARITY_NONE 0 | |
125 | #define DIGI_PARITY_ODD 1 | |
126 | #define DIGI_PARITY_EVEN 2 | |
127 | #define DIGI_PARITY_MARK 3 | |
128 | #define DIGI_PARITY_SPACE 4 | |
129 | ||
130 | #define DIGI_STOP_BITS_1 0 | |
131 | #define DIGI_STOP_BITS_2 1 | |
132 | ||
133 | #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1 | |
134 | #define DIGI_INPUT_FLOW_CONTROL_RTS 2 | |
135 | #define DIGI_INPUT_FLOW_CONTROL_DTR 4 | |
136 | ||
137 | #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1 | |
138 | #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2 | |
139 | #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4 | |
140 | ||
141 | #define DIGI_DTR_INACTIVE 0 | |
142 | #define DIGI_DTR_ACTIVE 1 | |
143 | #define DIGI_DTR_INPUT_FLOW_CONTROL 2 | |
144 | ||
145 | #define DIGI_RTS_INACTIVE 0 | |
146 | #define DIGI_RTS_ACTIVE 1 | |
147 | #define DIGI_RTS_INPUT_FLOW_CONTROL 2 | |
148 | #define DIGI_RTS_TOGGLE 3 | |
149 | ||
150 | #define DIGI_FLUSH_TX 1 | |
151 | #define DIGI_FLUSH_RX 2 | |
152 | #define DIGI_RESUME_TX 4 /* clears xoff condition */ | |
153 | ||
154 | #define DIGI_TRANSMIT_NOT_IDLE 0 | |
155 | #define DIGI_TRANSMIT_IDLE 1 | |
156 | ||
157 | #define DIGI_DISABLE 0 | |
158 | #define DIGI_ENABLE 1 | |
159 | ||
160 | #define DIGI_DEASSERT 0 | |
161 | #define DIGI_ASSERT 1 | |
162 | ||
163 | /* in band status codes */ | |
164 | #define DIGI_OVERRUN_ERROR 4 | |
165 | #define DIGI_PARITY_ERROR 8 | |
166 | #define DIGI_FRAMING_ERROR 16 | |
167 | #define DIGI_BREAK_ERROR 32 | |
168 | ||
169 | /* out of band status */ | |
170 | #define DIGI_NO_ERROR 0 | |
171 | #define DIGI_BAD_FIRST_PARAMETER 1 | |
172 | #define DIGI_BAD_SECOND_PARAMETER 2 | |
173 | #define DIGI_INVALID_LINE 3 | |
174 | #define DIGI_INVALID_OPCODE 4 | |
175 | ||
176 | /* input signals */ | |
177 | #define DIGI_READ_INPUT_SIGNALS_SLOT 1 | |
178 | #define DIGI_READ_INPUT_SIGNALS_ERR 2 | |
179 | #define DIGI_READ_INPUT_SIGNALS_BUSY 4 | |
180 | #define DIGI_READ_INPUT_SIGNALS_PE 8 | |
181 | #define DIGI_READ_INPUT_SIGNALS_CTS 16 | |
182 | #define DIGI_READ_INPUT_SIGNALS_DSR 32 | |
183 | #define DIGI_READ_INPUT_SIGNALS_RI 64 | |
184 | #define DIGI_READ_INPUT_SIGNALS_DCD 128 | |
185 | ||
186 | ||
187 | /* Structures */ | |
188 | ||
189 | struct digi_serial { | |
190 | spinlock_t ds_serial_lock; | |
191 | struct usb_serial_port *ds_oob_port; /* out-of-band port */ | |
192 | int ds_oob_port_num; /* index of out-of-band port */ | |
193 | int ds_device_started; | |
194 | }; | |
195 | ||
196 | struct digi_port { | |
197 | spinlock_t dp_port_lock; | |
198 | int dp_port_num; | |
199 | int dp_out_buf_len; | |
200 | unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE]; | |
1da177e4 LT |
201 | int dp_write_urb_in_use; |
202 | unsigned int dp_modem_signals; | |
203 | wait_queue_head_t dp_modem_change_wait; | |
204 | int dp_transmit_idle; | |
205 | wait_queue_head_t dp_transmit_idle_wait; | |
206 | int dp_throttled; | |
207 | int dp_throttle_restart; | |
208 | wait_queue_head_t dp_flush_wait; | |
1da177e4 LT |
209 | wait_queue_head_t dp_close_wait; /* wait queue for close */ |
210 | struct work_struct dp_wakeup_work; | |
c4028958 | 211 | struct usb_serial_port *dp_port; |
1da177e4 LT |
212 | }; |
213 | ||
214 | ||
215 | /* Local Function Declarations */ | |
216 | ||
c6d61269 | 217 | static void digi_wakeup_write(struct usb_serial_port *port); |
c4028958 | 218 | static void digi_wakeup_write_lock(struct work_struct *work); |
c6d61269 AC |
219 | static int digi_write_oob_command(struct usb_serial_port *port, |
220 | unsigned char *buf, int count, int interruptible); | |
221 | static int digi_write_inb_command(struct usb_serial_port *port, | |
222 | unsigned char *buf, int count, unsigned long timeout); | |
223 | static int digi_set_modem_signals(struct usb_serial_port *port, | |
224 | unsigned int modem_signals, int interruptible); | |
225 | static int digi_transmit_idle(struct usb_serial_port *port, | |
226 | unsigned long timeout); | |
41ad427d AC |
227 | static void digi_rx_throttle(struct tty_struct *tty); |
228 | static void digi_rx_unthrottle(struct tty_struct *tty); | |
95da310e AC |
229 | static void digi_set_termios(struct tty_struct *tty, |
230 | struct usb_serial_port *port, struct ktermios *old_termios); | |
231 | static void digi_break_ctl(struct tty_struct *tty, int break_state); | |
60b33c13 | 232 | static int digi_tiocmget(struct tty_struct *tty); |
20b9d177 AC |
233 | static int digi_tiocmset(struct tty_struct *tty, unsigned int set, |
234 | unsigned int clear); | |
95da310e | 235 | static int digi_write(struct tty_struct *tty, struct usb_serial_port *port, |
20b9d177 | 236 | const unsigned char *buf, int count); |
c6d61269 | 237 | static void digi_write_bulk_callback(struct urb *urb); |
95da310e AC |
238 | static int digi_write_room(struct tty_struct *tty); |
239 | static int digi_chars_in_buffer(struct tty_struct *tty); | |
a509a7e4 | 240 | static int digi_open(struct tty_struct *tty, struct usb_serial_port *port); |
335f8514 | 241 | static void digi_close(struct usb_serial_port *port); |
335f8514 | 242 | static void digi_dtr_rts(struct usb_serial_port *port, int on); |
c6d61269 AC |
243 | static int digi_startup_device(struct usb_serial *serial); |
244 | static int digi_startup(struct usb_serial *serial); | |
f9c99bb8 AS |
245 | static void digi_disconnect(struct usb_serial *serial); |
246 | static void digi_release(struct usb_serial *serial); | |
c6d61269 AC |
247 | static void digi_read_bulk_callback(struct urb *urb); |
248 | static int digi_read_inb_callback(struct urb *urb); | |
249 | static int digi_read_oob_callback(struct urb *urb); | |
1da177e4 LT |
250 | |
251 | ||
7d40d7e8 | 252 | static const struct usb_device_id id_table_combined[] = { |
1da177e4 LT |
253 | { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, |
254 | { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, | |
255 | { } /* Terminating entry */ | |
256 | }; | |
257 | ||
7d40d7e8 | 258 | static const struct usb_device_id id_table_2[] = { |
1da177e4 LT |
259 | { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, |
260 | { } /* Terminating entry */ | |
261 | }; | |
262 | ||
7d40d7e8 | 263 | static const struct usb_device_id id_table_4[] = { |
1da177e4 LT |
264 | { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, |
265 | { } /* Terminating entry */ | |
266 | }; | |
267 | ||
41ad427d | 268 | MODULE_DEVICE_TABLE(usb, id_table_combined); |
1da177e4 | 269 | |
1da177e4 LT |
270 | /* device info needed for the Digi serial converter */ |
271 | ||
ea65370d | 272 | static struct usb_serial_driver digi_acceleport_2_device = { |
18fcac35 | 273 | .driver = { |
269bda1c GKH |
274 | .owner = THIS_MODULE, |
275 | .name = "digi_2", | |
18fcac35 | 276 | }, |
269bda1c | 277 | .description = "Digi 2 port USB adapter", |
1da177e4 | 278 | .id_table = id_table_2, |
1da177e4 LT |
279 | .num_ports = 3, |
280 | .open = digi_open, | |
281 | .close = digi_close, | |
335f8514 | 282 | .dtr_rts = digi_dtr_rts, |
1da177e4 LT |
283 | .write = digi_write, |
284 | .write_room = digi_write_room, | |
285 | .write_bulk_callback = digi_write_bulk_callback, | |
286 | .read_bulk_callback = digi_read_bulk_callback, | |
287 | .chars_in_buffer = digi_chars_in_buffer, | |
288 | .throttle = digi_rx_throttle, | |
289 | .unthrottle = digi_rx_unthrottle, | |
1da177e4 LT |
290 | .set_termios = digi_set_termios, |
291 | .break_ctl = digi_break_ctl, | |
292 | .tiocmget = digi_tiocmget, | |
293 | .tiocmset = digi_tiocmset, | |
294 | .attach = digi_startup, | |
f9c99bb8 AS |
295 | .disconnect = digi_disconnect, |
296 | .release = digi_release, | |
1da177e4 LT |
297 | }; |
298 | ||
ea65370d | 299 | static struct usb_serial_driver digi_acceleport_4_device = { |
18fcac35 | 300 | .driver = { |
269bda1c GKH |
301 | .owner = THIS_MODULE, |
302 | .name = "digi_4", | |
18fcac35 | 303 | }, |
269bda1c | 304 | .description = "Digi 4 port USB adapter", |
1da177e4 | 305 | .id_table = id_table_4, |
1da177e4 LT |
306 | .num_ports = 4, |
307 | .open = digi_open, | |
308 | .close = digi_close, | |
309 | .write = digi_write, | |
310 | .write_room = digi_write_room, | |
311 | .write_bulk_callback = digi_write_bulk_callback, | |
312 | .read_bulk_callback = digi_read_bulk_callback, | |
313 | .chars_in_buffer = digi_chars_in_buffer, | |
314 | .throttle = digi_rx_throttle, | |
315 | .unthrottle = digi_rx_unthrottle, | |
1da177e4 LT |
316 | .set_termios = digi_set_termios, |
317 | .break_ctl = digi_break_ctl, | |
318 | .tiocmget = digi_tiocmget, | |
319 | .tiocmset = digi_tiocmset, | |
320 | .attach = digi_startup, | |
f9c99bb8 AS |
321 | .disconnect = digi_disconnect, |
322 | .release = digi_release, | |
1da177e4 LT |
323 | }; |
324 | ||
97b6b6d2 AS |
325 | static struct usb_serial_driver * const serial_drivers[] = { |
326 | &digi_acceleport_2_device, &digi_acceleport_4_device, NULL | |
327 | }; | |
1da177e4 LT |
328 | |
329 | /* Functions */ | |
330 | ||
331 | /* | |
41ad427d AC |
332 | * Cond Wait Interruptible Timeout Irqrestore |
333 | * | |
334 | * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout | |
335 | * so that wake ups are not lost if they occur between the unlock | |
336 | * and the sleep. In other words, spin_unlock_irqrestore and | |
337 | * interruptible_sleep_on_timeout are "atomic" with respect to | |
338 | * wake ups. This is used to implement condition variables. | |
339 | * | |
340 | * interruptible_sleep_on_timeout is deprecated and has been replaced | |
341 | * with the equivalent code. | |
342 | */ | |
1da177e4 | 343 | |
c6d61269 | 344 | static long cond_wait_interruptible_timeout_irqrestore( |
1da177e4 | 345 | wait_queue_head_t *q, long timeout, |
c6d61269 | 346 | spinlock_t *lock, unsigned long flags) |
5a33956a | 347 | __releases(lock) |
1da177e4 LT |
348 | { |
349 | DEFINE_WAIT(wait); | |
350 | ||
45f23f18 | 351 | prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE); |
1da177e4 LT |
352 | spin_unlock_irqrestore(lock, flags); |
353 | timeout = schedule_timeout(timeout); | |
354 | finish_wait(q, &wait); | |
355 | ||
356 | return timeout; | |
1da177e4 LT |
357 | } |
358 | ||
359 | ||
360 | /* | |
41ad427d AC |
361 | * Digi Wakeup Write |
362 | * | |
363 | * Wake up port, line discipline, and tty processes sleeping | |
364 | * on writes. | |
365 | */ | |
1da177e4 | 366 | |
c4028958 | 367 | static void digi_wakeup_write_lock(struct work_struct *work) |
1da177e4 | 368 | { |
41ad427d AC |
369 | struct digi_port *priv = |
370 | container_of(work, struct digi_port, dp_wakeup_work); | |
c4028958 | 371 | struct usb_serial_port *port = priv->dp_port; |
1da177e4 | 372 | unsigned long flags; |
1da177e4 | 373 | |
c6d61269 AC |
374 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
375 | digi_wakeup_write(port); | |
376 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); | |
1da177e4 LT |
377 | } |
378 | ||
c6d61269 | 379 | static void digi_wakeup_write(struct usb_serial_port *port) |
1da177e4 | 380 | { |
4a90f09b | 381 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
4287341d AC |
382 | if (tty) { |
383 | tty_wakeup(tty); | |
384 | tty_kref_put(tty); | |
385 | } | |
1da177e4 LT |
386 | } |
387 | ||
388 | ||
389 | /* | |
41ad427d AC |
390 | * Digi Write OOB Command |
391 | * | |
392 | * Write commands on the out of band port. Commands are 4 | |
393 | * bytes each, multiple commands can be sent at once, and | |
394 | * no command will be split across USB packets. Returns 0 | |
395 | * if successful, -EINTR if interrupted while sleeping and | |
396 | * the interruptible flag is true, or a negative error | |
397 | * returned by usb_submit_urb. | |
398 | */ | |
1da177e4 | 399 | |
c6d61269 AC |
400 | static int digi_write_oob_command(struct usb_serial_port *port, |
401 | unsigned char *buf, int count, int interruptible) | |
1da177e4 | 402 | { |
1da177e4 LT |
403 | int ret = 0; |
404 | int len; | |
405 | struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; | |
406 | struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); | |
407 | unsigned long flags = 0; | |
408 | ||
f9dfbebb GKH |
409 | dev_dbg(&port->dev, |
410 | "digi_write_oob_command: TOP: port=%d, count=%d\n", | |
411 | oob_priv->dp_port_num, count); | |
1da177e4 | 412 | |
c6d61269 | 413 | spin_lock_irqsave(&oob_priv->dp_port_lock, flags); |
41ad427d | 414 | while (count > 0) { |
50de36f7 | 415 | while (oob_priv->dp_write_urb_in_use) { |
1da177e4 LT |
416 | cond_wait_interruptible_timeout_irqrestore( |
417 | &oob_port->write_wait, DIGI_RETRY_TIMEOUT, | |
c6d61269 AC |
418 | &oob_priv->dp_port_lock, flags); |
419 | if (interruptible && signal_pending(current)) | |
420 | return -EINTR; | |
421 | spin_lock_irqsave(&oob_priv->dp_port_lock, flags); | |
1da177e4 LT |
422 | } |
423 | ||
424 | /* len must be a multiple of 4, so commands are not split */ | |
c6d61269 AC |
425 | len = min(count, oob_port->bulk_out_size); |
426 | if (len > 4) | |
1da177e4 | 427 | len &= ~3; |
c6d61269 | 428 | memcpy(oob_port->write_urb->transfer_buffer, buf, len); |
1da177e4 | 429 | oob_port->write_urb->transfer_buffer_length = len; |
41ad427d AC |
430 | ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); |
431 | if (ret == 0) { | |
1da177e4 LT |
432 | oob_priv->dp_write_urb_in_use = 1; |
433 | count -= len; | |
434 | buf += len; | |
435 | } | |
1da177e4 | 436 | } |
c6d61269 AC |
437 | spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); |
438 | if (ret) | |
194343d9 GKH |
439 | dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", |
440 | __func__, ret); | |
c6d61269 | 441 | return ret; |
1da177e4 LT |
442 | |
443 | } | |
444 | ||
445 | ||
446 | /* | |
41ad427d AC |
447 | * Digi Write In Band Command |
448 | * | |
449 | * Write commands on the given port. Commands are 4 | |
450 | * bytes each, multiple commands can be sent at once, and | |
451 | * no command will be split across USB packets. If timeout | |
452 | * is non-zero, write in band command will return after | |
453 | * waiting unsuccessfully for the URB status to clear for | |
454 | * timeout ticks. Returns 0 if successful, or a negative | |
455 | * error returned by digi_write. | |
456 | */ | |
1da177e4 | 457 | |
c6d61269 AC |
458 | static int digi_write_inb_command(struct usb_serial_port *port, |
459 | unsigned char *buf, int count, unsigned long timeout) | |
1da177e4 | 460 | { |
1da177e4 LT |
461 | int ret = 0; |
462 | int len; | |
463 | struct digi_port *priv = usb_get_serial_port_data(port); | |
464 | unsigned char *data = port->write_urb->transfer_buffer; | |
465 | unsigned long flags = 0; | |
466 | ||
f9dfbebb | 467 | dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n", |
c6d61269 | 468 | priv->dp_port_num, count); |
1da177e4 | 469 | |
c6d61269 | 470 | if (timeout) |
1da177e4 LT |
471 | timeout += jiffies; |
472 | else | |
473 | timeout = ULONG_MAX; | |
474 | ||
c6d61269 | 475 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
41ad427d | 476 | while (count > 0 && ret == 0) { |
50de36f7 GKH |
477 | while (priv->dp_write_urb_in_use && |
478 | time_before(jiffies, timeout)) { | |
1da177e4 LT |
479 | cond_wait_interruptible_timeout_irqrestore( |
480 | &port->write_wait, DIGI_RETRY_TIMEOUT, | |
c6d61269 AC |
481 | &priv->dp_port_lock, flags); |
482 | if (signal_pending(current)) | |
483 | return -EINTR; | |
484 | spin_lock_irqsave(&priv->dp_port_lock, flags); | |
1da177e4 LT |
485 | } |
486 | ||
487 | /* len must be a multiple of 4 and small enough to */ | |
488 | /* guarantee the write will send buffered data first, */ | |
489 | /* so commands are in order with data and not split */ | |
c6d61269 AC |
490 | len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); |
491 | if (len > 4) | |
1da177e4 LT |
492 | len &= ~3; |
493 | ||
494 | /* write any buffered data first */ | |
c6d61269 | 495 | if (priv->dp_out_buf_len > 0) { |
1da177e4 LT |
496 | data[0] = DIGI_CMD_SEND_DATA; |
497 | data[1] = priv->dp_out_buf_len; | |
c6d61269 AC |
498 | memcpy(data + 2, priv->dp_out_buf, |
499 | priv->dp_out_buf_len); | |
500 | memcpy(data + 2 + priv->dp_out_buf_len, buf, len); | |
1da177e4 | 501 | port->write_urb->transfer_buffer_length |
c6d61269 | 502 | = priv->dp_out_buf_len + 2 + len; |
1da177e4 | 503 | } else { |
c6d61269 | 504 | memcpy(data, buf, len); |
1da177e4 LT |
505 | port->write_urb->transfer_buffer_length = len; |
506 | } | |
1da177e4 | 507 | |
41ad427d AC |
508 | ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
509 | if (ret == 0) { | |
1da177e4 LT |
510 | priv->dp_write_urb_in_use = 1; |
511 | priv->dp_out_buf_len = 0; | |
512 | count -= len; | |
513 | buf += len; | |
514 | } | |
515 | ||
516 | } | |
c6d61269 | 517 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
1da177e4 | 518 | |
c6d61269 | 519 | if (ret) |
194343d9 GKH |
520 | dev_err(&port->dev, |
521 | "%s: usb_submit_urb failed, ret=%d, port=%d\n", | |
441b62c1 | 522 | __func__, ret, priv->dp_port_num); |
c6d61269 | 523 | return ret; |
1da177e4 LT |
524 | } |
525 | ||
526 | ||
527 | /* | |
41ad427d AC |
528 | * Digi Set Modem Signals |
529 | * | |
530 | * Sets or clears DTR and RTS on the port, according to the | |
531 | * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags | |
532 | * for the modem_signals argument. Returns 0 if successful, | |
533 | * -EINTR if interrupted while sleeping, or a non-zero error | |
534 | * returned by usb_submit_urb. | |
535 | */ | |
1da177e4 | 536 | |
c6d61269 AC |
537 | static int digi_set_modem_signals(struct usb_serial_port *port, |
538 | unsigned int modem_signals, int interruptible) | |
1da177e4 LT |
539 | { |
540 | ||
541 | int ret; | |
542 | struct digi_port *port_priv = usb_get_serial_port_data(port); | |
41ad427d | 543 | struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; |
1da177e4 LT |
544 | struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); |
545 | unsigned char *data = oob_port->write_urb->transfer_buffer; | |
546 | unsigned long flags = 0; | |
547 | ||
548 | ||
f9dfbebb GKH |
549 | dev_dbg(&port->dev, |
550 | "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n", | |
c6d61269 | 551 | port_priv->dp_port_num, modem_signals); |
1da177e4 | 552 | |
c6d61269 AC |
553 | spin_lock_irqsave(&oob_priv->dp_port_lock, flags); |
554 | spin_lock(&port_priv->dp_port_lock); | |
1da177e4 | 555 | |
50de36f7 | 556 | while (oob_priv->dp_write_urb_in_use) { |
c6d61269 | 557 | spin_unlock(&port_priv->dp_port_lock); |
1da177e4 LT |
558 | cond_wait_interruptible_timeout_irqrestore( |
559 | &oob_port->write_wait, DIGI_RETRY_TIMEOUT, | |
c6d61269 AC |
560 | &oob_priv->dp_port_lock, flags); |
561 | if (interruptible && signal_pending(current)) | |
562 | return -EINTR; | |
563 | spin_lock_irqsave(&oob_priv->dp_port_lock, flags); | |
564 | spin_lock(&port_priv->dp_port_lock); | |
1da177e4 | 565 | } |
1da177e4 LT |
566 | data[0] = DIGI_CMD_SET_DTR_SIGNAL; |
567 | data[1] = port_priv->dp_port_num; | |
41ad427d AC |
568 | data[2] = (modem_signals & TIOCM_DTR) ? |
569 | DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE; | |
1da177e4 | 570 | data[3] = 0; |
1da177e4 LT |
571 | data[4] = DIGI_CMD_SET_RTS_SIGNAL; |
572 | data[5] = port_priv->dp_port_num; | |
41ad427d AC |
573 | data[6] = (modem_signals & TIOCM_RTS) ? |
574 | DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE; | |
1da177e4 LT |
575 | data[7] = 0; |
576 | ||
577 | oob_port->write_urb->transfer_buffer_length = 8; | |
1da177e4 | 578 | |
41ad427d AC |
579 | ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); |
580 | if (ret == 0) { | |
1da177e4 LT |
581 | oob_priv->dp_write_urb_in_use = 1; |
582 | port_priv->dp_modem_signals = | |
583 | (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS)) | |
584 | | (modem_signals&(TIOCM_DTR|TIOCM_RTS)); | |
585 | } | |
c6d61269 AC |
586 | spin_unlock(&port_priv->dp_port_lock); |
587 | spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); | |
588 | if (ret) | |
194343d9 GKH |
589 | dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", |
590 | __func__, ret); | |
c6d61269 | 591 | return ret; |
1da177e4 LT |
592 | } |
593 | ||
1da177e4 | 594 | /* |
41ad427d AC |
595 | * Digi Transmit Idle |
596 | * | |
597 | * Digi transmit idle waits, up to timeout ticks, for the transmitter | |
598 | * to go idle. It returns 0 if successful or a negative error. | |
599 | * | |
600 | * There are race conditions here if more than one process is calling | |
601 | * digi_transmit_idle on the same port at the same time. However, this | |
602 | * is only called from close, and only one process can be in close on a | |
603 | * port at a time, so its ok. | |
604 | */ | |
1da177e4 | 605 | |
c6d61269 AC |
606 | static int digi_transmit_idle(struct usb_serial_port *port, |
607 | unsigned long timeout) | |
1da177e4 | 608 | { |
1da177e4 LT |
609 | int ret; |
610 | unsigned char buf[2]; | |
611 | struct digi_port *priv = usb_get_serial_port_data(port); | |
612 | unsigned long flags = 0; | |
613 | ||
c6d61269 | 614 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 615 | priv->dp_transmit_idle = 0; |
c6d61269 | 616 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
1da177e4 LT |
617 | |
618 | buf[0] = DIGI_CMD_TRANSMIT_IDLE; | |
619 | buf[1] = 0; | |
620 | ||
621 | timeout += jiffies; | |
622 | ||
41ad427d AC |
623 | ret = digi_write_inb_command(port, buf, 2, timeout - jiffies); |
624 | if (ret != 0) | |
c6d61269 | 625 | return ret; |
1da177e4 | 626 | |
c6d61269 | 627 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 628 | |
41ad427d | 629 | while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) { |
1da177e4 LT |
630 | cond_wait_interruptible_timeout_irqrestore( |
631 | &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT, | |
c6d61269 AC |
632 | &priv->dp_port_lock, flags); |
633 | if (signal_pending(current)) | |
634 | return -EINTR; | |
635 | spin_lock_irqsave(&priv->dp_port_lock, flags); | |
1da177e4 | 636 | } |
1da177e4 | 637 | priv->dp_transmit_idle = 0; |
c6d61269 AC |
638 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
639 | return 0; | |
1da177e4 LT |
640 | |
641 | } | |
642 | ||
643 | ||
95da310e | 644 | static void digi_rx_throttle(struct tty_struct *tty) |
1da177e4 | 645 | { |
1da177e4 | 646 | unsigned long flags; |
95da310e | 647 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
648 | struct digi_port *priv = usb_get_serial_port_data(port); |
649 | ||
1da177e4 | 650 | /* stop receiving characters by not resubmitting the read urb */ |
c6d61269 | 651 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 LT |
652 | priv->dp_throttled = 1; |
653 | priv->dp_throttle_restart = 0; | |
c6d61269 | 654 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
1da177e4 LT |
655 | } |
656 | ||
657 | ||
95da310e | 658 | static void digi_rx_unthrottle(struct tty_struct *tty) |
1da177e4 | 659 | { |
1da177e4 | 660 | int ret = 0; |
1da177e4 | 661 | unsigned long flags; |
95da310e | 662 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 663 | struct digi_port *priv = usb_get_serial_port_data(port); |
1da177e4 | 664 | |
c6d61269 | 665 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 666 | |
1da177e4 | 667 | /* restart read chain */ |
5833041f | 668 | if (priv->dp_throttle_restart) |
c6d61269 | 669 | ret = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
1da177e4 | 670 | |
ba6b702f JH |
671 | /* turn throttle off */ |
672 | priv->dp_throttled = 0; | |
673 | priv->dp_throttle_restart = 0; | |
674 | ||
c6d61269 | 675 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
1da177e4 | 676 | |
c6d61269 | 677 | if (ret) |
194343d9 GKH |
678 | dev_err(&port->dev, |
679 | "%s: usb_submit_urb failed, ret=%d, port=%d\n", | |
441b62c1 | 680 | __func__, ret, priv->dp_port_num); |
1da177e4 LT |
681 | } |
682 | ||
683 | ||
41ad427d | 684 | static void digi_set_termios(struct tty_struct *tty, |
95da310e | 685 | struct usb_serial_port *port, struct ktermios *old_termios) |
1da177e4 | 686 | { |
1da177e4 | 687 | struct digi_port *priv = usb_get_serial_port_data(port); |
f9dfbebb | 688 | struct device *dev = &port->dev; |
adc8d746 AC |
689 | unsigned int iflag = tty->termios.c_iflag; |
690 | unsigned int cflag = tty->termios.c_cflag; | |
1da177e4 LT |
691 | unsigned int old_iflag = old_termios->c_iflag; |
692 | unsigned int old_cflag = old_termios->c_cflag; | |
693 | unsigned char buf[32]; | |
694 | unsigned int modem_signals; | |
41ad427d | 695 | int arg, ret; |
1da177e4 | 696 | int i = 0; |
c6d61269 | 697 | speed_t baud; |
1da177e4 | 698 | |
f9dfbebb GKH |
699 | dev_dbg(dev, |
700 | "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n", | |
701 | priv->dp_port_num, iflag, old_iflag, cflag, old_cflag); | |
1da177e4 LT |
702 | |
703 | /* set baud rate */ | |
41ad427d AC |
704 | baud = tty_get_baud_rate(tty); |
705 | if (baud != tty_termios_baud_rate(old_termios)) { | |
1da177e4 LT |
706 | arg = -1; |
707 | ||
708 | /* reassert DTR and (maybe) RTS on transition from B0 */ | |
c6d61269 | 709 | if ((old_cflag&CBAUD) == B0) { |
1da177e4 LT |
710 | /* don't set RTS if using hardware flow control */ |
711 | /* and throttling input */ | |
712 | modem_signals = TIOCM_DTR; | |
adc8d746 | 713 | if (!(tty->termios.c_cflag & CRTSCTS) || |
c6d61269 | 714 | !test_bit(TTY_THROTTLED, &tty->flags)) |
1da177e4 | 715 | modem_signals |= TIOCM_RTS; |
c6d61269 | 716 | digi_set_modem_signals(port, modem_signals, 1); |
1da177e4 | 717 | } |
c6d61269 | 718 | switch (baud) { |
41ad427d AC |
719 | /* drop DTR and RTS on transition to B0 */ |
720 | case 0: digi_set_modem_signals(port, 0, 1); break; | |
721 | case 50: arg = DIGI_BAUD_50; break; | |
722 | case 75: arg = DIGI_BAUD_75; break; | |
723 | case 110: arg = DIGI_BAUD_110; break; | |
724 | case 150: arg = DIGI_BAUD_150; break; | |
725 | case 200: arg = DIGI_BAUD_200; break; | |
726 | case 300: arg = DIGI_BAUD_300; break; | |
727 | case 600: arg = DIGI_BAUD_600; break; | |
728 | case 1200: arg = DIGI_BAUD_1200; break; | |
729 | case 1800: arg = DIGI_BAUD_1800; break; | |
730 | case 2400: arg = DIGI_BAUD_2400; break; | |
731 | case 4800: arg = DIGI_BAUD_4800; break; | |
732 | case 9600: arg = DIGI_BAUD_9600; break; | |
733 | case 19200: arg = DIGI_BAUD_19200; break; | |
734 | case 38400: arg = DIGI_BAUD_38400; break; | |
735 | case 57600: arg = DIGI_BAUD_57600; break; | |
736 | case 115200: arg = DIGI_BAUD_115200; break; | |
737 | case 230400: arg = DIGI_BAUD_230400; break; | |
738 | case 460800: arg = DIGI_BAUD_460800; break; | |
739 | default: | |
740 | arg = DIGI_BAUD_9600; | |
741 | baud = 9600; | |
742 | break; | |
1da177e4 | 743 | } |
c6d61269 | 744 | if (arg != -1) { |
1da177e4 LT |
745 | buf[i++] = DIGI_CMD_SET_BAUD_RATE; |
746 | buf[i++] = priv->dp_port_num; | |
747 | buf[i++] = arg; | |
748 | buf[i++] = 0; | |
749 | } | |
1da177e4 | 750 | } |
1da177e4 | 751 | /* set parity */ |
adc8d746 | 752 | tty->termios.c_cflag &= ~CMSPAR; |
7fa36a99 | 753 | |
c6d61269 AC |
754 | if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) { |
755 | if (cflag&PARENB) { | |
756 | if (cflag&PARODD) | |
1da177e4 LT |
757 | arg = DIGI_PARITY_ODD; |
758 | else | |
759 | arg = DIGI_PARITY_EVEN; | |
760 | } else { | |
761 | arg = DIGI_PARITY_NONE; | |
762 | } | |
1da177e4 LT |
763 | buf[i++] = DIGI_CMD_SET_PARITY; |
764 | buf[i++] = priv->dp_port_num; | |
765 | buf[i++] = arg; | |
766 | buf[i++] = 0; | |
1da177e4 | 767 | } |
1da177e4 | 768 | /* set word size */ |
c6d61269 | 769 | if ((cflag&CSIZE) != (old_cflag&CSIZE)) { |
1da177e4 | 770 | arg = -1; |
c6d61269 | 771 | switch (cflag&CSIZE) { |
1da177e4 LT |
772 | case CS5: arg = DIGI_WORD_SIZE_5; break; |
773 | case CS6: arg = DIGI_WORD_SIZE_6; break; | |
774 | case CS7: arg = DIGI_WORD_SIZE_7; break; | |
775 | case CS8: arg = DIGI_WORD_SIZE_8; break; | |
776 | default: | |
f9dfbebb GKH |
777 | dev_dbg(dev, |
778 | "digi_set_termios: can't handle word size %d\n", | |
c6d61269 | 779 | (cflag&CSIZE)); |
1da177e4 LT |
780 | break; |
781 | } | |
782 | ||
c6d61269 | 783 | if (arg != -1) { |
1da177e4 LT |
784 | buf[i++] = DIGI_CMD_SET_WORD_SIZE; |
785 | buf[i++] = priv->dp_port_num; | |
786 | buf[i++] = arg; | |
787 | buf[i++] = 0; | |
788 | } | |
789 | ||
790 | } | |
791 | ||
792 | /* set stop bits */ | |
c6d61269 | 793 | if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) { |
1da177e4 | 794 | |
c6d61269 | 795 | if ((cflag&CSTOPB)) |
1da177e4 LT |
796 | arg = DIGI_STOP_BITS_2; |
797 | else | |
798 | arg = DIGI_STOP_BITS_1; | |
799 | ||
800 | buf[i++] = DIGI_CMD_SET_STOP_BITS; | |
801 | buf[i++] = priv->dp_port_num; | |
802 | buf[i++] = arg; | |
803 | buf[i++] = 0; | |
804 | ||
805 | } | |
806 | ||
807 | /* set input flow control */ | |
c6d61269 AC |
808 | if ((iflag&IXOFF) != (old_iflag&IXOFF) |
809 | || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) { | |
1da177e4 | 810 | arg = 0; |
c6d61269 | 811 | if (iflag&IXOFF) |
1da177e4 LT |
812 | arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF; |
813 | else | |
814 | arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF; | |
815 | ||
c6d61269 | 816 | if (cflag&CRTSCTS) { |
1da177e4 LT |
817 | arg |= DIGI_INPUT_FLOW_CONTROL_RTS; |
818 | ||
819 | /* On USB-4 it is necessary to assert RTS prior */ | |
820 | /* to selecting RTS input flow control. */ | |
821 | buf[i++] = DIGI_CMD_SET_RTS_SIGNAL; | |
822 | buf[i++] = priv->dp_port_num; | |
823 | buf[i++] = DIGI_RTS_ACTIVE; | |
824 | buf[i++] = 0; | |
825 | ||
826 | } else { | |
827 | arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS; | |
828 | } | |
1da177e4 LT |
829 | buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; |
830 | buf[i++] = priv->dp_port_num; | |
831 | buf[i++] = arg; | |
832 | buf[i++] = 0; | |
1da177e4 LT |
833 | } |
834 | ||
835 | /* set output flow control */ | |
7fa36a99 AC |
836 | if ((iflag & IXON) != (old_iflag & IXON) |
837 | || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) { | |
1da177e4 | 838 | arg = 0; |
7fa36a99 | 839 | if (iflag & IXON) |
1da177e4 LT |
840 | arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; |
841 | else | |
842 | arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; | |
843 | ||
7fa36a99 | 844 | if (cflag & CRTSCTS) { |
1da177e4 LT |
845 | arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS; |
846 | } else { | |
847 | arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS; | |
c6d61269 | 848 | tty->hw_stopped = 0; |
1da177e4 LT |
849 | } |
850 | ||
851 | buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; | |
852 | buf[i++] = priv->dp_port_num; | |
853 | buf[i++] = arg; | |
854 | buf[i++] = 0; | |
1da177e4 LT |
855 | } |
856 | ||
857 | /* set receive enable/disable */ | |
7fa36a99 AC |
858 | if ((cflag & CREAD) != (old_cflag & CREAD)) { |
859 | if (cflag & CREAD) | |
1da177e4 LT |
860 | arg = DIGI_ENABLE; |
861 | else | |
862 | arg = DIGI_DISABLE; | |
863 | ||
864 | buf[i++] = DIGI_CMD_RECEIVE_ENABLE; | |
865 | buf[i++] = priv->dp_port_num; | |
866 | buf[i++] = arg; | |
867 | buf[i++] = 0; | |
1da177e4 | 868 | } |
41ad427d AC |
869 | ret = digi_write_oob_command(port, buf, i, 1); |
870 | if (ret != 0) | |
f9dfbebb | 871 | dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret); |
7fa36a99 | 872 | tty_encode_baud_rate(tty, baud, baud); |
1da177e4 LT |
873 | } |
874 | ||
875 | ||
95da310e | 876 | static void digi_break_ctl(struct tty_struct *tty, int break_state) |
1da177e4 | 877 | { |
95da310e | 878 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
879 | unsigned char buf[4]; |
880 | ||
1da177e4 LT |
881 | buf[0] = DIGI_CMD_BREAK_CONTROL; |
882 | buf[1] = 2; /* length */ | |
883 | buf[2] = break_state ? 1 : 0; | |
884 | buf[3] = 0; /* pad */ | |
c6d61269 | 885 | digi_write_inb_command(port, buf, 4, 0); |
1da177e4 LT |
886 | } |
887 | ||
888 | ||
60b33c13 | 889 | static int digi_tiocmget(struct tty_struct *tty) |
1da177e4 | 890 | { |
95da310e | 891 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
892 | struct digi_port *priv = usb_get_serial_port_data(port); |
893 | unsigned int val; | |
894 | unsigned long flags; | |
895 | ||
c6d61269 | 896 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 897 | val = priv->dp_modem_signals; |
c6d61269 | 898 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
1da177e4 LT |
899 | return val; |
900 | } | |
901 | ||
902 | ||
20b9d177 AC |
903 | static int digi_tiocmset(struct tty_struct *tty, |
904 | unsigned int set, unsigned int clear) | |
1da177e4 | 905 | { |
95da310e | 906 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
907 | struct digi_port *priv = usb_get_serial_port_data(port); |
908 | unsigned int val; | |
909 | unsigned long flags; | |
910 | ||
c6d61269 | 911 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 912 | val = (priv->dp_modem_signals & ~clear) | set; |
c6d61269 AC |
913 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
914 | return digi_set_modem_signals(port, val, 1); | |
1da177e4 LT |
915 | } |
916 | ||
917 | ||
95da310e AC |
918 | static int digi_write(struct tty_struct *tty, struct usb_serial_port *port, |
919 | const unsigned char *buf, int count) | |
1da177e4 LT |
920 | { |
921 | ||
41ad427d | 922 | int ret, data_len, new_len; |
1da177e4 LT |
923 | struct digi_port *priv = usb_get_serial_port_data(port); |
924 | unsigned char *data = port->write_urb->transfer_buffer; | |
925 | unsigned long flags = 0; | |
926 | ||
f9dfbebb GKH |
927 | dev_dbg(&port->dev, |
928 | "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n", | |
c6d61269 | 929 | priv->dp_port_num, count, in_interrupt()); |
1da177e4 LT |
930 | |
931 | /* copy user data (which can sleep) before getting spin lock */ | |
c6d61269 AC |
932 | count = min(count, port->bulk_out_size-2); |
933 | count = min(64, count); | |
1da177e4 LT |
934 | |
935 | /* be sure only one write proceeds at a time */ | |
936 | /* there are races on the port private buffer */ | |
c6d61269 | 937 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 LT |
938 | |
939 | /* wait for urb status clear to submit another urb */ | |
50de36f7 | 940 | if (priv->dp_write_urb_in_use) { |
1da177e4 | 941 | /* buffer data if count is 1 (probably put_char) if possible */ |
c6d61269 | 942 | if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) { |
1da177e4 LT |
943 | priv->dp_out_buf[priv->dp_out_buf_len++] = *buf; |
944 | new_len = 1; | |
945 | } else { | |
946 | new_len = 0; | |
947 | } | |
c6d61269 AC |
948 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
949 | return new_len; | |
1da177e4 LT |
950 | } |
951 | ||
952 | /* allow space for any buffered data and for new data, up to */ | |
953 | /* transfer buffer size - 2 (for command and length bytes) */ | |
954 | new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); | |
955 | data_len = new_len + priv->dp_out_buf_len; | |
956 | ||
c6d61269 AC |
957 | if (data_len == 0) { |
958 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); | |
959 | return 0; | |
1da177e4 LT |
960 | } |
961 | ||
962 | port->write_urb->transfer_buffer_length = data_len+2; | |
1da177e4 LT |
963 | |
964 | *data++ = DIGI_CMD_SEND_DATA; | |
965 | *data++ = data_len; | |
966 | ||
967 | /* copy in buffered data first */ | |
c6d61269 | 968 | memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len); |
1da177e4 LT |
969 | data += priv->dp_out_buf_len; |
970 | ||
971 | /* copy in new data */ | |
c6d61269 | 972 | memcpy(data, buf, new_len); |
1da177e4 | 973 | |
41ad427d AC |
974 | ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
975 | if (ret == 0) { | |
1da177e4 LT |
976 | priv->dp_write_urb_in_use = 1; |
977 | ret = new_len; | |
978 | priv->dp_out_buf_len = 0; | |
979 | } | |
980 | ||
981 | /* return length of new data written, or error */ | |
c6d61269 AC |
982 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
983 | if (ret < 0) | |
22a416c4 | 984 | dev_err_console(port, |
194343d9 | 985 | "%s: usb_submit_urb failed, ret=%d, port=%d\n", |
441b62c1 | 986 | __func__, ret, priv->dp_port_num); |
f9dfbebb | 987 | dev_dbg(&port->dev, "digi_write: returning %d\n", ret); |
c6d61269 | 988 | return ret; |
1da177e4 | 989 | |
41ad427d | 990 | } |
1da177e4 | 991 | |
c6d61269 | 992 | static void digi_write_bulk_callback(struct urb *urb) |
1da177e4 LT |
993 | { |
994 | ||
cdc97792 | 995 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
996 | struct usb_serial *serial; |
997 | struct digi_port *priv; | |
998 | struct digi_serial *serial_priv; | |
999 | int ret = 0; | |
85d75107 | 1000 | int status = urb->status; |
1da177e4 | 1001 | |
1da177e4 | 1002 | /* port and serial sanity check */ |
41ad427d | 1003 | if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) { |
109f34e7 | 1004 | pr_err("%s: port or port->private is NULL, status=%d\n", |
194343d9 | 1005 | __func__, status); |
1da177e4 LT |
1006 | return; |
1007 | } | |
1008 | serial = port->serial; | |
41ad427d | 1009 | if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) { |
194343d9 GKH |
1010 | dev_err(&port->dev, |
1011 | "%s: serial or serial->private is NULL, status=%d\n", | |
1012 | __func__, status); | |
1da177e4 LT |
1013 | return; |
1014 | } | |
1015 | ||
1016 | /* handle oob callback */ | |
c6d61269 | 1017 | if (priv->dp_port_num == serial_priv->ds_oob_port_num) { |
f9dfbebb | 1018 | dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n"); |
c6d61269 | 1019 | spin_lock(&priv->dp_port_lock); |
1da177e4 | 1020 | priv->dp_write_urb_in_use = 0; |
c6d61269 AC |
1021 | wake_up_interruptible(&port->write_wait); |
1022 | spin_unlock(&priv->dp_port_lock); | |
1da177e4 LT |
1023 | return; |
1024 | } | |
1025 | ||
1f87158e | 1026 | /* try to send any buffered data on this port */ |
c6d61269 | 1027 | spin_lock(&priv->dp_port_lock); |
1da177e4 | 1028 | priv->dp_write_urb_in_use = 0; |
1f87158e | 1029 | if (priv->dp_out_buf_len > 0) { |
1da177e4 LT |
1030 | *((unsigned char *)(port->write_urb->transfer_buffer)) |
1031 | = (unsigned char)DIGI_CMD_SEND_DATA; | |
41ad427d | 1032 | *((unsigned char *)(port->write_urb->transfer_buffer) + 1) |
1da177e4 | 1033 | = (unsigned char)priv->dp_out_buf_len; |
41ad427d AC |
1034 | port->write_urb->transfer_buffer_length = |
1035 | priv->dp_out_buf_len + 2; | |
41ad427d | 1036 | memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf, |
c6d61269 | 1037 | priv->dp_out_buf_len); |
41ad427d AC |
1038 | ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
1039 | if (ret == 0) { | |
1da177e4 LT |
1040 | priv->dp_write_urb_in_use = 1; |
1041 | priv->dp_out_buf_len = 0; | |
1042 | } | |
1da177e4 | 1043 | } |
1da177e4 | 1044 | /* wake up processes sleeping on writes immediately */ |
c6d61269 | 1045 | digi_wakeup_write(port); |
1da177e4 LT |
1046 | /* also queue up a wakeup at scheduler time, in case we */ |
1047 | /* lost the race in write_chan(). */ | |
1048 | schedule_work(&priv->dp_wakeup_work); | |
1049 | ||
c6d61269 | 1050 | spin_unlock(&priv->dp_port_lock); |
1f87158e | 1051 | if (ret && ret != -EPERM) |
22a416c4 | 1052 | dev_err_console(port, |
194343d9 | 1053 | "%s: usb_submit_urb failed, ret=%d, port=%d\n", |
441b62c1 | 1054 | __func__, ret, priv->dp_port_num); |
1da177e4 LT |
1055 | } |
1056 | ||
95da310e | 1057 | static int digi_write_room(struct tty_struct *tty) |
1da177e4 | 1058 | { |
95da310e | 1059 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 1060 | struct digi_port *priv = usb_get_serial_port_data(port); |
95da310e | 1061 | int room; |
1da177e4 LT |
1062 | unsigned long flags = 0; |
1063 | ||
c6d61269 | 1064 | spin_lock_irqsave(&priv->dp_port_lock, flags); |
1da177e4 | 1065 | |
50de36f7 | 1066 | if (priv->dp_write_urb_in_use) |
1da177e4 LT |
1067 | room = 0; |
1068 | else | |
1069 | room = port->bulk_out_size - 2 - priv->dp_out_buf_len; | |
1070 | ||
c6d61269 | 1071 | spin_unlock_irqrestore(&priv->dp_port_lock, flags); |
f9dfbebb | 1072 | dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room); |
c6d61269 | 1073 | return room; |
1da177e4 LT |
1074 | |
1075 | } | |
1076 | ||
95da310e | 1077 | static int digi_chars_in_buffer(struct tty_struct *tty) |
1da177e4 | 1078 | { |
95da310e | 1079 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
1080 | struct digi_port *priv = usb_get_serial_port_data(port); |
1081 | ||
50de36f7 | 1082 | if (priv->dp_write_urb_in_use) { |
f9dfbebb | 1083 | dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n", |
c6d61269 AC |
1084 | priv->dp_port_num, port->bulk_out_size - 2); |
1085 | /* return(port->bulk_out_size - 2); */ | |
1086 | return 256; | |
1da177e4 | 1087 | } else { |
f9dfbebb | 1088 | dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n", |
c6d61269 AC |
1089 | priv->dp_port_num, priv->dp_out_buf_len); |
1090 | return priv->dp_out_buf_len; | |
1da177e4 LT |
1091 | } |
1092 | ||
1093 | } | |
1094 | ||
335f8514 AC |
1095 | static void digi_dtr_rts(struct usb_serial_port *port, int on) |
1096 | { | |
1097 | /* Adjust DTR and RTS */ | |
1098 | digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1); | |
1099 | } | |
1100 | ||
a509a7e4 | 1101 | static int digi_open(struct tty_struct *tty, struct usb_serial_port *port) |
1da177e4 | 1102 | { |
1da177e4 LT |
1103 | int ret; |
1104 | unsigned char buf[32]; | |
1105 | struct digi_port *priv = usb_get_serial_port_data(port); | |
606d099c | 1106 | struct ktermios not_termios; |
1da177e4 | 1107 | |
1da177e4 | 1108 | /* be sure the device is started up */ |
c6d61269 AC |
1109 | if (digi_startup_device(port->serial) != 0) |
1110 | return -ENXIO; | |
1da177e4 | 1111 | |
1da177e4 LT |
1112 | /* read modem signals automatically whenever they change */ |
1113 | buf[0] = DIGI_CMD_READ_INPUT_SIGNALS; | |
1114 | buf[1] = priv->dp_port_num; | |
1115 | buf[2] = DIGI_ENABLE; | |
1116 | buf[3] = 0; | |
1117 | ||
1118 | /* flush fifos */ | |
1119 | buf[4] = DIGI_CMD_IFLUSH_FIFO; | |
1120 | buf[5] = priv->dp_port_num; | |
1121 | buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; | |
1122 | buf[7] = 0; | |
1123 | ||
41ad427d AC |
1124 | ret = digi_write_oob_command(port, buf, 8, 1); |
1125 | if (ret != 0) | |
f9dfbebb | 1126 | dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret); |
1da177e4 LT |
1127 | |
1128 | /* set termios settings */ | |
95da310e | 1129 | if (tty) { |
adc8d746 AC |
1130 | not_termios.c_cflag = ~tty->termios.c_cflag; |
1131 | not_termios.c_iflag = ~tty->termios.c_iflag; | |
95da310e AC |
1132 | digi_set_termios(tty, port, ¬_termios); |
1133 | } | |
c6d61269 | 1134 | return 0; |
1da177e4 LT |
1135 | } |
1136 | ||
1137 | ||
335f8514 | 1138 | static void digi_close(struct usb_serial_port *port) |
1da177e4 LT |
1139 | { |
1140 | DEFINE_WAIT(wait); | |
1141 | int ret; | |
1142 | unsigned char buf[32]; | |
1da177e4 | 1143 | struct digi_port *priv = usb_get_serial_port_data(port); |
1da177e4 | 1144 | |
0915f490 | 1145 | mutex_lock(&port->serial->disc_mutex); |
1da177e4 | 1146 | /* if disconnected, just clear flags */ |
0915f490 | 1147 | if (port->serial->disconnected) |
1da177e4 LT |
1148 | goto exit; |
1149 | ||
1da177e4 | 1150 | if (port->serial->dev) { |
335f8514 AC |
1151 | /* FIXME: Transmit idle belongs in the wait_unti_sent path */ |
1152 | digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT); | |
1da177e4 LT |
1153 | |
1154 | /* disable input flow control */ | |
1155 | buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; | |
1156 | buf[1] = priv->dp_port_num; | |
1157 | buf[2] = DIGI_DISABLE; | |
1158 | buf[3] = 0; | |
1159 | ||
1160 | /* disable output flow control */ | |
1161 | buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; | |
1162 | buf[5] = priv->dp_port_num; | |
1163 | buf[6] = DIGI_DISABLE; | |
1164 | buf[7] = 0; | |
1165 | ||
1166 | /* disable reading modem signals automatically */ | |
1167 | buf[8] = DIGI_CMD_READ_INPUT_SIGNALS; | |
1168 | buf[9] = priv->dp_port_num; | |
1169 | buf[10] = DIGI_DISABLE; | |
1170 | buf[11] = 0; | |
1171 | ||
1172 | /* disable receive */ | |
1173 | buf[12] = DIGI_CMD_RECEIVE_ENABLE; | |
1174 | buf[13] = priv->dp_port_num; | |
1175 | buf[14] = DIGI_DISABLE; | |
1176 | buf[15] = 0; | |
1177 | ||
1178 | /* flush fifos */ | |
1179 | buf[16] = DIGI_CMD_IFLUSH_FIFO; | |
1180 | buf[17] = priv->dp_port_num; | |
1181 | buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; | |
1182 | buf[19] = 0; | |
1183 | ||
41ad427d AC |
1184 | ret = digi_write_oob_command(port, buf, 20, 0); |
1185 | if (ret != 0) | |
f9dfbebb | 1186 | dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n", ret); |
1da177e4 LT |
1187 | |
1188 | /* wait for final commands on oob port to complete */ | |
41ad427d AC |
1189 | prepare_to_wait(&priv->dp_flush_wait, &wait, |
1190 | TASK_INTERRUPTIBLE); | |
1da177e4 LT |
1191 | schedule_timeout(DIGI_CLOSE_TIMEOUT); |
1192 | finish_wait(&priv->dp_flush_wait, &wait); | |
1193 | ||
1194 | /* shutdown any outstanding bulk writes */ | |
1195 | usb_kill_urb(port->write_urb); | |
1196 | } | |
1da177e4 | 1197 | exit: |
0915f490 | 1198 | spin_lock_irq(&priv->dp_port_lock); |
1da177e4 | 1199 | priv->dp_write_urb_in_use = 0; |
c6d61269 | 1200 | wake_up_interruptible(&priv->dp_close_wait); |
0915f490 ON |
1201 | spin_unlock_irq(&priv->dp_port_lock); |
1202 | mutex_unlock(&port->serial->disc_mutex); | |
1da177e4 LT |
1203 | } |
1204 | ||
1205 | ||
1206 | /* | |
41ad427d AC |
1207 | * Digi Startup Device |
1208 | * | |
1209 | * Starts reads on all ports. Must be called AFTER startup, with | |
1210 | * urbs initialized. Returns 0 if successful, non-zero error otherwise. | |
1211 | */ | |
1da177e4 | 1212 | |
c6d61269 | 1213 | static int digi_startup_device(struct usb_serial *serial) |
1da177e4 | 1214 | { |
41ad427d | 1215 | int i, ret = 0; |
1da177e4 LT |
1216 | struct digi_serial *serial_priv = usb_get_serial_data(serial); |
1217 | struct usb_serial_port *port; | |
1218 | ||
1da177e4 | 1219 | /* be sure this happens exactly once */ |
c6d61269 AC |
1220 | spin_lock(&serial_priv->ds_serial_lock); |
1221 | if (serial_priv->ds_device_started) { | |
1222 | spin_unlock(&serial_priv->ds_serial_lock); | |
1223 | return 0; | |
1da177e4 LT |
1224 | } |
1225 | serial_priv->ds_device_started = 1; | |
c6d61269 | 1226 | spin_unlock(&serial_priv->ds_serial_lock); |
1da177e4 LT |
1227 | |
1228 | /* start reading from each bulk in endpoint for the device */ | |
1229 | /* set USB_DISABLE_SPD flag for write bulk urbs */ | |
c6d61269 | 1230 | for (i = 0; i < serial->type->num_ports + 1; i++) { |
1da177e4 | 1231 | port = serial->port[i]; |
41ad427d AC |
1232 | ret = usb_submit_urb(port->read_urb, GFP_KERNEL); |
1233 | if (ret != 0) { | |
194343d9 GKH |
1234 | dev_err(&port->dev, |
1235 | "%s: usb_submit_urb failed, ret=%d, port=%d\n", | |
1236 | __func__, ret, i); | |
1da177e4 LT |
1237 | break; |
1238 | } | |
1da177e4 | 1239 | } |
c6d61269 | 1240 | return ret; |
1da177e4 LT |
1241 | } |
1242 | ||
1243 | ||
c6d61269 | 1244 | static int digi_startup(struct usb_serial *serial) |
1da177e4 LT |
1245 | { |
1246 | ||
1247 | int i; | |
1248 | struct digi_port *priv; | |
1249 | struct digi_serial *serial_priv; | |
1250 | ||
1da177e4 LT |
1251 | /* allocate the private data structures for all ports */ |
1252 | /* number of regular ports + 1 for the out-of-band port */ | |
41ad427d | 1253 | for (i = 0; i < serial->type->num_ports + 1; i++) { |
1da177e4 | 1254 | /* allocate port private structure */ |
c6d61269 AC |
1255 | priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL); |
1256 | if (priv == NULL) { | |
1257 | while (--i >= 0) | |
1258 | kfree(usb_get_serial_port_data(serial->port[i])); | |
1259 | return 1; /* error */ | |
1da177e4 LT |
1260 | } |
1261 | ||
1262 | /* initialize port private structure */ | |
c6d61269 | 1263 | spin_lock_init(&priv->dp_port_lock); |
1da177e4 LT |
1264 | priv->dp_port_num = i; |
1265 | priv->dp_out_buf_len = 0; | |
1da177e4 LT |
1266 | priv->dp_write_urb_in_use = 0; |
1267 | priv->dp_modem_signals = 0; | |
c6d61269 | 1268 | init_waitqueue_head(&priv->dp_modem_change_wait); |
1da177e4 | 1269 | priv->dp_transmit_idle = 0; |
c6d61269 | 1270 | init_waitqueue_head(&priv->dp_transmit_idle_wait); |
1da177e4 LT |
1271 | priv->dp_throttled = 0; |
1272 | priv->dp_throttle_restart = 0; | |
c6d61269 | 1273 | init_waitqueue_head(&priv->dp_flush_wait); |
c6d61269 | 1274 | init_waitqueue_head(&priv->dp_close_wait); |
c4028958 DH |
1275 | INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock); |
1276 | priv->dp_port = serial->port[i]; | |
1da177e4 | 1277 | /* initialize write wait queue for this port */ |
c6d61269 | 1278 | init_waitqueue_head(&serial->port[i]->write_wait); |
1da177e4 LT |
1279 | |
1280 | usb_set_serial_port_data(serial->port[i], priv); | |
1281 | } | |
1282 | ||
1283 | /* allocate serial private structure */ | |
c6d61269 AC |
1284 | serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL); |
1285 | if (serial_priv == NULL) { | |
1286 | for (i = 0; i < serial->type->num_ports + 1; i++) | |
1287 | kfree(usb_get_serial_port_data(serial->port[i])); | |
1288 | return 1; /* error */ | |
1da177e4 LT |
1289 | } |
1290 | ||
1291 | /* initialize serial private structure */ | |
c6d61269 | 1292 | spin_lock_init(&serial_priv->ds_serial_lock); |
1da177e4 LT |
1293 | serial_priv->ds_oob_port_num = serial->type->num_ports; |
1294 | serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num]; | |
1295 | serial_priv->ds_device_started = 0; | |
1296 | usb_set_serial_data(serial, serial_priv); | |
1297 | ||
c6d61269 | 1298 | return 0; |
1da177e4 LT |
1299 | } |
1300 | ||
1301 | ||
f9c99bb8 | 1302 | static void digi_disconnect(struct usb_serial *serial) |
1da177e4 | 1303 | { |
1da177e4 | 1304 | int i; |
1da177e4 LT |
1305 | |
1306 | /* stop reads and writes on all ports */ | |
c6d61269 | 1307 | for (i = 0; i < serial->type->num_ports + 1; i++) { |
1da177e4 LT |
1308 | usb_kill_urb(serial->port[i]->read_urb); |
1309 | usb_kill_urb(serial->port[i]->write_urb); | |
1310 | } | |
f9c99bb8 AS |
1311 | } |
1312 | ||
1313 | ||
1314 | static void digi_release(struct usb_serial *serial) | |
1315 | { | |
1316 | int i; | |
1da177e4 LT |
1317 | |
1318 | /* free the private data structures for all ports */ | |
1319 | /* number of regular ports + 1 for the out-of-band port */ | |
41ad427d | 1320 | for (i = 0; i < serial->type->num_ports + 1; i++) |
c6d61269 AC |
1321 | kfree(usb_get_serial_port_data(serial->port[i])); |
1322 | kfree(usb_get_serial_data(serial)); | |
1da177e4 LT |
1323 | } |
1324 | ||
1325 | ||
c6d61269 | 1326 | static void digi_read_bulk_callback(struct urb *urb) |
1da177e4 | 1327 | { |
cdc97792 | 1328 | struct usb_serial_port *port = urb->context; |
1da177e4 LT |
1329 | struct digi_port *priv; |
1330 | struct digi_serial *serial_priv; | |
1331 | int ret; | |
85d75107 | 1332 | int status = urb->status; |
1da177e4 | 1333 | |
1da177e4 | 1334 | /* port sanity check, do not resubmit if port is not valid */ |
194343d9 GKH |
1335 | if (port == NULL) |
1336 | return; | |
1337 | priv = usb_get_serial_port_data(port); | |
1338 | if (priv == NULL) { | |
1339 | dev_err(&port->dev, "%s: port->private is NULL, status=%d\n", | |
1340 | __func__, status); | |
1da177e4 LT |
1341 | return; |
1342 | } | |
c6d61269 | 1343 | if (port->serial == NULL || |
41ad427d | 1344 | (serial_priv = usb_get_serial_data(port->serial)) == NULL) { |
194343d9 GKH |
1345 | dev_err(&port->dev, "%s: serial is bad or serial->private " |
1346 | "is NULL, status=%d\n", __func__, status); | |
1da177e4 LT |
1347 | return; |
1348 | } | |
1349 | ||
1350 | /* do not resubmit urb if it has any status error */ | |
85d75107 | 1351 | if (status) { |
194343d9 GKH |
1352 | dev_err(&port->dev, |
1353 | "%s: nonzero read bulk status: status=%d, port=%d\n", | |
1354 | __func__, status, priv->dp_port_num); | |
1da177e4 LT |
1355 | return; |
1356 | } | |
1357 | ||
1358 | /* handle oob or inb callback, do not resubmit if error */ | |
c6d61269 AC |
1359 | if (priv->dp_port_num == serial_priv->ds_oob_port_num) { |
1360 | if (digi_read_oob_callback(urb) != 0) | |
1da177e4 LT |
1361 | return; |
1362 | } else { | |
c6d61269 | 1363 | if (digi_read_inb_callback(urb) != 0) |
1da177e4 LT |
1364 | return; |
1365 | } | |
1366 | ||
1367 | /* continue read */ | |
41ad427d | 1368 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
1f87158e | 1369 | if (ret != 0 && ret != -EPERM) { |
194343d9 GKH |
1370 | dev_err(&port->dev, |
1371 | "%s: failed resubmitting urb, ret=%d, port=%d\n", | |
1372 | __func__, ret, priv->dp_port_num); | |
1da177e4 LT |
1373 | } |
1374 | ||
1375 | } | |
1376 | ||
41ad427d AC |
1377 | /* |
1378 | * Digi Read INB Callback | |
1379 | * | |
1380 | * Digi Read INB Callback handles reads on the in band ports, sending | |
1381 | * the data on to the tty subsystem. When called we know port and | |
1382 | * port->private are not NULL and port->serial has been validated. | |
1383 | * It returns 0 if successful, 1 if successful but the port is | |
1384 | * throttled, and -1 if the sanity checks failed. | |
1385 | */ | |
1da177e4 | 1386 | |
c6d61269 | 1387 | static int digi_read_inb_callback(struct urb *urb) |
1da177e4 LT |
1388 | { |
1389 | ||
cdc97792 | 1390 | struct usb_serial_port *port = urb->context; |
4a90f09b | 1391 | struct tty_struct *tty; |
1da177e4 LT |
1392 | struct digi_port *priv = usb_get_serial_port_data(port); |
1393 | int opcode = ((unsigned char *)urb->transfer_buffer)[0]; | |
1394 | int len = ((unsigned char *)urb->transfer_buffer)[1]; | |
85d75107 | 1395 | int port_status = ((unsigned char *)urb->transfer_buffer)[2]; |
41ad427d AC |
1396 | unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3; |
1397 | int flag, throttled; | |
85d75107 | 1398 | int status = urb->status; |
1da177e4 LT |
1399 | |
1400 | /* do not process callbacks on closed ports */ | |
1401 | /* but do continue the read chain */ | |
1f87158e | 1402 | if (urb->status == -ENOENT) |
c6d61269 | 1403 | return 0; |
1da177e4 LT |
1404 | |
1405 | /* short/multiple packet check */ | |
c6d61269 | 1406 | if (urb->actual_length != len + 2) { |
194343d9 | 1407 | dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, " |
50de36f7 | 1408 | "status=%d, port=%d, opcode=%d, len=%d, " |
194343d9 GKH |
1409 | "actual_length=%d, status=%d\n", __func__, status, |
1410 | priv->dp_port_num, opcode, len, urb->actual_length, | |
1411 | port_status); | |
c6d61269 | 1412 | return -1; |
1da177e4 LT |
1413 | } |
1414 | ||
4a90f09b | 1415 | tty = tty_port_tty_get(&port->port); |
c6d61269 | 1416 | spin_lock(&priv->dp_port_lock); |
1da177e4 LT |
1417 | |
1418 | /* check for throttle; if set, do not resubmit read urb */ | |
1419 | /* indicate the read chain needs to be restarted on unthrottle */ | |
1420 | throttled = priv->dp_throttled; | |
c6d61269 | 1421 | if (throttled) |
1da177e4 LT |
1422 | priv->dp_throttle_restart = 1; |
1423 | ||
1424 | /* receive data */ | |
4287341d | 1425 | if (tty && opcode == DIGI_CMD_RECEIVE_DATA) { |
85d75107 | 1426 | /* get flag from port_status */ |
1da177e4 LT |
1427 | flag = 0; |
1428 | ||
1429 | /* overrun is special, not associated with a char */ | |
c6d61269 AC |
1430 | if (port_status & DIGI_OVERRUN_ERROR) |
1431 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); | |
1da177e4 LT |
1432 | |
1433 | /* break takes precedence over parity, */ | |
1434 | /* which takes precedence over framing errors */ | |
c6d61269 | 1435 | if (port_status & DIGI_BREAK_ERROR) |
1da177e4 | 1436 | flag = TTY_BREAK; |
c6d61269 | 1437 | else if (port_status & DIGI_PARITY_ERROR) |
1da177e4 | 1438 | flag = TTY_PARITY; |
c6d61269 | 1439 | else if (port_status & DIGI_FRAMING_ERROR) |
1da177e4 | 1440 | flag = TTY_FRAME; |
1da177e4 | 1441 | |
85d75107 | 1442 | /* data length is len-1 (one byte of len is port_status) */ |
1da177e4 | 1443 | --len; |
c6d61269 | 1444 | if (len > 0) { |
70ced221 JH |
1445 | tty_insert_flip_string_fixed_flag(tty, data, flag, |
1446 | len); | |
c6d61269 | 1447 | tty_flip_buffer_push(tty); |
1da177e4 | 1448 | } |
1da177e4 | 1449 | } |
c6d61269 | 1450 | spin_unlock(&priv->dp_port_lock); |
4a90f09b | 1451 | tty_kref_put(tty); |
1da177e4 | 1452 | |
c6d61269 | 1453 | if (opcode == DIGI_CMD_RECEIVE_DISABLE) |
f9dfbebb | 1454 | dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__); |
c6d61269 | 1455 | else if (opcode != DIGI_CMD_RECEIVE_DATA) |
f9dfbebb | 1456 | dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode); |
1da177e4 | 1457 | |
41ad427d | 1458 | return throttled ? 1 : 0; |
1da177e4 LT |
1459 | |
1460 | } | |
1461 | ||
1462 | ||
41ad427d AC |
1463 | /* |
1464 | * Digi Read OOB Callback | |
1465 | * | |
1466 | * Digi Read OOB Callback handles reads on the out of band port. | |
1467 | * When called we know port and port->private are not NULL and | |
1468 | * the port->serial is valid. It returns 0 if successful, and | |
1469 | * -1 if the sanity checks failed. | |
1470 | */ | |
1da177e4 | 1471 | |
c6d61269 | 1472 | static int digi_read_oob_callback(struct urb *urb) |
1da177e4 LT |
1473 | { |
1474 | ||
cdc97792 | 1475 | struct usb_serial_port *port = urb->context; |
1da177e4 | 1476 | struct usb_serial *serial = port->serial; |
4a90f09b | 1477 | struct tty_struct *tty; |
1da177e4 LT |
1478 | struct digi_port *priv = usb_get_serial_port_data(port); |
1479 | int opcode, line, status, val; | |
1480 | int i; | |
41ad427d | 1481 | unsigned int rts; |
1da177e4 | 1482 | |
1da177e4 | 1483 | /* handle each oob command */ |
41ad427d | 1484 | for (i = 0; i < urb->actual_length - 3;) { |
1da177e4 LT |
1485 | opcode = ((unsigned char *)urb->transfer_buffer)[i++]; |
1486 | line = ((unsigned char *)urb->transfer_buffer)[i++]; | |
1487 | status = ((unsigned char *)urb->transfer_buffer)[i++]; | |
1488 | val = ((unsigned char *)urb->transfer_buffer)[i++]; | |
1489 | ||
f9dfbebb | 1490 | dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n", |
c6d61269 | 1491 | opcode, line, status, val); |
1da177e4 | 1492 | |
c6d61269 | 1493 | if (status != 0 || line >= serial->type->num_ports) |
1da177e4 LT |
1494 | continue; |
1495 | ||
1496 | port = serial->port[line]; | |
1497 | ||
41ad427d AC |
1498 | priv = usb_get_serial_port_data(port); |
1499 | if (priv == NULL) | |
1da177e4 LT |
1500 | return -1; |
1501 | ||
4a90f09b | 1502 | tty = tty_port_tty_get(&port->port); |
4287341d | 1503 | |
41ad427d | 1504 | rts = 0; |
4287341d | 1505 | if (tty) |
adc8d746 | 1506 | rts = tty->termios.c_cflag & CRTSCTS; |
4a90f09b | 1507 | |
4287341d | 1508 | if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) { |
c6d61269 | 1509 | spin_lock(&priv->dp_port_lock); |
1da177e4 | 1510 | /* convert from digi flags to termiox flags */ |
c6d61269 | 1511 | if (val & DIGI_READ_INPUT_SIGNALS_CTS) { |
1da177e4 LT |
1512 | priv->dp_modem_signals |= TIOCM_CTS; |
1513 | /* port must be open to use tty struct */ | |
41ad427d | 1514 | if (rts) { |
4a90f09b | 1515 | tty->hw_stopped = 0; |
c6d61269 | 1516 | digi_wakeup_write(port); |
1da177e4 LT |
1517 | } |
1518 | } else { | |
1519 | priv->dp_modem_signals &= ~TIOCM_CTS; | |
1520 | /* port must be open to use tty struct */ | |
41ad427d | 1521 | if (rts) |
4a90f09b | 1522 | tty->hw_stopped = 1; |
1da177e4 | 1523 | } |
c6d61269 | 1524 | if (val & DIGI_READ_INPUT_SIGNALS_DSR) |
1da177e4 LT |
1525 | priv->dp_modem_signals |= TIOCM_DSR; |
1526 | else | |
1527 | priv->dp_modem_signals &= ~TIOCM_DSR; | |
c6d61269 | 1528 | if (val & DIGI_READ_INPUT_SIGNALS_RI) |
1da177e4 LT |
1529 | priv->dp_modem_signals |= TIOCM_RI; |
1530 | else | |
1531 | priv->dp_modem_signals &= ~TIOCM_RI; | |
c6d61269 | 1532 | if (val & DIGI_READ_INPUT_SIGNALS_DCD) |
1da177e4 LT |
1533 | priv->dp_modem_signals |= TIOCM_CD; |
1534 | else | |
1535 | priv->dp_modem_signals &= ~TIOCM_CD; | |
1536 | ||
c6d61269 AC |
1537 | wake_up_interruptible(&priv->dp_modem_change_wait); |
1538 | spin_unlock(&priv->dp_port_lock); | |
1539 | } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) { | |
1540 | spin_lock(&priv->dp_port_lock); | |
1da177e4 | 1541 | priv->dp_transmit_idle = 1; |
c6d61269 AC |
1542 | wake_up_interruptible(&priv->dp_transmit_idle_wait); |
1543 | spin_unlock(&priv->dp_port_lock); | |
1544 | } else if (opcode == DIGI_CMD_IFLUSH_FIFO) { | |
1545 | wake_up_interruptible(&priv->dp_flush_wait); | |
1da177e4 | 1546 | } |
4a90f09b | 1547 | tty_kref_put(tty); |
1da177e4 | 1548 | } |
c6d61269 | 1549 | return 0; |
1da177e4 LT |
1550 | |
1551 | } | |
1552 | ||
68e24113 | 1553 | module_usb_serial_driver(serial_drivers, id_table_combined); |
1da177e4 | 1554 | |
c6d61269 AC |
1555 | MODULE_AUTHOR(DRIVER_AUTHOR); |
1556 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1da177e4 | 1557 | MODULE_LICENSE("GPL"); |