aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/serial.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/serial.c')
-rw-r--r--drivers/usb/gadget/serial.c553
1 files changed, 263 insertions, 290 deletions
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index 54cdd6f94034..00da3f6620a3 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -14,7 +14,6 @@
14 * This software is distributed under the terms of the GNU General 14 * This software is distributed under the terms of the GNU General
15 * Public License ("GPL") as published by the Free Software Foundation, 15 * Public License ("GPL") as published by the Free Software Foundation,
16 * either version 2 of that License or (at your option) any later version. 16 * either version 2 of that License or (at your option) any later version.
17 *
18 */ 17 */
19 18
20#include <linux/kernel.h> 19#include <linux/kernel.h>
@@ -41,7 +40,11 @@
41#define GS_MAJOR 127 40#define GS_MAJOR 127
42#define GS_MINOR_START 0 41#define GS_MINOR_START 0
43 42
44#define GS_NUM_PORTS 16 43/* REVISIT only one port is supported for now;
44 * see gs_{send,recv}_packet() ... no multiplexing,
45 * and no support for multiple ACM devices.
46 */
47#define GS_NUM_PORTS 1
45 48
46#define GS_NUM_CONFIGS 1 49#define GS_NUM_CONFIGS 1
47#define GS_NO_CONFIG_ID 0 50#define GS_NO_CONFIG_ID 0
@@ -65,6 +68,9 @@
65 68
66#define GS_DEFAULT_USE_ACM 0 69#define GS_DEFAULT_USE_ACM 0
67 70
71/* 9600-8-N-1 ... matches init_termios.c_cflag and defaults
72 * expected by "usbser.sys" on MS-Windows.
73 */
68#define GS_DEFAULT_DTE_RATE 9600 74#define GS_DEFAULT_DTE_RATE 9600
69#define GS_DEFAULT_DATA_BITS 8 75#define GS_DEFAULT_DATA_BITS 8
70#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY 76#define GS_DEFAULT_PARITY USB_CDC_NO_PARITY
@@ -107,10 +113,6 @@ static int debug = 1;
107#define GS_NOTIFY_MAXPACKET 8 113#define GS_NOTIFY_MAXPACKET 8
108 114
109 115
110/* Structures */
111
112struct gs_dev;
113
114/* circular buffer */ 116/* circular buffer */
115struct gs_buf { 117struct gs_buf {
116 unsigned int buf_size; 118 unsigned int buf_size;
@@ -164,26 +166,7 @@ struct gs_dev {
164 166
165/* Functions */ 167/* Functions */
166 168
167/* module */ 169/* tty driver internals */
168static int __init gs_module_init(void);
169static void __exit gs_module_exit(void);
170
171/* tty driver */
172static int gs_open(struct tty_struct *tty, struct file *file);
173static void gs_close(struct tty_struct *tty, struct file *file);
174static int gs_write(struct tty_struct *tty,
175 const unsigned char *buf, int count);
176static int gs_put_char(struct tty_struct *tty, unsigned char ch);
177static void gs_flush_chars(struct tty_struct *tty);
178static int gs_write_room(struct tty_struct *tty);
179static int gs_chars_in_buffer(struct tty_struct *tty);
180static void gs_throttle(struct tty_struct * tty);
181static void gs_unthrottle(struct tty_struct * tty);
182static void gs_break(struct tty_struct *tty, int break_state);
183static int gs_ioctl(struct tty_struct *tty, struct file *file,
184 unsigned int cmd, unsigned long arg);
185static void gs_set_termios(struct tty_struct *tty, struct ktermios *old);
186
187static int gs_send(struct gs_dev *dev); 170static int gs_send(struct gs_dev *dev);
188static int gs_send_packet(struct gs_dev *dev, char *packet, 171static int gs_send_packet(struct gs_dev *dev, char *packet,
189 unsigned int size); 172 unsigned int size);
@@ -192,19 +175,7 @@ static int gs_recv_packet(struct gs_dev *dev, char *packet,
192static void gs_read_complete(struct usb_ep *ep, struct usb_request *req); 175static void gs_read_complete(struct usb_ep *ep, struct usb_request *req);
193static void gs_write_complete(struct usb_ep *ep, struct usb_request *req); 176static void gs_write_complete(struct usb_ep *ep, struct usb_request *req);
194 177
195/* gadget driver */ 178/* gadget driver internals */
196static int gs_bind(struct usb_gadget *gadget);
197static void gs_unbind(struct usb_gadget *gadget);
198static int gs_setup(struct usb_gadget *gadget,
199 const struct usb_ctrlrequest *ctrl);
200static int gs_setup_standard(struct usb_gadget *gadget,
201 const struct usb_ctrlrequest *ctrl);
202static int gs_setup_class(struct usb_gadget *gadget,
203 const struct usb_ctrlrequest *ctrl);
204static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req);
205static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
206 struct usb_request *req);
207static void gs_disconnect(struct usb_gadget *gadget);
208static int gs_set_config(struct gs_dev *dev, unsigned config); 179static int gs_set_config(struct gs_dev *dev, unsigned config);
209static void gs_reset_config(struct gs_dev *dev); 180static void gs_reset_config(struct gs_dev *dev);
210static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, 181static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
@@ -232,9 +203,6 @@ static unsigned int gs_buf_put(struct gs_buf *gb, const char *buf,
232static unsigned int gs_buf_get(struct gs_buf *gb, char *buf, 203static unsigned int gs_buf_get(struct gs_buf *gb, char *buf,
233 unsigned int count); 204 unsigned int count);
234 205
235/* external functions */
236extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
237
238 206
239/* Globals */ 207/* Globals */
240 208
@@ -246,48 +214,8 @@ static const char *EP_NOTIFY_NAME;
246 214
247static struct mutex gs_open_close_lock[GS_NUM_PORTS]; 215static struct mutex gs_open_close_lock[GS_NUM_PORTS];
248 216
249static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
250static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
251
252static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
253
254static unsigned int use_acm = GS_DEFAULT_USE_ACM;
255
256
257/* tty driver struct */
258static const struct tty_operations gs_tty_ops = {
259 .open = gs_open,
260 .close = gs_close,
261 .write = gs_write,
262 .put_char = gs_put_char,
263 .flush_chars = gs_flush_chars,
264 .write_room = gs_write_room,
265 .ioctl = gs_ioctl,
266 .set_termios = gs_set_termios,
267 .throttle = gs_throttle,
268 .unthrottle = gs_unthrottle,
269 .break_ctl = gs_break,
270 .chars_in_buffer = gs_chars_in_buffer,
271};
272static struct tty_driver *gs_tty_driver;
273
274/* gadget driver struct */
275static struct usb_gadget_driver gs_gadget_driver = {
276#ifdef CONFIG_USB_GADGET_DUALSPEED
277 .speed = USB_SPEED_HIGH,
278#else
279 .speed = USB_SPEED_FULL,
280#endif /* CONFIG_USB_GADGET_DUALSPEED */
281 .function = GS_LONG_NAME,
282 .bind = gs_bind,
283 .unbind = gs_unbind,
284 .setup = gs_setup,
285 .disconnect = gs_disconnect,
286 .driver = {
287 .name = GS_SHORT_NAME,
288 },
289};
290 217
218/*-------------------------------------------------------------------------*/
291 219
292/* USB descriptors */ 220/* USB descriptors */
293 221
@@ -521,6 +449,8 @@ static const struct usb_descriptor_header *gs_acm_highspeed_function[] = {
521}; 449};
522 450
523 451
452/*-------------------------------------------------------------------------*/
453
524/* Module */ 454/* Module */
525MODULE_DESCRIPTION(GS_LONG_NAME); 455MODULE_DESCRIPTION(GS_LONG_NAME);
526MODULE_AUTHOR("Al Borchers"); 456MODULE_AUTHOR("Al Borchers");
@@ -531,84 +461,23 @@ module_param(debug, int, S_IRUGO|S_IWUSR);
531MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on"); 461MODULE_PARM_DESC(debug, "Enable debugging, 0=off, 1=on");
532#endif 462#endif
533 463
464static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
534module_param(read_q_size, uint, S_IRUGO); 465module_param(read_q_size, uint, S_IRUGO);
535MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32"); 466MODULE_PARM_DESC(read_q_size, "Read request queue size, default=32");
536 467
468static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
537module_param(write_q_size, uint, S_IRUGO); 469module_param(write_q_size, uint, S_IRUGO);
538MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32"); 470MODULE_PARM_DESC(write_q_size, "Write request queue size, default=32");
539 471
472static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
540module_param(write_buf_size, uint, S_IRUGO); 473module_param(write_buf_size, uint, S_IRUGO);
541MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192"); 474MODULE_PARM_DESC(write_buf_size, "Write buffer size, default=8192");
542 475
476static unsigned int use_acm = GS_DEFAULT_USE_ACM;
543module_param(use_acm, uint, S_IRUGO); 477module_param(use_acm, uint, S_IRUGO);
544MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); 478MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
545 479
546module_init(gs_module_init); 480/*-------------------------------------------------------------------------*/
547module_exit(gs_module_exit);
548
549/*
550* gs_module_init
551*
552* Register as a USB gadget driver and a tty driver.
553*/
554static int __init gs_module_init(void)
555{
556 int i;
557 int retval;
558
559 retval = usb_gadget_register_driver(&gs_gadget_driver);
560 if (retval) {
561 pr_err("gs_module_init: cannot register gadget driver, "
562 "ret=%d\n", retval);
563 return retval;
564 }
565
566 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
567 if (!gs_tty_driver)
568 return -ENOMEM;
569 gs_tty_driver->owner = THIS_MODULE;
570 gs_tty_driver->driver_name = GS_SHORT_NAME;
571 gs_tty_driver->name = "ttygs";
572 gs_tty_driver->major = GS_MAJOR;
573 gs_tty_driver->minor_start = GS_MINOR_START;
574 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
575 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
576 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
577 gs_tty_driver->init_termios = tty_std_termios;
578 gs_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
579 tty_set_operations(gs_tty_driver, &gs_tty_ops);
580
581 for (i=0; i < GS_NUM_PORTS; i++)
582 mutex_init(&gs_open_close_lock[i]);
583
584 retval = tty_register_driver(gs_tty_driver);
585 if (retval) {
586 usb_gadget_unregister_driver(&gs_gadget_driver);
587 put_tty_driver(gs_tty_driver);
588 pr_err("gs_module_init: cannot register tty driver, "
589 "ret=%d\n", retval);
590 return retval;
591 }
592
593 pr_info("gs_module_init: %s %s loaded\n",
594 GS_LONG_NAME, GS_VERSION_STR);
595 return 0;
596}
597
598/*
599* gs_module_exit
600*
601* Unregister as a tty driver and a USB gadget driver.
602*/
603static void __exit gs_module_exit(void)
604{
605 tty_unregister_driver(gs_tty_driver);
606 put_tty_driver(gs_tty_driver);
607 usb_gadget_unregister_driver(&gs_gadget_driver);
608
609 pr_info("gs_module_exit: %s %s unloaded\n",
610 GS_LONG_NAME, GS_VERSION_STR);
611}
612 481
613/* TTY Driver */ 482/* TTY Driver */
614 483
@@ -753,15 +622,15 @@ exit_unlock_dev:
753 * gs_close 622 * gs_close
754 */ 623 */
755 624
756#define GS_WRITE_FINISHED_EVENT_SAFELY(p) \ 625static int gs_write_finished_event_safely(struct gs_port *p)
757({ \ 626{
758 int cond; \ 627 int cond;
759 \ 628
760 spin_lock_irq(&(p)->port_lock); \ 629 spin_lock_irq(&(p)->port_lock);
761 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf); \ 630 cond = !(p)->port_dev || !gs_buf_data_avail((p)->port_write_buf);
762 spin_unlock_irq(&(p)->port_lock); \ 631 spin_unlock_irq(&(p)->port_lock);
763 cond; \ 632 return cond;
764}) 633}
765 634
766static void gs_close(struct tty_struct *tty, struct file *file) 635static void gs_close(struct tty_struct *tty, struct file *file)
767{ 636{
@@ -807,7 +676,7 @@ static void gs_close(struct tty_struct *tty, struct file *file)
807 if (gs_buf_data_avail(port->port_write_buf) > 0) { 676 if (gs_buf_data_avail(port->port_write_buf) > 0) {
808 spin_unlock_irq(&port->port_lock); 677 spin_unlock_irq(&port->port_lock);
809 wait_event_interruptible_timeout(port->port_write_wait, 678 wait_event_interruptible_timeout(port->port_write_wait,
810 GS_WRITE_FINISHED_EVENT_SAFELY(port), 679 gs_write_finished_event_safely(port),
811 GS_CLOSE_TIMEOUT * HZ); 680 GS_CLOSE_TIMEOUT * HZ);
812 spin_lock_irq(&port->port_lock); 681 spin_lock_irq(&port->port_lock);
813 } 682 }
@@ -1065,6 +934,23 @@ static void gs_set_termios(struct tty_struct *tty, struct ktermios *old)
1065{ 934{
1066} 935}
1067 936
937static const struct tty_operations gs_tty_ops = {
938 .open = gs_open,
939 .close = gs_close,
940 .write = gs_write,
941 .put_char = gs_put_char,
942 .flush_chars = gs_flush_chars,
943 .write_room = gs_write_room,
944 .ioctl = gs_ioctl,
945 .set_termios = gs_set_termios,
946 .throttle = gs_throttle,
947 .unthrottle = gs_unthrottle,
948 .break_ctl = gs_break,
949 .chars_in_buffer = gs_chars_in_buffer,
950};
951
952/*-------------------------------------------------------------------------*/
953
1068/* 954/*
1069* gs_send 955* gs_send
1070* 956*
@@ -1328,9 +1214,44 @@ requeue:
1328 } 1214 }
1329} 1215}
1330 1216
1217/*-------------------------------------------------------------------------*/
1218
1331/* Gadget Driver */ 1219/* Gadget Driver */
1332 1220
1333/* 1221/*
1222 * gs_unbind
1223 *
1224 * Called on module unload. Frees the control request and device
1225 * structure.
1226 */
1227static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1228{
1229 struct gs_dev *dev = get_gadget_data(gadget);
1230
1231 gs_device = NULL;
1232
1233 /* read/write requests already freed, only control request remains */
1234 if (dev != NULL) {
1235 if (dev->dev_ctrl_req != NULL) {
1236 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1237 dev->dev_ctrl_req = NULL;
1238 }
1239 gs_free_ports(dev);
1240 if (dev->dev_notify_ep)
1241 usb_ep_disable(dev->dev_notify_ep);
1242 if (dev->dev_in_ep)
1243 usb_ep_disable(dev->dev_in_ep);
1244 if (dev->dev_out_ep)
1245 usb_ep_disable(dev->dev_out_ep);
1246 kfree(dev);
1247 set_gadget_data(gadget, NULL);
1248 }
1249
1250 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1251 GS_VERSION_STR);
1252}
1253
1254/*
1334 * gs_bind 1255 * gs_bind
1335 * 1256 *
1336 * Called on module load. Allocates and initializes the device 1257 * Called on module load. Allocates and initializes the device
@@ -1441,8 +1362,6 @@ static int __init gs_bind(struct usb_gadget *gadget)
1441 gs_unbind(gadget); 1362 gs_unbind(gadget);
1442 return -ENOMEM; 1363 return -ENOMEM;
1443 } 1364 }
1444 dev->dev_ctrl_req->complete = gs_setup_complete;
1445
1446 gadget->ep0->driver_data = dev; 1365 gadget->ep0->driver_data = dev;
1447 1366
1448 pr_info("gs_bind: %s %s bound\n", 1367 pr_info("gs_bind: %s %s bound\n",
@@ -1455,95 +1374,6 @@ autoconf_fail:
1455 return -ENODEV; 1374 return -ENODEV;
1456} 1375}
1457 1376
1458/*
1459 * gs_unbind
1460 *
1461 * Called on module unload. Frees the control request and device
1462 * structure.
1463 */
1464static void /* __init_or_exit */ gs_unbind(struct usb_gadget *gadget)
1465{
1466 struct gs_dev *dev = get_gadget_data(gadget);
1467
1468 gs_device = NULL;
1469
1470 /* read/write requests already freed, only control request remains */
1471 if (dev != NULL) {
1472 if (dev->dev_ctrl_req != NULL) {
1473 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
1474 dev->dev_ctrl_req = NULL;
1475 }
1476 gs_free_ports(dev);
1477 if (dev->dev_notify_ep)
1478 usb_ep_disable(dev->dev_notify_ep);
1479 if (dev->dev_in_ep)
1480 usb_ep_disable(dev->dev_in_ep);
1481 if (dev->dev_out_ep)
1482 usb_ep_disable(dev->dev_out_ep);
1483 kfree(dev);
1484 set_gadget_data(gadget, NULL);
1485 }
1486
1487 pr_info("gs_unbind: %s %s unbound\n", GS_LONG_NAME,
1488 GS_VERSION_STR);
1489}
1490
1491/*
1492 * gs_setup
1493 *
1494 * Implements all the control endpoint functionality that's not
1495 * handled in hardware or the hardware driver.
1496 *
1497 * Returns the size of the data sent to the host, or a negative
1498 * error number.
1499 */
1500static int gs_setup(struct usb_gadget *gadget,
1501 const struct usb_ctrlrequest *ctrl)
1502{
1503 int ret = -EOPNOTSUPP;
1504 struct gs_dev *dev = get_gadget_data(gadget);
1505 struct usb_request *req = dev->dev_ctrl_req;
1506 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1507 u16 wValue = le16_to_cpu(ctrl->wValue);
1508 u16 wLength = le16_to_cpu(ctrl->wLength);
1509
1510 req->complete = gs_setup_complete;
1511
1512 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1513 case USB_TYPE_STANDARD:
1514 ret = gs_setup_standard(gadget,ctrl);
1515 break;
1516
1517 case USB_TYPE_CLASS:
1518 ret = gs_setup_class(gadget,ctrl);
1519 break;
1520
1521 default:
1522 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1523 "value=%04x, index=%04x, length=%d\n",
1524 ctrl->bRequestType, ctrl->bRequest,
1525 wValue, wIndex, wLength);
1526 break;
1527 }
1528
1529 /* respond with data transfer before status phase? */
1530 if (ret >= 0) {
1531 req->length = ret;
1532 req->zero = ret < wLength
1533 && (ret % gadget->ep0->maxpacket) == 0;
1534 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1535 if (ret < 0) {
1536 pr_err("gs_setup: cannot queue response, ret=%d\n",
1537 ret);
1538 req->status = 0;
1539 gs_setup_complete(gadget->ep0, req);
1540 }
1541 }
1542
1543 /* device either stalls (ret < 0) or reports success */
1544 return ret;
1545}
1546
1547static int gs_setup_standard(struct usb_gadget *gadget, 1377static int gs_setup_standard(struct usb_gadget *gadget,
1548 const struct usb_ctrlrequest *ctrl) 1378 const struct usb_ctrlrequest *ctrl)
1549{ 1379{
@@ -1673,6 +1503,42 @@ set_interface_done:
1673 return ret; 1503 return ret;
1674} 1504}
1675 1505
1506static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
1507 struct usb_request *req)
1508{
1509 struct gs_dev *dev = ep->driver_data;
1510 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */
1511
1512 switch (req->status) {
1513 case 0:
1514 /* normal completion */
1515 if (req->actual != sizeof(port->port_line_coding))
1516 usb_ep_set_halt(ep);
1517 else if (port) {
1518 struct usb_cdc_line_coding *value = req->buf;
1519
1520 /* REVISIT: we currently just remember this data.
1521 * If we change that, (a) validate it first, then
1522 * (b) update whatever hardware needs updating.
1523 */
1524 spin_lock(&port->port_lock);
1525 port->port_line_coding = *value;
1526 spin_unlock(&port->port_lock);
1527 }
1528 break;
1529
1530 case -ESHUTDOWN:
1531 /* disconnect */
1532 gs_free_req(ep, req);
1533 break;
1534
1535 default:
1536 /* unexpected */
1537 break;
1538 }
1539 return;
1540}
1541
1676static int gs_setup_class(struct usb_gadget *gadget, 1542static int gs_setup_class(struct usb_gadget *gadget,
1677 const struct usb_ctrlrequest *ctrl) 1543 const struct usb_ctrlrequest *ctrl)
1678{ 1544{
@@ -1734,52 +1600,72 @@ static int gs_setup_class(struct usb_gadget *gadget,
1734 return ret; 1600 return ret;
1735} 1601}
1736 1602
1737static void gs_setup_complete_set_line_coding(struct usb_ep *ep, 1603/*
1738 struct usb_request *req) 1604 * gs_setup_complete
1605 */
1606static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
1739{ 1607{
1740 struct gs_dev *dev = ep->driver_data; 1608 if (req->status || req->actual != req->length) {
1741 struct gs_port *port = dev->dev_port[0]; /* ACM only has one port */ 1609 pr_err("gs_setup_complete: status error, status=%d, "
1610 "actual=%d, length=%d\n",
1611 req->status, req->actual, req->length);
1612 }
1613}
1742 1614
1743 switch (req->status) { 1615/*
1744 case 0: 1616 * gs_setup
1745 /* normal completion */ 1617 *
1746 if (req->actual != sizeof(port->port_line_coding)) 1618 * Implements all the control endpoint functionality that's not
1747 usb_ep_set_halt(ep); 1619 * handled in hardware or the hardware driver.
1748 else if (port) { 1620 *
1749 struct usb_cdc_line_coding *value = req->buf; 1621 * Returns the size of the data sent to the host, or a negative
1622 * error number.
1623 */
1624static int gs_setup(struct usb_gadget *gadget,
1625 const struct usb_ctrlrequest *ctrl)
1626{
1627 int ret = -EOPNOTSUPP;
1628 struct gs_dev *dev = get_gadget_data(gadget);
1629 struct usb_request *req = dev->dev_ctrl_req;
1630 u16 wIndex = le16_to_cpu(ctrl->wIndex);
1631 u16 wValue = le16_to_cpu(ctrl->wValue);
1632 u16 wLength = le16_to_cpu(ctrl->wLength);
1750 1633
1751 /* REVISIT: we currently just remember this data. 1634 req->complete = gs_setup_complete;
1752 * If we change that, (a) validate it first, then 1635
1753 * (b) update whatever hardware needs updating. 1636 switch (ctrl->bRequestType & USB_TYPE_MASK) {
1754 */ 1637 case USB_TYPE_STANDARD:
1755 spin_lock(&port->port_lock); 1638 ret = gs_setup_standard(gadget, ctrl);
1756 port->port_line_coding = *value;
1757 spin_unlock(&port->port_lock);
1758 }
1759 break; 1639 break;
1760 1640
1761 case -ESHUTDOWN: 1641 case USB_TYPE_CLASS:
1762 /* disconnect */ 1642 ret = gs_setup_class(gadget, ctrl);
1763 gs_free_req(ep, req);
1764 break; 1643 break;
1765 1644
1766 default: 1645 default:
1767 /* unexpected */ 1646 pr_err("gs_setup: unknown request, type=%02x, request=%02x, "
1647 "value=%04x, index=%04x, length=%d\n",
1648 ctrl->bRequestType, ctrl->bRequest,
1649 wValue, wIndex, wLength);
1768 break; 1650 break;
1769 } 1651 }
1770 return;
1771}
1772 1652
1773/* 1653 /* respond with data transfer before status phase? */
1774 * gs_setup_complete 1654 if (ret >= 0) {
1775 */ 1655 req->length = ret;
1776static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req) 1656 req->zero = ret < wLength
1777{ 1657 && (ret % gadget->ep0->maxpacket) == 0;
1778 if (req->status || req->actual != req->length) { 1658 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1779 pr_err("gs_setup_complete: status error, status=%d, " 1659 if (ret < 0) {
1780 "actual=%d, length=%d\n", 1660 pr_err("gs_setup: cannot queue response, ret=%d\n",
1781 req->status, req->actual, req->length); 1661 ret);
1662 req->status = 0;
1663 gs_setup_complete(gadget->ep0, req);
1664 }
1782 } 1665 }
1666
1667 /* device either stalls (ret < 0) or reports success */
1668 return ret;
1783} 1669}
1784 1670
1785/* 1671/*
@@ -1811,6 +1697,23 @@ static void gs_disconnect(struct usb_gadget *gadget)
1811 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); 1697 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
1812} 1698}
1813 1699
1700static struct usb_gadget_driver gs_gadget_driver = {
1701#ifdef CONFIG_USB_GADGET_DUALSPEED
1702 .speed = USB_SPEED_HIGH,
1703#else
1704 .speed = USB_SPEED_FULL,
1705#endif /* CONFIG_USB_GADGET_DUALSPEED */
1706 .function = GS_LONG_NAME,
1707 .bind = gs_bind,
1708 .unbind = gs_unbind,
1709 .setup = gs_setup,
1710 .disconnect = gs_disconnect,
1711 .driver = {
1712 .name = GS_SHORT_NAME,
1713 .owner = THIS_MODULE,
1714 },
1715};
1716
1814/* 1717/*
1815 * gs_set_config 1718 * gs_set_config
1816 * 1719 *
@@ -1846,16 +1749,10 @@ static int gs_set_config(struct gs_dev *dev, unsigned config)
1846 case GS_BULK_CONFIG_ID: 1749 case GS_BULK_CONFIG_ID:
1847 if (use_acm) 1750 if (use_acm)
1848 return -EINVAL; 1751 return -EINVAL;
1849 /* device specific optimizations */
1850 if (gadget_is_net2280(gadget))
1851 net2280_set_fifo_mode(gadget, 1);
1852 break; 1752 break;
1853 case GS_ACM_CONFIG_ID: 1753 case GS_ACM_CONFIG_ID:
1854 if (!use_acm) 1754 if (!use_acm)
1855 return -EINVAL; 1755 return -EINVAL;
1856 /* device specific optimizations */
1857 if (gadget_is_net2280(gadget))
1858 net2280_set_fifo_mode(gadget, 1);
1859 break; 1756 break;
1860 default: 1757 default:
1861 return -EINVAL; 1758 return -EINVAL;
@@ -2233,6 +2130,8 @@ static void gs_free_ports(struct gs_dev *dev)
2233 } 2130 }
2234} 2131}
2235 2132
2133/*-------------------------------------------------------------------------*/
2134
2236/* Circular Buffer */ 2135/* Circular Buffer */
2237 2136
2238/* 2137/*
@@ -2393,3 +2292,77 @@ gs_buf_get(struct gs_buf *gb, char *buf, unsigned int count)
2393 2292
2394 return count; 2293 return count;
2395} 2294}
2295
2296/*-------------------------------------------------------------------------*/
2297
2298static struct tty_driver *gs_tty_driver;
2299
2300/*
2301 * gs_module_init
2302 *
2303 * Register as a USB gadget driver and a tty driver.
2304 */
2305static int __init gs_module_init(void)
2306{
2307 int i;
2308 int retval;
2309
2310 retval = usb_gadget_register_driver(&gs_gadget_driver);
2311 if (retval) {
2312 pr_err("gs_module_init: cannot register gadget driver, "
2313 "ret=%d\n", retval);
2314 return retval;
2315 }
2316
2317 gs_tty_driver = alloc_tty_driver(GS_NUM_PORTS);
2318 if (!gs_tty_driver)
2319 return -ENOMEM;
2320 gs_tty_driver->owner = THIS_MODULE;
2321 gs_tty_driver->driver_name = GS_SHORT_NAME;
2322 gs_tty_driver->name = "ttygs";
2323 gs_tty_driver->major = GS_MAJOR;
2324 gs_tty_driver->minor_start = GS_MINOR_START;
2325 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2326 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2327 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2328 gs_tty_driver->init_termios = tty_std_termios;
2329 /* must match GS_DEFAULT_DTE_RATE and friends */
2330 gs_tty_driver->init_termios.c_cflag =
2331 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2332 gs_tty_driver->init_termios.c_ispeed = GS_DEFAULT_DTE_RATE;
2333 gs_tty_driver->init_termios.c_ospeed = GS_DEFAULT_DTE_RATE;
2334 tty_set_operations(gs_tty_driver, &gs_tty_ops);
2335
2336 for (i = 0; i < GS_NUM_PORTS; i++)
2337 mutex_init(&gs_open_close_lock[i]);
2338
2339 retval = tty_register_driver(gs_tty_driver);
2340 if (retval) {
2341 usb_gadget_unregister_driver(&gs_gadget_driver);
2342 put_tty_driver(gs_tty_driver);
2343 pr_err("gs_module_init: cannot register tty driver, "
2344 "ret=%d\n", retval);
2345 return retval;
2346 }
2347
2348 pr_info("gs_module_init: %s %s loaded\n",
2349 GS_LONG_NAME, GS_VERSION_STR);
2350 return 0;
2351}
2352module_init(gs_module_init);
2353
2354/*
2355 * gs_module_exit
2356 *
2357 * Unregister as a tty driver and a USB gadget driver.
2358 */
2359static void __exit gs_module_exit(void)
2360{
2361 tty_unregister_driver(gs_tty_driver);
2362 put_tty_driver(gs_tty_driver);
2363 usb_gadget_unregister_driver(&gs_gadget_driver);
2364
2365 pr_info("gs_module_exit: %s %s unloaded\n",
2366 GS_LONG_NAME, GS_VERSION_STR);
2367}
2368module_exit(gs_module_exit);