Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * USB Serial Converter Generic functions | |
3 | * | |
659597b7 | 4 | * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com) |
1da177e4 LT |
5 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/kernel.h> |
14 | #include <linux/errno.h> | |
15 | #include <linux/slab.h> | |
7f0ae3a8 | 16 | #include <linux/sysrq.h> |
1da177e4 LT |
17 | #include <linux/tty.h> |
18 | #include <linux/tty_flip.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/moduleparam.h> | |
21 | #include <linux/usb.h> | |
a969888c | 22 | #include <linux/usb/serial.h> |
ae64387a | 23 | #include <linux/uaccess.h> |
8e8dce06 | 24 | #include <linux/kfifo.h> |
1f87158e | 25 | #include <linux/serial.h> |
d9b1b787 | 26 | |
1da177e4 | 27 | #ifdef CONFIG_USB_SERIAL_GENERIC |
b46d60fc | 28 | |
1da177e4 LT |
29 | static __u16 vendor = 0x05f9; |
30 | static __u16 product = 0xffff; | |
31 | ||
32 | module_param(vendor, ushort, 0); | |
33 | MODULE_PARM_DESC(vendor, "User specified USB idVendor"); | |
34 | ||
35 | module_param(product, ushort, 0); | |
36 | MODULE_PARM_DESC(product, "User specified USB idProduct"); | |
37 | ||
38 | static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */ | |
39 | ||
40 | /* All of the device info needed for the Generic Serial Converter */ | |
ea65370d | 41 | struct usb_serial_driver usb_serial_generic_device = { |
18fcac35 GKH |
42 | .driver = { |
43 | .owner = THIS_MODULE, | |
269bda1c | 44 | .name = "generic", |
18fcac35 | 45 | }, |
1da177e4 | 46 | .id_table = generic_device_ids, |
1da177e4 | 47 | .num_ports = 1, |
253ca923 JR |
48 | .throttle = usb_serial_generic_throttle, |
49 | .unthrottle = usb_serial_generic_unthrottle, | |
ec22559e | 50 | .resume = usb_serial_generic_resume, |
1da177e4 LT |
51 | }; |
52 | ||
765e0ba6 AS |
53 | static struct usb_serial_driver * const serial_drivers[] = { |
54 | &usb_serial_generic_device, NULL | |
55 | }; | |
56 | ||
1da177e4 LT |
57 | #endif |
58 | ||
3033bc8d | 59 | int usb_serial_generic_register(void) |
1da177e4 LT |
60 | { |
61 | int retval = 0; | |
62 | ||
1da177e4 LT |
63 | #ifdef CONFIG_USB_SERIAL_GENERIC |
64 | generic_device_ids[0].idVendor = vendor; | |
65 | generic_device_ids[0].idProduct = product; | |
ae64387a AC |
66 | generic_device_ids[0].match_flags = |
67 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; | |
1da177e4 LT |
68 | |
69 | /* register our generic driver with ourselves */ | |
0b84704a AS |
70 | retval = usb_serial_register_drivers(serial_drivers, |
71 | "usbserial_generic", generic_device_ids); | |
1da177e4 LT |
72 | #endif |
73 | return retval; | |
74 | } | |
75 | ||
ae64387a | 76 | void usb_serial_generic_deregister(void) |
1da177e4 LT |
77 | { |
78 | #ifdef CONFIG_USB_SERIAL_GENERIC | |
79 | /* remove our generic driver */ | |
68e24113 | 80 | usb_serial_deregister_drivers(serial_drivers); |
1da177e4 LT |
81 | #endif |
82 | } | |
83 | ||
a509a7e4 | 84 | int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) |
1da177e4 | 85 | { |
1da177e4 | 86 | int result = 0; |
253ca923 | 87 | unsigned long flags; |
1da177e4 | 88 | |
253ca923 JR |
89 | /* clear the throttle flags */ |
90 | spin_lock_irqsave(&port->lock, flags); | |
91 | port->throttled = 0; | |
92 | port->throttle_req = 0; | |
93 | spin_unlock_irqrestore(&port->lock, flags); | |
94 | ||
95 | /* if we have a bulk endpoint, start reading from it */ | |
41bd72f9 | 96 | if (port->bulk_in_size) |
d83b4053 | 97 | result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL); |
1da177e4 LT |
98 | |
99 | return result; | |
100 | } | |
815ddc99 | 101 | EXPORT_SYMBOL_GPL(usb_serial_generic_open); |
1da177e4 | 102 | |
f8f0ad86 | 103 | void usb_serial_generic_close(struct usb_serial_port *port) |
1da177e4 | 104 | { |
ec3ee508 | 105 | unsigned long flags; |
27c7acf2 | 106 | int i; |
1da177e4 | 107 | |
19c61853 JH |
108 | if (port->bulk_out_size) { |
109 | for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) | |
110 | usb_kill_urb(port->write_urbs[i]); | |
111 | ||
112 | spin_lock_irqsave(&port->lock, flags); | |
113 | kfifo_reset_out(&port->write_fifo); | |
114 | spin_unlock_irqrestore(&port->lock, flags); | |
115 | } | |
116 | if (port->bulk_in_size) { | |
117 | for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) | |
118 | usb_kill_urb(port->read_urbs[i]); | |
1da177e4 LT |
119 | } |
120 | } | |
f26788da | 121 | EXPORT_SYMBOL_GPL(usb_serial_generic_close); |
1da177e4 | 122 | |
eaa3bcb0 | 123 | int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port, |
c23e5fc1 | 124 | void *dest, size_t size) |
eaa3bcb0 | 125 | { |
c23e5fc1 | 126 | return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock); |
715b1dc0 JW |
127 | } |
128 | ||
8e8dce06 DV |
129 | /** |
130 | * usb_serial_generic_write_start - kick off an URB write | |
131 | * @port: Pointer to the &struct usb_serial_port data | |
132 | * | |
27c7acf2 | 133 | * Returns zero on success, or a negative errno value |
8e8dce06 DV |
134 | */ |
135 | static int usb_serial_generic_write_start(struct usb_serial_port *port) | |
136 | { | |
27c7acf2 JH |
137 | struct urb *urb; |
138 | int count, result; | |
8e8dce06 | 139 | unsigned long flags; |
27c7acf2 | 140 | int i; |
8e8dce06 | 141 | |
27c7acf2 JH |
142 | if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags)) |
143 | return 0; | |
144 | retry: | |
8e8dce06 | 145 | spin_lock_irqsave(&port->lock, flags); |
27c7acf2 JH |
146 | if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) { |
147 | clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); | |
50a5f70c JH |
148 | spin_unlock_irqrestore(&port->lock, flags); |
149 | return 0; | |
8e8dce06 | 150 | } |
27c7acf2 JH |
151 | i = (int)find_first_bit(&port->write_urbs_free, |
152 | ARRAY_SIZE(port->write_urbs)); | |
8e8dce06 DV |
153 | spin_unlock_irqrestore(&port->lock, flags); |
154 | ||
27c7acf2 | 155 | urb = port->write_urbs[i]; |
eaa3bcb0 | 156 | count = port->serial->type->prepare_write_buffer(port, |
c23e5fc1 JH |
157 | urb->transfer_buffer, |
158 | port->bulk_out_size); | |
27c7acf2 | 159 | urb->transfer_buffer_length = count; |
59d33f2f | 160 | usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer); |
b58af406 JH |
161 | spin_lock_irqsave(&port->lock, flags); |
162 | port->tx_bytes += count; | |
163 | spin_unlock_irqrestore(&port->lock, flags); | |
164 | ||
165 | clear_bit(i, &port->write_urbs_free); | |
27c7acf2 | 166 | result = usb_submit_urb(urb, GFP_ATOMIC); |
8e8dce06 | 167 | if (result) { |
f1475a00 | 168 | dev_err_console(port, "%s - error submitting urb: %d\n", |
8e8dce06 | 169 | __func__, result); |
b58af406 JH |
170 | set_bit(i, &port->write_urbs_free); |
171 | spin_lock_irqsave(&port->lock, flags); | |
172 | port->tx_bytes -= count; | |
173 | spin_unlock_irqrestore(&port->lock, flags); | |
174 | ||
27c7acf2 | 175 | clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); |
30af7fb5 JH |
176 | return result; |
177 | } | |
30af7fb5 | 178 | |
27c7acf2 JH |
179 | /* Try sending off another urb, unless in irq context (in which case |
180 | * there will be no free urb). */ | |
181 | if (!in_irq()) | |
182 | goto retry; | |
183 | ||
184 | clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags); | |
185 | ||
186 | return 0; | |
8e8dce06 DV |
187 | } |
188 | ||
189 | /** | |
190 | * usb_serial_generic_write - generic write function for serial USB devices | |
191 | * @tty: Pointer to &struct tty_struct for the device | |
192 | * @port: Pointer to the &usb_serial_port structure for the device | |
193 | * @buf: Pointer to the data to write | |
194 | * @count: Number of bytes to write | |
195 | * | |
196 | * Returns the number of characters actually written, which may be anything | |
197 | * from zero to @count. If an error occurs, it returns the negative errno | |
198 | * value. | |
199 | */ | |
95da310e AC |
200 | int usb_serial_generic_write(struct tty_struct *tty, |
201 | struct usb_serial_port *port, const unsigned char *buf, int count) | |
1da177e4 | 202 | { |
1da177e4 | 203 | int result; |
1da177e4 | 204 | |
eb8878a8 JH |
205 | /* only do something if we have a bulk out endpoint */ |
206 | if (!port->bulk_out_size) | |
207 | return -ENODEV; | |
208 | ||
1a1405e2 | 209 | if (!count) |
ae64387a | 210 | return 0; |
1da177e4 | 211 | |
119eecc8 | 212 | count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); |
8e8dce06 | 213 | result = usb_serial_generic_write_start(port); |
c23e5fc1 JH |
214 | if (result) |
215 | return result; | |
1da177e4 | 216 | |
c23e5fc1 | 217 | return count; |
1da177e4 | 218 | } |
98fcb5f7 | 219 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); |
1da177e4 | 220 | |
ae64387a | 221 | int usb_serial_generic_write_room(struct tty_struct *tty) |
1da177e4 | 222 | { |
95da310e | 223 | struct usb_serial_port *port = tty->driver_data; |
715b1dc0 | 224 | unsigned long flags; |
25d514ca | 225 | int room; |
1da177e4 | 226 | |
eb8878a8 JH |
227 | if (!port->bulk_out_size) |
228 | return 0; | |
229 | ||
715b1dc0 | 230 | spin_lock_irqsave(&port->lock, flags); |
c23e5fc1 | 231 | room = kfifo_avail(&port->write_fifo); |
715b1dc0 | 232 | spin_unlock_irqrestore(&port->lock, flags); |
1da177e4 | 233 | |
689c2781 | 234 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); |
a5b6f60c | 235 | return room; |
1da177e4 LT |
236 | } |
237 | ||
95da310e | 238 | int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) |
1da177e4 | 239 | { |
95da310e | 240 | struct usb_serial_port *port = tty->driver_data; |
715b1dc0 | 241 | unsigned long flags; |
eb8878a8 | 242 | int chars; |
1da177e4 | 243 | |
eb8878a8 JH |
244 | if (!port->bulk_out_size) |
245 | return 0; | |
246 | ||
25719e6b | 247 | spin_lock_irqsave(&port->lock, flags); |
c23e5fc1 | 248 | chars = kfifo_len(&port->write_fifo) + port->tx_bytes; |
25719e6b | 249 | spin_unlock_irqrestore(&port->lock, flags); |
1da177e4 | 250 | |
689c2781 | 251 | dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); |
95da310e | 252 | return chars; |
1da177e4 | 253 | } |
428d9988 | 254 | EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer); |
1da177e4 | 255 | |
dcf01050 JH |
256 | void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout) |
257 | { | |
258 | struct usb_serial_port *port = tty->driver_data; | |
259 | unsigned int bps; | |
260 | unsigned long period; | |
261 | unsigned long expire; | |
262 | ||
263 | bps = tty_get_baud_rate(tty); | |
264 | if (!bps) | |
265 | bps = 9600; /* B0 */ | |
266 | /* | |
267 | * Use a poll-period of roughly the time it takes to send one | |
268 | * character or at least one jiffy. | |
269 | */ | |
270 | period = max_t(unsigned long, (10 * HZ / bps), 1); | |
271 | period = min_t(unsigned long, period, timeout); | |
272 | ||
273 | dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n", | |
274 | __func__, jiffies_to_msecs(timeout), | |
275 | jiffies_to_msecs(period)); | |
276 | expire = jiffies + timeout; | |
277 | while (!port->serial->type->tx_empty(port)) { | |
278 | schedule_timeout_interruptible(period); | |
279 | if (signal_pending(current)) | |
280 | break; | |
281 | if (time_after(jiffies, expire)) | |
282 | break; | |
283 | } | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent); | |
286 | ||
d83b4053 JH |
287 | static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port, |
288 | int index, gfp_t mem_flags) | |
289 | { | |
290 | int res; | |
291 | ||
292 | if (!test_and_clear_bit(index, &port->read_urbs_free)) | |
293 | return 0; | |
294 | ||
49bd196d | 295 | dev_dbg(&port->dev, "%s - urb %d\n", __func__, index); |
d83b4053 JH |
296 | |
297 | res = usb_submit_urb(port->read_urbs[index], mem_flags); | |
298 | if (res) { | |
299 | if (res != -EPERM) { | |
300 | dev_err(&port->dev, | |
301 | "%s - usb_submit_urb failed: %d\n", | |
302 | __func__, res); | |
303 | } | |
304 | set_bit(index, &port->read_urbs_free); | |
305 | return res; | |
306 | } | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port, | |
41bd72f9 | 312 | gfp_t mem_flags) |
1da177e4 | 313 | { |
d83b4053 JH |
314 | int res; |
315 | int i; | |
1da177e4 | 316 | |
d83b4053 JH |
317 | for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) { |
318 | res = usb_serial_generic_submit_read_urb(port, i, mem_flags); | |
319 | if (res) | |
320 | goto err; | |
0ae14743 | 321 | } |
d83b4053 JH |
322 | |
323 | return 0; | |
324 | err: | |
325 | for (; i >= 0; --i) | |
326 | usb_kill_urb(port->read_urbs[i]); | |
327 | ||
328 | return res; | |
1da177e4 | 329 | } |
d83b4053 | 330 | EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs); |
253ca923 | 331 | |
23154320 | 332 | void usb_serial_generic_process_read_urb(struct urb *urb) |
1abdeeb1 | 333 | { |
0f3d5bae | 334 | struct usb_serial_port *port = urb->context; |
98fcb5f7 JW |
335 | char *ch = (char *)urb->transfer_buffer; |
336 | int i; | |
337 | ||
56a1df46 JH |
338 | if (!urb->actual_length) |
339 | return; | |
340 | ||
4cd1de0a AC |
341 | /* The per character mucking around with sysrq path it too slow for |
342 | stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases | |
343 | where the USB serial is not a console anyway */ | |
bd5afa9e | 344 | if (!port->port.console || !port->sysrq) |
05c7cd39 | 345 | tty_insert_flip_string(&port->port, ch, urb->actual_length); |
4cd1de0a | 346 | else { |
4cd1de0a | 347 | for (i = 0; i < urb->actual_length; i++, ch++) { |
6ee9f4b4 | 348 | if (!usb_serial_handle_sysrq_char(port, *ch)) |
92a19f9c | 349 | tty_insert_flip_char(&port->port, *ch, TTY_NORMAL); |
4cd1de0a | 350 | } |
1abdeeb1 | 351 | } |
2e124b4a | 352 | tty_flip_buffer_push(&port->port); |
1abdeeb1 | 353 | } |
23154320 | 354 | EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb); |
1abdeeb1 | 355 | |
95da310e | 356 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
253ca923 | 357 | { |
cdc97792 | 358 | struct usb_serial_port *port = urb->context; |
253ca923 | 359 | unsigned char *data = urb->transfer_buffer; |
bfaeafcf | 360 | unsigned long flags; |
d83b4053 | 361 | int i; |
253ca923 | 362 | |
d83b4053 JH |
363 | for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) { |
364 | if (urb == port->read_urbs[i]) | |
365 | break; | |
366 | } | |
367 | set_bit(i, &port->read_urbs_free); | |
253ca923 | 368 | |
49bd196d JH |
369 | dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i, |
370 | urb->actual_length); | |
689c2781 | 371 | |
d83b4053 | 372 | if (urb->status) { |
689c2781 GKH |
373 | dev_dbg(&port->dev, "%s - non-zero urb status: %d\n", |
374 | __func__, urb->status); | |
253ca923 JR |
375 | return; |
376 | } | |
377 | ||
59d33f2f | 378 | usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); |
23154320 | 379 | port->serial->type->process_read_urb(urb); |
253ca923 JR |
380 | |
381 | /* Throttle the device if requested by tty */ | |
bfaeafcf | 382 | spin_lock_irqsave(&port->lock, flags); |
ae64387a AC |
383 | port->throttled = port->throttle_req; |
384 | if (!port->throttled) { | |
b507cc97 | 385 | spin_unlock_irqrestore(&port->lock, flags); |
d83b4053 | 386 | usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC); |
ae64387a | 387 | } else |
b507cc97 | 388 | spin_unlock_irqrestore(&port->lock, flags); |
253ca923 | 389 | } |
166ffccf | 390 | EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); |
1da177e4 | 391 | |
95da310e | 392 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
1da177e4 | 393 | { |
715b1dc0 | 394 | unsigned long flags; |
cdc97792 | 395 | struct usb_serial_port *port = urb->context; |
fbd27225 | 396 | int status = urb->status; |
27c7acf2 | 397 | int i; |
1da177e4 | 398 | |
c23e5fc1 JH |
399 | for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) |
400 | if (port->write_urbs[i] == urb) | |
401 | break; | |
25915302 | 402 | |
c23e5fc1 JH |
403 | spin_lock_irqsave(&port->lock, flags); |
404 | port->tx_bytes -= urb->transfer_buffer_length; | |
405 | set_bit(i, &port->write_urbs_free); | |
406 | spin_unlock_irqrestore(&port->lock, flags); | |
407 | ||
408 | if (status) { | |
689c2781 GKH |
409 | dev_dbg(&port->dev, "%s - non-zero urb status: %d\n", |
410 | __func__, status); | |
27c7acf2 | 411 | |
30af7fb5 | 412 | spin_lock_irqsave(&port->lock, flags); |
c23e5fc1 | 413 | kfifo_reset_out(&port->write_fifo); |
30af7fb5 | 414 | spin_unlock_irqrestore(&port->lock, flags); |
c23e5fc1 JH |
415 | } else { |
416 | usb_serial_generic_write_start(port); | |
1da177e4 | 417 | } |
8e8dce06 | 418 | |
cf2c7481 | 419 | usb_serial_port_softint(port); |
1da177e4 | 420 | } |
bb833986 | 421 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
1da177e4 | 422 | |
95da310e | 423 | void usb_serial_generic_throttle(struct tty_struct *tty) |
253ca923 | 424 | { |
95da310e | 425 | struct usb_serial_port *port = tty->driver_data; |
253ca923 JR |
426 | unsigned long flags; |
427 | ||
253ca923 JR |
428 | /* Set the throttle request flag. It will be picked up |
429 | * by usb_serial_generic_read_bulk_callback(). */ | |
430 | spin_lock_irqsave(&port->lock, flags); | |
431 | port->throttle_req = 1; | |
432 | spin_unlock_irqrestore(&port->lock, flags); | |
433 | } | |
f1e949ac | 434 | EXPORT_SYMBOL_GPL(usb_serial_generic_throttle); |
253ca923 | 435 | |
95da310e | 436 | void usb_serial_generic_unthrottle(struct tty_struct *tty) |
253ca923 | 437 | { |
95da310e | 438 | struct usb_serial_port *port = tty->driver_data; |
253ca923 | 439 | int was_throttled; |
253ca923 | 440 | |
253ca923 | 441 | /* Clear the throttle flags */ |
0f3d5bae | 442 | spin_lock_irq(&port->lock); |
253ca923 JR |
443 | was_throttled = port->throttled; |
444 | port->throttled = port->throttle_req = 0; | |
0f3d5bae | 445 | spin_unlock_irq(&port->lock); |
253ca923 | 446 | |
0f3d5bae | 447 | if (was_throttled) |
d83b4053 | 448 | usb_serial_generic_submit_read_urbs(port, GFP_KERNEL); |
253ca923 | 449 | } |
f1e949ac | 450 | EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle); |
253ca923 | 451 | |
980373b7 JH |
452 | static bool usb_serial_generic_msr_changed(struct tty_struct *tty, |
453 | unsigned long arg, struct async_icount *cprev) | |
454 | { | |
455 | struct usb_serial_port *port = tty->driver_data; | |
456 | struct async_icount cnow; | |
457 | unsigned long flags; | |
458 | bool ret; | |
459 | ||
460 | /* | |
461 | * Use tty-port initialised flag to detect all hangups including the | |
462 | * one generated at USB-device disconnect. | |
980373b7 | 463 | */ |
980373b7 JH |
464 | if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
465 | return true; | |
466 | ||
467 | spin_lock_irqsave(&port->lock, flags); | |
468 | cnow = port->icount; /* atomic copy*/ | |
469 | spin_unlock_irqrestore(&port->lock, flags); | |
470 | ||
471 | ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) || | |
472 | ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) || | |
473 | ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) || | |
474 | ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts)); | |
475 | ||
476 | *cprev = cnow; | |
477 | ||
478 | return ret; | |
479 | } | |
480 | ||
481 | int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg) | |
482 | { | |
483 | struct usb_serial_port *port = tty->driver_data; | |
484 | struct async_icount cnow; | |
485 | unsigned long flags; | |
486 | int ret; | |
487 | ||
488 | spin_lock_irqsave(&port->lock, flags); | |
489 | cnow = port->icount; /* atomic copy */ | |
490 | spin_unlock_irqrestore(&port->lock, flags); | |
491 | ||
492 | ret = wait_event_interruptible(port->port.delta_msr_wait, | |
493 | usb_serial_generic_msr_changed(tty, arg, &cnow)); | |
91c4211c JH |
494 | if (!ret && !test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
495 | ret = -EIO; | |
980373b7 JH |
496 | |
497 | return ret; | |
498 | } | |
499 | EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait); | |
500 | ||
befefcda JH |
501 | int usb_serial_generic_get_icount(struct tty_struct *tty, |
502 | struct serial_icounter_struct *icount) | |
503 | { | |
504 | struct usb_serial_port *port = tty->driver_data; | |
505 | struct async_icount cnow; | |
506 | unsigned long flags; | |
507 | ||
508 | spin_lock_irqsave(&port->lock, flags); | |
509 | cnow = port->icount; /* atomic copy */ | |
510 | spin_unlock_irqrestore(&port->lock, flags); | |
511 | ||
512 | icount->cts = cnow.cts; | |
513 | icount->dsr = cnow.dsr; | |
514 | icount->rng = cnow.rng; | |
515 | icount->dcd = cnow.dcd; | |
516 | icount->tx = cnow.tx; | |
517 | icount->rx = cnow.rx; | |
518 | icount->frame = cnow.frame; | |
519 | icount->parity = cnow.parity; | |
520 | icount->overrun = cnow.overrun; | |
521 | icount->brk = cnow.brk; | |
522 | icount->buf_overrun = cnow.buf_overrun; | |
523 | ||
524 | return 0; | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount); | |
527 | ||
7f0ae3a8 | 528 | #ifdef CONFIG_MAGIC_SYSRQ |
6ee9f4b4 | 529 | int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch) |
98fcb5f7 | 530 | { |
bd5afa9e | 531 | if (port->sysrq && port->port.console) { |
98fcb5f7 | 532 | if (ch && time_before(jiffies, port->sysrq)) { |
f335397d | 533 | handle_sysrq(ch); |
98fcb5f7 JW |
534 | port->sysrq = 0; |
535 | return 1; | |
536 | } | |
537 | port->sysrq = 0; | |
538 | } | |
539 | return 0; | |
540 | } | |
7f0ae3a8 | 541 | #else |
6ee9f4b4 | 542 | int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch) |
7f0ae3a8 RD |
543 | { |
544 | return 0; | |
545 | } | |
546 | #endif | |
98fcb5f7 JW |
547 | EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); |
548 | ||
549 | int usb_serial_handle_break(struct usb_serial_port *port) | |
550 | { | |
551 | if (!port->sysrq) { | |
552 | port->sysrq = jiffies + HZ*5; | |
553 | return 1; | |
554 | } | |
555 | port->sysrq = 0; | |
556 | return 0; | |
557 | } | |
558 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); | |
559 | ||
d14fc1a7 LP |
560 | /** |
561 | * usb_serial_handle_dcd_change - handle a change of carrier detect state | |
562 | * @port: usb_serial_port structure for the open port | |
563 | * @tty: tty_struct structure for the port | |
564 | * @status: new carrier detect status, nonzero if active | |
565 | */ | |
566 | void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port, | |
567 | struct tty_struct *tty, unsigned int status) | |
568 | { | |
569 | struct tty_port *port = &usb_port->port; | |
570 | ||
49bd196d | 571 | dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status); |
d14fc1a7 LP |
572 | |
573 | if (status) | |
574 | wake_up_interruptible(&port->open_wait); | |
575 | else if (tty && !C_CLOCAL(tty)) | |
576 | tty_hangup(tty); | |
577 | } | |
578 | EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change); | |
579 | ||
8e8dce06 DV |
580 | int usb_serial_generic_resume(struct usb_serial *serial) |
581 | { | |
582 | struct usb_serial_port *port; | |
583 | int i, c = 0, r; | |
584 | ||
585 | for (i = 0; i < serial->num_ports; i++) { | |
586 | port = serial->port[i]; | |
1f87158e | 587 | if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
8e8dce06 DV |
588 | continue; |
589 | ||
d83b4053 JH |
590 | if (port->bulk_in_size) { |
591 | r = usb_serial_generic_submit_read_urbs(port, | |
592 | GFP_NOIO); | |
8e8dce06 DV |
593 | if (r < 0) |
594 | c++; | |
595 | } | |
596 | ||
27c7acf2 | 597 | if (port->bulk_out_size) { |
8e8dce06 DV |
598 | r = usb_serial_generic_write_start(port); |
599 | if (r < 0) | |
600 | c++; | |
601 | } | |
602 | } | |
603 | ||
604 | return c ? -EIO : 0; | |
605 | } | |
606 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); |