Skip to content

Commit a00e718

Browse files
committed
USB: serial: opticon: add chars_in_buffer() implementation
Add a chars_in_buffer() implementation so that the tty layer will wait for outgoing buffered data to be drained when needed (e.g. on final close()). Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Johan Hovold <[email protected]>
1 parent b3a987b commit a00e718

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

drivers/usb/serial/opticon.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct opticon_private {
4141
bool rts;
4242
bool cts;
4343
int outstanding_urbs;
44+
int outstanding_bytes;
4445
};
4546

4647

@@ -169,6 +170,7 @@ static void opticon_write_control_callback(struct urb *urb)
169170

170171
spin_lock_irqsave(&priv->lock, flags);
171172
--priv->outstanding_urbs;
173+
priv->outstanding_bytes -= urb->transfer_buffer_length;
172174
spin_unlock_irqrestore(&priv->lock, flags);
173175

174176
usb_serial_port_softint(port);
@@ -182,8 +184,8 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
182184
struct urb *urb;
183185
unsigned char *buffer;
184186
unsigned long flags;
185-
int status;
186187
struct usb_ctrlrequest *dr;
188+
int ret = -ENOMEM;
187189

188190
spin_lock_irqsave(&priv->lock, flags);
189191
if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
@@ -192,19 +194,16 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
192194
return 0;
193195
}
194196
priv->outstanding_urbs++;
197+
priv->outstanding_bytes += count;
195198
spin_unlock_irqrestore(&priv->lock, flags);
196199

197200
buffer = kmalloc(count, GFP_ATOMIC);
198-
if (!buffer) {
199-
count = -ENOMEM;
201+
if (!buffer)
200202
goto error_no_buffer;
201-
}
202203

203204
urb = usb_alloc_urb(0, GFP_ATOMIC);
204-
if (!urb) {
205-
count = -ENOMEM;
205+
if (!urb)
206206
goto error_no_urb;
207-
}
208207

209208
memcpy(buffer, buf, count);
210209

@@ -213,10 +212,8 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
213212
/* The connected devices do not have a bulk write endpoint,
214213
* to transmit data to de barcode device the control endpoint is used */
215214
dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
216-
if (!dr) {
217-
count = -ENOMEM;
215+
if (!dr)
218216
goto error_no_dr;
219-
}
220217

221218
dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
222219
dr->bRequest = 0x01;
@@ -230,12 +227,9 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
230227
opticon_write_control_callback, port);
231228

232229
/* send it down the pipe */
233-
status = usb_submit_urb(urb, GFP_ATOMIC);
234-
if (status) {
235-
dev_err(&port->dev,
236-
"%s - usb_submit_urb(write endpoint) failed status = %d\n",
237-
__func__, status);
238-
count = status;
230+
ret = usb_submit_urb(urb, GFP_ATOMIC);
231+
if (ret) {
232+
dev_err(&port->dev, "failed to submit write urb: %d\n", ret);
239233
goto error;
240234
}
241235

@@ -253,8 +247,10 @@ static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
253247
error_no_buffer:
254248
spin_lock_irqsave(&priv->lock, flags);
255249
--priv->outstanding_urbs;
250+
priv->outstanding_bytes -= count;
256251
spin_unlock_irqrestore(&priv->lock, flags);
257-
return count;
252+
253+
return ret;
258254
}
259255

260256
static int opticon_write_room(struct tty_struct *tty)
@@ -279,6 +275,20 @@ static int opticon_write_room(struct tty_struct *tty)
279275
return 2048;
280276
}
281277

278+
static int opticon_chars_in_buffer(struct tty_struct *tty)
279+
{
280+
struct usb_serial_port *port = tty->driver_data;
281+
struct opticon_private *priv = usb_get_serial_port_data(port);
282+
unsigned long flags;
283+
int count;
284+
285+
spin_lock_irqsave(&priv->lock, flags);
286+
count = priv->outstanding_bytes;
287+
spin_unlock_irqrestore(&priv->lock, flags);
288+
289+
return count;
290+
}
291+
282292
static int opticon_tiocmget(struct tty_struct *tty)
283293
{
284294
struct usb_serial_port *port = tty->driver_data;
@@ -383,6 +393,7 @@ static struct usb_serial_driver opticon_device = {
383393
.open = opticon_open,
384394
.write = opticon_write,
385395
.write_room = opticon_write_room,
396+
.chars_in_buffer = opticon_chars_in_buffer,
386397
.throttle = usb_serial_generic_throttle,
387398
.unthrottle = usb_serial_generic_unthrottle,
388399
.get_serial = get_serial_info,

0 commit comments

Comments
 (0)