mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Robert Jarzmik <robert.jarzmik@free.fr>
To: barebox@lists.infradead.org
Cc: Robert Jarzmik <robert.jarzmik@atosorigin.com>
Subject: [PATCH 5/5] usb/gadget: make serial gadget resistant to cable unplug
Date: Thu, 24 Nov 2011 04:01:30 +0100	[thread overview]
Message-ID: <1322103690-32669-6-git-send-email-robert.jarzmik@free.fr> (raw)
In-Reply-To: <1322103690-32669-1-git-send-email-robert.jarzmik@free.fr>

From: Robert Jarzmik <robert.jarzmik@atosorigin.com>

As USB can have its cable plugged out anytime, make the
serial gadget reliable in case of unplugging :
 - unregister the gadget_serial provided console
 - don't stay forever in the read loop if connection was cut

This behaviour relies on correct implementation of
usb_gadget_poll(), which should return an error if the USB
cable was removed.

Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
---
 drivers/usb/gadget/u_serial.c |   24 ++++++++++++++++++------
 1 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index 8c2c8fa..49aedc2 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -367,12 +367,13 @@ static void serial_putc(struct console_device *cdev, char c)
 	struct gs_port	*port = container_of(cdev,
 					struct gs_port, cdev);
 	struct list_head	*pool = &port->write_pool;
-	struct usb_ep		*in = port->port_usb->in;
+	struct usb_ep		*in;
 	struct usb_request	*req;
 	int status;
 
 	if (list_empty(pool))
 		return;
+	in = port->port_usb->in;
 	req = list_entry(pool->next, struct usb_request, list);
 
 	req->length = 1;
@@ -381,8 +382,8 @@ static void serial_putc(struct console_device *cdev, char c)
 	*(unsigned char *)req->buf = c;
 	status = usb_ep_queue(in, req);
 
-	while (list_empty(pool))
-		usb_gadget_poll();
+	while (status >= 0 && list_empty(pool))
+		status = usb_gadget_poll();
 }
 
 static int serial_tstc(struct console_device *cdev)
@@ -399,7 +400,10 @@ static int serial_getc(struct console_device *cdev)
 					struct gs_port, cdev);
 	unsigned char ch;
 
-	while (kfifo_getc(port->recv_fifo, &ch));
+	if (!port->port_usb)
+		return -EIO;
+	while (kfifo_getc(port->recv_fifo, &ch))
+		usb_gadget_poll();
 
 	return ch;
 }
@@ -421,6 +425,10 @@ int gserial_connect(struct gserial *gser, u8 port_num)
 	/* we "know" gserial_cleanup() hasn't been called */
 	port = ports[port_num].port;
 
+	/* In case of multiple activation (ie. multiple SET_INTERFACE) */
+	if (port->port_usb)
+		return 0;
+
 	/* activate the endpoints */
 	status = usb_ep_enable(gser->in, gser->in_desc);
 	if (status < 0)
@@ -498,7 +506,9 @@ BAREBOX_CMD_END
 void gserial_disconnect(struct gserial *gser)
 {
 	struct gs_port	*port = gser->ioport;
-printf("%s\n", __func__);
+	struct console_device *cdev;
+
+	printf("%s\n", __func__);
 	if (!port)
 		return;
 
@@ -518,7 +528,9 @@ printf("%s\n", __func__);
 	gser->in->driver_data = NULL;
 
 	/* finally, free any unused/unusable I/O buffers */
-
 	gs_free_requests(gser->out, &port->read_pool);
 	gs_free_requests(gser->in, &port->write_pool);
+
+	cdev = &port->cdev;
+	console_unregister(cdev);
 }
-- 
1.7.5.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

      parent reply	other threads:[~2011-11-24  3:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-24  3:01 [PATCH 0/5] Enable a console over a serial USB gadget Robert Jarzmik
2011-11-24  3:01 ` [PATCH 1/5] console: add console unregistering Robert Jarzmik
2011-11-24  3:01 ` [PATCH 2/5] usb/gadget: add pxa27x_udc driver Robert Jarzmik
2011-11-24  3:01 ` [PATCH 3/5] usb/gadget: use generic usb_gadget_poll() in u_serial Robert Jarzmik
2011-11-24  3:01 ` [PATCH 4/5] usb/gadget: add USB serial connect and disconnect Robert Jarzmik
2011-11-24  3:01 ` Robert Jarzmik [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1322103690-32669-6-git-send-email-robert.jarzmik@free.fr \
    --to=robert.jarzmik@free.fr \
    --cc=barebox@lists.infradead.org \
    --cc=robert.jarzmik@atosorigin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox