Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * USB Serial Converter Generic functions | |
3 | * | |
4 | * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License version | |
8 | * 2 as published by the Free Software Foundation. | |
9 | * | |
10 | */ | |
11 | ||
1da177e4 LT |
12 | #include <linux/kernel.h> |
13 | #include <linux/errno.h> | |
14 | #include <linux/slab.h> | |
7f0ae3a8 | 15 | #include <linux/sysrq.h> |
1da177e4 LT |
16 | #include <linux/tty.h> |
17 | #include <linux/tty_flip.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/moduleparam.h> | |
20 | #include <linux/usb.h> | |
a969888c | 21 | #include <linux/usb/serial.h> |
ae64387a | 22 | #include <linux/uaccess.h> |
8e8dce06 | 23 | #include <linux/kfifo.h> |
1f87158e | 24 | #include <linux/serial.h> |
d9b1b787 | 25 | |
1da177e4 LT |
26 | static int debug; |
27 | ||
28 | #ifdef CONFIG_USB_SERIAL_GENERIC | |
b46d60fc DB |
29 | |
30 | static int generic_probe(struct usb_interface *interface, | |
31 | const struct usb_device_id *id); | |
32 | ||
1da177e4 LT |
33 | static __u16 vendor = 0x05f9; |
34 | static __u16 product = 0xffff; | |
35 | ||
36 | module_param(vendor, ushort, 0); | |
37 | MODULE_PARM_DESC(vendor, "User specified USB idVendor"); | |
38 | ||
39 | module_param(product, ushort, 0); | |
40 | MODULE_PARM_DESC(product, "User specified USB idProduct"); | |
41 | ||
42 | static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */ | |
43 | ||
d9b1b787 JH |
44 | /* we want to look at all devices, as the vendor/product id can change |
45 | * depending on the command line argument */ | |
7d40d7e8 | 46 | static const struct usb_device_id generic_serial_ids[] = { |
d9b1b787 JH |
47 | {.driver_info = 42}, |
48 | {} | |
49 | }; | |
50 | ||
51 | static struct usb_driver generic_driver = { | |
52 | .name = "usbserial_generic", | |
53 | .probe = generic_probe, | |
54 | .disconnect = usb_serial_disconnect, | |
55 | .id_table = generic_serial_ids, | |
56 | .no_dynamic_id = 1, | |
57 | }; | |
58 | ||
1da177e4 | 59 | /* All of the device info needed for the Generic Serial Converter */ |
ea65370d | 60 | struct usb_serial_driver usb_serial_generic_device = { |
18fcac35 GKH |
61 | .driver = { |
62 | .owner = THIS_MODULE, | |
269bda1c | 63 | .name = "generic", |
18fcac35 | 64 | }, |
1da177e4 | 65 | .id_table = generic_device_ids, |
d9b1b787 | 66 | .usb_driver = &generic_driver, |
1da177e4 | 67 | .num_ports = 1, |
f9c99bb8 AS |
68 | .disconnect = usb_serial_generic_disconnect, |
69 | .release = usb_serial_generic_release, | |
253ca923 JR |
70 | .throttle = usb_serial_generic_throttle, |
71 | .unthrottle = usb_serial_generic_unthrottle, | |
ec22559e | 72 | .resume = usb_serial_generic_resume, |
1da177e4 LT |
73 | }; |
74 | ||
1da177e4 LT |
75 | static int generic_probe(struct usb_interface *interface, |
76 | const struct usb_device_id *id) | |
77 | { | |
78 | const struct usb_device_id *id_pattern; | |
79 | ||
80 | id_pattern = usb_match_id(interface, generic_device_ids); | |
81 | if (id_pattern != NULL) | |
82 | return usb_serial_probe(interface, id); | |
83 | return -ENODEV; | |
84 | } | |
1da177e4 LT |
85 | #endif |
86 | ||
ae64387a | 87 | int usb_serial_generic_register(int _debug) |
1da177e4 LT |
88 | { |
89 | int retval = 0; | |
90 | ||
91 | debug = _debug; | |
92 | #ifdef CONFIG_USB_SERIAL_GENERIC | |
93 | generic_device_ids[0].idVendor = vendor; | |
94 | generic_device_ids[0].idProduct = product; | |
ae64387a AC |
95 | generic_device_ids[0].match_flags = |
96 | USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; | |
1da177e4 LT |
97 | |
98 | /* register our generic driver with ourselves */ | |
ae64387a | 99 | retval = usb_serial_register(&usb_serial_generic_device); |
1da177e4 LT |
100 | if (retval) |
101 | goto exit; | |
102 | retval = usb_register(&generic_driver); | |
103 | if (retval) | |
104 | usb_serial_deregister(&usb_serial_generic_device); | |
105 | exit: | |
106 | #endif | |
107 | return retval; | |
108 | } | |
109 | ||
ae64387a | 110 | void usb_serial_generic_deregister(void) |
1da177e4 LT |
111 | { |
112 | #ifdef CONFIG_USB_SERIAL_GENERIC | |
113 | /* remove our generic driver */ | |
114 | usb_deregister(&generic_driver); | |
ae64387a | 115 | usb_serial_deregister(&usb_serial_generic_device); |
1da177e4 LT |
116 | #endif |
117 | } | |
118 | ||
a509a7e4 | 119 | int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) |
1da177e4 LT |
120 | { |
121 | struct usb_serial *serial = port->serial; | |
122 | int result = 0; | |
253ca923 | 123 | unsigned long flags; |
1da177e4 | 124 | |
441b62c1 | 125 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 126 | |
253ca923 JR |
127 | /* clear the throttle flags */ |
128 | spin_lock_irqsave(&port->lock, flags); | |
129 | port->throttled = 0; | |
130 | port->throttle_req = 0; | |
131 | spin_unlock_irqrestore(&port->lock, flags); | |
132 | ||
133 | /* if we have a bulk endpoint, start reading from it */ | |
eb8878a8 | 134 | if (port->bulk_in_size) { |
1da177e4 | 135 | /* Start reading from the device */ |
ae64387a AC |
136 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
137 | usb_rcvbulkpipe(serial->dev, | |
138 | port->bulk_in_endpointAddress), | |
1da177e4 LT |
139 | port->read_urb->transfer_buffer, |
140 | port->read_urb->transfer_buffer_length, | |
141 | ((serial->type->read_bulk_callback) ? | |
142 | serial->type->read_bulk_callback : | |
143 | usb_serial_generic_read_bulk_callback), | |
144 | port); | |
145 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); | |
146 | if (result) | |
ae64387a AC |
147 | dev_err(&port->dev, |
148 | "%s - failed resubmitting read urb, error %d\n", | |
149 | __func__, result); | |
1da177e4 LT |
150 | } |
151 | ||
152 | return result; | |
153 | } | |
815ddc99 | 154 | EXPORT_SYMBOL_GPL(usb_serial_generic_open); |
1da177e4 | 155 | |
95da310e | 156 | static void generic_cleanup(struct usb_serial_port *port) |
1da177e4 LT |
157 | { |
158 | struct usb_serial *serial = port->serial; | |
159 | ||
441b62c1 | 160 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
161 | |
162 | if (serial->dev) { | |
eb8878a8 JH |
163 | /* shutdown any bulk transfers that might be going on */ |
164 | if (port->bulk_out_size) | |
1da177e4 | 165 | usb_kill_urb(port->write_urb); |
eb8878a8 | 166 | if (port->bulk_in_size) |
1da177e4 LT |
167 | usb_kill_urb(port->read_urb); |
168 | } | |
169 | } | |
170 | ||
335f8514 | 171 | void usb_serial_generic_close(struct usb_serial_port *port) |
1da177e4 | 172 | { |
441b62c1 | 173 | dbg("%s - port %d", __func__, port->number); |
ae64387a | 174 | generic_cleanup(port); |
1da177e4 LT |
175 | } |
176 | ||
715b1dc0 JW |
177 | static int usb_serial_multi_urb_write(struct tty_struct *tty, |
178 | struct usb_serial_port *port, const unsigned char *buf, int count) | |
179 | { | |
180 | unsigned long flags; | |
181 | struct urb *urb; | |
182 | unsigned char *buffer; | |
183 | int status; | |
184 | int towrite; | |
185 | int bwrite = 0; | |
186 | ||
187 | dbg("%s - port %d", __func__, port->number); | |
188 | ||
189 | if (count == 0) | |
190 | dbg("%s - write request of 0 bytes", __func__); | |
191 | ||
192 | while (count > 0) { | |
193 | towrite = (count > port->bulk_out_size) ? | |
194 | port->bulk_out_size : count; | |
195 | spin_lock_irqsave(&port->lock, flags); | |
196 | if (port->urbs_in_flight > | |
197 | port->serial->type->max_in_flight_urbs) { | |
198 | spin_unlock_irqrestore(&port->lock, flags); | |
759f3634 | 199 | dbg("%s - write limit hit", __func__); |
715b1dc0 JW |
200 | return bwrite; |
201 | } | |
202 | port->tx_bytes_flight += towrite; | |
203 | port->urbs_in_flight++; | |
204 | spin_unlock_irqrestore(&port->lock, flags); | |
205 | ||
206 | buffer = kmalloc(towrite, GFP_ATOMIC); | |
207 | if (!buffer) { | |
208 | dev_err(&port->dev, | |
209 | "%s ran out of kernel memory for urb ...\n", __func__); | |
210 | goto error_no_buffer; | |
211 | } | |
212 | ||
213 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
214 | if (!urb) { | |
215 | dev_err(&port->dev, "%s - no more free urbs\n", | |
216 | __func__); | |
217 | goto error_no_urb; | |
218 | } | |
219 | ||
220 | /* Copy data */ | |
221 | memcpy(buffer, buf + bwrite, towrite); | |
222 | usb_serial_debug_data(debug, &port->dev, __func__, | |
223 | towrite, buffer); | |
224 | /* fill the buffer and send it */ | |
225 | usb_fill_bulk_urb(urb, port->serial->dev, | |
226 | usb_sndbulkpipe(port->serial->dev, | |
227 | port->bulk_out_endpointAddress), | |
228 | buffer, towrite, | |
229 | usb_serial_generic_write_bulk_callback, port); | |
230 | ||
231 | status = usb_submit_urb(urb, GFP_ATOMIC); | |
232 | if (status) { | |
233 | dev_err(&port->dev, | |
234 | "%s - failed submitting write urb, error %d\n", | |
235 | __func__, status); | |
236 | goto error; | |
237 | } | |
238 | ||
239 | /* This urb is the responsibility of the host driver now */ | |
240 | usb_free_urb(urb); | |
241 | dbg("%s write: %d", __func__, towrite); | |
242 | count -= towrite; | |
243 | bwrite += towrite; | |
244 | } | |
245 | return bwrite; | |
246 | ||
247 | error: | |
248 | usb_free_urb(urb); | |
249 | error_no_urb: | |
250 | kfree(buffer); | |
251 | error_no_buffer: | |
252 | spin_lock_irqsave(&port->lock, flags); | |
253 | port->urbs_in_flight--; | |
254 | port->tx_bytes_flight -= towrite; | |
255 | spin_unlock_irqrestore(&port->lock, flags); | |
256 | return bwrite; | |
257 | } | |
258 | ||
8e8dce06 DV |
259 | /** |
260 | * usb_serial_generic_write_start - kick off an URB write | |
261 | * @port: Pointer to the &struct usb_serial_port data | |
262 | * | |
263 | * Returns the number of bytes queued on success. This will be zero if there | |
264 | * was nothing to send. Otherwise, it returns a negative errno value | |
265 | */ | |
266 | static int usb_serial_generic_write_start(struct usb_serial_port *port) | |
267 | { | |
268 | struct usb_serial *serial = port->serial; | |
269 | unsigned char *data; | |
270 | int result; | |
271 | int count; | |
272 | unsigned long flags; | |
273 | bool start_io; | |
274 | ||
275 | /* Atomically determine whether we can and need to start a USB | |
276 | * operation. */ | |
277 | spin_lock_irqsave(&port->lock, flags); | |
278 | if (port->write_urb_busy) | |
279 | start_io = false; | |
280 | else { | |
119eecc8 | 281 | start_io = (kfifo_len(&port->write_fifo) != 0); |
8e8dce06 DV |
282 | port->write_urb_busy = start_io; |
283 | } | |
284 | spin_unlock_irqrestore(&port->lock, flags); | |
285 | ||
286 | if (!start_io) | |
287 | return 0; | |
288 | ||
289 | data = port->write_urb->transfer_buffer; | |
119eecc8 | 290 | count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock); |
8e8dce06 DV |
291 | usb_serial_debug_data(debug, &port->dev, __func__, count, data); |
292 | ||
293 | /* set up our urb */ | |
294 | usb_fill_bulk_urb(port->write_urb, serial->dev, | |
295 | usb_sndbulkpipe(serial->dev, | |
296 | port->bulk_out_endpointAddress), | |
297 | port->write_urb->transfer_buffer, count, | |
298 | ((serial->type->write_bulk_callback) ? | |
299 | serial->type->write_bulk_callback : | |
300 | usb_serial_generic_write_bulk_callback), | |
301 | port); | |
302 | ||
303 | /* send the data out the bulk port */ | |
304 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); | |
305 | if (result) { | |
306 | dev_err(&port->dev, | |
307 | "%s - failed submitting write urb, error %d\n", | |
308 | __func__, result); | |
309 | /* don't have to grab the lock here, as we will | |
310 | retry if != 0 */ | |
311 | port->write_urb_busy = 0; | |
312 | } else | |
313 | result = count; | |
314 | ||
315 | return result; | |
316 | } | |
317 | ||
318 | /** | |
319 | * usb_serial_generic_write - generic write function for serial USB devices | |
320 | * @tty: Pointer to &struct tty_struct for the device | |
321 | * @port: Pointer to the &usb_serial_port structure for the device | |
322 | * @buf: Pointer to the data to write | |
323 | * @count: Number of bytes to write | |
324 | * | |
325 | * Returns the number of characters actually written, which may be anything | |
326 | * from zero to @count. If an error occurs, it returns the negative errno | |
327 | * value. | |
328 | */ | |
95da310e AC |
329 | int usb_serial_generic_write(struct tty_struct *tty, |
330 | struct usb_serial_port *port, const unsigned char *buf, int count) | |
1da177e4 LT |
331 | { |
332 | struct usb_serial *serial = port->serial; | |
333 | int result; | |
1da177e4 | 334 | |
441b62c1 | 335 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 336 | |
eb8878a8 JH |
337 | /* only do something if we have a bulk out endpoint */ |
338 | if (!port->bulk_out_size) | |
339 | return -ENODEV; | |
340 | ||
1da177e4 | 341 | if (count == 0) { |
441b62c1 | 342 | dbg("%s - write request of 0 bytes", __func__); |
ae64387a | 343 | return 0; |
1da177e4 LT |
344 | } |
345 | ||
8e8dce06 DV |
346 | if (serial->type->max_in_flight_urbs) |
347 | return usb_serial_multi_urb_write(tty, port, | |
348 | buf, count); | |
1da177e4 | 349 | |
119eecc8 | 350 | count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); |
8e8dce06 | 351 | result = usb_serial_generic_write_start(port); |
1da177e4 | 352 | |
8e8dce06 DV |
353 | if (result >= 0) |
354 | result = count; | |
1da177e4 | 355 | |
8e8dce06 | 356 | return result; |
1da177e4 | 357 | } |
98fcb5f7 | 358 | EXPORT_SYMBOL_GPL(usb_serial_generic_write); |
1da177e4 | 359 | |
ae64387a | 360 | int usb_serial_generic_write_room(struct tty_struct *tty) |
1da177e4 | 361 | { |
95da310e | 362 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 363 | struct usb_serial *serial = port->serial; |
715b1dc0 | 364 | unsigned long flags; |
1da177e4 LT |
365 | int room = 0; |
366 | ||
441b62c1 | 367 | dbg("%s - port %d", __func__, port->number); |
eb8878a8 JH |
368 | |
369 | if (!port->bulk_out_size) | |
370 | return 0; | |
371 | ||
715b1dc0 JW |
372 | spin_lock_irqsave(&port->lock, flags); |
373 | if (serial->type->max_in_flight_urbs) { | |
374 | if (port->urbs_in_flight < serial->type->max_in_flight_urbs) | |
98fcb5f7 JW |
375 | room = port->bulk_out_size * |
376 | (serial->type->max_in_flight_urbs - | |
377 | port->urbs_in_flight); | |
eb8878a8 | 378 | } else { |
119eecc8 | 379 | room = kfifo_avail(&port->write_fifo); |
eb8878a8 | 380 | } |
715b1dc0 | 381 | spin_unlock_irqrestore(&port->lock, flags); |
1da177e4 | 382 | |
441b62c1 | 383 | dbg("%s - returns %d", __func__, room); |
a5b6f60c | 384 | return room; |
1da177e4 LT |
385 | } |
386 | ||
95da310e | 387 | int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) |
1da177e4 | 388 | { |
95da310e | 389 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 | 390 | struct usb_serial *serial = port->serial; |
715b1dc0 | 391 | unsigned long flags; |
eb8878a8 | 392 | int chars; |
1da177e4 | 393 | |
441b62c1 | 394 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 395 | |
eb8878a8 JH |
396 | if (!port->bulk_out_size) |
397 | return 0; | |
398 | ||
25719e6b SS |
399 | spin_lock_irqsave(&port->lock, flags); |
400 | if (serial->type->max_in_flight_urbs) | |
715b1dc0 | 401 | chars = port->tx_bytes_flight; |
eb8878a8 | 402 | else |
119eecc8 | 403 | chars = kfifo_len(&port->write_fifo); |
25719e6b | 404 | spin_unlock_irqrestore(&port->lock, flags); |
1da177e4 | 405 | |
441b62c1 | 406 | dbg("%s - returns %d", __func__, chars); |
95da310e | 407 | return chars; |
1da177e4 LT |
408 | } |
409 | ||
1abdeeb1 | 410 | |
98fcb5f7 JW |
411 | void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port, |
412 | gfp_t mem_flags) | |
1da177e4 | 413 | { |
253ca923 | 414 | struct urb *urb = port->read_urb; |
1abdeeb1 | 415 | struct usb_serial *serial = port->serial; |
1da177e4 LT |
416 | int result; |
417 | ||
253ca923 | 418 | /* Continue reading from device */ |
ae64387a AC |
419 | usb_fill_bulk_urb(urb, serial->dev, |
420 | usb_rcvbulkpipe(serial->dev, | |
421 | port->bulk_in_endpointAddress), | |
1abdeeb1 ON |
422 | urb->transfer_buffer, |
423 | urb->transfer_buffer_length, | |
ae64387a AC |
424 | ((serial->type->read_bulk_callback) ? |
425 | serial->type->read_bulk_callback : | |
1da177e4 | 426 | usb_serial_generic_read_bulk_callback), port); |
0ae14743 | 427 | |
1abdeeb1 | 428 | result = usb_submit_urb(urb, mem_flags); |
0ae14743 | 429 | if (result && result != -EPERM) { |
ae64387a AC |
430 | dev_err(&port->dev, |
431 | "%s - failed resubmitting read urb, error %d\n", | |
432 | __func__, result); | |
0ae14743 | 433 | } |
1da177e4 | 434 | } |
98fcb5f7 | 435 | EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb); |
253ca923 | 436 | |
1abdeeb1 | 437 | /* Push data to tty layer and resubmit the bulk read URB */ |
95da310e | 438 | static void flush_and_resubmit_read_urb(struct usb_serial_port *port) |
1abdeeb1 ON |
439 | { |
440 | struct urb *urb = port->read_urb; | |
4a90f09b | 441 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
98fcb5f7 JW |
442 | char *ch = (char *)urb->transfer_buffer; |
443 | int i; | |
444 | ||
445 | if (!tty) | |
446 | goto done; | |
1abdeeb1 | 447 | |
4cd1de0a AC |
448 | /* The per character mucking around with sysrq path it too slow for |
449 | stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases | |
450 | where the USB serial is not a console anyway */ | |
bd5afa9e | 451 | if (!port->port.console || !port->sysrq) |
4cd1de0a AC |
452 | tty_insert_flip_string(tty, ch, urb->actual_length); |
453 | else { | |
454 | /* Push data to tty */ | |
455 | for (i = 0; i < urb->actual_length; i++, ch++) { | |
24a15a62 | 456 | if (!usb_serial_handle_sysrq_char(tty, port, *ch)) |
4cd1de0a AC |
457 | tty_insert_flip_char(tty, *ch, TTY_NORMAL); |
458 | } | |
1abdeeb1 | 459 | } |
98fcb5f7 | 460 | tty_flip_buffer_push(tty); |
4a90f09b | 461 | tty_kref_put(tty); |
98fcb5f7 JW |
462 | done: |
463 | usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC); | |
1abdeeb1 ON |
464 | } |
465 | ||
95da310e | 466 | void usb_serial_generic_read_bulk_callback(struct urb *urb) |
253ca923 | 467 | { |
cdc97792 | 468 | struct usb_serial_port *port = urb->context; |
253ca923 | 469 | unsigned char *data = urb->transfer_buffer; |
fbd27225 | 470 | int status = urb->status; |
bfaeafcf | 471 | unsigned long flags; |
253ca923 | 472 | |
441b62c1 | 473 | dbg("%s - port %d", __func__, port->number); |
253ca923 | 474 | |
fbd27225 GKH |
475 | if (unlikely(status != 0)) { |
476 | dbg("%s - nonzero read bulk status received: %d", | |
441b62c1 | 477 | __func__, status); |
253ca923 JR |
478 | return; |
479 | } | |
480 | ||
ae64387a AC |
481 | usb_serial_debug_data(debug, &port->dev, __func__, |
482 | urb->actual_length, data); | |
253ca923 JR |
483 | |
484 | /* Throttle the device if requested by tty */ | |
bfaeafcf | 485 | spin_lock_irqsave(&port->lock, flags); |
ae64387a AC |
486 | port->throttled = port->throttle_req; |
487 | if (!port->throttled) { | |
b507cc97 | 488 | spin_unlock_irqrestore(&port->lock, flags); |
1abdeeb1 | 489 | flush_and_resubmit_read_urb(port); |
ae64387a | 490 | } else |
b507cc97 | 491 | spin_unlock_irqrestore(&port->lock, flags); |
253ca923 | 492 | } |
166ffccf | 493 | EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); |
1da177e4 | 494 | |
95da310e | 495 | void usb_serial_generic_write_bulk_callback(struct urb *urb) |
1da177e4 | 496 | { |
715b1dc0 | 497 | unsigned long flags; |
cdc97792 | 498 | struct usb_serial_port *port = urb->context; |
fbd27225 | 499 | int status = urb->status; |
1da177e4 | 500 | |
441b62c1 | 501 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 502 | |
715b1dc0 | 503 | if (port->serial->type->max_in_flight_urbs) { |
25915302 JH |
504 | kfree(urb->transfer_buffer); |
505 | ||
715b1dc0 JW |
506 | spin_lock_irqsave(&port->lock, flags); |
507 | --port->urbs_in_flight; | |
508 | port->tx_bytes_flight -= urb->transfer_buffer_length; | |
509 | if (port->urbs_in_flight < 0) | |
510 | port->urbs_in_flight = 0; | |
511 | spin_unlock_irqrestore(&port->lock, flags); | |
512 | } else { | |
715b1dc0 | 513 | port->write_urb_busy = 0; |
715b1dc0 | 514 | |
63136202 | 515 | if (status) |
119eecc8 | 516 | kfifo_reset_out(&port->write_fifo); |
63136202 | 517 | else |
8e8dce06 | 518 | usb_serial_generic_write_start(port); |
1da177e4 | 519 | } |
8e8dce06 | 520 | |
63136202 JH |
521 | if (status) |
522 | dbg("%s - non-zero urb status: %d", __func__, status); | |
523 | ||
cf2c7481 | 524 | usb_serial_port_softint(port); |
1da177e4 | 525 | } |
bb833986 | 526 | EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); |
1da177e4 | 527 | |
95da310e | 528 | void usb_serial_generic_throttle(struct tty_struct *tty) |
253ca923 | 529 | { |
95da310e | 530 | struct usb_serial_port *port = tty->driver_data; |
253ca923 JR |
531 | unsigned long flags; |
532 | ||
441b62c1 | 533 | dbg("%s - port %d", __func__, port->number); |
253ca923 JR |
534 | |
535 | /* Set the throttle request flag. It will be picked up | |
536 | * by usb_serial_generic_read_bulk_callback(). */ | |
537 | spin_lock_irqsave(&port->lock, flags); | |
538 | port->throttle_req = 1; | |
539 | spin_unlock_irqrestore(&port->lock, flags); | |
540 | } | |
541 | ||
95da310e | 542 | void usb_serial_generic_unthrottle(struct tty_struct *tty) |
253ca923 | 543 | { |
95da310e | 544 | struct usb_serial_port *port = tty->driver_data; |
253ca923 JR |
545 | int was_throttled; |
546 | unsigned long flags; | |
547 | ||
441b62c1 | 548 | dbg("%s - port %d", __func__, port->number); |
253ca923 JR |
549 | |
550 | /* Clear the throttle flags */ | |
551 | spin_lock_irqsave(&port->lock, flags); | |
552 | was_throttled = port->throttled; | |
553 | port->throttled = port->throttle_req = 0; | |
554 | spin_unlock_irqrestore(&port->lock, flags); | |
555 | ||
556 | if (was_throttled) { | |
1abdeeb1 | 557 | /* Resume reading from device */ |
63a96095 | 558 | flush_and_resubmit_read_urb(port); |
253ca923 JR |
559 | } |
560 | } | |
561 | ||
7f0ae3a8 | 562 | #ifdef CONFIG_MAGIC_SYSRQ |
24a15a62 AC |
563 | int usb_serial_handle_sysrq_char(struct tty_struct *tty, |
564 | struct usb_serial_port *port, unsigned int ch) | |
98fcb5f7 | 565 | { |
bd5afa9e | 566 | if (port->sysrq && port->port.console) { |
98fcb5f7 | 567 | if (ch && time_before(jiffies, port->sysrq)) { |
24a15a62 | 568 | handle_sysrq(ch, tty); |
98fcb5f7 JW |
569 | port->sysrq = 0; |
570 | return 1; | |
571 | } | |
572 | port->sysrq = 0; | |
573 | } | |
574 | return 0; | |
575 | } | |
7f0ae3a8 RD |
576 | #else |
577 | int usb_serial_handle_sysrq_char(struct tty_struct *tty, | |
578 | struct usb_serial_port *port, unsigned int ch) | |
579 | { | |
580 | return 0; | |
581 | } | |
582 | #endif | |
98fcb5f7 JW |
583 | EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); |
584 | ||
585 | int usb_serial_handle_break(struct usb_serial_port *port) | |
586 | { | |
587 | if (!port->sysrq) { | |
588 | port->sysrq = jiffies + HZ*5; | |
589 | return 1; | |
590 | } | |
591 | port->sysrq = 0; | |
592 | return 0; | |
593 | } | |
594 | EXPORT_SYMBOL_GPL(usb_serial_handle_break); | |
595 | ||
8e8dce06 DV |
596 | int usb_serial_generic_resume(struct usb_serial *serial) |
597 | { | |
598 | struct usb_serial_port *port; | |
599 | int i, c = 0, r; | |
600 | ||
601 | for (i = 0; i < serial->num_ports; i++) { | |
602 | port = serial->port[i]; | |
1f87158e | 603 | if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) |
8e8dce06 DV |
604 | continue; |
605 | ||
606 | if (port->read_urb) { | |
607 | r = usb_submit_urb(port->read_urb, GFP_NOIO); | |
608 | if (r < 0) | |
609 | c++; | |
610 | } | |
611 | ||
612 | if (port->write_urb) { | |
613 | r = usb_serial_generic_write_start(port); | |
614 | if (r < 0) | |
615 | c++; | |
616 | } | |
617 | } | |
618 | ||
619 | return c ? -EIO : 0; | |
620 | } | |
621 | EXPORT_SYMBOL_GPL(usb_serial_generic_resume); | |
622 | ||
f9c99bb8 | 623 | void usb_serial_generic_disconnect(struct usb_serial *serial) |
1da177e4 LT |
624 | { |
625 | int i; | |
626 | ||
441b62c1 | 627 | dbg("%s", __func__); |
1da177e4 LT |
628 | |
629 | /* stop reads and writes on all ports */ | |
ae64387a | 630 | for (i = 0; i < serial->num_ports; ++i) |
1da177e4 | 631 | generic_cleanup(serial->port[i]); |
1da177e4 LT |
632 | } |
633 | ||
f9c99bb8 AS |
634 | void usb_serial_generic_release(struct usb_serial *serial) |
635 | { | |
636 | dbg("%s", __func__); | |
637 | } |