aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Kconfig2
-rw-r--r--drivers/usb/Makefile1
-rw-r--r--drivers/usb/atm/ueagle-atm.c9
-rw-r--r--drivers/usb/class/cdc-acm.c530
-rw-r--r--drivers/usb/class/cdc-acm.h7
-rw-r--r--drivers/usb/class/cdc-wdm.c1
-rw-r--r--drivers/usb/class/usblp.c6
-rw-r--r--drivers/usb/class/usbtmc.c16
-rw-r--r--drivers/usb/core/Kconfig16
-rw-r--r--drivers/usb/core/Makefile4
-rw-r--r--drivers/usb/core/config.c192
-rw-r--r--drivers/usb/core/devices.c10
-rw-r--r--drivers/usb/core/devio.c78
-rw-r--r--drivers/usb/core/driver.c56
-rw-r--r--drivers/usb/core/endpoint.c160
-rw-r--r--drivers/usb/core/file.c13
-rw-r--r--drivers/usb/core/hcd-pci.c244
-rw-r--r--drivers/usb/core/hcd.c222
-rw-r--r--drivers/usb/core/hcd.h59
-rw-r--r--drivers/usb/core/hub.c174
-rw-r--r--drivers/usb/core/hub.h9
-rw-r--r--drivers/usb/core/inode.c5
-rw-r--r--drivers/usb/core/message.c257
-rw-r--r--drivers/usb/core/sysfs.c12
-rw-r--r--drivers/usb/core/urb.c12
-rw-r--r--drivers/usb/core/usb.c87
-rw-r--r--drivers/usb/core/usb.h13
-rw-r--r--drivers/usb/gadget/Kconfig96
-rw-r--r--drivers/usb/gadget/Makefile8
-rw-r--r--drivers/usb/gadget/amd5536udc.c1
-rw-r--r--drivers/usb/gadget/at91_udc.c10
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c20
-rw-r--r--drivers/usb/gadget/audio.c302
-rw-r--r--drivers/usb/gadget/ci13xxx_udc.c6
-rw-r--r--drivers/usb/gadget/ether.c11
-rw-r--r--drivers/usb/gadget/f_audio.c707
-rw-r--r--drivers/usb/gadget/f_phonet.c21
-rw-r--r--drivers/usb/gadget/f_rndis.c4
-rw-r--r--drivers/usb/gadget/file_storage.c93
-rw-r--r--drivers/usb/gadget/fsl_mx3_udc.c95
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c (renamed from drivers/usb/gadget/fsl_usb2_udc.c)69
-rw-r--r--drivers/usb/gadget/fsl_usb2_udc.h18
-rw-r--r--drivers/usb/gadget/gadget_chips.h8
-rw-r--r--drivers/usb/gadget/goku_udc.c6
-rw-r--r--drivers/usb/gadget/imx_udc.c14
-rw-r--r--drivers/usb/gadget/inode.c14
-rw-r--r--drivers/usb/gadget/langwell_udc.c3372
-rw-r--r--drivers/usb/gadget/langwell_udc.h228
-rw-r--r--drivers/usb/gadget/pxa25x_udc.c24
-rw-r--r--drivers/usb/gadget/pxa27x_udc.c71
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h2
-rw-r--r--drivers/usb/gadget/rndis.c2
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c3269
-rw-r--r--drivers/usb/gadget/s3c2410_udc.c1
-rw-r--r--drivers/usb/gadget/u_audio.c319
-rw-r--r--drivers/usb/gadget/u_audio.h56
-rw-r--r--drivers/usb/gadget/u_ether.c2
-rw-r--r--drivers/usb/gadget/u_serial.c1
-rw-r--r--drivers/usb/host/Kconfig53
-rw-r--r--drivers/usb/host/Makefile2
-rw-r--r--drivers/usb/host/ehci-au1xxx.c3
-rw-r--r--drivers/usb/host/ehci-fsl.c3
-rw-r--r--drivers/usb/host/ehci-hcd.c46
-rw-r--r--drivers/usb/host/ehci-hub.c4
-rw-r--r--drivers/usb/host/ehci-ixp4xx.c3
-rw-r--r--drivers/usb/host/ehci-orion.c5
-rw-r--r--drivers/usb/host/ehci-pci.c29
-rw-r--r--drivers/usb/host/ehci-ppc-of.c3
-rw-r--r--drivers/usb/host/ehci-ps3.c14
-rw-r--r--drivers/usb/host/ehci-q.c121
-rw-r--r--drivers/usb/host/ehci-sched.c20
-rw-r--r--drivers/usb/host/ehci.h3
-rw-r--r--drivers/usb/host/fhci-dbg.c2
-rw-r--r--drivers/usb/host/fhci-sched.c8
-rw-r--r--drivers/usb/host/hwa-hc.c21
-rw-r--r--drivers/usb/host/isp1760-if.c2
-rw-r--r--drivers/usb/host/ohci-dbg.c31
-rw-r--r--drivers/usb/host/ohci-ep93xx.c13
-rw-r--r--drivers/usb/host/ohci-hcd.c38
-rw-r--r--drivers/usb/host/ohci-pci.c24
-rw-r--r--drivers/usb/host/ohci-ps3.c11
-rw-r--r--drivers/usb/host/pci-quirks.c123
-rw-r--r--drivers/usb/host/r8a66597-hcd.c63
-rw-r--r--drivers/usb/host/r8a66597.h38
-rw-r--r--drivers/usb/host/uhci-hcd.c23
-rw-r--r--drivers/usb/host/uhci-q.c2
-rw-r--r--drivers/usb/host/xhci-dbg.c485
-rw-r--r--drivers/usb/host/xhci-ext-caps.h145
-rw-r--r--drivers/usb/host/xhci-hcd.c1274
-rw-r--r--drivers/usb/host/xhci-hub.c308
-rw-r--r--drivers/usb/host/xhci-mem.c769
-rw-r--r--drivers/usb/host/xhci-pci.c166
-rw-r--r--drivers/usb/host/xhci-ring.c1648
-rw-r--r--drivers/usb/host/xhci.h1157
-rw-r--r--drivers/usb/misc/iowarrior.c7
-rw-r--r--drivers/usb/misc/legousbtower.c6
-rw-r--r--drivers/usb/misc/rio500.c1
-rw-r--r--drivers/usb/misc/sisusbvga/Kconfig2
-rw-r--r--drivers/usb/misc/usblcd.c1
-rw-r--r--drivers/usb/misc/usbtest.c39
-rw-r--r--drivers/usb/mon/mon_bin.c2
-rw-r--r--drivers/usb/mon/mon_text.c2
-rw-r--r--drivers/usb/musb/Kconfig2
-rw-r--r--drivers/usb/musb/blackfin.c11
-rw-r--r--drivers/usb/musb/cppi_dma.c34
-rw-r--r--drivers/usb/musb/cppi_dma.h7
-rw-r--r--drivers/usb/musb/davinci.c86
-rw-r--r--drivers/usb/musb/musb_core.c228
-rw-r--r--drivers/usb/musb/musb_core.h23
-rw-r--r--drivers/usb/musb/musb_gadget.c45
-rw-r--r--drivers/usb/musb/musb_gadget_ep0.c45
-rw-r--r--drivers/usb/musb/musb_host.c294
-rw-r--r--drivers/usb/musb/musb_host.h1
-rw-r--r--drivers/usb/musb/musb_virthub.c35
-rw-r--r--drivers/usb/musb/omap2430.c71
-rw-r--r--drivers/usb/musb/tusb6010.c70
-rw-r--r--drivers/usb/otg/nop-usb-xceiv.c26
-rw-r--r--drivers/usb/otg/twl4030-usb.c28
-rw-r--r--drivers/usb/serial/aircable.c5
-rw-r--r--drivers/usb/serial/belkin_sa.c13
-rw-r--r--drivers/usb/serial/bus.c27
-rw-r--r--drivers/usb/serial/ch341.c46
-rw-r--r--drivers/usb/serial/console.c13
-rw-r--r--drivers/usb/serial/cp210x.c255
-rw-r--r--drivers/usb/serial/cyberjack.c26
-rw-r--r--drivers/usb/serial/cypress_m8.c96
-rw-r--r--drivers/usb/serial/digi_acceleport.c95
-rw-r--r--drivers/usb/serial/empeg.c14
-rw-r--r--drivers/usb/serial/ftdi_sio.c396
-rw-r--r--drivers/usb/serial/ftdi_sio.h31
-rw-r--r--drivers/usb/serial/garmin_gps.c217
-rw-r--r--drivers/usb/serial/generic.c199
-rw-r--r--drivers/usb/serial/io_edgeport.c39
-rw-r--r--drivers/usb/serial/io_tables.h12
-rw-r--r--drivers/usb/serial/io_ti.c27
-rw-r--r--drivers/usb/serial/ipaq.c13
-rw-r--r--drivers/usb/serial/ipw.c18
-rw-r--r--drivers/usb/serial/ir-usb.c6
-rw-r--r--drivers/usb/serial/iuu_phoenix.c108
-rw-r--r--drivers/usb/serial/keyspan.c26
-rw-r--r--drivers/usb/serial/keyspan.h20
-rw-r--r--drivers/usb/serial/keyspan_pda.c52
-rw-r--r--drivers/usb/serial/kl5kusb105.c45
-rw-r--r--drivers/usb/serial/kobil_sct.c19
-rw-r--r--drivers/usb/serial/mct_u232.c50
-rw-r--r--drivers/usb/serial/mos7720.c14
-rw-r--r--drivers/usb/serial/mos7840.c407
-rw-r--r--drivers/usb/serial/navman.c3
-rw-r--r--drivers/usb/serial/omninet.c25
-rw-r--r--drivers/usb/serial/opticon.c17
-rw-r--r--drivers/usb/serial/option.c154
-rw-r--r--drivers/usb/serial/oti6858.c64
-rw-r--r--drivers/usb/serial/pl2303.c144
-rw-r--r--drivers/usb/serial/pl2303.h4
-rw-r--r--drivers/usb/serial/sierra.c573
-rw-r--r--drivers/usb/serial/spcp8x5.c90
-rw-r--r--drivers/usb/serial/symbolserial.c17
-rw-r--r--drivers/usb/serial/ti_usb_3410_5052.c23
-rw-r--r--drivers/usb/serial/usb-serial.c204
-rw-r--r--drivers/usb/serial/usb_debug.c41
-rw-r--r--drivers/usb/serial/visor.c19
-rw-r--r--drivers/usb/serial/whiteheat.c39
-rw-r--r--drivers/usb/storage/initializers.c14
-rw-r--r--drivers/usb/storage/option_ms.c127
-rw-r--r--drivers/usb/storage/scsiglue.c4
-rw-r--r--drivers/usb/storage/sierra_ms.c2
-rw-r--r--drivers/usb/storage/unusual_devs.h2
167 files changed, 19321 insertions, 3305 deletions
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index c6c816b7ecb5..dcd49f1e96d0 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -22,6 +22,7 @@ config USB_ARCH_HAS_HCD
22 default y if PCMCIA && !M32R # sl811_cs 22 default y if PCMCIA && !M32R # sl811_cs
23 default y if ARM # SL-811 23 default y if ARM # SL-811
24 default y if SUPERH # r8a66597-hcd 24 default y if SUPERH # r8a66597-hcd
25 default y if MICROBLAZE
25 default PCI 26 default PCI
26 27
27# many non-PCI SOC chips embed OHCI 28# many non-PCI SOC chips embed OHCI
@@ -63,6 +64,7 @@ config USB_ARCH_HAS_EHCI
63config USB 64config USB
64 tristate "Support for Host-side USB" 65 tristate "Support for Host-side USB"
65 depends on USB_ARCH_HAS_HCD 66 depends on USB_ARCH_HAS_HCD
67 select NLS # for UTF-8 strings
66 ---help--- 68 ---help---
67 Universal Serial Bus (USB) is a specification for a serial bus 69 Universal Serial Bus (USB) is a specification for a serial bus
68 subsystem which offers higher speeds and more features than the 70 subsystem which offers higher speeds and more features than the
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 0a3dc5ece634..19cb7d5480d7 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_USB_ISP116X_HCD) += host/
14obj-$(CONFIG_USB_OHCI_HCD) += host/ 14obj-$(CONFIG_USB_OHCI_HCD) += host/
15obj-$(CONFIG_USB_UHCI_HCD) += host/ 15obj-$(CONFIG_USB_UHCI_HCD) += host/
16obj-$(CONFIG_USB_FHCI_HCD) += host/ 16obj-$(CONFIG_USB_FHCI_HCD) += host/
17obj-$(CONFIG_USB_XHCI_HCD) += host/
17obj-$(CONFIG_USB_SL811_HCD) += host/ 18obj-$(CONFIG_USB_SL811_HCD) += host/
18obj-$(CONFIG_USB_U132_HCD) += host/ 19obj-$(CONFIG_USB_U132_HCD) += host/
19obj-$(CONFIG_USB_R8A66597_HCD) += host/ 20obj-$(CONFIG_USB_R8A66597_HCD) += host/
diff --git a/drivers/usb/atm/ueagle-atm.c b/drivers/usb/atm/ueagle-atm.c
index 9cf9ff69e3e3..d171b563e94c 100644
--- a/drivers/usb/atm/ueagle-atm.c
+++ b/drivers/usb/atm/ueagle-atm.c
@@ -306,6 +306,7 @@ enum {
306#define FW_GET_BYTE(p) *((__u8 *) (p)) 306#define FW_GET_BYTE(p) *((__u8 *) (p))
307 307
308#define FW_DIR "ueagle-atm/" 308#define FW_DIR "ueagle-atm/"
309#define UEA_FW_NAME_MAX 30
309#define NB_MODEM 4 310#define NB_MODEM 4
310 311
311#define BULK_TIMEOUT 300 312#define BULK_TIMEOUT 300
@@ -1564,9 +1565,9 @@ static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
1564 file = cmv_file[sc->modem_index]; 1565 file = cmv_file[sc->modem_index];
1565 1566
1566 strcpy(cmv_name, FW_DIR); 1567 strcpy(cmv_name, FW_DIR);
1567 strlcat(cmv_name, file, FIRMWARE_NAME_MAX); 1568 strlcat(cmv_name, file, UEA_FW_NAME_MAX);
1568 if (ver == 2) 1569 if (ver == 2)
1569 strlcat(cmv_name, ".v2", FIRMWARE_NAME_MAX); 1570 strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
1570} 1571}
1571 1572
1572static int request_cmvs_old(struct uea_softc *sc, 1573static int request_cmvs_old(struct uea_softc *sc,
@@ -1574,7 +1575,7 @@ static int request_cmvs_old(struct uea_softc *sc,
1574{ 1575{
1575 int ret, size; 1576 int ret, size;
1576 u8 *data; 1577 u8 *data;
1577 char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */ 1578 char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1578 1579
1579 cmvs_file_name(sc, cmv_name, 1); 1580 cmvs_file_name(sc, cmv_name, 1);
1580 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev); 1581 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
@@ -1608,7 +1609,7 @@ static int request_cmvs(struct uea_softc *sc,
1608 int ret, size; 1609 int ret, size;
1609 u32 crc; 1610 u32 crc;
1610 u8 *data; 1611 u8 *data;
1611 char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */ 1612 char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1612 1613
1613 cmvs_file_name(sc, cmv_name, 2); 1614 cmvs_file_name(sc, cmv_name, 2);
1614 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev); 1615 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 7a1164dd1d37..e1f89416ef8c 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -16,7 +16,8 @@
16 * v0.9 - thorough cleaning, URBification, almost a rewrite 16 * v0.9 - thorough cleaning, URBification, almost a rewrite
17 * v0.10 - some more cleanups 17 * v0.10 - some more cleanups
18 * v0.11 - fixed flow control, read error doesn't stop reads 18 * v0.11 - fixed flow control, read error doesn't stop reads
19 * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced 19 * v0.12 - added TIOCM ioctls, added break handling, made struct acm
20 * kmalloced
20 * v0.13 - added termios, added hangup 21 * v0.13 - added termios, added hangup
21 * v0.14 - sized down struct acm 22 * v0.14 - sized down struct acm
22 * v0.15 - fixed flow control again - characters could be lost 23 * v0.15 - fixed flow control again - characters could be lost
@@ -62,7 +63,7 @@
62#include <linux/tty_flip.h> 63#include <linux/tty_flip.h>
63#include <linux/module.h> 64#include <linux/module.h>
64#include <linux/mutex.h> 65#include <linux/mutex.h>
65#include <asm/uaccess.h> 66#include <linux/uaccess.h>
66#include <linux/usb.h> 67#include <linux/usb.h>
67#include <linux/usb/cdc.h> 68#include <linux/usb/cdc.h>
68#include <asm/byteorder.h> 69#include <asm/byteorder.h>
@@ -87,7 +88,10 @@ static struct acm *acm_table[ACM_TTY_MINORS];
87 88
88static DEFINE_MUTEX(open_mutex); 89static DEFINE_MUTEX(open_mutex);
89 90
90#define ACM_READY(acm) (acm && acm->dev && acm->used) 91#define ACM_READY(acm) (acm && acm->dev && acm->port.count)
92
93static const struct tty_port_operations acm_port_ops = {
94};
91 95
92#ifdef VERBOSE_DEBUG 96#ifdef VERBOSE_DEBUG
93#define verbose 1 97#define verbose 1
@@ -99,13 +103,15 @@ static DEFINE_MUTEX(open_mutex);
99 * Functions for ACM control messages. 103 * Functions for ACM control messages.
100 */ 104 */
101 105
102static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len) 106static int acm_ctrl_msg(struct acm *acm, int request, int value,
107 void *buf, int len)
103{ 108{
104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), 109 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
105 request, USB_RT_ACM, value, 110 request, USB_RT_ACM, value,
106 acm->control->altsetting[0].desc.bInterfaceNumber, 111 acm->control->altsetting[0].desc.bInterfaceNumber,
107 buf, len, 5000); 112 buf, len, 5000);
108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval); 113 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d",
114 request, value, len, retval);
109 return retval < 0 ? retval : 0; 115 return retval < 0 ? retval : 0;
110} 116}
111 117
@@ -150,9 +156,8 @@ static int acm_wb_is_avail(struct acm *acm)
150 156
151 n = ACM_NW; 157 n = ACM_NW;
152 spin_lock_irqsave(&acm->write_lock, flags); 158 spin_lock_irqsave(&acm->write_lock, flags);
153 for (i = 0; i < ACM_NW; i++) { 159 for (i = 0; i < ACM_NW; i++)
154 n -= acm->wb[i].use; 160 n -= acm->wb[i].use;
155 }
156 spin_unlock_irqrestore(&acm->write_lock, flags); 161 spin_unlock_irqrestore(&acm->write_lock, flags);
157 return n; 162 return n;
158} 163}
@@ -183,7 +188,8 @@ static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
183 wb->urb->transfer_buffer_length = wb->len; 188 wb->urb->transfer_buffer_length = wb->len;
184 wb->urb->dev = acm->dev; 189 wb->urb->dev = acm->dev;
185 190
186 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) { 191 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
192 if (rc < 0) {
187 dbg("usb_submit_urb(write bulk) failed: %d", rc); 193 dbg("usb_submit_urb(write bulk) failed: %d", rc);
188 acm_write_done(acm, wb); 194 acm_write_done(acm, wb);
189 } 195 }
@@ -262,6 +268,7 @@ static void acm_ctrl_irq(struct urb *urb)
262{ 268{
263 struct acm *acm = urb->context; 269 struct acm *acm = urb->context;
264 struct usb_cdc_notification *dr = urb->transfer_buffer; 270 struct usb_cdc_notification *dr = urb->transfer_buffer;
271 struct tty_struct *tty;
265 unsigned char *data; 272 unsigned char *data;
266 int newctrl; 273 int newctrl;
267 int retval; 274 int retval;
@@ -287,40 +294,45 @@ static void acm_ctrl_irq(struct urb *urb)
287 294
288 data = (unsigned char *)(dr + 1); 295 data = (unsigned char *)(dr + 1);
289 switch (dr->bNotificationType) { 296 switch (dr->bNotificationType) {
297 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
298 dbg("%s network", dr->wValue ?
299 "connected to" : "disconnected from");
300 break;
290 301
291 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 302 case USB_CDC_NOTIFY_SERIAL_STATE:
292 303 tty = tty_port_tty_get(&acm->port);
293 dbg("%s network", dr->wValue ? "connected to" : "disconnected from"); 304 newctrl = get_unaligned_le16(data);
294 break;
295
296 case USB_CDC_NOTIFY_SERIAL_STATE:
297
298 newctrl = get_unaligned_le16(data);
299 305
300 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { 306 if (tty) {
307 if (!acm->clocal &&
308 (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
301 dbg("calling hangup"); 309 dbg("calling hangup");
302 tty_hangup(acm->tty); 310 tty_hangup(tty);
303 } 311 }
312 tty_kref_put(tty);
313 }
304 314
305 acm->ctrlin = newctrl; 315 acm->ctrlin = newctrl;
306
307 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
308 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
309 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
310 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
311 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
312 316
317 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
318 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
319 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
320 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
321 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
322 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
323 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
324 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
313 break; 325 break;
314 326
315 default: 327 default:
316 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d", 328 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
317 dr->bNotificationType, dr->wIndex, 329 dr->bNotificationType, dr->wIndex,
318 dr->wLength, data[0], data[1]); 330 dr->wLength, data[0], data[1]);
319 break; 331 break;
320 } 332 }
321exit: 333exit:
322 usb_mark_last_busy(acm->dev); 334 usb_mark_last_busy(acm->dev);
323 retval = usb_submit_urb (urb, GFP_ATOMIC); 335 retval = usb_submit_urb(urb, GFP_ATOMIC);
324 if (retval) 336 if (retval)
325 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with " 337 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
326 "result %d", __func__, retval); 338 "result %d", __func__, retval);
@@ -371,15 +383,15 @@ static void acm_rx_tasklet(unsigned long _acm)
371{ 383{
372 struct acm *acm = (void *)_acm; 384 struct acm *acm = (void *)_acm;
373 struct acm_rb *buf; 385 struct acm_rb *buf;
374 struct tty_struct *tty = acm->tty; 386 struct tty_struct *tty;
375 struct acm_ru *rcv; 387 struct acm_ru *rcv;
376 unsigned long flags; 388 unsigned long flags;
377 unsigned char throttled; 389 unsigned char throttled;
390 struct usb_host_endpoint *ep;
378 391
379 dbg("Entering acm_rx_tasklet"); 392 dbg("Entering acm_rx_tasklet");
380 393
381 if (!ACM_READY(acm)) 394 if (!ACM_READY(acm)) {
382 {
383 dbg("acm_rx_tasklet: ACM not ready"); 395 dbg("acm_rx_tasklet: ACM not ready");
384 return; 396 return;
385 } 397 }
@@ -387,12 +399,13 @@ static void acm_rx_tasklet(unsigned long _acm)
387 spin_lock_irqsave(&acm->throttle_lock, flags); 399 spin_lock_irqsave(&acm->throttle_lock, flags);
388 throttled = acm->throttle; 400 throttled = acm->throttle;
389 spin_unlock_irqrestore(&acm->throttle_lock, flags); 401 spin_unlock_irqrestore(&acm->throttle_lock, flags);
390 if (throttled) 402 if (throttled) {
391 {
392 dbg("acm_rx_tasklet: throttled"); 403 dbg("acm_rx_tasklet: throttled");
393 return; 404 return;
394 } 405 }
395 406
407 tty = tty_port_tty_get(&acm->port);
408
396next_buffer: 409next_buffer:
397 spin_lock_irqsave(&acm->read_lock, flags); 410 spin_lock_irqsave(&acm->read_lock, flags);
398 if (list_empty(&acm->filled_read_bufs)) { 411 if (list_empty(&acm->filled_read_bufs)) {
@@ -406,20 +419,22 @@ next_buffer:
406 419
407 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size); 420 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
408 421
409 tty_buffer_request_room(tty, buf->size); 422 if (tty) {
410 spin_lock_irqsave(&acm->throttle_lock, flags); 423 spin_lock_irqsave(&acm->throttle_lock, flags);
411 throttled = acm->throttle; 424 throttled = acm->throttle;
412 spin_unlock_irqrestore(&acm->throttle_lock, flags); 425 spin_unlock_irqrestore(&acm->throttle_lock, flags);
413 if (!throttled) 426 if (!throttled) {
414 tty_insert_flip_string(tty, buf->base, buf->size); 427 tty_buffer_request_room(tty, buf->size);
415 tty_flip_buffer_push(tty); 428 tty_insert_flip_string(tty, buf->base, buf->size);
416 429 tty_flip_buffer_push(tty);
417 if (throttled) { 430 } else {
418 dbg("Throttling noticed"); 431 tty_kref_put(tty);
419 spin_lock_irqsave(&acm->read_lock, flags); 432 dbg("Throttling noticed");
420 list_add(&buf->list, &acm->filled_read_bufs); 433 spin_lock_irqsave(&acm->read_lock, flags);
421 spin_unlock_irqrestore(&acm->read_lock, flags); 434 list_add(&buf->list, &acm->filled_read_bufs);
422 return; 435 spin_unlock_irqrestore(&acm->read_lock, flags);
436 return;
437 }
423 } 438 }
424 439
425 spin_lock_irqsave(&acm->read_lock, flags); 440 spin_lock_irqsave(&acm->read_lock, flags);
@@ -428,6 +443,8 @@ next_buffer:
428 goto next_buffer; 443 goto next_buffer;
429 444
430urbs: 445urbs:
446 tty_kref_put(tty);
447
431 while (!list_empty(&acm->spare_read_bufs)) { 448 while (!list_empty(&acm->spare_read_bufs)) {
432 spin_lock_irqsave(&acm->read_lock, flags); 449 spin_lock_irqsave(&acm->read_lock, flags);
433 if (list_empty(&acm->spare_read_urbs)) { 450 if (list_empty(&acm->spare_read_urbs)) {
@@ -446,18 +463,28 @@ urbs:
446 463
447 rcv->buffer = buf; 464 rcv->buffer = buf;
448 465
449 usb_fill_bulk_urb(rcv->urb, acm->dev, 466 ep = (usb_pipein(acm->rx_endpoint) ? acm->dev->ep_in : acm->dev->ep_out)
450 acm->rx_endpoint, 467 [usb_pipeendpoint(acm->rx_endpoint)];
451 buf->base, 468 if (usb_endpoint_xfer_int(&ep->desc))
452 acm->readsize, 469 usb_fill_int_urb(rcv->urb, acm->dev,
453 acm_read_bulk, rcv); 470 acm->rx_endpoint,
471 buf->base,
472 acm->readsize,
473 acm_read_bulk, rcv, ep->desc.bInterval);
474 else
475 usb_fill_bulk_urb(rcv->urb, acm->dev,
476 acm->rx_endpoint,
477 buf->base,
478 acm->readsize,
479 acm_read_bulk, rcv);
454 rcv->urb->transfer_dma = buf->dma; 480 rcv->urb->transfer_dma = buf->dma;
455 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 481 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
456 482
457 /* This shouldn't kill the driver as unsuccessful URBs are returned to the 483 /* This shouldn't kill the driver as unsuccessful URBs are
458 free-urbs-pool and resubmited ASAP */ 484 returned to the free-urbs-pool and resubmited ASAP */
459 spin_lock_irqsave(&acm->read_lock, flags); 485 spin_lock_irqsave(&acm->read_lock, flags);
460 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) { 486 if (acm->susp_count ||
487 usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
461 list_add(&buf->list, &acm->spare_read_bufs); 488 list_add(&buf->list, &acm->spare_read_bufs);
462 list_add(&rcv->list, &acm->spare_read_urbs); 489 list_add(&rcv->list, &acm->spare_read_urbs);
463 acm->processing = 0; 490 acm->processing = 0;
@@ -499,11 +526,14 @@ static void acm_write_bulk(struct urb *urb)
499static void acm_softint(struct work_struct *work) 526static void acm_softint(struct work_struct *work)
500{ 527{
501 struct acm *acm = container_of(work, struct acm, work); 528 struct acm *acm = container_of(work, struct acm, work);
529 struct tty_struct *tty;
502 530
503 dev_vdbg(&acm->data->dev, "tx work\n"); 531 dev_vdbg(&acm->data->dev, "tx work\n");
504 if (!ACM_READY(acm)) 532 if (!ACM_READY(acm))
505 return; 533 return;
506 tty_wakeup(acm->tty); 534 tty = tty_port_tty_get(&acm->port);
535 tty_wakeup(tty);
536 tty_kref_put(tty);
507} 537}
508 538
509static void acm_waker(struct work_struct *waker) 539static void acm_waker(struct work_struct *waker)
@@ -530,7 +560,7 @@ static void acm_waker(struct work_struct *waker)
530static int acm_tty_open(struct tty_struct *tty, struct file *filp) 560static int acm_tty_open(struct tty_struct *tty, struct file *filp)
531{ 561{
532 struct acm *acm; 562 struct acm *acm;
533 int rv = -EINVAL; 563 int rv = -ENODEV;
534 int i; 564 int i;
535 dbg("Entering acm_tty_open."); 565 dbg("Entering acm_tty_open.");
536 566
@@ -543,8 +573,9 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
543 rv = 0; 573 rv = 0;
544 574
545 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); 575 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
576
546 tty->driver_data = acm; 577 tty->driver_data = acm;
547 acm->tty = tty; 578 tty_port_tty_set(&acm->port, tty);
548 579
549 if (usb_autopm_get_interface(acm->control) < 0) 580 if (usb_autopm_get_interface(acm->control) < 0)
550 goto early_bail; 581 goto early_bail;
@@ -552,11 +583,10 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
552 acm->control->needs_remote_wakeup = 1; 583 acm->control->needs_remote_wakeup = 1;
553 584
554 mutex_lock(&acm->mutex); 585 mutex_lock(&acm->mutex);
555 if (acm->used++) { 586 if (acm->port.count++) {
556 usb_autopm_put_interface(acm->control); 587 usb_autopm_put_interface(acm->control);
557 goto done; 588 goto done;
558 } 589 }
559
560 590
561 acm->ctrlurb->dev = acm->dev; 591 acm->ctrlurb->dev = acm->dev;
562 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { 592 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
@@ -567,22 +597,22 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
567 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) && 597 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
568 (acm->ctrl_caps & USB_CDC_CAP_LINE)) 598 (acm->ctrl_caps & USB_CDC_CAP_LINE))
569 goto full_bailout; 599 goto full_bailout;
600
570 usb_autopm_put_interface(acm->control); 601 usb_autopm_put_interface(acm->control);
571 602
572 INIT_LIST_HEAD(&acm->spare_read_urbs); 603 INIT_LIST_HEAD(&acm->spare_read_urbs);
573 INIT_LIST_HEAD(&acm->spare_read_bufs); 604 INIT_LIST_HEAD(&acm->spare_read_bufs);
574 INIT_LIST_HEAD(&acm->filled_read_bufs); 605 INIT_LIST_HEAD(&acm->filled_read_bufs);
575 for (i = 0; i < acm->rx_buflimit; i++) { 606
607 for (i = 0; i < acm->rx_buflimit; i++)
576 list_add(&(acm->ru[i].list), &acm->spare_read_urbs); 608 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
577 } 609 for (i = 0; i < acm->rx_buflimit; i++)
578 for (i = 0; i < acm->rx_buflimit; i++) {
579 list_add(&(acm->rb[i].list), &acm->spare_read_bufs); 610 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
580 }
581 611
582 acm->throttle = 0; 612 acm->throttle = 0;
583 613
584 tasklet_schedule(&acm->urb_task); 614 tasklet_schedule(&acm->urb_task);
585 615 rv = tty_port_block_til_ready(&acm->port, tty, filp);
586done: 616done:
587 mutex_unlock(&acm->mutex); 617 mutex_unlock(&acm->mutex);
588err_out: 618err_out:
@@ -593,16 +623,17 @@ full_bailout:
593 usb_kill_urb(acm->ctrlurb); 623 usb_kill_urb(acm->ctrlurb);
594bail_out: 624bail_out:
595 usb_autopm_put_interface(acm->control); 625 usb_autopm_put_interface(acm->control);
596 acm->used--; 626 acm->port.count--;
597 mutex_unlock(&acm->mutex); 627 mutex_unlock(&acm->mutex);
598early_bail: 628early_bail:
599 mutex_unlock(&open_mutex); 629 mutex_unlock(&open_mutex);
630 tty_port_tty_set(&acm->port, NULL);
600 return -EIO; 631 return -EIO;
601} 632}
602 633
603static void acm_tty_unregister(struct acm *acm) 634static void acm_tty_unregister(struct acm *acm)
604{ 635{
605 int i,nr; 636 int i, nr;
606 637
607 nr = acm->rx_buflimit; 638 nr = acm->rx_buflimit;
608 tty_unregister_device(acm_tty_driver, acm->minor); 639 tty_unregister_device(acm_tty_driver, acm->minor);
@@ -619,41 +650,56 @@ static void acm_tty_unregister(struct acm *acm)
619 650
620static int acm_tty_chars_in_buffer(struct tty_struct *tty); 651static int acm_tty_chars_in_buffer(struct tty_struct *tty);
621 652
653static void acm_port_down(struct acm *acm, int drain)
654{
655 int i, nr = acm->rx_buflimit;
656 mutex_lock(&open_mutex);
657 if (acm->dev) {
658 usb_autopm_get_interface(acm->control);
659 acm_set_control(acm, acm->ctrlout = 0);
660 /* try letting the last writes drain naturally */
661 if (drain) {
662 wait_event_interruptible_timeout(acm->drain_wait,
663 (ACM_NW == acm_wb_is_avail(acm)) || !acm->dev,
664 ACM_CLOSE_TIMEOUT * HZ);
665 }
666 usb_kill_urb(acm->ctrlurb);
667 for (i = 0; i < ACM_NW; i++)
668 usb_kill_urb(acm->wb[i].urb);
669 for (i = 0; i < nr; i++)
670 usb_kill_urb(acm->ru[i].urb);
671 acm->control->needs_remote_wakeup = 0;
672 usb_autopm_put_interface(acm->control);
673 }
674 mutex_unlock(&open_mutex);
675}
676
677static void acm_tty_hangup(struct tty_struct *tty)
678{
679 struct acm *acm = tty->driver_data;
680 tty_port_hangup(&acm->port);
681 acm_port_down(acm, 0);
682}
683
622static void acm_tty_close(struct tty_struct *tty, struct file *filp) 684static void acm_tty_close(struct tty_struct *tty, struct file *filp)
623{ 685{
624 struct acm *acm = tty->driver_data; 686 struct acm *acm = tty->driver_data;
625 int i,nr;
626 687
627 if (!acm || !acm->used) 688 /* Perform the closing process and see if we need to do the hardware
689 shutdown */
690 if (!acm || tty_port_close_start(&acm->port, tty, filp) == 0)
628 return; 691 return;
629 692 acm_port_down(acm, 0);
630 nr = acm->rx_buflimit; 693 tty_port_close_end(&acm->port, tty);
631 mutex_lock(&open_mutex); 694 mutex_lock(&open_mutex);
632 if (!--acm->used) { 695 tty_port_tty_set(&acm->port, NULL);
633 if (acm->dev) { 696 if (!acm->dev)
634 usb_autopm_get_interface(acm->control); 697 acm_tty_unregister(acm);
635 acm_set_control(acm, acm->ctrlout = 0);
636
637 /* try letting the last writes drain naturally */
638 wait_event_interruptible_timeout(acm->drain_wait,
639 (ACM_NW == acm_wb_is_avail(acm))
640 || !acm->dev,
641 ACM_CLOSE_TIMEOUT * HZ);
642
643 usb_kill_urb(acm->ctrlurb);
644 for (i = 0; i < ACM_NW; i++)
645 usb_kill_urb(acm->wb[i].urb);
646 for (i = 0; i < nr; i++)
647 usb_kill_urb(acm->ru[i].urb);
648 acm->control->needs_remote_wakeup = 0;
649 usb_autopm_put_interface(acm->control);
650 } else
651 acm_tty_unregister(acm);
652 }
653 mutex_unlock(&open_mutex); 698 mutex_unlock(&open_mutex);
654} 699}
655 700
656static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 701static int acm_tty_write(struct tty_struct *tty,
702 const unsigned char *buf, int count)
657{ 703{
658 struct acm *acm = tty->driver_data; 704 struct acm *acm = tty->driver_data;
659 int stat; 705 int stat;
@@ -669,7 +715,8 @@ static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int c
669 return 0; 715 return 0;
670 716
671 spin_lock_irqsave(&acm->write_lock, flags); 717 spin_lock_irqsave(&acm->write_lock, flags);
672 if ((wbn = acm_wb_alloc(acm)) < 0) { 718 wbn = acm_wb_alloc(acm);
719 if (wbn < 0) {
673 spin_unlock_irqrestore(&acm->write_lock, flags); 720 spin_unlock_irqrestore(&acm->write_lock, flags);
674 return 0; 721 return 0;
675 } 722 }
@@ -681,7 +728,8 @@ static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int c
681 wb->len = count; 728 wb->len = count;
682 spin_unlock_irqrestore(&acm->write_lock, flags); 729 spin_unlock_irqrestore(&acm->write_lock, flags);
683 730
684 if ((stat = acm_write_start(acm, wbn)) < 0) 731 stat = acm_write_start(acm, wbn);
732 if (stat < 0)
685 return stat; 733 return stat;
686 return count; 734 return count;
687} 735}
@@ -702,7 +750,7 @@ static int acm_tty_chars_in_buffer(struct tty_struct *tty)
702{ 750{
703 struct acm *acm = tty->driver_data; 751 struct acm *acm = tty->driver_data;
704 if (!ACM_READY(acm)) 752 if (!ACM_READY(acm))
705 return -EINVAL; 753 return 0;
706 /* 754 /*
707 * This is inaccurate (overcounts), but it works. 755 * This is inaccurate (overcounts), but it works.
708 */ 756 */
@@ -767,8 +815,10 @@ static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
767 return -EINVAL; 815 return -EINVAL;
768 816
769 newctrl = acm->ctrlout; 817 newctrl = acm->ctrlout;
770 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); 818 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
771 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); 819 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
820 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
821 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
772 822
773 newctrl = (newctrl & ~clear) | set; 823 newctrl = (newctrl & ~clear) | set;
774 824
@@ -777,7 +827,8 @@ static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
777 return acm_set_control(acm, acm->ctrlout = newctrl); 827 return acm_set_control(acm, acm->ctrlout = newctrl);
778} 828}
779 829
780static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) 830static int acm_tty_ioctl(struct tty_struct *tty, struct file *file,
831 unsigned int cmd, unsigned long arg)
781{ 832{
782 struct acm *acm = tty->driver_data; 833 struct acm *acm = tty->driver_data;
783 834
@@ -799,7 +850,8 @@ static const __u8 acm_tty_size[] = {
799 5, 6, 7, 8 850 5, 6, 7, 8
800}; 851};
801 852
802static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old) 853static void acm_tty_set_termios(struct tty_struct *tty,
854 struct ktermios *termios_old)
803{ 855{
804 struct acm *acm = tty->driver_data; 856 struct acm *acm = tty->driver_data;
805 struct ktermios *termios = tty->termios; 857 struct ktermios *termios = tty->termios;
@@ -809,19 +861,23 @@ static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios
809 if (!ACM_READY(acm)) 861 if (!ACM_READY(acm))
810 return; 862 return;
811 863
864 /* FIXME: Needs to support the tty_baud interface */
865 /* FIXME: Broken on sparc */
812 newline.dwDTERate = cpu_to_le32p(acm_tty_speed + 866 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
813 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0)); 867 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
814 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; 868 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
815 newline.bParityType = termios->c_cflag & PARENB ? 869 newline.bParityType = termios->c_cflag & PARENB ?
816 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; 870 (termios->c_cflag & PARODD ? 1 : 2) +
871 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
817 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4]; 872 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
818 873 /* FIXME: Needs to clear unsupported bits in the termios */
819 acm->clocal = ((termios->c_cflag & CLOCAL) != 0); 874 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
820 875
821 if (!newline.dwDTERate) { 876 if (!newline.dwDTERate) {
822 newline.dwDTERate = acm->line.dwDTERate; 877 newline.dwDTERate = acm->line.dwDTERate;
823 newctrl &= ~ACM_CTRL_DTR; 878 newctrl &= ~ACM_CTRL_DTR;
824 } else newctrl |= ACM_CTRL_DTR; 879 } else
880 newctrl |= ACM_CTRL_DTR;
825 881
826 if (newctrl != acm->ctrlout) 882 if (newctrl != acm->ctrlout)
827 acm_set_control(acm, acm->ctrlout = newctrl); 883 acm_set_control(acm, acm->ctrlout = newctrl);
@@ -846,9 +902,8 @@ static void acm_write_buffers_free(struct acm *acm)
846 struct acm_wb *wb; 902 struct acm_wb *wb;
847 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 903 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
848 904
849 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { 905 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
850 usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah); 906 usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
851 }
852} 907}
853 908
854static void acm_read_buffers_free(struct acm *acm) 909static void acm_read_buffers_free(struct acm *acm)
@@ -857,7 +912,8 @@ static void acm_read_buffers_free(struct acm *acm)
857 int i, n = acm->rx_buflimit; 912 int i, n = acm->rx_buflimit;
858 913
859 for (i = 0; i < n; i++) 914 for (i = 0; i < n; i++)
860 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); 915 usb_buffer_free(usb_dev, acm->readsize,
916 acm->rb[i].base, acm->rb[i].dma);
861} 917}
862 918
863/* Little helper: write buffers allocate */ 919/* Little helper: write buffers allocate */
@@ -882,8 +938,8 @@ static int acm_write_buffers_alloc(struct acm *acm)
882 return 0; 938 return 0;
883} 939}
884 940
885static int acm_probe (struct usb_interface *intf, 941static int acm_probe(struct usb_interface *intf,
886 const struct usb_device_id *id) 942 const struct usb_device_id *id)
887{ 943{
888 struct usb_cdc_union_desc *union_header = NULL; 944 struct usb_cdc_union_desc *union_header = NULL;
889 struct usb_cdc_country_functional_desc *cfd = NULL; 945 struct usb_cdc_country_functional_desc *cfd = NULL;
@@ -891,13 +947,13 @@ static int acm_probe (struct usb_interface *intf,
891 int buflen = intf->altsetting->extralen; 947 int buflen = intf->altsetting->extralen;
892 struct usb_interface *control_interface; 948 struct usb_interface *control_interface;
893 struct usb_interface *data_interface; 949 struct usb_interface *data_interface;
894 struct usb_endpoint_descriptor *epctrl; 950 struct usb_endpoint_descriptor *epctrl = NULL;
895 struct usb_endpoint_descriptor *epread; 951 struct usb_endpoint_descriptor *epread = NULL;
896 struct usb_endpoint_descriptor *epwrite; 952 struct usb_endpoint_descriptor *epwrite = NULL;
897 struct usb_device *usb_dev = interface_to_usbdev(intf); 953 struct usb_device *usb_dev = interface_to_usbdev(intf);
898 struct acm *acm; 954 struct acm *acm;
899 int minor; 955 int minor;
900 int ctrlsize,readsize; 956 int ctrlsize, readsize;
901 u8 *buf; 957 u8 *buf;
902 u8 ac_management_function = 0; 958 u8 ac_management_function = 0;
903 u8 call_management_function = 0; 959 u8 call_management_function = 0;
@@ -906,6 +962,7 @@ static int acm_probe (struct usb_interface *intf,
906 unsigned long quirks; 962 unsigned long quirks;
907 int num_rx_buf; 963 int num_rx_buf;
908 int i; 964 int i;
965 int combined_interfaces = 0;
909 966
910 /* normal quirks */ 967 /* normal quirks */
911 quirks = (unsigned long)id->driver_info; 968 quirks = (unsigned long)id->driver_info;
@@ -917,7 +974,7 @@ static int acm_probe (struct usb_interface *intf,
917 control_interface = usb_ifnum_to_if(usb_dev, 0); 974 control_interface = usb_ifnum_to_if(usb_dev, 0);
918 goto skip_normal_probe; 975 goto skip_normal_probe;
919 } 976 }
920 977
921 /* normal probing*/ 978 /* normal probing*/
922 if (!buffer) { 979 if (!buffer) {
923 dev_err(&intf->dev, "Weird descriptor references\n"); 980 dev_err(&intf->dev, "Weird descriptor references\n");
@@ -925,8 +982,10 @@ static int acm_probe (struct usb_interface *intf,
925 } 982 }
926 983
927 if (!buflen) { 984 if (!buflen) {
928 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) { 985 if (intf->cur_altsetting->endpoint->extralen &&
929 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n"); 986 intf->cur_altsetting->endpoint->extra) {
987 dev_dbg(&intf->dev,
988 "Seeking extra descriptors on endpoint\n");
930 buflen = intf->cur_altsetting->endpoint->extralen; 989 buflen = intf->cur_altsetting->endpoint->extralen;
931 buffer = intf->cur_altsetting->endpoint->extra; 990 buffer = intf->cur_altsetting->endpoint->extra;
932 } else { 991 } else {
@@ -937,47 +996,43 @@ static int acm_probe (struct usb_interface *intf,
937 } 996 }
938 997
939 while (buflen > 0) { 998 while (buflen > 0) {
940 if (buffer [1] != USB_DT_CS_INTERFACE) { 999 if (buffer[1] != USB_DT_CS_INTERFACE) {
941 dev_err(&intf->dev, "skipping garbage\n"); 1000 dev_err(&intf->dev, "skipping garbage\n");
942 goto next_desc; 1001 goto next_desc;
943 } 1002 }
944 1003
945 switch (buffer [2]) { 1004 switch (buffer[2]) {
946 case USB_CDC_UNION_TYPE: /* we've found it */ 1005 case USB_CDC_UNION_TYPE: /* we've found it */
947 if (union_header) { 1006 if (union_header) {
948 dev_err(&intf->dev, "More than one " 1007 dev_err(&intf->dev, "More than one "
949 "union descriptor, " 1008 "union descriptor, skipping ...\n");
950 "skipping ...\n"); 1009 goto next_desc;
951 goto next_desc;
952 }
953 union_header = (struct usb_cdc_union_desc *)
954 buffer;
955 break;
956 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
957 cfd = (struct usb_cdc_country_functional_desc *)buffer;
958 break;
959 case USB_CDC_HEADER_TYPE: /* maybe check version */
960 break; /* for now we ignore it */
961 case USB_CDC_ACM_TYPE:
962 ac_management_function = buffer[3];
963 break;
964 case USB_CDC_CALL_MANAGEMENT_TYPE:
965 call_management_function = buffer[3];
966 call_interface_num = buffer[4];
967 if ((call_management_function & 3) != 3)
968 dev_err(&intf->dev, "This device "
969 "cannot do calls on its own. "
970 "It is no modem.\n");
971 break;
972 default:
973 /* there are LOTS more CDC descriptors that
974 * could legitimately be found here.
975 */
976 dev_dbg(&intf->dev, "Ignoring descriptor: "
977 "type %02x, length %d\n",
978 buffer[2], buffer[0]);
979 break;
980 } 1010 }
1011 union_header = (struct usb_cdc_union_desc *)buffer;
1012 break;
1013 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1014 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1015 break;
1016 case USB_CDC_HEADER_TYPE: /* maybe check version */
1017 break; /* for now we ignore it */
1018 case USB_CDC_ACM_TYPE:
1019 ac_management_function = buffer[3];
1020 break;
1021 case USB_CDC_CALL_MANAGEMENT_TYPE:
1022 call_management_function = buffer[3];
1023 call_interface_num = buffer[4];
1024 if ((call_management_function & 3) != 3)
1025 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1026 break;
1027 default:
1028 /* there are LOTS more CDC descriptors that
1029 * could legitimately be found here.
1030 */
1031 dev_dbg(&intf->dev, "Ignoring descriptor: "
1032 "type %02x, length %d\n",
1033 buffer[2], buffer[0]);
1034 break;
1035 }
981next_desc: 1036next_desc:
982 buflen -= buffer[0]; 1037 buflen -= buffer[0];
983 buffer += buffer[0]; 1038 buffer += buffer[0];
@@ -985,33 +1040,72 @@ next_desc:
985 1040
986 if (!union_header) { 1041 if (!union_header) {
987 if (call_interface_num > 0) { 1042 if (call_interface_num > 0) {
988 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n"); 1043 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
989 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); 1044 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
990 control_interface = intf; 1045 control_interface = intf;
991 } else { 1046 } else {
992 dev_dbg(&intf->dev,"No union descriptor, giving up\n"); 1047 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
993 return -ENODEV; 1048 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1049 return -ENODEV;
1050 } else {
1051 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1052 combined_interfaces = 1;
1053 control_interface = data_interface = intf;
1054 goto look_for_collapsed_interface;
1055 }
994 } 1056 }
995 } else { 1057 } else {
996 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); 1058 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
997 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); 1059 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
998 if (!control_interface || !data_interface) { 1060 if (!control_interface || !data_interface) {
999 dev_dbg(&intf->dev,"no interfaces\n"); 1061 dev_dbg(&intf->dev, "no interfaces\n");
1000 return -ENODEV; 1062 return -ENODEV;
1001 } 1063 }
1002 } 1064 }
1003 1065
1004 if (data_interface_num != call_interface_num) 1066 if (data_interface_num != call_interface_num)
1005 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n"); 1067 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1068
1069 if (control_interface == data_interface) {
1070 /* some broken devices designed for windows work this way */
1071 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1072 combined_interfaces = 1;
1073 /* a popular other OS doesn't use it */
1074 quirks |= NO_CAP_LINE;
1075 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1076 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1077 return -EINVAL;
1078 }
1079look_for_collapsed_interface:
1080 for (i = 0; i < 3; i++) {
1081 struct usb_endpoint_descriptor *ep;
1082 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1083
1084 if (usb_endpoint_is_int_in(ep))
1085 epctrl = ep;
1086 else if (usb_endpoint_is_bulk_out(ep))
1087 epwrite = ep;
1088 else if (usb_endpoint_is_bulk_in(ep))
1089 epread = ep;
1090 else
1091 return -EINVAL;
1092 }
1093 if (!epctrl || !epread || !epwrite)
1094 return -ENODEV;
1095 else
1096 goto made_compressed_probe;
1097 }
1006 1098
1007skip_normal_probe: 1099skip_normal_probe:
1008 1100
1009 /*workaround for switched interfaces */ 1101 /*workaround for switched interfaces */
1010 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) { 1102 if (data_interface->cur_altsetting->desc.bInterfaceClass
1011 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) { 1103 != CDC_DATA_INTERFACE_TYPE) {
1104 if (control_interface->cur_altsetting->desc.bInterfaceClass
1105 == CDC_DATA_INTERFACE_TYPE) {
1012 struct usb_interface *t; 1106 struct usb_interface *t;
1013 dev_dbg(&intf->dev,"Your device has switched interfaces.\n"); 1107 dev_dbg(&intf->dev,
1014 1108 "Your device has switched interfaces.\n");
1015 t = control_interface; 1109 t = control_interface;
1016 control_interface = data_interface; 1110 control_interface = data_interface;
1017 data_interface = t; 1111 data_interface = t;
@@ -1021,11 +1115,12 @@ skip_normal_probe:
1021 } 1115 }
1022 1116
1023 /* Accept probe requests only for the control interface */ 1117 /* Accept probe requests only for the control interface */
1024 if (intf != control_interface) 1118 if (!combined_interfaces && intf != control_interface)
1025 return -ENODEV; 1119 return -ENODEV;
1026 1120
1027 if (usb_interface_claimed(data_interface)) { /* valid in this context */ 1121 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1028 dev_dbg(&intf->dev,"The data interface isn't available\n"); 1122 /* valid in this context */
1123 dev_dbg(&intf->dev, "The data interface isn't available\n");
1029 return -EBUSY; 1124 return -EBUSY;
1030 } 1125 }
1031 1126
@@ -1042,12 +1137,13 @@ skip_normal_probe:
1042 if (!usb_endpoint_dir_in(epread)) { 1137 if (!usb_endpoint_dir_in(epread)) {
1043 /* descriptors are swapped */ 1138 /* descriptors are swapped */
1044 struct usb_endpoint_descriptor *t; 1139 struct usb_endpoint_descriptor *t;
1045 dev_dbg(&intf->dev,"The data interface has switched endpoints\n"); 1140 dev_dbg(&intf->dev,
1046 1141 "The data interface has switched endpoints\n");
1047 t = epread; 1142 t = epread;
1048 epread = epwrite; 1143 epread = epwrite;
1049 epwrite = t; 1144 epwrite = t;
1050 } 1145 }
1146made_compressed_probe:
1051 dbg("interfaces are valid"); 1147 dbg("interfaces are valid");
1052 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++); 1148 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1053 1149
@@ -1056,19 +1152,24 @@ skip_normal_probe:
1056 return -ENODEV; 1152 return -ENODEV;
1057 } 1153 }
1058 1154
1059 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) { 1155 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1156 if (acm == NULL) {
1060 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n"); 1157 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1061 goto alloc_fail; 1158 goto alloc_fail;
1062 } 1159 }
1063 1160
1064 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); 1161 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1065 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2); 1162 readsize = le16_to_cpu(epread->wMaxPacketSize) *
1163 (quirks == SINGLE_RX_URB ? 1 : 2);
1164 acm->combined_interfaces = combined_interfaces;
1066 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20; 1165 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1067 acm->control = control_interface; 1166 acm->control = control_interface;
1068 acm->data = data_interface; 1167 acm->data = data_interface;
1069 acm->minor = minor; 1168 acm->minor = minor;
1070 acm->dev = usb_dev; 1169 acm->dev = usb_dev;
1071 acm->ctrl_caps = ac_management_function; 1170 acm->ctrl_caps = ac_management_function;
1171 if (quirks & NO_CAP_LINE)
1172 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1072 acm->ctrlsize = ctrlsize; 1173 acm->ctrlsize = ctrlsize;
1073 acm->readsize = readsize; 1174 acm->readsize = readsize;
1074 acm->rx_buflimit = num_rx_buf; 1175 acm->rx_buflimit = num_rx_buf;
@@ -1082,6 +1183,8 @@ skip_normal_probe:
1082 spin_lock_init(&acm->read_lock); 1183 spin_lock_init(&acm->read_lock);
1083 mutex_init(&acm->mutex); 1184 mutex_init(&acm->mutex);
1084 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); 1185 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1186 tty_port_init(&acm->port);
1187 acm->port.ops = &acm_port_ops;
1085 1188
1086 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); 1189 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1087 if (!buf) { 1190 if (!buf) {
@@ -1103,8 +1206,10 @@ skip_normal_probe:
1103 for (i = 0; i < num_rx_buf; i++) { 1206 for (i = 0; i < num_rx_buf; i++) {
1104 struct acm_ru *rcv = &(acm->ru[i]); 1207 struct acm_ru *rcv = &(acm->ru[i]);
1105 1208
1106 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { 1209 rcv->urb = usb_alloc_urb(0, GFP_KERNEL);
1107 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n"); 1210 if (rcv->urb == NULL) {
1211 dev_dbg(&intf->dev,
1212 "out of memory (read urbs usb_alloc_urb)\n");
1108 goto alloc_fail7; 1213 goto alloc_fail7;
1109 } 1214 }
1110 1215
@@ -1117,26 +1222,34 @@ skip_normal_probe:
1117 rb->base = usb_buffer_alloc(acm->dev, readsize, 1222 rb->base = usb_buffer_alloc(acm->dev, readsize,
1118 GFP_KERNEL, &rb->dma); 1223 GFP_KERNEL, &rb->dma);
1119 if (!rb->base) { 1224 if (!rb->base) {
1120 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n"); 1225 dev_dbg(&intf->dev,
1226 "out of memory (read bufs usb_buffer_alloc)\n");
1121 goto alloc_fail7; 1227 goto alloc_fail7;
1122 } 1228 }
1123 } 1229 }
1124 for(i = 0; i < ACM_NW; i++) 1230 for (i = 0; i < ACM_NW; i++) {
1125 {
1126 struct acm_wb *snd = &(acm->wb[i]); 1231 struct acm_wb *snd = &(acm->wb[i]);
1127 1232
1128 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) { 1233 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1129 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)"); 1234 if (snd->urb == NULL) {
1235 dev_dbg(&intf->dev,
1236 "out of memory (write urbs usb_alloc_urb)");
1130 goto alloc_fail7; 1237 goto alloc_fail7;
1131 } 1238 }
1132 1239
1133 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), 1240 if (usb_endpoint_xfer_int(epwrite))
1241 usb_fill_int_urb(snd->urb, usb_dev,
1242 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1243 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1244 else
1245 usb_fill_bulk_urb(snd->urb, usb_dev,
1246 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1134 NULL, acm->writesize, acm_write_bulk, snd); 1247 NULL, acm->writesize, acm_write_bulk, snd);
1135 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1248 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1136 snd->instance = acm; 1249 snd->instance = acm;
1137 } 1250 }
1138 1251
1139 usb_set_intfdata (intf, acm); 1252 usb_set_intfdata(intf, acm);
1140 1253
1141 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); 1254 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1142 if (i < 0) 1255 if (i < 0)
@@ -1147,7 +1260,8 @@ skip_normal_probe:
1147 if (!acm->country_codes) 1260 if (!acm->country_codes)
1148 goto skip_countries; 1261 goto skip_countries;
1149 acm->country_code_size = cfd->bLength - 4; 1262 acm->country_code_size = cfd->bLength - 4;
1150 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4); 1263 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1264 cfd->bLength - 4);
1151 acm->country_rel_date = cfd->iCountryCodeRelDate; 1265 acm->country_rel_date = cfd->iCountryCodeRelDate;
1152 1266
1153 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); 1267 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
@@ -1156,7 +1270,8 @@ skip_normal_probe:
1156 goto skip_countries; 1270 goto skip_countries;
1157 } 1271 }
1158 1272
1159 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate); 1273 i = device_create_file(&intf->dev,
1274 &dev_attr_iCountryCodeRelDate);
1160 if (i < 0) { 1275 if (i < 0) {
1161 kfree(acm->country_codes); 1276 kfree(acm->country_codes);
1162 goto skip_countries; 1277 goto skip_countries;
@@ -1164,8 +1279,11 @@ skip_normal_probe:
1164 } 1279 }
1165 1280
1166skip_countries: 1281skip_countries:
1167 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), 1282 usb_fill_int_urb(acm->ctrlurb, usb_dev,
1168 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); 1283 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1284 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1285 /* works around buggy devices */
1286 epctrl->bInterval ? epctrl->bInterval : 0xff);
1169 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1287 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1170 acm->ctrlurb->transfer_dma = acm->ctrl_dma; 1288 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1171 1289
@@ -1212,7 +1330,7 @@ static void stop_data_traffic(struct acm *acm)
1212 tasklet_disable(&acm->urb_task); 1330 tasklet_disable(&acm->urb_task);
1213 1331
1214 usb_kill_urb(acm->ctrlurb); 1332 usb_kill_urb(acm->ctrlurb);
1215 for(i = 0; i < ACM_NW; i++) 1333 for (i = 0; i < ACM_NW; i++)
1216 usb_kill_urb(acm->wb[i].urb); 1334 usb_kill_urb(acm->wb[i].urb);
1217 for (i = 0; i < acm->rx_buflimit; i++) 1335 for (i = 0; i < acm->rx_buflimit; i++)
1218 usb_kill_urb(acm->ru[i].urb); 1336 usb_kill_urb(acm->ru[i].urb);
@@ -1227,13 +1345,14 @@ static void acm_disconnect(struct usb_interface *intf)
1227{ 1345{
1228 struct acm *acm = usb_get_intfdata(intf); 1346 struct acm *acm = usb_get_intfdata(intf);
1229 struct usb_device *usb_dev = interface_to_usbdev(intf); 1347 struct usb_device *usb_dev = interface_to_usbdev(intf);
1348 struct tty_struct *tty;
1230 1349
1231 /* sibling interface is already cleaning up */ 1350 /* sibling interface is already cleaning up */
1232 if (!acm) 1351 if (!acm)
1233 return; 1352 return;
1234 1353
1235 mutex_lock(&open_mutex); 1354 mutex_lock(&open_mutex);
1236 if (acm->country_codes){ 1355 if (acm->country_codes) {
1237 device_remove_file(&acm->control->dev, 1356 device_remove_file(&acm->control->dev,
1238 &dev_attr_wCountryCodes); 1357 &dev_attr_wCountryCodes);
1239 device_remove_file(&acm->control->dev, 1358 device_remove_file(&acm->control->dev,
@@ -1247,22 +1366,26 @@ static void acm_disconnect(struct usb_interface *intf)
1247 stop_data_traffic(acm); 1366 stop_data_traffic(acm);
1248 1367
1249 acm_write_buffers_free(acm); 1368 acm_write_buffers_free(acm);
1250 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1369 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer,
1370 acm->ctrl_dma);
1251 acm_read_buffers_free(acm); 1371 acm_read_buffers_free(acm);
1252 1372
1253 usb_driver_release_interface(&acm_driver, intf == acm->control ? 1373 if (!acm->combined_interfaces)
1374 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1254 acm->data : acm->control); 1375 acm->data : acm->control);
1255 1376
1256 if (!acm->used) { 1377 if (acm->port.count == 0) {
1257 acm_tty_unregister(acm); 1378 acm_tty_unregister(acm);
1258 mutex_unlock(&open_mutex); 1379 mutex_unlock(&open_mutex);
1259 return; 1380 return;
1260 } 1381 }
1261 1382
1262 mutex_unlock(&open_mutex); 1383 mutex_unlock(&open_mutex);
1263 1384 tty = tty_port_tty_get(&acm->port);
1264 if (acm->tty) 1385 if (tty) {
1265 tty_hangup(acm->tty); 1386 tty_hangup(tty);
1387 tty_kref_put(tty);
1388 }
1266} 1389}
1267 1390
1268#ifdef CONFIG_PM 1391#ifdef CONFIG_PM
@@ -1297,7 +1420,7 @@ static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1297 */ 1420 */
1298 mutex_lock(&acm->mutex); 1421 mutex_lock(&acm->mutex);
1299 1422
1300 if (acm->used) 1423 if (acm->port.count)
1301 stop_data_traffic(acm); 1424 stop_data_traffic(acm);
1302 1425
1303 mutex_unlock(&acm->mutex); 1426 mutex_unlock(&acm->mutex);
@@ -1319,7 +1442,7 @@ static int acm_resume(struct usb_interface *intf)
1319 return 0; 1442 return 0;
1320 1443
1321 mutex_lock(&acm->mutex); 1444 mutex_lock(&acm->mutex);
1322 if (acm->used) { 1445 if (acm->port.count) {
1323 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); 1446 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1324 if (rv < 0) 1447 if (rv < 0)
1325 goto err_out; 1448 goto err_out;
@@ -1387,6 +1510,9 @@ static struct usb_device_id acm_ids[] = {
1387 Maybe we should define a new 1510 Maybe we should define a new
1388 quirk for this. */ 1511 quirk for this. */
1389 }, 1512 },
1513 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1514 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1515 },
1390 1516
1391 /* control interfaces with various AT-command sets */ 1517 /* control interfaces with various AT-command sets */
1392 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1518 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
@@ -1398,7 +1524,7 @@ static struct usb_device_id acm_ids[] = {
1398 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1524 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1399 USB_CDC_ACM_PROTO_AT_GSM) }, 1525 USB_CDC_ACM_PROTO_AT_GSM) },
1400 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1526 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1401 USB_CDC_ACM_PROTO_AT_3G ) }, 1527 USB_CDC_ACM_PROTO_AT_3G) },
1402 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1528 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1403 USB_CDC_ACM_PROTO_AT_CDMA) }, 1529 USB_CDC_ACM_PROTO_AT_CDMA) },
1404 1530
@@ -1406,7 +1532,7 @@ static struct usb_device_id acm_ids[] = {
1406 { } 1532 { }
1407}; 1533};
1408 1534
1409MODULE_DEVICE_TABLE (usb, acm_ids); 1535MODULE_DEVICE_TABLE(usb, acm_ids);
1410 1536
1411static struct usb_driver acm_driver = { 1537static struct usb_driver acm_driver = {
1412 .name = "cdc_acm", 1538 .name = "cdc_acm",
@@ -1429,6 +1555,7 @@ static struct usb_driver acm_driver = {
1429static const struct tty_operations acm_ops = { 1555static const struct tty_operations acm_ops = {
1430 .open = acm_tty_open, 1556 .open = acm_tty_open,
1431 .close = acm_tty_close, 1557 .close = acm_tty_close,
1558 .hangup = acm_tty_hangup,
1432 .write = acm_tty_write, 1559 .write = acm_tty_write,
1433 .write_room = acm_tty_write_room, 1560 .write_room = acm_tty_write_room,
1434 .ioctl = acm_tty_ioctl, 1561 .ioctl = acm_tty_ioctl,
@@ -1460,7 +1587,8 @@ static int __init acm_init(void)
1460 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, 1587 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1461 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1588 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1462 acm_tty_driver->init_termios = tty_std_termios; 1589 acm_tty_driver->init_termios = tty_std_termios;
1463 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 1590 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1591 HUPCL | CLOCAL;
1464 tty_set_operations(acm_tty_driver, &acm_ops); 1592 tty_set_operations(acm_tty_driver, &acm_ops);
1465 1593
1466 retval = tty_register_driver(acm_tty_driver); 1594 retval = tty_register_driver(acm_tty_driver);
@@ -1492,7 +1620,7 @@ static void __exit acm_exit(void)
1492module_init(acm_init); 1620module_init(acm_init);
1493module_exit(acm_exit); 1621module_exit(acm_exit);
1494 1622
1495MODULE_AUTHOR( DRIVER_AUTHOR ); 1623MODULE_AUTHOR(DRIVER_AUTHOR);
1496MODULE_DESCRIPTION( DRIVER_DESC ); 1624MODULE_DESCRIPTION(DRIVER_DESC);
1497MODULE_LICENSE("GPL"); 1625MODULE_LICENSE("GPL");
1498MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR); 1626MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);
diff --git a/drivers/usb/class/cdc-acm.h b/drivers/usb/class/cdc-acm.h
index 1f95e7aa1b66..1602324808ba 100644
--- a/drivers/usb/class/cdc-acm.h
+++ b/drivers/usb/class/cdc-acm.h
@@ -89,8 +89,8 @@ struct acm {
89 struct usb_device *dev; /* the corresponding usb device */ 89 struct usb_device *dev; /* the corresponding usb device */
90 struct usb_interface *control; /* control interface */ 90 struct usb_interface *control; /* control interface */
91 struct usb_interface *data; /* data interface */ 91 struct usb_interface *data; /* data interface */
92 struct tty_struct *tty; /* the corresponding tty */ 92 struct tty_port port; /* our tty port data */
93 struct urb *ctrlurb; /* urbs */ 93 struct urb *ctrlurb; /* urbs */
94 u8 *ctrl_buffer; /* buffers of urbs */ 94 u8 *ctrl_buffer; /* buffers of urbs */
95 dma_addr_t ctrl_dma; /* dma handles of buffers */ 95 dma_addr_t ctrl_dma; /* dma handles of buffers */
96 u8 *country_codes; /* country codes from device */ 96 u8 *country_codes; /* country codes from device */
@@ -120,12 +120,12 @@ struct acm {
120 unsigned int ctrlout; /* output control lines (DTR, RTS) */ 120 unsigned int ctrlout; /* output control lines (DTR, RTS) */
121 unsigned int writesize; /* max packet size for the output bulk endpoint */ 121 unsigned int writesize; /* max packet size for the output bulk endpoint */
122 unsigned int readsize,ctrlsize; /* buffer sizes for freeing */ 122 unsigned int readsize,ctrlsize; /* buffer sizes for freeing */
123 unsigned int used; /* someone has this acm's device open */
124 unsigned int minor; /* acm minor number */ 123 unsigned int minor; /* acm minor number */
125 unsigned char throttle; /* throttled by tty layer */ 124 unsigned char throttle; /* throttled by tty layer */
126 unsigned char clocal; /* termios CLOCAL */ 125 unsigned char clocal; /* termios CLOCAL */
127 unsigned int ctrl_caps; /* control capabilities from the class specific header */ 126 unsigned int ctrl_caps; /* control capabilities from the class specific header */
128 unsigned int susp_count; /* number of suspended interfaces */ 127 unsigned int susp_count; /* number of suspended interfaces */
128 int combined_interfaces:1; /* control and data collapsed */
129 struct acm_wb *delayed_wb; /* write queued for a device about to be woken */ 129 struct acm_wb *delayed_wb; /* write queued for a device about to be woken */
130}; 130};
131 131
@@ -134,3 +134,4 @@ struct acm {
134/* constants describing various quirks and errors */ 134/* constants describing various quirks and errors */
135#define NO_UNION_NORMAL 1 135#define NO_UNION_NORMAL 1
136#define SINGLE_RX_URB 2 136#define SINGLE_RX_URB 2
137#define NO_CAP_LINE 4
diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c
index 0fe434505ac4..ba589d4ca8bc 100644
--- a/drivers/usb/class/cdc-wdm.c
+++ b/drivers/usb/class/cdc-wdm.c
@@ -15,7 +15,6 @@
15#include <linux/errno.h> 15#include <linux/errno.h>
16#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/smp_lock.h>
19#include <linux/mutex.h> 18#include <linux/mutex.h>
20#include <linux/uaccess.h> 19#include <linux/uaccess.h>
21#include <linux/bitops.h> 20#include <linux/bitops.h>
diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index d2747a49b974..26c09f0257db 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -1057,8 +1057,14 @@ static const struct file_operations usblp_fops = {
1057 .release = usblp_release, 1057 .release = usblp_release,
1058}; 1058};
1059 1059
1060static char *usblp_nodename(struct device *dev)
1061{
1062 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
1063}
1064
1060static struct usb_class_driver usblp_class = { 1065static struct usb_class_driver usblp_class = {
1061 .name = "lp%d", 1066 .name = "lp%d",
1067 .nodename = usblp_nodename,
1062 .fops = &usblp_fops, 1068 .fops = &usblp_fops,
1063 .minor_base = USBLP_MINOR_BASE, 1069 .minor_base = USBLP_MINOR_BASE,
1064}; 1070};
diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index c40a9b284cc9..b09a527f7341 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -751,7 +751,7 @@ static int get_capabilities(struct usbtmc_device_data *data)
751{ 751{
752 struct device *dev = &data->usb_dev->dev; 752 struct device *dev = &data->usb_dev->dev;
753 char *buffer; 753 char *buffer;
754 int rv; 754 int rv = 0;
755 755
756 buffer = kmalloc(0x18, GFP_KERNEL); 756 buffer = kmalloc(0x18, GFP_KERNEL);
757 if (!buffer) 757 if (!buffer)
@@ -763,7 +763,7 @@ static int get_capabilities(struct usbtmc_device_data *data)
763 0, 0, buffer, 0x18, USBTMC_TIMEOUT); 763 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
764 if (rv < 0) { 764 if (rv < 0) {
765 dev_err(dev, "usb_control_msg returned %d\n", rv); 765 dev_err(dev, "usb_control_msg returned %d\n", rv);
766 return rv; 766 goto err_out;
767 } 767 }
768 768
769 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); 769 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
@@ -773,7 +773,8 @@ static int get_capabilities(struct usbtmc_device_data *data)
773 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]); 773 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
774 if (buffer[0] != USBTMC_STATUS_SUCCESS) { 774 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
775 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]); 775 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
776 return -EPERM; 776 rv = -EPERM;
777 goto err_out;
777 } 778 }
778 779
779 data->capabilities.interface_capabilities = buffer[4]; 780 data->capabilities.interface_capabilities = buffer[4];
@@ -781,8 +782,9 @@ static int get_capabilities(struct usbtmc_device_data *data)
781 data->capabilities.usb488_interface_capabilities = buffer[14]; 782 data->capabilities.usb488_interface_capabilities = buffer[14];
782 data->capabilities.usb488_device_capabilities = buffer[15]; 783 data->capabilities.usb488_device_capabilities = buffer[15];
783 784
785err_out:
784 kfree(buffer); 786 kfree(buffer);
785 return 0; 787 return rv;
786} 788}
787 789
788#define capability_attribute(name) \ 790#define capability_attribute(name) \
@@ -927,21 +929,27 @@ static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
927 switch (cmd) { 929 switch (cmd) {
928 case USBTMC_IOCTL_CLEAR_OUT_HALT: 930 case USBTMC_IOCTL_CLEAR_OUT_HALT:
929 retval = usbtmc_ioctl_clear_out_halt(data); 931 retval = usbtmc_ioctl_clear_out_halt(data);
932 break;
930 933
931 case USBTMC_IOCTL_CLEAR_IN_HALT: 934 case USBTMC_IOCTL_CLEAR_IN_HALT:
932 retval = usbtmc_ioctl_clear_in_halt(data); 935 retval = usbtmc_ioctl_clear_in_halt(data);
936 break;
933 937
934 case USBTMC_IOCTL_INDICATOR_PULSE: 938 case USBTMC_IOCTL_INDICATOR_PULSE:
935 retval = usbtmc_ioctl_indicator_pulse(data); 939 retval = usbtmc_ioctl_indicator_pulse(data);
940 break;
936 941
937 case USBTMC_IOCTL_CLEAR: 942 case USBTMC_IOCTL_CLEAR:
938 retval = usbtmc_ioctl_clear(data); 943 retval = usbtmc_ioctl_clear(data);
944 break;
939 945
940 case USBTMC_IOCTL_ABORT_BULK_OUT: 946 case USBTMC_IOCTL_ABORT_BULK_OUT:
941 retval = usbtmc_ioctl_abort_bulk_out(data); 947 retval = usbtmc_ioctl_abort_bulk_out(data);
948 break;
942 949
943 case USBTMC_IOCTL_ABORT_BULK_IN: 950 case USBTMC_IOCTL_ABORT_BULK_IN:
944 retval = usbtmc_ioctl_abort_bulk_in(data); 951 retval = usbtmc_ioctl_abort_bulk_in(data);
952 break;
945 } 953 }
946 954
947 mutex_unlock(&data->io_mutex); 955 mutex_unlock(&data->io_mutex);
diff --git a/drivers/usb/core/Kconfig b/drivers/usb/core/Kconfig
index e1759d17ac5d..ad925946f869 100644
--- a/drivers/usb/core/Kconfig
+++ b/drivers/usb/core/Kconfig
@@ -28,7 +28,7 @@ comment "Miscellaneous USB options"
28 depends on USB 28 depends on USB
29 29
30config USB_DEVICEFS 30config USB_DEVICEFS
31 bool "USB device filesystem" 31 bool "USB device filesystem (DEPRECATED)"
32 depends on USB 32 depends on USB
33 ---help--- 33 ---help---
34 If you say Y here (and to "/proc file system support" in the "File 34 If you say Y here (and to "/proc file system support" in the "File
@@ -46,11 +46,15 @@ config USB_DEVICEFS
46 For the format of the various /proc/bus/usb/ files, please read 46 For the format of the various /proc/bus/usb/ files, please read
47 <file:Documentation/usb/proc_usb_info.txt>. 47 <file:Documentation/usb/proc_usb_info.txt>.
48 48
49 Usbfs files can't handle Access Control Lists (ACL), which are the 49 Modern Linux systems do not use this.
50 default way to grant access to USB devices for untrusted users of a 50
51 desktop system. The usbfs functionality is replaced by real 51 Usbfs entries are files and not character devices; usbfs can't
52 device-nodes managed by udev. These nodes live in /dev/bus/usb and 52 handle Access Control Lists (ACL) which are the default way to
53 are used by libusb. 53 grant access to USB devices for untrusted users of a desktop
54 system.
55
56 The usbfs functionality is replaced by real device-nodes managed by
57 udev. These nodes lived in /dev/bus/usb and are used by libusb.
54 58
55config USB_DEVICE_CLASS 59config USB_DEVICE_CLASS
56 bool "USB device class-devices (DEPRECATED)" 60 bool "USB device class-devices (DEPRECATED)"
diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index b6078706fb93..ec16e6029905 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -4,14 +4,14 @@
4 4
5usbcore-objs := usb.o hub.o hcd.o urb.o message.o driver.o \ 5usbcore-objs := usb.o hub.o hcd.o urb.o message.o driver.o \
6 config.o file.o buffer.o sysfs.o endpoint.o \ 6 config.o file.o buffer.o sysfs.o endpoint.o \
7 devio.o notify.o generic.o quirks.o 7 devio.o notify.o generic.o quirks.o devices.o
8 8
9ifeq ($(CONFIG_PCI),y) 9ifeq ($(CONFIG_PCI),y)
10 usbcore-objs += hcd-pci.o 10 usbcore-objs += hcd-pci.o
11endif 11endif
12 12
13ifeq ($(CONFIG_USB_DEVICEFS),y) 13ifeq ($(CONFIG_USB_DEVICEFS),y)
14 usbcore-objs += inode.o devices.o 14 usbcore-objs += inode.o
15endif 15endif
16 16
17obj-$(CONFIG_USB) += usbcore.o 17obj-$(CONFIG_USB) += usbcore.o
diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c
index 568244c99bdc..24dfb33f90cb 100644
--- a/drivers/usb/core/config.c
+++ b/drivers/usb/core/config.c
@@ -19,6 +19,32 @@ static inline const char *plural(int n)
19 return (n == 1 ? "" : "s"); 19 return (n == 1 ? "" : "s");
20} 20}
21 21
22/* FIXME: this is a kludge */
23static int find_next_descriptor_more(unsigned char *buffer, int size,
24 int dt1, int dt2, int dt3, int *num_skipped)
25{
26 struct usb_descriptor_header *h;
27 int n = 0;
28 unsigned char *buffer0 = buffer;
29
30 /* Find the next descriptor of type dt1 or dt2 or dt3 */
31 while (size > 0) {
32 h = (struct usb_descriptor_header *) buffer;
33 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
34 h->bDescriptorType == dt3)
35 break;
36 buffer += h->bLength;
37 size -= h->bLength;
38 ++n;
39 }
40
41 /* Store the number of descriptors skipped and return the
42 * number of bytes skipped */
43 if (num_skipped)
44 *num_skipped = n;
45 return buffer - buffer0;
46}
47
22static int find_next_descriptor(unsigned char *buffer, int size, 48static int find_next_descriptor(unsigned char *buffer, int size,
23 int dt1, int dt2, int *num_skipped) 49 int dt1, int dt2, int *num_skipped)
24{ 50{
@@ -43,6 +69,129 @@ static int find_next_descriptor(unsigned char *buffer, int size,
43 return buffer - buffer0; 69 return buffer - buffer0;
44} 70}
45 71
72static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
73 int inum, int asnum, struct usb_host_endpoint *ep,
74 int num_ep, unsigned char *buffer, int size)
75{
76 unsigned char *buffer_start = buffer;
77 struct usb_ss_ep_comp_descriptor *desc;
78 int retval;
79 int num_skipped;
80 int max_tx;
81 int i;
82
83 /* Allocate space for the SS endpoint companion descriptor */
84 ep->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp),
85 GFP_KERNEL);
86 if (!ep->ss_ep_comp)
87 return -ENOMEM;
88 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
89 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
90 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
91 " interface %d altsetting %d ep %d: "
92 "using minimum values\n",
93 cfgno, inum, asnum, ep->desc.bEndpointAddress);
94 ep->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE;
95 ep->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
96 ep->ss_ep_comp->desc.bMaxBurst = 0;
97 /*
98 * Leave bmAttributes as zero, which will mean no streams for
99 * bulk, and isoc won't support multiple bursts of packets.
100 * With bursts of only one packet, and a Mult of 1, the max
101 * amount of data moved per endpoint service interval is one
102 * packet.
103 */
104 if (usb_endpoint_xfer_isoc(&ep->desc) ||
105 usb_endpoint_xfer_int(&ep->desc))
106 ep->ss_ep_comp->desc.wBytesPerInterval =
107 ep->desc.wMaxPacketSize;
108 /*
109 * The next descriptor is for an Endpoint or Interface,
110 * no extra descriptors to copy into the companion structure,
111 * and we didn't eat up any of the buffer.
112 */
113 retval = 0;
114 goto valid;
115 }
116 memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE);
117 desc = &ep->ss_ep_comp->desc;
118 buffer += desc->bLength;
119 size -= desc->bLength;
120
121 /* Eat up the other descriptors we don't care about */
122 ep->ss_ep_comp->extra = buffer;
123 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
124 USB_DT_INTERFACE, &num_skipped);
125 ep->ss_ep_comp->extralen = i;
126 buffer += i;
127 size -= i;
128 retval = buffer - buffer_start + i;
129 if (num_skipped > 0)
130 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
131 num_skipped, plural(num_skipped),
132 "SuperSpeed endpoint companion");
133
134 /* Check the various values */
135 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
136 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
137 "config %d interface %d altsetting %d ep %d: "
138 "setting to zero\n", desc->bMaxBurst,
139 cfgno, inum, asnum, ep->desc.bEndpointAddress);
140 desc->bMaxBurst = 0;
141 }
142 if (desc->bMaxBurst > 15) {
143 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
144 "config %d interface %d altsetting %d ep %d: "
145 "setting to 15\n", desc->bMaxBurst,
146 cfgno, inum, asnum, ep->desc.bEndpointAddress);
147 desc->bMaxBurst = 15;
148 }
149 if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
150 && desc->bmAttributes != 0) {
151 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
152 "config %d interface %d altsetting %d ep %d: "
153 "setting to zero\n",
154 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
155 desc->bmAttributes,
156 cfgno, inum, asnum, ep->desc.bEndpointAddress);
157 desc->bmAttributes = 0;
158 }
159 if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
160 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
161 "config %d interface %d altsetting %d ep %d: "
162 "setting to max\n",
163 cfgno, inum, asnum, ep->desc.bEndpointAddress);
164 desc->bmAttributes = 16;
165 }
166 if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
167 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
168 "config %d interface %d altsetting %d ep %d: "
169 "setting to 3\n", desc->bmAttributes + 1,
170 cfgno, inum, asnum, ep->desc.bEndpointAddress);
171 desc->bmAttributes = 2;
172 }
173 if (usb_endpoint_xfer_isoc(&ep->desc)) {
174 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
175 (desc->bmAttributes + 1);
176 } else if (usb_endpoint_xfer_int(&ep->desc)) {
177 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
178 } else {
179 goto valid;
180 }
181 if (desc->wBytesPerInterval > max_tx) {
182 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
183 "config %d interface %d altsetting %d ep %d: "
184 "setting to %d\n",
185 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
186 desc->wBytesPerInterval,
187 cfgno, inum, asnum, ep->desc.bEndpointAddress,
188 max_tx);
189 desc->wBytesPerInterval = max_tx;
190 }
191valid:
192 return retval;
193}
194
46static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum, 195static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
47 int asnum, struct usb_host_interface *ifp, int num_ep, 196 int asnum, struct usb_host_interface *ifp, int num_ep,
48 unsigned char *buffer, int size) 197 unsigned char *buffer, int size)
@@ -50,7 +199,7 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
50 unsigned char *buffer0 = buffer; 199 unsigned char *buffer0 = buffer;
51 struct usb_endpoint_descriptor *d; 200 struct usb_endpoint_descriptor *d;
52 struct usb_host_endpoint *endpoint; 201 struct usb_host_endpoint *endpoint;
53 int n, i, j; 202 int n, i, j, retval;
54 203
55 d = (struct usb_endpoint_descriptor *) buffer; 204 d = (struct usb_endpoint_descriptor *) buffer;
56 buffer += d->bLength; 205 buffer += d->bLength;
@@ -92,6 +241,7 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
92 if (usb_endpoint_xfer_int(d)) { 241 if (usb_endpoint_xfer_int(d)) {
93 i = 1; 242 i = 1;
94 switch (to_usb_device(ddev)->speed) { 243 switch (to_usb_device(ddev)->speed) {
244 case USB_SPEED_SUPER:
95 case USB_SPEED_HIGH: 245 case USB_SPEED_HIGH:
96 /* Many device manufacturers are using full-speed 246 /* Many device manufacturers are using full-speed
97 * bInterval values in high-speed interrupt endpoint 247 * bInterval values in high-speed interrupt endpoint
@@ -161,17 +311,39 @@ static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
161 cfgno, inum, asnum, d->bEndpointAddress, 311 cfgno, inum, asnum, d->bEndpointAddress,
162 maxp); 312 maxp);
163 } 313 }
164 314 /* Allocate room for and parse any SS endpoint companion descriptors */
165 /* Skip over any Class Specific or Vendor Specific descriptors; 315 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
166 * find the next endpoint or interface descriptor */ 316 endpoint->extra = buffer;
167 endpoint->extra = buffer; 317 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
168 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, 318 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
169 USB_DT_INTERFACE, &n); 319 endpoint->extralen = i;
170 endpoint->extralen = i; 320 buffer += i;
321 size -= i;
322
323 if (size > 0) {
324 retval = usb_parse_ss_endpoint_companion(ddev, cfgno,
325 inum, asnum, endpoint, num_ep, buffer,
326 size);
327 if (retval >= 0) {
328 buffer += retval;
329 retval = buffer - buffer0;
330 }
331 } else {
332 retval = buffer - buffer0;
333 }
334 } else {
335 /* Skip over any Class Specific or Vendor Specific descriptors;
336 * find the next endpoint or interface descriptor */
337 endpoint->extra = buffer;
338 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
339 USB_DT_INTERFACE, &n);
340 endpoint->extralen = i;
341 retval = buffer - buffer0 + i;
342 }
171 if (n > 0) 343 if (n > 0)
172 dev_dbg(ddev, "skipped %d descriptor%s after %s\n", 344 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
173 n, plural(n), "endpoint"); 345 n, plural(n), "endpoint");
174 return buffer - buffer0 + i; 346 return retval;
175 347
176skip_to_next_endpoint_or_interface_descriptor: 348skip_to_next_endpoint_or_interface_descriptor:
177 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT, 349 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
@@ -452,6 +624,8 @@ static int usb_parse_configuration(struct device *ddev, int cfgidx,
452 kref_init(&intfc->ref); 624 kref_init(&intfc->ref);
453 } 625 }
454 626
627 /* FIXME: parse the BOS descriptor */
628
455 /* Skip over any Class Specific or Vendor Specific descriptors; 629 /* Skip over any Class Specific or Vendor Specific descriptors;
456 * find the first interface descriptor */ 630 * find the first interface descriptor */
457 config->extra = buffer; 631 config->extra = buffer;
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 73c108d117b4..96f11715cd26 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -136,17 +136,19 @@ static const struct class_info clas_info[] =
136 {USB_CLASS_AUDIO, "audio"}, 136 {USB_CLASS_AUDIO, "audio"},
137 {USB_CLASS_COMM, "comm."}, 137 {USB_CLASS_COMM, "comm."},
138 {USB_CLASS_HID, "HID"}, 138 {USB_CLASS_HID, "HID"},
139 {USB_CLASS_HUB, "hub"},
140 {USB_CLASS_PHYSICAL, "PID"}, 139 {USB_CLASS_PHYSICAL, "PID"},
140 {USB_CLASS_STILL_IMAGE, "still"},
141 {USB_CLASS_PRINTER, "print"}, 141 {USB_CLASS_PRINTER, "print"},
142 {USB_CLASS_MASS_STORAGE, "stor."}, 142 {USB_CLASS_MASS_STORAGE, "stor."},
143 {USB_CLASS_HUB, "hub"},
143 {USB_CLASS_CDC_DATA, "data"}, 144 {USB_CLASS_CDC_DATA, "data"},
144 {USB_CLASS_APP_SPEC, "app."},
145 {USB_CLASS_VENDOR_SPEC, "vend."},
146 {USB_CLASS_STILL_IMAGE, "still"},
147 {USB_CLASS_CSCID, "scard"}, 145 {USB_CLASS_CSCID, "scard"},
148 {USB_CLASS_CONTENT_SEC, "c-sec"}, 146 {USB_CLASS_CONTENT_SEC, "c-sec"},
149 {USB_CLASS_VIDEO, "video"}, 147 {USB_CLASS_VIDEO, "video"},
148 {USB_CLASS_WIRELESS_CONTROLLER, "wlcon"},
149 {USB_CLASS_MISC, "misc"},
150 {USB_CLASS_APP_SPEC, "app."},
151 {USB_CLASS_VENDOR_SPEC, "vend."},
150 {-1, "unk."} /* leave as last */ 152 {-1, "unk."} /* leave as last */
151}; 153};
152 154
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 308609039c73..38b8bce782d6 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -325,21 +325,34 @@ static void async_completed(struct urb *urb)
325 struct async *as = urb->context; 325 struct async *as = urb->context;
326 struct dev_state *ps = as->ps; 326 struct dev_state *ps = as->ps;
327 struct siginfo sinfo; 327 struct siginfo sinfo;
328 struct pid *pid = NULL;
329 uid_t uid = 0;
330 uid_t euid = 0;
331 u32 secid = 0;
332 int signr;
328 333
329 spin_lock(&ps->lock); 334 spin_lock(&ps->lock);
330 list_move_tail(&as->asynclist, &ps->async_completed); 335 list_move_tail(&as->asynclist, &ps->async_completed);
331 spin_unlock(&ps->lock);
332 as->status = urb->status; 336 as->status = urb->status;
333 if (as->signr) { 337 signr = as->signr;
338 if (signr) {
334 sinfo.si_signo = as->signr; 339 sinfo.si_signo = as->signr;
335 sinfo.si_errno = as->status; 340 sinfo.si_errno = as->status;
336 sinfo.si_code = SI_ASYNCIO; 341 sinfo.si_code = SI_ASYNCIO;
337 sinfo.si_addr = as->userurb; 342 sinfo.si_addr = as->userurb;
338 kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid, 343 pid = as->pid;
339 as->euid, as->secid); 344 uid = as->uid;
345 euid = as->euid;
346 secid = as->secid;
340 } 347 }
341 snoop(&urb->dev->dev, "urb complete\n"); 348 snoop(&urb->dev->dev, "urb complete\n");
342 snoop_urb(urb, as->userurb); 349 snoop_urb(urb, as->userurb);
350 spin_unlock(&ps->lock);
351
352 if (signr)
353 kill_pid_info_as_uid(sinfo.si_signo, &sinfo, pid, uid,
354 euid, secid);
355
343 wake_up(&ps->wait); 356 wake_up(&ps->wait);
344} 357}
345 358
@@ -982,7 +995,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
982 USBDEVFS_URB_ZERO_PACKET | 995 USBDEVFS_URB_ZERO_PACKET |
983 USBDEVFS_URB_NO_INTERRUPT)) 996 USBDEVFS_URB_NO_INTERRUPT))
984 return -EINVAL; 997 return -EINVAL;
985 if (!uurb->buffer) 998 if (uurb->buffer_length > 0 && !uurb->buffer)
986 return -EINVAL; 999 return -EINVAL;
987 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && 1000 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
988 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { 1001 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
@@ -1038,11 +1051,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1038 is_in = 0; 1051 is_in = 0;
1039 uurb->endpoint &= ~USB_DIR_IN; 1052 uurb->endpoint &= ~USB_DIR_IN;
1040 } 1053 }
1041 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1042 uurb->buffer, uurb->buffer_length)) {
1043 kfree(dr);
1044 return -EFAULT;
1045 }
1046 snoop(&ps->dev->dev, "control urb: bRequest=%02x " 1054 snoop(&ps->dev->dev, "control urb: bRequest=%02x "
1047 "bRrequestType=%02x wValue=%04x " 1055 "bRrequestType=%02x wValue=%04x "
1048 "wIndex=%04x wLength=%04x\n", 1056 "wIndex=%04x wLength=%04x\n",
@@ -1062,9 +1070,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1062 uurb->number_of_packets = 0; 1070 uurb->number_of_packets = 0;
1063 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) 1071 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1064 return -EINVAL; 1072 return -EINVAL;
1065 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1066 uurb->buffer, uurb->buffer_length))
1067 return -EFAULT;
1068 snoop(&ps->dev->dev, "bulk urb\n"); 1073 snoop(&ps->dev->dev, "bulk urb\n");
1069 break; 1074 break;
1070 1075
@@ -1106,28 +1111,35 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1106 return -EINVAL; 1111 return -EINVAL;
1107 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE) 1112 if (uurb->buffer_length > MAX_USBFS_BUFFER_SIZE)
1108 return -EINVAL; 1113 return -EINVAL;
1109 if (!access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1110 uurb->buffer, uurb->buffer_length))
1111 return -EFAULT;
1112 snoop(&ps->dev->dev, "interrupt urb\n"); 1114 snoop(&ps->dev->dev, "interrupt urb\n");
1113 break; 1115 break;
1114 1116
1115 default: 1117 default:
1116 return -EINVAL; 1118 return -EINVAL;
1117 } 1119 }
1118 as = alloc_async(uurb->number_of_packets); 1120 if (uurb->buffer_length > 0 &&
1119 if (!as) { 1121 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1122 uurb->buffer, uurb->buffer_length)) {
1120 kfree(isopkt); 1123 kfree(isopkt);
1121 kfree(dr); 1124 kfree(dr);
1122 return -ENOMEM; 1125 return -EFAULT;
1123 } 1126 }
1124 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, GFP_KERNEL); 1127 as = alloc_async(uurb->number_of_packets);
1125 if (!as->urb->transfer_buffer) { 1128 if (!as) {
1126 kfree(isopkt); 1129 kfree(isopkt);
1127 kfree(dr); 1130 kfree(dr);
1128 free_async(as);
1129 return -ENOMEM; 1131 return -ENOMEM;
1130 } 1132 }
1133 if (uurb->buffer_length > 0) {
1134 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1135 GFP_KERNEL);
1136 if (!as->urb->transfer_buffer) {
1137 kfree(isopkt);
1138 kfree(dr);
1139 free_async(as);
1140 return -ENOMEM;
1141 }
1142 }
1131 as->urb->dev = ps->dev; 1143 as->urb->dev = ps->dev;
1132 as->urb->pipe = (uurb->type << 30) | 1144 as->urb->pipe = (uurb->type << 30) |
1133 __create_pipe(ps->dev, uurb->endpoint & 0xf) | 1145 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
@@ -1169,7 +1181,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1169 kfree(isopkt); 1181 kfree(isopkt);
1170 as->ps = ps; 1182 as->ps = ps;
1171 as->userurb = arg; 1183 as->userurb = arg;
1172 if (uurb->endpoint & USB_DIR_IN) 1184 if (is_in && uurb->buffer_length > 0)
1173 as->userbuffer = uurb->buffer; 1185 as->userbuffer = uurb->buffer;
1174 else 1186 else
1175 as->userbuffer = NULL; 1187 as->userbuffer = NULL;
@@ -1179,9 +1191,9 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
1179 as->uid = cred->uid; 1191 as->uid = cred->uid;
1180 as->euid = cred->euid; 1192 as->euid = cred->euid;
1181 security_task_getsecid(current, &as->secid); 1193 security_task_getsecid(current, &as->secid);
1182 if (!is_in) { 1194 if (!is_in && uurb->buffer_length > 0) {
1183 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, 1195 if (copy_from_user(as->urb->transfer_buffer, uurb->buffer,
1184 as->urb->transfer_buffer_length)) { 1196 uurb->buffer_length)) {
1185 free_async(as); 1197 free_async(as);
1186 return -EFAULT; 1198 return -EFAULT;
1187 } 1199 }
@@ -1231,22 +1243,22 @@ static int processcompl(struct async *as, void __user * __user *arg)
1231 if (as->userbuffer) 1243 if (as->userbuffer)
1232 if (copy_to_user(as->userbuffer, urb->transfer_buffer, 1244 if (copy_to_user(as->userbuffer, urb->transfer_buffer,
1233 urb->transfer_buffer_length)) 1245 urb->transfer_buffer_length))
1234 return -EFAULT; 1246 goto err_out;
1235 if (put_user(as->status, &userurb->status)) 1247 if (put_user(as->status, &userurb->status))
1236 return -EFAULT; 1248 goto err_out;
1237 if (put_user(urb->actual_length, &userurb->actual_length)) 1249 if (put_user(urb->actual_length, &userurb->actual_length))
1238 return -EFAULT; 1250 goto err_out;
1239 if (put_user(urb->error_count, &userurb->error_count)) 1251 if (put_user(urb->error_count, &userurb->error_count))
1240 return -EFAULT; 1252 goto err_out;
1241 1253
1242 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 1254 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1243 for (i = 0; i < urb->number_of_packets; i++) { 1255 for (i = 0; i < urb->number_of_packets; i++) {
1244 if (put_user(urb->iso_frame_desc[i].actual_length, 1256 if (put_user(urb->iso_frame_desc[i].actual_length,
1245 &userurb->iso_frame_desc[i].actual_length)) 1257 &userurb->iso_frame_desc[i].actual_length))
1246 return -EFAULT; 1258 goto err_out;
1247 if (put_user(urb->iso_frame_desc[i].status, 1259 if (put_user(urb->iso_frame_desc[i].status,
1248 &userurb->iso_frame_desc[i].status)) 1260 &userurb->iso_frame_desc[i].status))
1249 return -EFAULT; 1261 goto err_out;
1250 } 1262 }
1251 } 1263 }
1252 1264
@@ -1255,6 +1267,10 @@ static int processcompl(struct async *as, void __user * __user *arg)
1255 if (put_user(addr, (void __user * __user *)arg)) 1267 if (put_user(addr, (void __user * __user *)arg))
1256 return -EFAULT; 1268 return -EFAULT;
1257 return 0; 1269 return 0;
1270
1271err_out:
1272 free_async(as);
1273 return -EFAULT;
1258} 1274}
1259 1275
1260static struct async *reap_as(struct dev_state *ps) 1276static struct async *reap_as(struct dev_state *ps)
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index d0a21a5f8201..69e5773abfce 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -154,16 +154,11 @@ static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *in
154static int usb_probe_device(struct device *dev) 154static int usb_probe_device(struct device *dev)
155{ 155{
156 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 156 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
157 struct usb_device *udev; 157 struct usb_device *udev = to_usb_device(dev);
158 int error = -ENODEV; 158 int error = -ENODEV;
159 159
160 dev_dbg(dev, "%s\n", __func__); 160 dev_dbg(dev, "%s\n", __func__);
161 161
162 if (!is_usb_device(dev)) /* Sanity check */
163 return error;
164
165 udev = to_usb_device(dev);
166
167 /* TODO: Add real matching code */ 162 /* TODO: Add real matching code */
168 163
169 /* The device should always appear to be in use 164 /* The device should always appear to be in use
@@ -203,18 +198,13 @@ static void usb_cancel_queued_reset(struct usb_interface *iface)
203static int usb_probe_interface(struct device *dev) 198static int usb_probe_interface(struct device *dev)
204{ 199{
205 struct usb_driver *driver = to_usb_driver(dev->driver); 200 struct usb_driver *driver = to_usb_driver(dev->driver);
206 struct usb_interface *intf; 201 struct usb_interface *intf = to_usb_interface(dev);
207 struct usb_device *udev; 202 struct usb_device *udev = interface_to_usbdev(intf);
208 const struct usb_device_id *id; 203 const struct usb_device_id *id;
209 int error = -ENODEV; 204 int error = -ENODEV;
210 205
211 dev_dbg(dev, "%s\n", __func__); 206 dev_dbg(dev, "%s\n", __func__);
212 207
213 if (is_usb_device(dev)) /* Sanity check */
214 return error;
215
216 intf = to_usb_interface(dev);
217 udev = interface_to_usbdev(intf);
218 intf->needs_binding = 0; 208 intf->needs_binding = 0;
219 209
220 if (udev->authorized == 0) { 210 if (udev->authorized == 0) {
@@ -385,7 +375,6 @@ void usb_driver_release_interface(struct usb_driver *driver,
385 struct usb_interface *iface) 375 struct usb_interface *iface)
386{ 376{
387 struct device *dev = &iface->dev; 377 struct device *dev = &iface->dev;
388 struct usb_device *udev = interface_to_usbdev(iface);
389 378
390 /* this should never happen, don't release something that's not ours */ 379 /* this should never happen, don't release something that's not ours */
391 if (!dev->driver || dev->driver != &driver->drvwrap.driver) 380 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
@@ -394,23 +383,19 @@ void usb_driver_release_interface(struct usb_driver *driver,
394 /* don't release from within disconnect() */ 383 /* don't release from within disconnect() */
395 if (iface->condition != USB_INTERFACE_BOUND) 384 if (iface->condition != USB_INTERFACE_BOUND)
396 return; 385 return;
386 iface->condition = USB_INTERFACE_UNBINDING;
397 387
398 /* don't release if the interface hasn't been added yet */ 388 /* Release via the driver core only if the interface
389 * has already been registered
390 */
399 if (device_is_registered(dev)) { 391 if (device_is_registered(dev)) {
400 iface->condition = USB_INTERFACE_UNBINDING;
401 device_release_driver(dev); 392 device_release_driver(dev);
402 } else { 393 } else {
403 iface->condition = USB_INTERFACE_UNBOUND; 394 down(&dev->sem);
404 usb_cancel_queued_reset(iface); 395 usb_unbind_interface(dev);
396 dev->driver = NULL;
397 up(&dev->sem);
405 } 398 }
406 dev->driver = NULL;
407 usb_set_intfdata(iface, NULL);
408
409 usb_pm_lock(udev);
410 iface->condition = USB_INTERFACE_UNBOUND;
411 mark_quiesced(iface);
412 iface->needs_remote_wakeup = 0;
413 usb_pm_unlock(udev);
414} 399}
415EXPORT_SYMBOL_GPL(usb_driver_release_interface); 400EXPORT_SYMBOL_GPL(usb_driver_release_interface);
416 401
@@ -598,7 +583,7 @@ static int usb_device_match(struct device *dev, struct device_driver *drv)
598 /* TODO: Add real matching code */ 583 /* TODO: Add real matching code */
599 return 1; 584 return 1;
600 585
601 } else { 586 } else if (is_usb_interface(dev)) {
602 struct usb_interface *intf; 587 struct usb_interface *intf;
603 struct usb_driver *usb_drv; 588 struct usb_driver *usb_drv;
604 const struct usb_device_id *id; 589 const struct usb_device_id *id;
@@ -630,11 +615,14 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
630 /* driver is often null here; dev_dbg() would oops */ 615 /* driver is often null here; dev_dbg() would oops */
631 pr_debug("usb %s: uevent\n", dev_name(dev)); 616 pr_debug("usb %s: uevent\n", dev_name(dev));
632 617
633 if (is_usb_device(dev)) 618 if (is_usb_device(dev)) {
634 usb_dev = to_usb_device(dev); 619 usb_dev = to_usb_device(dev);
635 else { 620 } else if (is_usb_interface(dev)) {
636 struct usb_interface *intf = to_usb_interface(dev); 621 struct usb_interface *intf = to_usb_interface(dev);
622
637 usb_dev = interface_to_usbdev(intf); 623 usb_dev = interface_to_usbdev(intf);
624 } else {
625 return 0;
638 } 626 }
639 627
640 if (usb_dev->devnum < 0) { 628 if (usb_dev->devnum < 0) {
@@ -1762,6 +1750,7 @@ int usb_suspend(struct device *dev, pm_message_t msg)
1762int usb_resume(struct device *dev, pm_message_t msg) 1750int usb_resume(struct device *dev, pm_message_t msg)
1763{ 1751{
1764 struct usb_device *udev; 1752 struct usb_device *udev;
1753 int status;
1765 1754
1766 udev = to_usb_device(dev); 1755 udev = to_usb_device(dev);
1767 1756
@@ -1771,7 +1760,14 @@ int usb_resume(struct device *dev, pm_message_t msg)
1771 */ 1760 */
1772 if (udev->skip_sys_resume) 1761 if (udev->skip_sys_resume)
1773 return 0; 1762 return 0;
1774 return usb_external_resume_device(udev, msg); 1763 status = usb_external_resume_device(udev, msg);
1764
1765 /* Avoid PM error messages for devices disconnected while suspended
1766 * as we'll display regular disconnect messages just a bit later.
1767 */
1768 if (status == -ENODEV)
1769 return 0;
1770 return status;
1775} 1771}
1776 1772
1777#endif /* CONFIG_PM */ 1773#endif /* CONFIG_PM */
diff --git a/drivers/usb/core/endpoint.c b/drivers/usb/core/endpoint.c
index 40dee2ac0133..bc39fc40bbde 100644
--- a/drivers/usb/core/endpoint.c
+++ b/drivers/usb/core/endpoint.c
@@ -15,19 +15,18 @@
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include "usb.h" 16#include "usb.h"
17 17
18#define MAX_ENDPOINT_MINORS (64*128*32)
19static int usb_endpoint_major;
20static DEFINE_IDR(endpoint_idr);
21
22struct ep_device { 18struct ep_device {
23 struct usb_endpoint_descriptor *desc; 19 struct usb_endpoint_descriptor *desc;
24 struct usb_device *udev; 20 struct usb_device *udev;
25 struct device dev; 21 struct device dev;
26 int minor;
27}; 22};
28#define to_ep_device(_dev) \ 23#define to_ep_device(_dev) \
29 container_of(_dev, struct ep_device, dev) 24 container_of(_dev, struct ep_device, dev)
30 25
26struct device_type usb_ep_device_type = {
27 .name = "usb_endpoint",
28};
29
31struct ep_attribute { 30struct ep_attribute {
32 struct attribute attr; 31 struct attribute attr;
33 ssize_t (*show)(struct usb_device *, 32 ssize_t (*show)(struct usb_device *,
@@ -160,118 +159,10 @@ static struct attribute_group *ep_dev_groups[] = {
160 NULL 159 NULL
161}; 160};
162 161
163static int usb_endpoint_major_init(void)
164{
165 dev_t dev;
166 int error;
167
168 error = alloc_chrdev_region(&dev, 0, MAX_ENDPOINT_MINORS,
169 "usb_endpoint");
170 if (error) {
171 printk(KERN_ERR "Unable to get a dynamic major for "
172 "usb endpoints.\n");
173 return error;
174 }
175 usb_endpoint_major = MAJOR(dev);
176
177 return error;
178}
179
180static void usb_endpoint_major_cleanup(void)
181{
182 unregister_chrdev_region(MKDEV(usb_endpoint_major, 0),
183 MAX_ENDPOINT_MINORS);
184}
185
186static int endpoint_get_minor(struct ep_device *ep_dev)
187{
188 static DEFINE_MUTEX(minor_lock);
189 int retval = -ENOMEM;
190 int id;
191
192 mutex_lock(&minor_lock);
193 if (idr_pre_get(&endpoint_idr, GFP_KERNEL) == 0)
194 goto exit;
195
196 retval = idr_get_new(&endpoint_idr, ep_dev, &id);
197 if (retval < 0) {
198 if (retval == -EAGAIN)
199 retval = -ENOMEM;
200 goto exit;
201 }
202 ep_dev->minor = id & MAX_ID_MASK;
203exit:
204 mutex_unlock(&minor_lock);
205 return retval;
206}
207
208static void endpoint_free_minor(struct ep_device *ep_dev)
209{
210 idr_remove(&endpoint_idr, ep_dev->minor);
211}
212
213static struct endpoint_class {
214 struct kref kref;
215 struct class *class;
216} *ep_class;
217
218static int init_endpoint_class(void)
219{
220 int result = 0;
221
222 if (ep_class != NULL) {
223 kref_get(&ep_class->kref);
224 goto exit;
225 }
226
227 ep_class = kmalloc(sizeof(*ep_class), GFP_KERNEL);
228 if (!ep_class) {
229 result = -ENOMEM;
230 goto exit;
231 }
232
233 kref_init(&ep_class->kref);
234 ep_class->class = class_create(THIS_MODULE, "usb_endpoint");
235 if (IS_ERR(ep_class->class)) {
236 result = PTR_ERR(ep_class->class);
237 goto class_create_error;
238 }
239
240 result = usb_endpoint_major_init();
241 if (result)
242 goto endpoint_major_error;
243
244 goto exit;
245
246endpoint_major_error:
247 class_destroy(ep_class->class);
248class_create_error:
249 kfree(ep_class);
250 ep_class = NULL;
251exit:
252 return result;
253}
254
255static void release_endpoint_class(struct kref *kref)
256{
257 /* Ok, we cheat as we know we only have one ep_class */
258 class_destroy(ep_class->class);
259 kfree(ep_class);
260 ep_class = NULL;
261 usb_endpoint_major_cleanup();
262}
263
264static void destroy_endpoint_class(void)
265{
266 if (ep_class)
267 kref_put(&ep_class->kref, release_endpoint_class);
268}
269
270static void ep_device_release(struct device *dev) 162static void ep_device_release(struct device *dev)
271{ 163{
272 struct ep_device *ep_dev = to_ep_device(dev); 164 struct ep_device *ep_dev = to_ep_device(dev);
273 165
274 endpoint_free_minor(ep_dev);
275 kfree(ep_dev); 166 kfree(ep_dev);
276} 167}
277 168
@@ -279,62 +170,32 @@ int usb_create_ep_devs(struct device *parent,
279 struct usb_host_endpoint *endpoint, 170 struct usb_host_endpoint *endpoint,
280 struct usb_device *udev) 171 struct usb_device *udev)
281{ 172{
282 char name[8];
283 struct ep_device *ep_dev; 173 struct ep_device *ep_dev;
284 int retval; 174 int retval;
285 175
286 retval = init_endpoint_class();
287 if (retval)
288 goto exit;
289
290 ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL); 176 ep_dev = kzalloc(sizeof(*ep_dev), GFP_KERNEL);
291 if (!ep_dev) { 177 if (!ep_dev) {
292 retval = -ENOMEM; 178 retval = -ENOMEM;
293 goto error_alloc; 179 goto exit;
294 }
295
296 retval = endpoint_get_minor(ep_dev);
297 if (retval) {
298 dev_err(parent, "can not allocate minor number for %s\n",
299 dev_name(&ep_dev->dev));
300 goto error_register;
301 } 180 }
302 181
303 ep_dev->desc = &endpoint->desc; 182 ep_dev->desc = &endpoint->desc;
304 ep_dev->udev = udev; 183 ep_dev->udev = udev;
305 ep_dev->dev.groups = ep_dev_groups; 184 ep_dev->dev.groups = ep_dev_groups;
306 ep_dev->dev.devt = MKDEV(usb_endpoint_major, ep_dev->minor); 185 ep_dev->dev.type = &usb_ep_device_type;
307 ep_dev->dev.class = ep_class->class;
308 ep_dev->dev.parent = parent; 186 ep_dev->dev.parent = parent;
309 ep_dev->dev.release = ep_device_release; 187 ep_dev->dev.release = ep_device_release;
310 dev_set_name(&ep_dev->dev, "usbdev%d.%d_ep%02x", 188 dev_set_name(&ep_dev->dev, "ep_%02x", endpoint->desc.bEndpointAddress);
311 udev->bus->busnum, udev->devnum,
312 endpoint->desc.bEndpointAddress);
313 189
314 retval = device_register(&ep_dev->dev); 190 retval = device_register(&ep_dev->dev);
315 if (retval) 191 if (retval)
316 goto error_chrdev; 192 goto error_register;
317 193
318 /* create the symlink to the old-style "ep_XX" directory */
319 sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
320 retval = sysfs_create_link(&parent->kobj, &ep_dev->dev.kobj, name);
321 if (retval)
322 goto error_link;
323 endpoint->ep_dev = ep_dev; 194 endpoint->ep_dev = ep_dev;
324 return retval; 195 return retval;
325 196
326error_link:
327 device_unregister(&ep_dev->dev);
328 destroy_endpoint_class();
329 return retval;
330
331error_chrdev:
332 endpoint_free_minor(ep_dev);
333
334error_register: 197error_register:
335 kfree(ep_dev); 198 kfree(ep_dev);
336error_alloc:
337 destroy_endpoint_class();
338exit: 199exit:
339 return retval; 200 return retval;
340} 201}
@@ -344,12 +205,7 @@ void usb_remove_ep_devs(struct usb_host_endpoint *endpoint)
344 struct ep_device *ep_dev = endpoint->ep_dev; 205 struct ep_device *ep_dev = endpoint->ep_dev;
345 206
346 if (ep_dev) { 207 if (ep_dev) {
347 char name[8];
348
349 sprintf(name, "ep_%02x", endpoint->desc.bEndpointAddress);
350 sysfs_remove_link(&ep_dev->dev.parent->kobj, name);
351 device_unregister(&ep_dev->dev); 208 device_unregister(&ep_dev->dev);
352 endpoint->ep_dev = NULL; 209 endpoint->ep_dev = NULL;
353 destroy_endpoint_class();
354 } 210 }
355} 211}
diff --git a/drivers/usb/core/file.c b/drivers/usb/core/file.c
index 997e659ff693..5cef88929b3e 100644
--- a/drivers/usb/core/file.c
+++ b/drivers/usb/core/file.c
@@ -67,6 +67,16 @@ static struct usb_class {
67 struct class *class; 67 struct class *class;
68} *usb_class; 68} *usb_class;
69 69
70static char *usb_nodename(struct device *dev)
71{
72 struct usb_class_driver *drv;
73
74 drv = dev_get_drvdata(dev);
75 if (!drv || !drv->nodename)
76 return NULL;
77 return drv->nodename(dev);
78}
79
70static int init_usb_class(void) 80static int init_usb_class(void)
71{ 81{
72 int result = 0; 82 int result = 0;
@@ -90,6 +100,7 @@ static int init_usb_class(void)
90 kfree(usb_class); 100 kfree(usb_class);
91 usb_class = NULL; 101 usb_class = NULL;
92 } 102 }
103 usb_class->class->nodename = usb_nodename;
93 104
94exit: 105exit:
95 return result; 106 return result;
@@ -198,7 +209,7 @@ int usb_register_dev(struct usb_interface *intf,
198 else 209 else
199 temp = name; 210 temp = name;
200 intf->usb_dev = device_create(usb_class->class, &intf->dev, 211 intf->usb_dev = device_create(usb_class->class, &intf->dev,
201 MKDEV(USB_MAJOR, minor), NULL, 212 MKDEV(USB_MAJOR, minor), class_driver,
202 "%s", temp); 213 "%s", temp);
203 if (IS_ERR(intf->usb_dev)) { 214 if (IS_ERR(intf->usb_dev)) {
204 down_write(&minor_rwsem); 215 down_write(&minor_rwsem);
diff --git a/drivers/usb/core/hcd-pci.c b/drivers/usb/core/hcd-pci.c
index a4301dc02d27..91f2885b6ee1 100644
--- a/drivers/usb/core/hcd-pci.c
+++ b/drivers/usb/core/hcd-pci.c
@@ -185,194 +185,198 @@ void usb_hcd_pci_remove(struct pci_dev *dev)
185} 185}
186EXPORT_SYMBOL_GPL(usb_hcd_pci_remove); 186EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
187 187
188
189#ifdef CONFIG_PM
190
191/** 188/**
192 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD 189 * usb_hcd_pci_shutdown - shutdown host controller
193 * @dev: USB Host Controller being suspended 190 * @dev: USB Host Controller being shutdown
194 * @message: Power Management message describing this state transition
195 *
196 * Store this function in the HCD's struct pci_driver as .suspend.
197 */ 191 */
198int usb_hcd_pci_suspend(struct pci_dev *dev, pm_message_t message) 192void usb_hcd_pci_shutdown(struct pci_dev *dev)
193{
194 struct usb_hcd *hcd;
195
196 hcd = pci_get_drvdata(dev);
197 if (!hcd)
198 return;
199
200 if (hcd->driver->shutdown)
201 hcd->driver->shutdown(hcd);
202}
203EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
204
205#ifdef CONFIG_PM_SLEEP
206
207static int check_root_hub_suspended(struct device *dev)
208{
209 struct pci_dev *pci_dev = to_pci_dev(dev);
210 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
211
212 if (!(hcd->state == HC_STATE_SUSPENDED ||
213 hcd->state == HC_STATE_HALT)) {
214 dev_warn(dev, "Root hub is not suspended\n");
215 return -EBUSY;
216 }
217 return 0;
218}
219
220static int hcd_pci_suspend(struct device *dev)
199{ 221{
200 struct usb_hcd *hcd = pci_get_drvdata(dev); 222 struct pci_dev *pci_dev = to_pci_dev(dev);
201 int retval = 0; 223 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
202 int wake, w; 224 int retval;
203 int has_pci_pm;
204 225
205 /* Root hub suspend should have stopped all downstream traffic, 226 /* Root hub suspend should have stopped all downstream traffic,
206 * and all bus master traffic. And done so for both the interface 227 * and all bus master traffic. And done so for both the interface
207 * and the stub usb_device (which we check here). But maybe it 228 * and the stub usb_device (which we check here). But maybe it
208 * didn't; writing sysfs power/state files ignores such rules... 229 * didn't; writing sysfs power/state files ignores such rules...
209 *
210 * We must ignore the FREEZE vs SUSPEND distinction here, because
211 * otherwise the swsusp will save (and restore) garbage state.
212 */ 230 */
213 if (!(hcd->state == HC_STATE_SUSPENDED || 231 retval = check_root_hub_suspended(dev);
214 hcd->state == HC_STATE_HALT)) { 232 if (retval)
215 dev_warn(&dev->dev, "Root hub is not suspended\n"); 233 return retval;
216 retval = -EBUSY;
217 goto done;
218 }
219 234
220 /* We might already be suspended (runtime PM -- not yet written) */ 235 /* We might already be suspended (runtime PM -- not yet written) */
221 if (dev->current_state != PCI_D0) 236 if (pci_dev->current_state != PCI_D0)
222 goto done; 237 return retval;
223 238
224 if (hcd->driver->pci_suspend) { 239 if (hcd->driver->pci_suspend) {
225 retval = hcd->driver->pci_suspend(hcd, message); 240 retval = hcd->driver->pci_suspend(hcd);
226 suspend_report_result(hcd->driver->pci_suspend, retval); 241 suspend_report_result(hcd->driver->pci_suspend, retval);
227 if (retval) 242 if (retval)
228 goto done; 243 return retval;
229 } 244 }
230 245
231 synchronize_irq(dev->irq); 246 synchronize_irq(pci_dev->irq);
232 247
233 /* Downstream ports from this root hub should already be quiesced, so 248 /* Downstream ports from this root hub should already be quiesced, so
234 * there will be no DMA activity. Now we can shut down the upstream 249 * there will be no DMA activity. Now we can shut down the upstream
235 * link (except maybe for PME# resume signaling) and enter some PCI 250 * link (except maybe for PME# resume signaling). We'll enter a
236 * low power state, if the hardware allows. 251 * low power state during suspend_noirq, if the hardware allows.
237 */ 252 */
238 pci_disable_device(dev); 253 pci_disable_device(pci_dev);
254 return retval;
255}
256
257static int hcd_pci_suspend_noirq(struct device *dev)
258{
259 struct pci_dev *pci_dev = to_pci_dev(dev);
260 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
261 int retval;
262
263 retval = check_root_hub_suspended(dev);
264 if (retval)
265 return retval;
239 266
240 pci_save_state(dev); 267 pci_save_state(pci_dev);
241 268
242 /* Don't fail on error to enable wakeup. We rely on pci code 269 /* If the root hub is HALTed rather than SUSPENDed,
243 * to reject requests the hardware can't implement, rather 270 * disallow remote wakeup.
244 * than coding the same thing.
245 */ 271 */
246 wake = (hcd->state == HC_STATE_SUSPENDED && 272 if (hcd->state == HC_STATE_HALT)
247 device_may_wakeup(&dev->dev)); 273 device_set_wakeup_enable(dev, 0);
248 w = pci_wake_from_d3(dev, wake); 274 dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
249 if (w < 0)
250 wake = w;
251 dev_dbg(&dev->dev, "wakeup: %d\n", wake);
252
253 /* Don't change state if we don't need to */
254 if (message.event == PM_EVENT_FREEZE ||
255 message.event == PM_EVENT_PRETHAW) {
256 dev_dbg(&dev->dev, "--> no state change\n");
257 goto done;
258 }
259 275
260 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); 276 /* Possibly enable remote wakeup,
261 if (!has_pci_pm) { 277 * choose the appropriate low-power state, and go to that state.
262 dev_dbg(&dev->dev, "--> PCI D0 legacy\n"); 278 */
279 retval = pci_prepare_to_sleep(pci_dev);
280 if (retval == -EIO) { /* Low-power not supported */
281 dev_dbg(dev, "--> PCI D0 legacy\n");
282 retval = 0;
283 } else if (retval == 0) {
284 dev_dbg(dev, "--> PCI %s\n",
285 pci_power_name(pci_dev->current_state));
263 } else { 286 } else {
264 287 suspend_report_result(pci_prepare_to_sleep, retval);
265 /* NOTE: dev->current_state becomes nonzero only here, and 288 return retval;
266 * only for devices that support PCI PM. Also, exiting
267 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
268 * some device state (e.g. as part of clock reinit).
269 */
270 retval = pci_set_power_state(dev, PCI_D3hot);
271 suspend_report_result(pci_set_power_state, retval);
272 if (retval == 0) {
273 dev_dbg(&dev->dev, "--> PCI D3\n");
274 } else {
275 dev_dbg(&dev->dev, "PCI D3 suspend fail, %d\n",
276 retval);
277 pci_restore_state(dev);
278 }
279 } 289 }
280 290
281#ifdef CONFIG_PPC_PMAC 291#ifdef CONFIG_PPC_PMAC
282 if (retval == 0) { 292 /* Disable ASIC clocks for USB */
283 /* Disable ASIC clocks for USB */ 293 if (machine_is(powermac)) {
284 if (machine_is(powermac)) { 294 struct device_node *of_node;
285 struct device_node *of_node; 295
286 296 of_node = pci_device_to_OF_node(pci_dev);
287 of_node = pci_device_to_OF_node(dev); 297 if (of_node)
288 if (of_node) 298 pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
289 pmac_call_feature(PMAC_FTR_USB_ENABLE,
290 of_node, 0, 0);
291 }
292 } 299 }
293#endif 300#endif
294
295 done:
296 return retval; 301 return retval;
297} 302}
298EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend);
299 303
300/** 304static int hcd_pci_resume_noirq(struct device *dev)
301 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
302 * @dev: USB Host Controller being resumed
303 *
304 * Store this function in the HCD's struct pci_driver as .resume.
305 */
306int usb_hcd_pci_resume(struct pci_dev *dev)
307{ 305{
308 struct usb_hcd *hcd; 306 struct pci_dev *pci_dev = to_pci_dev(dev);
309 int retval;
310 307
311#ifdef CONFIG_PPC_PMAC 308#ifdef CONFIG_PPC_PMAC
312 /* Reenable ASIC clocks for USB */ 309 /* Reenable ASIC clocks for USB */
313 if (machine_is(powermac)) { 310 if (machine_is(powermac)) {
314 struct device_node *of_node; 311 struct device_node *of_node;
315 312
316 of_node = pci_device_to_OF_node(dev); 313 of_node = pci_device_to_OF_node(pci_dev);
317 if (of_node) 314 if (of_node)
318 pmac_call_feature(PMAC_FTR_USB_ENABLE, 315 pmac_call_feature(PMAC_FTR_USB_ENABLE,
319 of_node, 0, 1); 316 of_node, 0, 1);
320 } 317 }
321#endif 318#endif
322 319
323 pci_restore_state(dev); 320 /* Go back to D0 and disable remote wakeup */
321 pci_back_from_sleep(pci_dev);
322 return 0;
323}
324
325static int resume_common(struct device *dev, bool hibernated)
326{
327 struct pci_dev *pci_dev = to_pci_dev(dev);
328 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
329 int retval;
324 330
325 hcd = pci_get_drvdata(dev);
326 if (hcd->state != HC_STATE_SUSPENDED) { 331 if (hcd->state != HC_STATE_SUSPENDED) {
327 dev_dbg(hcd->self.controller, 332 dev_dbg(dev, "can't resume, not suspended!\n");
328 "can't resume, not suspended!\n");
329 return 0; 333 return 0;
330 } 334 }
331 335
332 pci_enable_wake(dev, PCI_D0, false); 336 retval = pci_enable_device(pci_dev);
333
334 retval = pci_enable_device(dev);
335 if (retval < 0) { 337 if (retval < 0) {
336 dev_err(&dev->dev, "can't re-enable after resume, %d!\n", 338 dev_err(dev, "can't re-enable after resume, %d!\n", retval);
337 retval);
338 return retval; 339 return retval;
339 } 340 }
340 341
341 pci_set_master(dev); 342 pci_set_master(pci_dev);
342
343 /* yes, ignore this result too... */
344 (void) pci_wake_from_d3(dev, 0);
345 343
346 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); 344 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
347 345
348 if (hcd->driver->pci_resume) { 346 if (hcd->driver->pci_resume) {
349 retval = hcd->driver->pci_resume(hcd); 347 retval = hcd->driver->pci_resume(hcd, hibernated);
350 if (retval) { 348 if (retval) {
351 dev_err(hcd->self.controller, 349 dev_err(dev, "PCI post-resume error %d!\n", retval);
352 "PCI post-resume error %d!\n", retval);
353 usb_hc_died(hcd); 350 usb_hc_died(hcd);
354 } 351 }
355 } 352 }
356 return retval; 353 return retval;
357} 354}
358EXPORT_SYMBOL_GPL(usb_hcd_pci_resume);
359 355
360#endif /* CONFIG_PM */ 356static int hcd_pci_resume(struct device *dev)
361
362/**
363 * usb_hcd_pci_shutdown - shutdown host controller
364 * @dev: USB Host Controller being shutdown
365 */
366void usb_hcd_pci_shutdown(struct pci_dev *dev)
367{ 357{
368 struct usb_hcd *hcd; 358 return resume_common(dev, false);
369 359}
370 hcd = pci_get_drvdata(dev);
371 if (!hcd)
372 return;
373 360
374 if (hcd->driver->shutdown) 361static int hcd_pci_restore(struct device *dev)
375 hcd->driver->shutdown(hcd); 362{
363 return resume_common(dev, true);
376} 364}
377EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
378 365
366struct dev_pm_ops usb_hcd_pci_pm_ops = {
367 .suspend = hcd_pci_suspend,
368 .suspend_noirq = hcd_pci_suspend_noirq,
369 .resume_noirq = hcd_pci_resume_noirq,
370 .resume = hcd_pci_resume,
371 .freeze = check_root_hub_suspended,
372 .freeze_noirq = check_root_hub_suspended,
373 .thaw_noirq = NULL,
374 .thaw = NULL,
375 .poweroff = hcd_pci_suspend,
376 .poweroff_noirq = hcd_pci_suspend_noirq,
377 .restore_noirq = hcd_pci_resume_noirq,
378 .restore = hcd_pci_restore,
379};
380EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
381
382#endif /* CONFIG_PM_SLEEP */
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 42b93da1085d..95ccfa0b9fc5 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -128,6 +128,27 @@ static inline int is_root_hub(struct usb_device *udev)
128#define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff) 128#define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
129#define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff) 129#define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
130 130
131/* usb 3.0 root hub device descriptor */
132static const u8 usb3_rh_dev_descriptor[18] = {
133 0x12, /* __u8 bLength; */
134 0x01, /* __u8 bDescriptorType; Device */
135 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
136
137 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
138 0x00, /* __u8 bDeviceSubClass; */
139 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
140 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
141
142 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
143 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
144 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
145
146 0x03, /* __u8 iManufacturer; */
147 0x02, /* __u8 iProduct; */
148 0x01, /* __u8 iSerialNumber; */
149 0x01 /* __u8 bNumConfigurations; */
150};
151
131/* usb 2.0 root hub device descriptor */ 152/* usb 2.0 root hub device descriptor */
132static const u8 usb2_rh_dev_descriptor [18] = { 153static const u8 usb2_rh_dev_descriptor [18] = {
133 0x12, /* __u8 bLength; */ 154 0x12, /* __u8 bLength; */
@@ -273,6 +294,47 @@ static const u8 hs_rh_config_descriptor [] = {
273 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ 294 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
274}; 295};
275 296
297static const u8 ss_rh_config_descriptor[] = {
298 /* one configuration */
299 0x09, /* __u8 bLength; */
300 0x02, /* __u8 bDescriptorType; Configuration */
301 0x19, 0x00, /* __le16 wTotalLength; FIXME */
302 0x01, /* __u8 bNumInterfaces; (1) */
303 0x01, /* __u8 bConfigurationValue; */
304 0x00, /* __u8 iConfiguration; */
305 0xc0, /* __u8 bmAttributes;
306 Bit 7: must be set,
307 6: Self-powered,
308 5: Remote wakeup,
309 4..0: resvd */
310 0x00, /* __u8 MaxPower; */
311
312 /* one interface */
313 0x09, /* __u8 if_bLength; */
314 0x04, /* __u8 if_bDescriptorType; Interface */
315 0x00, /* __u8 if_bInterfaceNumber; */
316 0x00, /* __u8 if_bAlternateSetting; */
317 0x01, /* __u8 if_bNumEndpoints; */
318 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
319 0x00, /* __u8 if_bInterfaceSubClass; */
320 0x00, /* __u8 if_bInterfaceProtocol; */
321 0x00, /* __u8 if_iInterface; */
322
323 /* one endpoint (status change endpoint) */
324 0x07, /* __u8 ep_bLength; */
325 0x05, /* __u8 ep_bDescriptorType; Endpoint */
326 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
327 0x03, /* __u8 ep_bmAttributes; Interrupt */
328 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
329 * see hub.c:hub_configure() for details. */
330 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
331 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
332 /*
333 * All 3.0 hubs should have an endpoint companion descriptor,
334 * but we're ignoring that for now. FIXME?
335 */
336};
337
276/*-------------------------------------------------------------------------*/ 338/*-------------------------------------------------------------------------*/
277 339
278/* 340/*
@@ -426,23 +488,39 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
426 case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 488 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
427 switch (wValue & 0xff00) { 489 switch (wValue & 0xff00) {
428 case USB_DT_DEVICE << 8: 490 case USB_DT_DEVICE << 8:
429 if (hcd->driver->flags & HCD_USB2) 491 switch (hcd->driver->flags & HCD_MASK) {
492 case HCD_USB3:
493 bufp = usb3_rh_dev_descriptor;
494 break;
495 case HCD_USB2:
430 bufp = usb2_rh_dev_descriptor; 496 bufp = usb2_rh_dev_descriptor;
431 else if (hcd->driver->flags & HCD_USB11) 497 break;
498 case HCD_USB11:
432 bufp = usb11_rh_dev_descriptor; 499 bufp = usb11_rh_dev_descriptor;
433 else 500 break;
501 default:
434 goto error; 502 goto error;
503 }
435 len = 18; 504 len = 18;
436 if (hcd->has_tt) 505 if (hcd->has_tt)
437 patch_protocol = 1; 506 patch_protocol = 1;
438 break; 507 break;
439 case USB_DT_CONFIG << 8: 508 case USB_DT_CONFIG << 8:
440 if (hcd->driver->flags & HCD_USB2) { 509 switch (hcd->driver->flags & HCD_MASK) {
510 case HCD_USB3:
511 bufp = ss_rh_config_descriptor;
512 len = sizeof ss_rh_config_descriptor;
513 break;
514 case HCD_USB2:
441 bufp = hs_rh_config_descriptor; 515 bufp = hs_rh_config_descriptor;
442 len = sizeof hs_rh_config_descriptor; 516 len = sizeof hs_rh_config_descriptor;
443 } else { 517 break;
518 case HCD_USB11:
444 bufp = fs_rh_config_descriptor; 519 bufp = fs_rh_config_descriptor;
445 len = sizeof fs_rh_config_descriptor; 520 len = sizeof fs_rh_config_descriptor;
521 break;
522 default:
523 goto error;
446 } 524 }
447 if (device_can_wakeup(&hcd->self.root_hub->dev)) 525 if (device_can_wakeup(&hcd->self.root_hub->dev))
448 patch_wakeup = 1; 526 patch_wakeup = 1;
@@ -570,7 +648,7 @@ void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
570 struct urb *urb; 648 struct urb *urb;
571 int length; 649 int length;
572 unsigned long flags; 650 unsigned long flags;
573 char buffer[4]; /* Any root hubs with > 31 ports? */ 651 char buffer[6]; /* Any root hubs with > 31 ports? */
574 652
575 if (unlikely(!hcd->rh_registered)) 653 if (unlikely(!hcd->rh_registered))
576 return; 654 return;
@@ -755,23 +833,6 @@ static struct attribute_group usb_bus_attr_group = {
755 833
756/*-------------------------------------------------------------------------*/ 834/*-------------------------------------------------------------------------*/
757 835
758static struct class *usb_host_class;
759
760int usb_host_init(void)
761{
762 int retval = 0;
763
764 usb_host_class = class_create(THIS_MODULE, "usb_host");
765 if (IS_ERR(usb_host_class))
766 retval = PTR_ERR(usb_host_class);
767 return retval;
768}
769
770void usb_host_cleanup(void)
771{
772 class_destroy(usb_host_class);
773}
774
775/** 836/**
776 * usb_bus_init - shared initialization code 837 * usb_bus_init - shared initialization code
777 * @bus: the bus structure being initialized 838 * @bus: the bus structure being initialized
@@ -818,12 +879,6 @@ static int usb_register_bus(struct usb_bus *bus)
818 set_bit (busnum, busmap.busmap); 879 set_bit (busnum, busmap.busmap);
819 bus->busnum = busnum; 880 bus->busnum = busnum;
820 881
821 bus->dev = device_create(usb_host_class, bus->controller, MKDEV(0, 0),
822 bus, "usb_host%d", busnum);
823 result = PTR_ERR(bus->dev);
824 if (IS_ERR(bus->dev))
825 goto error_create_class_dev;
826
827 /* Add it to the local list of buses */ 882 /* Add it to the local list of buses */
828 list_add (&bus->bus_list, &usb_bus_list); 883 list_add (&bus->bus_list, &usb_bus_list);
829 mutex_unlock(&usb_bus_list_lock); 884 mutex_unlock(&usb_bus_list_lock);
@@ -834,8 +889,6 @@ static int usb_register_bus(struct usb_bus *bus)
834 "number %d\n", bus->busnum); 889 "number %d\n", bus->busnum);
835 return 0; 890 return 0;
836 891
837error_create_class_dev:
838 clear_bit(busnum, busmap.busmap);
839error_find_busnum: 892error_find_busnum:
840 mutex_unlock(&usb_bus_list_lock); 893 mutex_unlock(&usb_bus_list_lock);
841 return result; 894 return result;
@@ -865,8 +918,6 @@ static void usb_deregister_bus (struct usb_bus *bus)
865 usb_notify_remove_bus(bus); 918 usb_notify_remove_bus(bus);
866 919
867 clear_bit (bus->busnum, busmap.busmap); 920 clear_bit (bus->busnum, busmap.busmap);
868
869 device_unregister(bus->dev);
870} 921}
871 922
872/** 923/**
@@ -1199,7 +1250,8 @@ static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1199 1250
1200 /* Map the URB's buffers for DMA access. 1251 /* Map the URB's buffers for DMA access.
1201 * Lower level HCD code should use *_dma exclusively, 1252 * Lower level HCD code should use *_dma exclusively,
1202 * unless it uses pio or talks to another transport. 1253 * unless it uses pio or talks to another transport,
1254 * or uses the provided scatter gather list for bulk.
1203 */ 1255 */
1204 if (is_root_hub(urb->dev)) 1256 if (is_root_hub(urb->dev))
1205 return 0; 1257 return 0;
@@ -1520,6 +1572,92 @@ rescan:
1520 } 1572 }
1521} 1573}
1522 1574
1575/* Check whether a new configuration or alt setting for an interface
1576 * will exceed the bandwidth for the bus (or the host controller resources).
1577 * Only pass in a non-NULL config or interface, not both!
1578 * Passing NULL for both new_config and new_intf means the device will be
1579 * de-configured by issuing a set configuration 0 command.
1580 */
1581int usb_hcd_check_bandwidth(struct usb_device *udev,
1582 struct usb_host_config *new_config,
1583 struct usb_interface *new_intf)
1584{
1585 int num_intfs, i, j;
1586 struct usb_interface_cache *intf_cache;
1587 struct usb_host_interface *alt = 0;
1588 int ret = 0;
1589 struct usb_hcd *hcd;
1590 struct usb_host_endpoint *ep;
1591
1592 hcd = bus_to_hcd(udev->bus);
1593 if (!hcd->driver->check_bandwidth)
1594 return 0;
1595
1596 /* Configuration is being removed - set configuration 0 */
1597 if (!new_config && !new_intf) {
1598 for (i = 1; i < 16; ++i) {
1599 ep = udev->ep_out[i];
1600 if (ep)
1601 hcd->driver->drop_endpoint(hcd, udev, ep);
1602 ep = udev->ep_in[i];
1603 if (ep)
1604 hcd->driver->drop_endpoint(hcd, udev, ep);
1605 }
1606 hcd->driver->check_bandwidth(hcd, udev);
1607 return 0;
1608 }
1609 /* Check if the HCD says there's enough bandwidth. Enable all endpoints
1610 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1611 * of the bus. There will always be bandwidth for endpoint 0, so it's
1612 * ok to exclude it.
1613 */
1614 if (new_config) {
1615 num_intfs = new_config->desc.bNumInterfaces;
1616 /* Remove endpoints (except endpoint 0, which is always on the
1617 * schedule) from the old config from the schedule
1618 */
1619 for (i = 1; i < 16; ++i) {
1620 ep = udev->ep_out[i];
1621 if (ep) {
1622 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1623 if (ret < 0)
1624 goto reset;
1625 }
1626 ep = udev->ep_in[i];
1627 if (ep) {
1628 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1629 if (ret < 0)
1630 goto reset;
1631 }
1632 }
1633 for (i = 0; i < num_intfs; ++i) {
1634
1635 /* Dig the endpoints for alt setting 0 out of the
1636 * interface cache for this interface
1637 */
1638 intf_cache = new_config->intf_cache[i];
1639 for (j = 0; j < intf_cache->num_altsetting; j++) {
1640 if (intf_cache->altsetting[j].desc.bAlternateSetting == 0)
1641 alt = &intf_cache->altsetting[j];
1642 }
1643 if (!alt) {
1644 printk(KERN_DEBUG "Did not find alt setting 0 for intf %d\n", i);
1645 continue;
1646 }
1647 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1648 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1649 if (ret < 0)
1650 goto reset;
1651 }
1652 }
1653 }
1654 ret = hcd->driver->check_bandwidth(hcd, udev);
1655reset:
1656 if (ret < 0)
1657 hcd->driver->reset_bandwidth(hcd, udev);
1658 return ret;
1659}
1660
1523/* Disables the endpoint: synchronizes with the hcd to make sure all 1661/* Disables the endpoint: synchronizes with the hcd to make sure all
1524 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must 1662 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1525 * have been called previously. Use for set_configuration, set_interface, 1663 * have been called previously. Use for set_configuration, set_interface,
@@ -1897,8 +2035,20 @@ int usb_add_hcd(struct usb_hcd *hcd,
1897 retval = -ENOMEM; 2035 retval = -ENOMEM;
1898 goto err_allocate_root_hub; 2036 goto err_allocate_root_hub;
1899 } 2037 }
1900 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH : 2038
1901 USB_SPEED_FULL; 2039 switch (hcd->driver->flags & HCD_MASK) {
2040 case HCD_USB11:
2041 rhdev->speed = USB_SPEED_FULL;
2042 break;
2043 case HCD_USB2:
2044 rhdev->speed = USB_SPEED_HIGH;
2045 break;
2046 case HCD_USB3:
2047 rhdev->speed = USB_SPEED_SUPER;
2048 break;
2049 default:
2050 goto err_allocate_root_hub;
2051 }
1902 hcd->self.root_hub = rhdev; 2052 hcd->self.root_hub = rhdev;
1903 2053
1904 /* wakeup flag init defaults to "everything works" for root hubs, 2054 /* wakeup flag init defaults to "everything works" for root hubs,
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index e7d4479de41c..ec5c67ea07b7 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -173,6 +173,8 @@ struct hc_driver {
173#define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */ 173#define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */
174#define HCD_USB11 0x0010 /* USB 1.1 */ 174#define HCD_USB11 0x0010 /* USB 1.1 */
175#define HCD_USB2 0x0020 /* USB 2.0 */ 175#define HCD_USB2 0x0020 /* USB 2.0 */
176#define HCD_USB3 0x0040 /* USB 3.0 */
177#define HCD_MASK 0x0070
176 178
177 /* called to init HCD and root hub */ 179 /* called to init HCD and root hub */
178 int (*reset) (struct usb_hcd *hcd); 180 int (*reset) (struct usb_hcd *hcd);
@@ -182,10 +184,10 @@ struct hc_driver {
182 * a whole, not just the root hub; they're for PCI bus glue. 184 * a whole, not just the root hub; they're for PCI bus glue.
183 */ 185 */
184 /* called after suspending the hub, before entering D3 etc */ 186 /* called after suspending the hub, before entering D3 etc */
185 int (*pci_suspend) (struct usb_hcd *hcd, pm_message_t message); 187 int (*pci_suspend)(struct usb_hcd *hcd);
186 188
187 /* called after entering D0 (etc), before resuming the hub */ 189 /* called after entering D0 (etc), before resuming the hub */
188 int (*pci_resume) (struct usb_hcd *hcd); 190 int (*pci_resume)(struct usb_hcd *hcd, bool hibernated);
189 191
190 /* cleanly make HCD stop writing memory and doing I/O */ 192 /* cleanly make HCD stop writing memory and doing I/O */
191 void (*stop) (struct usb_hcd *hcd); 193 void (*stop) (struct usb_hcd *hcd);
@@ -224,6 +226,47 @@ struct hc_driver {
224 void (*relinquish_port)(struct usb_hcd *, int); 226 void (*relinquish_port)(struct usb_hcd *, int);
225 /* has a port been handed over to a companion? */ 227 /* has a port been handed over to a companion? */
226 int (*port_handed_over)(struct usb_hcd *, int); 228 int (*port_handed_over)(struct usb_hcd *, int);
229
230 /* CLEAR_TT_BUFFER completion callback */
231 void (*clear_tt_buffer_complete)(struct usb_hcd *,
232 struct usb_host_endpoint *);
233
234 /* xHCI specific functions */
235 /* Called by usb_alloc_dev to alloc HC device structures */
236 int (*alloc_dev)(struct usb_hcd *, struct usb_device *);
237 /* Called by usb_release_dev to free HC device structures */
238 void (*free_dev)(struct usb_hcd *, struct usb_device *);
239
240 /* Bandwidth computation functions */
241 /* Note that add_endpoint() can only be called once per endpoint before
242 * check_bandwidth() or reset_bandwidth() must be called.
243 * drop_endpoint() can only be called once per endpoint also.
244 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
245 * add the endpoint to the schedule with possibly new parameters denoted by a
246 * different endpoint descriptor in usb_host_endpoint.
247 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
248 * not allowed.
249 */
250 /* Allocate endpoint resources and add them to a new schedule */
251 int (*add_endpoint)(struct usb_hcd *, struct usb_device *, struct usb_host_endpoint *);
252 /* Drop an endpoint from a new schedule */
253 int (*drop_endpoint)(struct usb_hcd *, struct usb_device *, struct usb_host_endpoint *);
254 /* Check that a new hardware configuration, set using
255 * endpoint_enable and endpoint_disable, does not exceed bus
256 * bandwidth. This must be called before any set configuration
257 * or set interface requests are sent to the device.
258 */
259 int (*check_bandwidth)(struct usb_hcd *, struct usb_device *);
260 /* Reset the device schedule to the last known good schedule,
261 * which was set from a previous successful call to
262 * check_bandwidth(). This reverts any add_endpoint() and
263 * drop_endpoint() calls since that last successful call.
264 * Used for when a check_bandwidth() call fails due to resource
265 * or bandwidth constraints.
266 */
267 void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *);
268 /* Returns the hardware-chosen device address */
269 int (*address_device)(struct usb_hcd *, struct usb_device *udev);
227}; 270};
228 271
229extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb); 272extern int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb);
@@ -242,6 +285,9 @@ extern void usb_hcd_disable_endpoint(struct usb_device *udev,
242extern void usb_hcd_reset_endpoint(struct usb_device *udev, 285extern void usb_hcd_reset_endpoint(struct usb_device *udev,
243 struct usb_host_endpoint *ep); 286 struct usb_host_endpoint *ep);
244extern void usb_hcd_synchronize_unlinks(struct usb_device *udev); 287extern void usb_hcd_synchronize_unlinks(struct usb_device *udev);
288extern int usb_hcd_check_bandwidth(struct usb_device *udev,
289 struct usb_host_config *new_config,
290 struct usb_interface *new_intf);
245extern int usb_hcd_get_frame_number(struct usb_device *udev); 291extern int usb_hcd_get_frame_number(struct usb_device *udev);
246 292
247extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver, 293extern struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
@@ -261,14 +307,11 @@ struct pci_device_id;
261extern int usb_hcd_pci_probe(struct pci_dev *dev, 307extern int usb_hcd_pci_probe(struct pci_dev *dev,
262 const struct pci_device_id *id); 308 const struct pci_device_id *id);
263extern void usb_hcd_pci_remove(struct pci_dev *dev); 309extern void usb_hcd_pci_remove(struct pci_dev *dev);
264
265#ifdef CONFIG_PM
266extern int usb_hcd_pci_suspend(struct pci_dev *dev, pm_message_t msg);
267extern int usb_hcd_pci_resume(struct pci_dev *dev);
268#endif /* CONFIG_PM */
269
270extern void usb_hcd_pci_shutdown(struct pci_dev *dev); 310extern void usb_hcd_pci_shutdown(struct pci_dev *dev);
271 311
312#ifdef CONFIG_PM_SLEEP
313extern struct dev_pm_ops usb_hcd_pci_pm_ops;
314#endif
272#endif /* CONFIG_PCI */ 315#endif /* CONFIG_PCI */
273 316
274/* pci-ish (pdev null is ok) buffer alloc/mapping support */ 317/* pci-ish (pdev null is ok) buffer alloc/mapping support */
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index be86ae3f4088..71f86c60d83c 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -155,6 +155,8 @@ static inline char *portspeed(int portstatus)
155 return "480 Mb/s"; 155 return "480 Mb/s";
156 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED)) 156 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
157 return "1.5 Mb/s"; 157 return "1.5 Mb/s";
158 else if (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED))
159 return "5.0 Gb/s";
158 else 160 else
159 return "12 Mb/s"; 161 return "12 Mb/s";
160} 162}
@@ -448,42 +450,47 @@ hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
448 * talking to TTs must queue control transfers (not just bulk and iso), so 450 * talking to TTs must queue control transfers (not just bulk and iso), so
449 * both can talk to the same hub concurrently. 451 * both can talk to the same hub concurrently.
450 */ 452 */
451static void hub_tt_kevent (struct work_struct *work) 453static void hub_tt_work(struct work_struct *work)
452{ 454{
453 struct usb_hub *hub = 455 struct usb_hub *hub =
454 container_of(work, struct usb_hub, tt.kevent); 456 container_of(work, struct usb_hub, tt.clear_work);
455 unsigned long flags; 457 unsigned long flags;
456 int limit = 100; 458 int limit = 100;
457 459
458 spin_lock_irqsave (&hub->tt.lock, flags); 460 spin_lock_irqsave (&hub->tt.lock, flags);
459 while (--limit && !list_empty (&hub->tt.clear_list)) { 461 while (--limit && !list_empty (&hub->tt.clear_list)) {
460 struct list_head *temp; 462 struct list_head *next;
461 struct usb_tt_clear *clear; 463 struct usb_tt_clear *clear;
462 struct usb_device *hdev = hub->hdev; 464 struct usb_device *hdev = hub->hdev;
465 const struct hc_driver *drv;
463 int status; 466 int status;
464 467
465 temp = hub->tt.clear_list.next; 468 next = hub->tt.clear_list.next;
466 clear = list_entry (temp, struct usb_tt_clear, clear_list); 469 clear = list_entry (next, struct usb_tt_clear, clear_list);
467 list_del (&clear->clear_list); 470 list_del (&clear->clear_list);
468 471
469 /* drop lock so HCD can concurrently report other TT errors */ 472 /* drop lock so HCD can concurrently report other TT errors */
470 spin_unlock_irqrestore (&hub->tt.lock, flags); 473 spin_unlock_irqrestore (&hub->tt.lock, flags);
471 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 474 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
472 spin_lock_irqsave (&hub->tt.lock, flags);
473
474 if (status) 475 if (status)
475 dev_err (&hdev->dev, 476 dev_err (&hdev->dev,
476 "clear tt %d (%04x) error %d\n", 477 "clear tt %d (%04x) error %d\n",
477 clear->tt, clear->devinfo, status); 478 clear->tt, clear->devinfo, status);
479
480 /* Tell the HCD, even if the operation failed */
481 drv = clear->hcd->driver;
482 if (drv->clear_tt_buffer_complete)
483 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
484
478 kfree(clear); 485 kfree(clear);
486 spin_lock_irqsave(&hub->tt.lock, flags);
479 } 487 }
480 spin_unlock_irqrestore (&hub->tt.lock, flags); 488 spin_unlock_irqrestore (&hub->tt.lock, flags);
481} 489}
482 490
483/** 491/**
484 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub 492 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
485 * @udev: the device whose split transaction failed 493 * @urb: an URB associated with the failed or incomplete split transaction
486 * @pipe: identifies the endpoint of the failed transaction
487 * 494 *
488 * High speed HCDs use this to tell the hub driver that some split control or 495 * High speed HCDs use this to tell the hub driver that some split control or
489 * bulk transaction failed in a way that requires clearing internal state of 496 * bulk transaction failed in a way that requires clearing internal state of
@@ -493,8 +500,10 @@ static void hub_tt_kevent (struct work_struct *work)
493 * It may not be possible for that hub to handle additional full (or low) 500 * It may not be possible for that hub to handle additional full (or low)
494 * speed transactions until that state is fully cleared out. 501 * speed transactions until that state is fully cleared out.
495 */ 502 */
496void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe) 503int usb_hub_clear_tt_buffer(struct urb *urb)
497{ 504{
505 struct usb_device *udev = urb->dev;
506 int pipe = urb->pipe;
498 struct usb_tt *tt = udev->tt; 507 struct usb_tt *tt = udev->tt;
499 unsigned long flags; 508 unsigned long flags;
500 struct usb_tt_clear *clear; 509 struct usb_tt_clear *clear;
@@ -506,7 +515,7 @@ void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
506 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { 515 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
507 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 516 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
508 /* FIXME recover somehow ... RESET_TT? */ 517 /* FIXME recover somehow ... RESET_TT? */
509 return; 518 return -ENOMEM;
510 } 519 }
511 520
512 /* info that CLEAR_TT_BUFFER needs */ 521 /* info that CLEAR_TT_BUFFER needs */
@@ -518,14 +527,19 @@ void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
518 : (USB_ENDPOINT_XFER_BULK << 11); 527 : (USB_ENDPOINT_XFER_BULK << 11);
519 if (usb_pipein (pipe)) 528 if (usb_pipein (pipe))
520 clear->devinfo |= 1 << 15; 529 clear->devinfo |= 1 << 15;
521 530
531 /* info for completion callback */
532 clear->hcd = bus_to_hcd(udev->bus);
533 clear->ep = urb->ep;
534
522 /* tell keventd to clear state for this TT */ 535 /* tell keventd to clear state for this TT */
523 spin_lock_irqsave (&tt->lock, flags); 536 spin_lock_irqsave (&tt->lock, flags);
524 list_add_tail (&clear->clear_list, &tt->clear_list); 537 list_add_tail (&clear->clear_list, &tt->clear_list);
525 schedule_work (&tt->kevent); 538 schedule_work(&tt->clear_work);
526 spin_unlock_irqrestore (&tt->lock, flags); 539 spin_unlock_irqrestore (&tt->lock, flags);
540 return 0;
527} 541}
528EXPORT_SYMBOL_GPL(usb_hub_tt_clear_buffer); 542EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
529 543
530/* If do_delay is false, return the number of milliseconds the caller 544/* If do_delay is false, return the number of milliseconds the caller
531 * needs to delay. 545 * needs to delay.
@@ -816,7 +830,7 @@ static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
816 if (hub->has_indicators) 830 if (hub->has_indicators)
817 cancel_delayed_work_sync(&hub->leds); 831 cancel_delayed_work_sync(&hub->leds);
818 if (hub->tt.hub) 832 if (hub->tt.hub)
819 cancel_work_sync(&hub->tt.kevent); 833 cancel_work_sync(&hub->tt.clear_work);
820} 834}
821 835
822/* caller has locked the hub device */ 836/* caller has locked the hub device */
@@ -933,7 +947,7 @@ static int hub_configure(struct usb_hub *hub,
933 947
934 spin_lock_init (&hub->tt.lock); 948 spin_lock_init (&hub->tt.lock);
935 INIT_LIST_HEAD (&hub->tt.clear_list); 949 INIT_LIST_HEAD (&hub->tt.clear_list);
936 INIT_WORK (&hub->tt.kevent, hub_tt_kevent); 950 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
937 switch (hdev->descriptor.bDeviceProtocol) { 951 switch (hdev->descriptor.bDeviceProtocol) {
938 case 0: 952 case 0:
939 break; 953 break;
@@ -951,6 +965,9 @@ static int hub_configure(struct usb_hub *hub,
951 ret); 965 ret);
952 hub->tt.hub = hdev; 966 hub->tt.hub = hdev;
953 break; 967 break;
968 case 3:
969 /* USB 3.0 hubs don't have a TT */
970 break;
954 default: 971 default:
955 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 972 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
956 hdev->descriptor.bDeviceProtocol); 973 hdev->descriptor.bDeviceProtocol);
@@ -1323,6 +1340,11 @@ EXPORT_SYMBOL_GPL(usb_set_device_state);
1323 * 0 is reserved by USB for default address; (b) Linux's USB stack 1340 * 0 is reserved by USB for default address; (b) Linux's USB stack
1324 * uses always #1 for the root hub of the controller. So USB stack's 1341 * uses always #1 for the root hub of the controller. So USB stack's
1325 * port #1, which is wusb virtual-port #0 has address #2. 1342 * port #1, which is wusb virtual-port #0 has address #2.
1343 *
1344 * Devices connected under xHCI are not as simple. The host controller
1345 * supports virtualization, so the hardware assigns device addresses and
1346 * the HCD must setup data structures before issuing a set address
1347 * command to the hardware.
1326 */ 1348 */
1327static void choose_address(struct usb_device *udev) 1349static void choose_address(struct usb_device *udev)
1328{ 1350{
@@ -1642,6 +1664,9 @@ int usb_new_device(struct usb_device *udev)
1642 err = usb_configure_device(udev); /* detect & probe dev/intfs */ 1664 err = usb_configure_device(udev); /* detect & probe dev/intfs */
1643 if (err < 0) 1665 if (err < 0)
1644 goto fail; 1666 goto fail;
1667 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1668 udev->devnum, udev->bus->busnum,
1669 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
1645 /* export the usbdev device-node for libusb */ 1670 /* export the usbdev device-node for libusb */
1646 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 1671 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
1647 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 1672 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
@@ -2395,19 +2420,29 @@ EXPORT_SYMBOL_GPL(usb_ep0_reinit);
2395static int hub_set_address(struct usb_device *udev, int devnum) 2420static int hub_set_address(struct usb_device *udev, int devnum)
2396{ 2421{
2397 int retval; 2422 int retval;
2423 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2398 2424
2399 if (devnum <= 1) 2425 /*
2426 * The host controller will choose the device address,
2427 * instead of the core having chosen it earlier
2428 */
2429 if (!hcd->driver->address_device && devnum <= 1)
2400 return -EINVAL; 2430 return -EINVAL;
2401 if (udev->state == USB_STATE_ADDRESS) 2431 if (udev->state == USB_STATE_ADDRESS)
2402 return 0; 2432 return 0;
2403 if (udev->state != USB_STATE_DEFAULT) 2433 if (udev->state != USB_STATE_DEFAULT)
2404 return -EINVAL; 2434 return -EINVAL;
2405 retval = usb_control_msg(udev, usb_sndaddr0pipe(), 2435 if (hcd->driver->address_device) {
2406 USB_REQ_SET_ADDRESS, 0, devnum, 0, 2436 retval = hcd->driver->address_device(hcd, udev);
2407 NULL, 0, USB_CTRL_SET_TIMEOUT); 2437 } else {
2438 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
2439 USB_REQ_SET_ADDRESS, 0, devnum, 0,
2440 NULL, 0, USB_CTRL_SET_TIMEOUT);
2441 if (retval == 0)
2442 update_address(udev, devnum);
2443 }
2408 if (retval == 0) { 2444 if (retval == 0) {
2409 /* Device now using proper address. */ 2445 /* Device now using proper address. */
2410 update_address(udev, devnum);
2411 usb_set_device_state(udev, USB_STATE_ADDRESS); 2446 usb_set_device_state(udev, USB_STATE_ADDRESS);
2412 usb_ep0_reinit(udev); 2447 usb_ep0_reinit(udev);
2413 } 2448 }
@@ -2430,6 +2465,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2430 static DEFINE_MUTEX(usb_address0_mutex); 2465 static DEFINE_MUTEX(usb_address0_mutex);
2431 2466
2432 struct usb_device *hdev = hub->hdev; 2467 struct usb_device *hdev = hub->hdev;
2468 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
2433 int i, j, retval; 2469 int i, j, retval;
2434 unsigned delay = HUB_SHORT_RESET_TIME; 2470 unsigned delay = HUB_SHORT_RESET_TIME;
2435 enum usb_device_speed oldspeed = udev->speed; 2471 enum usb_device_speed oldspeed = udev->speed;
@@ -2452,11 +2488,24 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2452 2488
2453 mutex_lock(&usb_address0_mutex); 2489 mutex_lock(&usb_address0_mutex);
2454 2490
2455 /* Reset the device; full speed may morph to high speed */ 2491 if ((hcd->driver->flags & HCD_USB3) && udev->config) {
2456 retval = hub_port_reset(hub, port1, udev, delay); 2492 /* FIXME this will need special handling by the xHCI driver. */
2457 if (retval < 0) /* error or disconnect */ 2493 dev_dbg(&udev->dev,
2494 "xHCI reset of configured device "
2495 "not supported yet.\n");
2496 retval = -EINVAL;
2458 goto fail; 2497 goto fail;
2459 /* success, speed is known */ 2498 } else if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2499 /* Don't reset USB 3.0 devices during an initial setup */
2500 usb_set_device_state(udev, USB_STATE_DEFAULT);
2501 } else {
2502 /* Reset the device; full speed may morph to high speed */
2503 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
2504 retval = hub_port_reset(hub, port1, udev, delay);
2505 if (retval < 0) /* error or disconnect */
2506 goto fail;
2507 /* success, speed is known */
2508 }
2460 retval = -ENODEV; 2509 retval = -ENODEV;
2461 2510
2462 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 2511 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
@@ -2471,6 +2520,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2471 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 2520 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
2472 */ 2521 */
2473 switch (udev->speed) { 2522 switch (udev->speed) {
2523 case USB_SPEED_SUPER:
2474 case USB_SPEED_VARIABLE: /* fixed at 512 */ 2524 case USB_SPEED_VARIABLE: /* fixed at 512 */
2475 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 2525 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
2476 break; 2526 break;
@@ -2496,16 +2546,20 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2496 case USB_SPEED_LOW: speed = "low"; break; 2546 case USB_SPEED_LOW: speed = "low"; break;
2497 case USB_SPEED_FULL: speed = "full"; break; 2547 case USB_SPEED_FULL: speed = "full"; break;
2498 case USB_SPEED_HIGH: speed = "high"; break; 2548 case USB_SPEED_HIGH: speed = "high"; break;
2549 case USB_SPEED_SUPER:
2550 speed = "super";
2551 break;
2499 case USB_SPEED_VARIABLE: 2552 case USB_SPEED_VARIABLE:
2500 speed = "variable"; 2553 speed = "variable";
2501 type = "Wireless "; 2554 type = "Wireless ";
2502 break; 2555 break;
2503 default: speed = "?"; break; 2556 default: speed = "?"; break;
2504 } 2557 }
2505 dev_info (&udev->dev, 2558 if (udev->speed != USB_SPEED_SUPER)
2506 "%s %s speed %sUSB device using %s and address %d\n", 2559 dev_info(&udev->dev,
2507 (udev->config) ? "reset" : "new", speed, type, 2560 "%s %s speed %sUSB device using %s and address %d\n",
2508 udev->bus->controller->driver->name, devnum); 2561 (udev->config) ? "reset" : "new", speed, type,
2562 udev->bus->controller->driver->name, devnum);
2509 2563
2510 /* Set up TT records, if needed */ 2564 /* Set up TT records, if needed */
2511 if (hdev->tt) { 2565 if (hdev->tt) {
@@ -2530,7 +2584,11 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2530 * value. 2584 * value.
2531 */ 2585 */
2532 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 2586 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2533 if (USE_NEW_SCHEME(retry_counter)) { 2587 /*
2588 * An xHCI controller cannot send any packets to a device until
2589 * a set address command successfully completes.
2590 */
2591 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
2534 struct usb_device_descriptor *buf; 2592 struct usb_device_descriptor *buf;
2535 int r = 0; 2593 int r = 0;
2536 2594
@@ -2596,7 +2654,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2596 * unauthorized address in the Connect Ack sequence; 2654 * unauthorized address in the Connect Ack sequence;
2597 * authorization will assign the final address. 2655 * authorization will assign the final address.
2598 */ 2656 */
2599 if (udev->wusb == 0) { 2657 if (udev->wusb == 0) {
2600 for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 2658 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
2601 retval = hub_set_address(udev, devnum); 2659 retval = hub_set_address(udev, devnum);
2602 if (retval >= 0) 2660 if (retval >= 0)
@@ -2609,13 +2667,20 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2609 devnum, retval); 2667 devnum, retval);
2610 goto fail; 2668 goto fail;
2611 } 2669 }
2670 if (udev->speed == USB_SPEED_SUPER) {
2671 devnum = udev->devnum;
2672 dev_info(&udev->dev,
2673 "%s SuperSpeed USB device using %s and address %d\n",
2674 (udev->config) ? "reset" : "new",
2675 udev->bus->controller->driver->name, devnum);
2676 }
2612 2677
2613 /* cope with hardware quirkiness: 2678 /* cope with hardware quirkiness:
2614 * - let SET_ADDRESS settle, some device hardware wants it 2679 * - let SET_ADDRESS settle, some device hardware wants it
2615 * - read ep0 maxpacket even for high and low speed, 2680 * - read ep0 maxpacket even for high and low speed,
2616 */ 2681 */
2617 msleep(10); 2682 msleep(10);
2618 if (USE_NEW_SCHEME(retry_counter)) 2683 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
2619 break; 2684 break;
2620 } 2685 }
2621 2686
@@ -2634,8 +2699,11 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2634 if (retval) 2699 if (retval)
2635 goto fail; 2700 goto fail;
2636 2701
2637 i = udev->descriptor.bMaxPacketSize0 == 0xff? /* wusb device? */ 2702 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
2638 512 : udev->descriptor.bMaxPacketSize0; 2703 udev->speed == USB_SPEED_SUPER)
2704 i = 512;
2705 else
2706 i = udev->descriptor.bMaxPacketSize0;
2639 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) { 2707 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
2640 if (udev->speed != USB_SPEED_FULL || 2708 if (udev->speed != USB_SPEED_FULL ||
2641 !(i == 8 || i == 16 || i == 32 || i == 64)) { 2709 !(i == 8 || i == 16 || i == 32 || i == 64)) {
@@ -2847,19 +2915,41 @@ static void hub_port_connect_change(struct usb_hub *hub, int port1,
2847 } 2915 }
2848 2916
2849 usb_set_device_state(udev, USB_STATE_POWERED); 2917 usb_set_device_state(udev, USB_STATE_POWERED);
2850 udev->speed = USB_SPEED_UNKNOWN;
2851 udev->bus_mA = hub->mA_per_port; 2918 udev->bus_mA = hub->mA_per_port;
2852 udev->level = hdev->level + 1; 2919 udev->level = hdev->level + 1;
2853 udev->wusb = hub_is_wusb(hub); 2920 udev->wusb = hub_is_wusb(hub);
2854 2921
2855 /* set the address */ 2922 /*
2856 choose_address(udev); 2923 * USB 3.0 devices are reset automatically before the connect
2857 if (udev->devnum <= 0) { 2924 * port status change appears, and the root hub port status
2858 status = -ENOTCONN; /* Don't retry */ 2925 * shows the correct speed. We also get port change
2859 goto loop; 2926 * notifications for USB 3.0 devices from the USB 3.0 portion of
2927 * an external USB 3.0 hub, but this isn't handled correctly yet
2928 * FIXME.
2929 */
2930
2931 if (!(hcd->driver->flags & HCD_USB3))
2932 udev->speed = USB_SPEED_UNKNOWN;
2933 else if ((hdev->parent == NULL) &&
2934 (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED)))
2935 udev->speed = USB_SPEED_SUPER;
2936 else
2937 udev->speed = USB_SPEED_UNKNOWN;
2938
2939 /*
2940 * xHCI needs to issue an address device command later
2941 * in the hub_port_init sequence for SS/HS/FS/LS devices.
2942 */
2943 if (!(hcd->driver->flags & HCD_USB3)) {
2944 /* set the address */
2945 choose_address(udev);
2946 if (udev->devnum <= 0) {
2947 status = -ENOTCONN; /* Don't retry */
2948 goto loop;
2949 }
2860 } 2950 }
2861 2951
2862 /* reset and get descriptor */ 2952 /* reset (non-USB 3.0 devices) and get descriptor */
2863 status = hub_port_init(hub, udev, port1, i); 2953 status = hub_port_init(hub, udev, port1, i);
2864 if (status < 0) 2954 if (status < 0)
2865 goto loop; 2955 goto loop;
diff --git a/drivers/usb/core/hub.h b/drivers/usb/core/hub.h
index 2a116ce53c9b..de8081f065ed 100644
--- a/drivers/usb/core/hub.h
+++ b/drivers/usb/core/hub.h
@@ -47,7 +47,10 @@
47#define USB_PORT_FEAT_L1 5 /* L1 suspend */ 47#define USB_PORT_FEAT_L1 5 /* L1 suspend */
48#define USB_PORT_FEAT_POWER 8 48#define USB_PORT_FEAT_POWER 8
49#define USB_PORT_FEAT_LOWSPEED 9 49#define USB_PORT_FEAT_LOWSPEED 9
50/* This value was never in Table 11-17 */
50#define USB_PORT_FEAT_HIGHSPEED 10 51#define USB_PORT_FEAT_HIGHSPEED 10
52/* This value is also fake */
53#define USB_PORT_FEAT_SUPERSPEED 11
51#define USB_PORT_FEAT_C_CONNECTION 16 54#define USB_PORT_FEAT_C_CONNECTION 16
52#define USB_PORT_FEAT_C_ENABLE 17 55#define USB_PORT_FEAT_C_ENABLE 17
53#define USB_PORT_FEAT_C_SUSPEND 18 56#define USB_PORT_FEAT_C_SUSPEND 18
@@ -185,16 +188,18 @@ struct usb_tt {
185 /* for control/bulk error recovery (CLEAR_TT_BUFFER) */ 188 /* for control/bulk error recovery (CLEAR_TT_BUFFER) */
186 spinlock_t lock; 189 spinlock_t lock;
187 struct list_head clear_list; /* of usb_tt_clear */ 190 struct list_head clear_list; /* of usb_tt_clear */
188 struct work_struct kevent; 191 struct work_struct clear_work;
189}; 192};
190 193
191struct usb_tt_clear { 194struct usb_tt_clear {
192 struct list_head clear_list; 195 struct list_head clear_list;
193 unsigned tt; 196 unsigned tt;
194 u16 devinfo; 197 u16 devinfo;
198 struct usb_hcd *hcd;
199 struct usb_host_endpoint *ep;
195}; 200};
196 201
197extern void usb_hub_tt_clear_buffer(struct usb_device *dev, int pipe); 202extern int usb_hub_clear_tt_buffer(struct urb *urb);
198extern void usb_ep0_reinit(struct usb_device *); 203extern void usb_ep0_reinit(struct usb_device *);
199 204
200#endif /* __LINUX_HUB_H */ 205#endif /* __LINUX_HUB_H */
diff --git a/drivers/usb/core/inode.c b/drivers/usb/core/inode.c
index dff5760a37f6..ffe75e83787c 100644
--- a/drivers/usb/core/inode.c
+++ b/drivers/usb/core/inode.c
@@ -39,6 +39,7 @@
39#include <linux/parser.h> 39#include <linux/parser.h>
40#include <linux/notifier.h> 40#include <linux/notifier.h>
41#include <linux/seq_file.h> 41#include <linux/seq_file.h>
42#include <linux/smp_lock.h>
42#include <asm/byteorder.h> 43#include <asm/byteorder.h>
43#include "usb.h" 44#include "usb.h"
44#include "hcd.h" 45#include "hcd.h"
@@ -265,9 +266,13 @@ static int remount(struct super_block *sb, int *flags, char *data)
265 return -EINVAL; 266 return -EINVAL;
266 } 267 }
267 268
269 lock_kernel();
270
268 if (usbfs_mount && usbfs_mount->mnt_sb) 271 if (usbfs_mount && usbfs_mount->mnt_sb)
269 update_sb(usbfs_mount->mnt_sb); 272 update_sb(usbfs_mount->mnt_sb);
270 273
274 unlock_kernel();
275
271 return 0; 276 return 0;
272} 277}
273 278
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index b62628377654..9720e699f472 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -10,6 +10,7 @@
10#include <linux/mm.h> 10#include <linux/mm.h>
11#include <linux/timer.h> 11#include <linux/timer.h>
12#include <linux/ctype.h> 12#include <linux/ctype.h>
13#include <linux/nls.h>
13#include <linux/device.h> 14#include <linux/device.h>
14#include <linux/scatterlist.h> 15#include <linux/scatterlist.h>
15#include <linux/usb/quirks.h> 16#include <linux/usb/quirks.h>
@@ -364,6 +365,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
364 int i; 365 int i;
365 int urb_flags; 366 int urb_flags;
366 int dma; 367 int dma;
368 int use_sg;
367 369
368 if (!io || !dev || !sg 370 if (!io || !dev || !sg
369 || usb_pipecontrol(pipe) 371 || usb_pipecontrol(pipe)
@@ -391,7 +393,19 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
391 if (io->entries <= 0) 393 if (io->entries <= 0)
392 return io->entries; 394 return io->entries;
393 395
394 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags); 396 /* If we're running on an xHCI host controller, queue the whole scatter
397 * gather list with one call to urb_enqueue(). This is only for bulk,
398 * as that endpoint type does not care how the data gets broken up
399 * across frames.
400 */
401 if (usb_pipebulk(pipe) &&
402 bus_to_hcd(dev->bus)->driver->flags & HCD_USB3) {
403 io->urbs = kmalloc(sizeof *io->urbs, mem_flags);
404 use_sg = true;
405 } else {
406 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
407 use_sg = false;
408 }
395 if (!io->urbs) 409 if (!io->urbs)
396 goto nomem; 410 goto nomem;
397 411
@@ -401,62 +415,92 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
401 if (usb_pipein(pipe)) 415 if (usb_pipein(pipe))
402 urb_flags |= URB_SHORT_NOT_OK; 416 urb_flags |= URB_SHORT_NOT_OK;
403 417
404 for_each_sg(sg, sg, io->entries, i) { 418 if (use_sg) {
405 unsigned len; 419 io->urbs[0] = usb_alloc_urb(0, mem_flags);
406 420 if (!io->urbs[0]) {
407 io->urbs[i] = usb_alloc_urb(0, mem_flags); 421 io->entries = 0;
408 if (!io->urbs[i]) {
409 io->entries = i;
410 goto nomem; 422 goto nomem;
411 } 423 }
412 424
413 io->urbs[i]->dev = NULL; 425 io->urbs[0]->dev = NULL;
414 io->urbs[i]->pipe = pipe; 426 io->urbs[0]->pipe = pipe;
415 io->urbs[i]->interval = period; 427 io->urbs[0]->interval = period;
416 io->urbs[i]->transfer_flags = urb_flags; 428 io->urbs[0]->transfer_flags = urb_flags;
417 429
418 io->urbs[i]->complete = sg_complete; 430 io->urbs[0]->complete = sg_complete;
419 io->urbs[i]->context = io; 431 io->urbs[0]->context = io;
420 432 /* A length of zero means transfer the whole sg list */
421 /* 433 io->urbs[0]->transfer_buffer_length = length;
422 * Some systems need to revert to PIO when DMA is temporarily 434 if (length == 0) {
423 * unavailable. For their sakes, both transfer_buffer and 435 for_each_sg(sg, sg, io->entries, i) {
424 * transfer_dma are set when possible. However this can only 436 io->urbs[0]->transfer_buffer_length +=
425 * work on systems without: 437 sg_dma_len(sg);
426 * 438 }
427 * - HIGHMEM, since DMA buffers located in high memory are 439 }
428 * not directly addressable by the CPU for PIO; 440 io->urbs[0]->sg = io;
429 * 441 io->urbs[0]->num_sgs = io->entries;
430 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to 442 io->entries = 1;
431 * make virtually discontiguous buffers be "dma-contiguous" 443 } else {
432 * so that PIO and DMA need diferent numbers of URBs. 444 for_each_sg(sg, sg, io->entries, i) {
433 * 445 unsigned len;
434 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL 446
435 * to prevent stale pointers and to help spot bugs. 447 io->urbs[i] = usb_alloc_urb(0, mem_flags);
436 */ 448 if (!io->urbs[i]) {
437 if (dma) { 449 io->entries = i;
438 io->urbs[i]->transfer_dma = sg_dma_address(sg); 450 goto nomem;
439 len = sg_dma_len(sg); 451 }
452
453 io->urbs[i]->dev = NULL;
454 io->urbs[i]->pipe = pipe;
455 io->urbs[i]->interval = period;
456 io->urbs[i]->transfer_flags = urb_flags;
457
458 io->urbs[i]->complete = sg_complete;
459 io->urbs[i]->context = io;
460
461 /*
462 * Some systems need to revert to PIO when DMA is
463 * temporarily unavailable. For their sakes, both
464 * transfer_buffer and transfer_dma are set when
465 * possible. However this can only work on systems
466 * without:
467 *
468 * - HIGHMEM, since DMA buffers located in high memory
469 * are not directly addressable by the CPU for PIO;
470 *
471 * - IOMMU, since dma_map_sg() is allowed to use an
472 * IOMMU to make virtually discontiguous buffers be
473 * "dma-contiguous" so that PIO and DMA need diferent
474 * numbers of URBs.
475 *
476 * So when HIGHMEM or IOMMU are in use, transfer_buffer
477 * is NULL to prevent stale pointers and to help spot
478 * bugs.
479 */
480 if (dma) {
481 io->urbs[i]->transfer_dma = sg_dma_address(sg);
482 len = sg_dma_len(sg);
440#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU) 483#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
441 io->urbs[i]->transfer_buffer = NULL; 484 io->urbs[i]->transfer_buffer = NULL;
442#else 485#else
443 io->urbs[i]->transfer_buffer = sg_virt(sg); 486 io->urbs[i]->transfer_buffer = sg_virt(sg);
444#endif 487#endif
445 } else { 488 } else {
446 /* hc may use _only_ transfer_buffer */ 489 /* hc may use _only_ transfer_buffer */
447 io->urbs[i]->transfer_buffer = sg_virt(sg); 490 io->urbs[i]->transfer_buffer = sg_virt(sg);
448 len = sg->length; 491 len = sg->length;
449 } 492 }
450 493
451 if (length) { 494 if (length) {
452 len = min_t(unsigned, len, length); 495 len = min_t(unsigned, len, length);
453 length -= len; 496 length -= len;
454 if (length == 0) 497 if (length == 0)
455 io->entries = i + 1; 498 io->entries = i + 1;
499 }
500 io->urbs[i]->transfer_buffer_length = len;
456 } 501 }
457 io->urbs[i]->transfer_buffer_length = len; 502 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
458 } 503 }
459 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
460 504
461 /* transaction state */ 505 /* transaction state */
462 io->count = io->entries; 506 io->count = io->entries;
@@ -509,6 +553,10 @@ EXPORT_SYMBOL_GPL(usb_sg_init);
509 * could be transferred. That capability is less useful for low or full 553 * could be transferred. That capability is less useful for low or full
510 * speed interrupt endpoints, which allow at most one packet per millisecond, 554 * speed interrupt endpoints, which allow at most one packet per millisecond,
511 * of at most 8 or 64 bytes (respectively). 555 * of at most 8 or 64 bytes (respectively).
556 *
557 * It is not necessary to call this function to reserve bandwidth for devices
558 * under an xHCI host controller, as the bandwidth is reserved when the
559 * configuration or interface alt setting is selected.
512 */ 560 */
513void usb_sg_wait(struct usb_sg_request *io) 561void usb_sg_wait(struct usb_sg_request *io)
514{ 562{
@@ -758,8 +806,50 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
758 return rc; 806 return rc;
759} 807}
760 808
809static int usb_get_langid(struct usb_device *dev, unsigned char *tbuf)
810{
811 int err;
812
813 if (dev->have_langid)
814 return 0;
815
816 if (dev->string_langid < 0)
817 return -EPIPE;
818
819 err = usb_string_sub(dev, 0, 0, tbuf);
820
821 /* If the string was reported but is malformed, default to english
822 * (0x0409) */
823 if (err == -ENODATA || (err > 0 && err < 4)) {
824 dev->string_langid = 0x0409;
825 dev->have_langid = 1;
826 dev_err(&dev->dev,
827 "string descriptor 0 malformed (err = %d), "
828 "defaulting to 0x%04x\n",
829 err, dev->string_langid);
830 return 0;
831 }
832
833 /* In case of all other errors, we assume the device is not able to
834 * deal with strings at all. Set string_langid to -1 in order to
835 * prevent any string to be retrieved from the device */
836 if (err < 0) {
837 dev_err(&dev->dev, "string descriptor 0 read error: %d\n",
838 err);
839 dev->string_langid = -1;
840 return -EPIPE;
841 }
842
843 /* always use the first langid listed */
844 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
845 dev->have_langid = 1;
846 dev_dbg(&dev->dev, "default language 0x%04x\n",
847 dev->string_langid);
848 return 0;
849}
850
761/** 851/**
762 * usb_string - returns ISO 8859-1 version of a string descriptor 852 * usb_string - returns UTF-8 version of a string descriptor
763 * @dev: the device whose string descriptor is being retrieved 853 * @dev: the device whose string descriptor is being retrieved
764 * @index: the number of the descriptor 854 * @index: the number of the descriptor
765 * @buf: where to put the string 855 * @buf: where to put the string
@@ -767,17 +857,10 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
767 * Context: !in_interrupt () 857 * Context: !in_interrupt ()
768 * 858 *
769 * This converts the UTF-16LE encoded strings returned by devices, from 859 * This converts the UTF-16LE encoded strings returned by devices, from
770 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones 860 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
771 * that are more usable in most kernel contexts. Note that all characters 861 * that are more usable in most kernel contexts. Note that this function
772 * in the chosen descriptor that can't be encoded using ISO-8859-1
773 * are converted to the question mark ("?") character, and this function
774 * chooses strings in the first language supported by the device. 862 * chooses strings in the first language supported by the device.
775 * 863 *
776 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
777 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
778 * and is appropriate for use many uses of English and several other
779 * Western European languages. (But it doesn't include the "Euro" symbol.)
780 *
781 * This call is synchronous, and may not be used in an interrupt context. 864 * This call is synchronous, and may not be used in an interrupt context.
782 * 865 *
783 * Returns length of the string (>= 0) or usb_control_msg status (< 0). 866 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
@@ -786,7 +869,6 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
786{ 869{
787 unsigned char *tbuf; 870 unsigned char *tbuf;
788 int err; 871 int err;
789 unsigned int u, idx;
790 872
791 if (dev->state == USB_STATE_SUSPENDED) 873 if (dev->state == USB_STATE_SUSPENDED)
792 return -EHOSTUNREACH; 874 return -EHOSTUNREACH;
@@ -797,40 +879,18 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
797 if (!tbuf) 879 if (!tbuf)
798 return -ENOMEM; 880 return -ENOMEM;
799 881
800 /* get langid for strings if it's not yet known */ 882 err = usb_get_langid(dev, tbuf);
801 if (!dev->have_langid) { 883 if (err < 0)
802 err = usb_string_sub(dev, 0, 0, tbuf); 884 goto errout;
803 if (err < 0) {
804 dev_err(&dev->dev,
805 "string descriptor 0 read error: %d\n",
806 err);
807 } else if (err < 4) {
808 dev_err(&dev->dev, "string descriptor 0 too short\n");
809 } else {
810 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
811 /* always use the first langid listed */
812 dev_dbg(&dev->dev, "default language 0x%04x\n",
813 dev->string_langid);
814 }
815
816 dev->have_langid = 1;
817 }
818 885
819 err = usb_string_sub(dev, dev->string_langid, index, tbuf); 886 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
820 if (err < 0) 887 if (err < 0)
821 goto errout; 888 goto errout;
822 889
823 size--; /* leave room for trailing NULL char in output buffer */ 890 size--; /* leave room for trailing NULL char in output buffer */
824 for (idx = 0, u = 2; u < err; u += 2) { 891 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
825 if (idx >= size) 892 UTF16_LITTLE_ENDIAN, buf, size);
826 break; 893 buf[err] = 0;
827 if (tbuf[u+1]) /* high byte */
828 buf[idx++] = '?'; /* non ISO-8859-1 character */
829 else
830 buf[idx++] = tbuf[u];
831 }
832 buf[idx] = 0;
833 err = idx;
834 894
835 if (tbuf[1] != USB_DT_STRING) 895 if (tbuf[1] != USB_DT_STRING)
836 dev_dbg(&dev->dev, 896 dev_dbg(&dev->dev,
@@ -843,6 +903,9 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
843} 903}
844EXPORT_SYMBOL_GPL(usb_string); 904EXPORT_SYMBOL_GPL(usb_string);
845 905
906/* one UTF-8-encoded 16-bit character has at most three bytes */
907#define MAX_USB_STRING_SIZE (127 * 3 + 1)
908
846/** 909/**
847 * usb_cache_string - read a string descriptor and cache it for later use 910 * usb_cache_string - read a string descriptor and cache it for later use
848 * @udev: the device whose string descriptor is being read 911 * @udev: the device whose string descriptor is being read
@@ -860,9 +923,9 @@ char *usb_cache_string(struct usb_device *udev, int index)
860 if (index <= 0) 923 if (index <= 0)
861 return NULL; 924 return NULL;
862 925
863 buf = kmalloc(256, GFP_KERNEL); 926 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_KERNEL);
864 if (buf) { 927 if (buf) {
865 len = usb_string(udev, index, buf, 256); 928 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
866 if (len > 0) { 929 if (len > 0) {
867 smallbuf = kmalloc(++len, GFP_KERNEL); 930 smallbuf = kmalloc(++len, GFP_KERNEL);
868 if (!smallbuf) 931 if (!smallbuf)
@@ -1664,6 +1727,21 @@ free_interfaces:
1664 if (ret) 1727 if (ret)
1665 goto free_interfaces; 1728 goto free_interfaces;
1666 1729
1730 /* Make sure we have bandwidth (and available HCD resources) for this
1731 * configuration. Remove endpoints from the schedule if we're dropping
1732 * this configuration to set configuration 0. After this point, the
1733 * host controller will not allow submissions to dropped endpoints. If
1734 * this call fails, the device state is unchanged.
1735 */
1736 if (cp)
1737 ret = usb_hcd_check_bandwidth(dev, cp, NULL);
1738 else
1739 ret = usb_hcd_check_bandwidth(dev, NULL, NULL);
1740 if (ret < 0) {
1741 usb_autosuspend_device(dev);
1742 goto free_interfaces;
1743 }
1744
1667 /* if it's already configured, clear out old state first. 1745 /* if it's already configured, clear out old state first.
1668 * getting rid of old interfaces means unbinding their drivers. 1746 * getting rid of old interfaces means unbinding their drivers.
1669 */ 1747 */
@@ -1686,6 +1764,7 @@ free_interfaces:
1686 dev->actconfig = cp; 1764 dev->actconfig = cp;
1687 if (!cp) { 1765 if (!cp) {
1688 usb_set_device_state(dev, USB_STATE_ADDRESS); 1766 usb_set_device_state(dev, USB_STATE_ADDRESS);
1767 usb_hcd_check_bandwidth(dev, NULL, NULL);
1689 usb_autosuspend_device(dev); 1768 usb_autosuspend_device(dev);
1690 goto free_interfaces; 1769 goto free_interfaces;
1691 } 1770 }
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index c66789197927..b5c72e458943 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -552,8 +552,8 @@ static struct attribute *dev_string_attrs[] = {
552static mode_t dev_string_attrs_are_visible(struct kobject *kobj, 552static mode_t dev_string_attrs_are_visible(struct kobject *kobj,
553 struct attribute *a, int n) 553 struct attribute *a, int n)
554{ 554{
555 struct usb_device *udev = to_usb_device( 555 struct device *dev = container_of(kobj, struct device, kobj);
556 container_of(kobj, struct device, kobj)); 556 struct usb_device *udev = to_usb_device(dev);
557 557
558 if (a == &dev_attr_manufacturer.attr) { 558 if (a == &dev_attr_manufacturer.attr) {
559 if (udev->manufacturer == NULL) 559 if (udev->manufacturer == NULL)
@@ -585,8 +585,8 @@ static ssize_t
585read_descriptors(struct kobject *kobj, struct bin_attribute *attr, 585read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
586 char *buf, loff_t off, size_t count) 586 char *buf, loff_t off, size_t count)
587{ 587{
588 struct usb_device *udev = to_usb_device( 588 struct device *dev = container_of(kobj, struct device, kobj);
589 container_of(kobj, struct device, kobj)); 589 struct usb_device *udev = to_usb_device(dev);
590 size_t nleft = count; 590 size_t nleft = count;
591 size_t srclen, n; 591 size_t srclen, n;
592 int cfgno; 592 int cfgno;
@@ -786,8 +786,8 @@ static struct attribute *intf_assoc_attrs[] = {
786static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj, 786static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
787 struct attribute *a, int n) 787 struct attribute *a, int n)
788{ 788{
789 struct usb_interface *intf = to_usb_interface( 789 struct device *dev = container_of(kobj, struct device, kobj);
790 container_of(kobj, struct device, kobj)); 790 struct usb_interface *intf = to_usb_interface(dev);
791 791
792 if (intf->intf_assoc == NULL) 792 if (intf->intf_assoc == NULL)
793 return 0; 793 return 0;
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 3376055f36e7..0885d4abdc62 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -241,6 +241,12 @@ EXPORT_SYMBOL_GPL(usb_unanchor_urb);
241 * If the USB subsystem can't allocate sufficient bandwidth to perform 241 * If the USB subsystem can't allocate sufficient bandwidth to perform
242 * the periodic request, submitting such a periodic request should fail. 242 * the periodic request, submitting such a periodic request should fail.
243 * 243 *
244 * For devices under xHCI, the bandwidth is reserved at configuration time, or
245 * when the alt setting is selected. If there is not enough bus bandwidth, the
246 * configuration/alt setting request will fail. Therefore, submissions to
247 * periodic endpoints on devices under xHCI should never fail due to bandwidth
248 * constraints.
249 *
244 * Device drivers must explicitly request that repetition, by ensuring that 250 * Device drivers must explicitly request that repetition, by ensuring that
245 * some URB is always on the endpoint's queue (except possibly for short 251 * some URB is always on the endpoint's queue (except possibly for short
246 * periods during completion callacks). When there is no longer an urb 252 * periods during completion callacks). When there is no longer an urb
@@ -351,6 +357,7 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
351 if (xfertype == USB_ENDPOINT_XFER_ISOC) { 357 if (xfertype == USB_ENDPOINT_XFER_ISOC) {
352 int n, len; 358 int n, len;
353 359
360 /* FIXME SuperSpeed isoc endpoints have up to 16 bursts */
354 /* "high bandwidth" mode, 1-3 packets/uframe? */ 361 /* "high bandwidth" mode, 1-3 packets/uframe? */
355 if (dev->speed == USB_SPEED_HIGH) { 362 if (dev->speed == USB_SPEED_HIGH) {
356 int mult = 1 + ((max >> 11) & 0x03); 363 int mult = 1 + ((max >> 11) & 0x03);
@@ -426,6 +433,11 @@ int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
426 return -EINVAL; 433 return -EINVAL;
427 /* too big? */ 434 /* too big? */
428 switch (dev->speed) { 435 switch (dev->speed) {
436 case USB_SPEED_SUPER: /* units are 125us */
437 /* Handle up to 2^(16-1) microframes */
438 if (urb->interval > (1 << 15))
439 return -EINVAL;
440 max = 1 << 15;
429 case USB_SPEED_HIGH: /* units are microframes */ 441 case USB_SPEED_HIGH: /* units are microframes */
430 /* NOTE usb handles 2^15 */ 442 /* NOTE usb handles 2^15 */
431 if (urb->interval > (1024 * 8)) 443 if (urb->interval > (1024 * 8))
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 7eee400d3e32..a26f73880c32 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -34,6 +34,7 @@
34#include <linux/usb.h> 34#include <linux/usb.h>
35#include <linux/mutex.h> 35#include <linux/mutex.h>
36#include <linux/workqueue.h> 36#include <linux/workqueue.h>
37#include <linux/debugfs.h>
37 38
38#include <asm/io.h> 39#include <asm/io.h>
39#include <linux/scatterlist.h> 40#include <linux/scatterlist.h>
@@ -139,8 +140,7 @@ static int __find_interface(struct device *dev, void *data)
139 struct find_interface_arg *arg = data; 140 struct find_interface_arg *arg = data;
140 struct usb_interface *intf; 141 struct usb_interface *intf;
141 142
142 /* can't look at usb devices, only interfaces */ 143 if (!is_usb_interface(dev))
143 if (is_usb_device(dev))
144 return 0; 144 return 0;
145 145
146 intf = to_usb_interface(dev); 146 intf = to_usb_interface(dev);
@@ -184,11 +184,16 @@ EXPORT_SYMBOL_GPL(usb_find_interface);
184static void usb_release_dev(struct device *dev) 184static void usb_release_dev(struct device *dev)
185{ 185{
186 struct usb_device *udev; 186 struct usb_device *udev;
187 struct usb_hcd *hcd;
187 188
188 udev = to_usb_device(dev); 189 udev = to_usb_device(dev);
190 hcd = bus_to_hcd(udev->bus);
189 191
190 usb_destroy_configuration(udev); 192 usb_destroy_configuration(udev);
191 usb_put_hcd(bus_to_hcd(udev->bus)); 193 /* Root hubs aren't real devices, so don't free HCD resources */
194 if (hcd->driver->free_dev && udev->parent)
195 hcd->driver->free_dev(hcd, udev);
196 usb_put_hcd(hcd);
192 kfree(udev->product); 197 kfree(udev->product);
193 kfree(udev->manufacturer); 198 kfree(udev->manufacturer);
194 kfree(udev->serial); 199 kfree(udev->serial);
@@ -305,10 +310,21 @@ static struct dev_pm_ops usb_device_pm_ops = {
305 310
306#endif /* CONFIG_PM */ 311#endif /* CONFIG_PM */
307 312
313
314static char *usb_nodename(struct device *dev)
315{
316 struct usb_device *usb_dev;
317
318 usb_dev = to_usb_device(dev);
319 return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
320 usb_dev->bus->busnum, usb_dev->devnum);
321}
322
308struct device_type usb_device_type = { 323struct device_type usb_device_type = {
309 .name = "usb_device", 324 .name = "usb_device",
310 .release = usb_release_dev, 325 .release = usb_release_dev,
311 .uevent = usb_dev_uevent, 326 .uevent = usb_dev_uevent,
327 .nodename = usb_nodename,
312 .pm = &usb_device_pm_ops, 328 .pm = &usb_device_pm_ops,
313}; 329};
314 330
@@ -348,6 +364,13 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
348 kfree(dev); 364 kfree(dev);
349 return NULL; 365 return NULL;
350 } 366 }
367 /* Root hubs aren't true devices, so don't allocate HCD resources */
368 if (usb_hcd->driver->alloc_dev && parent &&
369 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
370 usb_put_hcd(bus_to_hcd(bus));
371 kfree(dev);
372 return NULL;
373 }
351 374
352 device_initialize(&dev->dev); 375 device_initialize(&dev->dev);
353 dev->dev.bus = &usb_bus_type; 376 dev->dev.bus = &usb_bus_type;
@@ -375,18 +398,24 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
375 */ 398 */
376 if (unlikely(!parent)) { 399 if (unlikely(!parent)) {
377 dev->devpath[0] = '0'; 400 dev->devpath[0] = '0';
401 dev->route = 0;
378 402
379 dev->dev.parent = bus->controller; 403 dev->dev.parent = bus->controller;
380 dev_set_name(&dev->dev, "usb%d", bus->busnum); 404 dev_set_name(&dev->dev, "usb%d", bus->busnum);
381 root_hub = 1; 405 root_hub = 1;
382 } else { 406 } else {
383 /* match any labeling on the hubs; it's one-based */ 407 /* match any labeling on the hubs; it's one-based */
384 if (parent->devpath[0] == '0') 408 if (parent->devpath[0] == '0') {
385 snprintf(dev->devpath, sizeof dev->devpath, 409 snprintf(dev->devpath, sizeof dev->devpath,
386 "%d", port1); 410 "%d", port1);
387 else 411 /* Root ports are not counted in route string */
412 dev->route = 0;
413 } else {
388 snprintf(dev->devpath, sizeof dev->devpath, 414 snprintf(dev->devpath, sizeof dev->devpath,
389 "%s.%d", parent->devpath, port1); 415 "%s.%d", parent->devpath, port1);
416 dev->route = parent->route +
417 (port1 << ((parent->level - 1)*4));
418 }
390 419
391 dev->dev.parent = &parent->dev; 420 dev->dev.parent = &parent->dev;
392 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath); 421 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
@@ -799,12 +828,12 @@ void usb_buffer_dmasync(struct urb *urb)
799 return; 828 return;
800 829
801 if (controller->dma_mask) { 830 if (controller->dma_mask) {
802 dma_sync_single(controller, 831 dma_sync_single_for_cpu(controller,
803 urb->transfer_dma, urb->transfer_buffer_length, 832 urb->transfer_dma, urb->transfer_buffer_length,
804 usb_pipein(urb->pipe) 833 usb_pipein(urb->pipe)
805 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 834 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
806 if (usb_pipecontrol(urb->pipe)) 835 if (usb_pipecontrol(urb->pipe))
807 dma_sync_single(controller, 836 dma_sync_single_for_cpu(controller,
808 urb->setup_dma, 837 urb->setup_dma,
809 sizeof(struct usb_ctrlrequest), 838 sizeof(struct usb_ctrlrequest),
810 DMA_TO_DEVICE); 839 DMA_TO_DEVICE);
@@ -922,8 +951,8 @@ void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
922 || !controller->dma_mask) 951 || !controller->dma_mask)
923 return; 952 return;
924 953
925 dma_sync_sg(controller, sg, n_hw_ents, 954 dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
926 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 955 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
927} 956}
928EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg); 957EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
929#endif 958#endif
@@ -1001,6 +1030,35 @@ static struct notifier_block usb_bus_nb = {
1001 .notifier_call = usb_bus_notify, 1030 .notifier_call = usb_bus_notify,
1002}; 1031};
1003 1032
1033struct dentry *usb_debug_root;
1034EXPORT_SYMBOL_GPL(usb_debug_root);
1035
1036struct dentry *usb_debug_devices;
1037
1038static int usb_debugfs_init(void)
1039{
1040 usb_debug_root = debugfs_create_dir("usb", NULL);
1041 if (!usb_debug_root)
1042 return -ENOENT;
1043
1044 usb_debug_devices = debugfs_create_file("devices", 0444,
1045 usb_debug_root, NULL,
1046 &usbfs_devices_fops);
1047 if (!usb_debug_devices) {
1048 debugfs_remove(usb_debug_root);
1049 usb_debug_root = NULL;
1050 return -ENOENT;
1051 }
1052
1053 return 0;
1054}
1055
1056static void usb_debugfs_cleanup(void)
1057{
1058 debugfs_remove(usb_debug_devices);
1059 debugfs_remove(usb_debug_root);
1060}
1061
1004/* 1062/*
1005 * Init 1063 * Init
1006 */ 1064 */
@@ -1012,6 +1070,10 @@ static int __init usb_init(void)
1012 return 0; 1070 return 0;
1013 } 1071 }
1014 1072
1073 retval = usb_debugfs_init();
1074 if (retval)
1075 goto out;
1076
1015 retval = ksuspend_usb_init(); 1077 retval = ksuspend_usb_init();
1016 if (retval) 1078 if (retval)
1017 goto out; 1079 goto out;
@@ -1021,9 +1083,6 @@ static int __init usb_init(void)
1021 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb); 1083 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1022 if (retval) 1084 if (retval)
1023 goto bus_notifier_failed; 1085 goto bus_notifier_failed;
1024 retval = usb_host_init();
1025 if (retval)
1026 goto host_init_failed;
1027 retval = usb_major_init(); 1086 retval = usb_major_init();
1028 if (retval) 1087 if (retval)
1029 goto major_init_failed; 1088 goto major_init_failed;
@@ -1053,8 +1112,6 @@ usb_devio_init_failed:
1053driver_register_failed: 1112driver_register_failed:
1054 usb_major_cleanup(); 1113 usb_major_cleanup();
1055major_init_failed: 1114major_init_failed:
1056 usb_host_cleanup();
1057host_init_failed:
1058 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1115 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1059bus_notifier_failed: 1116bus_notifier_failed:
1060 bus_unregister(&usb_bus_type); 1117 bus_unregister(&usb_bus_type);
@@ -1079,10 +1136,10 @@ static void __exit usb_exit(void)
1079 usb_deregister(&usbfs_driver); 1136 usb_deregister(&usbfs_driver);
1080 usb_devio_cleanup(); 1137 usb_devio_cleanup();
1081 usb_hub_cleanup(); 1138 usb_hub_cleanup();
1082 usb_host_cleanup();
1083 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1139 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1084 bus_unregister(&usb_bus_type); 1140 bus_unregister(&usb_bus_type);
1085 ksuspend_usb_cleanup(); 1141 ksuspend_usb_cleanup();
1142 usb_debugfs_cleanup();
1086} 1143}
1087 1144
1088subsys_initcall(usb_init); 1145subsys_initcall(usb_init);
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index 79d8a9ea559b..e2a8cfaade1d 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -41,8 +41,6 @@ extern int usb_hub_init(void);
41extern void usb_hub_cleanup(void); 41extern void usb_hub_cleanup(void);
42extern int usb_major_init(void); 42extern int usb_major_init(void);
43extern void usb_major_cleanup(void); 43extern void usb_major_cleanup(void);
44extern int usb_host_init(void);
45extern void usb_host_cleanup(void);
46 44
47#ifdef CONFIG_PM 45#ifdef CONFIG_PM
48 46
@@ -106,6 +104,7 @@ extern struct workqueue_struct *ksuspend_usb_wq;
106extern struct bus_type usb_bus_type; 104extern struct bus_type usb_bus_type;
107extern struct device_type usb_device_type; 105extern struct device_type usb_device_type;
108extern struct device_type usb_if_device_type; 106extern struct device_type usb_if_device_type;
107extern struct device_type usb_ep_device_type;
109extern struct usb_device_driver usb_generic_driver; 108extern struct usb_device_driver usb_generic_driver;
110 109
111static inline int is_usb_device(const struct device *dev) 110static inline int is_usb_device(const struct device *dev)
@@ -113,6 +112,16 @@ static inline int is_usb_device(const struct device *dev)
113 return dev->type == &usb_device_type; 112 return dev->type == &usb_device_type;
114} 113}
115 114
115static inline int is_usb_interface(const struct device *dev)
116{
117 return dev->type == &usb_if_device_type;
118}
119
120static inline int is_usb_endpoint(const struct device *dev)
121{
122 return dev->type == &usb_ep_device_type;
123}
124
116/* Do the same for device drivers and interface drivers. */ 125/* Do the same for device drivers and interface drivers. */
117 126
118static inline int is_usb_device_driver(struct device_driver *drv) 127static inline int is_usb_device_driver(struct device_driver *drv)
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 080bb1e4b847..7f8e83a954ac 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -156,7 +156,7 @@ config USB_ATMEL_USBA
156 156
157config USB_GADGET_FSL_USB2 157config USB_GADGET_FSL_USB2
158 boolean "Freescale Highspeed USB DR Peripheral Controller" 158 boolean "Freescale Highspeed USB DR Peripheral Controller"
159 depends on FSL_SOC 159 depends on FSL_SOC || ARCH_MXC
160 select USB_GADGET_DUALSPEED 160 select USB_GADGET_DUALSPEED
161 help 161 help
162 Some of Freescale PowerPC processors have a High Speed 162 Some of Freescale PowerPC processors have a High Speed
@@ -253,7 +253,7 @@ config USB_PXA25X_SMALL
253 253
254config USB_GADGET_PXA27X 254config USB_GADGET_PXA27X
255 boolean "PXA 27x" 255 boolean "PXA 27x"
256 depends on ARCH_PXA && PXA27x 256 depends on ARCH_PXA && (PXA27x || PXA3xx)
257 select USB_OTG_UTILS 257 select USB_OTG_UTILS
258 help 258 help
259 Intel's PXA 27x series XScale ARM v5TE processors include 259 Intel's PXA 27x series XScale ARM v5TE processors include
@@ -272,6 +272,41 @@ config USB_PXA27X
272 default USB_GADGET 272 default USB_GADGET
273 select USB_GADGET_SELECTED 273 select USB_GADGET_SELECTED
274 274
275config USB_GADGET_S3C_HSOTG
276 boolean "S3C HS/OtG USB Device controller"
277 depends on S3C_DEV_USB_HSOTG
278 select USB_GADGET_S3C_HSOTG_PIO
279 help
280 The Samsung S3C64XX USB2.0 high-speed gadget controller
281 integrated into the S3C64XX series SoC.
282
283config USB_S3C_HSOTG
284 tristate
285 depends on USB_GADGET_S3C_HSOTG
286 default USB_GADGET
287 select USB_GADGET_SELECTED
288
289config USB_GADGET_IMX
290 boolean "Freescale IMX USB Peripheral Controller"
291 depends on ARCH_MX1
292 help
293 Freescale's IMX series include an integrated full speed
294 USB 1.1 device controller. The controller in the IMX series
295 is register-compatible.
296
297 It has Six fixed-function endpoints, as well as endpoint
298 zero (for control transfers).
299
300 Say "y" to link the driver statically, or "m" to build a
301 dynamically linked module called "imx_udc" and force all
302 gadget drivers to also be dynamically linked.
303
304config USB_IMX
305 tristate
306 depends on USB_GADGET_IMX
307 default USB_GADGET
308 select USB_GADGET_SELECTED
309
275config USB_GADGET_S3C2410 310config USB_GADGET_S3C2410
276 boolean "S3C2410 USB Device Controller" 311 boolean "S3C2410 USB Device Controller"
277 depends on ARCH_S3C2410 312 depends on ARCH_S3C2410
@@ -307,27 +342,6 @@ config USB_GADGET_MUSB_HDRC
307 This OTG-capable silicon IP is used in dual designs including 342 This OTG-capable silicon IP is used in dual designs including
308 the TI DaVinci, OMAP 243x, OMAP 343x, TUSB 6010, and ADI Blackfin 343 the TI DaVinci, OMAP 243x, OMAP 343x, TUSB 6010, and ADI Blackfin
309 344
310config USB_GADGET_IMX
311 boolean "Freescale IMX USB Peripheral Controller"
312 depends on ARCH_MX1
313 help
314 Freescale's IMX series include an integrated full speed
315 USB 1.1 device controller. The controller in the IMX series
316 is register-compatible.
317
318 It has Six fixed-function endpoints, as well as endpoint
319 zero (for control transfers).
320
321 Say "y" to link the driver statically, or "m" to build a
322 dynamically linked module called "imx_udc" and force all
323 gadget drivers to also be dynamically linked.
324
325config USB_IMX
326 tristate
327 depends on USB_GADGET_IMX
328 default USB_GADGET
329 select USB_GADGET_SELECTED
330
331config USB_GADGET_M66592 345config USB_GADGET_M66592
332 boolean "Renesas M66592 USB Peripheral Controller" 346 boolean "Renesas M66592 USB Peripheral Controller"
333 select USB_GADGET_DUALSPEED 347 select USB_GADGET_DUALSPEED
@@ -460,6 +474,27 @@ config USB_GOKU
460 default USB_GADGET 474 default USB_GADGET
461 select USB_GADGET_SELECTED 475 select USB_GADGET_SELECTED
462 476
477config USB_GADGET_LANGWELL
478 boolean "Intel Langwell USB Device Controller"
479 depends on PCI
480 select USB_GADGET_DUALSPEED
481 help
482 Intel Langwell USB Device Controller is a High-Speed USB
483 On-The-Go device controller.
484
485 The number of programmable endpoints is different through
486 controller revision.
487
488 Say "y" to link the driver statically, or "m" to build a
489 dynamically linked module called "langwell_udc" and force all
490 gadget drivers to also be dynamically linked.
491
492config USB_LANGWELL
493 tristate
494 depends on USB_GADGET_LANGWELL
495 default USB_GADGET
496 select USB_GADGET_SELECTED
497
463 498
464# 499#
465# LAST -- dummy/emulated controller 500# LAST -- dummy/emulated controller
@@ -566,6 +601,21 @@ config USB_ZERO_HNPTEST
566 the "B-Peripheral" role, that device will use HNP to let this 601 the "B-Peripheral" role, that device will use HNP to let this
567 one serve as the USB host instead (in the "B-Host" role). 602 one serve as the USB host instead (in the "B-Host" role).
568 603
604config USB_AUDIO
605 tristate "Audio Gadget (EXPERIMENTAL)"
606 depends on SND
607 select SND_PCM
608 help
609 Gadget Audio is compatible with USB Audio Class specification 1.0.
610 It will include at least one AudioControl interface, zero or more
611 AudioStream interface and zero or more MIDIStream interface.
612
613 Gadget Audio will use on-board ALSA (CONFIG_SND) audio card to
614 playback or capture audio stream.
615
616 Say "y" to link the driver statically, or "m" to build a
617 dynamically linked module called "g_audio".
618
569config USB_ETH 619config USB_ETH
570 tristate "Ethernet Gadget (with CDC Ethernet support)" 620 tristate "Ethernet Gadget (with CDC Ethernet support)"
571 depends on NET 621 depends on NET
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 39a51d746cb7..e6017e6bf6da 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -18,14 +18,21 @@ obj-$(CONFIG_USB_S3C2410) += s3c2410_udc.o
18obj-$(CONFIG_USB_AT91) += at91_udc.o 18obj-$(CONFIG_USB_AT91) += at91_udc.o
19obj-$(CONFIG_USB_ATMEL_USBA) += atmel_usba_udc.o 19obj-$(CONFIG_USB_ATMEL_USBA) += atmel_usba_udc.o
20obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o 20obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o
21fsl_usb2_udc-objs := fsl_udc_core.o
22ifeq ($(CONFIG_ARCH_MXC),y)
23fsl_usb2_udc-objs += fsl_mx3_udc.o
24endif
21obj-$(CONFIG_USB_M66592) += m66592-udc.o 25obj-$(CONFIG_USB_M66592) += m66592-udc.o
22obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o 26obj-$(CONFIG_USB_FSL_QE) += fsl_qe_udc.o
23obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o 27obj-$(CONFIG_USB_CI13XXX) += ci13xxx_udc.o
28obj-$(CONFIG_USB_S3C_HSOTG) += s3c-hsotg.o
29obj-$(CONFIG_USB_LANGWELL) += langwell_udc.o
24 30
25# 31#
26# USB gadget drivers 32# USB gadget drivers
27# 33#
28g_zero-objs := zero.o 34g_zero-objs := zero.o
35g_audio-objs := audio.o
29g_ether-objs := ether.o 36g_ether-objs := ether.o
30g_serial-objs := serial.o 37g_serial-objs := serial.o
31g_midi-objs := gmidi.o 38g_midi-objs := gmidi.o
@@ -35,6 +42,7 @@ g_printer-objs := printer.o
35g_cdc-objs := cdc2.o 42g_cdc-objs := cdc2.o
36 43
37obj-$(CONFIG_USB_ZERO) += g_zero.o 44obj-$(CONFIG_USB_ZERO) += g_zero.o
45obj-$(CONFIG_USB_AUDIO) += g_audio.o
38obj-$(CONFIG_USB_ETH) += g_ether.o 46obj-$(CONFIG_USB_ETH) += g_ether.o
39obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 47obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
40obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 48obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
diff --git a/drivers/usb/gadget/amd5536udc.c b/drivers/usb/gadget/amd5536udc.c
index 826f3adde5d8..77352ccc245e 100644
--- a/drivers/usb/gadget/amd5536udc.c
+++ b/drivers/usb/gadget/amd5536udc.c
@@ -48,7 +48,6 @@
48#include <linux/ioport.h> 48#include <linux/ioport.h>
49#include <linux/sched.h> 49#include <linux/sched.h>
50#include <linux/slab.h> 50#include <linux/slab.h>
51#include <linux/smp_lock.h>
52#include <linux/errno.h> 51#include <linux/errno.h>
53#include <linux/init.h> 52#include <linux/init.h>
54#include <linux/timer.h> 53#include <linux/timer.h>
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index 0b2bb8f0706d..72bae8f39d81 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -485,7 +485,7 @@ static int at91_ep_enable(struct usb_ep *_ep,
485 return -ESHUTDOWN; 485 return -ESHUTDOWN;
486 } 486 }
487 487
488 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 488 tmp = usb_endpoint_type(desc);
489 switch (tmp) { 489 switch (tmp) {
490 case USB_ENDPOINT_XFER_CONTROL: 490 case USB_ENDPOINT_XFER_CONTROL:
491 DBG("only one control endpoint\n"); 491 DBG("only one control endpoint\n");
@@ -517,7 +517,7 @@ ok:
517 local_irq_save(flags); 517 local_irq_save(flags);
518 518
519 /* initialize endpoint to match this descriptor */ 519 /* initialize endpoint to match this descriptor */
520 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0; 520 ep->is_in = usb_endpoint_dir_in(desc);
521 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC); 521 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
522 ep->stopped = 0; 522 ep->stopped = 0;
523 if (ep->is_in) 523 if (ep->is_in)
@@ -1574,7 +1574,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1574 1574
1575 udc->driver = driver; 1575 udc->driver = driver;
1576 udc->gadget.dev.driver = &driver->driver; 1576 udc->gadget.dev.driver = &driver->driver;
1577 udc->gadget.dev.driver_data = &driver->driver; 1577 dev_set_drvdata(&udc->gadget.dev, &driver->driver);
1578 udc->enabled = 1; 1578 udc->enabled = 1;
1579 udc->selfpowered = 1; 1579 udc->selfpowered = 1;
1580 1580
@@ -1583,7 +1583,7 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1583 DBG("driver->bind() returned %d\n", retval); 1583 DBG("driver->bind() returned %d\n", retval);
1584 udc->driver = NULL; 1584 udc->driver = NULL;
1585 udc->gadget.dev.driver = NULL; 1585 udc->gadget.dev.driver = NULL;
1586 udc->gadget.dev.driver_data = NULL; 1586 dev_set_drvdata(&udc->gadget.dev, NULL);
1587 udc->enabled = 0; 1587 udc->enabled = 0;
1588 udc->selfpowered = 0; 1588 udc->selfpowered = 0;
1589 return retval; 1589 return retval;
@@ -1613,7 +1613,7 @@ int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1613 1613
1614 driver->unbind(&udc->gadget); 1614 driver->unbind(&udc->gadget);
1615 udc->gadget.dev.driver = NULL; 1615 udc->gadget.dev.driver = NULL;
1616 udc->gadget.dev.driver_data = NULL; 1616 dev_set_drvdata(&udc->gadget.dev, NULL);
1617 udc->driver = NULL; 1617 udc->driver = NULL;
1618 1618
1619 DBG("unbound from %s\n", driver->driver.name); 1619 DBG("unbound from %s\n", driver->driver.name);
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 05c913cc3658..4e970cf0e29a 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -326,13 +326,7 @@ static int vbus_is_present(struct usba_udc *udc)
326 return 1; 326 return 1;
327} 327}
328 328
329#if defined(CONFIG_AVR32) 329#if defined(CONFIG_ARCH_AT91SAM9RL)
330
331static void toggle_bias(int is_on)
332{
333}
334
335#elif defined(CONFIG_ARCH_AT91)
336 330
337#include <mach/at91_pmc.h> 331#include <mach/at91_pmc.h>
338 332
@@ -346,7 +340,13 @@ static void toggle_bias(int is_on)
346 at91_sys_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN)); 340 at91_sys_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
347} 341}
348 342
349#endif /* CONFIG_ARCH_AT91 */ 343#else
344
345static void toggle_bias(int is_on)
346{
347}
348
349#endif /* CONFIG_ARCH_AT91SAM9RL */
350 350
351static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req) 351static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
352{ 352{
@@ -550,12 +550,12 @@ usba_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
550 DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n", 550 DBG(DBG_HW, "%s: EPT_SIZE = %lu (maxpacket = %lu)\n",
551 ep->ep.name, ept_cfg, maxpacket); 551 ep->ep.name, ept_cfg, maxpacket);
552 552
553 if ((desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) { 553 if (usb_endpoint_dir_in(desc)) {
554 ep->is_in = 1; 554 ep->is_in = 1;
555 ept_cfg |= USBA_EPT_DIR_IN; 555 ept_cfg |= USBA_EPT_DIR_IN;
556 } 556 }
557 557
558 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 558 switch (usb_endpoint_type(desc)) {
559 case USB_ENDPOINT_XFER_CONTROL: 559 case USB_ENDPOINT_XFER_CONTROL:
560 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL); 560 ept_cfg |= USBA_BF(EPT_TYPE, USBA_EPT_TYPE_CONTROL);
561 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE); 561 ept_cfg |= USBA_BF(BK_NUMBER, USBA_BK_NUMBER_ONE);
diff --git a/drivers/usb/gadget/audio.c b/drivers/usb/gadget/audio.c
new file mode 100644
index 000000000000..9f80f4e970bd
--- /dev/null
+++ b/drivers/usb/gadget/audio.c
@@ -0,0 +1,302 @@
1/*
2 * audio.c -- Audio gadget driver
3 *
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kernel.h>
15#include <linux/utsname.h>
16
17#include "u_audio.h"
18
19#define DRIVER_DESC "Linux USB Audio Gadget"
20#define DRIVER_VERSION "Dec 18, 2008"
21
22/*-------------------------------------------------------------------------*/
23
24/*
25 * Kbuild is not very cooperative with respect to linking separately
26 * compiled library objects into one module. So for now we won't use
27 * separate compilation ... ensuring init/exit sections work to shrink
28 * the runtime footprint, and giving us at least some parts of what
29 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
30 */
31#include "composite.c"
32#include "usbstring.c"
33#include "config.c"
34#include "epautoconf.c"
35
36#include "u_audio.c"
37#include "f_audio.c"
38
39/*-------------------------------------------------------------------------*/
40
41/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
42 * Instead: allocate your own, using normal USB-IF procedures.
43 */
44
45/* Thanks to Linux Foundation for donating this product ID. */
46#define AUDIO_VENDOR_NUM 0x1d6b /* Linux Foundation */
47#define AUDIO_PRODUCT_NUM 0x0101 /* Linux-USB Audio Gadget */
48
49/*-------------------------------------------------------------------------*/
50
51static struct usb_device_descriptor device_desc = {
52 .bLength = sizeof device_desc,
53 .bDescriptorType = USB_DT_DEVICE,
54
55 .bcdUSB = __constant_cpu_to_le16(0x200),
56
57 .bDeviceClass = USB_CLASS_PER_INTERFACE,
58 .bDeviceSubClass = 0,
59 .bDeviceProtocol = 0,
60 /* .bMaxPacketSize0 = f(hardware) */
61
62 /* Vendor and product id defaults change according to what configs
63 * we support. (As does bNumConfigurations.) These values can
64 * also be overridden by module parameters.
65 */
66 .idVendor = __constant_cpu_to_le16(AUDIO_VENDOR_NUM),
67 .idProduct = __constant_cpu_to_le16(AUDIO_PRODUCT_NUM),
68 /* .bcdDevice = f(hardware) */
69 /* .iManufacturer = DYNAMIC */
70 /* .iProduct = DYNAMIC */
71 /* NO SERIAL NUMBER */
72 .bNumConfigurations = 1,
73};
74
75static struct usb_otg_descriptor otg_descriptor = {
76 .bLength = sizeof otg_descriptor,
77 .bDescriptorType = USB_DT_OTG,
78
79 /* REVISIT SRP-only hardware is possible, although
80 * it would not be called "OTG" ...
81 */
82 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
83};
84
85static const struct usb_descriptor_header *otg_desc[] = {
86 (struct usb_descriptor_header *) &otg_descriptor,
87 NULL,
88};
89
90/*-------------------------------------------------------------------------*/
91
92/**
93 * Handle USB audio endpoint set/get command in setup class request
94 */
95
96static int audio_set_endpoint_req(struct usb_configuration *c,
97 const struct usb_ctrlrequest *ctrl)
98{
99 struct usb_composite_dev *cdev = c->cdev;
100 int value = -EOPNOTSUPP;
101 u16 ep = le16_to_cpu(ctrl->wIndex);
102 u16 len = le16_to_cpu(ctrl->wLength);
103 u16 w_value = le16_to_cpu(ctrl->wValue);
104
105 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
106 ctrl->bRequest, w_value, len, ep);
107
108 switch (ctrl->bRequest) {
109 case SET_CUR:
110 value = 0;
111 break;
112
113 case SET_MIN:
114 break;
115
116 case SET_MAX:
117 break;
118
119 case SET_RES:
120 break;
121
122 case SET_MEM:
123 break;
124
125 default:
126 break;
127 }
128
129 return value;
130}
131
132static int audio_get_endpoint_req(struct usb_configuration *c,
133 const struct usb_ctrlrequest *ctrl)
134{
135 struct usb_composite_dev *cdev = c->cdev;
136 int value = -EOPNOTSUPP;
137 u8 ep = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
138 u16 len = le16_to_cpu(ctrl->wLength);
139 u16 w_value = le16_to_cpu(ctrl->wValue);
140
141 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, endpoint %d\n",
142 ctrl->bRequest, w_value, len, ep);
143
144 switch (ctrl->bRequest) {
145 case GET_CUR:
146 case GET_MIN:
147 case GET_MAX:
148 case GET_RES:
149 value = 3;
150 break;
151 case GET_MEM:
152 break;
153 default:
154 break;
155 }
156
157 return value;
158}
159
160static int
161audio_setup(struct usb_configuration *c, const struct usb_ctrlrequest *ctrl)
162{
163 struct usb_composite_dev *cdev = c->cdev;
164 struct usb_request *req = cdev->req;
165 int value = -EOPNOTSUPP;
166 u16 w_index = le16_to_cpu(ctrl->wIndex);
167 u16 w_value = le16_to_cpu(ctrl->wValue);
168 u16 w_length = le16_to_cpu(ctrl->wLength);
169
170 /* composite driver infrastructure handles everything except
171 * Audio class messages; interface activation uses set_alt().
172 */
173 switch (ctrl->bRequestType) {
174 case USB_AUDIO_SET_ENDPOINT:
175 value = audio_set_endpoint_req(c, ctrl);
176 break;
177
178 case USB_AUDIO_GET_ENDPOINT:
179 value = audio_get_endpoint_req(c, ctrl);
180 break;
181
182 default:
183 ERROR(cdev, "Invalid control req%02x.%02x v%04x i%04x l%d\n",
184 ctrl->bRequestType, ctrl->bRequest,
185 w_value, w_index, w_length);
186 }
187
188 /* respond with data transfer or status phase? */
189 if (value >= 0) {
190 DBG(cdev, "Audio req%02x.%02x v%04x i%04x l%d\n",
191 ctrl->bRequestType, ctrl->bRequest,
192 w_value, w_index, w_length);
193 req->zero = 0;
194 req->length = value;
195 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
196 if (value < 0)
197 ERROR(cdev, "Audio response on err %d\n", value);
198 }
199
200 /* device either stalls (value < 0) or reports success */
201 return value;
202}
203
204/*-------------------------------------------------------------------------*/
205
206static int __init audio_do_config(struct usb_configuration *c)
207{
208 /* FIXME alloc iConfiguration string, set it in c->strings */
209
210 if (gadget_is_otg(c->cdev->gadget)) {
211 c->descriptors = otg_desc;
212 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
213 }
214
215 audio_bind_config(c);
216
217 return 0;
218}
219
220static struct usb_configuration audio_config_driver = {
221 .label = DRIVER_DESC,
222 .bind = audio_do_config,
223 .setup = audio_setup,
224 .bConfigurationValue = 1,
225 /* .iConfiguration = DYNAMIC */
226 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
227};
228
229/*-------------------------------------------------------------------------*/
230
231static int __init audio_bind(struct usb_composite_dev *cdev)
232{
233 int gcnum;
234 int status;
235
236 gcnum = usb_gadget_controller_number(cdev->gadget);
237 if (gcnum >= 0)
238 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
239 else {
240 ERROR(cdev, "controller '%s' not recognized; trying %s\n",
241 cdev->gadget->name,
242 audio_config_driver.label);
243 device_desc.bcdDevice =
244 __constant_cpu_to_le16(0x0300 | 0x0099);
245 }
246
247 /* device descriptor strings: manufacturer, product */
248 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
249 init_utsname()->sysname, init_utsname()->release,
250 cdev->gadget->name);
251 status = usb_string_id(cdev);
252 if (status < 0)
253 goto fail;
254 strings_dev[STRING_MANUFACTURER_IDX].id = status;
255 device_desc.iManufacturer = status;
256
257 status = usb_string_id(cdev);
258 if (status < 0)
259 goto fail;
260 strings_dev[STRING_PRODUCT_IDX].id = status;
261 device_desc.iProduct = status;
262
263 status = usb_add_config(cdev, &audio_config_driver);
264 if (status < 0)
265 goto fail;
266
267 INFO(cdev, "%s, version: %s\n", DRIVER_DESC, DRIVER_VERSION);
268 return 0;
269
270fail:
271 return status;
272}
273
274static int __exit audio_unbind(struct usb_composite_dev *cdev)
275{
276 return 0;
277}
278
279static struct usb_composite_driver audio_driver = {
280 .name = "g_audio",
281 .dev = &device_desc,
282 .strings = audio_strings,
283 .bind = audio_bind,
284 .unbind = __exit_p(audio_unbind),
285};
286
287static int __init init(void)
288{
289 return usb_composite_register(&audio_driver);
290}
291module_init(init);
292
293static void __exit cleanup(void)
294{
295 usb_composite_unregister(&audio_driver);
296}
297module_exit(cleanup);
298
299MODULE_DESCRIPTION(DRIVER_DESC);
300MODULE_AUTHOR("Bryan Wu <cooloney@kernel.org>");
301MODULE_LICENSE("GPL");
302
diff --git a/drivers/usb/gadget/ci13xxx_udc.c b/drivers/usb/gadget/ci13xxx_udc.c
index 38e531ecae4d..c7cb87a6fee2 100644
--- a/drivers/usb/gadget/ci13xxx_udc.c
+++ b/drivers/usb/gadget/ci13xxx_udc.c
@@ -1977,9 +1977,9 @@ static int ep_enable(struct usb_ep *ep,
1977 if (!list_empty(&mEp->qh[mEp->dir].queue)) 1977 if (!list_empty(&mEp->qh[mEp->dir].queue))
1978 warn("enabling a non-empty endpoint!"); 1978 warn("enabling a non-empty endpoint!");
1979 1979
1980 mEp->dir = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? TX : RX; 1980 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1981 mEp->num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1981 mEp->num = usb_endpoint_num(desc);
1982 mEp->type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 1982 mEp->type = usb_endpoint_type(desc);
1983 1983
1984 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize); 1984 mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize);
1985 1985
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index d006dc652e02..bd102f5052ba 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -293,15 +293,16 @@ static int __init eth_bind(struct usb_composite_dev *cdev)
293 /* CDC Subset */ 293 /* CDC Subset */
294 eth_config_driver.label = "CDC Subset/SAFE"; 294 eth_config_driver.label = "CDC Subset/SAFE";
295 295
296 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM), 296 device_desc.idVendor = cpu_to_le16(SIMPLE_VENDOR_NUM);
297 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM), 297 device_desc.idProduct = cpu_to_le16(SIMPLE_PRODUCT_NUM);
298 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; 298 if (!has_rndis())
299 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
299 } 300 }
300 301
301 if (has_rndis()) { 302 if (has_rndis()) {
302 /* RNDIS plus ECM-or-Subset */ 303 /* RNDIS plus ECM-or-Subset */
303 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM), 304 device_desc.idVendor = cpu_to_le16(RNDIS_VENDOR_NUM);
304 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM), 305 device_desc.idProduct = cpu_to_le16(RNDIS_PRODUCT_NUM);
305 device_desc.bNumConfigurations = 2; 306 device_desc.bNumConfigurations = 2;
306 } 307 }
307 308
diff --git a/drivers/usb/gadget/f_audio.c b/drivers/usb/gadget/f_audio.c
new file mode 100644
index 000000000000..66527ba2d2ea
--- /dev/null
+++ b/drivers/usb/gadget/f_audio.c
@@ -0,0 +1,707 @@
1/*
2 * f_audio.c -- USB Audio class function driver
3 *
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <asm/atomic.h>
15
16#include "u_audio.h"
17
18#define OUT_EP_MAX_PACKET_SIZE 200
19static int req_buf_size = OUT_EP_MAX_PACKET_SIZE;
20module_param(req_buf_size, int, S_IRUGO);
21MODULE_PARM_DESC(req_buf_size, "ISO OUT endpoint request buffer size");
22
23static int req_count = 256;
24module_param(req_count, int, S_IRUGO);
25MODULE_PARM_DESC(req_count, "ISO OUT endpoint request count");
26
27static int audio_buf_size = 48000;
28module_param(audio_buf_size, int, S_IRUGO);
29MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
30
31/*
32 * DESCRIPTORS ... most are static, but strings and full
33 * configuration descriptors are built on demand.
34 */
35
36/*
37 * We have two interfaces- AudioControl and AudioStreaming
38 * TODO: only supcard playback currently
39 */
40#define F_AUDIO_AC_INTERFACE 0
41#define F_AUDIO_AS_INTERFACE 1
42#define F_AUDIO_NUM_INTERFACES 2
43
44/* B.3.1 Standard AC Interface Descriptor */
45static struct usb_interface_descriptor ac_interface_desc __initdata = {
46 .bLength = USB_DT_INTERFACE_SIZE,
47 .bDescriptorType = USB_DT_INTERFACE,
48 .bNumEndpoints = 0,
49 .bInterfaceClass = USB_CLASS_AUDIO,
50 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
51};
52
53DECLARE_USB_AC_HEADER_DESCRIPTOR(2);
54
55#define USB_DT_AC_HEADER_LENGH USB_DT_AC_HEADER_SIZE(F_AUDIO_NUM_INTERFACES)
56/* B.3.2 Class-Specific AC Interface Descriptor */
57static struct usb_ac_header_descriptor_2 ac_header_desc = {
58 .bLength = USB_DT_AC_HEADER_LENGH,
59 .bDescriptorType = USB_DT_CS_INTERFACE,
60 .bDescriptorSubtype = HEADER,
61 .bcdADC = __constant_cpu_to_le16(0x0100),
62 .wTotalLength = __constant_cpu_to_le16(USB_DT_AC_HEADER_LENGH),
63 .bInCollection = F_AUDIO_NUM_INTERFACES,
64 .baInterfaceNr = {
65 [0] = F_AUDIO_AC_INTERFACE,
66 [1] = F_AUDIO_AS_INTERFACE,
67 }
68};
69
70#define INPUT_TERMINAL_ID 1
71static struct usb_input_terminal_descriptor input_terminal_desc = {
72 .bLength = USB_DT_AC_INPUT_TERMINAL_SIZE,
73 .bDescriptorType = USB_DT_CS_INTERFACE,
74 .bDescriptorSubtype = INPUT_TERMINAL,
75 .bTerminalID = INPUT_TERMINAL_ID,
76 .wTerminalType = USB_AC_TERMINAL_STREAMING,
77 .bAssocTerminal = 0,
78 .wChannelConfig = 0x3,
79};
80
81DECLARE_USB_AC_FEATURE_UNIT_DESCRIPTOR(0);
82
83#define FEATURE_UNIT_ID 2
84static struct usb_ac_feature_unit_descriptor_0 feature_unit_desc = {
85 .bLength = USB_DT_AC_FEATURE_UNIT_SIZE(0),
86 .bDescriptorType = USB_DT_CS_INTERFACE,
87 .bDescriptorSubtype = FEATURE_UNIT,
88 .bUnitID = FEATURE_UNIT_ID,
89 .bSourceID = INPUT_TERMINAL_ID,
90 .bControlSize = 2,
91 .bmaControls[0] = (FU_MUTE | FU_VOLUME),
92};
93
94static struct usb_audio_control mute_control = {
95 .list = LIST_HEAD_INIT(mute_control.list),
96 .name = "Mute Control",
97 .type = MUTE_CONTROL,
98 /* Todo: add real Mute control code */
99 .set = generic_set_cmd,
100 .get = generic_get_cmd,
101};
102
103static struct usb_audio_control volume_control = {
104 .list = LIST_HEAD_INIT(volume_control.list),
105 .name = "Volume Control",
106 .type = VOLUME_CONTROL,
107 /* Todo: add real Volume control code */
108 .set = generic_set_cmd,
109 .get = generic_get_cmd,
110};
111
112static struct usb_audio_control_selector feature_unit = {
113 .list = LIST_HEAD_INIT(feature_unit.list),
114 .id = FEATURE_UNIT_ID,
115 .name = "Mute & Volume Control",
116 .type = FEATURE_UNIT,
117 .desc = (struct usb_descriptor_header *)&feature_unit_desc,
118};
119
120#define OUTPUT_TERMINAL_ID 3
121static struct usb_output_terminal_descriptor output_terminal_desc = {
122 .bLength = USB_DT_AC_OUTPUT_TERMINAL_SIZE,
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubtype = OUTPUT_TERMINAL,
125 .bTerminalID = OUTPUT_TERMINAL_ID,
126 .wTerminalType = USB_AC_OUTPUT_TERMINAL_SPEAKER,
127 .bAssocTerminal = FEATURE_UNIT_ID,
128 .bSourceID = FEATURE_UNIT_ID,
129};
130
131/* B.4.1 Standard AS Interface Descriptor */
132static struct usb_interface_descriptor as_interface_alt_0_desc = {
133 .bLength = USB_DT_INTERFACE_SIZE,
134 .bDescriptorType = USB_DT_INTERFACE,
135 .bAlternateSetting = 0,
136 .bNumEndpoints = 0,
137 .bInterfaceClass = USB_CLASS_AUDIO,
138 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
139};
140
141static struct usb_interface_descriptor as_interface_alt_1_desc = {
142 .bLength = USB_DT_INTERFACE_SIZE,
143 .bDescriptorType = USB_DT_INTERFACE,
144 .bAlternateSetting = 1,
145 .bNumEndpoints = 1,
146 .bInterfaceClass = USB_CLASS_AUDIO,
147 .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING,
148};
149
150/* B.4.2 Class-Specific AS Interface Descriptor */
151static struct usb_as_header_descriptor as_header_desc = {
152 .bLength = USB_DT_AS_HEADER_SIZE,
153 .bDescriptorType = USB_DT_CS_INTERFACE,
154 .bDescriptorSubtype = AS_GENERAL,
155 .bTerminalLink = INPUT_TERMINAL_ID,
156 .bDelay = 1,
157 .wFormatTag = USB_AS_AUDIO_FORMAT_TYPE_I_PCM,
158};
159
160DECLARE_USB_AS_FORMAT_TYPE_I_DISCRETE_DESC(1);
161
162static struct usb_as_formate_type_i_discrete_descriptor_1 as_type_i_desc = {
163 .bLength = USB_AS_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
164 .bDescriptorType = USB_DT_CS_INTERFACE,
165 .bDescriptorSubtype = FORMAT_TYPE,
166 .bFormatType = USB_AS_FORMAT_TYPE_I,
167 .bSubframeSize = 2,
168 .bBitResolution = 16,
169 .bSamFreqType = 1,
170};
171
172/* Standard ISO OUT Endpoint Descriptor */
173static struct usb_endpoint_descriptor as_out_ep_desc __initdata = {
174 .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_OUT,
177 .bmAttributes = USB_AS_ENDPOINT_ADAPTIVE
178 | USB_ENDPOINT_XFER_ISOC,
179 .wMaxPacketSize = __constant_cpu_to_le16(OUT_EP_MAX_PACKET_SIZE),
180 .bInterval = 4,
181};
182
183/* Class-specific AS ISO OUT Endpoint Descriptor */
184static struct usb_as_iso_endpoint_descriptor as_iso_out_desc __initdata = {
185 .bLength = USB_AS_ISO_ENDPOINT_DESC_SIZE,
186 .bDescriptorType = USB_DT_CS_ENDPOINT,
187 .bDescriptorSubtype = EP_GENERAL,
188 .bmAttributes = 1,
189 .bLockDelayUnits = 1,
190 .wLockDelay = __constant_cpu_to_le16(1),
191};
192
193static struct usb_descriptor_header *f_audio_desc[] __initdata = {
194 (struct usb_descriptor_header *)&ac_interface_desc,
195 (struct usb_descriptor_header *)&ac_header_desc,
196
197 (struct usb_descriptor_header *)&input_terminal_desc,
198 (struct usb_descriptor_header *)&output_terminal_desc,
199 (struct usb_descriptor_header *)&feature_unit_desc,
200
201 (struct usb_descriptor_header *)&as_interface_alt_0_desc,
202 (struct usb_descriptor_header *)&as_interface_alt_1_desc,
203 (struct usb_descriptor_header *)&as_header_desc,
204
205 (struct usb_descriptor_header *)&as_type_i_desc,
206
207 (struct usb_descriptor_header *)&as_out_ep_desc,
208 (struct usb_descriptor_header *)&as_iso_out_desc,
209 NULL,
210};
211
212/* string IDs are assigned dynamically */
213
214#define STRING_MANUFACTURER_IDX 0
215#define STRING_PRODUCT_IDX 1
216
217static char manufacturer[50];
218
219static struct usb_string strings_dev[] = {
220 [STRING_MANUFACTURER_IDX].s = manufacturer,
221 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
222 { } /* end of list */
223};
224
225static struct usb_gadget_strings stringtab_dev = {
226 .language = 0x0409, /* en-us */
227 .strings = strings_dev,
228};
229
230static struct usb_gadget_strings *audio_strings[] = {
231 &stringtab_dev,
232 NULL,
233};
234
235/*
236 * This function is an ALSA sound card following USB Audio Class Spec 1.0.
237 */
238
239/*-------------------------------------------------------------------------*/
240struct f_audio_buf {
241 u8 *buf;
242 int actual;
243 struct list_head list;
244};
245
246static struct f_audio_buf *f_audio_buffer_alloc(int buf_size)
247{
248 struct f_audio_buf *copy_buf;
249
250 copy_buf = kzalloc(sizeof *copy_buf, GFP_ATOMIC);
251 if (!copy_buf)
252 return (struct f_audio_buf *)-ENOMEM;
253
254 copy_buf->buf = kzalloc(buf_size, GFP_ATOMIC);
255 if (!copy_buf->buf) {
256 kfree(copy_buf);
257 return (struct f_audio_buf *)-ENOMEM;
258 }
259
260 return copy_buf;
261}
262
263static void f_audio_buffer_free(struct f_audio_buf *audio_buf)
264{
265 kfree(audio_buf->buf);
266 kfree(audio_buf);
267}
268/*-------------------------------------------------------------------------*/
269
270struct f_audio {
271 struct gaudio card;
272
273 /* endpoints handle full and/or high speeds */
274 struct usb_ep *out_ep;
275 struct usb_endpoint_descriptor *out_desc;
276
277 spinlock_t lock;
278 struct f_audio_buf *copy_buf;
279 struct work_struct playback_work;
280 struct list_head play_queue;
281
282 /* Control Set command */
283 struct list_head cs;
284 u8 set_cmd;
285 struct usb_audio_control *set_con;
286};
287
288static inline struct f_audio *func_to_audio(struct usb_function *f)
289{
290 return container_of(f, struct f_audio, card.func);
291}
292
293/*-------------------------------------------------------------------------*/
294
295static void f_audio_playback_work(struct work_struct *data)
296{
297 struct f_audio *audio = container_of(data, struct f_audio,
298 playback_work);
299 struct f_audio_buf *play_buf;
300
301 spin_lock_irq(&audio->lock);
302 if (list_empty(&audio->play_queue)) {
303 spin_unlock_irq(&audio->lock);
304 return;
305 }
306 play_buf = list_first_entry(&audio->play_queue,
307 struct f_audio_buf, list);
308 list_del(&play_buf->list);
309 spin_unlock_irq(&audio->lock);
310
311 u_audio_playback(&audio->card, play_buf->buf, play_buf->actual);
312 f_audio_buffer_free(play_buf);
313
314 return;
315}
316
317static int f_audio_out_ep_complete(struct usb_ep *ep, struct usb_request *req)
318{
319 struct f_audio *audio = req->context;
320 struct usb_composite_dev *cdev = audio->card.func.config->cdev;
321 struct f_audio_buf *copy_buf = audio->copy_buf;
322 int err;
323
324 if (!copy_buf)
325 return -EINVAL;
326
327 /* Copy buffer is full, add it to the play_queue */
328 if (audio_buf_size - copy_buf->actual < req->actual) {
329 list_add_tail(&copy_buf->list, &audio->play_queue);
330 schedule_work(&audio->playback_work);
331 copy_buf = f_audio_buffer_alloc(audio_buf_size);
332 if (copy_buf < 0)
333 return -ENOMEM;
334 }
335
336 memcpy(copy_buf->buf + copy_buf->actual, req->buf, req->actual);
337 copy_buf->actual += req->actual;
338 audio->copy_buf = copy_buf;
339
340 err = usb_ep_queue(ep, req, GFP_ATOMIC);
341 if (err)
342 ERROR(cdev, "%s queue req: %d\n", ep->name, err);
343
344 return 0;
345
346}
347
348static void f_audio_complete(struct usb_ep *ep, struct usb_request *req)
349{
350 struct f_audio *audio = req->context;
351 int status = req->status;
352 u32 data = 0;
353 struct usb_ep *out_ep = audio->out_ep;
354
355 switch (status) {
356
357 case 0: /* normal completion? */
358 if (ep == out_ep)
359 f_audio_out_ep_complete(ep, req);
360 else if (audio->set_con) {
361 memcpy(&data, req->buf, req->length);
362 audio->set_con->set(audio->set_con, audio->set_cmd,
363 le16_to_cpu(data));
364 audio->set_con = NULL;
365 }
366 break;
367 default:
368 break;
369 }
370}
371
372static int audio_set_intf_req(struct usb_function *f,
373 const struct usb_ctrlrequest *ctrl)
374{
375 struct f_audio *audio = func_to_audio(f);
376 struct usb_composite_dev *cdev = f->config->cdev;
377 struct usb_request *req = cdev->req;
378 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
379 u16 len = le16_to_cpu(ctrl->wLength);
380 u16 w_value = le16_to_cpu(ctrl->wValue);
381 u8 con_sel = (w_value >> 8) & 0xFF;
382 u8 cmd = (ctrl->bRequest & 0x0F);
383 struct usb_audio_control_selector *cs;
384 struct usb_audio_control *con;
385
386 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
387 ctrl->bRequest, w_value, len, id);
388
389 list_for_each_entry(cs, &audio->cs, list) {
390 if (cs->id == id) {
391 list_for_each_entry(con, &cs->control, list) {
392 if (con->type == con_sel) {
393 audio->set_con = con;
394 break;
395 }
396 }
397 break;
398 }
399 }
400
401 audio->set_cmd = cmd;
402 req->context = audio;
403 req->complete = f_audio_complete;
404
405 return len;
406}
407
408static int audio_get_intf_req(struct usb_function *f,
409 const struct usb_ctrlrequest *ctrl)
410{
411 struct f_audio *audio = func_to_audio(f);
412 struct usb_composite_dev *cdev = f->config->cdev;
413 struct usb_request *req = cdev->req;
414 int value = -EOPNOTSUPP;
415 u8 id = ((le16_to_cpu(ctrl->wIndex) >> 8) & 0xFF);
416 u16 len = le16_to_cpu(ctrl->wLength);
417 u16 w_value = le16_to_cpu(ctrl->wValue);
418 u8 con_sel = (w_value >> 8) & 0xFF;
419 u8 cmd = (ctrl->bRequest & 0x0F);
420 struct usb_audio_control_selector *cs;
421 struct usb_audio_control *con;
422
423 DBG(cdev, "bRequest 0x%x, w_value 0x%04x, len %d, entity %d\n",
424 ctrl->bRequest, w_value, len, id);
425
426 list_for_each_entry(cs, &audio->cs, list) {
427 if (cs->id == id) {
428 list_for_each_entry(con, &cs->control, list) {
429 if (con->type == con_sel && con->get) {
430 value = con->get(con, cmd);
431 break;
432 }
433 }
434 break;
435 }
436 }
437
438 req->context = audio;
439 req->complete = f_audio_complete;
440 memcpy(req->buf, &value, len);
441
442 return len;
443}
444
445static int
446f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
447{
448 struct usb_composite_dev *cdev = f->config->cdev;
449 struct usb_request *req = cdev->req;
450 int value = -EOPNOTSUPP;
451 u16 w_index = le16_to_cpu(ctrl->wIndex);
452 u16 w_value = le16_to_cpu(ctrl->wValue);
453 u16 w_length = le16_to_cpu(ctrl->wLength);
454
455 /* composite driver infrastructure handles everything except
456 * Audio class messages; interface activation uses set_alt().
457 */
458 switch (ctrl->bRequestType) {
459 case USB_AUDIO_SET_INTF:
460 value = audio_set_intf_req(f, ctrl);
461 break;
462
463 case USB_AUDIO_GET_INTF:
464 value = audio_get_intf_req(f, ctrl);
465 break;
466
467 default:
468 ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
469 ctrl->bRequestType, ctrl->bRequest,
470 w_value, w_index, w_length);
471 }
472
473 /* respond with data transfer or status phase? */
474 if (value >= 0) {
475 DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",
476 ctrl->bRequestType, ctrl->bRequest,
477 w_value, w_index, w_length);
478 req->zero = 0;
479 req->length = value;
480 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
481 if (value < 0)
482 ERROR(cdev, "audio response on err %d\n", value);
483 }
484
485 /* device either stalls (value < 0) or reports success */
486 return value;
487}
488
489static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
490{
491 struct f_audio *audio = func_to_audio(f);
492 struct usb_composite_dev *cdev = f->config->cdev;
493 struct usb_ep *out_ep = audio->out_ep;
494 struct usb_request *req;
495 int i = 0, err = 0;
496
497 DBG(cdev, "intf %d, alt %d\n", intf, alt);
498
499 if (intf == 1) {
500 if (alt == 1) {
501 usb_ep_enable(out_ep, audio->out_desc);
502 out_ep->driver_data = audio;
503 audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);
504
505 /*
506 * allocate a bunch of read buffers
507 * and queue them all at once.
508 */
509 for (i = 0; i < req_count && err == 0; i++) {
510 req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);
511 if (req) {
512 req->buf = kzalloc(req_buf_size,
513 GFP_ATOMIC);
514 if (req->buf) {
515 req->length = req_buf_size;
516 req->context = audio;
517 req->complete =
518 f_audio_complete;
519 err = usb_ep_queue(out_ep,
520 req, GFP_ATOMIC);
521 if (err)
522 ERROR(cdev,
523 "%s queue req: %d\n",
524 out_ep->name, err);
525 } else
526 err = -ENOMEM;
527 } else
528 err = -ENOMEM;
529 }
530
531 } else {
532 struct f_audio_buf *copy_buf = audio->copy_buf;
533 if (copy_buf) {
534 list_add_tail(&copy_buf->list,
535 &audio->play_queue);
536 schedule_work(&audio->playback_work);
537 }
538 }
539 }
540
541 return err;
542}
543
544static void f_audio_disable(struct usb_function *f)
545{
546 return;
547}
548
549/*-------------------------------------------------------------------------*/
550
551static void f_audio_build_desc(struct f_audio *audio)
552{
553 struct gaudio *card = &audio->card;
554 u8 *sam_freq;
555 int rate;
556
557 /* Set channel numbers */
558 input_terminal_desc.bNrChannels = u_audio_get_playback_channels(card);
559 as_type_i_desc.bNrChannels = u_audio_get_playback_channels(card);
560
561 /* Set sample rates */
562 rate = u_audio_get_playback_rate(card);
563 sam_freq = as_type_i_desc.tSamFreq[0];
564 memcpy(sam_freq, &rate, 3);
565
566 /* Todo: Set Sample bits and other parameters */
567
568 return;
569}
570
571/* audio function driver setup/binding */
572static int __init
573f_audio_bind(struct usb_configuration *c, struct usb_function *f)
574{
575 struct usb_composite_dev *cdev = c->cdev;
576 struct f_audio *audio = func_to_audio(f);
577 int status;
578 struct usb_ep *ep;
579
580 f_audio_build_desc(audio);
581
582 /* allocate instance-specific interface IDs, and patch descriptors */
583 status = usb_interface_id(c, f);
584 if (status < 0)
585 goto fail;
586 ac_interface_desc.bInterfaceNumber = status;
587
588 status = usb_interface_id(c, f);
589 if (status < 0)
590 goto fail;
591 as_interface_alt_0_desc.bInterfaceNumber = status;
592 as_interface_alt_1_desc.bInterfaceNumber = status;
593
594 status = -ENODEV;
595
596 /* allocate instance-specific endpoints */
597 ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
598 if (!ep)
599 goto fail;
600 audio->out_ep = ep;
601 ep->driver_data = cdev; /* claim */
602
603 status = -ENOMEM;
604
605 /* supcard all relevant hardware speeds... we expect that when
606 * hardware is dual speed, all bulk-capable endpoints work at
607 * both speeds
608 */
609
610 /* copy descriptors, and track endpoint copies */
611 if (gadget_is_dualspeed(c->cdev->gadget)) {
612 c->highspeed = true;
613 f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
614 } else
615 f->descriptors = usb_copy_descriptors(f_audio_desc);
616
617 return 0;
618
619fail:
620
621 return status;
622}
623
624static void
625f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
626{
627 struct f_audio *audio = func_to_audio(f);
628
629 usb_free_descriptors(f->descriptors);
630 kfree(audio);
631}
632
633/*-------------------------------------------------------------------------*/
634
635/* Todo: add more control selecotor dynamically */
636int __init control_selector_init(struct f_audio *audio)
637{
638 INIT_LIST_HEAD(&audio->cs);
639 list_add(&feature_unit.list, &audio->cs);
640
641 INIT_LIST_HEAD(&feature_unit.control);
642 list_add(&mute_control.list, &feature_unit.control);
643 list_add(&volume_control.list, &feature_unit.control);
644
645 volume_control.data[_CUR] = 0xffc0;
646 volume_control.data[_MIN] = 0xe3a0;
647 volume_control.data[_MAX] = 0xfff0;
648 volume_control.data[_RES] = 0x0030;
649
650 return 0;
651}
652
653/**
654 * audio_bind_config - add USB audio fucntion to a configuration
655 * @c: the configuration to supcard the USB audio function
656 * Context: single threaded during gadget setup
657 *
658 * Returns zero on success, else negative errno.
659 */
660int __init audio_bind_config(struct usb_configuration *c)
661{
662 struct f_audio *audio;
663 int status;
664
665 /* allocate and initialize one new instance */
666 audio = kzalloc(sizeof *audio, GFP_KERNEL);
667 if (!audio)
668 return -ENOMEM;
669
670 audio->card.func.name = "g_audio";
671 audio->card.gadget = c->cdev->gadget;
672
673 INIT_LIST_HEAD(&audio->play_queue);
674 spin_lock_init(&audio->lock);
675
676 /* set up ASLA audio devices */
677 status = gaudio_setup(&audio->card);
678 if (status < 0)
679 goto setup_fail;
680
681 audio->card.func.strings = audio_strings;
682 audio->card.func.bind = f_audio_bind;
683 audio->card.func.unbind = f_audio_unbind;
684 audio->card.func.set_alt = f_audio_set_alt;
685 audio->card.func.setup = f_audio_setup;
686 audio->card.func.disable = f_audio_disable;
687 audio->out_desc = &as_out_ep_desc;
688
689 control_selector_init(audio);
690
691 INIT_WORK(&audio->playback_work, f_audio_playback_work);
692
693 status = usb_add_function(c, &audio->card.func);
694 if (status)
695 goto add_fail;
696
697 INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
698 audio_buf_size, req_buf_size, req_count);
699
700 return status;
701
702add_fail:
703 gaudio_cleanup(&audio->card);
704setup_fail:
705 kfree(audio);
706 return status;
707}
diff --git a/drivers/usb/gadget/f_phonet.c b/drivers/usb/gadget/f_phonet.c
index c1abeb89b413..96fb118355b0 100644
--- a/drivers/usb/gadget/f_phonet.c
+++ b/drivers/usb/gadget/f_phonet.c
@@ -188,8 +188,7 @@ static struct usb_descriptor_header *hs_pn_function[] = {
188 188
189static int pn_net_open(struct net_device *dev) 189static int pn_net_open(struct net_device *dev)
190{ 190{
191 if (netif_carrier_ok(dev)) 191 netif_wake_queue(dev);
192 netif_wake_queue(dev);
193 return 0; 192 return 0;
194} 193}
195 194
@@ -219,8 +218,7 @@ static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
219 } 218 }
220 219
221 dev_kfree_skb_any(skb); 220 dev_kfree_skb_any(skb);
222 if (netif_carrier_ok(dev)) 221 netif_wake_queue(dev);
223 netif_wake_queue(dev);
224} 222}
225 223
226static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev) 224static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -255,7 +253,7 @@ out_unlock:
255 spin_unlock_irqrestore(&port->lock, flags); 253 spin_unlock_irqrestore(&port->lock, flags);
256out: 254out:
257 if (unlikely(skb)) { 255 if (unlikely(skb)) {
258 dev_kfree_skb_any(skb); 256 dev_kfree_skb(skb);
259 dev->stats.tx_dropped++; 257 dev->stats.tx_dropped++;
260 } 258 }
261 return 0; 259 return 0;
@@ -383,7 +381,6 @@ static void __pn_reset(struct usb_function *f)
383 struct phonet_port *port = netdev_priv(dev); 381 struct phonet_port *port = netdev_priv(dev);
384 382
385 netif_carrier_off(dev); 383 netif_carrier_off(dev);
386 netif_stop_queue(dev);
387 port->usb = NULL; 384 port->usb = NULL;
388 385
389 usb_ep_disable(fp->out_ep); 386 usb_ep_disable(fp->out_ep);
@@ -427,8 +424,6 @@ static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
427 fp->in_ep->driver_data = fp; 424 fp->in_ep->driver_data = fp;
428 425
429 netif_carrier_on(dev); 426 netif_carrier_on(dev);
430 if (netif_running(dev))
431 netif_wake_queue(dev);
432 for (i = 0; i < phonet_rxq_size; i++) 427 for (i = 0; i < phonet_rxq_size; i++)
433 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC); 428 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
434 } 429 }
@@ -574,9 +569,10 @@ static struct net_device *dev;
574int __init phonet_bind_config(struct usb_configuration *c) 569int __init phonet_bind_config(struct usb_configuration *c)
575{ 570{
576 struct f_phonet *fp; 571 struct f_phonet *fp;
577 int err; 572 int err, size;
578 573
579 fp = kzalloc(sizeof(*fp), GFP_KERNEL); 574 size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *));
575 fp = kzalloc(size, GFP_KERNEL);
580 if (!fp) 576 if (!fp)
581 return -ENOMEM; 577 return -ENOMEM;
582 578
@@ -601,16 +597,13 @@ int __init gphonet_setup(struct usb_gadget *gadget)
601 597
602 /* Create net device */ 598 /* Create net device */
603 BUG_ON(dev); 599 BUG_ON(dev);
604 dev = alloc_netdev(sizeof(*port) 600 dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup);
605 + (phonet_rxq_size * sizeof(struct usb_request *)),
606 "upnlink%d", pn_net_setup);
607 if (!dev) 601 if (!dev)
608 return -ENOMEM; 602 return -ENOMEM;
609 603
610 port = netdev_priv(dev); 604 port = netdev_priv(dev);
611 spin_lock_init(&port->lock); 605 spin_lock_init(&port->lock);
612 netif_carrier_off(dev); 606 netif_carrier_off(dev);
613 netif_stop_queue(dev);
614 SET_NETDEV_DEV(dev, &gadget->dev); 607 SET_NETDEV_DEV(dev, &gadget->dev);
615 608
616 err = register_netdev(dev); 609 err = register_netdev(dev);
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index 3279a4726042..424a37c5773f 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -475,7 +475,9 @@ static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
475 if (rndis->port.in_ep->driver_data) { 475 if (rndis->port.in_ep->driver_data) {
476 DBG(cdev, "reset rndis\n"); 476 DBG(cdev, "reset rndis\n");
477 gether_disconnect(&rndis->port); 477 gether_disconnect(&rndis->port);
478 } else { 478 }
479
480 if (!rndis->port.in) {
479 DBG(cdev, "init rndis\n"); 481 DBG(cdev, "init rndis\n");
480 rndis->port.in = ep_choose(cdev->gadget, 482 rndis->port.in = ep_choose(cdev->gadget,
481 rndis->hs.in, rndis->fs.in); 483 rndis->hs.in, rndis->fs.in);
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 381a53b3e11c..1e6aa504d58a 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -248,6 +248,8 @@
248#include <linux/freezer.h> 248#include <linux/freezer.h>
249#include <linux/utsname.h> 249#include <linux/utsname.h>
250 250
251#include <asm/unaligned.h>
252
251#include <linux/usb/ch9.h> 253#include <linux/usb/ch9.h>
252#include <linux/usb/gadget.h> 254#include <linux/usb/gadget.h>
253 255
@@ -799,29 +801,9 @@ static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
799 801
800/* Routines for unaligned data access */ 802/* Routines for unaligned data access */
801 803
802static u16 get_be16(u8 *buf) 804static u32 get_unaligned_be24(u8 *buf)
803{
804 return ((u16) buf[0] << 8) | ((u16) buf[1]);
805}
806
807static u32 get_be32(u8 *buf)
808{
809 return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) |
810 ((u32) buf[2] << 8) | ((u32) buf[3]);
811}
812
813static void put_be16(u8 *buf, u16 val)
814{
815 buf[0] = val >> 8;
816 buf[1] = val;
817}
818
819static void put_be32(u8 *buf, u32 val)
820{ 805{
821 buf[0] = val >> 24; 806 return 0xffffff & (u32) get_unaligned_be32(buf - 1);
822 buf[1] = val >> 16;
823 buf[2] = val >> 8;
824 buf[3] = val & 0xff;
825} 807}
826 808
827 809
@@ -1582,9 +1564,9 @@ static int do_read(struct fsg_dev *fsg)
1582 /* Get the starting Logical Block Address and check that it's 1564 /* Get the starting Logical Block Address and check that it's
1583 * not too big */ 1565 * not too big */
1584 if (fsg->cmnd[0] == SC_READ_6) 1566 if (fsg->cmnd[0] == SC_READ_6)
1585 lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]); 1567 lba = get_unaligned_be24(&fsg->cmnd[1]);
1586 else { 1568 else {
1587 lba = get_be32(&fsg->cmnd[2]); 1569 lba = get_unaligned_be32(&fsg->cmnd[2]);
1588 1570
1589 /* We allow DPO (Disable Page Out = don't save data in the 1571 /* We allow DPO (Disable Page Out = don't save data in the
1590 * cache) and FUA (Force Unit Access = don't read from the 1572 * cache) and FUA (Force Unit Access = don't read from the
@@ -1717,9 +1699,9 @@ static int do_write(struct fsg_dev *fsg)
1717 /* Get the starting Logical Block Address and check that it's 1699 /* Get the starting Logical Block Address and check that it's
1718 * not too big */ 1700 * not too big */
1719 if (fsg->cmnd[0] == SC_WRITE_6) 1701 if (fsg->cmnd[0] == SC_WRITE_6)
1720 lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]); 1702 lba = get_unaligned_be24(&fsg->cmnd[1]);
1721 else { 1703 else {
1722 lba = get_be32(&fsg->cmnd[2]); 1704 lba = get_unaligned_be32(&fsg->cmnd[2]);
1723 1705
1724 /* We allow DPO (Disable Page Out = don't save data in the 1706 /* We allow DPO (Disable Page Out = don't save data in the
1725 * cache) and FUA (Force Unit Access = write directly to the 1707 * cache) and FUA (Force Unit Access = write directly to the
@@ -1940,7 +1922,7 @@ static int do_verify(struct fsg_dev *fsg)
1940 1922
1941 /* Get the starting Logical Block Address and check that it's 1923 /* Get the starting Logical Block Address and check that it's
1942 * not too big */ 1924 * not too big */
1943 lba = get_be32(&fsg->cmnd[2]); 1925 lba = get_unaligned_be32(&fsg->cmnd[2]);
1944 if (lba >= curlun->num_sectors) { 1926 if (lba >= curlun->num_sectors) {
1945 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1927 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1946 return -EINVAL; 1928 return -EINVAL;
@@ -1953,7 +1935,7 @@ static int do_verify(struct fsg_dev *fsg)
1953 return -EINVAL; 1935 return -EINVAL;
1954 } 1936 }
1955 1937
1956 verification_length = get_be16(&fsg->cmnd[7]); 1938 verification_length = get_unaligned_be16(&fsg->cmnd[7]);
1957 if (unlikely(verification_length == 0)) 1939 if (unlikely(verification_length == 0))
1958 return -EIO; // No default reply 1940 return -EIO; // No default reply
1959 1941
@@ -2103,7 +2085,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2103 memset(buf, 0, 18); 2085 memset(buf, 0, 18);
2104 buf[0] = valid | 0x70; // Valid, current error 2086 buf[0] = valid | 0x70; // Valid, current error
2105 buf[2] = SK(sd); 2087 buf[2] = SK(sd);
2106 put_be32(&buf[3], sdinfo); // Sense information 2088 put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
2107 buf[7] = 18 - 8; // Additional sense length 2089 buf[7] = 18 - 8; // Additional sense length
2108 buf[12] = ASC(sd); 2090 buf[12] = ASC(sd);
2109 buf[13] = ASCQ(sd); 2091 buf[13] = ASCQ(sd);
@@ -2114,7 +2096,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2114static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) 2096static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2115{ 2097{
2116 struct lun *curlun = fsg->curlun; 2098 struct lun *curlun = fsg->curlun;
2117 u32 lba = get_be32(&fsg->cmnd[2]); 2099 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2118 int pmi = fsg->cmnd[8]; 2100 int pmi = fsg->cmnd[8];
2119 u8 *buf = (u8 *) bh->buf; 2101 u8 *buf = (u8 *) bh->buf;
2120 2102
@@ -2124,8 +2106,9 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2124 return -EINVAL; 2106 return -EINVAL;
2125 } 2107 }
2126 2108
2127 put_be32(&buf[0], curlun->num_sectors - 1); // Max logical block 2109 put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
2128 put_be32(&buf[4], 512); // Block length 2110 /* Max logical block */
2111 put_unaligned_be32(512, &buf[4]); /* Block length */
2129 return 8; 2112 return 8;
2130} 2113}
2131 2114
@@ -2144,7 +2127,7 @@ static void store_cdrom_address(u8 *dest, int msf, u32 addr)
2144 dest[0] = 0; /* Reserved */ 2127 dest[0] = 0; /* Reserved */
2145 } else { 2128 } else {
2146 /* Absolute sector */ 2129 /* Absolute sector */
2147 put_be32(dest, addr); 2130 put_unaligned_be32(addr, dest);
2148 } 2131 }
2149} 2132}
2150 2133
@@ -2152,7 +2135,7 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2152{ 2135{
2153 struct lun *curlun = fsg->curlun; 2136 struct lun *curlun = fsg->curlun;
2154 int msf = fsg->cmnd[1] & 0x02; 2137 int msf = fsg->cmnd[1] & 0x02;
2155 u32 lba = get_be32(&fsg->cmnd[2]); 2138 u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
2156 u8 *buf = (u8 *) bh->buf; 2139 u8 *buf = (u8 *) bh->buf;
2157 2140
2158 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ 2141 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
@@ -2252,10 +2235,13 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2252 buf[2] = 0x04; // Write cache enable, 2235 buf[2] = 0x04; // Write cache enable,
2253 // Read cache not disabled 2236 // Read cache not disabled
2254 // No cache retention priorities 2237 // No cache retention priorities
2255 put_be16(&buf[4], 0xffff); // Don't disable prefetch 2238 put_unaligned_be16(0xffff, &buf[4]);
2256 // Minimum prefetch = 0 2239 /* Don't disable prefetch */
2257 put_be16(&buf[8], 0xffff); // Maximum prefetch 2240 /* Minimum prefetch = 0 */
2258 put_be16(&buf[10], 0xffff); // Maximum prefetch ceiling 2241 put_unaligned_be16(0xffff, &buf[8]);
2242 /* Maximum prefetch */
2243 put_unaligned_be16(0xffff, &buf[10]);
2244 /* Maximum prefetch ceiling */
2259 } 2245 }
2260 buf += 12; 2246 buf += 12;
2261 } 2247 }
@@ -2272,7 +2258,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2272 if (mscmnd == SC_MODE_SENSE_6) 2258 if (mscmnd == SC_MODE_SENSE_6)
2273 buf0[0] = len - 1; 2259 buf0[0] = len - 1;
2274 else 2260 else
2275 put_be16(buf0, len - 2); 2261 put_unaligned_be16(len - 2, buf0);
2276 return len; 2262 return len;
2277} 2263}
2278 2264
@@ -2360,9 +2346,10 @@ static int do_read_format_capacities(struct fsg_dev *fsg,
2360 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor 2346 buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
2361 buf += 4; 2347 buf += 4;
2362 2348
2363 put_be32(&buf[0], curlun->num_sectors); // Number of blocks 2349 put_unaligned_be32(curlun->num_sectors, &buf[0]);
2364 put_be32(&buf[4], 512); // Block length 2350 /* Number of blocks */
2365 buf[4] = 0x02; // Current capacity 2351 put_unaligned_be32(512, &buf[4]); /* Block length */
2352 buf[4] = 0x02; /* Current capacity */
2366 return 12; 2353 return 12;
2367} 2354}
2368 2355
@@ -2882,7 +2869,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2882 break; 2869 break;
2883 2870
2884 case SC_MODE_SELECT_10: 2871 case SC_MODE_SELECT_10:
2885 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]); 2872 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2886 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2873 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2887 (1<<1) | (3<<7), 0, 2874 (1<<1) | (3<<7), 0,
2888 "MODE SELECT(10)")) == 0) 2875 "MODE SELECT(10)")) == 0)
@@ -2898,7 +2885,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2898 break; 2885 break;
2899 2886
2900 case SC_MODE_SENSE_10: 2887 case SC_MODE_SENSE_10:
2901 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]); 2888 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2902 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2889 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2903 (1<<1) | (1<<2) | (3<<7), 0, 2890 (1<<1) | (1<<2) | (3<<7), 0,
2904 "MODE SENSE(10)")) == 0) 2891 "MODE SENSE(10)")) == 0)
@@ -2923,7 +2910,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
2923 break; 2910 break;
2924 2911
2925 case SC_READ_10: 2912 case SC_READ_10:
2926 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9; 2913 fsg->data_size_from_cmnd =
2914 get_unaligned_be16(&fsg->cmnd[7]) << 9;
2927 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2915 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2928 (1<<1) | (0xf<<2) | (3<<7), 1, 2916 (1<<1) | (0xf<<2) | (3<<7), 1,
2929 "READ(10)")) == 0) 2917 "READ(10)")) == 0)
@@ -2931,7 +2919,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
2931 break; 2919 break;
2932 2920
2933 case SC_READ_12: 2921 case SC_READ_12:
2934 fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9; 2922 fsg->data_size_from_cmnd =
2923 get_unaligned_be32(&fsg->cmnd[6]) << 9;
2935 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 2924 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2936 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2925 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2937 "READ(12)")) == 0) 2926 "READ(12)")) == 0)
@@ -2949,7 +2938,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2949 case SC_READ_HEADER: 2938 case SC_READ_HEADER:
2950 if (!mod_data.cdrom) 2939 if (!mod_data.cdrom)
2951 goto unknown_cmnd; 2940 goto unknown_cmnd;
2952 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]); 2941 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2953 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2942 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2954 (3<<7) | (0x1f<<1), 1, 2943 (3<<7) | (0x1f<<1), 1,
2955 "READ HEADER")) == 0) 2944 "READ HEADER")) == 0)
@@ -2959,7 +2948,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2959 case SC_READ_TOC: 2948 case SC_READ_TOC:
2960 if (!mod_data.cdrom) 2949 if (!mod_data.cdrom)
2961 goto unknown_cmnd; 2950 goto unknown_cmnd;
2962 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]); 2951 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2963 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2952 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2964 (7<<6) | (1<<1), 1, 2953 (7<<6) | (1<<1), 1,
2965 "READ TOC")) == 0) 2954 "READ TOC")) == 0)
@@ -2967,7 +2956,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2967 break; 2956 break;
2968 2957
2969 case SC_READ_FORMAT_CAPACITIES: 2958 case SC_READ_FORMAT_CAPACITIES:
2970 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]); 2959 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2971 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2960 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2972 (3<<7), 1, 2961 (3<<7), 1,
2973 "READ FORMAT CAPACITIES")) == 0) 2962 "READ FORMAT CAPACITIES")) == 0)
@@ -3025,7 +3014,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
3025 break; 3014 break;
3026 3015
3027 case SC_WRITE_10: 3016 case SC_WRITE_10:
3028 fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9; 3017 fsg->data_size_from_cmnd =
3018 get_unaligned_be16(&fsg->cmnd[7]) << 9;
3029 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 3019 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
3030 (1<<1) | (0xf<<2) | (3<<7), 1, 3020 (1<<1) | (0xf<<2) | (3<<7), 1,
3031 "WRITE(10)")) == 0) 3021 "WRITE(10)")) == 0)
@@ -3033,7 +3023,8 @@ static int do_scsi_command(struct fsg_dev *fsg)
3033 break; 3023 break;
3034 3024
3035 case SC_WRITE_12: 3025 case SC_WRITE_12:
3036 fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9; 3026 fsg->data_size_from_cmnd =
3027 get_unaligned_be32(&fsg->cmnd[6]) << 9;
3037 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 3028 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
3038 (1<<1) | (0xf<<2) | (0xf<<6), 1, 3029 (1<<1) | (0xf<<2) | (0xf<<6), 1,
3039 "WRITE(12)")) == 0) 3030 "WRITE(12)")) == 0)
diff --git a/drivers/usb/gadget/fsl_mx3_udc.c b/drivers/usb/gadget/fsl_mx3_udc.c
new file mode 100644
index 000000000000..4bc2bf3d602e
--- /dev/null
+++ b/drivers/usb/gadget/fsl_mx3_udc.c
@@ -0,0 +1,95 @@
1/*
2 * Copyright (C) 2009
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
5 * Description:
6 * Helper routines for i.MX3x SoCs from Freescale, needed by the fsl_usb2_udc.c
7 * driver to function correctly on these systems.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/fsl_devices.h>
18#include <linux/platform_device.h>
19
20static struct clk *mxc_ahb_clk;
21static struct clk *mxc_usb_clk;
22
23int fsl_udc_clk_init(struct platform_device *pdev)
24{
25 struct fsl_usb2_platform_data *pdata;
26 unsigned long freq;
27 int ret;
28
29 pdata = pdev->dev.platform_data;
30
31 mxc_ahb_clk = clk_get(&pdev->dev, "usb_ahb");
32 if (IS_ERR(mxc_ahb_clk))
33 return PTR_ERR(mxc_ahb_clk);
34
35 ret = clk_enable(mxc_ahb_clk);
36 if (ret < 0) {
37 dev_err(&pdev->dev, "clk_enable(\"usb_ahb\") failed\n");
38 goto eenahb;
39 }
40
41 /* make sure USB_CLK is running at 60 MHz +/- 1000 Hz */
42 mxc_usb_clk = clk_get(&pdev->dev, "usb");
43 if (IS_ERR(mxc_usb_clk)) {
44 dev_err(&pdev->dev, "clk_get(\"usb\") failed\n");
45 ret = PTR_ERR(mxc_usb_clk);
46 goto egusb;
47 }
48
49 freq = clk_get_rate(mxc_usb_clk);
50 if (pdata->phy_mode != FSL_USB2_PHY_ULPI &&
51 (freq < 59999000 || freq > 60001000)) {
52 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq);
53 goto eclkrate;
54 }
55
56 ret = clk_enable(mxc_usb_clk);
57 if (ret < 0) {
58 dev_err(&pdev->dev, "clk_enable(\"usb_clk\") failed\n");
59 goto eenusb;
60 }
61
62 return 0;
63
64eenusb:
65eclkrate:
66 clk_put(mxc_usb_clk);
67 mxc_usb_clk = NULL;
68egusb:
69 clk_disable(mxc_ahb_clk);
70eenahb:
71 clk_put(mxc_ahb_clk);
72 return ret;
73}
74
75void fsl_udc_clk_finalize(struct platform_device *pdev)
76{
77 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
78
79 /* ULPI transceivers don't need usbpll */
80 if (pdata->phy_mode == FSL_USB2_PHY_ULPI) {
81 clk_disable(mxc_usb_clk);
82 clk_put(mxc_usb_clk);
83 mxc_usb_clk = NULL;
84 }
85}
86
87void fsl_udc_clk_release(void)
88{
89 if (mxc_usb_clk) {
90 clk_disable(mxc_usb_clk);
91 clk_put(mxc_usb_clk);
92 }
93 clk_disable(mxc_ahb_clk);
94 clk_put(mxc_ahb_clk);
95}
diff --git a/drivers/usb/gadget/fsl_usb2_udc.c b/drivers/usb/gadget/fsl_udc_core.c
index 9d7b95d4e3d2..42a74b8a0bb8 100644
--- a/drivers/usb/gadget/fsl_usb2_udc.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -38,6 +38,7 @@
38#include <linux/platform_device.h> 38#include <linux/platform_device.h>
39#include <linux/fsl_devices.h> 39#include <linux/fsl_devices.h>
40#include <linux/dmapool.h> 40#include <linux/dmapool.h>
41#include <linux/delay.h>
41 42
42#include <asm/byteorder.h> 43#include <asm/byteorder.h>
43#include <asm/io.h> 44#include <asm/io.h>
@@ -57,7 +58,9 @@ static const char driver_name[] = "fsl-usb2-udc";
57static const char driver_desc[] = DRIVER_DESC; 58static const char driver_desc[] = DRIVER_DESC;
58 59
59static struct usb_dr_device *dr_regs; 60static struct usb_dr_device *dr_regs;
61#ifndef CONFIG_ARCH_MXC
60static struct usb_sys_interface *usb_sys_regs; 62static struct usb_sys_interface *usb_sys_regs;
63#endif
61 64
62/* it is initialized in probe() */ 65/* it is initialized in probe() */
63static struct fsl_udc *udc_controller = NULL; 66static struct fsl_udc *udc_controller = NULL;
@@ -174,10 +177,34 @@ static void nuke(struct fsl_ep *ep, int status)
174 177
175static int dr_controller_setup(struct fsl_udc *udc) 178static int dr_controller_setup(struct fsl_udc *udc)
176{ 179{
177 unsigned int tmp = 0, portctrl = 0, ctrl = 0; 180 unsigned int tmp, portctrl;
181#ifndef CONFIG_ARCH_MXC
182 unsigned int ctrl;
183#endif
178 unsigned long timeout; 184 unsigned long timeout;
179#define FSL_UDC_RESET_TIMEOUT 1000 185#define FSL_UDC_RESET_TIMEOUT 1000
180 186
187 /* Config PHY interface */
188 portctrl = fsl_readl(&dr_regs->portsc1);
189 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
190 switch (udc->phy_mode) {
191 case FSL_USB2_PHY_ULPI:
192 portctrl |= PORTSCX_PTS_ULPI;
193 break;
194 case FSL_USB2_PHY_UTMI_WIDE:
195 portctrl |= PORTSCX_PTW_16BIT;
196 /* fall through */
197 case FSL_USB2_PHY_UTMI:
198 portctrl |= PORTSCX_PTS_UTMI;
199 break;
200 case FSL_USB2_PHY_SERIAL:
201 portctrl |= PORTSCX_PTS_FSLS;
202 break;
203 default:
204 return -EINVAL;
205 }
206 fsl_writel(portctrl, &dr_regs->portsc1);
207
181 /* Stop and reset the usb controller */ 208 /* Stop and reset the usb controller */
182 tmp = fsl_readl(&dr_regs->usbcmd); 209 tmp = fsl_readl(&dr_regs->usbcmd);
183 tmp &= ~USB_CMD_RUN_STOP; 210 tmp &= ~USB_CMD_RUN_STOP;
@@ -215,31 +242,12 @@ static int dr_controller_setup(struct fsl_udc *udc)
215 udc->ep_qh, (int)tmp, 242 udc->ep_qh, (int)tmp,
216 fsl_readl(&dr_regs->endpointlistaddr)); 243 fsl_readl(&dr_regs->endpointlistaddr));
217 244
218 /* Config PHY interface */
219 portctrl = fsl_readl(&dr_regs->portsc1);
220 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
221 switch (udc->phy_mode) {
222 case FSL_USB2_PHY_ULPI:
223 portctrl |= PORTSCX_PTS_ULPI;
224 break;
225 case FSL_USB2_PHY_UTMI_WIDE:
226 portctrl |= PORTSCX_PTW_16BIT;
227 /* fall through */
228 case FSL_USB2_PHY_UTMI:
229 portctrl |= PORTSCX_PTS_UTMI;
230 break;
231 case FSL_USB2_PHY_SERIAL:
232 portctrl |= PORTSCX_PTS_FSLS;
233 break;
234 default:
235 return -EINVAL;
236 }
237 fsl_writel(portctrl, &dr_regs->portsc1);
238
239 /* Config control enable i/o output, cpu endian register */ 245 /* Config control enable i/o output, cpu endian register */
246#ifndef CONFIG_ARCH_MXC
240 ctrl = __raw_readl(&usb_sys_regs->control); 247 ctrl = __raw_readl(&usb_sys_regs->control);
241 ctrl |= USB_CTRL_IOENB; 248 ctrl |= USB_CTRL_IOENB;
242 __raw_writel(ctrl, &usb_sys_regs->control); 249 __raw_writel(ctrl, &usb_sys_regs->control);
250#endif
243 251
244#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE) 252#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
245 /* Turn on cache snooping hardware, since some PowerPC platforms 253 /* Turn on cache snooping hardware, since some PowerPC platforms
@@ -2043,6 +2051,7 @@ static int fsl_proc_read(char *page, char **start, off_t off, int count,
2043 size -= t; 2051 size -= t;
2044 next += t; 2052 next += t;
2045 2053
2054#ifndef CONFIG_ARCH_MXC
2046 tmp_reg = usb_sys_regs->snoop1; 2055 tmp_reg = usb_sys_regs->snoop1;
2047 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg); 2056 t = scnprintf(next, size, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2048 size -= t; 2057 size -= t;
@@ -2053,6 +2062,7 @@ static int fsl_proc_read(char *page, char **start, off_t off, int count,
2053 tmp_reg); 2062 tmp_reg);
2054 size -= t; 2063 size -= t;
2055 next += t; 2064 next += t;
2065#endif
2056 2066
2057 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */ 2067 /* ------fsl_udc, fsl_ep, fsl_request structure information ----- */
2058 ep = &udc->eps[0]; 2068 ep = &udc->eps[0];
@@ -2263,14 +2273,21 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2263 goto err_kfree; 2273 goto err_kfree;
2264 } 2274 }
2265 2275
2266 dr_regs = ioremap(res->start, res->end - res->start + 1); 2276 dr_regs = ioremap(res->start, resource_size(res));
2267 if (!dr_regs) { 2277 if (!dr_regs) {
2268 ret = -ENOMEM; 2278 ret = -ENOMEM;
2269 goto err_release_mem_region; 2279 goto err_release_mem_region;
2270 } 2280 }
2271 2281
2282#ifndef CONFIG_ARCH_MXC
2272 usb_sys_regs = (struct usb_sys_interface *) 2283 usb_sys_regs = (struct usb_sys_interface *)
2273 ((u32)dr_regs + USB_DR_SYS_OFFSET); 2284 ((u32)dr_regs + USB_DR_SYS_OFFSET);
2285#endif
2286
2287 /* Initialize USB clocks */
2288 ret = fsl_udc_clk_init(pdev);
2289 if (ret < 0)
2290 goto err_iounmap_noclk;
2274 2291
2275 /* Read Device Controller Capability Parameters register */ 2292 /* Read Device Controller Capability Parameters register */
2276 dccparams = fsl_readl(&dr_regs->dccparams); 2293 dccparams = fsl_readl(&dr_regs->dccparams);
@@ -2308,6 +2325,8 @@ static int __init fsl_udc_probe(struct platform_device *pdev)
2308 * leave usbintr reg untouched */ 2325 * leave usbintr reg untouched */
2309 dr_controller_setup(udc_controller); 2326 dr_controller_setup(udc_controller);
2310 2327
2328 fsl_udc_clk_finalize(pdev);
2329
2311 /* Setup gadget structure */ 2330 /* Setup gadget structure */
2312 udc_controller->gadget.ops = &fsl_gadget_ops; 2331 udc_controller->gadget.ops = &fsl_gadget_ops;
2313 udc_controller->gadget.is_dualspeed = 1; 2332 udc_controller->gadget.is_dualspeed = 1;
@@ -2362,6 +2381,8 @@ err_unregister:
2362err_free_irq: 2381err_free_irq:
2363 free_irq(udc_controller->irq, udc_controller); 2382 free_irq(udc_controller->irq, udc_controller);
2364err_iounmap: 2383err_iounmap:
2384 fsl_udc_clk_release();
2385err_iounmap_noclk:
2365 iounmap(dr_regs); 2386 iounmap(dr_regs);
2366err_release_mem_region: 2387err_release_mem_region:
2367 release_mem_region(res->start, res->end - res->start + 1); 2388 release_mem_region(res->start, res->end - res->start + 1);
@@ -2384,6 +2405,8 @@ static int __exit fsl_udc_remove(struct platform_device *pdev)
2384 return -ENODEV; 2405 return -ENODEV;
2385 udc_controller->done = &done; 2406 udc_controller->done = &done;
2386 2407
2408 fsl_udc_clk_release();
2409
2387 /* DR has been stopped in usb_gadget_unregister_driver() */ 2410 /* DR has been stopped in usb_gadget_unregister_driver() */
2388 remove_proc_file(); 2411 remove_proc_file();
2389 2412
diff --git a/drivers/usb/gadget/fsl_usb2_udc.h b/drivers/usb/gadget/fsl_usb2_udc.h
index e63ef12645f5..20aeceed48c7 100644
--- a/drivers/usb/gadget/fsl_usb2_udc.h
+++ b/drivers/usb/gadget/fsl_usb2_udc.h
@@ -563,4 +563,22 @@ static void dump_msg(const char *label, const u8 * buf, unsigned int length)
563 * 2 + ((windex & USB_DIR_IN) ? 1 : 0)) 563 * 2 + ((windex & USB_DIR_IN) ? 1 : 0))
564#define get_pipe_by_ep(EP) (ep_index(EP) * 2 + ep_is_in(EP)) 564#define get_pipe_by_ep(EP) (ep_index(EP) * 2 + ep_is_in(EP))
565 565
566struct platform_device;
567#ifdef CONFIG_ARCH_MXC
568int fsl_udc_clk_init(struct platform_device *pdev);
569void fsl_udc_clk_finalize(struct platform_device *pdev);
570void fsl_udc_clk_release(void);
571#else
572static inline int fsl_udc_clk_init(struct platform_device *pdev)
573{
574 return 0;
575}
576static inline void fsl_udc_clk_finalize(struct platform_device *pdev)
577{
578}
579static inline void fsl_udc_clk_release(void)
580{
581}
582#endif
583
566#endif 584#endif
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index ec6d439a2aa5..8e0e9a0b7364 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -137,6 +137,12 @@
137#define gadget_is_musbhdrc(g) 0 137#define gadget_is_musbhdrc(g) 0
138#endif 138#endif
139 139
140#ifdef CONFIG_USB_GADGET_LANGWELL
141#define gadget_is_langwell(g) (!strcmp("langwell_udc", (g)->name))
142#else
143#define gadget_is_langwell(g) 0
144#endif
145
140/* from Montavista kernel (?) */ 146/* from Montavista kernel (?) */
141#ifdef CONFIG_USB_GADGET_MPC8272 147#ifdef CONFIG_USB_GADGET_MPC8272
142#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name) 148#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name)
@@ -231,6 +237,8 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
231 return 0x22; 237 return 0x22;
232 else if (gadget_is_ci13xxx(gadget)) 238 else if (gadget_is_ci13xxx(gadget))
233 return 0x23; 239 return 0x23;
240 else if (gadget_is_langwell(gadget))
241 return 0x24;
234 return -ENOENT; 242 return -ENOENT;
235} 243}
236 244
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index de010c939dbb..112bb40a427c 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -110,10 +110,10 @@ goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
110 return -EINVAL; 110 return -EINVAL;
111 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 111 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
112 return -ESHUTDOWN; 112 return -ESHUTDOWN;
113 if (ep->num != (desc->bEndpointAddress & 0x0f)) 113 if (ep->num != usb_endpoint_num(desc))
114 return -EINVAL; 114 return -EINVAL;
115 115
116 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 116 switch (usb_endpoint_type(desc)) {
117 case USB_ENDPOINT_XFER_BULK: 117 case USB_ENDPOINT_XFER_BULK:
118 case USB_ENDPOINT_XFER_INT: 118 case USB_ENDPOINT_XFER_INT:
119 break; 119 break;
@@ -142,7 +142,7 @@ goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
142 /* ep1/ep2 dma direction is chosen early; it works in the other 142 /* ep1/ep2 dma direction is chosen early; it works in the other
143 * direction, with pio. be cautious with out-dma. 143 * direction, with pio. be cautious with out-dma.
144 */ 144 */
145 ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0; 145 ep->is_in = usb_endpoint_dir_in(desc);
146 if (ep->is_in) { 146 if (ep->is_in) {
147 mode |= 1; 147 mode |= 1;
148 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT); 148 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
diff --git a/drivers/usb/gadget/imx_udc.c b/drivers/usb/gadget/imx_udc.c
index 168658b4b4e2..c52a681f376c 100644
--- a/drivers/usb/gadget/imx_udc.c
+++ b/drivers/usb/gadget/imx_udc.c
@@ -415,6 +415,13 @@ static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
415 u8 *buf; 415 u8 *buf;
416 int length, count, temp; 416 int length, count, temp;
417 417
418 if (unlikely(__raw_readl(imx_ep->imx_usb->base +
419 USB_EP_STAT(EP_NO(imx_ep))) & EPSTAT_ZLPS)) {
420 D_TRX(imx_ep->imx_usb->dev, "<%s> zlp still queued in EP %s\n",
421 __func__, imx_ep->ep.name);
422 return -1;
423 }
424
418 buf = req->req.buf + req->req.actual; 425 buf = req->req.buf + req->req.actual;
419 prefetch(buf); 426 prefetch(buf);
420 427
@@ -734,9 +741,12 @@ static struct usb_request *imx_ep_alloc_request
734{ 741{
735 struct imx_request *req; 742 struct imx_request *req;
736 743
744 if (!usb_ep)
745 return NULL;
746
737 req = kzalloc(sizeof *req, gfp_flags); 747 req = kzalloc(sizeof *req, gfp_flags);
738 if (!req || !usb_ep) 748 if (!req)
739 return 0; 749 return NULL;
740 750
741 INIT_LIST_HEAD(&req->queue); 751 INIT_LIST_HEAD(&req->queue);
742 req->in_use = 0; 752 req->in_use = 0;
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index d20937f28a19..7d33f50b5874 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -384,9 +384,8 @@ ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
384 return value; 384 return value;
385 385
386 /* halt any endpoint by doing a "wrong direction" i/o call */ 386 /* halt any endpoint by doing a "wrong direction" i/o call */
387 if (data->desc.bEndpointAddress & USB_DIR_IN) { 387 if (usb_endpoint_dir_in(&data->desc)) {
388 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 388 if (usb_endpoint_xfer_isoc(&data->desc))
389 == USB_ENDPOINT_XFER_ISOC)
390 return -EINVAL; 389 return -EINVAL;
391 DBG (data->dev, "%s halt\n", data->name); 390 DBG (data->dev, "%s halt\n", data->name);
392 spin_lock_irq (&data->dev->lock); 391 spin_lock_irq (&data->dev->lock);
@@ -428,9 +427,8 @@ ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
428 return value; 427 return value;
429 428
430 /* halt any endpoint by doing a "wrong direction" i/o call */ 429 /* halt any endpoint by doing a "wrong direction" i/o call */
431 if (!(data->desc.bEndpointAddress & USB_DIR_IN)) { 430 if (!usb_endpoint_dir_in(&data->desc)) {
432 if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 431 if (usb_endpoint_xfer_isoc(&data->desc))
433 == USB_ENDPOINT_XFER_ISOC)
434 return -EINVAL; 432 return -EINVAL;
435 DBG (data->dev, "%s halt\n", data->name); 433 DBG (data->dev, "%s halt\n", data->name);
436 spin_lock_irq (&data->dev->lock); 434 spin_lock_irq (&data->dev->lock);
@@ -691,7 +689,7 @@ ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
691 struct ep_data *epdata = iocb->ki_filp->private_data; 689 struct ep_data *epdata = iocb->ki_filp->private_data;
692 char *buf; 690 char *buf;
693 691
694 if (unlikely(epdata->desc.bEndpointAddress & USB_DIR_IN)) 692 if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
695 return -EINVAL; 693 return -EINVAL;
696 694
697 buf = kmalloc(iocb->ki_left, GFP_KERNEL); 695 buf = kmalloc(iocb->ki_left, GFP_KERNEL);
@@ -711,7 +709,7 @@ ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
711 size_t len = 0; 709 size_t len = 0;
712 int i = 0; 710 int i = 0;
713 711
714 if (unlikely(!(epdata->desc.bEndpointAddress & USB_DIR_IN))) 712 if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
715 return -EINVAL; 713 return -EINVAL;
716 714
717 buf = kmalloc(iocb->ki_left, GFP_KERNEL); 715 buf = kmalloc(iocb->ki_left, GFP_KERNEL);
diff --git a/drivers/usb/gadget/langwell_udc.c b/drivers/usb/gadget/langwell_udc.c
new file mode 100644
index 000000000000..a3913519fd58
--- /dev/null
+++ b/drivers/usb/gadget/langwell_udc.c
@@ -0,0 +1,3372 @@
1/*
2 * Intel Langwell USB Device Controller driver
3 * Copyright (C) 2008-2009, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20
21/* #undef DEBUG */
22/* #undef VERBOSE */
23
24#if defined(CONFIG_USB_LANGWELL_OTG)
25#define OTG_TRANSCEIVER
26#endif
27
28
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/dma-mapping.h>
32#include <linux/kernel.h>
33#include <linux/delay.h>
34#include <linux/ioport.h>
35#include <linux/sched.h>
36#include <linux/slab.h>
37#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/timer.h>
40#include <linux/list.h>
41#include <linux/interrupt.h>
42#include <linux/moduleparam.h>
43#include <linux/device.h>
44#include <linux/usb/ch9.h>
45#include <linux/usb/gadget.h>
46#include <linux/usb/otg.h>
47#include <linux/pm.h>
48#include <linux/io.h>
49#include <linux/irq.h>
50#include <asm/system.h>
51#include <asm/unaligned.h>
52
53#include "langwell_udc.h"
54
55
56#define DRIVER_DESC "Intel Langwell USB Device Controller driver"
57#define DRIVER_VERSION "16 May 2009"
58
59static const char driver_name[] = "langwell_udc";
60static const char driver_desc[] = DRIVER_DESC;
61
62
63/* controller device global variable */
64static struct langwell_udc *the_controller;
65
66/* for endpoint 0 operations */
67static const struct usb_endpoint_descriptor
68langwell_ep0_desc = {
69 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71 .bEndpointAddress = 0,
72 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
73 .wMaxPacketSize = EP0_MAX_PKT_SIZE,
74};
75
76
77/*-------------------------------------------------------------------------*/
78/* debugging */
79
80#ifdef DEBUG
81#define DBG(dev, fmt, args...) \
82 pr_debug("%s %s: " fmt , driver_name, \
83 pci_name(dev->pdev), ## args)
84#else
85#define DBG(dev, fmt, args...) \
86 do { } while (0)
87#endif /* DEBUG */
88
89
90#ifdef VERBOSE
91#define VDBG DBG
92#else
93#define VDBG(dev, fmt, args...) \
94 do { } while (0)
95#endif /* VERBOSE */
96
97
98#define ERROR(dev, fmt, args...) \
99 pr_err("%s %s: " fmt , driver_name, \
100 pci_name(dev->pdev), ## args)
101
102#define WARNING(dev, fmt, args...) \
103 pr_warning("%s %s: " fmt , driver_name, \
104 pci_name(dev->pdev), ## args)
105
106#define INFO(dev, fmt, args...) \
107 pr_info("%s %s: " fmt , driver_name, \
108 pci_name(dev->pdev), ## args)
109
110
111#ifdef VERBOSE
112static inline void print_all_registers(struct langwell_udc *dev)
113{
114 int i;
115
116 /* Capability Registers */
117 printk(KERN_DEBUG "Capability Registers (offset: "
118 "0x%04x, length: 0x%08x)\n",
119 CAP_REG_OFFSET,
120 (u32)sizeof(struct langwell_cap_regs));
121 printk(KERN_DEBUG "caplength=0x%02x\n",
122 readb(&dev->cap_regs->caplength));
123 printk(KERN_DEBUG "hciversion=0x%04x\n",
124 readw(&dev->cap_regs->hciversion));
125 printk(KERN_DEBUG "hcsparams=0x%08x\n",
126 readl(&dev->cap_regs->hcsparams));
127 printk(KERN_DEBUG "hccparams=0x%08x\n",
128 readl(&dev->cap_regs->hccparams));
129 printk(KERN_DEBUG "dciversion=0x%04x\n",
130 readw(&dev->cap_regs->dciversion));
131 printk(KERN_DEBUG "dccparams=0x%08x\n",
132 readl(&dev->cap_regs->dccparams));
133
134 /* Operational Registers */
135 printk(KERN_DEBUG "Operational Registers (offset: "
136 "0x%04x, length: 0x%08x)\n",
137 OP_REG_OFFSET,
138 (u32)sizeof(struct langwell_op_regs));
139 printk(KERN_DEBUG "extsts=0x%08x\n",
140 readl(&dev->op_regs->extsts));
141 printk(KERN_DEBUG "extintr=0x%08x\n",
142 readl(&dev->op_regs->extintr));
143 printk(KERN_DEBUG "usbcmd=0x%08x\n",
144 readl(&dev->op_regs->usbcmd));
145 printk(KERN_DEBUG "usbsts=0x%08x\n",
146 readl(&dev->op_regs->usbsts));
147 printk(KERN_DEBUG "usbintr=0x%08x\n",
148 readl(&dev->op_regs->usbintr));
149 printk(KERN_DEBUG "frindex=0x%08x\n",
150 readl(&dev->op_regs->frindex));
151 printk(KERN_DEBUG "ctrldssegment=0x%08x\n",
152 readl(&dev->op_regs->ctrldssegment));
153 printk(KERN_DEBUG "deviceaddr=0x%08x\n",
154 readl(&dev->op_regs->deviceaddr));
155 printk(KERN_DEBUG "endpointlistaddr=0x%08x\n",
156 readl(&dev->op_regs->endpointlistaddr));
157 printk(KERN_DEBUG "ttctrl=0x%08x\n",
158 readl(&dev->op_regs->ttctrl));
159 printk(KERN_DEBUG "burstsize=0x%08x\n",
160 readl(&dev->op_regs->burstsize));
161 printk(KERN_DEBUG "txfilltuning=0x%08x\n",
162 readl(&dev->op_regs->txfilltuning));
163 printk(KERN_DEBUG "txttfilltuning=0x%08x\n",
164 readl(&dev->op_regs->txttfilltuning));
165 printk(KERN_DEBUG "ic_usb=0x%08x\n",
166 readl(&dev->op_regs->ic_usb));
167 printk(KERN_DEBUG "ulpi_viewport=0x%08x\n",
168 readl(&dev->op_regs->ulpi_viewport));
169 printk(KERN_DEBUG "configflag=0x%08x\n",
170 readl(&dev->op_regs->configflag));
171 printk(KERN_DEBUG "portsc1=0x%08x\n",
172 readl(&dev->op_regs->portsc1));
173 printk(KERN_DEBUG "devlc=0x%08x\n",
174 readl(&dev->op_regs->devlc));
175 printk(KERN_DEBUG "otgsc=0x%08x\n",
176 readl(&dev->op_regs->otgsc));
177 printk(KERN_DEBUG "usbmode=0x%08x\n",
178 readl(&dev->op_regs->usbmode));
179 printk(KERN_DEBUG "endptnak=0x%08x\n",
180 readl(&dev->op_regs->endptnak));
181 printk(KERN_DEBUG "endptnaken=0x%08x\n",
182 readl(&dev->op_regs->endptnaken));
183 printk(KERN_DEBUG "endptsetupstat=0x%08x\n",
184 readl(&dev->op_regs->endptsetupstat));
185 printk(KERN_DEBUG "endptprime=0x%08x\n",
186 readl(&dev->op_regs->endptprime));
187 printk(KERN_DEBUG "endptflush=0x%08x\n",
188 readl(&dev->op_regs->endptflush));
189 printk(KERN_DEBUG "endptstat=0x%08x\n",
190 readl(&dev->op_regs->endptstat));
191 printk(KERN_DEBUG "endptcomplete=0x%08x\n",
192 readl(&dev->op_regs->endptcomplete));
193
194 for (i = 0; i < dev->ep_max / 2; i++) {
195 printk(KERN_DEBUG "endptctrl[%d]=0x%08x\n",
196 i, readl(&dev->op_regs->endptctrl[i]));
197 }
198}
199#endif /* VERBOSE */
200
201
202/*-------------------------------------------------------------------------*/
203
204#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
205
206#define is_in(ep) (((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir == \
207 USB_DIR_IN) : ((ep)->desc->bEndpointAddress \
208 & USB_DIR_IN) == USB_DIR_IN)
209
210
211#ifdef DEBUG
212static char *type_string(u8 bmAttributes)
213{
214 switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
215 case USB_ENDPOINT_XFER_BULK:
216 return "bulk";
217 case USB_ENDPOINT_XFER_ISOC:
218 return "iso";
219 case USB_ENDPOINT_XFER_INT:
220 return "int";
221 };
222
223 return "control";
224}
225#endif
226
227
228/* configure endpoint control registers */
229static void ep_reset(struct langwell_ep *ep, unsigned char ep_num,
230 unsigned char is_in, unsigned char ep_type)
231{
232 struct langwell_udc *dev;
233 u32 endptctrl;
234
235 dev = ep->dev;
236 VDBG(dev, "---> %s()\n", __func__);
237
238 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
239 if (is_in) { /* TX */
240 if (ep_num)
241 endptctrl |= EPCTRL_TXR;
242 endptctrl |= EPCTRL_TXE;
243 endptctrl |= ep_type << EPCTRL_TXT_SHIFT;
244 } else { /* RX */
245 if (ep_num)
246 endptctrl |= EPCTRL_RXR;
247 endptctrl |= EPCTRL_RXE;
248 endptctrl |= ep_type << EPCTRL_RXT_SHIFT;
249 }
250
251 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
252
253 VDBG(dev, "<--- %s()\n", __func__);
254}
255
256
257/* reset ep0 dQH and endptctrl */
258static void ep0_reset(struct langwell_udc *dev)
259{
260 struct langwell_ep *ep;
261 int i;
262
263 VDBG(dev, "---> %s()\n", __func__);
264
265 /* ep0 in and out */
266 for (i = 0; i < 2; i++) {
267 ep = &dev->ep[i];
268 ep->dev = dev;
269
270 /* ep0 dQH */
271 ep->dqh = &dev->ep_dqh[i];
272
273 /* configure ep0 endpoint capabilities in dQH */
274 ep->dqh->dqh_ios = 1;
275 ep->dqh->dqh_mpl = EP0_MAX_PKT_SIZE;
276
277 /* FIXME: enable ep0-in HW zero length termination select */
278 if (is_in(ep))
279 ep->dqh->dqh_zlt = 0;
280 ep->dqh->dqh_mult = 0;
281
282 /* configure ep0 control registers */
283 ep_reset(&dev->ep[0], 0, i, USB_ENDPOINT_XFER_CONTROL);
284 }
285
286 VDBG(dev, "<--- %s()\n", __func__);
287 return;
288}
289
290
291/*-------------------------------------------------------------------------*/
292
293/* endpoints operations */
294
295/* configure endpoint, making it usable */
296static int langwell_ep_enable(struct usb_ep *_ep,
297 const struct usb_endpoint_descriptor *desc)
298{
299 struct langwell_udc *dev;
300 struct langwell_ep *ep;
301 u16 max = 0;
302 unsigned long flags;
303 int retval = 0;
304 unsigned char zlt, ios = 0, mult = 0;
305
306 ep = container_of(_ep, struct langwell_ep, ep);
307 dev = ep->dev;
308 VDBG(dev, "---> %s()\n", __func__);
309
310 if (!_ep || !desc || ep->desc
311 || desc->bDescriptorType != USB_DT_ENDPOINT)
312 return -EINVAL;
313
314 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
315 return -ESHUTDOWN;
316
317 max = le16_to_cpu(desc->wMaxPacketSize);
318
319 /*
320 * disable HW zero length termination select
321 * driver handles zero length packet through req->req.zero
322 */
323 zlt = 1;
324
325 /*
326 * sanity check type, direction, address, and then
327 * initialize the endpoint capabilities fields in dQH
328 */
329 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
330 case USB_ENDPOINT_XFER_CONTROL:
331 ios = 1;
332 break;
333 case USB_ENDPOINT_XFER_BULK:
334 if ((dev->gadget.speed == USB_SPEED_HIGH
335 && max != 512)
336 || (dev->gadget.speed == USB_SPEED_FULL
337 && max > 64)) {
338 goto done;
339 }
340 break;
341 case USB_ENDPOINT_XFER_INT:
342 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
343 goto done;
344
345 switch (dev->gadget.speed) {
346 case USB_SPEED_HIGH:
347 if (max <= 1024)
348 break;
349 case USB_SPEED_FULL:
350 if (max <= 64)
351 break;
352 default:
353 if (max <= 8)
354 break;
355 goto done;
356 }
357 break;
358 case USB_ENDPOINT_XFER_ISOC:
359 if (strstr(ep->ep.name, "-bulk")
360 || strstr(ep->ep.name, "-int"))
361 goto done;
362
363 switch (dev->gadget.speed) {
364 case USB_SPEED_HIGH:
365 if (max <= 1024)
366 break;
367 case USB_SPEED_FULL:
368 if (max <= 1023)
369 break;
370 default:
371 goto done;
372 }
373 /*
374 * FIXME:
375 * calculate transactions needed for high bandwidth iso
376 */
377 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
378 max = max & 0x8ff; /* bit 0~10 */
379 /* 3 transactions at most */
380 if (mult > 3)
381 goto done;
382 break;
383 default:
384 goto done;
385 }
386
387 spin_lock_irqsave(&dev->lock, flags);
388
389 /* configure endpoint capabilities in dQH */
390 ep->dqh->dqh_ios = ios;
391 ep->dqh->dqh_mpl = cpu_to_le16(max);
392 ep->dqh->dqh_zlt = zlt;
393 ep->dqh->dqh_mult = mult;
394
395 ep->ep.maxpacket = max;
396 ep->desc = desc;
397 ep->stopped = 0;
398 ep->ep_num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
399
400 /* ep_type */
401 ep->ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
402
403 /* configure endpoint control registers */
404 ep_reset(ep, ep->ep_num, is_in(ep), ep->ep_type);
405
406 DBG(dev, "enabled %s (ep%d%s-%s), max %04x\n",
407 _ep->name,
408 ep->ep_num,
409 DIR_STRING(desc->bEndpointAddress),
410 type_string(desc->bmAttributes),
411 max);
412
413 spin_unlock_irqrestore(&dev->lock, flags);
414done:
415 VDBG(dev, "<--- %s()\n", __func__);
416 return retval;
417}
418
419
420/*-------------------------------------------------------------------------*/
421
422/* retire a request */
423static void done(struct langwell_ep *ep, struct langwell_request *req,
424 int status)
425{
426 struct langwell_udc *dev = ep->dev;
427 unsigned stopped = ep->stopped;
428 struct langwell_dtd *curr_dtd, *next_dtd;
429 int i;
430
431 VDBG(dev, "---> %s()\n", __func__);
432
433 /* remove the req from ep->queue */
434 list_del_init(&req->queue);
435
436 if (req->req.status == -EINPROGRESS)
437 req->req.status = status;
438 else
439 status = req->req.status;
440
441 /* free dTD for the request */
442 next_dtd = req->head;
443 for (i = 0; i < req->dtd_count; i++) {
444 curr_dtd = next_dtd;
445 if (i != req->dtd_count - 1)
446 next_dtd = curr_dtd->next_dtd_virt;
447 dma_pool_free(dev->dtd_pool, curr_dtd, curr_dtd->dtd_dma);
448 }
449
450 if (req->mapped) {
451 dma_unmap_single(&dev->pdev->dev, req->req.dma, req->req.length,
452 is_in(ep) ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
453 req->req.dma = DMA_ADDR_INVALID;
454 req->mapped = 0;
455 } else
456 dma_sync_single_for_cpu(&dev->pdev->dev, req->req.dma,
457 req->req.length,
458 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
459
460 if (status != -ESHUTDOWN)
461 DBG(dev, "complete %s, req %p, stat %d, len %u/%u\n",
462 ep->ep.name, &req->req, status,
463 req->req.actual, req->req.length);
464
465 /* don't modify queue heads during completion callback */
466 ep->stopped = 1;
467
468 spin_unlock(&dev->lock);
469 /* complete routine from gadget driver */
470 if (req->req.complete)
471 req->req.complete(&ep->ep, &req->req);
472
473 spin_lock(&dev->lock);
474 ep->stopped = stopped;
475
476 VDBG(dev, "<--- %s()\n", __func__);
477}
478
479
480static void langwell_ep_fifo_flush(struct usb_ep *_ep);
481
482/* delete all endpoint requests, called with spinlock held */
483static void nuke(struct langwell_ep *ep, int status)
484{
485 /* called with spinlock held */
486 ep->stopped = 1;
487
488 /* endpoint fifo flush */
489 if (&ep->ep && ep->desc)
490 langwell_ep_fifo_flush(&ep->ep);
491
492 while (!list_empty(&ep->queue)) {
493 struct langwell_request *req = NULL;
494 req = list_entry(ep->queue.next, struct langwell_request,
495 queue);
496 done(ep, req, status);
497 }
498}
499
500
501/*-------------------------------------------------------------------------*/
502
503/* endpoint is no longer usable */
504static int langwell_ep_disable(struct usb_ep *_ep)
505{
506 struct langwell_ep *ep;
507 unsigned long flags;
508 struct langwell_udc *dev;
509 int ep_num;
510 u32 endptctrl;
511
512 ep = container_of(_ep, struct langwell_ep, ep);
513 dev = ep->dev;
514 VDBG(dev, "---> %s()\n", __func__);
515
516 if (!_ep || !ep->desc)
517 return -EINVAL;
518
519 spin_lock_irqsave(&dev->lock, flags);
520
521 /* disable endpoint control register */
522 ep_num = ep->ep_num;
523 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
524 if (is_in(ep))
525 endptctrl &= ~EPCTRL_TXE;
526 else
527 endptctrl &= ~EPCTRL_RXE;
528 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
529
530 /* nuke all pending requests (does flush) */
531 nuke(ep, -ESHUTDOWN);
532
533 ep->desc = NULL;
534 ep->stopped = 1;
535
536 spin_unlock_irqrestore(&dev->lock, flags);
537
538 DBG(dev, "disabled %s\n", _ep->name);
539 VDBG(dev, "<--- %s()\n", __func__);
540
541 return 0;
542}
543
544
545/* allocate a request object to use with this endpoint */
546static struct usb_request *langwell_alloc_request(struct usb_ep *_ep,
547 gfp_t gfp_flags)
548{
549 struct langwell_ep *ep;
550 struct langwell_udc *dev;
551 struct langwell_request *req = NULL;
552
553 if (!_ep)
554 return NULL;
555
556 ep = container_of(_ep, struct langwell_ep, ep);
557 dev = ep->dev;
558 VDBG(dev, "---> %s()\n", __func__);
559
560 req = kzalloc(sizeof(*req), gfp_flags);
561 if (!req)
562 return NULL;
563
564 req->req.dma = DMA_ADDR_INVALID;
565 INIT_LIST_HEAD(&req->queue);
566
567 VDBG(dev, "alloc request for %s\n", _ep->name);
568 VDBG(dev, "<--- %s()\n", __func__);
569 return &req->req;
570}
571
572
573/* free a request object */
574static void langwell_free_request(struct usb_ep *_ep,
575 struct usb_request *_req)
576{
577 struct langwell_ep *ep;
578 struct langwell_udc *dev;
579 struct langwell_request *req = NULL;
580
581 ep = container_of(_ep, struct langwell_ep, ep);
582 dev = ep->dev;
583 VDBG(dev, "---> %s()\n", __func__);
584
585 if (!_ep || !_req)
586 return;
587
588 req = container_of(_req, struct langwell_request, req);
589 WARN_ON(!list_empty(&req->queue));
590
591 if (_req)
592 kfree(req);
593
594 VDBG(dev, "free request for %s\n", _ep->name);
595 VDBG(dev, "<--- %s()\n", __func__);
596}
597
598
599/*-------------------------------------------------------------------------*/
600
601/* queue dTD and PRIME endpoint */
602static int queue_dtd(struct langwell_ep *ep, struct langwell_request *req)
603{
604 u32 bit_mask, usbcmd, endptstat, dtd_dma;
605 u8 dtd_status;
606 int i;
607 struct langwell_dqh *dqh;
608 struct langwell_udc *dev;
609
610 dev = ep->dev;
611 VDBG(dev, "---> %s()\n", __func__);
612
613 i = ep->ep_num * 2 + is_in(ep);
614 dqh = &dev->ep_dqh[i];
615
616 if (ep->ep_num)
617 VDBG(dev, "%s\n", ep->name);
618 else
619 /* ep0 */
620 VDBG(dev, "%s-%s\n", ep->name, is_in(ep) ? "in" : "out");
621
622 VDBG(dev, "ep_dqh[%d] addr: 0x%08x\n", i, (u32)&(dev->ep_dqh[i]));
623
624 bit_mask = is_in(ep) ?
625 (1 << (ep->ep_num + 16)) : (1 << (ep->ep_num));
626
627 VDBG(dev, "bit_mask = 0x%08x\n", bit_mask);
628
629 /* check if the pipe is empty */
630 if (!(list_empty(&ep->queue))) {
631 /* add dTD to the end of linked list */
632 struct langwell_request *lastreq;
633 lastreq = list_entry(ep->queue.prev,
634 struct langwell_request, queue);
635
636 lastreq->tail->dtd_next =
637 cpu_to_le32(req->head->dtd_dma & DTD_NEXT_MASK);
638
639 /* read prime bit, if 1 goto out */
640 if (readl(&dev->op_regs->endptprime) & bit_mask)
641 goto out;
642
643 do {
644 /* set ATDTW bit in USBCMD */
645 usbcmd = readl(&dev->op_regs->usbcmd);
646 writel(usbcmd | CMD_ATDTW, &dev->op_regs->usbcmd);
647
648 /* read correct status bit */
649 endptstat = readl(&dev->op_regs->endptstat) & bit_mask;
650
651 } while (!(readl(&dev->op_regs->usbcmd) & CMD_ATDTW));
652
653 /* write ATDTW bit to 0 */
654 usbcmd = readl(&dev->op_regs->usbcmd);
655 writel(usbcmd & ~CMD_ATDTW, &dev->op_regs->usbcmd);
656
657 if (endptstat)
658 goto out;
659 }
660
661 /* write dQH next pointer and terminate bit to 0 */
662 dtd_dma = req->head->dtd_dma & DTD_NEXT_MASK;
663 dqh->dtd_next = cpu_to_le32(dtd_dma);
664
665 /* clear active and halt bit */
666 dtd_status = (u8) ~(DTD_STS_ACTIVE | DTD_STS_HALTED);
667 dqh->dtd_status &= dtd_status;
668 VDBG(dev, "dqh->dtd_status = 0x%x\n", dqh->dtd_status);
669
670 /* write 1 to endptprime register to PRIME endpoint */
671 bit_mask = is_in(ep) ? (1 << (ep->ep_num + 16)) : (1 << ep->ep_num);
672 VDBG(dev, "endprime bit_mask = 0x%08x\n", bit_mask);
673 writel(bit_mask, &dev->op_regs->endptprime);
674out:
675 VDBG(dev, "<--- %s()\n", __func__);
676 return 0;
677}
678
679
680/* fill in the dTD structure to build a transfer descriptor */
681static struct langwell_dtd *build_dtd(struct langwell_request *req,
682 unsigned *length, dma_addr_t *dma, int *is_last)
683{
684 u32 buf_ptr;
685 struct langwell_dtd *dtd;
686 struct langwell_udc *dev;
687 int i;
688
689 dev = req->ep->dev;
690 VDBG(dev, "---> %s()\n", __func__);
691
692 /* the maximum transfer length, up to 16k bytes */
693 *length = min(req->req.length - req->req.actual,
694 (unsigned)DTD_MAX_TRANSFER_LENGTH);
695
696 /* create dTD dma_pool resource */
697 dtd = dma_pool_alloc(dev->dtd_pool, GFP_KERNEL, dma);
698 if (dtd == NULL)
699 return dtd;
700 dtd->dtd_dma = *dma;
701
702 /* initialize buffer page pointers */
703 buf_ptr = (u32)(req->req.dma + req->req.actual);
704 for (i = 0; i < 5; i++)
705 dtd->dtd_buf[i] = cpu_to_le32(buf_ptr + i * PAGE_SIZE);
706
707 req->req.actual += *length;
708
709 /* fill in total bytes with transfer size */
710 dtd->dtd_total = cpu_to_le16(*length);
711 VDBG(dev, "dtd->dtd_total = %d\n", dtd->dtd_total);
712
713 /* set is_last flag if req->req.zero is set or not */
714 if (req->req.zero) {
715 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
716 *is_last = 1;
717 else
718 *is_last = 0;
719 } else if (req->req.length == req->req.actual) {
720 *is_last = 1;
721 } else
722 *is_last = 0;
723
724 if (*is_last == 0)
725 VDBG(dev, "multi-dtd request!\n");
726
727 /* set interrupt on complete bit for the last dTD */
728 if (*is_last && !req->req.no_interrupt)
729 dtd->dtd_ioc = 1;
730
731 /* set multiplier override 0 for non-ISO and non-TX endpoint */
732 dtd->dtd_multo = 0;
733
734 /* set the active bit of status field to 1 */
735 dtd->dtd_status = DTD_STS_ACTIVE;
736 VDBG(dev, "dtd->dtd_status = 0x%02x\n", dtd->dtd_status);
737
738 VDBG(dev, "length = %d, dma addr= 0x%08x\n", *length, (int)*dma);
739 VDBG(dev, "<--- %s()\n", __func__);
740 return dtd;
741}
742
743
744/* generate dTD linked list for a request */
745static int req_to_dtd(struct langwell_request *req)
746{
747 unsigned count;
748 int is_last, is_first = 1;
749 struct langwell_dtd *dtd, *last_dtd = NULL;
750 struct langwell_udc *dev;
751 dma_addr_t dma;
752
753 dev = req->ep->dev;
754 VDBG(dev, "---> %s()\n", __func__);
755 do {
756 dtd = build_dtd(req, &count, &dma, &is_last);
757 if (dtd == NULL)
758 return -ENOMEM;
759
760 if (is_first) {
761 is_first = 0;
762 req->head = dtd;
763 } else {
764 last_dtd->dtd_next = cpu_to_le32(dma);
765 last_dtd->next_dtd_virt = dtd;
766 }
767 last_dtd = dtd;
768 req->dtd_count++;
769 } while (!is_last);
770
771 /* set terminate bit to 1 for the last dTD */
772 dtd->dtd_next = DTD_TERM;
773
774 req->tail = dtd;
775
776 VDBG(dev, "<--- %s()\n", __func__);
777 return 0;
778}
779
780/*-------------------------------------------------------------------------*/
781
782/* queue (submits) an I/O requests to an endpoint */
783static int langwell_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
784 gfp_t gfp_flags)
785{
786 struct langwell_request *req;
787 struct langwell_ep *ep;
788 struct langwell_udc *dev;
789 unsigned long flags;
790 int is_iso = 0, zlflag = 0;
791
792 /* always require a cpu-view buffer */
793 req = container_of(_req, struct langwell_request, req);
794 ep = container_of(_ep, struct langwell_ep, ep);
795
796 if (!_req || !_req->complete || !_req->buf
797 || !list_empty(&req->queue)) {
798 return -EINVAL;
799 }
800
801 if (unlikely(!_ep || !ep->desc))
802 return -EINVAL;
803
804 dev = ep->dev;
805 req->ep = ep;
806 VDBG(dev, "---> %s()\n", __func__);
807
808 if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
809 if (req->req.length > ep->ep.maxpacket)
810 return -EMSGSIZE;
811 is_iso = 1;
812 }
813
814 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
815 return -ESHUTDOWN;
816
817 /* set up dma mapping in case the caller didn't */
818 if (_req->dma == DMA_ADDR_INVALID) {
819 /* WORKAROUND: WARN_ON(size == 0) */
820 if (_req->length == 0) {
821 VDBG(dev, "req->length: 0->1\n");
822 zlflag = 1;
823 _req->length++;
824 }
825
826 _req->dma = dma_map_single(&dev->pdev->dev,
827 _req->buf, _req->length,
828 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
829 if (zlflag && (_req->length == 1)) {
830 VDBG(dev, "req->length: 1->0\n");
831 zlflag = 0;
832 _req->length = 0;
833 }
834
835 req->mapped = 1;
836 VDBG(dev, "req->mapped = 1\n");
837 } else {
838 dma_sync_single_for_device(&dev->pdev->dev,
839 _req->dma, _req->length,
840 is_in(ep) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
841 req->mapped = 0;
842 VDBG(dev, "req->mapped = 0\n");
843 }
844
845 DBG(dev, "%s queue req %p, len %u, buf %p, dma 0x%08x\n",
846 _ep->name,
847 _req, _req->length, _req->buf, _req->dma);
848
849 _req->status = -EINPROGRESS;
850 _req->actual = 0;
851 req->dtd_count = 0;
852
853 spin_lock_irqsave(&dev->lock, flags);
854
855 /* build and put dTDs to endpoint queue */
856 if (!req_to_dtd(req)) {
857 queue_dtd(ep, req);
858 } else {
859 spin_unlock_irqrestore(&dev->lock, flags);
860 return -ENOMEM;
861 }
862
863 /* update ep0 state */
864 if (ep->ep_num == 0)
865 dev->ep0_state = DATA_STATE_XMIT;
866
867 if (likely(req != NULL)) {
868 list_add_tail(&req->queue, &ep->queue);
869 VDBG(dev, "list_add_tail() \n");
870 }
871
872 spin_unlock_irqrestore(&dev->lock, flags);
873
874 VDBG(dev, "<--- %s()\n", __func__);
875 return 0;
876}
877
878
879/* dequeue (cancels, unlinks) an I/O request from an endpoint */
880static int langwell_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
881{
882 struct langwell_ep *ep;
883 struct langwell_udc *dev;
884 struct langwell_request *req;
885 unsigned long flags;
886 int stopped, ep_num, retval = 0;
887 u32 endptctrl;
888
889 ep = container_of(_ep, struct langwell_ep, ep);
890 dev = ep->dev;
891 VDBG(dev, "---> %s()\n", __func__);
892
893 if (!_ep || !ep->desc || !_req)
894 return -EINVAL;
895
896 if (!dev->driver)
897 return -ESHUTDOWN;
898
899 spin_lock_irqsave(&dev->lock, flags);
900 stopped = ep->stopped;
901
902 /* quiesce dma while we patch the queue */
903 ep->stopped = 1;
904 ep_num = ep->ep_num;
905
906 /* disable endpoint control register */
907 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
908 if (is_in(ep))
909 endptctrl &= ~EPCTRL_TXE;
910 else
911 endptctrl &= ~EPCTRL_RXE;
912 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
913
914 /* make sure it's still queued on this endpoint */
915 list_for_each_entry(req, &ep->queue, queue) {
916 if (&req->req == _req)
917 break;
918 }
919
920 if (&req->req != _req) {
921 retval = -EINVAL;
922 goto done;
923 }
924
925 /* queue head may be partially complete. */
926 if (ep->queue.next == &req->queue) {
927 DBG(dev, "unlink (%s) dma\n", _ep->name);
928 _req->status = -ECONNRESET;
929 langwell_ep_fifo_flush(&ep->ep);
930
931 /* not the last request in endpoint queue */
932 if (likely(ep->queue.next == &req->queue)) {
933 struct langwell_dqh *dqh;
934 struct langwell_request *next_req;
935
936 dqh = ep->dqh;
937 next_req = list_entry(req->queue.next,
938 struct langwell_request, queue);
939
940 /* point the dQH to the first dTD of next request */
941 writel((u32) next_req->head, &dqh->dqh_current);
942 }
943 } else {
944 struct langwell_request *prev_req;
945
946 prev_req = list_entry(req->queue.prev,
947 struct langwell_request, queue);
948 writel(readl(&req->tail->dtd_next),
949 &prev_req->tail->dtd_next);
950 }
951
952 done(ep, req, -ECONNRESET);
953
954done:
955 /* enable endpoint again */
956 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
957 if (is_in(ep))
958 endptctrl |= EPCTRL_TXE;
959 else
960 endptctrl |= EPCTRL_RXE;
961 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
962
963 ep->stopped = stopped;
964 spin_unlock_irqrestore(&dev->lock, flags);
965
966 VDBG(dev, "<--- %s()\n", __func__);
967 return retval;
968}
969
970
971/*-------------------------------------------------------------------------*/
972
973/* endpoint set/clear halt */
974static void ep_set_halt(struct langwell_ep *ep, int value)
975{
976 u32 endptctrl = 0;
977 int ep_num;
978 struct langwell_udc *dev = ep->dev;
979 VDBG(dev, "---> %s()\n", __func__);
980
981 ep_num = ep->ep_num;
982 endptctrl = readl(&dev->op_regs->endptctrl[ep_num]);
983
984 /* value: 1 - set halt, 0 - clear halt */
985 if (value) {
986 /* set the stall bit */
987 if (is_in(ep))
988 endptctrl |= EPCTRL_TXS;
989 else
990 endptctrl |= EPCTRL_RXS;
991 } else {
992 /* clear the stall bit and reset data toggle */
993 if (is_in(ep)) {
994 endptctrl &= ~EPCTRL_TXS;
995 endptctrl |= EPCTRL_TXR;
996 } else {
997 endptctrl &= ~EPCTRL_RXS;
998 endptctrl |= EPCTRL_RXR;
999 }
1000 }
1001
1002 writel(endptctrl, &dev->op_regs->endptctrl[ep_num]);
1003
1004 VDBG(dev, "<--- %s()\n", __func__);
1005}
1006
1007
1008/* set the endpoint halt feature */
1009static int langwell_ep_set_halt(struct usb_ep *_ep, int value)
1010{
1011 struct langwell_ep *ep;
1012 struct langwell_udc *dev;
1013 unsigned long flags;
1014 int retval = 0;
1015
1016 ep = container_of(_ep, struct langwell_ep, ep);
1017 dev = ep->dev;
1018
1019 VDBG(dev, "---> %s()\n", __func__);
1020
1021 if (!_ep || !ep->desc)
1022 return -EINVAL;
1023
1024 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1025 return -ESHUTDOWN;
1026
1027 if (ep->desc && (ep->desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1028 == USB_ENDPOINT_XFER_ISOC)
1029 return -EOPNOTSUPP;
1030
1031 spin_lock_irqsave(&dev->lock, flags);
1032
1033 /*
1034 * attempt to halt IN ep will fail if any transfer requests
1035 * are still queue
1036 */
1037 if (!list_empty(&ep->queue) && is_in(ep) && value) {
1038 /* IN endpoint FIFO holds bytes */
1039 DBG(dev, "%s FIFO holds bytes\n", _ep->name);
1040 retval = -EAGAIN;
1041 goto done;
1042 }
1043
1044 /* endpoint set/clear halt */
1045 if (ep->ep_num) {
1046 ep_set_halt(ep, value);
1047 } else { /* endpoint 0 */
1048 dev->ep0_state = WAIT_FOR_SETUP;
1049 dev->ep0_dir = USB_DIR_OUT;
1050 }
1051done:
1052 spin_unlock_irqrestore(&dev->lock, flags);
1053 DBG(dev, "%s %s halt\n", _ep->name, value ? "set" : "clear");
1054 VDBG(dev, "<--- %s()\n", __func__);
1055 return retval;
1056}
1057
1058
1059/* set the halt feature and ignores clear requests */
1060static int langwell_ep_set_wedge(struct usb_ep *_ep)
1061{
1062 struct langwell_ep *ep;
1063 struct langwell_udc *dev;
1064
1065 ep = container_of(_ep, struct langwell_ep, ep);
1066 dev = ep->dev;
1067
1068 VDBG(dev, "---> %s()\n", __func__);
1069
1070 if (!_ep || !ep->desc)
1071 return -EINVAL;
1072
1073 VDBG(dev, "<--- %s()\n", __func__);
1074 return usb_ep_set_halt(_ep);
1075}
1076
1077
1078/* flush contents of a fifo */
1079static void langwell_ep_fifo_flush(struct usb_ep *_ep)
1080{
1081 struct langwell_ep *ep;
1082 struct langwell_udc *dev;
1083 u32 flush_bit;
1084 unsigned long timeout;
1085
1086 ep = container_of(_ep, struct langwell_ep, ep);
1087 dev = ep->dev;
1088
1089 VDBG(dev, "---> %s()\n", __func__);
1090
1091 if (!_ep || !ep->desc) {
1092 VDBG(dev, "ep or ep->desc is NULL\n");
1093 VDBG(dev, "<--- %s()\n", __func__);
1094 return;
1095 }
1096
1097 VDBG(dev, "%s-%s fifo flush\n", _ep->name, is_in(ep) ? "in" : "out");
1098
1099 /* flush endpoint buffer */
1100 if (ep->ep_num == 0)
1101 flush_bit = (1 << 16) | 1;
1102 else if (is_in(ep))
1103 flush_bit = 1 << (ep->ep_num + 16); /* TX */
1104 else
1105 flush_bit = 1 << ep->ep_num; /* RX */
1106
1107 /* wait until flush complete */
1108 timeout = jiffies + FLUSH_TIMEOUT;
1109 do {
1110 writel(flush_bit, &dev->op_regs->endptflush);
1111 while (readl(&dev->op_regs->endptflush)) {
1112 if (time_after(jiffies, timeout)) {
1113 ERROR(dev, "ep flush timeout\n");
1114 goto done;
1115 }
1116 cpu_relax();
1117 }
1118 } while (readl(&dev->op_regs->endptstat) & flush_bit);
1119done:
1120 VDBG(dev, "<--- %s()\n", __func__);
1121}
1122
1123
1124/* endpoints operations structure */
1125static const struct usb_ep_ops langwell_ep_ops = {
1126
1127 /* configure endpoint, making it usable */
1128 .enable = langwell_ep_enable,
1129
1130 /* endpoint is no longer usable */
1131 .disable = langwell_ep_disable,
1132
1133 /* allocate a request object to use with this endpoint */
1134 .alloc_request = langwell_alloc_request,
1135
1136 /* free a request object */
1137 .free_request = langwell_free_request,
1138
1139 /* queue (submits) an I/O requests to an endpoint */
1140 .queue = langwell_ep_queue,
1141
1142 /* dequeue (cancels, unlinks) an I/O request from an endpoint */
1143 .dequeue = langwell_ep_dequeue,
1144
1145 /* set the endpoint halt feature */
1146 .set_halt = langwell_ep_set_halt,
1147
1148 /* set the halt feature and ignores clear requests */
1149 .set_wedge = langwell_ep_set_wedge,
1150
1151 /* flush contents of a fifo */
1152 .fifo_flush = langwell_ep_fifo_flush,
1153};
1154
1155
1156/*-------------------------------------------------------------------------*/
1157
1158/* device controller usb_gadget_ops structure */
1159
1160/* returns the current frame number */
1161static int langwell_get_frame(struct usb_gadget *_gadget)
1162{
1163 struct langwell_udc *dev;
1164 u16 retval;
1165
1166 if (!_gadget)
1167 return -ENODEV;
1168
1169 dev = container_of(_gadget, struct langwell_udc, gadget);
1170 VDBG(dev, "---> %s()\n", __func__);
1171
1172 retval = readl(&dev->op_regs->frindex) & FRINDEX_MASK;
1173
1174 VDBG(dev, "<--- %s()\n", __func__);
1175 return retval;
1176}
1177
1178
1179/* tries to wake up the host connected to this gadget */
1180static int langwell_wakeup(struct usb_gadget *_gadget)
1181{
1182 struct langwell_udc *dev;
1183 u32 portsc1, devlc;
1184 unsigned long flags;
1185
1186 if (!_gadget)
1187 return 0;
1188
1189 dev = container_of(_gadget, struct langwell_udc, gadget);
1190 VDBG(dev, "---> %s()\n", __func__);
1191
1192 /* Remote Wakeup feature not enabled by host */
1193 if (!dev->remote_wakeup)
1194 return -ENOTSUPP;
1195
1196 spin_lock_irqsave(&dev->lock, flags);
1197
1198 portsc1 = readl(&dev->op_regs->portsc1);
1199 if (!(portsc1 & PORTS_SUSP)) {
1200 spin_unlock_irqrestore(&dev->lock, flags);
1201 return 0;
1202 }
1203
1204 /* LPM L1 to L0, remote wakeup */
1205 if (dev->lpm && dev->lpm_state == LPM_L1) {
1206 portsc1 |= PORTS_SLP;
1207 writel(portsc1, &dev->op_regs->portsc1);
1208 }
1209
1210 /* force port resume */
1211 if (dev->usb_state == USB_STATE_SUSPENDED) {
1212 portsc1 |= PORTS_FPR;
1213 writel(portsc1, &dev->op_regs->portsc1);
1214 }
1215
1216 /* exit PHY low power suspend */
1217 devlc = readl(&dev->op_regs->devlc);
1218 VDBG(dev, "devlc = 0x%08x\n", devlc);
1219 devlc &= ~LPM_PHCD;
1220 writel(devlc, &dev->op_regs->devlc);
1221
1222 spin_unlock_irqrestore(&dev->lock, flags);
1223
1224 VDBG(dev, "<--- %s()\n", __func__);
1225 return 0;
1226}
1227
1228
1229/* notify controller that VBUS is powered or not */
1230static int langwell_vbus_session(struct usb_gadget *_gadget, int is_active)
1231{
1232 struct langwell_udc *dev;
1233 unsigned long flags;
1234 u32 usbcmd;
1235
1236 if (!_gadget)
1237 return -ENODEV;
1238
1239 dev = container_of(_gadget, struct langwell_udc, gadget);
1240 VDBG(dev, "---> %s()\n", __func__);
1241
1242 spin_lock_irqsave(&dev->lock, flags);
1243 VDBG(dev, "VBUS status: %s\n", is_active ? "on" : "off");
1244
1245 dev->vbus_active = (is_active != 0);
1246 if (dev->driver && dev->softconnected && dev->vbus_active) {
1247 usbcmd = readl(&dev->op_regs->usbcmd);
1248 usbcmd |= CMD_RUNSTOP;
1249 writel(usbcmd, &dev->op_regs->usbcmd);
1250 } else {
1251 usbcmd = readl(&dev->op_regs->usbcmd);
1252 usbcmd &= ~CMD_RUNSTOP;
1253 writel(usbcmd, &dev->op_regs->usbcmd);
1254 }
1255
1256 spin_unlock_irqrestore(&dev->lock, flags);
1257
1258 VDBG(dev, "<--- %s()\n", __func__);
1259 return 0;
1260}
1261
1262
1263/* constrain controller's VBUS power usage */
1264static int langwell_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1265{
1266 struct langwell_udc *dev;
1267
1268 if (!_gadget)
1269 return -ENODEV;
1270
1271 dev = container_of(_gadget, struct langwell_udc, gadget);
1272 VDBG(dev, "---> %s()\n", __func__);
1273
1274 if (dev->transceiver) {
1275 VDBG(dev, "otg_set_power\n");
1276 VDBG(dev, "<--- %s()\n", __func__);
1277 return otg_set_power(dev->transceiver, mA);
1278 }
1279
1280 VDBG(dev, "<--- %s()\n", __func__);
1281 return -ENOTSUPP;
1282}
1283
1284
1285/* D+ pullup, software-controlled connect/disconnect to USB host */
1286static int langwell_pullup(struct usb_gadget *_gadget, int is_on)
1287{
1288 struct langwell_udc *dev;
1289 u32 usbcmd;
1290 unsigned long flags;
1291
1292 if (!_gadget)
1293 return -ENODEV;
1294
1295 dev = container_of(_gadget, struct langwell_udc, gadget);
1296
1297 VDBG(dev, "---> %s()\n", __func__);
1298
1299 spin_lock_irqsave(&dev->lock, flags);
1300 dev->softconnected = (is_on != 0);
1301
1302 if (dev->driver && dev->softconnected && dev->vbus_active) {
1303 usbcmd = readl(&dev->op_regs->usbcmd);
1304 usbcmd |= CMD_RUNSTOP;
1305 writel(usbcmd, &dev->op_regs->usbcmd);
1306 } else {
1307 usbcmd = readl(&dev->op_regs->usbcmd);
1308 usbcmd &= ~CMD_RUNSTOP;
1309 writel(usbcmd, &dev->op_regs->usbcmd);
1310 }
1311 spin_unlock_irqrestore(&dev->lock, flags);
1312
1313 VDBG(dev, "<--- %s()\n", __func__);
1314 return 0;
1315}
1316
1317
1318/* device controller usb_gadget_ops structure */
1319static const struct usb_gadget_ops langwell_ops = {
1320
1321 /* returns the current frame number */
1322 .get_frame = langwell_get_frame,
1323
1324 /* tries to wake up the host connected to this gadget */
1325 .wakeup = langwell_wakeup,
1326
1327 /* set the device selfpowered feature, always selfpowered */
1328 /* .set_selfpowered = langwell_set_selfpowered, */
1329
1330 /* notify controller that VBUS is powered or not */
1331 .vbus_session = langwell_vbus_session,
1332
1333 /* constrain controller's VBUS power usage */
1334 .vbus_draw = langwell_vbus_draw,
1335
1336 /* D+ pullup, software-controlled connect/disconnect to USB host */
1337 .pullup = langwell_pullup,
1338};
1339
1340
1341/*-------------------------------------------------------------------------*/
1342
1343/* device controller operations */
1344
1345/* reset device controller */
1346static int langwell_udc_reset(struct langwell_udc *dev)
1347{
1348 u32 usbcmd, usbmode, devlc, endpointlistaddr;
1349 unsigned long timeout;
1350
1351 if (!dev)
1352 return -EINVAL;
1353
1354 DBG(dev, "---> %s()\n", __func__);
1355
1356 /* set controller to stop state */
1357 usbcmd = readl(&dev->op_regs->usbcmd);
1358 usbcmd &= ~CMD_RUNSTOP;
1359 writel(usbcmd, &dev->op_regs->usbcmd);
1360
1361 /* reset device controller */
1362 usbcmd = readl(&dev->op_regs->usbcmd);
1363 usbcmd |= CMD_RST;
1364 writel(usbcmd, &dev->op_regs->usbcmd);
1365
1366 /* wait for reset to complete */
1367 timeout = jiffies + RESET_TIMEOUT;
1368 while (readl(&dev->op_regs->usbcmd) & CMD_RST) {
1369 if (time_after(jiffies, timeout)) {
1370 ERROR(dev, "device reset timeout\n");
1371 return -ETIMEDOUT;
1372 }
1373 cpu_relax();
1374 }
1375
1376 /* set controller to device mode */
1377 usbmode = readl(&dev->op_regs->usbmode);
1378 usbmode |= MODE_DEVICE;
1379
1380 /* turn setup lockout off, require setup tripwire in usbcmd */
1381 usbmode |= MODE_SLOM;
1382
1383 writel(usbmode, &dev->op_regs->usbmode);
1384 usbmode = readl(&dev->op_regs->usbmode);
1385 VDBG(dev, "usbmode=0x%08x\n", usbmode);
1386
1387 /* Write-Clear setup status */
1388 writel(0, &dev->op_regs->usbsts);
1389
1390 /* if support USB LPM, ACK all LPM token */
1391 if (dev->lpm) {
1392 devlc = readl(&dev->op_regs->devlc);
1393 devlc &= ~LPM_STL; /* don't STALL LPM token */
1394 devlc &= ~LPM_NYT_ACK; /* ACK LPM token */
1395 writel(devlc, &dev->op_regs->devlc);
1396 }
1397
1398 /* fill endpointlistaddr register */
1399 endpointlistaddr = dev->ep_dqh_dma;
1400 endpointlistaddr &= ENDPOINTLISTADDR_MASK;
1401 writel(endpointlistaddr, &dev->op_regs->endpointlistaddr);
1402
1403 VDBG(dev, "dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n",
1404 dev->ep_dqh, endpointlistaddr,
1405 readl(&dev->op_regs->endpointlistaddr));
1406 DBG(dev, "<--- %s()\n", __func__);
1407 return 0;
1408}
1409
1410
1411/* reinitialize device controller endpoints */
1412static int eps_reinit(struct langwell_udc *dev)
1413{
1414 struct langwell_ep *ep;
1415 char name[14];
1416 int i;
1417
1418 VDBG(dev, "---> %s()\n", __func__);
1419
1420 /* initialize ep0 */
1421 ep = &dev->ep[0];
1422 ep->dev = dev;
1423 strncpy(ep->name, "ep0", sizeof(ep->name));
1424 ep->ep.name = ep->name;
1425 ep->ep.ops = &langwell_ep_ops;
1426 ep->stopped = 0;
1427 ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
1428 ep->ep_num = 0;
1429 ep->desc = &langwell_ep0_desc;
1430 INIT_LIST_HEAD(&ep->queue);
1431
1432 ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
1433
1434 /* initialize other endpoints */
1435 for (i = 2; i < dev->ep_max; i++) {
1436 ep = &dev->ep[i];
1437 if (i % 2)
1438 snprintf(name, sizeof(name), "ep%din", i / 2);
1439 else
1440 snprintf(name, sizeof(name), "ep%dout", i / 2);
1441 ep->dev = dev;
1442 strncpy(ep->name, name, sizeof(ep->name));
1443 ep->ep.name = ep->name;
1444
1445 ep->ep.ops = &langwell_ep_ops;
1446 ep->stopped = 0;
1447 ep->ep.maxpacket = (unsigned short) ~0;
1448 ep->ep_num = i / 2;
1449
1450 INIT_LIST_HEAD(&ep->queue);
1451 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1452
1453 ep->dqh = &dev->ep_dqh[i];
1454 }
1455
1456 VDBG(dev, "<--- %s()\n", __func__);
1457 return 0;
1458}
1459
1460
1461/* enable interrupt and set controller to run state */
1462static void langwell_udc_start(struct langwell_udc *dev)
1463{
1464 u32 usbintr, usbcmd;
1465 DBG(dev, "---> %s()\n", __func__);
1466
1467 /* enable interrupts */
1468 usbintr = INTR_ULPIE /* ULPI */
1469 | INTR_SLE /* suspend */
1470 /* | INTR_SRE SOF received */
1471 | INTR_URE /* USB reset */
1472 | INTR_AAE /* async advance */
1473 | INTR_SEE /* system error */
1474 | INTR_FRE /* frame list rollover */
1475 | INTR_PCE /* port change detect */
1476 | INTR_UEE /* USB error interrupt */
1477 | INTR_UE; /* USB interrupt */
1478 writel(usbintr, &dev->op_regs->usbintr);
1479
1480 /* clear stopped bit */
1481 dev->stopped = 0;
1482
1483 /* set controller to run */
1484 usbcmd = readl(&dev->op_regs->usbcmd);
1485 usbcmd |= CMD_RUNSTOP;
1486 writel(usbcmd, &dev->op_regs->usbcmd);
1487
1488 DBG(dev, "<--- %s()\n", __func__);
1489 return;
1490}
1491
1492
1493/* disable interrupt and set controller to stop state */
1494static void langwell_udc_stop(struct langwell_udc *dev)
1495{
1496 u32 usbcmd;
1497
1498 DBG(dev, "---> %s()\n", __func__);
1499
1500 /* disable all interrupts */
1501 writel(0, &dev->op_regs->usbintr);
1502
1503 /* set stopped bit */
1504 dev->stopped = 1;
1505
1506 /* set controller to stop state */
1507 usbcmd = readl(&dev->op_regs->usbcmd);
1508 usbcmd &= ~CMD_RUNSTOP;
1509 writel(usbcmd, &dev->op_regs->usbcmd);
1510
1511 DBG(dev, "<--- %s()\n", __func__);
1512 return;
1513}
1514
1515
1516/* stop all USB activities */
1517static void stop_activity(struct langwell_udc *dev,
1518 struct usb_gadget_driver *driver)
1519{
1520 struct langwell_ep *ep;
1521 DBG(dev, "---> %s()\n", __func__);
1522
1523 nuke(&dev->ep[0], -ESHUTDOWN);
1524
1525 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1526 nuke(ep, -ESHUTDOWN);
1527 }
1528
1529 /* report disconnect; the driver is already quiesced */
1530 if (driver) {
1531 spin_unlock(&dev->lock);
1532 driver->disconnect(&dev->gadget);
1533 spin_lock(&dev->lock);
1534 }
1535
1536 DBG(dev, "<--- %s()\n", __func__);
1537}
1538
1539
1540/*-------------------------------------------------------------------------*/
1541
1542/* device "function" sysfs attribute file */
1543static ssize_t show_function(struct device *_dev,
1544 struct device_attribute *attr, char *buf)
1545{
1546 struct langwell_udc *dev = the_controller;
1547
1548 if (!dev->driver || !dev->driver->function
1549 || strlen(dev->driver->function) > PAGE_SIZE)
1550 return 0;
1551
1552 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
1553}
1554static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
1555
1556
1557/* device "langwell_udc" sysfs attribute file */
1558static ssize_t show_langwell_udc(struct device *_dev,
1559 struct device_attribute *attr, char *buf)
1560{
1561 struct langwell_udc *dev = the_controller;
1562 struct langwell_request *req;
1563 struct langwell_ep *ep = NULL;
1564 char *next;
1565 unsigned size;
1566 unsigned t;
1567 unsigned i;
1568 unsigned long flags;
1569 u32 tmp_reg;
1570
1571 next = buf;
1572 size = PAGE_SIZE;
1573 spin_lock_irqsave(&dev->lock, flags);
1574
1575 /* driver basic information */
1576 t = scnprintf(next, size,
1577 DRIVER_DESC "\n"
1578 "%s version: %s\n"
1579 "Gadget driver: %s\n\n",
1580 driver_name, DRIVER_VERSION,
1581 dev->driver ? dev->driver->driver.name : "(none)");
1582 size -= t;
1583 next += t;
1584
1585 /* device registers */
1586 tmp_reg = readl(&dev->op_regs->usbcmd);
1587 t = scnprintf(next, size,
1588 "USBCMD reg:\n"
1589 "SetupTW: %d\n"
1590 "Run/Stop: %s\n\n",
1591 (tmp_reg & CMD_SUTW) ? 1 : 0,
1592 (tmp_reg & CMD_RUNSTOP) ? "Run" : "Stop");
1593 size -= t;
1594 next += t;
1595
1596 tmp_reg = readl(&dev->op_regs->usbsts);
1597 t = scnprintf(next, size,
1598 "USB Status Reg:\n"
1599 "Device Suspend: %d\n"
1600 "Reset Received: %d\n"
1601 "System Error: %s\n"
1602 "USB Error Interrupt: %s\n\n",
1603 (tmp_reg & STS_SLI) ? 1 : 0,
1604 (tmp_reg & STS_URI) ? 1 : 0,
1605 (tmp_reg & STS_SEI) ? "Error" : "No error",
1606 (tmp_reg & STS_UEI) ? "Error detected" : "No error");
1607 size -= t;
1608 next += t;
1609
1610 tmp_reg = readl(&dev->op_regs->usbintr);
1611 t = scnprintf(next, size,
1612 "USB Intrrupt Enable Reg:\n"
1613 "Sleep Enable: %d\n"
1614 "SOF Received Enable: %d\n"
1615 "Reset Enable: %d\n"
1616 "System Error Enable: %d\n"
1617 "Port Change Dectected Enable: %d\n"
1618 "USB Error Intr Enable: %d\n"
1619 "USB Intr Enable: %d\n\n",
1620 (tmp_reg & INTR_SLE) ? 1 : 0,
1621 (tmp_reg & INTR_SRE) ? 1 : 0,
1622 (tmp_reg & INTR_URE) ? 1 : 0,
1623 (tmp_reg & INTR_SEE) ? 1 : 0,
1624 (tmp_reg & INTR_PCE) ? 1 : 0,
1625 (tmp_reg & INTR_UEE) ? 1 : 0,
1626 (tmp_reg & INTR_UE) ? 1 : 0);
1627 size -= t;
1628 next += t;
1629
1630 tmp_reg = readl(&dev->op_regs->frindex);
1631 t = scnprintf(next, size,
1632 "USB Frame Index Reg:\n"
1633 "Frame Number is 0x%08x\n\n",
1634 (tmp_reg & FRINDEX_MASK));
1635 size -= t;
1636 next += t;
1637
1638 tmp_reg = readl(&dev->op_regs->deviceaddr);
1639 t = scnprintf(next, size,
1640 "USB Device Address Reg:\n"
1641 "Device Addr is 0x%x\n\n",
1642 USBADR(tmp_reg));
1643 size -= t;
1644 next += t;
1645
1646 tmp_reg = readl(&dev->op_regs->endpointlistaddr);
1647 t = scnprintf(next, size,
1648 "USB Endpoint List Address Reg:\n"
1649 "Endpoint List Pointer is 0x%x\n\n",
1650 EPBASE(tmp_reg));
1651 size -= t;
1652 next += t;
1653
1654 tmp_reg = readl(&dev->op_regs->portsc1);
1655 t = scnprintf(next, size,
1656 "USB Port Status & Control Reg:\n"
1657 "Port Reset: %s\n"
1658 "Port Suspend Mode: %s\n"
1659 "Over-current Change: %s\n"
1660 "Port Enable/Disable Change: %s\n"
1661 "Port Enabled/Disabled: %s\n"
1662 "Current Connect Status: %s\n\n",
1663 (tmp_reg & PORTS_PR) ? "Reset" : "Not Reset",
1664 (tmp_reg & PORTS_SUSP) ? "Suspend " : "Not Suspend",
1665 (tmp_reg & PORTS_OCC) ? "Detected" : "No",
1666 (tmp_reg & PORTS_PEC) ? "Changed" : "Not Changed",
1667 (tmp_reg & PORTS_PE) ? "Enable" : "Not Correct",
1668 (tmp_reg & PORTS_CCS) ? "Attached" : "Not Attached");
1669 size -= t;
1670 next += t;
1671
1672 tmp_reg = readl(&dev->op_regs->devlc);
1673 t = scnprintf(next, size,
1674 "Device LPM Control Reg:\n"
1675 "Parallel Transceiver : %d\n"
1676 "Serial Transceiver : %d\n"
1677 "Port Speed: %s\n"
1678 "Port Force Full Speed Connenct: %s\n"
1679 "PHY Low Power Suspend Clock Disable: %s\n"
1680 "BmAttributes: %d\n\n",
1681 LPM_PTS(tmp_reg),
1682 (tmp_reg & LPM_STS) ? 1 : 0,
1683 ({
1684 char *s;
1685 switch (LPM_PSPD(tmp_reg)) {
1686 case LPM_SPEED_FULL:
1687 s = "Full Speed"; break;
1688 case LPM_SPEED_LOW:
1689 s = "Low Speed"; break;
1690 case LPM_SPEED_HIGH:
1691 s = "High Speed"; break;
1692 default:
1693 s = "Unknown Speed"; break;
1694 }
1695 s;
1696 }),
1697 (tmp_reg & LPM_PFSC) ? "Force Full Speed" : "Not Force",
1698 (tmp_reg & LPM_PHCD) ? "Disabled" : "Enabled",
1699 LPM_BA(tmp_reg));
1700 size -= t;
1701 next += t;
1702
1703 tmp_reg = readl(&dev->op_regs->usbmode);
1704 t = scnprintf(next, size,
1705 "USB Mode Reg:\n"
1706 "Controller Mode is : %s\n\n", ({
1707 char *s;
1708 switch (MODE_CM(tmp_reg)) {
1709 case MODE_IDLE:
1710 s = "Idle"; break;
1711 case MODE_DEVICE:
1712 s = "Device Controller"; break;
1713 case MODE_HOST:
1714 s = "Host Controller"; break;
1715 default:
1716 s = "None"; break;
1717 }
1718 s;
1719 }));
1720 size -= t;
1721 next += t;
1722
1723 tmp_reg = readl(&dev->op_regs->endptsetupstat);
1724 t = scnprintf(next, size,
1725 "Endpoint Setup Status Reg:\n"
1726 "SETUP on ep 0x%04x\n\n",
1727 tmp_reg & SETUPSTAT_MASK);
1728 size -= t;
1729 next += t;
1730
1731 for (i = 0; i < dev->ep_max / 2; i++) {
1732 tmp_reg = readl(&dev->op_regs->endptctrl[i]);
1733 t = scnprintf(next, size, "EP Ctrl Reg [%d]: 0x%08x\n",
1734 i, tmp_reg);
1735 size -= t;
1736 next += t;
1737 }
1738 tmp_reg = readl(&dev->op_regs->endptprime);
1739 t = scnprintf(next, size, "EP Prime Reg: 0x%08x\n\n", tmp_reg);
1740 size -= t;
1741 next += t;
1742
1743 /* langwell_udc, langwell_ep, langwell_request structure information */
1744 ep = &dev->ep[0];
1745 t = scnprintf(next, size, "%s MaxPacketSize: 0x%x, ep_num: %d\n",
1746 ep->ep.name, ep->ep.maxpacket, ep->ep_num);
1747 size -= t;
1748 next += t;
1749
1750 if (list_empty(&ep->queue)) {
1751 t = scnprintf(next, size, "its req queue is empty\n\n");
1752 size -= t;
1753 next += t;
1754 } else {
1755 list_for_each_entry(req, &ep->queue, queue) {
1756 t = scnprintf(next, size,
1757 "req %p actual 0x%x length 0x%x buf %p\n",
1758 &req->req, req->req.actual,
1759 req->req.length, req->req.buf);
1760 size -= t;
1761 next += t;
1762 }
1763 }
1764 /* other gadget->eplist ep */
1765 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
1766 if (ep->desc) {
1767 t = scnprintf(next, size,
1768 "\n%s MaxPacketSize: 0x%x, "
1769 "ep_num: %d\n",
1770 ep->ep.name, ep->ep.maxpacket,
1771 ep->ep_num);
1772 size -= t;
1773 next += t;
1774
1775 if (list_empty(&ep->queue)) {
1776 t = scnprintf(next, size,
1777 "its req queue is empty\n\n");
1778 size -= t;
1779 next += t;
1780 } else {
1781 list_for_each_entry(req, &ep->queue, queue) {
1782 t = scnprintf(next, size,
1783 "req %p actual 0x%x length "
1784 "0x%x buf %p\n",
1785 &req->req, req->req.actual,
1786 req->req.length, req->req.buf);
1787 size -= t;
1788 next += t;
1789 }
1790 }
1791 }
1792 }
1793
1794 spin_unlock_irqrestore(&dev->lock, flags);
1795 return PAGE_SIZE - size;
1796}
1797static DEVICE_ATTR(langwell_udc, S_IRUGO, show_langwell_udc, NULL);
1798
1799
1800/*-------------------------------------------------------------------------*/
1801
1802/*
1803 * when a driver is successfully registered, it will receive
1804 * control requests including set_configuration(), which enables
1805 * non-control requests. then usb traffic follows until a
1806 * disconnect is reported. then a host may connect again, or
1807 * the driver might get unbound.
1808 */
1809
1810int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1811{
1812 struct langwell_udc *dev = the_controller;
1813 unsigned long flags;
1814 int retval;
1815
1816 if (!dev)
1817 return -ENODEV;
1818
1819 DBG(dev, "---> %s()\n", __func__);
1820
1821 if (dev->driver)
1822 return -EBUSY;
1823
1824 spin_lock_irqsave(&dev->lock, flags);
1825
1826 /* hook up the driver ... */
1827 driver->driver.bus = NULL;
1828 dev->driver = driver;
1829 dev->gadget.dev.driver = &driver->driver;
1830
1831 spin_unlock_irqrestore(&dev->lock, flags);
1832
1833 retval = driver->bind(&dev->gadget);
1834 if (retval) {
1835 DBG(dev, "bind to driver %s --> %d\n",
1836 driver->driver.name, retval);
1837 dev->driver = NULL;
1838 dev->gadget.dev.driver = NULL;
1839 return retval;
1840 }
1841
1842 retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
1843 if (retval)
1844 goto err_unbind;
1845
1846 dev->usb_state = USB_STATE_ATTACHED;
1847 dev->ep0_state = WAIT_FOR_SETUP;
1848 dev->ep0_dir = USB_DIR_OUT;
1849
1850 /* enable interrupt and set controller to run state */
1851 if (dev->got_irq)
1852 langwell_udc_start(dev);
1853
1854 VDBG(dev, "After langwell_udc_start(), print all registers:\n");
1855#ifdef VERBOSE
1856 print_all_registers(dev);
1857#endif
1858
1859 INFO(dev, "register driver: %s\n", driver->driver.name);
1860 VDBG(dev, "<--- %s()\n", __func__);
1861 return 0;
1862
1863err_unbind:
1864 driver->unbind(&dev->gadget);
1865 dev->gadget.dev.driver = NULL;
1866 dev->driver = NULL;
1867
1868 DBG(dev, "<--- %s()\n", __func__);
1869 return retval;
1870}
1871EXPORT_SYMBOL(usb_gadget_register_driver);
1872
1873
1874/* unregister gadget driver */
1875int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1876{
1877 struct langwell_udc *dev = the_controller;
1878 unsigned long flags;
1879
1880 if (!dev)
1881 return -ENODEV;
1882
1883 DBG(dev, "---> %s()\n", __func__);
1884
1885 if (unlikely(!driver || !driver->bind || !driver->unbind))
1886 return -EINVAL;
1887
1888 /* unbind OTG transceiver */
1889 if (dev->transceiver)
1890 (void)otg_set_peripheral(dev->transceiver, 0);
1891
1892 /* disable interrupt and set controller to stop state */
1893 langwell_udc_stop(dev);
1894
1895 dev->usb_state = USB_STATE_ATTACHED;
1896 dev->ep0_state = WAIT_FOR_SETUP;
1897 dev->ep0_dir = USB_DIR_OUT;
1898
1899 spin_lock_irqsave(&dev->lock, flags);
1900
1901 /* stop all usb activities */
1902 dev->gadget.speed = USB_SPEED_UNKNOWN;
1903 stop_activity(dev, driver);
1904 spin_unlock_irqrestore(&dev->lock, flags);
1905
1906 /* unbind gadget driver */
1907 driver->unbind(&dev->gadget);
1908 dev->gadget.dev.driver = NULL;
1909 dev->driver = NULL;
1910
1911 device_remove_file(&dev->pdev->dev, &dev_attr_function);
1912
1913 INFO(dev, "unregistered driver '%s'\n", driver->driver.name);
1914 DBG(dev, "<--- %s()\n", __func__);
1915 return 0;
1916}
1917EXPORT_SYMBOL(usb_gadget_unregister_driver);
1918
1919
1920/*-------------------------------------------------------------------------*/
1921
1922/*
1923 * setup tripwire is used as a semaphore to ensure that the setup data
1924 * payload is extracted from a dQH without being corrupted
1925 */
1926static void setup_tripwire(struct langwell_udc *dev)
1927{
1928 u32 usbcmd,
1929 endptsetupstat;
1930 unsigned long timeout;
1931 struct langwell_dqh *dqh;
1932
1933 VDBG(dev, "---> %s()\n", __func__);
1934
1935 /* ep0 OUT dQH */
1936 dqh = &dev->ep_dqh[EP_DIR_OUT];
1937
1938 /* Write-Clear endptsetupstat */
1939 endptsetupstat = readl(&dev->op_regs->endptsetupstat);
1940 writel(endptsetupstat, &dev->op_regs->endptsetupstat);
1941
1942 /* wait until endptsetupstat is cleared */
1943 timeout = jiffies + SETUPSTAT_TIMEOUT;
1944 while (readl(&dev->op_regs->endptsetupstat)) {
1945 if (time_after(jiffies, timeout)) {
1946 ERROR(dev, "setup_tripwire timeout\n");
1947 break;
1948 }
1949 cpu_relax();
1950 }
1951
1952 /* while a hazard exists when setup packet arrives */
1953 do {
1954 /* set setup tripwire bit */
1955 usbcmd = readl(&dev->op_regs->usbcmd);
1956 writel(usbcmd | CMD_SUTW, &dev->op_regs->usbcmd);
1957
1958 /* copy the setup packet to local buffer */
1959 memcpy(&dev->local_setup_buff, &dqh->dqh_setup, 8);
1960 } while (!(readl(&dev->op_regs->usbcmd) & CMD_SUTW));
1961
1962 /* Write-Clear setup tripwire bit */
1963 usbcmd = readl(&dev->op_regs->usbcmd);
1964 writel(usbcmd & ~CMD_SUTW, &dev->op_regs->usbcmd);
1965
1966 VDBG(dev, "<--- %s()\n", __func__);
1967}
1968
1969
1970/* protocol ep0 stall, will automatically be cleared on new transaction */
1971static void ep0_stall(struct langwell_udc *dev)
1972{
1973 u32 endptctrl;
1974
1975 VDBG(dev, "---> %s()\n", __func__);
1976
1977 /* set TX and RX to stall */
1978 endptctrl = readl(&dev->op_regs->endptctrl[0]);
1979 endptctrl |= EPCTRL_TXS | EPCTRL_RXS;
1980 writel(endptctrl, &dev->op_regs->endptctrl[0]);
1981
1982 /* update ep0 state */
1983 dev->ep0_state = WAIT_FOR_SETUP;
1984 dev->ep0_dir = USB_DIR_OUT;
1985
1986 VDBG(dev, "<--- %s()\n", __func__);
1987}
1988
1989
1990/* PRIME a status phase for ep0 */
1991static int prime_status_phase(struct langwell_udc *dev, int dir)
1992{
1993 struct langwell_request *req;
1994 struct langwell_ep *ep;
1995 int status = 0;
1996
1997 VDBG(dev, "---> %s()\n", __func__);
1998
1999 if (dir == EP_DIR_IN)
2000 dev->ep0_dir = USB_DIR_IN;
2001 else
2002 dev->ep0_dir = USB_DIR_OUT;
2003
2004 ep = &dev->ep[0];
2005 dev->ep0_state = WAIT_FOR_OUT_STATUS;
2006
2007 req = dev->status_req;
2008
2009 req->ep = ep;
2010 req->req.length = 0;
2011 req->req.status = -EINPROGRESS;
2012 req->req.actual = 0;
2013 req->req.complete = NULL;
2014 req->dtd_count = 0;
2015
2016 if (!req_to_dtd(req))
2017 status = queue_dtd(ep, req);
2018 else
2019 return -ENOMEM;
2020
2021 if (status)
2022 ERROR(dev, "can't queue ep0 status request\n");
2023
2024 list_add_tail(&req->queue, &ep->queue);
2025
2026 VDBG(dev, "<--- %s()\n", __func__);
2027 return status;
2028}
2029
2030
2031/* SET_ADDRESS request routine */
2032static void set_address(struct langwell_udc *dev, u16 value,
2033 u16 index, u16 length)
2034{
2035 VDBG(dev, "---> %s()\n", __func__);
2036
2037 /* save the new address to device struct */
2038 dev->dev_addr = (u8) value;
2039 VDBG(dev, "dev->dev_addr = %d\n", dev->dev_addr);
2040
2041 /* update usb state */
2042 dev->usb_state = USB_STATE_ADDRESS;
2043
2044 /* STATUS phase */
2045 if (prime_status_phase(dev, EP_DIR_IN))
2046 ep0_stall(dev);
2047
2048 VDBG(dev, "<--- %s()\n", __func__);
2049}
2050
2051
2052/* return endpoint by windex */
2053static struct langwell_ep *get_ep_by_windex(struct langwell_udc *dev,
2054 u16 wIndex)
2055{
2056 struct langwell_ep *ep;
2057 VDBG(dev, "---> %s()\n", __func__);
2058
2059 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2060 return &dev->ep[0];
2061
2062 list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
2063 u8 bEndpointAddress;
2064 if (!ep->desc)
2065 continue;
2066
2067 bEndpointAddress = ep->desc->bEndpointAddress;
2068 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2069 continue;
2070
2071 if ((wIndex & USB_ENDPOINT_NUMBER_MASK)
2072 == (bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
2073 return ep;
2074 }
2075
2076 VDBG(dev, "<--- %s()\n", __func__);
2077 return NULL;
2078}
2079
2080
2081/* return whether endpoint is stalled, 0: not stalled; 1: stalled */
2082static int ep_is_stall(struct langwell_ep *ep)
2083{
2084 struct langwell_udc *dev = ep->dev;
2085 u32 endptctrl;
2086 int retval;
2087
2088 VDBG(dev, "---> %s()\n", __func__);
2089
2090 endptctrl = readl(&dev->op_regs->endptctrl[ep->ep_num]);
2091 if (is_in(ep))
2092 retval = endptctrl & EPCTRL_TXS ? 1 : 0;
2093 else
2094 retval = endptctrl & EPCTRL_RXS ? 1 : 0;
2095
2096 VDBG(dev, "<--- %s()\n", __func__);
2097 return retval;
2098}
2099
2100
2101/* GET_STATUS request routine */
2102static void get_status(struct langwell_udc *dev, u8 request_type, u16 value,
2103 u16 index, u16 length)
2104{
2105 struct langwell_request *req;
2106 struct langwell_ep *ep;
2107 u16 status_data = 0; /* 16 bits cpu view status data */
2108 int status = 0;
2109
2110 VDBG(dev, "---> %s()\n", __func__);
2111
2112 ep = &dev->ep[0];
2113
2114 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
2115 /* get device status */
2116 status_data = 1 << USB_DEVICE_SELF_POWERED;
2117 status_data |= dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
2118 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
2119 /* get interface status */
2120 status_data = 0;
2121 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
2122 /* get endpoint status */
2123 struct langwell_ep *epn;
2124 epn = get_ep_by_windex(dev, index);
2125 /* stall if endpoint doesn't exist */
2126 if (!epn)
2127 goto stall;
2128
2129 status_data = ep_is_stall(epn) << USB_ENDPOINT_HALT;
2130 }
2131
2132 dev->ep0_dir = USB_DIR_IN;
2133
2134 /* borrow the per device status_req */
2135 req = dev->status_req;
2136
2137 /* fill in the reqest structure */
2138 *((u16 *) req->req.buf) = cpu_to_le16(status_data);
2139 req->ep = ep;
2140 req->req.length = 2;
2141 req->req.status = -EINPROGRESS;
2142 req->req.actual = 0;
2143 req->req.complete = NULL;
2144 req->dtd_count = 0;
2145
2146 /* prime the data phase */
2147 if (!req_to_dtd(req))
2148 status = queue_dtd(ep, req);
2149 else /* no mem */
2150 goto stall;
2151
2152 if (status) {
2153 ERROR(dev, "response error on GET_STATUS request\n");
2154 goto stall;
2155 }
2156
2157 list_add_tail(&req->queue, &ep->queue);
2158 dev->ep0_state = DATA_STATE_XMIT;
2159
2160 VDBG(dev, "<--- %s()\n", __func__);
2161 return;
2162stall:
2163 ep0_stall(dev);
2164 VDBG(dev, "<--- %s()\n", __func__);
2165}
2166
2167
2168/* setup packet interrupt handler */
2169static void handle_setup_packet(struct langwell_udc *dev,
2170 struct usb_ctrlrequest *setup)
2171{
2172 u16 wValue = le16_to_cpu(setup->wValue);
2173 u16 wIndex = le16_to_cpu(setup->wIndex);
2174 u16 wLength = le16_to_cpu(setup->wLength);
2175
2176 VDBG(dev, "---> %s()\n", __func__);
2177
2178 /* ep0 fifo flush */
2179 nuke(&dev->ep[0], -ESHUTDOWN);
2180
2181 DBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
2182 setup->bRequestType, setup->bRequest,
2183 wValue, wIndex, wLength);
2184
2185 /* RNDIS gadget delegate */
2186 if ((setup->bRequestType == 0x21) && (setup->bRequest == 0x00)) {
2187 /* USB_CDC_SEND_ENCAPSULATED_COMMAND */
2188 goto delegate;
2189 }
2190
2191 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2192 if ((setup->bRequestType == 0xa1) && (setup->bRequest == 0x01)) {
2193 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2194 goto delegate;
2195 }
2196
2197 /* We process some stardard setup requests here */
2198 switch (setup->bRequest) {
2199 case USB_REQ_GET_STATUS:
2200 DBG(dev, "SETUP: USB_REQ_GET_STATUS\n");
2201 /* get status, DATA and STATUS phase */
2202 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2203 != (USB_DIR_IN | USB_TYPE_STANDARD))
2204 break;
2205 get_status(dev, setup->bRequestType, wValue, wIndex, wLength);
2206 goto end;
2207
2208 case USB_REQ_SET_ADDRESS:
2209 DBG(dev, "SETUP: USB_REQ_SET_ADDRESS\n");
2210 /* STATUS phase */
2211 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
2212 | USB_RECIP_DEVICE))
2213 break;
2214 set_address(dev, wValue, wIndex, wLength);
2215 goto end;
2216
2217 case USB_REQ_CLEAR_FEATURE:
2218 case USB_REQ_SET_FEATURE:
2219 /* STATUS phase */
2220 {
2221 int rc = -EOPNOTSUPP;
2222 if (setup->bRequest == USB_REQ_SET_FEATURE)
2223 DBG(dev, "SETUP: USB_REQ_SET_FEATURE\n");
2224 else if (setup->bRequest == USB_REQ_CLEAR_FEATURE)
2225 DBG(dev, "SETUP: USB_REQ_CLEAR_FEATURE\n");
2226
2227 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
2228 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
2229 struct langwell_ep *epn;
2230 epn = get_ep_by_windex(dev, wIndex);
2231 /* stall if endpoint doesn't exist */
2232 if (!epn) {
2233 ep0_stall(dev);
2234 goto end;
2235 }
2236
2237 if (wValue != 0 || wLength != 0
2238 || epn->ep_num > dev->ep_max)
2239 break;
2240
2241 spin_unlock(&dev->lock);
2242 rc = langwell_ep_set_halt(&epn->ep,
2243 (setup->bRequest == USB_REQ_SET_FEATURE)
2244 ? 1 : 0);
2245 spin_lock(&dev->lock);
2246
2247 } else if ((setup->bRequestType & (USB_RECIP_MASK
2248 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
2249 | USB_TYPE_STANDARD)) {
2250 if (!gadget_is_otg(&dev->gadget))
2251 break;
2252 else if (setup->bRequest == USB_DEVICE_B_HNP_ENABLE) {
2253 dev->gadget.b_hnp_enable = 1;
2254#ifdef OTG_TRANSCEIVER
2255 if (!dev->lotg->otg.default_a)
2256 dev->lotg->hsm.b_hnp_enable = 1;
2257#endif
2258 } else if (setup->bRequest == USB_DEVICE_A_HNP_SUPPORT)
2259 dev->gadget.a_hnp_support = 1;
2260 else if (setup->bRequest ==
2261 USB_DEVICE_A_ALT_HNP_SUPPORT)
2262 dev->gadget.a_alt_hnp_support = 1;
2263 else
2264 break;
2265 rc = 0;
2266 } else
2267 break;
2268
2269 if (rc == 0) {
2270 if (prime_status_phase(dev, EP_DIR_IN))
2271 ep0_stall(dev);
2272 }
2273 goto end;
2274 }
2275
2276 case USB_REQ_GET_DESCRIPTOR:
2277 DBG(dev, "SETUP: USB_REQ_GET_DESCRIPTOR\n");
2278 goto delegate;
2279
2280 case USB_REQ_SET_DESCRIPTOR:
2281 DBG(dev, "SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n");
2282 goto delegate;
2283
2284 case USB_REQ_GET_CONFIGURATION:
2285 DBG(dev, "SETUP: USB_REQ_GET_CONFIGURATION\n");
2286 goto delegate;
2287
2288 case USB_REQ_SET_CONFIGURATION:
2289 DBG(dev, "SETUP: USB_REQ_SET_CONFIGURATION\n");
2290 goto delegate;
2291
2292 case USB_REQ_GET_INTERFACE:
2293 DBG(dev, "SETUP: USB_REQ_GET_INTERFACE\n");
2294 goto delegate;
2295
2296 case USB_REQ_SET_INTERFACE:
2297 DBG(dev, "SETUP: USB_REQ_SET_INTERFACE\n");
2298 goto delegate;
2299
2300 case USB_REQ_SYNCH_FRAME:
2301 DBG(dev, "SETUP: USB_REQ_SYNCH_FRAME unsupported\n");
2302 goto delegate;
2303
2304 default:
2305 /* delegate USB standard requests to the gadget driver */
2306 goto delegate;
2307delegate:
2308 /* USB requests handled by gadget */
2309 if (wLength) {
2310 /* DATA phase from gadget, STATUS phase from udc */
2311 dev->ep0_dir = (setup->bRequestType & USB_DIR_IN)
2312 ? USB_DIR_IN : USB_DIR_OUT;
2313 VDBG(dev, "dev->ep0_dir = 0x%x, wLength = %d\n",
2314 dev->ep0_dir, wLength);
2315 spin_unlock(&dev->lock);
2316 if (dev->driver->setup(&dev->gadget,
2317 &dev->local_setup_buff) < 0)
2318 ep0_stall(dev);
2319 spin_lock(&dev->lock);
2320 dev->ep0_state = (setup->bRequestType & USB_DIR_IN)
2321 ? DATA_STATE_XMIT : DATA_STATE_RECV;
2322 } else {
2323 /* no DATA phase, IN STATUS phase from gadget */
2324 dev->ep0_dir = USB_DIR_IN;
2325 VDBG(dev, "dev->ep0_dir = 0x%x, wLength = %d\n",
2326 dev->ep0_dir, wLength);
2327 spin_unlock(&dev->lock);
2328 if (dev->driver->setup(&dev->gadget,
2329 &dev->local_setup_buff) < 0)
2330 ep0_stall(dev);
2331 spin_lock(&dev->lock);
2332 dev->ep0_state = WAIT_FOR_OUT_STATUS;
2333 }
2334 break;
2335 }
2336end:
2337 VDBG(dev, "<--- %s()\n", __func__);
2338 return;
2339}
2340
2341
2342/* transfer completion, process endpoint request and free the completed dTDs
2343 * for this request
2344 */
2345static int process_ep_req(struct langwell_udc *dev, int index,
2346 struct langwell_request *curr_req)
2347{
2348 struct langwell_dtd *curr_dtd;
2349 struct langwell_dqh *curr_dqh;
2350 int td_complete, actual, remaining_length;
2351 int i, dir;
2352 u8 dtd_status = 0;
2353 int retval = 0;
2354
2355 curr_dqh = &dev->ep_dqh[index];
2356 dir = index % 2;
2357
2358 curr_dtd = curr_req->head;
2359 td_complete = 0;
2360 actual = curr_req->req.length;
2361
2362 VDBG(dev, "---> %s()\n", __func__);
2363
2364 for (i = 0; i < curr_req->dtd_count; i++) {
2365 remaining_length = le16_to_cpu(curr_dtd->dtd_total);
2366 actual -= remaining_length;
2367
2368 /* command execution states by dTD */
2369 dtd_status = curr_dtd->dtd_status;
2370
2371 if (!dtd_status) {
2372 /* transfers completed successfully */
2373 if (!remaining_length) {
2374 td_complete++;
2375 VDBG(dev, "dTD transmitted successfully\n");
2376 } else {
2377 if (dir) {
2378 VDBG(dev, "TX dTD remains data\n");
2379 retval = -EPROTO;
2380 break;
2381
2382 } else {
2383 td_complete++;
2384 break;
2385 }
2386 }
2387 } else {
2388 /* transfers completed with errors */
2389 if (dtd_status & DTD_STS_ACTIVE) {
2390 DBG(dev, "request not completed\n");
2391 retval = 1;
2392 return retval;
2393 } else if (dtd_status & DTD_STS_HALTED) {
2394 ERROR(dev, "dTD error %08x dQH[%d]\n",
2395 dtd_status, index);
2396 /* clear the errors and halt condition */
2397 curr_dqh->dtd_status = 0;
2398 retval = -EPIPE;
2399 break;
2400 } else if (dtd_status & DTD_STS_DBE) {
2401 DBG(dev, "data buffer (overflow) error\n");
2402 retval = -EPROTO;
2403 break;
2404 } else if (dtd_status & DTD_STS_TRE) {
2405 DBG(dev, "transaction(ISO) error\n");
2406 retval = -EILSEQ;
2407 break;
2408 } else
2409 ERROR(dev, "unknown error (0x%x)!\n",
2410 dtd_status);
2411 }
2412
2413 if (i != curr_req->dtd_count - 1)
2414 curr_dtd = (struct langwell_dtd *)
2415 curr_dtd->next_dtd_virt;
2416 }
2417
2418 if (retval)
2419 return retval;
2420
2421 curr_req->req.actual = actual;
2422
2423 VDBG(dev, "<--- %s()\n", __func__);
2424 return 0;
2425}
2426
2427
2428/* complete DATA or STATUS phase of ep0 prime status phase if needed */
2429static void ep0_req_complete(struct langwell_udc *dev,
2430 struct langwell_ep *ep0, struct langwell_request *req)
2431{
2432 u32 new_addr;
2433 VDBG(dev, "---> %s()\n", __func__);
2434
2435 if (dev->usb_state == USB_STATE_ADDRESS) {
2436 /* set the new address */
2437 new_addr = (u32)dev->dev_addr;
2438 writel(new_addr << USBADR_SHIFT, &dev->op_regs->deviceaddr);
2439
2440 new_addr = USBADR(readl(&dev->op_regs->deviceaddr));
2441 VDBG(dev, "new_addr = %d\n", new_addr);
2442 }
2443
2444 done(ep0, req, 0);
2445
2446 switch (dev->ep0_state) {
2447 case DATA_STATE_XMIT:
2448 /* receive status phase */
2449 if (prime_status_phase(dev, EP_DIR_OUT))
2450 ep0_stall(dev);
2451 break;
2452 case DATA_STATE_RECV:
2453 /* send status phase */
2454 if (prime_status_phase(dev, EP_DIR_IN))
2455 ep0_stall(dev);
2456 break;
2457 case WAIT_FOR_OUT_STATUS:
2458 dev->ep0_state = WAIT_FOR_SETUP;
2459 break;
2460 case WAIT_FOR_SETUP:
2461 ERROR(dev, "unexpect ep0 packets\n");
2462 break;
2463 default:
2464 ep0_stall(dev);
2465 break;
2466 }
2467
2468 VDBG(dev, "<--- %s()\n", __func__);
2469}
2470
2471
2472/* USB transfer completion interrupt */
2473static void handle_trans_complete(struct langwell_udc *dev)
2474{
2475 u32 complete_bits;
2476 int i, ep_num, dir, bit_mask, status;
2477 struct langwell_ep *epn;
2478 struct langwell_request *curr_req, *temp_req;
2479
2480 VDBG(dev, "---> %s()\n", __func__);
2481
2482 complete_bits = readl(&dev->op_regs->endptcomplete);
2483 VDBG(dev, "endptcomplete register: 0x%08x\n", complete_bits);
2484
2485 /* Write-Clear the bits in endptcomplete register */
2486 writel(complete_bits, &dev->op_regs->endptcomplete);
2487
2488 if (!complete_bits) {
2489 DBG(dev, "complete_bits = 0\n");
2490 goto done;
2491 }
2492
2493 for (i = 0; i < dev->ep_max; i++) {
2494 ep_num = i / 2;
2495 dir = i % 2;
2496
2497 bit_mask = 1 << (ep_num + 16 * dir);
2498
2499 if (!(complete_bits & bit_mask))
2500 continue;
2501
2502 /* ep0 */
2503 if (i == 1)
2504 epn = &dev->ep[0];
2505 else
2506 epn = &dev->ep[i];
2507
2508 if (epn->name == NULL) {
2509 WARNING(dev, "invalid endpoint\n");
2510 continue;
2511 }
2512
2513 if (i < 2)
2514 /* ep0 in and out */
2515 DBG(dev, "%s-%s transfer completed\n",
2516 epn->name,
2517 is_in(epn) ? "in" : "out");
2518 else
2519 DBG(dev, "%s transfer completed\n", epn->name);
2520
2521 /* process the req queue until an uncomplete request */
2522 list_for_each_entry_safe(curr_req, temp_req,
2523 &epn->queue, queue) {
2524 status = process_ep_req(dev, i, curr_req);
2525 VDBG(dev, "%s req status: %d\n", epn->name, status);
2526
2527 if (status)
2528 break;
2529
2530 /* write back status to req */
2531 curr_req->req.status = status;
2532
2533 /* ep0 request completion */
2534 if (ep_num == 0) {
2535 ep0_req_complete(dev, epn, curr_req);
2536 break;
2537 } else {
2538 done(epn, curr_req, status);
2539 }
2540 }
2541 }
2542done:
2543 VDBG(dev, "<--- %s()\n", __func__);
2544 return;
2545}
2546
2547
2548/* port change detect interrupt handler */
2549static void handle_port_change(struct langwell_udc *dev)
2550{
2551 u32 portsc1, devlc;
2552 u32 speed;
2553
2554 VDBG(dev, "---> %s()\n", __func__);
2555
2556 if (dev->bus_reset)
2557 dev->bus_reset = 0;
2558
2559 portsc1 = readl(&dev->op_regs->portsc1);
2560 devlc = readl(&dev->op_regs->devlc);
2561 VDBG(dev, "portsc1 = 0x%08x, devlc = 0x%08x\n",
2562 portsc1, devlc);
2563
2564 /* bus reset is finished */
2565 if (!(portsc1 & PORTS_PR)) {
2566 /* get the speed */
2567 speed = LPM_PSPD(devlc);
2568 switch (speed) {
2569 case LPM_SPEED_HIGH:
2570 dev->gadget.speed = USB_SPEED_HIGH;
2571 break;
2572 case LPM_SPEED_FULL:
2573 dev->gadget.speed = USB_SPEED_FULL;
2574 break;
2575 case LPM_SPEED_LOW:
2576 dev->gadget.speed = USB_SPEED_LOW;
2577 break;
2578 default:
2579 dev->gadget.speed = USB_SPEED_UNKNOWN;
2580 break;
2581 }
2582 VDBG(dev, "speed = %d, dev->gadget.speed = %d\n",
2583 speed, dev->gadget.speed);
2584 }
2585
2586 /* LPM L0 to L1 */
2587 if (dev->lpm && dev->lpm_state == LPM_L0)
2588 if (portsc1 & PORTS_SUSP && portsc1 & PORTS_SLP) {
2589 INFO(dev, "LPM L0 to L1\n");
2590 dev->lpm_state = LPM_L1;
2591 }
2592
2593 /* LPM L1 to L0, force resume or remote wakeup finished */
2594 if (dev->lpm && dev->lpm_state == LPM_L1)
2595 if (!(portsc1 & PORTS_SUSP)) {
2596 if (portsc1 & PORTS_SLP)
2597 INFO(dev, "LPM L1 to L0, force resume\n");
2598 else
2599 INFO(dev, "LPM L1 to L0, remote wakeup\n");
2600
2601 dev->lpm_state = LPM_L0;
2602 }
2603
2604 /* update USB state */
2605 if (!dev->resume_state)
2606 dev->usb_state = USB_STATE_DEFAULT;
2607
2608 VDBG(dev, "<--- %s()\n", __func__);
2609}
2610
2611
2612/* USB reset interrupt handler */
2613static void handle_usb_reset(struct langwell_udc *dev)
2614{
2615 u32 deviceaddr,
2616 endptsetupstat,
2617 endptcomplete;
2618 unsigned long timeout;
2619
2620 VDBG(dev, "---> %s()\n", __func__);
2621
2622 /* Write-Clear the device address */
2623 deviceaddr = readl(&dev->op_regs->deviceaddr);
2624 writel(deviceaddr & ~USBADR_MASK, &dev->op_regs->deviceaddr);
2625
2626 dev->dev_addr = 0;
2627
2628 /* clear usb state */
2629 dev->resume_state = 0;
2630
2631 /* LPM L1 to L0, reset */
2632 if (dev->lpm)
2633 dev->lpm_state = LPM_L0;
2634
2635 dev->ep0_dir = USB_DIR_OUT;
2636 dev->ep0_state = WAIT_FOR_SETUP;
2637 dev->remote_wakeup = 0; /* default to 0 on reset */
2638 dev->gadget.b_hnp_enable = 0;
2639 dev->gadget.a_hnp_support = 0;
2640 dev->gadget.a_alt_hnp_support = 0;
2641
2642 /* Write-Clear all the setup token semaphores */
2643 endptsetupstat = readl(&dev->op_regs->endptsetupstat);
2644 writel(endptsetupstat, &dev->op_regs->endptsetupstat);
2645
2646 /* Write-Clear all the endpoint complete status bits */
2647 endptcomplete = readl(&dev->op_regs->endptcomplete);
2648 writel(endptcomplete, &dev->op_regs->endptcomplete);
2649
2650 /* wait until all endptprime bits cleared */
2651 timeout = jiffies + PRIME_TIMEOUT;
2652 while (readl(&dev->op_regs->endptprime)) {
2653 if (time_after(jiffies, timeout)) {
2654 ERROR(dev, "USB reset timeout\n");
2655 break;
2656 }
2657 cpu_relax();
2658 }
2659
2660 /* write 1s to endptflush register to clear any primed buffers */
2661 writel((u32) ~0, &dev->op_regs->endptflush);
2662
2663 if (readl(&dev->op_regs->portsc1) & PORTS_PR) {
2664 VDBG(dev, "USB bus reset\n");
2665 /* bus is reseting */
2666 dev->bus_reset = 1;
2667
2668 /* reset all the queues, stop all USB activities */
2669 stop_activity(dev, dev->driver);
2670 dev->usb_state = USB_STATE_DEFAULT;
2671 } else {
2672 VDBG(dev, "device controller reset\n");
2673 /* controller reset */
2674 langwell_udc_reset(dev);
2675
2676 /* reset all the queues, stop all USB activities */
2677 stop_activity(dev, dev->driver);
2678
2679 /* reset ep0 dQH and endptctrl */
2680 ep0_reset(dev);
2681
2682 /* enable interrupt and set controller to run state */
2683 langwell_udc_start(dev);
2684
2685 dev->usb_state = USB_STATE_ATTACHED;
2686 }
2687
2688#ifdef OTG_TRANSCEIVER
2689 /* refer to USB OTG 6.6.2.3 b_hnp_en is cleared */
2690 if (!dev->lotg->otg.default_a)
2691 dev->lotg->hsm.b_hnp_enable = 0;
2692#endif
2693
2694 VDBG(dev, "<--- %s()\n", __func__);
2695}
2696
2697
2698/* USB bus suspend/resume interrupt */
2699static void handle_bus_suspend(struct langwell_udc *dev)
2700{
2701 u32 devlc;
2702 DBG(dev, "---> %s()\n", __func__);
2703
2704 dev->resume_state = dev->usb_state;
2705 dev->usb_state = USB_STATE_SUSPENDED;
2706
2707#ifdef OTG_TRANSCEIVER
2708 if (dev->lotg->otg.default_a) {
2709 if (dev->lotg->hsm.b_bus_suspend_vld == 1) {
2710 dev->lotg->hsm.b_bus_suspend = 1;
2711 /* notify transceiver the state changes */
2712 if (spin_trylock(&dev->lotg->wq_lock)) {
2713 langwell_update_transceiver();
2714 spin_unlock(&dev->lotg->wq_lock);
2715 }
2716 }
2717 dev->lotg->hsm.b_bus_suspend_vld++;
2718 } else {
2719 if (!dev->lotg->hsm.a_bus_suspend) {
2720 dev->lotg->hsm.a_bus_suspend = 1;
2721 /* notify transceiver the state changes */
2722 if (spin_trylock(&dev->lotg->wq_lock)) {
2723 langwell_update_transceiver();
2724 spin_unlock(&dev->lotg->wq_lock);
2725 }
2726 }
2727 }
2728#endif
2729
2730 /* report suspend to the driver */
2731 if (dev->driver) {
2732 if (dev->driver->suspend) {
2733 spin_unlock(&dev->lock);
2734 dev->driver->suspend(&dev->gadget);
2735 spin_lock(&dev->lock);
2736 DBG(dev, "suspend %s\n", dev->driver->driver.name);
2737 }
2738 }
2739
2740 /* enter PHY low power suspend */
2741 devlc = readl(&dev->op_regs->devlc);
2742 VDBG(dev, "devlc = 0x%08x\n", devlc);
2743 devlc |= LPM_PHCD;
2744 writel(devlc, &dev->op_regs->devlc);
2745
2746 DBG(dev, "<--- %s()\n", __func__);
2747}
2748
2749
2750static void handle_bus_resume(struct langwell_udc *dev)
2751{
2752 u32 devlc;
2753 DBG(dev, "---> %s()\n", __func__);
2754
2755 dev->usb_state = dev->resume_state;
2756 dev->resume_state = 0;
2757
2758 /* exit PHY low power suspend */
2759 devlc = readl(&dev->op_regs->devlc);
2760 VDBG(dev, "devlc = 0x%08x\n", devlc);
2761 devlc &= ~LPM_PHCD;
2762 writel(devlc, &dev->op_regs->devlc);
2763
2764#ifdef OTG_TRANSCEIVER
2765 if (dev->lotg->otg.default_a == 0)
2766 dev->lotg->hsm.a_bus_suspend = 0;
2767#endif
2768
2769 /* report resume to the driver */
2770 if (dev->driver) {
2771 if (dev->driver->resume) {
2772 spin_unlock(&dev->lock);
2773 dev->driver->resume(&dev->gadget);
2774 spin_lock(&dev->lock);
2775 DBG(dev, "resume %s\n", dev->driver->driver.name);
2776 }
2777 }
2778
2779 DBG(dev, "<--- %s()\n", __func__);
2780}
2781
2782
2783/* USB device controller interrupt handler */
2784static irqreturn_t langwell_irq(int irq, void *_dev)
2785{
2786 struct langwell_udc *dev = _dev;
2787 u32 usbsts,
2788 usbintr,
2789 irq_sts,
2790 portsc1;
2791
2792 VDBG(dev, "---> %s()\n", __func__);
2793
2794 if (dev->stopped) {
2795 VDBG(dev, "handle IRQ_NONE\n");
2796 VDBG(dev, "<--- %s()\n", __func__);
2797 return IRQ_NONE;
2798 }
2799
2800 spin_lock(&dev->lock);
2801
2802 /* USB status */
2803 usbsts = readl(&dev->op_regs->usbsts);
2804
2805 /* USB interrupt enable */
2806 usbintr = readl(&dev->op_regs->usbintr);
2807
2808 irq_sts = usbsts & usbintr;
2809 VDBG(dev, "usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n",
2810 usbsts, usbintr, irq_sts);
2811
2812 if (!irq_sts) {
2813 VDBG(dev, "handle IRQ_NONE\n");
2814 VDBG(dev, "<--- %s()\n", __func__);
2815 spin_unlock(&dev->lock);
2816 return IRQ_NONE;
2817 }
2818
2819 /* Write-Clear interrupt status bits */
2820 writel(irq_sts, &dev->op_regs->usbsts);
2821
2822 /* resume from suspend */
2823 portsc1 = readl(&dev->op_regs->portsc1);
2824 if (dev->usb_state == USB_STATE_SUSPENDED)
2825 if (!(portsc1 & PORTS_SUSP))
2826 handle_bus_resume(dev);
2827
2828 /* USB interrupt */
2829 if (irq_sts & STS_UI) {
2830 VDBG(dev, "USB interrupt\n");
2831
2832 /* setup packet received from ep0 */
2833 if (readl(&dev->op_regs->endptsetupstat)
2834 & EP0SETUPSTAT_MASK) {
2835 VDBG(dev, "USB SETUP packet received interrupt\n");
2836 /* setup tripwire semaphone */
2837 setup_tripwire(dev);
2838 handle_setup_packet(dev, &dev->local_setup_buff);
2839 }
2840
2841 /* USB transfer completion */
2842 if (readl(&dev->op_regs->endptcomplete)) {
2843 VDBG(dev, "USB transfer completion interrupt\n");
2844 handle_trans_complete(dev);
2845 }
2846 }
2847
2848 /* SOF received interrupt (for ISO transfer) */
2849 if (irq_sts & STS_SRI) {
2850 /* FIXME */
2851 /* VDBG(dev, "SOF received interrupt\n"); */
2852 }
2853
2854 /* port change detect interrupt */
2855 if (irq_sts & STS_PCI) {
2856 VDBG(dev, "port change detect interrupt\n");
2857 handle_port_change(dev);
2858 }
2859
2860 /* suspend interrrupt */
2861 if (irq_sts & STS_SLI) {
2862 VDBG(dev, "suspend interrupt\n");
2863 handle_bus_suspend(dev);
2864 }
2865
2866 /* USB reset interrupt */
2867 if (irq_sts & STS_URI) {
2868 VDBG(dev, "USB reset interrupt\n");
2869 handle_usb_reset(dev);
2870 }
2871
2872 /* USB error or system error interrupt */
2873 if (irq_sts & (STS_UEI | STS_SEI)) {
2874 /* FIXME */
2875 WARNING(dev, "error IRQ, irq_sts: %x\n", irq_sts);
2876 }
2877
2878 spin_unlock(&dev->lock);
2879
2880 VDBG(dev, "<--- %s()\n", __func__);
2881 return IRQ_HANDLED;
2882}
2883
2884
2885/*-------------------------------------------------------------------------*/
2886
2887/* release device structure */
2888static void gadget_release(struct device *_dev)
2889{
2890 struct langwell_udc *dev = the_controller;
2891
2892 DBG(dev, "---> %s()\n", __func__);
2893
2894 complete(dev->done);
2895
2896 DBG(dev, "<--- %s()\n", __func__);
2897 kfree(dev);
2898}
2899
2900
2901/* tear down the binding between this driver and the pci device */
2902static void langwell_udc_remove(struct pci_dev *pdev)
2903{
2904 struct langwell_udc *dev = the_controller;
2905
2906 DECLARE_COMPLETION(done);
2907
2908 BUG_ON(dev->driver);
2909 DBG(dev, "---> %s()\n", __func__);
2910
2911 dev->done = &done;
2912
2913 /* free memory allocated in probe */
2914 if (dev->dtd_pool)
2915 dma_pool_destroy(dev->dtd_pool);
2916
2917 if (dev->status_req) {
2918 kfree(dev->status_req->req.buf);
2919 kfree(dev->status_req);
2920 }
2921
2922 if (dev->ep_dqh)
2923 dma_free_coherent(&pdev->dev, dev->ep_dqh_size,
2924 dev->ep_dqh, dev->ep_dqh_dma);
2925
2926 kfree(dev->ep);
2927
2928 /* diable IRQ handler */
2929 if (dev->got_irq)
2930 free_irq(pdev->irq, dev);
2931
2932#ifndef OTG_TRANSCEIVER
2933 if (dev->cap_regs)
2934 iounmap(dev->cap_regs);
2935
2936 if (dev->region)
2937 release_mem_region(pci_resource_start(pdev, 0),
2938 pci_resource_len(pdev, 0));
2939
2940 if (dev->enabled)
2941 pci_disable_device(pdev);
2942#else
2943 if (dev->transceiver) {
2944 otg_put_transceiver(dev->transceiver);
2945 dev->transceiver = NULL;
2946 dev->lotg = NULL;
2947 }
2948#endif
2949
2950 dev->cap_regs = NULL;
2951
2952 INFO(dev, "unbind\n");
2953 DBG(dev, "<--- %s()\n", __func__);
2954
2955 device_unregister(&dev->gadget.dev);
2956 device_remove_file(&pdev->dev, &dev_attr_langwell_udc);
2957
2958#ifndef OTG_TRANSCEIVER
2959 pci_set_drvdata(pdev, NULL);
2960#endif
2961
2962 /* free dev, wait for the release() finished */
2963 wait_for_completion(&done);
2964
2965 the_controller = NULL;
2966}
2967
2968
2969/*
2970 * wrap this driver around the specified device, but
2971 * don't respond over USB until a gadget driver binds to us.
2972 */
2973static int langwell_udc_probe(struct pci_dev *pdev,
2974 const struct pci_device_id *id)
2975{
2976 struct langwell_udc *dev;
2977#ifndef OTG_TRANSCEIVER
2978 unsigned long resource, len;
2979#endif
2980 void __iomem *base = NULL;
2981 size_t size;
2982 int retval;
2983
2984 if (the_controller) {
2985 dev_warn(&pdev->dev, "ignoring\n");
2986 return -EBUSY;
2987 }
2988
2989 /* alloc, and start init */
2990 dev = kzalloc(sizeof *dev, GFP_KERNEL);
2991 if (dev == NULL) {
2992 retval = -ENOMEM;
2993 goto error;
2994 }
2995
2996 /* initialize device spinlock */
2997 spin_lock_init(&dev->lock);
2998
2999 dev->pdev = pdev;
3000 DBG(dev, "---> %s()\n", __func__);
3001
3002#ifdef OTG_TRANSCEIVER
3003 /* PCI device is already enabled by otg_transceiver driver */
3004 dev->enabled = 1;
3005
3006 /* mem region and register base */
3007 dev->region = 1;
3008 dev->transceiver = otg_get_transceiver();
3009 dev->lotg = otg_to_langwell(dev->transceiver);
3010 base = dev->lotg->regs;
3011#else
3012 pci_set_drvdata(pdev, dev);
3013
3014 /* now all the pci goodies ... */
3015 if (pci_enable_device(pdev) < 0) {
3016 retval = -ENODEV;
3017 goto error;
3018 }
3019 dev->enabled = 1;
3020
3021 /* control register: BAR 0 */
3022 resource = pci_resource_start(pdev, 0);
3023 len = pci_resource_len(pdev, 0);
3024 if (!request_mem_region(resource, len, driver_name)) {
3025 ERROR(dev, "controller already in use\n");
3026 retval = -EBUSY;
3027 goto error;
3028 }
3029 dev->region = 1;
3030
3031 base = ioremap_nocache(resource, len);
3032#endif
3033 if (base == NULL) {
3034 ERROR(dev, "can't map memory\n");
3035 retval = -EFAULT;
3036 goto error;
3037 }
3038
3039 dev->cap_regs = (struct langwell_cap_regs __iomem *) base;
3040 VDBG(dev, "dev->cap_regs: %p\n", dev->cap_regs);
3041 dev->op_regs = (struct langwell_op_regs __iomem *)
3042 (base + OP_REG_OFFSET);
3043 VDBG(dev, "dev->op_regs: %p\n", dev->op_regs);
3044
3045 /* irq setup after old hardware is cleaned up */
3046 if (!pdev->irq) {
3047 ERROR(dev, "No IRQ. Check PCI setup!\n");
3048 retval = -ENODEV;
3049 goto error;
3050 }
3051
3052#ifndef OTG_TRANSCEIVER
3053 INFO(dev, "irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n",
3054 pdev->irq, resource, len, base);
3055 /* enables bus-mastering for device dev */
3056 pci_set_master(pdev);
3057
3058 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED,
3059 driver_name, dev) != 0) {
3060 ERROR(dev, "request interrupt %d failed\n", pdev->irq);
3061 retval = -EBUSY;
3062 goto error;
3063 }
3064 dev->got_irq = 1;
3065#endif
3066
3067 /* set stopped bit */
3068 dev->stopped = 1;
3069
3070 /* capabilities and endpoint number */
3071 dev->lpm = (readl(&dev->cap_regs->hccparams) & HCC_LEN) ? 1 : 0;
3072 dev->dciversion = readw(&dev->cap_regs->dciversion);
3073 dev->devcap = (readl(&dev->cap_regs->dccparams) & DEVCAP) ? 1 : 0;
3074 VDBG(dev, "dev->lpm: %d\n", dev->lpm);
3075 VDBG(dev, "dev->dciversion: 0x%04x\n", dev->dciversion);
3076 VDBG(dev, "dccparams: 0x%08x\n", readl(&dev->cap_regs->dccparams));
3077 VDBG(dev, "dev->devcap: %d\n", dev->devcap);
3078 if (!dev->devcap) {
3079 ERROR(dev, "can't support device mode\n");
3080 retval = -ENODEV;
3081 goto error;
3082 }
3083
3084 /* a pair of endpoints (out/in) for each address */
3085 dev->ep_max = DEN(readl(&dev->cap_regs->dccparams)) * 2;
3086 VDBG(dev, "dev->ep_max: %d\n", dev->ep_max);
3087
3088 /* allocate endpoints memory */
3089 dev->ep = kzalloc(sizeof(struct langwell_ep) * dev->ep_max,
3090 GFP_KERNEL);
3091 if (!dev->ep) {
3092 ERROR(dev, "allocate endpoints memory failed\n");
3093 retval = -ENOMEM;
3094 goto error;
3095 }
3096
3097 /* allocate device dQH memory */
3098 size = dev->ep_max * sizeof(struct langwell_dqh);
3099 VDBG(dev, "orig size = %d\n", size);
3100 if (size < DQH_ALIGNMENT)
3101 size = DQH_ALIGNMENT;
3102 else if ((size % DQH_ALIGNMENT) != 0) {
3103 size += DQH_ALIGNMENT + 1;
3104 size &= ~(DQH_ALIGNMENT - 1);
3105 }
3106 dev->ep_dqh = dma_alloc_coherent(&pdev->dev, size,
3107 &dev->ep_dqh_dma, GFP_KERNEL);
3108 if (!dev->ep_dqh) {
3109 ERROR(dev, "allocate dQH memory failed\n");
3110 retval = -ENOMEM;
3111 goto error;
3112 }
3113 dev->ep_dqh_size = size;
3114 VDBG(dev, "ep_dqh_size = %d\n", dev->ep_dqh_size);
3115
3116 /* initialize ep0 status request structure */
3117 dev->status_req = kzalloc(sizeof(struct langwell_request), GFP_KERNEL);
3118 if (!dev->status_req) {
3119 ERROR(dev, "allocate status_req memory failed\n");
3120 retval = -ENOMEM;
3121 goto error;
3122 }
3123 INIT_LIST_HEAD(&dev->status_req->queue);
3124
3125 /* allocate a small amount of memory to get valid address */
3126 dev->status_req->req.buf = kmalloc(8, GFP_KERNEL);
3127 dev->status_req->req.dma = virt_to_phys(dev->status_req->req.buf);
3128
3129 dev->resume_state = USB_STATE_NOTATTACHED;
3130 dev->usb_state = USB_STATE_POWERED;
3131 dev->ep0_dir = USB_DIR_OUT;
3132 dev->remote_wakeup = 0; /* default to 0 on reset */
3133
3134#ifndef OTG_TRANSCEIVER
3135 /* reset device controller */
3136 langwell_udc_reset(dev);
3137#endif
3138
3139 /* initialize gadget structure */
3140 dev->gadget.ops = &langwell_ops; /* usb_gadget_ops */
3141 dev->gadget.ep0 = &dev->ep[0].ep; /* gadget ep0 */
3142 INIT_LIST_HEAD(&dev->gadget.ep_list); /* ep_list */
3143 dev->gadget.speed = USB_SPEED_UNKNOWN; /* speed */
3144 dev->gadget.is_dualspeed = 1; /* support dual speed */
3145#ifdef OTG_TRANSCEIVER
3146 dev->gadget.is_otg = 1; /* support otg mode */
3147#endif
3148
3149 /* the "gadget" abstracts/virtualizes the controller */
3150 dev_set_name(&dev->gadget.dev, "gadget");
3151 dev->gadget.dev.parent = &pdev->dev;
3152 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
3153 dev->gadget.dev.release = gadget_release;
3154 dev->gadget.name = driver_name; /* gadget name */
3155
3156 /* controller endpoints reinit */
3157 eps_reinit(dev);
3158
3159#ifndef OTG_TRANSCEIVER
3160 /* reset ep0 dQH and endptctrl */
3161 ep0_reset(dev);
3162#endif
3163
3164 /* create dTD dma_pool resource */
3165 dev->dtd_pool = dma_pool_create("langwell_dtd",
3166 &dev->pdev->dev,
3167 sizeof(struct langwell_dtd),
3168 DTD_ALIGNMENT,
3169 DMA_BOUNDARY);
3170
3171 if (!dev->dtd_pool) {
3172 retval = -ENOMEM;
3173 goto error;
3174 }
3175
3176 /* done */
3177 INFO(dev, "%s\n", driver_desc);
3178 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
3179 INFO(dev, "Driver version: " DRIVER_VERSION "\n");
3180 INFO(dev, "Support (max) %d endpoints\n", dev->ep_max);
3181 INFO(dev, "Device interface version: 0x%04x\n", dev->dciversion);
3182 INFO(dev, "Controller mode: %s\n", dev->devcap ? "Device" : "Host");
3183 INFO(dev, "Support USB LPM: %s\n", dev->lpm ? "Yes" : "No");
3184
3185 VDBG(dev, "After langwell_udc_probe(), print all registers:\n");
3186#ifdef VERBOSE
3187 print_all_registers(dev);
3188#endif
3189
3190 the_controller = dev;
3191
3192 retval = device_register(&dev->gadget.dev);
3193 if (retval)
3194 goto error;
3195
3196 retval = device_create_file(&pdev->dev, &dev_attr_langwell_udc);
3197 if (retval)
3198 goto error;
3199
3200 VDBG(dev, "<--- %s()\n", __func__);
3201 return 0;
3202
3203error:
3204 if (dev) {
3205 DBG(dev, "<--- %s()\n", __func__);
3206 langwell_udc_remove(pdev);
3207 }
3208
3209 return retval;
3210}
3211
3212
3213/* device controller suspend */
3214static int langwell_udc_suspend(struct pci_dev *pdev, pm_message_t state)
3215{
3216 struct langwell_udc *dev = the_controller;
3217 u32 devlc;
3218
3219 DBG(dev, "---> %s()\n", __func__);
3220
3221 /* disable interrupt and set controller to stop state */
3222 langwell_udc_stop(dev);
3223
3224 /* diable IRQ handler */
3225 if (dev->got_irq)
3226 free_irq(pdev->irq, dev);
3227 dev->got_irq = 0;
3228
3229
3230 /* save PCI state */
3231 pci_save_state(pdev);
3232
3233 /* set device power state */
3234 pci_set_power_state(pdev, PCI_D3hot);
3235
3236 /* enter PHY low power suspend */
3237 devlc = readl(&dev->op_regs->devlc);
3238 VDBG(dev, "devlc = 0x%08x\n", devlc);
3239 devlc |= LPM_PHCD;
3240 writel(devlc, &dev->op_regs->devlc);
3241
3242 DBG(dev, "<--- %s()\n", __func__);
3243 return 0;
3244}
3245
3246
3247/* device controller resume */
3248static int langwell_udc_resume(struct pci_dev *pdev)
3249{
3250 struct langwell_udc *dev = the_controller;
3251 u32 devlc;
3252
3253 DBG(dev, "---> %s()\n", __func__);
3254
3255 /* exit PHY low power suspend */
3256 devlc = readl(&dev->op_regs->devlc);
3257 VDBG(dev, "devlc = 0x%08x\n", devlc);
3258 devlc &= ~LPM_PHCD;
3259 writel(devlc, &dev->op_regs->devlc);
3260
3261 /* set device D0 power state */
3262 pci_set_power_state(pdev, PCI_D0);
3263
3264 /* restore PCI state */
3265 pci_restore_state(pdev);
3266
3267 /* enable IRQ handler */
3268 if (request_irq(pdev->irq, langwell_irq, IRQF_SHARED, driver_name, dev)
3269 != 0) {
3270 ERROR(dev, "request interrupt %d failed\n", pdev->irq);
3271 return -1;
3272 }
3273 dev->got_irq = 1;
3274
3275 /* reset and start controller to run state */
3276 if (dev->stopped) {
3277 /* reset device controller */
3278 langwell_udc_reset(dev);
3279
3280 /* reset ep0 dQH and endptctrl */
3281 ep0_reset(dev);
3282
3283 /* start device if gadget is loaded */
3284 if (dev->driver)
3285 langwell_udc_start(dev);
3286 }
3287
3288 /* reset USB status */
3289 dev->usb_state = USB_STATE_ATTACHED;
3290 dev->ep0_state = WAIT_FOR_SETUP;
3291 dev->ep0_dir = USB_DIR_OUT;
3292
3293 DBG(dev, "<--- %s()\n", __func__);
3294 return 0;
3295}
3296
3297
3298/* pci driver shutdown */
3299static void langwell_udc_shutdown(struct pci_dev *pdev)
3300{
3301 struct langwell_udc *dev = the_controller;
3302 u32 usbmode;
3303
3304 DBG(dev, "---> %s()\n", __func__);
3305
3306 /* reset controller mode to IDLE */
3307 usbmode = readl(&dev->op_regs->usbmode);
3308 DBG(dev, "usbmode = 0x%08x\n", usbmode);
3309 usbmode &= (~3 | MODE_IDLE);
3310 writel(usbmode, &dev->op_regs->usbmode);
3311
3312 DBG(dev, "<--- %s()\n", __func__);
3313}
3314
3315/*-------------------------------------------------------------------------*/
3316
3317static const struct pci_device_id pci_ids[] = { {
3318 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
3319 .class_mask = ~0,
3320 .vendor = 0x8086,
3321 .device = 0x0811,
3322 .subvendor = PCI_ANY_ID,
3323 .subdevice = PCI_ANY_ID,
3324}, { /* end: all zeroes */ }
3325};
3326
3327
3328MODULE_DEVICE_TABLE(pci, pci_ids);
3329
3330
3331static struct pci_driver langwell_pci_driver = {
3332 .name = (char *) driver_name,
3333 .id_table = pci_ids,
3334
3335 .probe = langwell_udc_probe,
3336 .remove = langwell_udc_remove,
3337
3338 /* device controller suspend/resume */
3339 .suspend = langwell_udc_suspend,
3340 .resume = langwell_udc_resume,
3341
3342 .shutdown = langwell_udc_shutdown,
3343};
3344
3345
3346MODULE_DESCRIPTION(DRIVER_DESC);
3347MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
3348MODULE_VERSION(DRIVER_VERSION);
3349MODULE_LICENSE("GPL");
3350
3351
3352static int __init init(void)
3353{
3354#ifdef OTG_TRANSCEIVER
3355 return langwell_register_peripheral(&langwell_pci_driver);
3356#else
3357 return pci_register_driver(&langwell_pci_driver);
3358#endif
3359}
3360module_init(init);
3361
3362
3363static void __exit cleanup(void)
3364{
3365#ifdef OTG_TRANSCEIVER
3366 return langwell_unregister_peripheral(&langwell_pci_driver);
3367#else
3368 pci_unregister_driver(&langwell_pci_driver);
3369#endif
3370}
3371module_exit(cleanup);
3372
diff --git a/drivers/usb/gadget/langwell_udc.h b/drivers/usb/gadget/langwell_udc.h
new file mode 100644
index 000000000000..9719934e1c08
--- /dev/null
+++ b/drivers/usb/gadget/langwell_udc.h
@@ -0,0 +1,228 @@
1/*
2 * Intel Langwell USB Device Controller driver
3 * Copyright (C) 2008-2009, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20#include <linux/usb/langwell_udc.h>
21
22#if defined(CONFIG_USB_LANGWELL_OTG)
23#include <linux/usb/langwell_otg.h>
24#endif
25
26
27/*-------------------------------------------------------------------------*/
28
29/* driver data structures and utilities */
30
31/*
32 * dTD: Device Endpoint Transfer Descriptor
33 * describe to the device controller the location and quantity of
34 * data to be send/received for given transfer
35 */
36struct langwell_dtd {
37 u32 dtd_next;
38/* bits 31:5, next transfer element pointer */
39#define DTD_NEXT(d) (((d)>>5)&0x7ffffff)
40#define DTD_NEXT_MASK (0x7ffffff << 5)
41/* terminate */
42#define DTD_TERM BIT(0)
43 /* bits 7:0, execution back states */
44 u32 dtd_status:8;
45#define DTD_STATUS(d) (((d)>>0)&0xff)
46#define DTD_STS_ACTIVE BIT(7) /* active */
47#define DTD_STS_HALTED BIT(6) /* halted */
48#define DTD_STS_DBE BIT(5) /* data buffer error */
49#define DTD_STS_TRE BIT(3) /* transaction error */
50 /* bits 9:8 */
51 u32 dtd_res0:2;
52 /* bits 11:10, multipier override */
53 u32 dtd_multo:2;
54#define DTD_MULTO (BIT(11) | BIT(10))
55 /* bits 14:12 */
56 u32 dtd_res1:3;
57 /* bit 15, interrupt on complete */
58 u32 dtd_ioc:1;
59#define DTD_IOC BIT(15)
60 /* bits 30:16, total bytes */
61 u32 dtd_total:15;
62#define DTD_TOTAL(d) (((d)>>16)&0x7fff)
63#define DTD_MAX_TRANSFER_LENGTH 0x4000
64 /* bit 31 */
65 u32 dtd_res2:1;
66 /* dTD buffer pointer page 0 to 4 */
67 u32 dtd_buf[5];
68#define DTD_OFFSET_MASK 0xfff
69/* bits 31:12, buffer pointer */
70#define DTD_BUFFER(d) (((d)>>12)&0x3ff)
71/* bits 11:0, current offset */
72#define DTD_C_OFFSET(d) (((d)>>0)&0xfff)
73/* bits 10:0, frame number */
74#define DTD_FRAME(d) (((d)>>0)&0x7ff)
75
76 /* driver-private parts */
77
78 /* dtd dma address */
79 dma_addr_t dtd_dma;
80 /* next dtd virtual address */
81 struct langwell_dtd *next_dtd_virt;
82};
83
84
85/*
86 * dQH: Device Endpoint Queue Head
87 * describe where all transfers are managed
88 * 48-byte data structure, aligned on 64-byte boundary
89 *
90 * These are associated with dTD structure
91 */
92struct langwell_dqh {
93 /* endpoint capabilities and characteristics */
94 u32 dqh_res0:15; /* bits 14:0 */
95 u32 dqh_ios:1; /* bit 15, interrupt on setup */
96#define DQH_IOS BIT(15)
97 u32 dqh_mpl:11; /* bits 26:16, maximum packet length */
98#define DQH_MPL (0x7ff << 16)
99 u32 dqh_res1:2; /* bits 28:27 */
100 u32 dqh_zlt:1; /* bit 29, zero length termination */
101#define DQH_ZLT BIT(29)
102 u32 dqh_mult:2; /* bits 31:30 */
103#define DQH_MULT (BIT(30) | BIT(31))
104
105 /* current dTD pointer */
106 u32 dqh_current; /* locate the transfer in progress */
107#define DQH_C_DTD(e) \
108 (((e)>>5)&0x7ffffff) /* bits 31:5, current dTD pointer */
109
110 /* transfer overlay, hardware parts of a struct langwell_dtd */
111 u32 dtd_next;
112 u32 dtd_status:8; /* bits 7:0, execution back states */
113 u32 dtd_res0:2; /* bits 9:8 */
114 u32 dtd_multo:2; /* bits 11:10, multipier override */
115 u32 dtd_res1:3; /* bits 14:12 */
116 u32 dtd_ioc:1; /* bit 15, interrupt on complete */
117 u32 dtd_total:15; /* bits 30:16, total bytes */
118 u32 dtd_res2:1; /* bit 31 */
119 u32 dtd_buf[5]; /* dTD buffer pointer page 0 to 4 */
120
121 u32 dqh_res2;
122 struct usb_ctrlrequest dqh_setup; /* setup packet buffer */
123} __attribute__ ((aligned(64)));
124
125
126/* endpoint data structure */
127struct langwell_ep {
128 struct usb_ep ep;
129 dma_addr_t dma;
130 struct langwell_udc *dev;
131 unsigned long irqs;
132 struct list_head queue;
133 struct langwell_dqh *dqh;
134 const struct usb_endpoint_descriptor *desc;
135 char name[14];
136 unsigned stopped:1,
137 ep_type:2,
138 ep_num:8;
139};
140
141
142/* request data structure */
143struct langwell_request {
144 struct usb_request req;
145 struct langwell_dtd *dtd, *head, *tail;
146 struct langwell_ep *ep;
147 dma_addr_t dtd_dma;
148 struct list_head queue;
149 unsigned dtd_count;
150 unsigned mapped:1;
151};
152
153
154/* ep0 transfer state */
155enum ep0_state {
156 WAIT_FOR_SETUP,
157 DATA_STATE_XMIT,
158 DATA_STATE_NEED_ZLP,
159 WAIT_FOR_OUT_STATUS,
160 DATA_STATE_RECV,
161};
162
163
164/* device suspend state */
165enum lpm_state {
166 LPM_L0, /* on */
167 LPM_L1, /* LPM L1 sleep */
168 LPM_L2, /* suspend */
169 LPM_L3, /* off */
170};
171
172
173/* device data structure */
174struct langwell_udc {
175 /* each pci device provides one gadget, several endpoints */
176 struct usb_gadget gadget;
177 spinlock_t lock; /* device lock */
178 struct langwell_ep *ep;
179 struct usb_gadget_driver *driver;
180 struct otg_transceiver *transceiver;
181 u8 dev_addr;
182 u32 usb_state;
183 u32 resume_state;
184 u32 bus_reset;
185 enum lpm_state lpm_state;
186 enum ep0_state ep0_state;
187 u32 ep0_dir;
188 u16 dciversion;
189 unsigned ep_max;
190 unsigned devcap:1,
191 enabled:1,
192 region:1,
193 got_irq:1,
194 powered:1,
195 remote_wakeup:1,
196 rate:1,
197 is_reset:1,
198 softconnected:1,
199 vbus_active:1,
200 suspended:1,
201 stopped:1,
202 lpm:1; /* LPM capability */
203
204 /* pci state used to access those endpoints */
205 struct pci_dev *pdev;
206
207 /* Langwell otg transceiver */
208 struct langwell_otg *lotg;
209
210 /* control registers */
211 struct langwell_cap_regs __iomem *cap_regs;
212 struct langwell_op_regs __iomem *op_regs;
213
214 struct usb_ctrlrequest local_setup_buff;
215 struct langwell_dqh *ep_dqh;
216 size_t ep_dqh_size;
217 dma_addr_t ep_dqh_dma;
218
219 /* ep0 status request */
220 struct langwell_request *status_req;
221
222 /* dma pool */
223 struct dma_pool *dtd_pool;
224
225 /* make sure release() is done */
226 struct completion *done;
227};
228
diff --git a/drivers/usb/gadget/pxa25x_udc.c b/drivers/usb/gadget/pxa25x_udc.c
index 0ce4e2819847..ed21e263f832 100644
--- a/drivers/usb/gadget/pxa25x_udc.c
+++ b/drivers/usb/gadget/pxa25x_udc.c
@@ -139,7 +139,7 @@ static int is_vbus_present(void)
139{ 139{
140 struct pxa2xx_udc_mach_info *mach = the_controller->mach; 140 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
141 141
142 if (mach->gpio_vbus) { 142 if (gpio_is_valid(mach->gpio_vbus)) {
143 int value = gpio_get_value(mach->gpio_vbus); 143 int value = gpio_get_value(mach->gpio_vbus);
144 144
145 if (mach->gpio_vbus_inverted) 145 if (mach->gpio_vbus_inverted)
@@ -158,7 +158,7 @@ static void pullup_off(void)
158 struct pxa2xx_udc_mach_info *mach = the_controller->mach; 158 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
159 int off_level = mach->gpio_pullup_inverted; 159 int off_level = mach->gpio_pullup_inverted;
160 160
161 if (mach->gpio_pullup) 161 if (gpio_is_valid(mach->gpio_pullup))
162 gpio_set_value(mach->gpio_pullup, off_level); 162 gpio_set_value(mach->gpio_pullup, off_level);
163 else if (mach->udc_command) 163 else if (mach->udc_command)
164 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT); 164 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
@@ -169,7 +169,7 @@ static void pullup_on(void)
169 struct pxa2xx_udc_mach_info *mach = the_controller->mach; 169 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
170 int on_level = !mach->gpio_pullup_inverted; 170 int on_level = !mach->gpio_pullup_inverted;
171 171
172 if (mach->gpio_pullup) 172 if (gpio_is_valid(mach->gpio_pullup))
173 gpio_set_value(mach->gpio_pullup, on_level); 173 gpio_set_value(mach->gpio_pullup, on_level);
174 else if (mach->udc_command) 174 else if (mach->udc_command)
175 mach->udc_command(PXA2XX_UDC_CMD_CONNECT); 175 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
@@ -1000,7 +1000,7 @@ static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1000 udc = container_of(_gadget, struct pxa25x_udc, gadget); 1000 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1001 1001
1002 /* not all boards support pullup control */ 1002 /* not all boards support pullup control */
1003 if (!udc->mach->gpio_pullup && !udc->mach->udc_command) 1003 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
1004 return -EOPNOTSUPP; 1004 return -EOPNOTSUPP;
1005 1005
1006 udc->pullup = (is_active != 0); 1006 udc->pullup = (is_active != 0);
@@ -1802,11 +1802,13 @@ pxa25x_udc_irq(int irq, void *_dev)
1802 USIR0 |= tmp; 1802 USIR0 |= tmp;
1803 handled = 1; 1803 handled = 1;
1804 } 1804 }
1805#ifndef CONFIG_USB_PXA25X_SMALL
1805 if (usir1 & tmp) { 1806 if (usir1 & tmp) {
1806 handle_ep(&dev->ep[i+8]); 1807 handle_ep(&dev->ep[i+8]);
1807 USIR1 |= tmp; 1808 USIR1 |= tmp;
1808 handled = 1; 1809 handled = 1;
1809 } 1810 }
1811#endif
1810 } 1812 }
1811 } 1813 }
1812 1814
@@ -2160,7 +2162,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2160 dev->dev = &pdev->dev; 2162 dev->dev = &pdev->dev;
2161 dev->mach = pdev->dev.platform_data; 2163 dev->mach = pdev->dev.platform_data;
2162 2164
2163 if (dev->mach->gpio_vbus) { 2165 if (gpio_is_valid(dev->mach->gpio_vbus)) {
2164 if ((retval = gpio_request(dev->mach->gpio_vbus, 2166 if ((retval = gpio_request(dev->mach->gpio_vbus,
2165 "pxa25x_udc GPIO VBUS"))) { 2167 "pxa25x_udc GPIO VBUS"))) {
2166 dev_dbg(&pdev->dev, 2168 dev_dbg(&pdev->dev,
@@ -2173,7 +2175,7 @@ static int __init pxa25x_udc_probe(struct platform_device *pdev)
2173 } else 2175 } else
2174 vbus_irq = 0; 2176 vbus_irq = 0;
2175 2177
2176 if (dev->mach->gpio_pullup) { 2178 if (gpio_is_valid(dev->mach->gpio_pullup)) {
2177 if ((retval = gpio_request(dev->mach->gpio_pullup, 2179 if ((retval = gpio_request(dev->mach->gpio_pullup,
2178 "pca25x_udc GPIO PULLUP"))) { 2180 "pca25x_udc GPIO PULLUP"))) {
2179 dev_dbg(&pdev->dev, 2181 dev_dbg(&pdev->dev,
@@ -2256,10 +2258,10 @@ lubbock_fail0:
2256#endif 2258#endif
2257 free_irq(irq, dev); 2259 free_irq(irq, dev);
2258 err_irq1: 2260 err_irq1:
2259 if (dev->mach->gpio_pullup) 2261 if (gpio_is_valid(dev->mach->gpio_pullup))
2260 gpio_free(dev->mach->gpio_pullup); 2262 gpio_free(dev->mach->gpio_pullup);
2261 err_gpio_pullup: 2263 err_gpio_pullup:
2262 if (dev->mach->gpio_vbus) 2264 if (gpio_is_valid(dev->mach->gpio_vbus))
2263 gpio_free(dev->mach->gpio_vbus); 2265 gpio_free(dev->mach->gpio_vbus);
2264 err_gpio_vbus: 2266 err_gpio_vbus:
2265 clk_put(dev->clk); 2267 clk_put(dev->clk);
@@ -2294,11 +2296,11 @@ static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2294 free_irq(LUBBOCK_USB_IRQ, dev); 2296 free_irq(LUBBOCK_USB_IRQ, dev);
2295 } 2297 }
2296#endif 2298#endif
2297 if (dev->mach->gpio_vbus) { 2299 if (gpio_is_valid(dev->mach->gpio_vbus)) {
2298 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev); 2300 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2299 gpio_free(dev->mach->gpio_vbus); 2301 gpio_free(dev->mach->gpio_vbus);
2300 } 2302 }
2301 if (dev->mach->gpio_pullup) 2303 if (gpio_is_valid(dev->mach->gpio_pullup))
2302 gpio_free(dev->mach->gpio_pullup); 2304 gpio_free(dev->mach->gpio_pullup);
2303 2305
2304 clk_put(dev->clk); 2306 clk_put(dev->clk);
@@ -2329,7 +2331,7 @@ static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
2329 struct pxa25x_udc *udc = platform_get_drvdata(dev); 2331 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2330 unsigned long flags; 2332 unsigned long flags;
2331 2333
2332 if (!udc->mach->gpio_pullup && !udc->mach->udc_command) 2334 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
2333 WARNING("USB host won't detect disconnect!\n"); 2335 WARNING("USB host won't detect disconnect!\n");
2334 udc->suspended = 1; 2336 udc->suspended = 1;
2335 2337
diff --git a/drivers/usb/gadget/pxa27x_udc.c b/drivers/usb/gadget/pxa27x_udc.c
index 8cc676ecbb23..1937d8c7b433 100644
--- a/drivers/usb/gadget/pxa27x_udc.c
+++ b/drivers/usb/gadget/pxa27x_udc.c
@@ -38,7 +38,6 @@
38#include <linux/usb.h> 38#include <linux/usb.h>
39#include <linux/usb/ch9.h> 39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h> 40#include <linux/usb/gadget.h>
41#include <mach/pxa2xx-regs.h> /* FIXME: for PSSR */
42#include <mach/udc.h> 41#include <mach/udc.h>
43 42
44#include "pxa27x_udc.h" 43#include "pxa27x_udc.h"
@@ -474,6 +473,23 @@ static inline void udc_clear_mask_UDCCR(struct pxa_udc *udc, int mask)
474} 473}
475 474
476/** 475/**
476 * ep_write_UDCCSR - set bits in UDCCSR
477 * @udc: udc device
478 * @mask: bits to set in UDCCR
479 *
480 * Sets bits in UDCCSR (UDCCSR0 and UDCCSR*).
481 *
482 * A specific case is applied to ep0 : the ACM bit is always set to 1, for
483 * SET_INTERFACE and SET_CONFIGURATION.
484 */
485static inline void ep_write_UDCCSR(struct pxa_ep *ep, int mask)
486{
487 if (is_ep0(ep))
488 mask |= UDCCSR0_ACM;
489 udc_ep_writel(ep, UDCCSR, mask);
490}
491
492/**
477 * ep_count_bytes_remain - get how many bytes in udc endpoint 493 * ep_count_bytes_remain - get how many bytes in udc endpoint
478 * @ep: udc endpoint 494 * @ep: udc endpoint
479 * 495 *
@@ -861,7 +877,7 @@ static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
861 *buf++ = udc_ep_readl(ep, UDCDR); 877 *buf++ = udc_ep_readl(ep, UDCDR);
862 req->req.actual += count; 878 req->req.actual += count;
863 879
864 udc_ep_writel(ep, UDCCSR, UDCCSR_PC); 880 ep_write_UDCCSR(ep, UDCCSR_PC);
865 881
866 return count; 882 return count;
867} 883}
@@ -969,12 +985,12 @@ static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
969 if (udccsr & UDCCSR_PC) { 985 if (udccsr & UDCCSR_PC) {
970 ep_vdbg(ep, "Clearing Transmit Complete, udccsr=%x\n", 986 ep_vdbg(ep, "Clearing Transmit Complete, udccsr=%x\n",
971 udccsr); 987 udccsr);
972 udc_ep_writel(ep, UDCCSR, UDCCSR_PC); 988 ep_write_UDCCSR(ep, UDCCSR_PC);
973 } 989 }
974 if (udccsr & UDCCSR_TRN) { 990 if (udccsr & UDCCSR_TRN) {
975 ep_vdbg(ep, "Clearing Underrun on, udccsr=%x\n", 991 ep_vdbg(ep, "Clearing Underrun on, udccsr=%x\n",
976 udccsr); 992 udccsr);
977 udc_ep_writel(ep, UDCCSR, UDCCSR_TRN); 993 ep_write_UDCCSR(ep, UDCCSR_TRN);
978 } 994 }
979 995
980 count = write_packet(ep, req, max); 996 count = write_packet(ep, req, max);
@@ -996,7 +1012,7 @@ static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
996 } 1012 }
997 1013
998 if (is_short) 1014 if (is_short)
999 udc_ep_writel(ep, UDCCSR, UDCCSR_SP); 1015 ep_write_UDCCSR(ep, UDCCSR_SP);
1000 1016
1001 /* requests complete when all IN data is in the FIFO */ 1017 /* requests complete when all IN data is in the FIFO */
1002 if (is_last) { 1018 if (is_last) {
@@ -1029,7 +1045,7 @@ static int read_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
1029 1045
1030 while (epout_has_pkt(ep)) { 1046 while (epout_has_pkt(ep)) {
1031 count = read_packet(ep, req); 1047 count = read_packet(ep, req);
1032 udc_ep_writel(ep, UDCCSR, UDCCSR0_OPC); 1048 ep_write_UDCCSR(ep, UDCCSR0_OPC);
1033 inc_ep_stats_bytes(ep, count, !USB_DIR_IN); 1049 inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
1034 1050
1035 is_short = (count < ep->fifo_size); 1051 is_short = (count < ep->fifo_size);
@@ -1074,7 +1090,7 @@ static int write_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
1074 1090
1075 /* Sends either a short packet or a 0 length packet */ 1091 /* Sends either a short packet or a 0 length packet */
1076 if (unlikely(is_short)) 1092 if (unlikely(is_short))
1077 udc_ep_writel(ep, UDCCSR, UDCCSR0_IPR); 1093 ep_write_UDCCSR(ep, UDCCSR0_IPR);
1078 1094
1079 ep_dbg(ep, "in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n", 1095 ep_dbg(ep, "in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n",
1080 count, is_short ? "/S" : "", is_last ? "/L" : "", 1096 count, is_short ? "/S" : "", is_last ? "/L" : "",
@@ -1277,7 +1293,7 @@ static int pxa_ep_set_halt(struct usb_ep *_ep, int value)
1277 1293
1278 /* FST, FEF bits are the same for control and non control endpoints */ 1294 /* FST, FEF bits are the same for control and non control endpoints */
1279 rc = 0; 1295 rc = 0;
1280 udc_ep_writel(ep, UDCCSR, UDCCSR_FST | UDCCSR_FEF); 1296 ep_write_UDCCSR(ep, UDCCSR_FST | UDCCSR_FEF);
1281 if (is_ep0(ep)) 1297 if (is_ep0(ep))
1282 set_ep0state(ep->dev, STALL); 1298 set_ep0state(ep->dev, STALL);
1283 1299
@@ -1343,7 +1359,7 @@ static void pxa_ep_fifo_flush(struct usb_ep *_ep)
1343 udc_ep_readl(ep, UDCDR); 1359 udc_ep_readl(ep, UDCDR);
1344 } else { 1360 } else {
1345 /* most IN status is the same, but ISO can't stall */ 1361 /* most IN status is the same, but ISO can't stall */
1346 udc_ep_writel(ep, UDCCSR, 1362 ep_write_UDCCSR(ep,
1347 UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN 1363 UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN
1348 | (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST)); 1364 | (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST));
1349 } 1365 }
@@ -1728,6 +1744,7 @@ static void udc_enable(struct pxa_udc *udc)
1728 memset(&udc->stats, 0, sizeof(udc->stats)); 1744 memset(&udc->stats, 0, sizeof(udc->stats));
1729 1745
1730 udc_set_mask_UDCCR(udc, UDCCR_UDE); 1746 udc_set_mask_UDCCR(udc, UDCCR_UDE);
1747 ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_ACM);
1731 udelay(2); 1748 udelay(2);
1732 if (udc_readl(udc, UDCCR) & UDCCR_EMCE) 1749 if (udc_readl(udc, UDCCR) & UDCCR_EMCE)
1733 dev_err(udc->dev, "Configuration errors, udc disabled\n"); 1750 dev_err(udc->dev, "Configuration errors, udc disabled\n");
@@ -1893,6 +1910,15 @@ static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1893 1910
1894 nuke(ep, -EPROTO); 1911 nuke(ep, -EPROTO);
1895 1912
1913 /*
1914 * In the PXA320 manual, in the section about Back-to-Back setup
1915 * packets, it describes this situation. The solution is to set OPC to
1916 * get rid of the status packet, and then continue with the setup
1917 * packet. Generalize to pxa27x CPUs.
1918 */
1919 if (epout_has_pkt(ep) && (ep_count_bytes_remain(ep) == 0))
1920 ep_write_UDCCSR(ep, UDCCSR0_OPC);
1921
1896 /* read SETUP packet */ 1922 /* read SETUP packet */
1897 for (i = 0; i < 2; i++) { 1923 for (i = 0; i < 2; i++) {
1898 if (unlikely(ep_is_empty(ep))) 1924 if (unlikely(ep_is_empty(ep)))
@@ -1919,7 +1945,7 @@ static void handle_ep0_ctrl_req(struct pxa_udc *udc,
1919 set_ep0state(udc, OUT_DATA_STAGE); 1945 set_ep0state(udc, OUT_DATA_STAGE);
1920 1946
1921 /* Tell UDC to enter Data Stage */ 1947 /* Tell UDC to enter Data Stage */
1922 udc_ep_writel(ep, UDCCSR, UDCCSR0_SA | UDCCSR0_OPC); 1948 ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
1923 1949
1924 i = udc->driver->setup(&udc->gadget, &u.r); 1950 i = udc->driver->setup(&udc->gadget, &u.r);
1925 if (i < 0) 1951 if (i < 0)
@@ -1929,7 +1955,7 @@ out:
1929stall: 1955stall:
1930 ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n", 1956 ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
1931 udc_ep_readl(ep, UDCCSR), i); 1957 udc_ep_readl(ep, UDCCSR), i);
1932 udc_ep_writel(ep, UDCCSR, UDCCSR0_FST | UDCCSR0_FTF); 1958 ep_write_UDCCSR(ep, UDCCSR0_FST | UDCCSR0_FTF);
1933 set_ep0state(udc, STALL); 1959 set_ep0state(udc, STALL);
1934 goto out; 1960 goto out;
1935} 1961}
@@ -1966,6 +1992,8 @@ stall:
1966 * cleared by software. 1992 * cleared by software.
1967 * - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it 1993 * - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it
1968 * before reading ep0. 1994 * before reading ep0.
1995 * This is true only for PXA27x. This is not true anymore for PXA3xx family
1996 * (check Back-to-Back setup packet in developers guide).
1969 * - irq can be called on a "packet complete" event (opc_irq=1), while 1997 * - irq can be called on a "packet complete" event (opc_irq=1), while
1970 * UDCCSR0_OPC is not yet raised (delta can be as big as 100ms 1998 * UDCCSR0_OPC is not yet raised (delta can be as big as 100ms
1971 * from experimentation). 1999 * from experimentation).
@@ -1998,7 +2026,7 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
1998 if (udccsr0 & UDCCSR0_SST) { 2026 if (udccsr0 & UDCCSR0_SST) {
1999 ep_dbg(ep, "clearing stall status\n"); 2027 ep_dbg(ep, "clearing stall status\n");
2000 nuke(ep, -EPIPE); 2028 nuke(ep, -EPIPE);
2001 udc_ep_writel(ep, UDCCSR, UDCCSR0_SST); 2029 ep_write_UDCCSR(ep, UDCCSR0_SST);
2002 ep0_idle(udc); 2030 ep0_idle(udc);
2003 } 2031 }
2004 2032
@@ -2023,7 +2051,7 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
2023 break; 2051 break;
2024 case IN_DATA_STAGE: /* GET_DESCRIPTOR */ 2052 case IN_DATA_STAGE: /* GET_DESCRIPTOR */
2025 if (epout_has_pkt(ep)) 2053 if (epout_has_pkt(ep))
2026 udc_ep_writel(ep, UDCCSR, UDCCSR0_OPC); 2054 ep_write_UDCCSR(ep, UDCCSR0_OPC);
2027 if (req && !ep_is_full(ep)) 2055 if (req && !ep_is_full(ep))
2028 completed = write_ep0_fifo(ep, req); 2056 completed = write_ep0_fifo(ep, req);
2029 if (completed) 2057 if (completed)
@@ -2036,7 +2064,7 @@ static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
2036 ep0_end_out_req(ep, req); 2064 ep0_end_out_req(ep, req);
2037 break; 2065 break;
2038 case STALL: 2066 case STALL:
2039 udc_ep_writel(ep, UDCCSR, UDCCSR0_FST); 2067 ep_write_UDCCSR(ep, UDCCSR0_FST);
2040 break; 2068 break;
2041 case IN_STATUS_STAGE: 2069 case IN_STATUS_STAGE:
2042 /* 2070 /*
@@ -2131,6 +2159,7 @@ static void pxa27x_change_configuration(struct pxa_udc *udc, int config)
2131 2159
2132 set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF); 2160 set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
2133 udc->driver->setup(&udc->gadget, &req); 2161 udc->driver->setup(&udc->gadget, &req);
2162 ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
2134} 2163}
2135 2164
2136/** 2165/**
@@ -2159,6 +2188,7 @@ static void pxa27x_change_interface(struct pxa_udc *udc, int iface, int alt)
2159 2188
2160 set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF); 2189 set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
2161 udc->driver->setup(&udc->gadget, &req); 2190 udc->driver->setup(&udc->gadget, &req);
2191 ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
2162} 2192}
2163 2193
2164/* 2194/*
@@ -2280,7 +2310,7 @@ static void irq_udc_reset(struct pxa_udc *udc)
2280 memset(&udc->stats, 0, sizeof udc->stats); 2310 memset(&udc->stats, 0, sizeof udc->stats);
2281 2311
2282 nuke(ep, -EPROTO); 2312 nuke(ep, -EPROTO);
2283 udc_ep_writel(ep, UDCCSR, UDCCSR0_FTF | UDCCSR0_OPC); 2313 ep_write_UDCCSR(ep, UDCCSR0_FTF | UDCCSR0_OPC);
2284 ep0_idle(udc); 2314 ep0_idle(udc);
2285} 2315}
2286 2316
@@ -2479,6 +2509,12 @@ static void pxa_udc_shutdown(struct platform_device *_dev)
2479 udc_disable(udc); 2509 udc_disable(udc);
2480} 2510}
2481 2511
2512#ifdef CONFIG_CPU_PXA27x
2513extern void pxa27x_clear_otgph(void);
2514#else
2515#define pxa27x_clear_otgph() do {} while (0)
2516#endif
2517
2482#ifdef CONFIG_PM 2518#ifdef CONFIG_PM
2483/** 2519/**
2484 * pxa_udc_suspend - Suspend udc device 2520 * pxa_udc_suspend - Suspend udc device
@@ -2546,8 +2582,7 @@ static int pxa_udc_resume(struct platform_device *_dev)
2546 * Software must configure the USB OTG pad, UDC, and UHC 2582 * Software must configure the USB OTG pad, UDC, and UHC
2547 * to the state they were in before entering sleep mode. 2583 * to the state they were in before entering sleep mode.
2548 */ 2584 */
2549 if (cpu_is_pxa27x()) 2585 pxa27x_clear_otgph();
2550 PSSR |= PSSR_OTGPH;
2551 2586
2552 return 0; 2587 return 0;
2553} 2588}
@@ -2571,7 +2606,7 @@ static struct platform_driver udc_driver = {
2571 2606
2572static int __init udc_init(void) 2607static int __init udc_init(void)
2573{ 2608{
2574 if (!cpu_is_pxa27x()) 2609 if (!cpu_is_pxa27x() && !cpu_is_pxa3xx())
2575 return -ENODEV; 2610 return -ENODEV;
2576 2611
2577 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION); 2612 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index db58125331da..e25225e26586 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -130,6 +130,8 @@
130#define UP2OCR_HXOE (1 << 17) /* Transceiver Output Enable */ 130#define UP2OCR_HXOE (1 << 17) /* Transceiver Output Enable */
131#define UP2OCR_SEOS (1 << 24) /* Single-Ended Output Select */ 131#define UP2OCR_SEOS (1 << 24) /* Single-Ended Output Select */
132 132
133#define UDCCSR0_ACM (1 << 9) /* Ack Control Mode */
134#define UDCCSR0_AREN (1 << 8) /* Ack Response Enable */
133#define UDCCSR0_SA (1 << 7) /* Setup Active */ 135#define UDCCSR0_SA (1 << 7) /* Setup Active */
134#define UDCCSR0_RNE (1 << 6) /* Receive FIFO Not Empty */ 136#define UDCCSR0_RNE (1 << 6) /* Receive FIFO Not Empty */
135#define UDCCSR0_FST (1 << 5) /* Force Stall */ 137#define UDCCSR0_FST (1 << 5) /* Force Stall */
diff --git a/drivers/usb/gadget/rndis.c b/drivers/usb/gadget/rndis.c
index 2b4660e08c4d..ca41b0b5afb3 100644
--- a/drivers/usb/gadget/rndis.c
+++ b/drivers/usb/gadget/rndis.c
@@ -442,6 +442,8 @@ gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
442 442
443 case OID_802_3_MAC_OPTIONS: 443 case OID_802_3_MAC_OPTIONS:
444 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__); 444 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
445 *outbuf = cpu_to_le32(0);
446 retval = 0;
445 break; 447 break;
446 448
447 /* ieee802.3 statistics OIDs (table 4-4) */ 449 /* ieee802.3 statistics OIDs (table 4-4) */
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
new file mode 100644
index 000000000000..50c71aae2cc2
--- /dev/null
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -0,0 +1,3269 @@
1/* linux/drivers/usb/gadget/s3c-hsotg.c
2 *
3 * Copyright 2008 Openmoko, Inc.
4 * Copyright 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
7 *
8 * S3C USB2.0 High-speed / OtG driver
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13*/
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
19#include <linux/platform_device.h>
20#include <linux/dma-mapping.h>
21#include <linux/debugfs.h>
22#include <linux/seq_file.h>
23#include <linux/delay.h>
24#include <linux/io.h>
25
26#include <linux/usb/ch9.h>
27#include <linux/usb/gadget.h>
28
29#include <mach/map.h>
30
31#include <plat/regs-usb-hsotg-phy.h>
32#include <plat/regs-usb-hsotg.h>
33#include <plat/regs-sys.h>
34#include <plat/udc-hs.h>
35
36#define DMA_ADDR_INVALID (~((dma_addr_t)0))
37
38/* EP0_MPS_LIMIT
39 *
40 * Unfortunately there seems to be a limit of the amount of data that can
41 * be transfered by IN transactions on EP0. This is either 127 bytes or 3
42 * packets (which practially means 1 packet and 63 bytes of data) when the
43 * MPS is set to 64.
44 *
45 * This means if we are wanting to move >127 bytes of data, we need to
46 * split the transactions up, but just doing one packet at a time does
47 * not work (this may be an implicit DATA0 PID on first packet of the
48 * transaction) and doing 2 packets is outside the controller's limits.
49 *
50 * If we try to lower the MPS size for EP0, then no transfers work properly
51 * for EP0, and the system will fail basic enumeration. As no cause for this
52 * has currently been found, we cannot support any large IN transfers for
53 * EP0.
54 */
55#define EP0_MPS_LIMIT 64
56
57struct s3c_hsotg;
58struct s3c_hsotg_req;
59
60/**
61 * struct s3c_hsotg_ep - driver endpoint definition.
62 * @ep: The gadget layer representation of the endpoint.
63 * @name: The driver generated name for the endpoint.
64 * @queue: Queue of requests for this endpoint.
65 * @parent: Reference back to the parent device structure.
66 * @req: The current request that the endpoint is processing. This is
67 * used to indicate an request has been loaded onto the endpoint
68 * and has yet to be completed (maybe due to data move, or simply
69 * awaiting an ack from the core all the data has been completed).
70 * @debugfs: File entry for debugfs file for this endpoint.
71 * @lock: State lock to protect contents of endpoint.
72 * @dir_in: Set to true if this endpoint is of the IN direction, which
73 * means that it is sending data to the Host.
74 * @index: The index for the endpoint registers.
75 * @name: The name array passed to the USB core.
76 * @halted: Set if the endpoint has been halted.
77 * @periodic: Set if this is a periodic ep, such as Interrupt
78 * @sent_zlp: Set if we've sent a zero-length packet.
79 * @total_data: The total number of data bytes done.
80 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
81 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
82 * @last_load: The offset of data for the last start of request.
83 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
84 *
85 * This is the driver's state for each registered enpoint, allowing it
86 * to keep track of transactions that need doing. Each endpoint has a
87 * lock to protect the state, to try and avoid using an overall lock
88 * for the host controller as much as possible.
89 *
90 * For periodic IN endpoints, we have fifo_size and fifo_load to try
91 * and keep track of the amount of data in the periodic FIFO for each
92 * of these as we don't have a status register that tells us how much
93 * is in each of them.
94 */
95struct s3c_hsotg_ep {
96 struct usb_ep ep;
97 struct list_head queue;
98 struct s3c_hsotg *parent;
99 struct s3c_hsotg_req *req;
100 struct dentry *debugfs;
101
102 spinlock_t lock;
103
104 unsigned long total_data;
105 unsigned int size_loaded;
106 unsigned int last_load;
107 unsigned int fifo_load;
108 unsigned short fifo_size;
109
110 unsigned char dir_in;
111 unsigned char index;
112
113 unsigned int halted:1;
114 unsigned int periodic:1;
115 unsigned int sent_zlp:1;
116
117 char name[10];
118};
119
120#define S3C_HSOTG_EPS (8+1) /* limit to 9 for the moment */
121
122/**
123 * struct s3c_hsotg - driver state.
124 * @dev: The parent device supplied to the probe function
125 * @driver: USB gadget driver
126 * @plat: The platform specific configuration data.
127 * @regs: The memory area mapped for accessing registers.
128 * @regs_res: The resource that was allocated when claiming register space.
129 * @irq: The IRQ number we are using
130 * @debug_root: root directrory for debugfs.
131 * @debug_file: main status file for debugfs.
132 * @debug_fifo: FIFO status file for debugfs.
133 * @ep0_reply: Request used for ep0 reply.
134 * @ep0_buff: Buffer for EP0 reply data, if needed.
135 * @ctrl_buff: Buffer for EP0 control requests.
136 * @ctrl_req: Request for EP0 control packets.
137 * @eps: The endpoints being supplied to the gadget framework
138 */
139struct s3c_hsotg {
140 struct device *dev;
141 struct usb_gadget_driver *driver;
142 struct s3c_hsotg_plat *plat;
143
144 void __iomem *regs;
145 struct resource *regs_res;
146 int irq;
147
148 struct dentry *debug_root;
149 struct dentry *debug_file;
150 struct dentry *debug_fifo;
151
152 struct usb_request *ep0_reply;
153 struct usb_request *ctrl_req;
154 u8 ep0_buff[8];
155 u8 ctrl_buff[8];
156
157 struct usb_gadget gadget;
158 struct s3c_hsotg_ep eps[];
159};
160
161/**
162 * struct s3c_hsotg_req - data transfer request
163 * @req: The USB gadget request
164 * @queue: The list of requests for the endpoint this is queued for.
165 * @in_progress: Has already had size/packets written to core
166 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
167 */
168struct s3c_hsotg_req {
169 struct usb_request req;
170 struct list_head queue;
171 unsigned char in_progress;
172 unsigned char mapped;
173};
174
175/* conversion functions */
176static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
177{
178 return container_of(req, struct s3c_hsotg_req, req);
179}
180
181static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
182{
183 return container_of(ep, struct s3c_hsotg_ep, ep);
184}
185
186static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
187{
188 return container_of(gadget, struct s3c_hsotg, gadget);
189}
190
191static inline void __orr32(void __iomem *ptr, u32 val)
192{
193 writel(readl(ptr) | val, ptr);
194}
195
196static inline void __bic32(void __iomem *ptr, u32 val)
197{
198 writel(readl(ptr) & ~val, ptr);
199}
200
201/* forward decleration of functions */
202static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
203
204/**
205 * using_dma - return the DMA status of the driver.
206 * @hsotg: The driver state.
207 *
208 * Return true if we're using DMA.
209 *
210 * Currently, we have the DMA support code worked into everywhere
211 * that needs it, but the AMBA DMA implementation in the hardware can
212 * only DMA from 32bit aligned addresses. This means that gadgets such
213 * as the CDC Ethernet cannot work as they often pass packets which are
214 * not 32bit aligned.
215 *
216 * Unfortunately the choice to use DMA or not is global to the controller
217 * and seems to be only settable when the controller is being put through
218 * a core reset. This means we either need to fix the gadgets to take
219 * account of DMA alignment, or add bounce buffers (yuerk).
220 *
221 * Until this issue is sorted out, we always return 'false'.
222 */
223static inline bool using_dma(struct s3c_hsotg *hsotg)
224{
225 return false; /* support is not complete */
226}
227
228/**
229 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
230 * @hsotg: The device state
231 * @ints: A bitmask of the interrupts to enable
232 */
233static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
234{
235 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
236 u32 new_gsintmsk;
237
238 new_gsintmsk = gsintmsk | ints;
239
240 if (new_gsintmsk != gsintmsk) {
241 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
242 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
243 }
244}
245
246/**
247 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
248 * @hsotg: The device state
249 * @ints: A bitmask of the interrupts to enable
250 */
251static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
252{
253 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
254 u32 new_gsintmsk;
255
256 new_gsintmsk = gsintmsk & ~ints;
257
258 if (new_gsintmsk != gsintmsk)
259 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
260}
261
262/**
263 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
264 * @hsotg: The device state
265 * @ep: The endpoint index
266 * @dir_in: True if direction is in.
267 * @en: The enable value, true to enable
268 *
269 * Set or clear the mask for an individual endpoint's interrupt
270 * request.
271 */
272static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
273 unsigned int ep, unsigned int dir_in,
274 unsigned int en)
275{
276 unsigned long flags;
277 u32 bit = 1 << ep;
278 u32 daint;
279
280 if (!dir_in)
281 bit <<= 16;
282
283 local_irq_save(flags);
284 daint = readl(hsotg->regs + S3C_DAINTMSK);
285 if (en)
286 daint |= bit;
287 else
288 daint &= ~bit;
289 writel(daint, hsotg->regs + S3C_DAINTMSK);
290 local_irq_restore(flags);
291}
292
293/**
294 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
295 * @hsotg: The device instance.
296 */
297static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
298{
299 /* the ryu 2.6.24 release ahs
300 writel(0x1C0, hsotg->regs + S3C_GRXFSIZ);
301 writel(S3C_GNPTXFSIZ_NPTxFStAddr(0x200) |
302 S3C_GNPTXFSIZ_NPTxFDep(0x1C0),
303 hsotg->regs + S3C_GNPTXFSIZ);
304 */
305
306 /* set FIFO sizes to 2048/0x1C0 */
307
308 writel(2048, hsotg->regs + S3C_GRXFSIZ);
309 writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) |
310 S3C_GNPTXFSIZ_NPTxFDep(0x1C0),
311 hsotg->regs + S3C_GNPTXFSIZ);
312}
313
314/**
315 * @ep: USB endpoint to allocate request for.
316 * @flags: Allocation flags
317 *
318 * Allocate a new USB request structure appropriate for the specified endpoint
319 */
320struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep, gfp_t flags)
321{
322 struct s3c_hsotg_req *req;
323
324 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
325 if (!req)
326 return NULL;
327
328 INIT_LIST_HEAD(&req->queue);
329
330 req->req.dma = DMA_ADDR_INVALID;
331 return &req->req;
332}
333
334/**
335 * is_ep_periodic - return true if the endpoint is in periodic mode.
336 * @hs_ep: The endpoint to query.
337 *
338 * Returns true if the endpoint is in periodic mode, meaning it is being
339 * used for an Interrupt or ISO transfer.
340 */
341static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
342{
343 return hs_ep->periodic;
344}
345
346/**
347 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
348 * @hsotg: The device state.
349 * @hs_ep: The endpoint for the request
350 * @hs_req: The request being processed.
351 *
352 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
353 * of a request to ensure the buffer is ready for access by the caller.
354*/
355static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
356 struct s3c_hsotg_ep *hs_ep,
357 struct s3c_hsotg_req *hs_req)
358{
359 struct usb_request *req = &hs_req->req;
360 enum dma_data_direction dir;
361
362 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
363
364 /* ignore this if we're not moving any data */
365 if (hs_req->req.length == 0)
366 return;
367
368 if (hs_req->mapped) {
369 /* we mapped this, so unmap and remove the dma */
370
371 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
372
373 req->dma = DMA_ADDR_INVALID;
374 hs_req->mapped = 0;
375 } else {
376 dma_sync_single(hsotg->dev, req->dma, req->length, dir);
377 }
378}
379
380/**
381 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
382 * @hsotg: The controller state.
383 * @hs_ep: The endpoint we're going to write for.
384 * @hs_req: The request to write data for.
385 *
386 * This is called when the TxFIFO has some space in it to hold a new
387 * transmission and we have something to give it. The actual setup of
388 * the data size is done elsewhere, so all we have to do is to actually
389 * write the data.
390 *
391 * The return value is zero if there is more space (or nothing was done)
392 * otherwise -ENOSPC is returned if the FIFO space was used up.
393 *
394 * This routine is only needed for PIO
395*/
396static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
397 struct s3c_hsotg_ep *hs_ep,
398 struct s3c_hsotg_req *hs_req)
399{
400 bool periodic = is_ep_periodic(hs_ep);
401 u32 gnptxsts = readl(hsotg->regs + S3C_GNPTXSTS);
402 int buf_pos = hs_req->req.actual;
403 int to_write = hs_ep->size_loaded;
404 void *data;
405 int can_write;
406 int pkt_round;
407
408 to_write -= (buf_pos - hs_ep->last_load);
409
410 /* if there's nothing to write, get out early */
411 if (to_write == 0)
412 return 0;
413
414 if (periodic) {
415 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
416 int size_left;
417 int size_done;
418
419 /* work out how much data was loaded so we can calculate
420 * how much data is left in the fifo. */
421
422 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
423
424 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
425 __func__, size_left,
426 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
427
428 /* how much of the data has moved */
429 size_done = hs_ep->size_loaded - size_left;
430
431 /* how much data is left in the fifo */
432 can_write = hs_ep->fifo_load - size_done;
433 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
434 __func__, can_write);
435
436 can_write = hs_ep->fifo_size - can_write;
437 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
438 __func__, can_write);
439
440 if (can_write <= 0) {
441 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
442 return -ENOSPC;
443 }
444 } else {
445 if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
446 dev_dbg(hsotg->dev,
447 "%s: no queue slots available (0x%08x)\n",
448 __func__, gnptxsts);
449
450 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
451 return -ENOSPC;
452 }
453
454 can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
455 }
456
457 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
458 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
459
460 /* limit to 512 bytes of data, it seems at least on the non-periodic
461 * FIFO, requests of >512 cause the endpoint to get stuck with a
462 * fragment of the end of the transfer in it.
463 */
464 if (can_write > 512)
465 can_write = 512;
466
467 /* see if we can write data */
468
469 if (to_write > can_write) {
470 to_write = can_write;
471 pkt_round = to_write % hs_ep->ep.maxpacket;
472
473 /* Not sure, but we probably shouldn't be writing partial
474 * packets into the FIFO, so round the write down to an
475 * exact number of packets.
476 *
477 * Note, we do not currently check to see if we can ever
478 * write a full packet or not to the FIFO.
479 */
480
481 if (pkt_round)
482 to_write -= pkt_round;
483
484 /* enable correct FIFO interrupt to alert us when there
485 * is more room left. */
486
487 s3c_hsotg_en_gsint(hsotg,
488 periodic ? S3C_GINTSTS_PTxFEmp :
489 S3C_GINTSTS_NPTxFEmp);
490 }
491
492 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
493 to_write, hs_req->req.length, can_write, buf_pos);
494
495 if (to_write <= 0)
496 return -ENOSPC;
497
498 hs_req->req.actual = buf_pos + to_write;
499 hs_ep->total_data += to_write;
500
501 if (periodic)
502 hs_ep->fifo_load += to_write;
503
504 to_write = DIV_ROUND_UP(to_write, 4);
505 data = hs_req->req.buf + buf_pos;
506
507 writesl(hsotg->regs + S3C_EPFIFO(hs_ep->index), data, to_write);
508
509 return (to_write >= can_write) ? -ENOSPC : 0;
510}
511
512/**
513 * get_ep_limit - get the maximum data legnth for this endpoint
514 * @hs_ep: The endpoint
515 *
516 * Return the maximum data that can be queued in one go on a given endpoint
517 * so that transfers that are too long can be split.
518 */
519static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
520{
521 int index = hs_ep->index;
522 unsigned maxsize;
523 unsigned maxpkt;
524
525 if (index != 0) {
526 maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1;
527 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
528 } else {
529 if (hs_ep->dir_in) {
530 /* maxsize = S3C_DIEPTSIZ0_XferSize_LIMIT + 1; */
531 maxsize = 64+64+1;
532 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
533 } else {
534 maxsize = 0x3f;
535 maxpkt = 2;
536 }
537 }
538
539 /* we made the constant loading easier above by using +1 */
540 maxpkt--;
541 maxsize--;
542
543 /* constrain by packet count if maxpkts*pktsize is greater
544 * than the length register size. */
545
546 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
547 maxsize = maxpkt * hs_ep->ep.maxpacket;
548
549 return maxsize;
550}
551
552/**
553 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
554 * @hsotg: The controller state.
555 * @hs_ep: The endpoint to process a request for
556 * @hs_req: The request to start.
557 * @continuing: True if we are doing more for the current request.
558 *
559 * Start the given request running by setting the endpoint registers
560 * appropriately, and writing any data to the FIFOs.
561 */
562static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
563 struct s3c_hsotg_ep *hs_ep,
564 struct s3c_hsotg_req *hs_req,
565 bool continuing)
566{
567 struct usb_request *ureq = &hs_req->req;
568 int index = hs_ep->index;
569 int dir_in = hs_ep->dir_in;
570 u32 epctrl_reg;
571 u32 epsize_reg;
572 u32 epsize;
573 u32 ctrl;
574 unsigned length;
575 unsigned packets;
576 unsigned maxreq;
577
578 if (index != 0) {
579 if (hs_ep->req && !continuing) {
580 dev_err(hsotg->dev, "%s: active request\n", __func__);
581 WARN_ON(1);
582 return;
583 } else if (hs_ep->req != hs_req && continuing) {
584 dev_err(hsotg->dev,
585 "%s: continue different req\n", __func__);
586 WARN_ON(1);
587 return;
588 }
589 }
590
591 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
592 epsize_reg = dir_in ? S3C_DIEPTSIZ(index) : S3C_DOEPTSIZ(index);
593
594 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
595 __func__, readl(hsotg->regs + epctrl_reg), index,
596 hs_ep->dir_in ? "in" : "out");
597
598 length = ureq->length - ureq->actual;
599
600 if (0)
601 dev_dbg(hsotg->dev,
602 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
603 ureq->buf, length, ureq->dma,
604 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
605
606 maxreq = get_ep_limit(hs_ep);
607 if (length > maxreq) {
608 int round = maxreq % hs_ep->ep.maxpacket;
609
610 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
611 __func__, length, maxreq, round);
612
613 /* round down to multiple of packets */
614 if (round)
615 maxreq -= round;
616
617 length = maxreq;
618 }
619
620 if (length)
621 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
622 else
623 packets = 1; /* send one packet if length is zero. */
624
625 if (dir_in && index != 0)
626 epsize = S3C_DxEPTSIZ_MC(1);
627 else
628 epsize = 0;
629
630 if (index != 0 && ureq->zero) {
631 /* test for the packets being exactly right for the
632 * transfer */
633
634 if (length == (packets * hs_ep->ep.maxpacket))
635 packets++;
636 }
637
638 epsize |= S3C_DxEPTSIZ_PktCnt(packets);
639 epsize |= S3C_DxEPTSIZ_XferSize(length);
640
641 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
642 __func__, packets, length, ureq->length, epsize, epsize_reg);
643
644 /* store the request as the current one we're doing */
645 hs_ep->req = hs_req;
646
647 /* write size / packets */
648 writel(epsize, hsotg->regs + epsize_reg);
649
650 ctrl = readl(hsotg->regs + epctrl_reg);
651
652 if (ctrl & S3C_DxEPCTL_Stall) {
653 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
654
655 /* not sure what we can do here, if it is EP0 then we should
656 * get this cleared once the endpoint has transmitted the
657 * STALL packet, otherwise it needs to be cleared by the
658 * host.
659 */
660 }
661
662 if (using_dma(hsotg)) {
663 unsigned int dma_reg;
664
665 /* write DMA address to control register, buffer already
666 * synced by s3c_hsotg_ep_queue(). */
667
668 dma_reg = dir_in ? S3C_DIEPDMA(index) : S3C_DOEPDMA(index);
669 writel(ureq->dma, hsotg->regs + dma_reg);
670
671 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
672 __func__, ureq->dma, dma_reg);
673 }
674
675 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
676 ctrl |= S3C_DxEPCTL_USBActEp;
677 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
678
679 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
680 writel(ctrl, hsotg->regs + epctrl_reg);
681
682 /* set these, it seems that DMA support increments past the end
683 * of the packet buffer so we need to calculate the length from
684 * this information. */
685 hs_ep->size_loaded = length;
686 hs_ep->last_load = ureq->actual;
687
688 if (dir_in && !using_dma(hsotg)) {
689 /* set these anyway, we may need them for non-periodic in */
690 hs_ep->fifo_load = 0;
691
692 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
693 }
694
695 /* clear the INTknTXFEmpMsk when we start request, more as a aide
696 * to debugging to see what is going on. */
697 if (dir_in)
698 writel(S3C_DIEPMSK_INTknTXFEmpMsk,
699 hsotg->regs + S3C_DIEPINT(index));
700
701 /* Note, trying to clear the NAK here causes problems with transmit
702 * on the S3C6400 ending up with the TXFIFO becomming full. */
703
704 /* check ep is enabled */
705 if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna))
706 dev_warn(hsotg->dev,
707 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
708 index, readl(hsotg->regs + epctrl_reg));
709
710 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
711 __func__, readl(hsotg->regs + epctrl_reg));
712}
713
714/**
715 * s3c_hsotg_map_dma - map the DMA memory being used for the request
716 * @hsotg: The device state.
717 * @hs_ep: The endpoint the request is on.
718 * @req: The request being processed.
719 *
720 * We've been asked to queue a request, so ensure that the memory buffer
721 * is correctly setup for DMA. If we've been passed an extant DMA address
722 * then ensure the buffer has been synced to memory. If our buffer has no
723 * DMA memory, then we map the memory and mark our request to allow us to
724 * cleanup on completion.
725*/
726static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
727 struct s3c_hsotg_ep *hs_ep,
728 struct usb_request *req)
729{
730 enum dma_data_direction dir;
731 struct s3c_hsotg_req *hs_req = our_req(req);
732
733 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
734
735 /* if the length is zero, ignore the DMA data */
736 if (hs_req->req.length == 0)
737 return 0;
738
739 if (req->dma == DMA_ADDR_INVALID) {
740 dma_addr_t dma;
741
742 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
743
744 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
745 goto dma_error;
746
747 if (dma & 3) {
748 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
749 __func__);
750
751 dma_unmap_single(hsotg->dev, dma, req->length, dir);
752 return -EINVAL;
753 }
754
755 hs_req->mapped = 1;
756 req->dma = dma;
757 } else {
758 dma_sync_single(hsotg->dev, req->dma, req->length, dir);
759 hs_req->mapped = 0;
760 }
761
762 return 0;
763
764dma_error:
765 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
766 __func__, req->buf, req->length);
767
768 return -EIO;
769}
770
771static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
772 gfp_t gfp_flags)
773{
774 struct s3c_hsotg_req *hs_req = our_req(req);
775 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
776 struct s3c_hsotg *hs = hs_ep->parent;
777 unsigned long irqflags;
778 bool first;
779
780 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
781 ep->name, req, req->length, req->buf, req->no_interrupt,
782 req->zero, req->short_not_ok);
783
784 /* initialise status of the request */
785 INIT_LIST_HEAD(&hs_req->queue);
786 req->actual = 0;
787 req->status = -EINPROGRESS;
788
789 /* if we're using DMA, sync the buffers as necessary */
790 if (using_dma(hs)) {
791 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
792 if (ret)
793 return ret;
794 }
795
796 spin_lock_irqsave(&hs_ep->lock, irqflags);
797
798 first = list_empty(&hs_ep->queue);
799 list_add_tail(&hs_req->queue, &hs_ep->queue);
800
801 if (first)
802 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
803
804 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
805
806 return 0;
807}
808
809static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
810 struct usb_request *req)
811{
812 struct s3c_hsotg_req *hs_req = our_req(req);
813
814 kfree(hs_req);
815}
816
817/**
818 * s3c_hsotg_complete_oursetup - setup completion callback
819 * @ep: The endpoint the request was on.
820 * @req: The request completed.
821 *
822 * Called on completion of any requests the driver itself
823 * submitted that need cleaning up.
824 */
825static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
826 struct usb_request *req)
827{
828 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
829 struct s3c_hsotg *hsotg = hs_ep->parent;
830
831 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
832
833 s3c_hsotg_ep_free_request(ep, req);
834}
835
836/**
837 * ep_from_windex - convert control wIndex value to endpoint
838 * @hsotg: The driver state.
839 * @windex: The control request wIndex field (in host order).
840 *
841 * Convert the given wIndex into a pointer to an driver endpoint
842 * structure, or return NULL if it is not a valid endpoint.
843*/
844static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
845 u32 windex)
846{
847 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
848 int dir = (windex & USB_DIR_IN) ? 1 : 0;
849 int idx = windex & 0x7F;
850
851 if (windex >= 0x100)
852 return NULL;
853
854 if (idx > S3C_HSOTG_EPS)
855 return NULL;
856
857 if (idx && ep->dir_in != dir)
858 return NULL;
859
860 return ep;
861}
862
863/**
864 * s3c_hsotg_send_reply - send reply to control request
865 * @hsotg: The device state
866 * @ep: Endpoint 0
867 * @buff: Buffer for request
868 * @length: Length of reply.
869 *
870 * Create a request and queue it on the given endpoint. This is useful as
871 * an internal method of sending replies to certain control requests, etc.
872 */
873static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
874 struct s3c_hsotg_ep *ep,
875 void *buff,
876 int length)
877{
878 struct usb_request *req;
879 int ret;
880
881 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
882
883 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
884 hsotg->ep0_reply = req;
885 if (!req) {
886 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
887 return -ENOMEM;
888 }
889
890 req->buf = hsotg->ep0_buff;
891 req->length = length;
892 req->zero = 1; /* always do zero-length final transfer */
893 req->complete = s3c_hsotg_complete_oursetup;
894
895 if (length)
896 memcpy(req->buf, buff, length);
897 else
898 ep->sent_zlp = 1;
899
900 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
901 if (ret) {
902 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
903 return ret;
904 }
905
906 return 0;
907}
908
909/**
910 * s3c_hsotg_process_req_status - process request GET_STATUS
911 * @hsotg: The device state
912 * @ctrl: USB control request
913 */
914static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
915 struct usb_ctrlrequest *ctrl)
916{
917 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
918 struct s3c_hsotg_ep *ep;
919 __le16 reply;
920 int ret;
921
922 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
923
924 if (!ep0->dir_in) {
925 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
926 return -EINVAL;
927 }
928
929 switch (ctrl->bRequestType & USB_RECIP_MASK) {
930 case USB_RECIP_DEVICE:
931 reply = cpu_to_le16(0); /* bit 0 => self powered,
932 * bit 1 => remote wakeup */
933 break;
934
935 case USB_RECIP_INTERFACE:
936 /* currently, the data result should be zero */
937 reply = cpu_to_le16(0);
938 break;
939
940 case USB_RECIP_ENDPOINT:
941 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
942 if (!ep)
943 return -ENOENT;
944
945 reply = cpu_to_le16(ep->halted ? 1 : 0);
946 break;
947
948 default:
949 return 0;
950 }
951
952 if (le16_to_cpu(ctrl->wLength) != 2)
953 return -EINVAL;
954
955 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
956 if (ret) {
957 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
958 return ret;
959 }
960
961 return 1;
962}
963
964static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
965
966/**
967 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
968 * @hsotg: The device state
969 * @ctrl: USB control request
970 */
971static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
972 struct usb_ctrlrequest *ctrl)
973{
974 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
975 struct s3c_hsotg_ep *ep;
976
977 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
978 __func__, set ? "SET" : "CLEAR");
979
980 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
981 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
982 if (!ep) {
983 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
984 __func__, le16_to_cpu(ctrl->wIndex));
985 return -ENOENT;
986 }
987
988 switch (le16_to_cpu(ctrl->wValue)) {
989 case USB_ENDPOINT_HALT:
990 s3c_hsotg_ep_sethalt(&ep->ep, set);
991 break;
992
993 default:
994 return -ENOENT;
995 }
996 } else
997 return -ENOENT; /* currently only deal with endpoint */
998
999 return 1;
1000}
1001
1002/**
1003 * s3c_hsotg_process_control - process a control request
1004 * @hsotg: The device state
1005 * @ctrl: The control request received
1006 *
1007 * The controller has received the SETUP phase of a control request, and
1008 * needs to work out what to do next (and whether to pass it on to the
1009 * gadget driver).
1010 */
1011static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1012 struct usb_ctrlrequest *ctrl)
1013{
1014 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1015 int ret = 0;
1016 u32 dcfg;
1017
1018 ep0->sent_zlp = 0;
1019
1020 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1021 ctrl->bRequest, ctrl->bRequestType,
1022 ctrl->wValue, ctrl->wLength);
1023
1024 /* record the direction of the request, for later use when enquing
1025 * packets onto EP0. */
1026
1027 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1028 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1029
1030 /* if we've no data with this request, then the last part of the
1031 * transaction is going to implicitly be IN. */
1032 if (ctrl->wLength == 0)
1033 ep0->dir_in = 1;
1034
1035 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1036 switch (ctrl->bRequest) {
1037 case USB_REQ_SET_ADDRESS:
1038 dcfg = readl(hsotg->regs + S3C_DCFG);
1039 dcfg &= ~S3C_DCFG_DevAddr_MASK;
1040 dcfg |= ctrl->wValue << S3C_DCFG_DevAddr_SHIFT;
1041 writel(dcfg, hsotg->regs + S3C_DCFG);
1042
1043 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1044
1045 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1046 return;
1047
1048 case USB_REQ_GET_STATUS:
1049 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1050 break;
1051
1052 case USB_REQ_CLEAR_FEATURE:
1053 case USB_REQ_SET_FEATURE:
1054 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1055 break;
1056 }
1057 }
1058
1059 /* as a fallback, try delivering it to the driver to deal with */
1060
1061 if (ret == 0 && hsotg->driver) {
1062 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1063 if (ret < 0)
1064 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1065 }
1066
1067 if (ret > 0) {
1068 if (!ep0->dir_in) {
1069 /* need to generate zlp in reply or take data */
1070 /* todo - deal with any data we might be sent? */
1071 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1072 }
1073 }
1074
1075 /* the request is either unhandlable, or is not formatted correctly
1076 * so respond with a STALL for the status stage to indicate failure.
1077 */
1078
1079 if (ret < 0) {
1080 u32 reg;
1081 u32 ctrl;
1082
1083 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1084 reg = (ep0->dir_in) ? S3C_DIEPCTL0 : S3C_DOEPCTL0;
1085
1086 /* S3C_DxEPCTL_Stall will be cleared by EP once it has
1087 * taken effect, so no need to clear later. */
1088
1089 ctrl = readl(hsotg->regs + reg);
1090 ctrl |= S3C_DxEPCTL_Stall;
1091 ctrl |= S3C_DxEPCTL_CNAK;
1092 writel(ctrl, hsotg->regs + reg);
1093
1094 dev_dbg(hsotg->dev,
1095 "writen DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
1096 ctrl, reg, readl(hsotg->regs + reg));
1097
1098 /* don't belive we need to anything more to get the EP
1099 * to reply with a STALL packet */
1100 }
1101}
1102
1103static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1104
1105/**
1106 * s3c_hsotg_complete_setup - completion of a setup transfer
1107 * @ep: The endpoint the request was on.
1108 * @req: The request completed.
1109 *
1110 * Called on completion of any requests the driver itself submitted for
1111 * EP0 setup packets
1112 */
1113static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1114 struct usb_request *req)
1115{
1116 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1117 struct s3c_hsotg *hsotg = hs_ep->parent;
1118
1119 if (req->status < 0) {
1120 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1121 return;
1122 }
1123
1124 if (req->actual == 0)
1125 s3c_hsotg_enqueue_setup(hsotg);
1126 else
1127 s3c_hsotg_process_control(hsotg, req->buf);
1128}
1129
1130/**
1131 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1132 * @hsotg: The device state.
1133 *
1134 * Enqueue a request on EP0 if necessary to received any SETUP packets
1135 * received from the host.
1136 */
1137static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1138{
1139 struct usb_request *req = hsotg->ctrl_req;
1140 struct s3c_hsotg_req *hs_req = our_req(req);
1141 int ret;
1142
1143 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1144
1145 req->zero = 0;
1146 req->length = 8;
1147 req->buf = hsotg->ctrl_buff;
1148 req->complete = s3c_hsotg_complete_setup;
1149
1150 if (!list_empty(&hs_req->queue)) {
1151 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1152 return;
1153 }
1154
1155 hsotg->eps[0].dir_in = 0;
1156
1157 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1158 if (ret < 0) {
1159 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1160 /* Don't think there's much we can do other than watch the
1161 * driver fail. */
1162 }
1163}
1164
1165/**
1166 * get_ep_head - return the first request on the endpoint
1167 * @hs_ep: The controller endpoint to get
1168 *
1169 * Get the first request on the endpoint.
1170*/
1171static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1172{
1173 if (list_empty(&hs_ep->queue))
1174 return NULL;
1175
1176 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1177}
1178
1179/**
1180 * s3c_hsotg_complete_request - complete a request given to us
1181 * @hsotg: The device state.
1182 * @hs_ep: The endpoint the request was on.
1183 * @hs_req: The request to complete.
1184 * @result: The result code (0 => Ok, otherwise errno)
1185 *
1186 * The given request has finished, so call the necessary completion
1187 * if it has one and then look to see if we can start a new request
1188 * on the endpoint.
1189 *
1190 * Note, expects the ep to already be locked as appropriate.
1191*/
1192static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1193 struct s3c_hsotg_ep *hs_ep,
1194 struct s3c_hsotg_req *hs_req,
1195 int result)
1196{
1197 bool restart;
1198
1199 if (!hs_req) {
1200 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1201 return;
1202 }
1203
1204 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1205 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1206
1207 /* only replace the status if we've not already set an error
1208 * from a previous transaction */
1209
1210 if (hs_req->req.status == -EINPROGRESS)
1211 hs_req->req.status = result;
1212
1213 hs_ep->req = NULL;
1214 list_del_init(&hs_req->queue);
1215
1216 if (using_dma(hsotg))
1217 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1218
1219 /* call the complete request with the locks off, just in case the
1220 * request tries to queue more work for this endpoint. */
1221
1222 if (hs_req->req.complete) {
1223 spin_unlock(&hs_ep->lock);
1224 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1225 spin_lock(&hs_ep->lock);
1226 }
1227
1228 /* Look to see if there is anything else to do. Note, the completion
1229 * of the previous request may have caused a new request to be started
1230 * so be careful when doing this. */
1231
1232 if (!hs_ep->req && result >= 0) {
1233 restart = !list_empty(&hs_ep->queue);
1234 if (restart) {
1235 hs_req = get_ep_head(hs_ep);
1236 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1237 }
1238 }
1239}
1240
1241/**
1242 * s3c_hsotg_complete_request_lock - complete a request given to us (locked)
1243 * @hsotg: The device state.
1244 * @hs_ep: The endpoint the request was on.
1245 * @hs_req: The request to complete.
1246 * @result: The result code (0 => Ok, otherwise errno)
1247 *
1248 * See s3c_hsotg_complete_request(), but called with the endpoint's
1249 * lock held.
1250*/
1251static void s3c_hsotg_complete_request_lock(struct s3c_hsotg *hsotg,
1252 struct s3c_hsotg_ep *hs_ep,
1253 struct s3c_hsotg_req *hs_req,
1254 int result)
1255{
1256 unsigned long flags;
1257
1258 spin_lock_irqsave(&hs_ep->lock, flags);
1259 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1260 spin_unlock_irqrestore(&hs_ep->lock, flags);
1261}
1262
1263/**
1264 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1265 * @hsotg: The device state.
1266 * @ep_idx: The endpoint index for the data
1267 * @size: The size of data in the fifo, in bytes
1268 *
1269 * The FIFO status shows there is data to read from the FIFO for a given
1270 * endpoint, so sort out whether we need to read the data into a request
1271 * that has been made for that endpoint.
1272 */
1273static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1274{
1275 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1276 struct s3c_hsotg_req *hs_req = hs_ep->req;
1277 void __iomem *fifo = hsotg->regs + S3C_EPFIFO(ep_idx);
1278 int to_read;
1279 int max_req;
1280 int read_ptr;
1281
1282 if (!hs_req) {
1283 u32 epctl = readl(hsotg->regs + S3C_DOEPCTL(ep_idx));
1284 int ptr;
1285
1286 dev_warn(hsotg->dev,
1287 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1288 __func__, size, ep_idx, epctl);
1289
1290 /* dump the data from the FIFO, we've nothing we can do */
1291 for (ptr = 0; ptr < size; ptr += 4)
1292 (void)readl(fifo);
1293
1294 return;
1295 }
1296
1297 spin_lock(&hs_ep->lock);
1298
1299 to_read = size;
1300 read_ptr = hs_req->req.actual;
1301 max_req = hs_req->req.length - read_ptr;
1302
1303 if (to_read > max_req) {
1304 /* more data appeared than we where willing
1305 * to deal with in this request.
1306 */
1307
1308 /* currently we don't deal this */
1309 WARN_ON_ONCE(1);
1310 }
1311
1312 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1313 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1314
1315 hs_ep->total_data += to_read;
1316 hs_req->req.actual += to_read;
1317 to_read = DIV_ROUND_UP(to_read, 4);
1318
1319 /* note, we might over-write the buffer end by 3 bytes depending on
1320 * alignment of the data. */
1321 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1322
1323 spin_unlock(&hs_ep->lock);
1324}
1325
1326/**
1327 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1328 * @hsotg: The device instance
1329 * @req: The request currently on this endpoint
1330 *
1331 * Generate a zero-length IN packet request for terminating a SETUP
1332 * transaction.
1333 *
1334 * Note, since we don't write any data to the TxFIFO, then it is
1335 * currently belived that we do not need to wait for any space in
1336 * the TxFIFO.
1337 */
1338static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1339 struct s3c_hsotg_req *req)
1340{
1341 u32 ctrl;
1342
1343 if (!req) {
1344 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1345 return;
1346 }
1347
1348 if (req->req.length == 0) {
1349 hsotg->eps[0].sent_zlp = 1;
1350 s3c_hsotg_enqueue_setup(hsotg);
1351 return;
1352 }
1353
1354 hsotg->eps[0].dir_in = 1;
1355 hsotg->eps[0].sent_zlp = 1;
1356
1357 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1358
1359 /* issue a zero-sized packet to terminate this */
1360 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
1361 S3C_DxEPTSIZ_XferSize(0), hsotg->regs + S3C_DIEPTSIZ(0));
1362
1363 ctrl = readl(hsotg->regs + S3C_DIEPCTL0);
1364 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
1365 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
1366 ctrl |= S3C_DxEPCTL_USBActEp;
1367 writel(ctrl, hsotg->regs + S3C_DIEPCTL0);
1368}
1369
1370/**
1371 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1372 * @hsotg: The device instance
1373 * @epnum: The endpoint received from
1374 * @was_setup: Set if processing a SetupDone event.
1375 *
1376 * The RXFIFO has delivered an OutDone event, which means that the data
1377 * transfer for an OUT endpoint has been completed, either by a short
1378 * packet or by the finish of a transfer.
1379*/
1380static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1381 int epnum, bool was_setup)
1382{
1383 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1384 struct s3c_hsotg_req *hs_req = hs_ep->req;
1385 struct usb_request *req = &hs_req->req;
1386 int result = 0;
1387
1388 if (!hs_req) {
1389 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1390 return;
1391 }
1392
1393 if (using_dma(hsotg)) {
1394 u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
1395 unsigned size_done;
1396 unsigned size_left;
1397
1398 /* Calculate the size of the transfer by checking how much
1399 * is left in the endpoint size register and then working it
1400 * out from the amount we loaded for the transfer.
1401 *
1402 * We need to do this as DMA pointers are always 32bit aligned
1403 * so may overshoot/undershoot the transfer.
1404 */
1405
1406 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1407
1408 size_done = hs_ep->size_loaded - size_left;
1409 size_done += hs_ep->last_load;
1410
1411 req->actual = size_done;
1412 }
1413
1414 if (req->actual < req->length && req->short_not_ok) {
1415 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1416 __func__, req->actual, req->length);
1417
1418 /* todo - what should we return here? there's no one else
1419 * even bothering to check the status. */
1420 }
1421
1422 if (epnum == 0) {
1423 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1424 s3c_hsotg_send_zlp(hsotg, hs_req);
1425 }
1426
1427 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, result);
1428}
1429
1430/**
1431 * s3c_hsotg_read_frameno - read current frame number
1432 * @hsotg: The device instance
1433 *
1434 * Return the current frame number
1435*/
1436static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1437{
1438 u32 dsts;
1439
1440 dsts = readl(hsotg->regs + S3C_DSTS);
1441 dsts &= S3C_DSTS_SOFFN_MASK;
1442 dsts >>= S3C_DSTS_SOFFN_SHIFT;
1443
1444 return dsts;
1445}
1446
1447/**
1448 * s3c_hsotg_handle_rx - RX FIFO has data
1449 * @hsotg: The device instance
1450 *
1451 * The IRQ handler has detected that the RX FIFO has some data in it
1452 * that requires processing, so find out what is in there and do the
1453 * appropriate read.
1454 *
1455 * The RXFIFO is a true FIFO, the packets comming out are still in packet
1456 * chunks, so if you have x packets received on an endpoint you'll get x
1457 * FIFO events delivered, each with a packet's worth of data in it.
1458 *
1459 * When using DMA, we should not be processing events from the RXFIFO
1460 * as the actual data should be sent to the memory directly and we turn
1461 * on the completion interrupts to get notifications of transfer completion.
1462 */
1463void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
1464{
1465 u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP);
1466 u32 epnum, status, size;
1467
1468 WARN_ON(using_dma(hsotg));
1469
1470 epnum = grxstsr & S3C_GRXSTS_EPNum_MASK;
1471 status = grxstsr & S3C_GRXSTS_PktSts_MASK;
1472
1473 size = grxstsr & S3C_GRXSTS_ByteCnt_MASK;
1474 size >>= S3C_GRXSTS_ByteCnt_SHIFT;
1475
1476 if (1)
1477 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1478 __func__, grxstsr, size, epnum);
1479
1480#define __status(x) ((x) >> S3C_GRXSTS_PktSts_SHIFT)
1481
1482 switch (status >> S3C_GRXSTS_PktSts_SHIFT) {
1483 case __status(S3C_GRXSTS_PktSts_GlobalOutNAK):
1484 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1485 break;
1486
1487 case __status(S3C_GRXSTS_PktSts_OutDone):
1488 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1489 s3c_hsotg_read_frameno(hsotg));
1490
1491 if (!using_dma(hsotg))
1492 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1493 break;
1494
1495 case __status(S3C_GRXSTS_PktSts_SetupDone):
1496 dev_dbg(hsotg->dev,
1497 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1498 s3c_hsotg_read_frameno(hsotg),
1499 readl(hsotg->regs + S3C_DOEPCTL(0)));
1500
1501 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1502 break;
1503
1504 case __status(S3C_GRXSTS_PktSts_OutRX):
1505 s3c_hsotg_rx_data(hsotg, epnum, size);
1506 break;
1507
1508 case __status(S3C_GRXSTS_PktSts_SetupRX):
1509 dev_dbg(hsotg->dev,
1510 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1511 s3c_hsotg_read_frameno(hsotg),
1512 readl(hsotg->regs + S3C_DOEPCTL(0)));
1513
1514 s3c_hsotg_rx_data(hsotg, epnum, size);
1515 break;
1516
1517 default:
1518 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1519 __func__, grxstsr);
1520
1521 s3c_hsotg_dump(hsotg);
1522 break;
1523 }
1524}
1525
1526/**
1527 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1528 * @mps: The maximum packet size in bytes.
1529*/
1530static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1531{
1532 switch (mps) {
1533 case 64:
1534 return S3C_D0EPCTL_MPS_64;
1535 case 32:
1536 return S3C_D0EPCTL_MPS_32;
1537 case 16:
1538 return S3C_D0EPCTL_MPS_16;
1539 case 8:
1540 return S3C_D0EPCTL_MPS_8;
1541 }
1542
1543 /* bad max packet size, warn and return invalid result */
1544 WARN_ON(1);
1545 return (u32)-1;
1546}
1547
1548/**
1549 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1550 * @hsotg: The driver state.
1551 * @ep: The index number of the endpoint
1552 * @mps: The maximum packet size in bytes
1553 *
1554 * Configure the maximum packet size for the given endpoint, updating
1555 * the hardware control registers to reflect this.
1556 */
1557static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1558 unsigned int ep, unsigned int mps)
1559{
1560 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1561 void __iomem *regs = hsotg->regs;
1562 u32 mpsval;
1563 u32 reg;
1564
1565 if (ep == 0) {
1566 /* EP0 is a special case */
1567 mpsval = s3c_hsotg_ep0_mps(mps);
1568 if (mpsval > 3)
1569 goto bad_mps;
1570 } else {
1571 if (mps >= S3C_DxEPCTL_MPS_LIMIT+1)
1572 goto bad_mps;
1573
1574 mpsval = mps;
1575 }
1576
1577 hs_ep->ep.maxpacket = mps;
1578
1579 /* update both the in and out endpoint controldir_ registers, even
1580 * if one of the directions may not be in use. */
1581
1582 reg = readl(regs + S3C_DIEPCTL(ep));
1583 reg &= ~S3C_DxEPCTL_MPS_MASK;
1584 reg |= mpsval;
1585 writel(reg, regs + S3C_DIEPCTL(ep));
1586
1587 reg = readl(regs + S3C_DOEPCTL(ep));
1588 reg &= ~S3C_DxEPCTL_MPS_MASK;
1589 reg |= mpsval;
1590 writel(reg, regs + S3C_DOEPCTL(ep));
1591
1592 return;
1593
1594bad_mps:
1595 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1596}
1597
1598
1599/**
1600 * s3c_hsotg_trytx - check to see if anything needs transmitting
1601 * @hsotg: The driver state
1602 * @hs_ep: The driver endpoint to check.
1603 *
1604 * Check to see if there is a request that has data to send, and if so
1605 * make an attempt to write data into the FIFO.
1606 */
1607static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1608 struct s3c_hsotg_ep *hs_ep)
1609{
1610 struct s3c_hsotg_req *hs_req = hs_ep->req;
1611
1612 if (!hs_ep->dir_in || !hs_req)
1613 return 0;
1614
1615 if (hs_req->req.actual < hs_req->req.length) {
1616 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1617 hs_ep->index);
1618 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1619 }
1620
1621 return 0;
1622}
1623
1624/**
1625 * s3c_hsotg_complete_in - complete IN transfer
1626 * @hsotg: The device state.
1627 * @hs_ep: The endpoint that has just completed.
1628 *
1629 * An IN transfer has been completed, update the transfer's state and then
1630 * call the relevant completion routines.
1631 */
1632static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1633 struct s3c_hsotg_ep *hs_ep)
1634{
1635 struct s3c_hsotg_req *hs_req = hs_ep->req;
1636 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
1637 int size_left, size_done;
1638
1639 if (!hs_req) {
1640 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1641 return;
1642 }
1643
1644 /* Calculate the size of the transfer by checking how much is left
1645 * in the endpoint size register and then working it out from
1646 * the amount we loaded for the transfer.
1647 *
1648 * We do this even for DMA, as the transfer may have incremented
1649 * past the end of the buffer (DMA transfers are always 32bit
1650 * aligned).
1651 */
1652
1653 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1654
1655 size_done = hs_ep->size_loaded - size_left;
1656 size_done += hs_ep->last_load;
1657
1658 if (hs_req->req.actual != size_done)
1659 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1660 __func__, hs_req->req.actual, size_done);
1661
1662 hs_req->req.actual = size_done;
1663
1664 /* if we did all of the transfer, and there is more data left
1665 * around, then try restarting the rest of the request */
1666
1667 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1668 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1669 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1670 } else
1671 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, 0);
1672}
1673
1674/**
1675 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1676 * @hsotg: The driver state
1677 * @idx: The index for the endpoint (0..15)
1678 * @dir_in: Set if this is an IN endpoint
1679 *
1680 * Process and clear any interrupt pending for an individual endpoint
1681*/
1682static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1683 int dir_in)
1684{
1685 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1686 u32 epint_reg = dir_in ? S3C_DIEPINT(idx) : S3C_DOEPINT(idx);
1687 u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx);
1688 u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx);
1689 u32 ints;
1690 u32 clear = 0;
1691
1692 ints = readl(hsotg->regs + epint_reg);
1693
1694 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1695 __func__, idx, dir_in ? "in" : "out", ints);
1696
1697 if (ints & S3C_DxEPINT_XferCompl) {
1698 dev_dbg(hsotg->dev,
1699 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1700 __func__, readl(hsotg->regs + epctl_reg),
1701 readl(hsotg->regs + epsiz_reg));
1702
1703 /* we get OutDone from the FIFO, so we only need to look
1704 * at completing IN requests here */
1705 if (dir_in) {
1706 s3c_hsotg_complete_in(hsotg, hs_ep);
1707
1708 if (idx == 0)
1709 s3c_hsotg_enqueue_setup(hsotg);
1710 } else if (using_dma(hsotg)) {
1711 /* We're using DMA, we need to fire an OutDone here
1712 * as we ignore the RXFIFO. */
1713
1714 s3c_hsotg_handle_outdone(hsotg, idx, false);
1715 }
1716
1717 clear |= S3C_DxEPINT_XferCompl;
1718 }
1719
1720 if (ints & S3C_DxEPINT_EPDisbld) {
1721 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
1722 clear |= S3C_DxEPINT_EPDisbld;
1723 }
1724
1725 if (ints & S3C_DxEPINT_AHBErr) {
1726 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
1727 clear |= S3C_DxEPINT_AHBErr;
1728 }
1729
1730 if (ints & S3C_DxEPINT_Setup) { /* Setup or Timeout */
1731 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1732
1733 if (using_dma(hsotg) && idx == 0) {
1734 /* this is the notification we've received a
1735 * setup packet. In non-DMA mode we'd get this
1736 * from the RXFIFO, instead we need to process
1737 * the setup here. */
1738
1739 if (dir_in)
1740 WARN_ON_ONCE(1);
1741 else
1742 s3c_hsotg_handle_outdone(hsotg, 0, true);
1743 }
1744
1745 clear |= S3C_DxEPINT_Setup;
1746 }
1747
1748 if (ints & S3C_DxEPINT_Back2BackSetup) {
1749 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
1750 clear |= S3C_DxEPINT_Back2BackSetup;
1751 }
1752
1753 if (dir_in) {
1754 /* not sure if this is important, but we'll clear it anyway
1755 */
1756 if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) {
1757 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1758 __func__, idx);
1759 clear |= S3C_DIEPMSK_INTknTXFEmpMsk;
1760 }
1761
1762 /* this probably means something bad is happening */
1763 if (ints & S3C_DIEPMSK_INTknEPMisMsk) {
1764 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1765 __func__, idx);
1766 clear |= S3C_DIEPMSK_INTknEPMisMsk;
1767 }
1768 }
1769
1770 writel(clear, hsotg->regs + epint_reg);
1771}
1772
1773/**
1774 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1775 * @hsotg: The device state.
1776 *
1777 * Handle updating the device settings after the enumeration phase has
1778 * been completed.
1779*/
1780static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
1781{
1782 u32 dsts = readl(hsotg->regs + S3C_DSTS);
1783 int ep0_mps = 0, ep_mps;
1784
1785 /* This should signal the finish of the enumeration phase
1786 * of the USB handshaking, so we should now know what rate
1787 * we connected at. */
1788
1789 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1790
1791 /* note, since we're limited by the size of transfer on EP0, and
1792 * it seems IN transfers must be a even number of packets we do
1793 * not advertise a 64byte MPS on EP0. */
1794
1795 /* catch both EnumSpd_FS and EnumSpd_FS48 */
1796 switch (dsts & S3C_DSTS_EnumSpd_MASK) {
1797 case S3C_DSTS_EnumSpd_FS:
1798 case S3C_DSTS_EnumSpd_FS48:
1799 hsotg->gadget.speed = USB_SPEED_FULL;
1800 dev_info(hsotg->dev, "new device is full-speed\n");
1801
1802 ep0_mps = EP0_MPS_LIMIT;
1803 ep_mps = 64;
1804 break;
1805
1806 case S3C_DSTS_EnumSpd_HS:
1807 dev_info(hsotg->dev, "new device is high-speed\n");
1808 hsotg->gadget.speed = USB_SPEED_HIGH;
1809
1810 ep0_mps = EP0_MPS_LIMIT;
1811 ep_mps = 512;
1812 break;
1813
1814 case S3C_DSTS_EnumSpd_LS:
1815 hsotg->gadget.speed = USB_SPEED_LOW;
1816 dev_info(hsotg->dev, "new device is low-speed\n");
1817
1818 /* note, we don't actually support LS in this driver at the
1819 * moment, and the documentation seems to imply that it isn't
1820 * supported by the PHYs on some of the devices.
1821 */
1822 break;
1823 }
1824
1825 /* we should now know the maximum packet size for an
1826 * endpoint, so set the endpoints to a default value. */
1827
1828 if (ep0_mps) {
1829 int i;
1830 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
1831 for (i = 1; i < S3C_HSOTG_EPS; i++)
1832 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
1833 }
1834
1835 /* ensure after enumeration our EP0 is active */
1836
1837 s3c_hsotg_enqueue_setup(hsotg);
1838
1839 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
1840 readl(hsotg->regs + S3C_DIEPCTL0),
1841 readl(hsotg->regs + S3C_DOEPCTL0));
1842}
1843
1844/**
1845 * kill_all_requests - remove all requests from the endpoint's queue
1846 * @hsotg: The device state.
1847 * @ep: The endpoint the requests may be on.
1848 * @result: The result code to use.
1849 * @force: Force removal of any current requests
1850 *
1851 * Go through the requests on the given endpoint and mark them
1852 * completed with the given result code.
1853 */
1854static void kill_all_requests(struct s3c_hsotg *hsotg,
1855 struct s3c_hsotg_ep *ep,
1856 int result, bool force)
1857{
1858 struct s3c_hsotg_req *req, *treq;
1859 unsigned long flags;
1860
1861 spin_lock_irqsave(&ep->lock, flags);
1862
1863 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
1864 /* currently, we can't do much about an already
1865 * running request on an in endpoint */
1866
1867 if (ep->req == req && ep->dir_in && !force)
1868 continue;
1869
1870 s3c_hsotg_complete_request(hsotg, ep, req,
1871 result);
1872 }
1873
1874 spin_unlock_irqrestore(&ep->lock, flags);
1875}
1876
1877#define call_gadget(_hs, _entry) \
1878 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
1879 (_hs)->driver && (_hs)->driver->_entry) \
1880 (_hs)->driver->_entry(&(_hs)->gadget);
1881
1882/**
1883 * s3c_hsotg_disconnect_irq - disconnect irq service
1884 * @hsotg: The device state.
1885 *
1886 * A disconnect IRQ has been received, meaning that the host has
1887 * lost contact with the bus. Remove all current transactions
1888 * and signal the gadget driver that this has happened.
1889*/
1890static void s3c_hsotg_disconnect_irq(struct s3c_hsotg *hsotg)
1891{
1892 unsigned ep;
1893
1894 for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
1895 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
1896
1897 call_gadget(hsotg, disconnect);
1898}
1899
1900/**
1901 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
1902 * @hsotg: The device state:
1903 * @periodic: True if this is a periodic FIFO interrupt
1904 */
1905static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
1906{
1907 struct s3c_hsotg_ep *ep;
1908 int epno, ret;
1909
1910 /* look through for any more data to transmit */
1911
1912 for (epno = 0; epno < S3C_HSOTG_EPS; epno++) {
1913 ep = &hsotg->eps[epno];
1914
1915 if (!ep->dir_in)
1916 continue;
1917
1918 if ((periodic && !ep->periodic) ||
1919 (!periodic && ep->periodic))
1920 continue;
1921
1922 ret = s3c_hsotg_trytx(hsotg, ep);
1923 if (ret < 0)
1924 break;
1925 }
1926}
1927
1928static struct s3c_hsotg *our_hsotg;
1929
1930/* IRQ flags which will trigger a retry around the IRQ loop */
1931#define IRQ_RETRY_MASK (S3C_GINTSTS_NPTxFEmp | \
1932 S3C_GINTSTS_PTxFEmp | \
1933 S3C_GINTSTS_RxFLvl)
1934
1935/**
1936 * s3c_hsotg_irq - handle device interrupt
1937 * @irq: The IRQ number triggered
1938 * @pw: The pw value when registered the handler.
1939 */
1940static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
1941{
1942 struct s3c_hsotg *hsotg = pw;
1943 int retry_count = 8;
1944 u32 gintsts;
1945 u32 gintmsk;
1946
1947irq_retry:
1948 gintsts = readl(hsotg->regs + S3C_GINTSTS);
1949 gintmsk = readl(hsotg->regs + S3C_GINTMSK);
1950
1951 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
1952 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
1953
1954 gintsts &= gintmsk;
1955
1956 if (gintsts & S3C_GINTSTS_OTGInt) {
1957 u32 otgint = readl(hsotg->regs + S3C_GOTGINT);
1958
1959 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
1960
1961 writel(otgint, hsotg->regs + S3C_GOTGINT);
1962 writel(S3C_GINTSTS_OTGInt, hsotg->regs + S3C_GINTSTS);
1963 }
1964
1965 if (gintsts & S3C_GINTSTS_DisconnInt) {
1966 dev_dbg(hsotg->dev, "%s: DisconnInt\n", __func__);
1967 writel(S3C_GINTSTS_DisconnInt, hsotg->regs + S3C_GINTSTS);
1968
1969 s3c_hsotg_disconnect_irq(hsotg);
1970 }
1971
1972 if (gintsts & S3C_GINTSTS_SessReqInt) {
1973 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
1974 writel(S3C_GINTSTS_SessReqInt, hsotg->regs + S3C_GINTSTS);
1975 }
1976
1977 if (gintsts & S3C_GINTSTS_EnumDone) {
1978 s3c_hsotg_irq_enumdone(hsotg);
1979 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
1980 }
1981
1982 if (gintsts & S3C_GINTSTS_ConIDStsChng) {
1983 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
1984 readl(hsotg->regs + S3C_DSTS),
1985 readl(hsotg->regs + S3C_GOTGCTL));
1986
1987 writel(S3C_GINTSTS_ConIDStsChng, hsotg->regs + S3C_GINTSTS);
1988 }
1989
1990 if (gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt)) {
1991 u32 daint = readl(hsotg->regs + S3C_DAINT);
1992 u32 daint_out = daint >> S3C_DAINT_OutEP_SHIFT;
1993 u32 daint_in = daint & ~(daint_out << S3C_DAINT_OutEP_SHIFT);
1994 int ep;
1995
1996 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
1997
1998 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
1999 if (daint_out & 1)
2000 s3c_hsotg_epint(hsotg, ep, 0);
2001 }
2002
2003 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2004 if (daint_in & 1)
2005 s3c_hsotg_epint(hsotg, ep, 1);
2006 }
2007
2008 writel(daint, hsotg->regs + S3C_DAINT);
2009 writel(gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt),
2010 hsotg->regs + S3C_GINTSTS);
2011 }
2012
2013 if (gintsts & S3C_GINTSTS_USBRst) {
2014 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2015 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2016 readl(hsotg->regs + S3C_GNPTXSTS));
2017
2018 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2019
2020 /* it seems after a reset we can end up with a situation
2021 * where the TXFIFO still has data in it... try flushing
2022 * it to remove anything that may still be in it.
2023 */
2024
2025 if (1) {
2026 writel(S3C_GRSTCTL_TxFNum(0) | S3C_GRSTCTL_TxFFlsh,
2027 hsotg->regs + S3C_GRSTCTL);
2028
2029 dev_info(hsotg->dev, "GNPTXSTS=%08x\n",
2030 readl(hsotg->regs + S3C_GNPTXSTS));
2031 }
2032
2033 s3c_hsotg_enqueue_setup(hsotg);
2034
2035 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2036 }
2037
2038 /* check both FIFOs */
2039
2040 if (gintsts & S3C_GINTSTS_NPTxFEmp) {
2041 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2042
2043 /* Disable the interrupt to stop it happening again
2044 * unless one of these endpoint routines decides that
2045 * it needs re-enabling */
2046
2047 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2048 s3c_hsotg_irq_fifoempty(hsotg, false);
2049
2050 writel(S3C_GINTSTS_NPTxFEmp, hsotg->regs + S3C_GINTSTS);
2051 }
2052
2053 if (gintsts & S3C_GINTSTS_PTxFEmp) {
2054 dev_dbg(hsotg->dev, "PTxFEmp\n");
2055
2056 /* See note in S3C_GINTSTS_NPTxFEmp */
2057
2058 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2059 s3c_hsotg_irq_fifoempty(hsotg, true);
2060
2061 writel(S3C_GINTSTS_PTxFEmp, hsotg->regs + S3C_GINTSTS);
2062 }
2063
2064 if (gintsts & S3C_GINTSTS_RxFLvl) {
2065 /* note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
2066 * we need to retry s3c_hsotg_handle_rx if this is still
2067 * set. */
2068
2069 s3c_hsotg_handle_rx(hsotg);
2070 writel(S3C_GINTSTS_RxFLvl, hsotg->regs + S3C_GINTSTS);
2071 }
2072
2073 if (gintsts & S3C_GINTSTS_ModeMis) {
2074 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2075 writel(S3C_GINTSTS_ModeMis, hsotg->regs + S3C_GINTSTS);
2076 }
2077
2078 if (gintsts & S3C_GINTSTS_USBSusp) {
2079 dev_info(hsotg->dev, "S3C_GINTSTS_USBSusp\n");
2080 writel(S3C_GINTSTS_USBSusp, hsotg->regs + S3C_GINTSTS);
2081
2082 call_gadget(hsotg, suspend);
2083 }
2084
2085 if (gintsts & S3C_GINTSTS_WkUpInt) {
2086 dev_info(hsotg->dev, "S3C_GINTSTS_WkUpIn\n");
2087 writel(S3C_GINTSTS_WkUpInt, hsotg->regs + S3C_GINTSTS);
2088
2089 call_gadget(hsotg, resume);
2090 }
2091
2092 if (gintsts & S3C_GINTSTS_ErlySusp) {
2093 dev_dbg(hsotg->dev, "S3C_GINTSTS_ErlySusp\n");
2094 writel(S3C_GINTSTS_ErlySusp, hsotg->regs + S3C_GINTSTS);
2095 }
2096
2097 /* these next two seem to crop-up occasionally causing the core
2098 * to shutdown the USB transfer, so try clearing them and logging
2099 * the occurence. */
2100
2101 if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2102 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2103
2104 s3c_hsotg_dump(hsotg);
2105
2106 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
2107 writel(S3C_GINTSTS_GOUTNakEff, hsotg->regs + S3C_GINTSTS);
2108 }
2109
2110 if (gintsts & S3C_GINTSTS_GINNakEff) {
2111 dev_info(hsotg->dev, "GINNakEff triggered\n");
2112
2113 s3c_hsotg_dump(hsotg);
2114
2115 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
2116 writel(S3C_GINTSTS_GINNakEff, hsotg->regs + S3C_GINTSTS);
2117 }
2118
2119 /* if we've had fifo events, we should try and go around the
2120 * loop again to see if there's any point in returning yet. */
2121
2122 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2123 goto irq_retry;
2124
2125 return IRQ_HANDLED;
2126}
2127
2128/**
2129 * s3c_hsotg_ep_enable - enable the given endpoint
2130 * @ep: The USB endpint to configure
2131 * @desc: The USB endpoint descriptor to configure with.
2132 *
2133 * This is called from the USB gadget code's usb_ep_enable().
2134*/
2135static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2136 const struct usb_endpoint_descriptor *desc)
2137{
2138 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2139 struct s3c_hsotg *hsotg = hs_ep->parent;
2140 unsigned long flags;
2141 int index = hs_ep->index;
2142 u32 epctrl_reg;
2143 u32 epctrl;
2144 u32 mps;
2145 int dir_in;
2146
2147 dev_dbg(hsotg->dev,
2148 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2149 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2150 desc->wMaxPacketSize, desc->bInterval);
2151
2152 /* not to be called for EP0 */
2153 WARN_ON(index == 0);
2154
2155 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2156 if (dir_in != hs_ep->dir_in) {
2157 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2158 return -EINVAL;
2159 }
2160
2161 mps = le16_to_cpu(desc->wMaxPacketSize);
2162
2163 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2164
2165 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2166 epctrl = readl(hsotg->regs + epctrl_reg);
2167
2168 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2169 __func__, epctrl, epctrl_reg);
2170
2171 spin_lock_irqsave(&hs_ep->lock, flags);
2172
2173 epctrl &= ~(S3C_DxEPCTL_EPType_MASK | S3C_DxEPCTL_MPS_MASK);
2174 epctrl |= S3C_DxEPCTL_MPS(mps);
2175
2176 /* mark the endpoint as active, otherwise the core may ignore
2177 * transactions entirely for this endpoint */
2178 epctrl |= S3C_DxEPCTL_USBActEp;
2179
2180 /* set the NAK status on the endpoint, otherwise we might try and
2181 * do something with data that we've yet got a request to process
2182 * since the RXFIFO will take data for an endpoint even if the
2183 * size register hasn't been set.
2184 */
2185
2186 epctrl |= S3C_DxEPCTL_SNAK;
2187
2188 /* update the endpoint state */
2189 hs_ep->ep.maxpacket = mps;
2190
2191 /* default, set to non-periodic */
2192 hs_ep->periodic = 0;
2193
2194 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2195 case USB_ENDPOINT_XFER_ISOC:
2196 dev_err(hsotg->dev, "no current ISOC support\n");
2197 return -EINVAL;
2198
2199 case USB_ENDPOINT_XFER_BULK:
2200 epctrl |= S3C_DxEPCTL_EPType_Bulk;
2201 break;
2202
2203 case USB_ENDPOINT_XFER_INT:
2204 if (dir_in) {
2205 /* Allocate our TxFNum by simply using the index
2206 * of the endpoint for the moment. We could do
2207 * something better if the host indicates how
2208 * many FIFOs we are expecting to use. */
2209
2210 hs_ep->periodic = 1;
2211 epctrl |= S3C_DxEPCTL_TxFNum(index);
2212 }
2213
2214 epctrl |= S3C_DxEPCTL_EPType_Intterupt;
2215 break;
2216
2217 case USB_ENDPOINT_XFER_CONTROL:
2218 epctrl |= S3C_DxEPCTL_EPType_Control;
2219 break;
2220 }
2221
2222 /* for non control endpoints, set PID to D0 */
2223 if (index)
2224 epctrl |= S3C_DxEPCTL_SetD0PID;
2225
2226 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2227 __func__, epctrl);
2228
2229 writel(epctrl, hsotg->regs + epctrl_reg);
2230 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2231 __func__, readl(hsotg->regs + epctrl_reg));
2232
2233 /* enable the endpoint interrupt */
2234 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2235
2236 spin_unlock_irqrestore(&hs_ep->lock, flags);
2237 return 0;
2238}
2239
2240static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2241{
2242 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2243 struct s3c_hsotg *hsotg = hs_ep->parent;
2244 int dir_in = hs_ep->dir_in;
2245 int index = hs_ep->index;
2246 unsigned long flags;
2247 u32 epctrl_reg;
2248 u32 ctrl;
2249
2250 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2251
2252 if (ep == &hsotg->eps[0].ep) {
2253 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2254 return -EINVAL;
2255 }
2256
2257 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2258
2259 /* terminate all requests with shutdown */
2260 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2261
2262 spin_lock_irqsave(&hs_ep->lock, flags);
2263
2264 ctrl = readl(hsotg->regs + epctrl_reg);
2265 ctrl &= ~S3C_DxEPCTL_EPEna;
2266 ctrl &= ~S3C_DxEPCTL_USBActEp;
2267 ctrl |= S3C_DxEPCTL_SNAK;
2268
2269 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2270 writel(ctrl, hsotg->regs + epctrl_reg);
2271
2272 /* disable endpoint interrupts */
2273 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2274
2275 spin_unlock_irqrestore(&hs_ep->lock, flags);
2276 return 0;
2277}
2278
2279/**
2280 * on_list - check request is on the given endpoint
2281 * @ep: The endpoint to check.
2282 * @test: The request to test if it is on the endpoint.
2283*/
2284static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2285{
2286 struct s3c_hsotg_req *req, *treq;
2287
2288 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2289 if (req == test)
2290 return true;
2291 }
2292
2293 return false;
2294}
2295
2296static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2297{
2298 struct s3c_hsotg_req *hs_req = our_req(req);
2299 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2300 struct s3c_hsotg *hs = hs_ep->parent;
2301 unsigned long flags;
2302
2303 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2304
2305 if (hs_req == hs_ep->req) {
2306 dev_dbg(hs->dev, "%s: already in progress\n", __func__);
2307 return -EINPROGRESS;
2308 }
2309
2310 spin_lock_irqsave(&hs_ep->lock, flags);
2311
2312 if (!on_list(hs_ep, hs_req)) {
2313 spin_unlock_irqrestore(&hs_ep->lock, flags);
2314 return -EINVAL;
2315 }
2316
2317 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2318 spin_unlock_irqrestore(&hs_ep->lock, flags);
2319
2320 return 0;
2321}
2322
2323static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2324{
2325 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2326 struct s3c_hsotg *hs = hs_ep->parent;
2327 int index = hs_ep->index;
2328 unsigned long irqflags;
2329 u32 epreg;
2330 u32 epctl;
2331
2332 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2333
2334 spin_lock_irqsave(&hs_ep->lock, irqflags);
2335
2336 /* write both IN and OUT control registers */
2337
2338 epreg = S3C_DIEPCTL(index);
2339 epctl = readl(hs->regs + epreg);
2340
2341 if (value)
2342 epctl |= S3C_DxEPCTL_Stall;
2343 else
2344 epctl &= ~S3C_DxEPCTL_Stall;
2345
2346 writel(epctl, hs->regs + epreg);
2347
2348 epreg = S3C_DOEPCTL(index);
2349 epctl = readl(hs->regs + epreg);
2350
2351 if (value)
2352 epctl |= S3C_DxEPCTL_Stall;
2353 else
2354 epctl &= ~S3C_DxEPCTL_Stall;
2355
2356 writel(epctl, hs->regs + epreg);
2357
2358 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
2359
2360 return 0;
2361}
2362
2363static struct usb_ep_ops s3c_hsotg_ep_ops = {
2364 .enable = s3c_hsotg_ep_enable,
2365 .disable = s3c_hsotg_ep_disable,
2366 .alloc_request = s3c_hsotg_ep_alloc_request,
2367 .free_request = s3c_hsotg_ep_free_request,
2368 .queue = s3c_hsotg_ep_queue,
2369 .dequeue = s3c_hsotg_ep_dequeue,
2370 .set_halt = s3c_hsotg_ep_sethalt,
2371 /* note, don't belive we have any call for the fifo routines */
2372};
2373
2374/**
2375 * s3c_hsotg_corereset - issue softreset to the core
2376 * @hsotg: The device state
2377 *
2378 * Issue a soft reset to the core, and await the core finishing it.
2379*/
2380static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2381{
2382 int timeout;
2383 u32 grstctl;
2384
2385 dev_dbg(hsotg->dev, "resetting core\n");
2386
2387 /* issue soft reset */
2388 writel(S3C_GRSTCTL_CSftRst, hsotg->regs + S3C_GRSTCTL);
2389
2390 timeout = 1000;
2391 do {
2392 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2393 } while (!(grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
2394
2395 if (!grstctl & S3C_GRSTCTL_CSftRst) {
2396 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2397 return -EINVAL;
2398 }
2399
2400 timeout = 1000;
2401
2402 while (1) {
2403 u32 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2404
2405 if (timeout-- < 0) {
2406 dev_info(hsotg->dev,
2407 "%s: reset failed, GRSTCTL=%08x\n",
2408 __func__, grstctl);
2409 return -ETIMEDOUT;
2410 }
2411
2412 if (grstctl & S3C_GRSTCTL_CSftRst)
2413 continue;
2414
2415 if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2416 continue;
2417
2418 break; /* reset done */
2419 }
2420
2421 dev_dbg(hsotg->dev, "reset successful\n");
2422 return 0;
2423}
2424
2425int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2426{
2427 struct s3c_hsotg *hsotg = our_hsotg;
2428 int ret;
2429
2430 if (!hsotg) {
2431 printk(KERN_ERR "%s: called with no device\n", __func__);
2432 return -ENODEV;
2433 }
2434
2435 if (!driver) {
2436 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2437 return -EINVAL;
2438 }
2439
2440 if (driver->speed != USB_SPEED_HIGH &&
2441 driver->speed != USB_SPEED_FULL) {
2442 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
2443 }
2444
2445 if (!driver->bind || !driver->setup) {
2446 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2447 return -EINVAL;
2448 }
2449
2450 WARN_ON(hsotg->driver);
2451
2452 driver->driver.bus = NULL;
2453 hsotg->driver = driver;
2454 hsotg->gadget.dev.driver = &driver->driver;
2455 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2456 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2457
2458 ret = device_add(&hsotg->gadget.dev);
2459 if (ret) {
2460 dev_err(hsotg->dev, "failed to register gadget device\n");
2461 goto err;
2462 }
2463
2464 ret = driver->bind(&hsotg->gadget);
2465 if (ret) {
2466 dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name);
2467
2468 hsotg->gadget.dev.driver = NULL;
2469 hsotg->driver = NULL;
2470 goto err;
2471 }
2472
2473 /* we must now enable ep0 ready for host detection and then
2474 * set configuration. */
2475
2476 s3c_hsotg_corereset(hsotg);
2477
2478 /* set the PLL on, remove the HNP/SRP and set the PHY */
2479 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) |
2480 (0x5 << 10), hsotg->regs + S3C_GUSBCFG);
2481
2482 /* looks like soft-reset changes state of FIFOs */
2483 s3c_hsotg_init_fifo(hsotg);
2484
2485 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2486
2487 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG);
2488
2489 writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt |
2490 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2491 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
2492 S3C_GINTSTS_USBSusp | S3C_GINTSTS_WkUpInt |
2493 S3C_GINTSTS_GOUTNakEff | S3C_GINTSTS_GINNakEff |
2494 S3C_GINTSTS_ErlySusp,
2495 hsotg->regs + S3C_GINTMSK);
2496
2497 if (using_dma(hsotg))
2498 writel(S3C_GAHBCFG_GlblIntrEn | S3C_GAHBCFG_DMAEn |
2499 S3C_GAHBCFG_HBstLen_Incr4,
2500 hsotg->regs + S3C_GAHBCFG);
2501 else
2502 writel(S3C_GAHBCFG_GlblIntrEn, hsotg->regs + S3C_GAHBCFG);
2503
2504 /* Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2505 * up being flooded with interrupts if the host is polling the
2506 * endpoint to try and read data. */
2507
2508 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2509 S3C_DIEPMSK_INTknEPMisMsk |
2510 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2511 hsotg->regs + S3C_DIEPMSK);
2512
2513 /* don't need XferCompl, we get that from RXFIFO in slave mode. In
2514 * DMA mode we may need this. */
2515 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2516 S3C_DOEPMSK_EPDisbldMsk |
2517 using_dma(hsotg) ? (S3C_DIEPMSK_XferComplMsk |
2518 S3C_DIEPMSK_TimeOUTMsk) : 0,
2519 hsotg->regs + S3C_DOEPMSK);
2520
2521 writel(0, hsotg->regs + S3C_DAINTMSK);
2522
2523 dev_info(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2524 readl(hsotg->regs + S3C_DIEPCTL0),
2525 readl(hsotg->regs + S3C_DOEPCTL0));
2526
2527 /* enable in and out endpoint interrupts */
2528 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
2529
2530 /* Enable the RXFIFO when in slave mode, as this is how we collect
2531 * the data. In DMA mode, we get events from the FIFO but also
2532 * things we cannot process, so do not use it. */
2533 if (!using_dma(hsotg))
2534 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_RxFLvl);
2535
2536 /* Enable interrupts for EP0 in and out */
2537 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2538 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2539
2540 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2541 udelay(10); /* see openiboot */
2542 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2543
2544 dev_info(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
2545
2546 /* S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2547 writing to the EPCTL register.. */
2548
2549 /* set to read 1 8byte packet */
2550 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
2551 S3C_DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2552
2553 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2554 S3C_DxEPCTL_CNAK | S3C_DxEPCTL_EPEna |
2555 S3C_DxEPCTL_USBActEp,
2556 hsotg->regs + S3C_DOEPCTL0);
2557
2558 /* enable, but don't activate EP0in */
2559 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2560 S3C_DxEPCTL_USBActEp, hsotg->regs + S3C_DIEPCTL0);
2561
2562 s3c_hsotg_enqueue_setup(hsotg);
2563
2564 dev_info(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2565 readl(hsotg->regs + S3C_DIEPCTL0),
2566 readl(hsotg->regs + S3C_DOEPCTL0));
2567
2568 /* clear global NAKs */
2569 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
2570 hsotg->regs + S3C_DCTL);
2571
2572 /* remove the soft-disconnect and let's go */
2573 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2574
2575 /* report to the user, and return */
2576
2577 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2578 return 0;
2579
2580err:
2581 hsotg->driver = NULL;
2582 hsotg->gadget.dev.driver = NULL;
2583 return ret;
2584}
2585
2586int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2587{
2588 struct s3c_hsotg *hsotg = our_hsotg;
2589 int ep;
2590
2591 if (!hsotg)
2592 return -ENODEV;
2593
2594 if (!driver || driver != hsotg->driver || !driver->unbind)
2595 return -EINVAL;
2596
2597 /* all endpoints should be shutdown */
2598 for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2599 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2600
2601 call_gadget(hsotg, disconnect);
2602
2603 driver->unbind(&hsotg->gadget);
2604 hsotg->driver = NULL;
2605 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2606
2607 device_del(&hsotg->gadget.dev);
2608
2609 dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
2610 driver->driver.name);
2611
2612 return 0;
2613}
2614EXPORT_SYMBOL(usb_gadget_unregister_driver);
2615
2616static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2617{
2618 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2619}
2620
2621static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
2622 .get_frame = s3c_hsotg_gadget_getframe,
2623};
2624
2625/**
2626 * s3c_hsotg_initep - initialise a single endpoint
2627 * @hsotg: The device state.
2628 * @hs_ep: The endpoint to be initialised.
2629 * @epnum: The endpoint number
2630 *
2631 * Initialise the given endpoint (as part of the probe and device state
2632 * creation) to give to the gadget driver. Setup the endpoint name, any
2633 * direction information and other state that may be required.
2634 */
2635static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2636 struct s3c_hsotg_ep *hs_ep,
2637 int epnum)
2638{
2639 u32 ptxfifo;
2640 char *dir;
2641
2642 if (epnum == 0)
2643 dir = "";
2644 else if ((epnum % 2) == 0) {
2645 dir = "out";
2646 } else {
2647 dir = "in";
2648 hs_ep->dir_in = 1;
2649 }
2650
2651 hs_ep->index = epnum;
2652
2653 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
2654
2655 INIT_LIST_HEAD(&hs_ep->queue);
2656 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
2657
2658 spin_lock_init(&hs_ep->lock);
2659
2660 /* add to the list of endpoints known by the gadget driver */
2661 if (epnum)
2662 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
2663
2664 hs_ep->parent = hsotg;
2665 hs_ep->ep.name = hs_ep->name;
2666 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
2667 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
2668
2669 /* Read the FIFO size for the Periodic TX FIFO, even if we're
2670 * an OUT endpoint, we may as well do this if in future the
2671 * code is changed to make each endpoint's direction changeable.
2672 */
2673
2674 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
2675 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo);
2676
2677 /* if we're using dma, we need to set the next-endpoint pointer
2678 * to be something valid.
2679 */
2680
2681 if (using_dma(hsotg)) {
2682 u32 next = S3C_DxEPCTL_NextEp((epnum + 1) % 15);
2683 writel(next, hsotg->regs + S3C_DIEPCTL(epnum));
2684 writel(next, hsotg->regs + S3C_DOEPCTL(epnum));
2685 }
2686}
2687
2688/**
2689 * s3c_hsotg_otgreset - reset the OtG phy block
2690 * @hsotg: The host state.
2691 *
2692 * Power up the phy, set the basic configuration and start the PHY.
2693 */
2694static void s3c_hsotg_otgreset(struct s3c_hsotg *hsotg)
2695{
2696 u32 osc;
2697
2698 writel(0, S3C_PHYPWR);
2699 mdelay(1);
2700
2701 osc = hsotg->plat->is_osc ? S3C_PHYCLK_EXT_OSC : 0;
2702
2703 writel(osc | 0x10, S3C_PHYCLK);
2704
2705 /* issue a full set of resets to the otg and core */
2706
2707 writel(S3C_RSTCON_PHY, S3C_RSTCON);
2708 udelay(20); /* at-least 10uS */
2709 writel(0, S3C_RSTCON);
2710}
2711
2712
2713static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2714{
2715 /* unmask subset of endpoint interrupts */
2716
2717 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2718 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2719 hsotg->regs + S3C_DIEPMSK);
2720
2721 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2722 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_XferComplMsk,
2723 hsotg->regs + S3C_DOEPMSK);
2724
2725 writel(0, hsotg->regs + S3C_DAINTMSK);
2726
2727 if (0) {
2728 /* post global nak until we're ready */
2729 writel(S3C_DCTL_SGNPInNAK | S3C_DCTL_SGOUTNak,
2730 hsotg->regs + S3C_DCTL);
2731 }
2732
2733 /* setup fifos */
2734
2735 dev_info(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2736 readl(hsotg->regs + S3C_GRXFSIZ),
2737 readl(hsotg->regs + S3C_GNPTXFSIZ));
2738
2739 s3c_hsotg_init_fifo(hsotg);
2740
2741 /* set the PLL on, remove the HNP/SRP and set the PHY */
2742 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) | (0x5 << 10),
2743 hsotg->regs + S3C_GUSBCFG);
2744
2745 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2746 hsotg->regs + S3C_GAHBCFG);
2747}
2748
2749static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2750{
2751 struct device *dev = hsotg->dev;
2752 void __iomem *regs = hsotg->regs;
2753 u32 val;
2754 int idx;
2755
2756 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
2757 readl(regs + S3C_DCFG), readl(regs + S3C_DCTL),
2758 readl(regs + S3C_DIEPMSK));
2759
2760 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
2761 readl(regs + S3C_GAHBCFG), readl(regs + 0x44));
2762
2763 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2764 readl(regs + S3C_GRXFSIZ), readl(regs + S3C_GNPTXFSIZ));
2765
2766 /* show periodic fifo settings */
2767
2768 for (idx = 1; idx <= 15; idx++) {
2769 val = readl(regs + S3C_DPTXFSIZn(idx));
2770 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
2771 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
2772 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
2773 }
2774
2775 for (idx = 0; idx < 15; idx++) {
2776 dev_info(dev,
2777 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
2778 readl(regs + S3C_DIEPCTL(idx)),
2779 readl(regs + S3C_DIEPTSIZ(idx)),
2780 readl(regs + S3C_DIEPDMA(idx)));
2781
2782 val = readl(regs + S3C_DOEPCTL(idx));
2783 dev_info(dev,
2784 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
2785 idx, readl(regs + S3C_DOEPCTL(idx)),
2786 readl(regs + S3C_DOEPTSIZ(idx)),
2787 readl(regs + S3C_DOEPDMA(idx)));
2788
2789 }
2790
2791 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
2792 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
2793}
2794
2795
2796/**
2797 * state_show - debugfs: show overall driver and device state.
2798 * @seq: The seq file to write to.
2799 * @v: Unused parameter.
2800 *
2801 * This debugfs entry shows the overall state of the hardware and
2802 * some general information about each of the endpoints available
2803 * to the system.
2804 */
2805static int state_show(struct seq_file *seq, void *v)
2806{
2807 struct s3c_hsotg *hsotg = seq->private;
2808 void __iomem *regs = hsotg->regs;
2809 int idx;
2810
2811 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
2812 readl(regs + S3C_DCFG),
2813 readl(regs + S3C_DCTL),
2814 readl(regs + S3C_DSTS));
2815
2816 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
2817 readl(regs + S3C_DIEPMSK), readl(regs + S3C_DOEPMSK));
2818
2819 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
2820 readl(regs + S3C_GINTMSK),
2821 readl(regs + S3C_GINTSTS));
2822
2823 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
2824 readl(regs + S3C_DAINTMSK),
2825 readl(regs + S3C_DAINT));
2826
2827 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
2828 readl(regs + S3C_GNPTXSTS),
2829 readl(regs + S3C_GRXSTSR));
2830
2831 seq_printf(seq, "\nEndpoint status:\n");
2832
2833 for (idx = 0; idx < 15; idx++) {
2834 u32 in, out;
2835
2836 in = readl(regs + S3C_DIEPCTL(idx));
2837 out = readl(regs + S3C_DOEPCTL(idx));
2838
2839 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
2840 idx, in, out);
2841
2842 in = readl(regs + S3C_DIEPTSIZ(idx));
2843 out = readl(regs + S3C_DOEPTSIZ(idx));
2844
2845 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
2846 in, out);
2847
2848 seq_printf(seq, "\n");
2849 }
2850
2851 return 0;
2852}
2853
2854static int state_open(struct inode *inode, struct file *file)
2855{
2856 return single_open(file, state_show, inode->i_private);
2857}
2858
2859static const struct file_operations state_fops = {
2860 .owner = THIS_MODULE,
2861 .open = state_open,
2862 .read = seq_read,
2863 .llseek = seq_lseek,
2864 .release = single_release,
2865};
2866
2867/**
2868 * fifo_show - debugfs: show the fifo information
2869 * @seq: The seq_file to write data to.
2870 * @v: Unused parameter.
2871 *
2872 * Show the FIFO information for the overall fifo and all the
2873 * periodic transmission FIFOs.
2874*/
2875static int fifo_show(struct seq_file *seq, void *v)
2876{
2877 struct s3c_hsotg *hsotg = seq->private;
2878 void __iomem *regs = hsotg->regs;
2879 u32 val;
2880 int idx;
2881
2882 seq_printf(seq, "Non-periodic FIFOs:\n");
2883 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + S3C_GRXFSIZ));
2884
2885 val = readl(regs + S3C_GNPTXFSIZ);
2886 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
2887 val >> S3C_GNPTXFSIZ_NPTxFDep_SHIFT,
2888 val & S3C_GNPTXFSIZ_NPTxFStAddr_MASK);
2889
2890 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
2891
2892 for (idx = 1; idx <= 15; idx++) {
2893 val = readl(regs + S3C_DPTXFSIZn(idx));
2894
2895 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
2896 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
2897 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
2898 }
2899
2900 return 0;
2901}
2902
2903static int fifo_open(struct inode *inode, struct file *file)
2904{
2905 return single_open(file, fifo_show, inode->i_private);
2906}
2907
2908static const struct file_operations fifo_fops = {
2909 .owner = THIS_MODULE,
2910 .open = fifo_open,
2911 .read = seq_read,
2912 .llseek = seq_lseek,
2913 .release = single_release,
2914};
2915
2916
2917static const char *decode_direction(int is_in)
2918{
2919 return is_in ? "in" : "out";
2920}
2921
2922/**
2923 * ep_show - debugfs: show the state of an endpoint.
2924 * @seq: The seq_file to write data to.
2925 * @v: Unused parameter.
2926 *
2927 * This debugfs entry shows the state of the given endpoint (one is
2928 * registered for each available).
2929*/
2930static int ep_show(struct seq_file *seq, void *v)
2931{
2932 struct s3c_hsotg_ep *ep = seq->private;
2933 struct s3c_hsotg *hsotg = ep->parent;
2934 struct s3c_hsotg_req *req;
2935 void __iomem *regs = hsotg->regs;
2936 int index = ep->index;
2937 int show_limit = 15;
2938 unsigned long flags;
2939
2940 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
2941 ep->index, ep->ep.name, decode_direction(ep->dir_in));
2942
2943 /* first show the register state */
2944
2945 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
2946 readl(regs + S3C_DIEPCTL(index)),
2947 readl(regs + S3C_DOEPCTL(index)));
2948
2949 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
2950 readl(regs + S3C_DIEPDMA(index)),
2951 readl(regs + S3C_DOEPDMA(index)));
2952
2953 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
2954 readl(regs + S3C_DIEPINT(index)),
2955 readl(regs + S3C_DOEPINT(index)));
2956
2957 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
2958 readl(regs + S3C_DIEPTSIZ(index)),
2959 readl(regs + S3C_DOEPTSIZ(index)));
2960
2961 seq_printf(seq, "\n");
2962 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
2963 seq_printf(seq, "total_data=%ld\n", ep->total_data);
2964
2965 seq_printf(seq, "request list (%p,%p):\n",
2966 ep->queue.next, ep->queue.prev);
2967
2968 spin_lock_irqsave(&ep->lock, flags);
2969
2970 list_for_each_entry(req, &ep->queue, queue) {
2971 if (--show_limit < 0) {
2972 seq_printf(seq, "not showing more requests...\n");
2973 break;
2974 }
2975
2976 seq_printf(seq, "%c req %p: %d bytes @%p, ",
2977 req == ep->req ? '*' : ' ',
2978 req, req->req.length, req->req.buf);
2979 seq_printf(seq, "%d done, res %d\n",
2980 req->req.actual, req->req.status);
2981 }
2982
2983 spin_unlock_irqrestore(&ep->lock, flags);
2984
2985 return 0;
2986}
2987
2988static int ep_open(struct inode *inode, struct file *file)
2989{
2990 return single_open(file, ep_show, inode->i_private);
2991}
2992
2993static const struct file_operations ep_fops = {
2994 .owner = THIS_MODULE,
2995 .open = ep_open,
2996 .read = seq_read,
2997 .llseek = seq_lseek,
2998 .release = single_release,
2999};
3000
3001/**
3002 * s3c_hsotg_create_debug - create debugfs directory and files
3003 * @hsotg: The driver state
3004 *
3005 * Create the debugfs files to allow the user to get information
3006 * about the state of the system. The directory name is created
3007 * with the same name as the device itself, in case we end up
3008 * with multiple blocks in future systems.
3009*/
3010static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3011{
3012 struct dentry *root;
3013 unsigned epidx;
3014
3015 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3016 hsotg->debug_root = root;
3017 if (IS_ERR(root)) {
3018 dev_err(hsotg->dev, "cannot create debug root\n");
3019 return;
3020 }
3021
3022 /* create general state file */
3023
3024 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3025 hsotg, &state_fops);
3026
3027 if (IS_ERR(hsotg->debug_file))
3028 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3029
3030 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3031 hsotg, &fifo_fops);
3032
3033 if (IS_ERR(hsotg->debug_fifo))
3034 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3035
3036 /* create one file for each endpoint */
3037
3038 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3039 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3040
3041 ep->debugfs = debugfs_create_file(ep->name, 0444,
3042 root, ep, &ep_fops);
3043
3044 if (IS_ERR(ep->debugfs))
3045 dev_err(hsotg->dev, "failed to create %s debug file\n",
3046 ep->name);
3047 }
3048}
3049
3050/**
3051 * s3c_hsotg_delete_debug - cleanup debugfs entries
3052 * @hsotg: The driver state
3053 *
3054 * Cleanup (remove) the debugfs files for use on module exit.
3055*/
3056static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3057{
3058 unsigned epidx;
3059
3060 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3061 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3062 debugfs_remove(ep->debugfs);
3063 }
3064
3065 debugfs_remove(hsotg->debug_file);
3066 debugfs_remove(hsotg->debug_fifo);
3067 debugfs_remove(hsotg->debug_root);
3068}
3069
3070/**
3071 * s3c_hsotg_gate - set the hardware gate for the block
3072 * @pdev: The device we bound to
3073 * @on: On or off.
3074 *
3075 * Set the hardware gate setting into the block. If we end up on
3076 * something other than an S3C64XX, then we might need to change this
3077 * to using a platform data callback, or some other mechanism.
3078 */
3079static void s3c_hsotg_gate(struct platform_device *pdev, bool on)
3080{
3081 unsigned long flags;
3082 u32 others;
3083
3084 local_irq_save(flags);
3085
3086 others = __raw_readl(S3C64XX_OTHERS);
3087 if (on)
3088 others |= S3C64XX_OTHERS_USBMASK;
3089 else
3090 others &= ~S3C64XX_OTHERS_USBMASK;
3091 __raw_writel(others, S3C64XX_OTHERS);
3092
3093 local_irq_restore(flags);
3094}
3095
3096struct s3c_hsotg_plat s3c_hsotg_default_pdata;
3097
3098static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3099{
3100 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3101 struct device *dev = &pdev->dev;
3102 struct s3c_hsotg *hsotg;
3103 struct resource *res;
3104 int epnum;
3105 int ret;
3106
3107 if (!plat)
3108 plat = &s3c_hsotg_default_pdata;
3109
3110 hsotg = kzalloc(sizeof(struct s3c_hsotg) +
3111 sizeof(struct s3c_hsotg_ep) * S3C_HSOTG_EPS,
3112 GFP_KERNEL);
3113 if (!hsotg) {
3114 dev_err(dev, "cannot get memory\n");
3115 return -ENOMEM;
3116 }
3117
3118 hsotg->dev = dev;
3119 hsotg->plat = plat;
3120
3121 platform_set_drvdata(pdev, hsotg);
3122
3123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3124 if (!res) {
3125 dev_err(dev, "cannot find register resource 0\n");
3126 ret = -EINVAL;
3127 goto err_mem;
3128 }
3129
3130 hsotg->regs_res = request_mem_region(res->start, resource_size(res),
3131 dev_name(dev));
3132 if (!hsotg->regs_res) {
3133 dev_err(dev, "cannot reserve registers\n");
3134 ret = -ENOENT;
3135 goto err_mem;
3136 }
3137
3138 hsotg->regs = ioremap(res->start, resource_size(res));
3139 if (!hsotg->regs) {
3140 dev_err(dev, "cannot map registers\n");
3141 ret = -ENXIO;
3142 goto err_regs_res;
3143 }
3144
3145 ret = platform_get_irq(pdev, 0);
3146 if (ret < 0) {
3147 dev_err(dev, "cannot find IRQ\n");
3148 goto err_regs;
3149 }
3150
3151 hsotg->irq = ret;
3152
3153 ret = request_irq(ret, s3c_hsotg_irq, 0, dev_name(dev), hsotg);
3154 if (ret < 0) {
3155 dev_err(dev, "cannot claim IRQ\n");
3156 goto err_regs;
3157 }
3158
3159 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3160
3161 device_initialize(&hsotg->gadget.dev);
3162
3163 dev_set_name(&hsotg->gadget.dev, "gadget");
3164
3165 hsotg->gadget.is_dualspeed = 1;
3166 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3167 hsotg->gadget.name = dev_name(dev);
3168
3169 hsotg->gadget.dev.parent = dev;
3170 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3171
3172 /* setup endpoint information */
3173
3174 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3175 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3176
3177 /* allocate EP0 request */
3178
3179 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3180 GFP_KERNEL);
3181 if (!hsotg->ctrl_req) {
3182 dev_err(dev, "failed to allocate ctrl req\n");
3183 goto err_regs;
3184 }
3185
3186 /* reset the system */
3187
3188 s3c_hsotg_gate(pdev, true);
3189
3190 s3c_hsotg_otgreset(hsotg);
3191 s3c_hsotg_corereset(hsotg);
3192 s3c_hsotg_init(hsotg);
3193
3194 /* initialise the endpoints now the core has been initialised */
3195 for (epnum = 0; epnum < S3C_HSOTG_EPS; epnum++)
3196 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3197
3198 s3c_hsotg_create_debug(hsotg);
3199
3200 s3c_hsotg_dump(hsotg);
3201
3202 our_hsotg = hsotg;
3203 return 0;
3204
3205err_regs:
3206 iounmap(hsotg->regs);
3207
3208err_regs_res:
3209 release_resource(hsotg->regs_res);
3210 kfree(hsotg->regs_res);
3211
3212err_mem:
3213 kfree(hsotg);
3214 return ret;
3215}
3216
3217static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3218{
3219 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3220
3221 s3c_hsotg_delete_debug(hsotg);
3222
3223 usb_gadget_unregister_driver(hsotg->driver);
3224
3225 free_irq(hsotg->irq, hsotg);
3226 iounmap(hsotg->regs);
3227
3228 release_resource(hsotg->regs_res);
3229 kfree(hsotg->regs_res);
3230
3231 s3c_hsotg_gate(pdev, false);
3232
3233 kfree(hsotg);
3234 return 0;
3235}
3236
3237#if 1
3238#define s3c_hsotg_suspend NULL
3239#define s3c_hsotg_resume NULL
3240#endif
3241
3242static struct platform_driver s3c_hsotg_driver = {
3243 .driver = {
3244 .name = "s3c-hsotg",
3245 .owner = THIS_MODULE,
3246 },
3247 .probe = s3c_hsotg_probe,
3248 .remove = __devexit_p(s3c_hsotg_remove),
3249 .suspend = s3c_hsotg_suspend,
3250 .resume = s3c_hsotg_resume,
3251};
3252
3253static int __init s3c_hsotg_modinit(void)
3254{
3255 return platform_driver_register(&s3c_hsotg_driver);
3256}
3257
3258static void __exit s3c_hsotg_modexit(void)
3259{
3260 platform_driver_unregister(&s3c_hsotg_driver);
3261}
3262
3263module_init(s3c_hsotg_modinit);
3264module_exit(s3c_hsotg_modexit);
3265
3266MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3267MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3268MODULE_LICENSE("GPL");
3269MODULE_ALIAS("platform:s3c-hsotg");
diff --git a/drivers/usb/gadget/s3c2410_udc.c b/drivers/usb/gadget/s3c2410_udc.c
index 9a2b8920532d..a9b452fe6221 100644
--- a/drivers/usb/gadget/s3c2410_udc.c
+++ b/drivers/usb/gadget/s3c2410_udc.c
@@ -28,7 +28,6 @@
28#include <linux/ioport.h> 28#include <linux/ioport.h>
29#include <linux/sched.h> 29#include <linux/sched.h>
30#include <linux/slab.h> 30#include <linux/slab.h>
31#include <linux/smp_lock.h>
32#include <linux/errno.h> 31#include <linux/errno.h>
33#include <linux/init.h> 32#include <linux/init.h>
34#include <linux/timer.h> 33#include <linux/timer.h>
diff --git a/drivers/usb/gadget/u_audio.c b/drivers/usb/gadget/u_audio.c
new file mode 100644
index 000000000000..0f3d22fc030e
--- /dev/null
+++ b/drivers/usb/gadget/u_audio.c
@@ -0,0 +1,319 @@
1/*
2 * u_audio.c -- ALSA audio utilities for Gadget stack
3 *
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12#include <linux/kernel.h>
13#include <linux/utsname.h>
14#include <linux/device.h>
15#include <linux/delay.h>
16#include <linux/ctype.h>
17#include <linux/random.h>
18#include <linux/syscalls.h>
19
20#include "u_audio.h"
21
22/*
23 * This component encapsulates the ALSA devices for USB audio gadget
24 */
25
26#define FILE_PCM_PLAYBACK "/dev/snd/pcmC0D0p"
27#define FILE_PCM_CAPTURE "/dev/snd/pcmC0D0c"
28#define FILE_CONTROL "/dev/snd/controlC0"
29
30static char *fn_play = FILE_PCM_PLAYBACK;
31module_param(fn_play, charp, S_IRUGO);
32MODULE_PARM_DESC(fn_play, "Playback PCM device file name");
33
34static char *fn_cap = FILE_PCM_CAPTURE;
35module_param(fn_cap, charp, S_IRUGO);
36MODULE_PARM_DESC(fn_cap, "Capture PCM device file name");
37
38static char *fn_cntl = FILE_CONTROL;
39module_param(fn_cntl, charp, S_IRUGO);
40MODULE_PARM_DESC(fn_cntl, "Control device file name");
41
42/*-------------------------------------------------------------------------*/
43
44/**
45 * Some ALSA internal helper functions
46 */
47static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
48{
49 struct snd_interval t;
50 t.empty = 0;
51 t.min = t.max = val;
52 t.openmin = t.openmax = 0;
53 t.integer = 1;
54 return snd_interval_refine(i, &t);
55}
56
57static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
58 snd_pcm_hw_param_t var, unsigned int val,
59 int dir)
60{
61 int changed;
62 if (hw_is_mask(var)) {
63 struct snd_mask *m = hw_param_mask(params, var);
64 if (val == 0 && dir < 0) {
65 changed = -EINVAL;
66 snd_mask_none(m);
67 } else {
68 if (dir > 0)
69 val++;
70 else if (dir < 0)
71 val--;
72 changed = snd_mask_refine_set(
73 hw_param_mask(params, var), val);
74 }
75 } else if (hw_is_interval(var)) {
76 struct snd_interval *i = hw_param_interval(params, var);
77 if (val == 0 && dir < 0) {
78 changed = -EINVAL;
79 snd_interval_none(i);
80 } else if (dir == 0)
81 changed = snd_interval_refine_set(i, val);
82 else {
83 struct snd_interval t;
84 t.openmin = 1;
85 t.openmax = 1;
86 t.empty = 0;
87 t.integer = 0;
88 if (dir < 0) {
89 t.min = val - 1;
90 t.max = val;
91 } else {
92 t.min = val;
93 t.max = val+1;
94 }
95 changed = snd_interval_refine(i, &t);
96 }
97 } else
98 return -EINVAL;
99 if (changed) {
100 params->cmask |= 1 << var;
101 params->rmask |= 1 << var;
102 }
103 return changed;
104}
105/*-------------------------------------------------------------------------*/
106
107/**
108 * Set default hardware params
109 */
110static int playback_default_hw_params(struct gaudio_snd_dev *snd)
111{
112 struct snd_pcm_substream *substream = snd->substream;
113 struct snd_pcm_hw_params *params;
114 snd_pcm_sframes_t result;
115
116 /*
117 * SNDRV_PCM_ACCESS_RW_INTERLEAVED,
118 * SNDRV_PCM_FORMAT_S16_LE
119 * CHANNELS: 2
120 * RATE: 48000
121 */
122 snd->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
123 snd->format = SNDRV_PCM_FORMAT_S16_LE;
124 snd->channels = 2;
125 snd->rate = 48000;
126
127 params = kzalloc(sizeof(*params), GFP_KERNEL);
128 if (!params)
129 return -ENOMEM;
130
131 _snd_pcm_hw_params_any(params);
132 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
133 snd->access, 0);
134 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
135 snd->format, 0);
136 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
137 snd->channels, 0);
138 _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
139 snd->rate, 0);
140
141 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
142 snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params);
143
144 result = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
145 if (result < 0) {
146 ERROR(snd->card,
147 "Preparing sound card failed: %d\n", (int)result);
148 kfree(params);
149 return result;
150 }
151
152 /* Store the hardware parameters */
153 snd->access = params_access(params);
154 snd->format = params_format(params);
155 snd->channels = params_channels(params);
156 snd->rate = params_rate(params);
157
158 kfree(params);
159
160 INFO(snd->card,
161 "Hardware params: access %x, format %x, channels %d, rate %d\n",
162 snd->access, snd->format, snd->channels, snd->rate);
163
164 return 0;
165}
166
167/**
168 * Playback audio buffer data by ALSA PCM device
169 */
170static size_t u_audio_playback(struct gaudio *card, void *buf, size_t count)
171{
172 struct gaudio_snd_dev *snd = &card->playback;
173 struct snd_pcm_substream *substream = snd->substream;
174 struct snd_pcm_runtime *runtime = substream->runtime;
175 mm_segment_t old_fs;
176 ssize_t result;
177 snd_pcm_sframes_t frames;
178
179try_again:
180 if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
181 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
182 result = snd_pcm_kernel_ioctl(substream,
183 SNDRV_PCM_IOCTL_PREPARE, NULL);
184 if (result < 0) {
185 ERROR(card, "Preparing sound card failed: %d\n",
186 (int)result);
187 return result;
188 }
189 }
190
191 frames = bytes_to_frames(runtime, count);
192 old_fs = get_fs();
193 set_fs(KERNEL_DS);
194 result = snd_pcm_lib_write(snd->substream, buf, frames);
195 if (result != frames) {
196 ERROR(card, "Playback error: %d\n", (int)result);
197 set_fs(old_fs);
198 goto try_again;
199 }
200 set_fs(old_fs);
201
202 return 0;
203}
204
205static int u_audio_get_playback_channels(struct gaudio *card)
206{
207 return card->playback.channels;
208}
209
210static int u_audio_get_playback_rate(struct gaudio *card)
211{
212 return card->playback.rate;
213}
214
215/**
216 * Open ALSA PCM and control device files
217 * Initial the PCM or control device
218 */
219static int gaudio_open_snd_dev(struct gaudio *card)
220{
221 struct snd_pcm_file *pcm_file;
222 struct gaudio_snd_dev *snd;
223
224 if (!card)
225 return -ENODEV;
226
227 /* Open control device */
228 snd = &card->control;
229 snd->filp = filp_open(fn_cntl, O_RDWR, 0);
230 if (IS_ERR(snd->filp)) {
231 int ret = PTR_ERR(snd->filp);
232 ERROR(card, "unable to open sound control device file: %s\n",
233 fn_cntl);
234 snd->filp = NULL;
235 return ret;
236 }
237 snd->card = card;
238
239 /* Open PCM playback device and setup substream */
240 snd = &card->playback;
241 snd->filp = filp_open(fn_play, O_WRONLY, 0);
242 if (IS_ERR(snd->filp)) {
243 ERROR(card, "No such PCM playback device: %s\n", fn_play);
244 snd->filp = NULL;
245 }
246 pcm_file = snd->filp->private_data;
247 snd->substream = pcm_file->substream;
248 snd->card = card;
249 playback_default_hw_params(snd);
250
251 /* Open PCM capture device and setup substream */
252 snd = &card->capture;
253 snd->filp = filp_open(fn_cap, O_RDONLY, 0);
254 if (IS_ERR(snd->filp)) {
255 ERROR(card, "No such PCM capture device: %s\n", fn_cap);
256 snd->filp = NULL;
257 }
258 pcm_file = snd->filp->private_data;
259 snd->substream = pcm_file->substream;
260 snd->card = card;
261
262 return 0;
263}
264
265/**
266 * Close ALSA PCM and control device files
267 */
268static int gaudio_close_snd_dev(struct gaudio *gau)
269{
270 struct gaudio_snd_dev *snd;
271
272 /* Close control device */
273 snd = &gau->control;
274 if (!IS_ERR(snd->filp))
275 filp_close(snd->filp, current->files);
276
277 /* Close PCM playback device and setup substream */
278 snd = &gau->playback;
279 if (!IS_ERR(snd->filp))
280 filp_close(snd->filp, current->files);
281
282 /* Close PCM capture device and setup substream */
283 snd = &gau->capture;
284 if (!IS_ERR(snd->filp))
285 filp_close(snd->filp, current->files);
286
287 return 0;
288}
289
290/**
291 * gaudio_setup - setup ALSA interface and preparing for USB transfer
292 *
293 * This sets up PCM, mixer or MIDI ALSA devices fore USB gadget using.
294 *
295 * Returns negative errno, or zero on success
296 */
297int __init gaudio_setup(struct gaudio *card)
298{
299 int ret;
300
301 ret = gaudio_open_snd_dev(card);
302 if (ret)
303 ERROR(card, "we need at least one control device\n");
304
305 return ret;
306
307}
308
309/**
310 * gaudio_cleanup - remove ALSA device interface
311 *
312 * This is called to free all resources allocated by @gaudio_setup().
313 */
314void gaudio_cleanup(struct gaudio *card)
315{
316 if (card)
317 gaudio_close_snd_dev(card);
318}
319
diff --git a/drivers/usb/gadget/u_audio.h b/drivers/usb/gadget/u_audio.h
new file mode 100644
index 000000000000..cc8d159c648a
--- /dev/null
+++ b/drivers/usb/gadget/u_audio.h
@@ -0,0 +1,56 @@
1/*
2 * u_audio.h -- interface to USB gadget "ALSA AUDIO" utilities
3 *
4 * Copyright (C) 2008 Bryan Wu <cooloney@kernel.org>
5 * Copyright (C) 2008 Analog Devices, Inc
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 *
9 * Licensed under the GPL-2 or later.
10 */
11
12#ifndef __U_AUDIO_H
13#define __U_AUDIO_H
14
15#include <linux/device.h>
16#include <linux/err.h>
17#include <linux/usb/audio.h>
18#include <linux/usb/composite.h>
19
20#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23
24#include "gadget_chips.h"
25
26/*
27 * This represents the USB side of an audio card device, managed by a USB
28 * function which provides control and stream interfaces.
29 */
30
31struct gaudio_snd_dev {
32 struct gaudio *card;
33 struct file *filp;
34 struct snd_pcm_substream *substream;
35 int access;
36 int format;
37 int channels;
38 int rate;
39};
40
41struct gaudio {
42 struct usb_function func;
43 struct usb_gadget *gadget;
44
45 /* ALSA sound device interfaces */
46 struct gaudio_snd_dev control;
47 struct gaudio_snd_dev playback;
48 struct gaudio_snd_dev capture;
49
50 /* TODO */
51};
52
53int gaudio_setup(struct gaudio *card);
54void gaudio_cleanup(struct gaudio *card);
55
56#endif /* __U_AUDIO_H */
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index 4007770f7ed2..016f63b39028 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -520,7 +520,7 @@ static int eth_start_xmit(struct sk_buff *skb, struct net_device *net)
520 */ 520 */
521 if (list_empty(&dev->tx_reqs)) { 521 if (list_empty(&dev->tx_reqs)) {
522 spin_unlock_irqrestore(&dev->req_lock, flags); 522 spin_unlock_irqrestore(&dev->req_lock, flags);
523 return 1; 523 return NETDEV_TX_BUSY;
524 } 524 }
525 525
526 req = container_of(dev->tx_reqs.next, struct usb_request, list); 526 req = container_of(dev->tx_reqs.next, struct usb_request, list);
diff --git a/drivers/usb/gadget/u_serial.c b/drivers/usb/gadget/u_serial.c
index 0a4d99ab40d8..fc6e709f45b1 100644
--- a/drivers/usb/gadget/u_serial.c
+++ b/drivers/usb/gadget/u_serial.c
@@ -371,6 +371,7 @@ __acquires(&port->port_lock)
371 371
372 req->length = len; 372 req->length = len;
373 list_del(&req->list); 373 list_del(&req->list);
374 req->zero = (gs_buf_data_avail(&port->port_write_buf) == 0);
374 375
375 pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n", 376 pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n",
376 port->port_num, len, *((u8 *)req->buf), 377 port->port_num, len, *((u8 *)req->buf),
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index 845479f7c707..1a920c70b5a1 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -17,6 +17,26 @@ config USB_C67X00_HCD
17 To compile this driver as a module, choose M here: the 17 To compile this driver as a module, choose M here: the
18 module will be called c67x00. 18 module will be called c67x00.
19 19
20config USB_XHCI_HCD
21 tristate "xHCI HCD (USB 3.0) support (EXPERIMENTAL)"
22 depends on USB && PCI && EXPERIMENTAL
23 ---help---
24 The eXtensible Host Controller Interface (xHCI) is standard for USB 3.0
25 "SuperSpeed" host controller hardware.
26
27 To compile this driver as a module, choose M here: the
28 module will be called xhci-hcd.
29
30config USB_XHCI_HCD_DEBUGGING
31 bool "Debugging for the xHCI host controller"
32 depends on USB_XHCI_HCD
33 ---help---
34 Say 'Y' to turn on debugging for the xHCI host controller driver.
35 This will spew debugging output, even in interrupt context.
36 This should only be used for debugging xHCI driver bugs.
37
38 If unsure, say N.
39
20config USB_EHCI_HCD 40config USB_EHCI_HCD
21 tristate "EHCI HCD (USB 2.0) support" 41 tristate "EHCI HCD (USB 2.0) support"
22 depends on USB && USB_ARCH_HAS_EHCI 42 depends on USB && USB_ARCH_HAS_EHCI
@@ -161,26 +181,27 @@ config USB_OHCI_HCD_PPC_SOC
161 Enables support for the USB controller on the MPC52xx or 181 Enables support for the USB controller on the MPC52xx or
162 STB03xxx processor chip. If unsure, say Y. 182 STB03xxx processor chip. If unsure, say Y.
163 183
164config USB_OHCI_HCD_PPC_OF
165 bool "OHCI support for PPC USB controller on OF platform bus"
166 depends on USB_OHCI_HCD && PPC_OF
167 default y
168 ---help---
169 Enables support for the USB controller PowerPC present on the
170 OpenFirmware platform bus.
171
172config USB_OHCI_HCD_PPC_OF_BE 184config USB_OHCI_HCD_PPC_OF_BE
173 bool "Support big endian HC" 185 bool "OHCI support for OF platform bus (big endian)"
174 depends on USB_OHCI_HCD_PPC_OF 186 depends on USB_OHCI_HCD && PPC_OF
175 default y
176 select USB_OHCI_BIG_ENDIAN_DESC 187 select USB_OHCI_BIG_ENDIAN_DESC
177 select USB_OHCI_BIG_ENDIAN_MMIO 188 select USB_OHCI_BIG_ENDIAN_MMIO
189 ---help---
190 Enables support for big-endian USB controllers present on the
191 OpenFirmware platform bus.
178 192
179config USB_OHCI_HCD_PPC_OF_LE 193config USB_OHCI_HCD_PPC_OF_LE
180 bool "Support little endian HC" 194 bool "OHCI support for OF platform bus (little endian)"
181 depends on USB_OHCI_HCD_PPC_OF 195 depends on USB_OHCI_HCD && PPC_OF
182 default n
183 select USB_OHCI_LITTLE_ENDIAN 196 select USB_OHCI_LITTLE_ENDIAN
197 ---help---
198 Enables support for little-endian USB controllers present on the
199 OpenFirmware platform bus.
200
201config USB_OHCI_HCD_PPC_OF
202 bool
203 depends on USB_OHCI_HCD && PPC_OF
204 default USB_OHCI_HCD_PPC_OF_BE || USB_OHCI_HCD_PPC_OF_LE
184 205
185config USB_OHCI_HCD_PCI 206config USB_OHCI_HCD_PCI
186 bool "OHCI support for PCI-bus USB controllers" 207 bool "OHCI support for PCI-bus USB controllers"
@@ -317,10 +338,10 @@ config USB_R8A66597_HCD
317 338
318config SUPERH_ON_CHIP_R8A66597 339config SUPERH_ON_CHIP_R8A66597
319 boolean "Enable SuperH on-chip R8A66597 USB" 340 boolean "Enable SuperH on-chip R8A66597 USB"
320 depends on USB_R8A66597_HCD && (CPU_SUBTYPE_SH7366 || CPU_SUBTYPE_SH7723) 341 depends on USB_R8A66597_HCD && (CPU_SUBTYPE_SH7366 || CPU_SUBTYPE_SH7723 || CPU_SUBTYPE_SH7724)
321 help 342 help
322 This driver enables support for the on-chip R8A66597 in the 343 This driver enables support for the on-chip R8A66597 in the
323 SH7366 and SH7723 processors. 344 SH7366, SH7723 and SH7724 processors.
324 345
325config USB_WHCI_HCD 346config USB_WHCI_HCD
326 tristate "Wireless USB Host Controller Interface (WHCI) driver (EXPERIMENTAL)" 347 tristate "Wireless USB Host Controller Interface (WHCI) driver (EXPERIMENTAL)"
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index f163571e33d8..289d748bb414 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -12,6 +12,7 @@ fhci-objs := fhci-hcd.o fhci-hub.o fhci-q.o fhci-mem.o \
12ifeq ($(CONFIG_FHCI_DEBUG),y) 12ifeq ($(CONFIG_FHCI_DEBUG),y)
13fhci-objs += fhci-dbg.o 13fhci-objs += fhci-dbg.o
14endif 14endif
15xhci-objs := xhci-hcd.o xhci-mem.o xhci-pci.o xhci-ring.o xhci-hub.o xhci-dbg.o
15 16
16obj-$(CONFIG_USB_WHCI_HCD) += whci/ 17obj-$(CONFIG_USB_WHCI_HCD) += whci/
17 18
@@ -23,6 +24,7 @@ obj-$(CONFIG_USB_ISP116X_HCD) += isp116x-hcd.o
23obj-$(CONFIG_USB_OHCI_HCD) += ohci-hcd.o 24obj-$(CONFIG_USB_OHCI_HCD) += ohci-hcd.o
24obj-$(CONFIG_USB_UHCI_HCD) += uhci-hcd.o 25obj-$(CONFIG_USB_UHCI_HCD) += uhci-hcd.o
25obj-$(CONFIG_USB_FHCI_HCD) += fhci.o 26obj-$(CONFIG_USB_FHCI_HCD) += fhci.o
27obj-$(CONFIG_USB_XHCI_HCD) += xhci.o
26obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd.o 28obj-$(CONFIG_USB_SL811_HCD) += sl811-hcd.o
27obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o 29obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
28obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o 30obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
index bf69f4739107..59d208d94d4e 100644
--- a/drivers/usb/host/ehci-au1xxx.c
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -97,6 +97,7 @@ static const struct hc_driver ehci_au1xxx_hc_driver = {
97 .urb_enqueue = ehci_urb_enqueue, 97 .urb_enqueue = ehci_urb_enqueue,
98 .urb_dequeue = ehci_urb_dequeue, 98 .urb_dequeue = ehci_urb_dequeue,
99 .endpoint_disable = ehci_endpoint_disable, 99 .endpoint_disable = ehci_endpoint_disable,
100 .endpoint_reset = ehci_endpoint_reset,
100 101
101 /* 102 /*
102 * scheduling support 103 * scheduling support
@@ -112,6 +113,8 @@ static const struct hc_driver ehci_au1xxx_hc_driver = {
112 .bus_resume = ehci_bus_resume, 113 .bus_resume = ehci_bus_resume,
113 .relinquish_port = ehci_relinquish_port, 114 .relinquish_port = ehci_relinquish_port,
114 .port_handed_over = ehci_port_handed_over, 115 .port_handed_over = ehci_port_handed_over,
116
117 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
115}; 118};
116 119
117static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev) 120static int ehci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
index 01c3da34f678..991174937db3 100644
--- a/drivers/usb/host/ehci-fsl.c
+++ b/drivers/usb/host/ehci-fsl.c
@@ -309,6 +309,7 @@ static const struct hc_driver ehci_fsl_hc_driver = {
309 .urb_enqueue = ehci_urb_enqueue, 309 .urb_enqueue = ehci_urb_enqueue,
310 .urb_dequeue = ehci_urb_dequeue, 310 .urb_dequeue = ehci_urb_dequeue,
311 .endpoint_disable = ehci_endpoint_disable, 311 .endpoint_disable = ehci_endpoint_disable,
312 .endpoint_reset = ehci_endpoint_reset,
312 313
313 /* 314 /*
314 * scheduling support 315 * scheduling support
@@ -324,6 +325,8 @@ static const struct hc_driver ehci_fsl_hc_driver = {
324 .bus_resume = ehci_bus_resume, 325 .bus_resume = ehci_bus_resume,
325 .relinquish_port = ehci_relinquish_port, 326 .relinquish_port = ehci_relinquish_port,
326 .port_handed_over = ehci_port_handed_over, 327 .port_handed_over = ehci_port_handed_over,
328
329 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
327}; 330};
328 331
329static int ehci_fsl_drv_probe(struct platform_device *pdev) 332static int ehci_fsl_drv_probe(struct platform_device *pdev)
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index c637207a1c80..7d03549c3339 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -1003,6 +1003,8 @@ idle_timeout:
1003 schedule_timeout_uninterruptible(1); 1003 schedule_timeout_uninterruptible(1);
1004 goto rescan; 1004 goto rescan;
1005 case QH_STATE_IDLE: /* fully unlinked */ 1005 case QH_STATE_IDLE: /* fully unlinked */
1006 if (qh->clearing_tt)
1007 goto idle_timeout;
1006 if (list_empty (&qh->qtd_list)) { 1008 if (list_empty (&qh->qtd_list)) {
1007 qh_put (qh); 1009 qh_put (qh);
1008 break; 1010 break;
@@ -1024,6 +1026,48 @@ done:
1024 return; 1026 return;
1025} 1027}
1026 1028
1029static void
1030ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
1031{
1032 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1033 struct ehci_qh *qh;
1034 int eptype = usb_endpoint_type(&ep->desc);
1035 int epnum = usb_endpoint_num(&ep->desc);
1036 int is_out = usb_endpoint_dir_out(&ep->desc);
1037 unsigned long flags;
1038
1039 if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
1040 return;
1041
1042 spin_lock_irqsave(&ehci->lock, flags);
1043 qh = ep->hcpriv;
1044
1045 /* For Bulk and Interrupt endpoints we maintain the toggle state
1046 * in the hardware; the toggle bits in udev aren't used at all.
1047 * When an endpoint is reset by usb_clear_halt() we must reset
1048 * the toggle bit in the QH.
1049 */
1050 if (qh) {
1051 usb_settoggle(qh->dev, epnum, is_out, 0);
1052 if (!list_empty(&qh->qtd_list)) {
1053 WARN_ONCE(1, "clear_halt for a busy endpoint\n");
1054 } else if (qh->qh_state == QH_STATE_LINKED) {
1055
1056 /* The toggle value in the QH can't be updated
1057 * while the QH is active. Unlink it now;
1058 * re-linking will call qh_refresh().
1059 */
1060 if (eptype == USB_ENDPOINT_XFER_BULK) {
1061 unlink_async(ehci, qh);
1062 } else {
1063 intr_deschedule(ehci, qh);
1064 (void) qh_schedule(ehci, qh);
1065 }
1066 }
1067 }
1068 spin_unlock_irqrestore(&ehci->lock, flags);
1069}
1070
1027static int ehci_get_frame (struct usb_hcd *hcd) 1071static int ehci_get_frame (struct usb_hcd *hcd)
1028{ 1072{
1029 struct ehci_hcd *ehci = hcd_to_ehci (hcd); 1073 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
@@ -1097,7 +1141,7 @@ static int __init ehci_hcd_init(void)
1097 sizeof(struct ehci_itd), sizeof(struct ehci_sitd)); 1141 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
1098 1142
1099#ifdef DEBUG 1143#ifdef DEBUG
1100 ehci_debug_root = debugfs_create_dir("ehci", NULL); 1144 ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
1101 if (!ehci_debug_root) { 1145 if (!ehci_debug_root) {
1102 retval = -ENOENT; 1146 retval = -ENOENT;
1103 goto err_debug; 1147 goto err_debug;
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 97a53a48a3d8..f46ad27c9a90 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -391,7 +391,7 @@ static inline void create_companion_file(struct ehci_hcd *ehci)
391 391
392 /* with integrated TT there is no companion! */ 392 /* with integrated TT there is no companion! */
393 if (!ehci_is_TDI(ehci)) 393 if (!ehci_is_TDI(ehci))
394 i = device_create_file(ehci_to_hcd(ehci)->self.dev, 394 i = device_create_file(ehci_to_hcd(ehci)->self.controller,
395 &dev_attr_companion); 395 &dev_attr_companion);
396} 396}
397 397
@@ -399,7 +399,7 @@ static inline void remove_companion_file(struct ehci_hcd *ehci)
399{ 399{
400 /* with integrated TT there is no companion! */ 400 /* with integrated TT there is no companion! */
401 if (!ehci_is_TDI(ehci)) 401 if (!ehci_is_TDI(ehci))
402 device_remove_file(ehci_to_hcd(ehci)->self.dev, 402 device_remove_file(ehci_to_hcd(ehci)->self.controller,
403 &dev_attr_companion); 403 &dev_attr_companion);
404} 404}
405 405
diff --git a/drivers/usb/host/ehci-ixp4xx.c b/drivers/usb/host/ehci-ixp4xx.c
index 9c32063a0c2f..89b7c70c6ed6 100644
--- a/drivers/usb/host/ehci-ixp4xx.c
+++ b/drivers/usb/host/ehci-ixp4xx.c
@@ -51,6 +51,7 @@ static const struct hc_driver ixp4xx_ehci_hc_driver = {
51 .urb_enqueue = ehci_urb_enqueue, 51 .urb_enqueue = ehci_urb_enqueue,
52 .urb_dequeue = ehci_urb_dequeue, 52 .urb_dequeue = ehci_urb_dequeue,
53 .endpoint_disable = ehci_endpoint_disable, 53 .endpoint_disable = ehci_endpoint_disable,
54 .endpoint_reset = ehci_endpoint_reset,
54 .get_frame_number = ehci_get_frame, 55 .get_frame_number = ehci_get_frame,
55 .hub_status_data = ehci_hub_status_data, 56 .hub_status_data = ehci_hub_status_data,
56 .hub_control = ehci_hub_control, 57 .hub_control = ehci_hub_control,
@@ -60,6 +61,8 @@ static const struct hc_driver ixp4xx_ehci_hc_driver = {
60#endif 61#endif
61 .relinquish_port = ehci_relinquish_port, 62 .relinquish_port = ehci_relinquish_port,
62 .port_handed_over = ehci_port_handed_over, 63 .port_handed_over = ehci_port_handed_over,
64
65 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
63}; 66};
64 67
65static int ixp4xx_ehci_probe(struct platform_device *pdev) 68static int ixp4xx_ehci_probe(struct platform_device *pdev)
diff --git a/drivers/usb/host/ehci-orion.c b/drivers/usb/host/ehci-orion.c
index 9d487908012e..dc2ac613a9d1 100644
--- a/drivers/usb/host/ehci-orion.c
+++ b/drivers/usb/host/ehci-orion.c
@@ -149,6 +149,7 @@ static const struct hc_driver ehci_orion_hc_driver = {
149 .urb_enqueue = ehci_urb_enqueue, 149 .urb_enqueue = ehci_urb_enqueue,
150 .urb_dequeue = ehci_urb_dequeue, 150 .urb_dequeue = ehci_urb_dequeue,
151 .endpoint_disable = ehci_endpoint_disable, 151 .endpoint_disable = ehci_endpoint_disable,
152 .endpoint_reset = ehci_endpoint_reset,
152 153
153 /* 154 /*
154 * scheduling support 155 * scheduling support
@@ -164,6 +165,8 @@ static const struct hc_driver ehci_orion_hc_driver = {
164 .bus_resume = ehci_bus_resume, 165 .bus_resume = ehci_bus_resume,
165 .relinquish_port = ehci_relinquish_port, 166 .relinquish_port = ehci_relinquish_port,
166 .port_handed_over = ehci_port_handed_over, 167 .port_handed_over = ehci_port_handed_over,
168
169 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
167}; 170};
168 171
169static void __init 172static void __init
@@ -187,7 +190,7 @@ ehci_orion_conf_mbus_windows(struct usb_hcd *hcd,
187 } 190 }
188} 191}
189 192
190static int __init ehci_orion_drv_probe(struct platform_device *pdev) 193static int __devinit ehci_orion_drv_probe(struct platform_device *pdev)
191{ 194{
192 struct orion_ehci_data *pd = pdev->dev.platform_data; 195 struct orion_ehci_data *pd = pdev->dev.platform_data;
193 struct resource *res; 196 struct resource *res;
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index 5aa8bce90e1f..c2f1b7df918c 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -268,7 +268,7 @@ done:
268 * Also they depend on separate root hub suspend/resume. 268 * Also they depend on separate root hub suspend/resume.
269 */ 269 */
270 270
271static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message) 271static int ehci_pci_suspend(struct usb_hcd *hcd)
272{ 272{
273 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 273 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
274 unsigned long flags; 274 unsigned long flags;
@@ -293,12 +293,6 @@ static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
293 ehci_writel(ehci, 0, &ehci->regs->intr_enable); 293 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
294 (void)ehci_readl(ehci, &ehci->regs->intr_enable); 294 (void)ehci_readl(ehci, &ehci->regs->intr_enable);
295 295
296 /* make sure snapshot being resumed re-enumerates everything */
297 if (message.event == PM_EVENT_PRETHAW) {
298 ehci_halt(ehci);
299 ehci_reset(ehci);
300 }
301
302 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 296 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
303 bail: 297 bail:
304 spin_unlock_irqrestore (&ehci->lock, flags); 298 spin_unlock_irqrestore (&ehci->lock, flags);
@@ -309,7 +303,7 @@ static int ehci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
309 return rc; 303 return rc;
310} 304}
311 305
312static int ehci_pci_resume(struct usb_hcd *hcd) 306static int ehci_pci_resume(struct usb_hcd *hcd, bool hibernated)
313{ 307{
314 struct ehci_hcd *ehci = hcd_to_ehci(hcd); 308 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
315 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 309 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
@@ -322,10 +316,12 @@ static int ehci_pci_resume(struct usb_hcd *hcd)
322 /* Mark hardware accessible again as we are out of D3 state by now */ 316 /* Mark hardware accessible again as we are out of D3 state by now */
323 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 317 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
324 318
325 /* If CF is still set, we maintained PCI Vaux power. 319 /* If CF is still set and we aren't resuming from hibernation
320 * then we maintained PCI Vaux power.
326 * Just undo the effect of ehci_pci_suspend(). 321 * Just undo the effect of ehci_pci_suspend().
327 */ 322 */
328 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF) { 323 if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
324 !hibernated) {
329 int mask = INTR_MASK; 325 int mask = INTR_MASK;
330 326
331 if (!hcd->self.root_hub->do_remote_wakeup) 327 if (!hcd->self.root_hub->do_remote_wakeup)
@@ -335,7 +331,6 @@ static int ehci_pci_resume(struct usb_hcd *hcd)
335 return 0; 331 return 0;
336 } 332 }
337 333
338 ehci_dbg(ehci, "lost power, restarting\n");
339 usb_root_hub_lost_power(hcd->self.root_hub); 334 usb_root_hub_lost_power(hcd->self.root_hub);
340 335
341 /* Else reset, to cope with power loss or flush-to-storage 336 /* Else reset, to cope with power loss or flush-to-storage
@@ -393,6 +388,7 @@ static const struct hc_driver ehci_pci_hc_driver = {
393 .urb_enqueue = ehci_urb_enqueue, 388 .urb_enqueue = ehci_urb_enqueue,
394 .urb_dequeue = ehci_urb_dequeue, 389 .urb_dequeue = ehci_urb_dequeue,
395 .endpoint_disable = ehci_endpoint_disable, 390 .endpoint_disable = ehci_endpoint_disable,
391 .endpoint_reset = ehci_endpoint_reset,
396 392
397 /* 393 /*
398 * scheduling support 394 * scheduling support
@@ -408,6 +404,8 @@ static const struct hc_driver ehci_pci_hc_driver = {
408 .bus_resume = ehci_bus_resume, 404 .bus_resume = ehci_bus_resume,
409 .relinquish_port = ehci_relinquish_port, 405 .relinquish_port = ehci_relinquish_port,
410 .port_handed_over = ehci_port_handed_over, 406 .port_handed_over = ehci_port_handed_over,
407
408 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
411}; 409};
412 410
413/*-------------------------------------------------------------------------*/ 411/*-------------------------------------------------------------------------*/
@@ -429,10 +427,11 @@ static struct pci_driver ehci_pci_driver = {
429 427
430 .probe = usb_hcd_pci_probe, 428 .probe = usb_hcd_pci_probe,
431 .remove = usb_hcd_pci_remove, 429 .remove = usb_hcd_pci_remove,
430 .shutdown = usb_hcd_pci_shutdown,
432 431
433#ifdef CONFIG_PM 432#ifdef CONFIG_PM_SLEEP
434 .suspend = usb_hcd_pci_suspend, 433 .driver = {
435 .resume = usb_hcd_pci_resume, 434 .pm = &usb_hcd_pci_pm_ops
435 },
436#endif 436#endif
437 .shutdown = usb_hcd_pci_shutdown,
438}; 437};
diff --git a/drivers/usb/host/ehci-ppc-of.c b/drivers/usb/host/ehci-ppc-of.c
index ef732b704f53..36f96da129f5 100644
--- a/drivers/usb/host/ehci-ppc-of.c
+++ b/drivers/usb/host/ehci-ppc-of.c
@@ -61,6 +61,7 @@ static const struct hc_driver ehci_ppc_of_hc_driver = {
61 .urb_enqueue = ehci_urb_enqueue, 61 .urb_enqueue = ehci_urb_enqueue,
62 .urb_dequeue = ehci_urb_dequeue, 62 .urb_dequeue = ehci_urb_dequeue,
63 .endpoint_disable = ehci_endpoint_disable, 63 .endpoint_disable = ehci_endpoint_disable,
64 .endpoint_reset = ehci_endpoint_reset,
64 65
65 /* 66 /*
66 * scheduling support 67 * scheduling support
@@ -78,6 +79,8 @@ static const struct hc_driver ehci_ppc_of_hc_driver = {
78#endif 79#endif
79 .relinquish_port = ehci_relinquish_port, 80 .relinquish_port = ehci_relinquish_port,
80 .port_handed_over = ehci_port_handed_over, 81 .port_handed_over = ehci_port_handed_over,
82
83 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
81}; 84};
82 85
83 86
diff --git a/drivers/usb/host/ehci-ps3.c b/drivers/usb/host/ehci-ps3.c
index 1ba9f9a8c308..1dee33b9139e 100644
--- a/drivers/usb/host/ehci-ps3.c
+++ b/drivers/usb/host/ehci-ps3.c
@@ -65,6 +65,7 @@ static const struct hc_driver ps3_ehci_hc_driver = {
65 .urb_enqueue = ehci_urb_enqueue, 65 .urb_enqueue = ehci_urb_enqueue,
66 .urb_dequeue = ehci_urb_dequeue, 66 .urb_dequeue = ehci_urb_dequeue,
67 .endpoint_disable = ehci_endpoint_disable, 67 .endpoint_disable = ehci_endpoint_disable,
68 .endpoint_reset = ehci_endpoint_reset,
68 .get_frame_number = ehci_get_frame, 69 .get_frame_number = ehci_get_frame,
69 .hub_status_data = ehci_hub_status_data, 70 .hub_status_data = ehci_hub_status_data,
70 .hub_control = ehci_hub_control, 71 .hub_control = ehci_hub_control,
@@ -74,9 +75,11 @@ static const struct hc_driver ps3_ehci_hc_driver = {
74#endif 75#endif
75 .relinquish_port = ehci_relinquish_port, 76 .relinquish_port = ehci_relinquish_port,
76 .port_handed_over = ehci_port_handed_over, 77 .port_handed_over = ehci_port_handed_over,
78
79 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
77}; 80};
78 81
79static int ps3_ehci_probe(struct ps3_system_bus_device *dev) 82static int __devinit ps3_ehci_probe(struct ps3_system_bus_device *dev)
80{ 83{
81 int result; 84 int result;
82 struct usb_hcd *hcd; 85 struct usb_hcd *hcd;
@@ -162,7 +165,7 @@ static int ps3_ehci_probe(struct ps3_system_bus_device *dev)
162 dev_dbg(&dev->core, "%s:%d: virq %lu\n", __func__, __LINE__, 165 dev_dbg(&dev->core, "%s:%d: virq %lu\n", __func__, __LINE__,
163 (unsigned long)virq); 166 (unsigned long)virq);
164 167
165 ps3_system_bus_set_driver_data(dev, hcd); 168 ps3_system_bus_set_drvdata(dev, hcd);
166 169
167 result = usb_add_hcd(hcd, virq, IRQF_DISABLED); 170 result = usb_add_hcd(hcd, virq, IRQF_DISABLED);
168 171
@@ -195,8 +198,7 @@ fail_start:
195static int ps3_ehci_remove(struct ps3_system_bus_device *dev) 198static int ps3_ehci_remove(struct ps3_system_bus_device *dev)
196{ 199{
197 unsigned int tmp; 200 unsigned int tmp;
198 struct usb_hcd *hcd = 201 struct usb_hcd *hcd = ps3_system_bus_get_drvdata(dev);
199 (struct usb_hcd *)ps3_system_bus_get_driver_data(dev);
200 202
201 BUG_ON(!hcd); 203 BUG_ON(!hcd);
202 204
@@ -208,7 +210,7 @@ static int ps3_ehci_remove(struct ps3_system_bus_device *dev)
208 ehci_shutdown(hcd); 210 ehci_shutdown(hcd);
209 usb_remove_hcd(hcd); 211 usb_remove_hcd(hcd);
210 212
211 ps3_system_bus_set_driver_data(dev, NULL); 213 ps3_system_bus_set_drvdata(dev, NULL);
212 214
213 BUG_ON(!hcd->regs); 215 BUG_ON(!hcd->regs);
214 iounmap(hcd->regs); 216 iounmap(hcd->regs);
@@ -225,7 +227,7 @@ static int ps3_ehci_remove(struct ps3_system_bus_device *dev)
225 return 0; 227 return 0;
226} 228}
227 229
228static int ps3_ehci_driver_register(struct ps3_system_bus_driver *drv) 230static int __init ps3_ehci_driver_register(struct ps3_system_bus_driver *drv)
229{ 231{
230 return firmware_has_feature(FW_FEATURE_PS3_LV1) 232 return firmware_has_feature(FW_FEATURE_PS3_LV1)
231 ? ps3_system_bus_driver_register(drv) 233 ? ps3_system_bus_driver_register(drv)
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 1976b1b3778c..9a1384747f3b 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -139,6 +139,55 @@ qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
139 139
140/*-------------------------------------------------------------------------*/ 140/*-------------------------------------------------------------------------*/
141 141
142static void qh_link_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
143
144static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
145 struct usb_host_endpoint *ep)
146{
147 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
148 struct ehci_qh *qh = ep->hcpriv;
149 unsigned long flags;
150
151 spin_lock_irqsave(&ehci->lock, flags);
152 qh->clearing_tt = 0;
153 if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
154 && HC_IS_RUNNING(hcd->state))
155 qh_link_async(ehci, qh);
156 spin_unlock_irqrestore(&ehci->lock, flags);
157}
158
159static void ehci_clear_tt_buffer(struct ehci_hcd *ehci, struct ehci_qh *qh,
160 struct urb *urb, u32 token)
161{
162
163 /* If an async split transaction gets an error or is unlinked,
164 * the TT buffer may be left in an indeterminate state. We
165 * have to clear the TT buffer.
166 *
167 * Note: this routine is never called for Isochronous transfers.
168 */
169 if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
170#ifdef DEBUG
171 struct usb_device *tt = urb->dev->tt->hub;
172 dev_dbg(&tt->dev,
173 "clear tt buffer port %d, a%d ep%d t%08x\n",
174 urb->dev->ttport, urb->dev->devnum,
175 usb_pipeendpoint(urb->pipe), token);
176#endif /* DEBUG */
177 if (!ehci_is_TDI(ehci)
178 || urb->dev->tt->hub !=
179 ehci_to_hcd(ehci)->self.root_hub) {
180 if (usb_hub_clear_tt_buffer(urb) == 0)
181 qh->clearing_tt = 1;
182 } else {
183
184 /* REVISIT ARC-derived cores don't clear the root
185 * hub TT buffer in this way...
186 */
187 }
188 }
189}
190
142static int qtd_copy_status ( 191static int qtd_copy_status (
143 struct ehci_hcd *ehci, 192 struct ehci_hcd *ehci,
144 struct urb *urb, 193 struct urb *urb,
@@ -165,6 +214,14 @@ static int qtd_copy_status (
165 if (token & QTD_STS_BABBLE) { 214 if (token & QTD_STS_BABBLE) {
166 /* FIXME "must" disable babbling device's port too */ 215 /* FIXME "must" disable babbling device's port too */
167 status = -EOVERFLOW; 216 status = -EOVERFLOW;
217 /* CERR nonzero + halt --> stall */
218 } else if (QTD_CERR(token)) {
219 status = -EPIPE;
220
221 /* In theory, more than one of the following bits can be set
222 * since they are sticky and the transaction is retried.
223 * Which to test first is rather arbitrary.
224 */
168 } else if (token & QTD_STS_MMF) { 225 } else if (token & QTD_STS_MMF) {
169 /* fs/ls interrupt xfer missed the complete-split */ 226 /* fs/ls interrupt xfer missed the complete-split */
170 status = -EPROTO; 227 status = -EPROTO;
@@ -173,21 +230,15 @@ static int qtd_copy_status (
173 ? -ENOSR /* hc couldn't read data */ 230 ? -ENOSR /* hc couldn't read data */
174 : -ECOMM; /* hc couldn't write data */ 231 : -ECOMM; /* hc couldn't write data */
175 } else if (token & QTD_STS_XACT) { 232 } else if (token & QTD_STS_XACT) {
176 /* timeout, bad crc, wrong PID, etc; retried */ 233 /* timeout, bad CRC, wrong PID, etc */
177 if (QTD_CERR (token)) 234 ehci_dbg(ehci, "devpath %s ep%d%s 3strikes\n",
178 status = -EPIPE; 235 urb->dev->devpath,
179 else { 236 usb_pipeendpoint(urb->pipe),
180 ehci_dbg (ehci, "devpath %s ep%d%s 3strikes\n", 237 usb_pipein(urb->pipe) ? "in" : "out");
181 urb->dev->devpath, 238 status = -EPROTO;
182 usb_pipeendpoint (urb->pipe), 239 } else { /* unknown */
183 usb_pipein (urb->pipe) ? "in" : "out");
184 status = -EPROTO;
185 }
186 /* CERR nonzero + no errors + halt --> stall */
187 } else if (QTD_CERR (token))
188 status = -EPIPE;
189 else /* unknown */
190 status = -EPROTO; 240 status = -EPROTO;
241 }
191 242
192 ehci_vdbg (ehci, 243 ehci_vdbg (ehci,
193 "dev%d ep%d%s qtd token %08x --> status %d\n", 244 "dev%d ep%d%s qtd token %08x --> status %d\n",
@@ -195,28 +246,6 @@ static int qtd_copy_status (
195 usb_pipeendpoint (urb->pipe), 246 usb_pipeendpoint (urb->pipe),
196 usb_pipein (urb->pipe) ? "in" : "out", 247 usb_pipein (urb->pipe) ? "in" : "out",
197 token, status); 248 token, status);
198
199 /* if async CSPLIT failed, try cleaning out the TT buffer */
200 if (status != -EPIPE
201 && urb->dev->tt
202 && !usb_pipeint(urb->pipe)
203 && ((token & QTD_STS_MMF) != 0
204 || QTD_CERR(token) == 0)
205 && (!ehci_is_TDI(ehci)
206 || urb->dev->tt->hub !=
207 ehci_to_hcd(ehci)->self.root_hub)) {
208#ifdef DEBUG
209 struct usb_device *tt = urb->dev->tt->hub;
210 dev_dbg (&tt->dev,
211 "clear tt buffer port %d, a%d ep%d t%08x\n",
212 urb->dev->ttport, urb->dev->devnum,
213 usb_pipeendpoint (urb->pipe), token);
214#endif /* DEBUG */
215 /* REVISIT ARC-derived cores don't clear the root
216 * hub TT buffer in this way...
217 */
218 usb_hub_tt_clear_buffer (urb->dev, urb->pipe);
219 }
220 } 249 }
221 250
222 return status; 251 return status;
@@ -407,9 +436,16 @@ qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
407 /* qh unlinked; token in overlay may be most current */ 436 /* qh unlinked; token in overlay may be most current */
408 if (state == QH_STATE_IDLE 437 if (state == QH_STATE_IDLE
409 && cpu_to_hc32(ehci, qtd->qtd_dma) 438 && cpu_to_hc32(ehci, qtd->qtd_dma)
410 == qh->hw_current) 439 == qh->hw_current) {
411 token = hc32_to_cpu(ehci, qh->hw_token); 440 token = hc32_to_cpu(ehci, qh->hw_token);
412 441
442 /* An unlink may leave an incomplete
443 * async transaction in the TT buffer.
444 * We have to clear it.
445 */
446 ehci_clear_tt_buffer(ehci, qh, urb, token);
447 }
448
413 /* force halt for unlinked or blocked qh, so we'll 449 /* force halt for unlinked or blocked qh, so we'll
414 * patch the qh later and so that completions can't 450 * patch the qh later and so that completions can't
415 * activate it while we "know" it's stopped. 451 * activate it while we "know" it's stopped.
@@ -435,6 +471,13 @@ halt:
435 && (qtd->hw_alt_next 471 && (qtd->hw_alt_next
436 & EHCI_LIST_END(ehci))) 472 & EHCI_LIST_END(ehci)))
437 last_status = -EINPROGRESS; 473 last_status = -EINPROGRESS;
474
475 /* As part of low/full-speed endpoint-halt processing
476 * we must clear the TT buffer (11.17.5).
477 */
478 if (unlikely(last_status != -EINPROGRESS &&
479 last_status != -EREMOTEIO))
480 ehci_clear_tt_buffer(ehci, qh, urb, token);
438 } 481 }
439 482
440 /* if we're removing something not at the queue head, 483 /* if we're removing something not at the queue head,
@@ -864,6 +907,10 @@ static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
864 __hc32 dma = QH_NEXT(ehci, qh->qh_dma); 907 __hc32 dma = QH_NEXT(ehci, qh->qh_dma);
865 struct ehci_qh *head; 908 struct ehci_qh *head;
866 909
910 /* Don't link a QH if there's a Clear-TT-Buffer pending */
911 if (unlikely(qh->clearing_tt))
912 return;
913
867 /* (re)start the async schedule? */ 914 /* (re)start the async schedule? */
868 head = ehci->async; 915 head = ehci->async;
869 timer_action_done (ehci, TIMER_ASYNC_OFF); 916 timer_action_done (ehci, TIMER_ASYNC_OFF);
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index 556d0ec0c1f8..74f7f83b29ad 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -760,8 +760,10 @@ static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
760 if (status) { 760 if (status) {
761 /* "normal" case, uframing flexible except with splits */ 761 /* "normal" case, uframing flexible except with splits */
762 if (qh->period) { 762 if (qh->period) {
763 frame = qh->period - 1; 763 int i;
764 do { 764
765 for (i = qh->period; status && i > 0; --i) {
766 frame = ++ehci->random_frame % qh->period;
765 for (uframe = 0; uframe < 8; uframe++) { 767 for (uframe = 0; uframe < 8; uframe++) {
766 status = check_intr_schedule (ehci, 768 status = check_intr_schedule (ehci,
767 frame, uframe, qh, 769 frame, uframe, qh,
@@ -769,7 +771,7 @@ static int qh_schedule(struct ehci_hcd *ehci, struct ehci_qh *qh)
769 if (status == 0) 771 if (status == 0)
770 break; 772 break;
771 } 773 }
772 } while (status && frame--); 774 }
773 775
774 /* qh->period == 0 means every uframe */ 776 /* qh->period == 0 means every uframe */
775 } else { 777 } else {
@@ -1617,11 +1619,14 @@ itd_complete (
1617 desc->status = -EPROTO; 1619 desc->status = -EPROTO;
1618 1620
1619 /* HC need not update length with this error */ 1621 /* HC need not update length with this error */
1620 if (!(t & EHCI_ISOC_BABBLE)) 1622 if (!(t & EHCI_ISOC_BABBLE)) {
1621 desc->actual_length = EHCI_ITD_LENGTH (t); 1623 desc->actual_length = EHCI_ITD_LENGTH(t);
1624 urb->actual_length += desc->actual_length;
1625 }
1622 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) { 1626 } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) {
1623 desc->status = 0; 1627 desc->status = 0;
1624 desc->actual_length = EHCI_ITD_LENGTH (t); 1628 desc->actual_length = EHCI_ITD_LENGTH(t);
1629 urb->actual_length += desc->actual_length;
1625 } else { 1630 } else {
1626 /* URB was too late */ 1631 /* URB was too late */
1627 desc->status = -EXDEV; 1632 desc->status = -EXDEV;
@@ -2012,7 +2017,8 @@ sitd_complete (
2012 desc->status = -EPROTO; 2017 desc->status = -EPROTO;
2013 } else { 2018 } else {
2014 desc->status = 0; 2019 desc->status = 0;
2015 desc->actual_length = desc->length - SITD_LENGTH (t); 2020 desc->actual_length = desc->length - SITD_LENGTH(t);
2021 urb->actual_length += desc->actual_length;
2016 } 2022 }
2017 stream->depth -= stream->interval << 3; 2023 stream->depth -= stream->interval << 3;
2018 2024
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 6cff195e1a36..2bfff30f4704 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -116,6 +116,7 @@ struct ehci_hcd { /* one per controller */
116 struct timer_list watchdog; 116 struct timer_list watchdog;
117 unsigned long actions; 117 unsigned long actions;
118 unsigned stamp; 118 unsigned stamp;
119 unsigned random_frame;
119 unsigned long next_statechange; 120 unsigned long next_statechange;
120 u32 command; 121 u32 command;
121 122
@@ -353,7 +354,9 @@ struct ehci_qh {
353 unsigned short period; /* polling interval */ 354 unsigned short period; /* polling interval */
354 unsigned short start; /* where polling starts */ 355 unsigned short start; /* where polling starts */
355#define NO_FRAME ((unsigned short)~0) /* pick new start */ 356#define NO_FRAME ((unsigned short)~0) /* pick new start */
357
356 struct usb_device *dev; /* access to TT */ 358 struct usb_device *dev; /* access to TT */
359 unsigned clearing_tt:1; /* Clear-TT-Buf in progress */
357} __attribute__ ((aligned (32))); 360} __attribute__ ((aligned (32)));
358 361
359/*-------------------------------------------------------------------------*/ 362/*-------------------------------------------------------------------------*/
diff --git a/drivers/usb/host/fhci-dbg.c b/drivers/usb/host/fhci-dbg.c
index ea8a4255c5da..e799f86dab11 100644
--- a/drivers/usb/host/fhci-dbg.c
+++ b/drivers/usb/host/fhci-dbg.c
@@ -108,7 +108,7 @@ void fhci_dfs_create(struct fhci_hcd *fhci)
108{ 108{
109 struct device *dev = fhci_to_hcd(fhci)->self.controller; 109 struct device *dev = fhci_to_hcd(fhci)->self.controller;
110 110
111 fhci->dfs_root = debugfs_create_dir(dev_name(dev), NULL); 111 fhci->dfs_root = debugfs_create_dir(dev_name(dev), usb_debug_root);
112 if (!fhci->dfs_root) { 112 if (!fhci->dfs_root) {
113 WARN_ON(1); 113 WARN_ON(1);
114 return; 114 return;
diff --git a/drivers/usb/host/fhci-sched.c b/drivers/usb/host/fhci-sched.c
index bb63b68ddb77..62a226b61670 100644
--- a/drivers/usb/host/fhci-sched.c
+++ b/drivers/usb/host/fhci-sched.c
@@ -576,9 +576,7 @@ irqreturn_t fhci_irq(struct usb_hcd *hcd)
576 out_be16(&usb->fhci->regs->usb_event, 576 out_be16(&usb->fhci->regs->usb_event,
577 usb->saved_msk); 577 usb->saved_msk);
578 } else if (usb->port_status == FHCI_PORT_DISABLED) { 578 } else if (usb->port_status == FHCI_PORT_DISABLED) {
579 if (fhci_ioports_check_bus_state(fhci) == 1 && 579 if (fhci_ioports_check_bus_state(fhci) == 1)
580 usb->port_status != FHCI_PORT_LOW &&
581 usb->port_status != FHCI_PORT_FULL)
582 fhci_device_connected_interrupt(fhci); 580 fhci_device_connected_interrupt(fhci);
583 } 581 }
584 usb_er &= ~USB_E_RESET_MASK; 582 usb_er &= ~USB_E_RESET_MASK;
@@ -605,9 +603,7 @@ irqreturn_t fhci_irq(struct usb_hcd *hcd)
605 } 603 }
606 604
607 if (usb_er & USB_E_IDLE_MASK) { 605 if (usb_er & USB_E_IDLE_MASK) {
608 if (usb->port_status == FHCI_PORT_DISABLED && 606 if (usb->port_status == FHCI_PORT_DISABLED) {
609 usb->port_status != FHCI_PORT_LOW &&
610 usb->port_status != FHCI_PORT_FULL) {
611 usb_er &= ~USB_E_RESET_MASK; 607 usb_er &= ~USB_E_RESET_MASK;
612 fhci_device_connected_interrupt(fhci); 608 fhci_device_connected_interrupt(fhci);
613 } else if (usb->port_status == 609 } else if (usb->port_status ==
diff --git a/drivers/usb/host/hwa-hc.c b/drivers/usb/host/hwa-hc.c
index cbf30e515f29..88b03214622b 100644
--- a/drivers/usb/host/hwa-hc.c
+++ b/drivers/usb/host/hwa-hc.c
@@ -172,25 +172,6 @@ error_cluster_id_get:
172 172
173} 173}
174 174
175static int hwahc_op_suspend(struct usb_hcd *usb_hcd, pm_message_t msg)
176{
177 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
178 struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
179 dev_err(wusbhc->dev, "%s (%p [%p], 0x%lx) UNIMPLEMENTED\n", __func__,
180 usb_hcd, hwahc, *(unsigned long *) &msg);
181 return -ENOSYS;
182}
183
184static int hwahc_op_resume(struct usb_hcd *usb_hcd)
185{
186 struct wusbhc *wusbhc = usb_hcd_to_wusbhc(usb_hcd);
187 struct hwahc *hwahc = container_of(wusbhc, struct hwahc, wusbhc);
188
189 dev_err(wusbhc->dev, "%s (%p [%p]) UNIMPLEMENTED\n", __func__,
190 usb_hcd, hwahc);
191 return -ENOSYS;
192}
193
194/* 175/*
195 * No need to abort pipes, as when this is called, all the children 176 * No need to abort pipes, as when this is called, all the children
196 * has been disconnected and that has done it [through 177 * has been disconnected and that has done it [through
@@ -598,8 +579,6 @@ static struct hc_driver hwahc_hc_driver = {
598 .flags = HCD_USB2, /* FIXME */ 579 .flags = HCD_USB2, /* FIXME */
599 .reset = hwahc_op_reset, 580 .reset = hwahc_op_reset,
600 .start = hwahc_op_start, 581 .start = hwahc_op_start,
601 .pci_suspend = hwahc_op_suspend,
602 .pci_resume = hwahc_op_resume,
603 .stop = hwahc_op_stop, 582 .stop = hwahc_op_stop,
604 .get_frame_number = hwahc_op_get_frame_number, 583 .get_frame_number = hwahc_op_get_frame_number,
605 .urb_enqueue = hwahc_op_urb_enqueue, 584 .urb_enqueue = hwahc_op_urb_enqueue,
diff --git a/drivers/usb/host/isp1760-if.c b/drivers/usb/host/isp1760-if.c
index 3fa3a1702796..d4feebfc63bd 100644
--- a/drivers/usb/host/isp1760-if.c
+++ b/drivers/usb/host/isp1760-if.c
@@ -361,7 +361,7 @@ static int __devexit isp1760_plat_remove(struct platform_device *pdev)
361 361
362static struct platform_driver isp1760_plat_driver = { 362static struct platform_driver isp1760_plat_driver = {
363 .probe = isp1760_plat_probe, 363 .probe = isp1760_plat_probe,
364 .remove = isp1760_plat_remove, 364 .remove = __devexit_p(isp1760_plat_remove),
365 .driver = { 365 .driver = {
366 .name = "isp1760", 366 .name = "isp1760",
367 }, 367 },
diff --git a/drivers/usb/host/ohci-dbg.c b/drivers/usb/host/ohci-dbg.c
index d3269656aa4d..811f5dfdc582 100644
--- a/drivers/usb/host/ohci-dbg.c
+++ b/drivers/usb/host/ohci-dbg.c
@@ -431,7 +431,7 @@ static struct dentry *ohci_debug_root;
431 431
432struct debug_buffer { 432struct debug_buffer {
433 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */ 433 ssize_t (*fill_func)(struct debug_buffer *); /* fill method */
434 struct device *dev; 434 struct ohci_hcd *ohci;
435 struct mutex mutex; /* protect filling of buffer */ 435 struct mutex mutex; /* protect filling of buffer */
436 size_t count; /* number of characters filled into buffer */ 436 size_t count; /* number of characters filled into buffer */
437 char *page; 437 char *page;
@@ -505,15 +505,11 @@ show_list (struct ohci_hcd *ohci, char *buf, size_t count, struct ed *ed)
505 505
506static ssize_t fill_async_buffer(struct debug_buffer *buf) 506static ssize_t fill_async_buffer(struct debug_buffer *buf)
507{ 507{
508 struct usb_bus *bus;
509 struct usb_hcd *hcd;
510 struct ohci_hcd *ohci; 508 struct ohci_hcd *ohci;
511 size_t temp; 509 size_t temp;
512 unsigned long flags; 510 unsigned long flags;
513 511
514 bus = dev_get_drvdata(buf->dev); 512 ohci = buf->ohci;
515 hcd = bus_to_hcd(bus);
516 ohci = hcd_to_ohci(hcd);
517 513
518 /* display control and bulk lists together, for simplicity */ 514 /* display control and bulk lists together, for simplicity */
519 spin_lock_irqsave (&ohci->lock, flags); 515 spin_lock_irqsave (&ohci->lock, flags);
@@ -529,8 +525,6 @@ static ssize_t fill_async_buffer(struct debug_buffer *buf)
529 525
530static ssize_t fill_periodic_buffer(struct debug_buffer *buf) 526static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
531{ 527{
532 struct usb_bus *bus;
533 struct usb_hcd *hcd;
534 struct ohci_hcd *ohci; 528 struct ohci_hcd *ohci;
535 struct ed **seen, *ed; 529 struct ed **seen, *ed;
536 unsigned long flags; 530 unsigned long flags;
@@ -542,9 +536,7 @@ static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
542 return 0; 536 return 0;
543 seen_count = 0; 537 seen_count = 0;
544 538
545 bus = (struct usb_bus *)dev_get_drvdata(buf->dev); 539 ohci = buf->ohci;
546 hcd = bus_to_hcd(bus);
547 ohci = hcd_to_ohci(hcd);
548 next = buf->page; 540 next = buf->page;
549 size = PAGE_SIZE; 541 size = PAGE_SIZE;
550 542
@@ -626,7 +618,6 @@ static ssize_t fill_periodic_buffer(struct debug_buffer *buf)
626 618
627static ssize_t fill_registers_buffer(struct debug_buffer *buf) 619static ssize_t fill_registers_buffer(struct debug_buffer *buf)
628{ 620{
629 struct usb_bus *bus;
630 struct usb_hcd *hcd; 621 struct usb_hcd *hcd;
631 struct ohci_hcd *ohci; 622 struct ohci_hcd *ohci;
632 struct ohci_regs __iomem *regs; 623 struct ohci_regs __iomem *regs;
@@ -635,9 +626,8 @@ static ssize_t fill_registers_buffer(struct debug_buffer *buf)
635 char *next; 626 char *next;
636 u32 rdata; 627 u32 rdata;
637 628
638 bus = (struct usb_bus *)dev_get_drvdata(buf->dev); 629 ohci = buf->ohci;
639 hcd = bus_to_hcd(bus); 630 hcd = ohci_to_hcd(ohci);
640 ohci = hcd_to_ohci(hcd);
641 regs = ohci->regs; 631 regs = ohci->regs;
642 next = buf->page; 632 next = buf->page;
643 size = PAGE_SIZE; 633 size = PAGE_SIZE;
@@ -710,7 +700,7 @@ done:
710 return PAGE_SIZE - size; 700 return PAGE_SIZE - size;
711} 701}
712 702
713static struct debug_buffer *alloc_buffer(struct device *dev, 703static struct debug_buffer *alloc_buffer(struct ohci_hcd *ohci,
714 ssize_t (*fill_func)(struct debug_buffer *)) 704 ssize_t (*fill_func)(struct debug_buffer *))
715{ 705{
716 struct debug_buffer *buf; 706 struct debug_buffer *buf;
@@ -718,7 +708,7 @@ static struct debug_buffer *alloc_buffer(struct device *dev,
718 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL); 708 buf = kzalloc(sizeof(struct debug_buffer), GFP_KERNEL);
719 709
720 if (buf) { 710 if (buf) {
721 buf->dev = dev; 711 buf->ohci = ohci;
722 buf->fill_func = fill_func; 712 buf->fill_func = fill_func;
723 mutex_init(&buf->mutex); 713 mutex_init(&buf->mutex);
724 } 714 }
@@ -810,26 +800,25 @@ static int debug_registers_open(struct inode *inode, struct file *file)
810static inline void create_debug_files (struct ohci_hcd *ohci) 800static inline void create_debug_files (struct ohci_hcd *ohci)
811{ 801{
812 struct usb_bus *bus = &ohci_to_hcd(ohci)->self; 802 struct usb_bus *bus = &ohci_to_hcd(ohci)->self;
813 struct device *dev = bus->dev;
814 803
815 ohci->debug_dir = debugfs_create_dir(bus->bus_name, ohci_debug_root); 804 ohci->debug_dir = debugfs_create_dir(bus->bus_name, ohci_debug_root);
816 if (!ohci->debug_dir) 805 if (!ohci->debug_dir)
817 goto dir_error; 806 goto dir_error;
818 807
819 ohci->debug_async = debugfs_create_file("async", S_IRUGO, 808 ohci->debug_async = debugfs_create_file("async", S_IRUGO,
820 ohci->debug_dir, dev, 809 ohci->debug_dir, ohci,
821 &debug_async_fops); 810 &debug_async_fops);
822 if (!ohci->debug_async) 811 if (!ohci->debug_async)
823 goto async_error; 812 goto async_error;
824 813
825 ohci->debug_periodic = debugfs_create_file("periodic", S_IRUGO, 814 ohci->debug_periodic = debugfs_create_file("periodic", S_IRUGO,
826 ohci->debug_dir, dev, 815 ohci->debug_dir, ohci,
827 &debug_periodic_fops); 816 &debug_periodic_fops);
828 if (!ohci->debug_periodic) 817 if (!ohci->debug_periodic)
829 goto periodic_error; 818 goto periodic_error;
830 819
831 ohci->debug_registers = debugfs_create_file("registers", S_IRUGO, 820 ohci->debug_registers = debugfs_create_file("registers", S_IRUGO,
832 ohci->debug_dir, dev, 821 ohci->debug_dir, ohci,
833 &debug_registers_fops); 822 &debug_registers_fops);
834 if (!ohci->debug_registers) 823 if (!ohci->debug_registers)
835 goto registers_error; 824 goto registers_error;
diff --git a/drivers/usb/host/ohci-ep93xx.c b/drivers/usb/host/ohci-ep93xx.c
index 7cf74f8c2db1..b0dbf4157d29 100644
--- a/drivers/usb/host/ohci-ep93xx.c
+++ b/drivers/usb/host/ohci-ep93xx.c
@@ -47,7 +47,7 @@ static int usb_hcd_ep93xx_probe(const struct hc_driver *driver,
47 struct usb_hcd *hcd; 47 struct usb_hcd *hcd;
48 48
49 if (pdev->resource[1].flags != IORESOURCE_IRQ) { 49 if (pdev->resource[1].flags != IORESOURCE_IRQ) {
50 pr_debug("resource[1] is not IORESOURCE_IRQ"); 50 dbg("resource[1] is not IORESOURCE_IRQ");
51 return -ENOMEM; 51 return -ENOMEM;
52 } 52 }
53 53
@@ -65,12 +65,18 @@ static int usb_hcd_ep93xx_probe(const struct hc_driver *driver,
65 65
66 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); 66 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
67 if (hcd->regs == NULL) { 67 if (hcd->regs == NULL) {
68 pr_debug("ioremap failed"); 68 dbg("ioremap failed");
69 retval = -ENOMEM; 69 retval = -ENOMEM;
70 goto err2; 70 goto err2;
71 } 71 }
72 72
73 usb_host_clock = clk_get(&pdev->dev, "usb_host"); 73 usb_host_clock = clk_get(&pdev->dev, NULL);
74 if (IS_ERR(usb_host_clock)) {
75 dbg("clk_get failed");
76 retval = PTR_ERR(usb_host_clock);
77 goto err3;
78 }
79
74 ep93xx_start_hc(&pdev->dev); 80 ep93xx_start_hc(&pdev->dev);
75 81
76 ohci_hcd_init(hcd_to_ohci(hcd)); 82 ohci_hcd_init(hcd_to_ohci(hcd));
@@ -80,6 +86,7 @@ static int usb_hcd_ep93xx_probe(const struct hc_driver *driver,
80 return retval; 86 return retval;
81 87
82 ep93xx_stop_hc(&pdev->dev); 88 ep93xx_stop_hc(&pdev->dev);
89err3:
83 iounmap(hcd->regs); 90 iounmap(hcd->regs);
84err2: 91err2:
85 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 92 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index 25db704f3a2a..58151687d351 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -571,7 +571,7 @@ static int ohci_init (struct ohci_hcd *ohci)
571 */ 571 */
572static int ohci_run (struct ohci_hcd *ohci) 572static int ohci_run (struct ohci_hcd *ohci)
573{ 573{
574 u32 mask, temp; 574 u32 mask, val;
575 int first = ohci->fminterval == 0; 575 int first = ohci->fminterval == 0;
576 struct usb_hcd *hcd = ohci_to_hcd(ohci); 576 struct usb_hcd *hcd = ohci_to_hcd(ohci);
577 577
@@ -580,8 +580,8 @@ static int ohci_run (struct ohci_hcd *ohci)
580 /* boot firmware should have set this up (5.1.1.3.1) */ 580 /* boot firmware should have set this up (5.1.1.3.1) */
581 if (first) { 581 if (first) {
582 582
583 temp = ohci_readl (ohci, &ohci->regs->fminterval); 583 val = ohci_readl (ohci, &ohci->regs->fminterval);
584 ohci->fminterval = temp & 0x3fff; 584 ohci->fminterval = val & 0x3fff;
585 if (ohci->fminterval != FI) 585 if (ohci->fminterval != FI)
586 ohci_dbg (ohci, "fminterval delta %d\n", 586 ohci_dbg (ohci, "fminterval delta %d\n",
587 ohci->fminterval - FI); 587 ohci->fminterval - FI);
@@ -600,25 +600,25 @@ static int ohci_run (struct ohci_hcd *ohci)
600 600
601 switch (ohci->hc_control & OHCI_CTRL_HCFS) { 601 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
602 case OHCI_USB_OPER: 602 case OHCI_USB_OPER:
603 temp = 0; 603 val = 0;
604 break; 604 break;
605 case OHCI_USB_SUSPEND: 605 case OHCI_USB_SUSPEND:
606 case OHCI_USB_RESUME: 606 case OHCI_USB_RESUME:
607 ohci->hc_control &= OHCI_CTRL_RWC; 607 ohci->hc_control &= OHCI_CTRL_RWC;
608 ohci->hc_control |= OHCI_USB_RESUME; 608 ohci->hc_control |= OHCI_USB_RESUME;
609 temp = 10 /* msec wait */; 609 val = 10 /* msec wait */;
610 break; 610 break;
611 // case OHCI_USB_RESET: 611 // case OHCI_USB_RESET:
612 default: 612 default:
613 ohci->hc_control &= OHCI_CTRL_RWC; 613 ohci->hc_control &= OHCI_CTRL_RWC;
614 ohci->hc_control |= OHCI_USB_RESET; 614 ohci->hc_control |= OHCI_USB_RESET;
615 temp = 50 /* msec wait */; 615 val = 50 /* msec wait */;
616 break; 616 break;
617 } 617 }
618 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 618 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
619 // flush the writes 619 // flush the writes
620 (void) ohci_readl (ohci, &ohci->regs->control); 620 (void) ohci_readl (ohci, &ohci->regs->control);
621 msleep(temp); 621 msleep(val);
622 622
623 memset (ohci->hcca, 0, sizeof (struct ohci_hcca)); 623 memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
624 624
@@ -628,9 +628,9 @@ static int ohci_run (struct ohci_hcd *ohci)
628retry: 628retry:
629 /* HC Reset requires max 10 us delay */ 629 /* HC Reset requires max 10 us delay */
630 ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus); 630 ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
631 temp = 30; /* ... allow extra time */ 631 val = 30; /* ... allow extra time */
632 while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) { 632 while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
633 if (--temp == 0) { 633 if (--val == 0) {
634 spin_unlock_irq (&ohci->lock); 634 spin_unlock_irq (&ohci->lock);
635 ohci_err (ohci, "USB HC reset timed out!\n"); 635 ohci_err (ohci, "USB HC reset timed out!\n");
636 return -1; 636 return -1;
@@ -699,23 +699,23 @@ retry:
699 ohci_writel (ohci, mask, &ohci->regs->intrenable); 699 ohci_writel (ohci, mask, &ohci->regs->intrenable);
700 700
701 /* handle root hub init quirks ... */ 701 /* handle root hub init quirks ... */
702 temp = roothub_a (ohci); 702 val = roothub_a (ohci);
703 temp &= ~(RH_A_PSM | RH_A_OCPM); 703 val &= ~(RH_A_PSM | RH_A_OCPM);
704 if (ohci->flags & OHCI_QUIRK_SUPERIO) { 704 if (ohci->flags & OHCI_QUIRK_SUPERIO) {
705 /* NSC 87560 and maybe others */ 705 /* NSC 87560 and maybe others */
706 temp |= RH_A_NOCP; 706 val |= RH_A_NOCP;
707 temp &= ~(RH_A_POTPGT | RH_A_NPS); 707 val &= ~(RH_A_POTPGT | RH_A_NPS);
708 ohci_writel (ohci, temp, &ohci->regs->roothub.a); 708 ohci_writel (ohci, val, &ohci->regs->roothub.a);
709 } else if ((ohci->flags & OHCI_QUIRK_AMD756) || 709 } else if ((ohci->flags & OHCI_QUIRK_AMD756) ||
710 (ohci->flags & OHCI_QUIRK_HUB_POWER)) { 710 (ohci->flags & OHCI_QUIRK_HUB_POWER)) {
711 /* hub power always on; required for AMD-756 and some 711 /* hub power always on; required for AMD-756 and some
712 * Mac platforms. ganged overcurrent reporting, if any. 712 * Mac platforms. ganged overcurrent reporting, if any.
713 */ 713 */
714 temp |= RH_A_NPS; 714 val |= RH_A_NPS;
715 ohci_writel (ohci, temp, &ohci->regs->roothub.a); 715 ohci_writel (ohci, val, &ohci->regs->roothub.a);
716 } 716 }
717 ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status); 717 ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
718 ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM, 718 ohci_writel (ohci, (val & RH_A_NPS) ? 0 : RH_B_PPCM,
719 &ohci->regs->roothub.b); 719 &ohci->regs->roothub.b);
720 // flush those writes 720 // flush those writes
721 (void) ohci_readl (ohci, &ohci->regs->control); 721 (void) ohci_readl (ohci, &ohci->regs->control);
@@ -724,7 +724,7 @@ retry:
724 spin_unlock_irq (&ohci->lock); 724 spin_unlock_irq (&ohci->lock);
725 725
726 // POTPGT delay is bits 24-31, in 2 ms units. 726 // POTPGT delay is bits 24-31, in 2 ms units.
727 mdelay ((temp >> 23) & 0x1fe); 727 mdelay ((val >> 23) & 0x1fe);
728 hcd->state = HC_STATE_RUNNING; 728 hcd->state = HC_STATE_RUNNING;
729 729
730 if (quirk_zfmicro(ohci)) { 730 if (quirk_zfmicro(ohci)) {
@@ -1105,7 +1105,7 @@ static int __init ohci_hcd_mod_init(void)
1105 set_bit(USB_OHCI_LOADED, &usb_hcds_loaded); 1105 set_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1106 1106
1107#ifdef DEBUG 1107#ifdef DEBUG
1108 ohci_debug_root = debugfs_create_dir("ohci", NULL); 1108 ohci_debug_root = debugfs_create_dir("ohci", usb_debug_root);
1109 if (!ohci_debug_root) { 1109 if (!ohci_debug_root) {
1110 retval = -ENOENT; 1110 retval = -ENOENT;
1111 goto error_debug; 1111 goto error_debug;
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index f9961b4c0da3..d2ba04dd785e 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -372,7 +372,7 @@ static int __devinit ohci_pci_start (struct usb_hcd *hcd)
372 372
373#ifdef CONFIG_PM 373#ifdef CONFIG_PM
374 374
375static int ohci_pci_suspend (struct usb_hcd *hcd, pm_message_t message) 375static int ohci_pci_suspend(struct usb_hcd *hcd)
376{ 376{
377 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 377 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
378 unsigned long flags; 378 unsigned long flags;
@@ -394,10 +394,6 @@ static int ohci_pci_suspend (struct usb_hcd *hcd, pm_message_t message)
394 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable); 394 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
395 (void)ohci_readl(ohci, &ohci->regs->intrdisable); 395 (void)ohci_readl(ohci, &ohci->regs->intrdisable);
396 396
397 /* make sure snapshot being resumed re-enumerates everything */
398 if (message.event == PM_EVENT_PRETHAW)
399 ohci_usb_reset(ohci);
400
401 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 397 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
402 bail: 398 bail:
403 spin_unlock_irqrestore (&ohci->lock, flags); 399 spin_unlock_irqrestore (&ohci->lock, flags);
@@ -406,9 +402,14 @@ static int ohci_pci_suspend (struct usb_hcd *hcd, pm_message_t message)
406} 402}
407 403
408 404
409static int ohci_pci_resume (struct usb_hcd *hcd) 405static int ohci_pci_resume(struct usb_hcd *hcd, bool hibernated)
410{ 406{
411 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 407 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
408
409 /* Make sure resume from hibernation re-enumerates everything */
410 if (hibernated)
411 ohci_usb_reset(hcd_to_ohci(hcd));
412
412 ohci_finish_controller_resume(hcd); 413 ohci_finish_controller_resume(hcd);
413 return 0; 414 return 0;
414} 415}
@@ -484,12 +485,11 @@ static struct pci_driver ohci_pci_driver = {
484 485
485 .probe = usb_hcd_pci_probe, 486 .probe = usb_hcd_pci_probe,
486 .remove = usb_hcd_pci_remove, 487 .remove = usb_hcd_pci_remove,
488 .shutdown = usb_hcd_pci_shutdown,
487 489
488#ifdef CONFIG_PM 490#ifdef CONFIG_PM_SLEEP
489 .suspend = usb_hcd_pci_suspend, 491 .driver = {
490 .resume = usb_hcd_pci_resume, 492 .pm = &usb_hcd_pci_pm_ops
493 },
491#endif 494#endif
492
493 .shutdown = usb_hcd_pci_shutdown,
494}; 495};
495
diff --git a/drivers/usb/host/ohci-ps3.c b/drivers/usb/host/ohci-ps3.c
index 3d1910317328..700950455f4d 100644
--- a/drivers/usb/host/ohci-ps3.c
+++ b/drivers/usb/host/ohci-ps3.c
@@ -75,7 +75,7 @@ static const struct hc_driver ps3_ohci_hc_driver = {
75#endif 75#endif
76}; 76};
77 77
78static int ps3_ohci_probe(struct ps3_system_bus_device *dev) 78static int __devinit ps3_ohci_probe(struct ps3_system_bus_device *dev)
79{ 79{
80 int result; 80 int result;
81 struct usb_hcd *hcd; 81 struct usb_hcd *hcd;
@@ -162,7 +162,7 @@ static int ps3_ohci_probe(struct ps3_system_bus_device *dev)
162 dev_dbg(&dev->core, "%s:%d: virq %lu\n", __func__, __LINE__, 162 dev_dbg(&dev->core, "%s:%d: virq %lu\n", __func__, __LINE__,
163 (unsigned long)virq); 163 (unsigned long)virq);
164 164
165 ps3_system_bus_set_driver_data(dev, hcd); 165 ps3_system_bus_set_drvdata(dev, hcd);
166 166
167 result = usb_add_hcd(hcd, virq, IRQF_DISABLED); 167 result = usb_add_hcd(hcd, virq, IRQF_DISABLED);
168 168
@@ -195,8 +195,7 @@ fail_start:
195static int ps3_ohci_remove(struct ps3_system_bus_device *dev) 195static int ps3_ohci_remove(struct ps3_system_bus_device *dev)
196{ 196{
197 unsigned int tmp; 197 unsigned int tmp;
198 struct usb_hcd *hcd = 198 struct usb_hcd *hcd = ps3_system_bus_get_drvdata(dev);
199 (struct usb_hcd *)ps3_system_bus_get_driver_data(dev);
200 199
201 BUG_ON(!hcd); 200 BUG_ON(!hcd);
202 201
@@ -208,7 +207,7 @@ static int ps3_ohci_remove(struct ps3_system_bus_device *dev)
208 ohci_shutdown(hcd); 207 ohci_shutdown(hcd);
209 usb_remove_hcd(hcd); 208 usb_remove_hcd(hcd);
210 209
211 ps3_system_bus_set_driver_data(dev, NULL); 210 ps3_system_bus_set_drvdata(dev, NULL);
212 211
213 BUG_ON(!hcd->regs); 212 BUG_ON(!hcd->regs);
214 iounmap(hcd->regs); 213 iounmap(hcd->regs);
@@ -225,7 +224,7 @@ static int ps3_ohci_remove(struct ps3_system_bus_device *dev)
225 return 0; 224 return 0;
226} 225}
227 226
228static int ps3_ohci_driver_register(struct ps3_system_bus_driver *drv) 227static int __init ps3_ohci_driver_register(struct ps3_system_bus_driver *drv)
229{ 228{
230 return firmware_has_feature(FW_FEATURE_PS3_LV1) 229 return firmware_has_feature(FW_FEATURE_PS3_LV1)
231 ? ps3_system_bus_driver_register(drv) 230 ? ps3_system_bus_driver_register(drv)
diff --git a/drivers/usb/host/pci-quirks.c b/drivers/usb/host/pci-quirks.c
index 033c2846ce59..83b5f9cea85a 100644
--- a/drivers/usb/host/pci-quirks.c
+++ b/drivers/usb/host/pci-quirks.c
@@ -15,6 +15,7 @@
15#include <linux/delay.h> 15#include <linux/delay.h>
16#include <linux/acpi.h> 16#include <linux/acpi.h>
17#include "pci-quirks.h" 17#include "pci-quirks.h"
18#include "xhci-ext-caps.h"
18 19
19 20
20#define UHCI_USBLEGSUP 0xc0 /* legacy support */ 21#define UHCI_USBLEGSUP 0xc0 /* legacy support */
@@ -341,7 +342,127 @@ static void __devinit quirk_usb_disable_ehci(struct pci_dev *pdev)
341 return; 342 return;
342} 343}
343 344
345/*
346 * handshake - spin reading a register until handshake completes
347 * @ptr: address of hc register to be read
348 * @mask: bits to look at in result of read
349 * @done: value of those bits when handshake succeeds
350 * @wait_usec: timeout in microseconds
351 * @delay_usec: delay in microseconds to wait between polling
352 *
353 * Polls a register every delay_usec microseconds.
354 * Returns 0 when the mask bits have the value done.
355 * Returns -ETIMEDOUT if this condition is not true after
356 * wait_usec microseconds have passed.
357 */
358static int handshake(void __iomem *ptr, u32 mask, u32 done,
359 int wait_usec, int delay_usec)
360{
361 u32 result;
362
363 do {
364 result = readl(ptr);
365 result &= mask;
366 if (result == done)
367 return 0;
368 udelay(delay_usec);
369 wait_usec -= delay_usec;
370 } while (wait_usec > 0);
371 return -ETIMEDOUT;
372}
373
374/**
375 * PCI Quirks for xHCI.
376 *
377 * Takes care of the handoff between the Pre-OS (i.e. BIOS) and the OS.
378 * It signals to the BIOS that the OS wants control of the host controller,
379 * and then waits 5 seconds for the BIOS to hand over control.
380 * If we timeout, assume the BIOS is broken and take control anyway.
381 */
382static void __devinit quirk_usb_handoff_xhci(struct pci_dev *pdev)
383{
384 void __iomem *base;
385 int ext_cap_offset;
386 void __iomem *op_reg_base;
387 u32 val;
388 int timeout;
389
390 if (!mmio_resource_enabled(pdev, 0))
391 return;
392
393 base = ioremap_nocache(pci_resource_start(pdev, 0),
394 pci_resource_len(pdev, 0));
395 if (base == NULL)
396 return;
344 397
398 /*
399 * Find the Legacy Support Capability register -
400 * this is optional for xHCI host controllers.
401 */
402 ext_cap_offset = xhci_find_next_cap_offset(base, XHCI_HCC_PARAMS_OFFSET);
403 do {
404 if (!ext_cap_offset)
405 /* We've reached the end of the extended capabilities */
406 goto hc_init;
407 val = readl(base + ext_cap_offset);
408 if (XHCI_EXT_CAPS_ID(val) == XHCI_EXT_CAPS_LEGACY)
409 break;
410 ext_cap_offset = xhci_find_next_cap_offset(base, ext_cap_offset);
411 } while (1);
412
413 /* If the BIOS owns the HC, signal that the OS wants it, and wait */
414 if (val & XHCI_HC_BIOS_OWNED) {
415 writel(val & XHCI_HC_OS_OWNED, base + ext_cap_offset);
416
417 /* Wait for 5 seconds with 10 microsecond polling interval */
418 timeout = handshake(base + ext_cap_offset, XHCI_HC_BIOS_OWNED,
419 0, 5000, 10);
420
421 /* Assume a buggy BIOS and take HC ownership anyway */
422 if (timeout) {
423 dev_warn(&pdev->dev, "xHCI BIOS handoff failed"
424 " (BIOS bug ?) %08x\n", val);
425 writel(val & ~XHCI_HC_BIOS_OWNED, base + ext_cap_offset);
426 }
427 }
428
429 /* Disable any BIOS SMIs */
430 writel(XHCI_LEGACY_DISABLE_SMI,
431 base + ext_cap_offset + XHCI_LEGACY_CONTROL_OFFSET);
432
433hc_init:
434 op_reg_base = base + XHCI_HC_LENGTH(readl(base));
435
436 /* Wait for the host controller to be ready before writing any
437 * operational or runtime registers. Wait 5 seconds and no more.
438 */
439 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_CNR, 0,
440 5000, 10);
441 /* Assume a buggy HC and start HC initialization anyway */
442 if (timeout) {
443 val = readl(op_reg_base + XHCI_STS_OFFSET);
444 dev_warn(&pdev->dev,
445 "xHCI HW not ready after 5 sec (HC bug?) "
446 "status = 0x%x\n", val);
447 }
448
449 /* Send the halt and disable interrupts command */
450 val = readl(op_reg_base + XHCI_CMD_OFFSET);
451 val &= ~(XHCI_CMD_RUN | XHCI_IRQS);
452 writel(val, op_reg_base + XHCI_CMD_OFFSET);
453
454 /* Wait for the HC to halt - poll every 125 usec (one microframe). */
455 timeout = handshake(op_reg_base + XHCI_STS_OFFSET, XHCI_STS_HALT, 1,
456 XHCI_MAX_HALT_USEC, 125);
457 if (timeout) {
458 val = readl(op_reg_base + XHCI_STS_OFFSET);
459 dev_warn(&pdev->dev,
460 "xHCI HW did not halt within %d usec "
461 "status = 0x%x\n", XHCI_MAX_HALT_USEC, val);
462 }
463
464 iounmap(base);
465}
345 466
346static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev) 467static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
347{ 468{
@@ -351,5 +472,7 @@ static void __devinit quirk_usb_early_handoff(struct pci_dev *pdev)
351 quirk_usb_handoff_ohci(pdev); 472 quirk_usb_handoff_ohci(pdev);
352 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI) 473 else if (pdev->class == PCI_CLASS_SERIAL_USB_EHCI)
353 quirk_usb_disable_ehci(pdev); 474 quirk_usb_disable_ehci(pdev);
475 else if (pdev->class == PCI_CLASS_SERIAL_USB_XHCI)
476 quirk_usb_handoff_xhci(pdev);
354} 477}
355DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff); 478DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_usb_early_handoff);
diff --git a/drivers/usb/host/r8a66597-hcd.c b/drivers/usb/host/r8a66597-hcd.c
index f1626e58c141..e18f74946e68 100644
--- a/drivers/usb/host/r8a66597-hcd.c
+++ b/drivers/usb/host/r8a66597-hcd.c
@@ -26,7 +26,6 @@
26#include <linux/module.h> 26#include <linux/module.h>
27#include <linux/kernel.h> 27#include <linux/kernel.h>
28#include <linux/sched.h> 28#include <linux/sched.h>
29#include <linux/smp_lock.h>
30#include <linux/errno.h> 29#include <linux/errno.h>
31#include <linux/init.h> 30#include <linux/init.h>
32#include <linux/timer.h> 31#include <linux/timer.h>
@@ -46,31 +45,10 @@ MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Yoshihiro Shimoda"); 45MODULE_AUTHOR("Yoshihiro Shimoda");
47MODULE_ALIAS("platform:r8a66597_hcd"); 46MODULE_ALIAS("platform:r8a66597_hcd");
48 47
49#define DRIVER_VERSION "10 Apr 2008" 48#define DRIVER_VERSION "2009-05-26"
50 49
51static const char hcd_name[] = "r8a66597_hcd"; 50static const char hcd_name[] = "r8a66597_hcd";
52 51
53/* module parameters */
54#if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597)
55static unsigned short clock = XTAL12;
56module_param(clock, ushort, 0644);
57MODULE_PARM_DESC(clock, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
58 "(default=0)");
59#endif
60
61static unsigned short vif = LDRV;
62module_param(vif, ushort, 0644);
63MODULE_PARM_DESC(vif, "input VIF: 3.3V=32768, 1.5V=0(default=32768)");
64
65static unsigned short endian;
66module_param(endian, ushort, 0644);
67MODULE_PARM_DESC(endian, "data endian: big=256, little=0 (default=0)");
68
69static unsigned short irq_sense = 0xff;
70module_param(irq_sense, ushort, 0644);
71MODULE_PARM_DESC(irq_sense, "IRQ sense: low level=32, falling edge=0 "
72 "(default=32)");
73
74static void packet_write(struct r8a66597 *r8a66597, u16 pipenum); 52static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
75static int r8a66597_get_frame(struct usb_hcd *hcd); 53static int r8a66597_get_frame(struct usb_hcd *hcd);
76 54
@@ -136,7 +114,8 @@ static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
136 } 114 }
137 } while ((tmp & USBE) != USBE); 115 } while ((tmp & USBE) != USBE);
138 r8a66597_bclr(r8a66597, USBE, SYSCFG0); 116 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
139 r8a66597_mdfy(r8a66597, clock, XTAL, SYSCFG0); 117 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata), XTAL,
118 SYSCFG0);
140 119
141 i = 0; 120 i = 0;
142 r8a66597_bset(r8a66597, XCKE, SYSCFG0); 121 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
@@ -203,6 +182,9 @@ static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
203static int enable_controller(struct r8a66597 *r8a66597) 182static int enable_controller(struct r8a66597 *r8a66597)
204{ 183{
205 int ret, port; 184 int ret, port;
185 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
186 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
187 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
206 188
207 ret = r8a66597_clock_enable(r8a66597); 189 ret = r8a66597_clock_enable(r8a66597);
208 if (ret < 0) 190 if (ret < 0)
@@ -2373,7 +2355,7 @@ static int __init_or_module r8a66597_remove(struct platform_device *pdev)
2373 return 0; 2355 return 0;
2374} 2356}
2375 2357
2376static int __init r8a66597_probe(struct platform_device *pdev) 2358static int __devinit r8a66597_probe(struct platform_device *pdev)
2377{ 2359{
2378#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK) 2360#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2379 char clk_name[8]; 2361 char clk_name[8];
@@ -2418,6 +2400,12 @@ static int __init r8a66597_probe(struct platform_device *pdev)
2418 goto clean_up; 2400 goto clean_up;
2419 } 2401 }
2420 2402
2403 if (pdev->dev.platform_data == NULL) {
2404 dev_err(&pdev->dev, "no platform data\n");
2405 ret = -ENODEV;
2406 goto clean_up;
2407 }
2408
2421 /* initialize hcd */ 2409 /* initialize hcd */
2422 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name); 2410 hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
2423 if (!hcd) { 2411 if (!hcd) {
@@ -2428,6 +2416,8 @@ static int __init r8a66597_probe(struct platform_device *pdev)
2428 r8a66597 = hcd_to_r8a66597(hcd); 2416 r8a66597 = hcd_to_r8a66597(hcd);
2429 memset(r8a66597, 0, sizeof(struct r8a66597)); 2417 memset(r8a66597, 0, sizeof(struct r8a66597));
2430 dev_set_drvdata(&pdev->dev, r8a66597); 2418 dev_set_drvdata(&pdev->dev, r8a66597);
2419 r8a66597->pdata = pdev->dev.platform_data;
2420 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
2431 2421
2432#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK) 2422#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
2433 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id); 2423 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
@@ -2458,29 +2448,6 @@ static int __init r8a66597_probe(struct platform_device *pdev)
2458 2448
2459 hcd->rsrc_start = res->start; 2449 hcd->rsrc_start = res->start;
2460 2450
2461 /* irq_sense setting on cmdline takes precedence over resource
2462 * settings, so the introduction of irqflags in IRQ resourse
2463 * won't disturb existing setups */
2464 switch (irq_sense) {
2465 case INTL:
2466 irq_trigger = IRQF_TRIGGER_LOW;
2467 break;
2468 case 0:
2469 irq_trigger = IRQF_TRIGGER_FALLING;
2470 break;
2471 case 0xff:
2472 if (irq_trigger)
2473 irq_sense = (irq_trigger & IRQF_TRIGGER_LOW) ?
2474 INTL : 0;
2475 else {
2476 irq_sense = INTL;
2477 irq_trigger = IRQF_TRIGGER_LOW;
2478 }
2479 break;
2480 default:
2481 dev_err(&pdev->dev, "Unknown irq_sense value.\n");
2482 }
2483
2484 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger); 2451 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | irq_trigger);
2485 if (ret != 0) { 2452 if (ret != 0) {
2486 dev_err(&pdev->dev, "Failed to add hcd\n"); 2453 dev_err(&pdev->dev, "Failed to add hcd\n");
diff --git a/drivers/usb/host/r8a66597.h b/drivers/usb/host/r8a66597.h
index f49208f1bb74..d72680b433f9 100644
--- a/drivers/usb/host/r8a66597.h
+++ b/drivers/usb/host/r8a66597.h
@@ -30,6 +30,8 @@
30#include <linux/clk.h> 30#include <linux/clk.h>
31#endif 31#endif
32 32
33#include <linux/usb/r8a66597.h>
34
33#define SYSCFG0 0x00 35#define SYSCFG0 0x00
34#define SYSCFG1 0x02 36#define SYSCFG1 0x02
35#define SYSSTS0 0x04 37#define SYSSTS0 0x04
@@ -488,6 +490,7 @@ struct r8a66597 {
488#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK) 490#if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) && defined(CONFIG_HAVE_CLK)
489 struct clk *clk; 491 struct clk *clk;
490#endif 492#endif
493 struct r8a66597_platdata *pdata;
491 struct r8a66597_device device0; 494 struct r8a66597_device device0;
492 struct r8a66597_root_hub root_hub[R8A66597_MAX_ROOT_HUB]; 495 struct r8a66597_root_hub root_hub[R8A66597_MAX_ROOT_HUB];
493 struct list_head pipe_queue[R8A66597_MAX_NUM_PIPE]; 496 struct list_head pipe_queue[R8A66597_MAX_NUM_PIPE];
@@ -506,6 +509,7 @@ struct r8a66597 {
506 unsigned long child_connect_map[4]; 509 unsigned long child_connect_map[4];
507 510
508 unsigned bus_suspended:1; 511 unsigned bus_suspended:1;
512 unsigned irq_sense_low:1;
509}; 513};
510 514
511static inline struct r8a66597 *hcd_to_r8a66597(struct usb_hcd *hcd) 515static inline struct r8a66597 *hcd_to_r8a66597(struct usb_hcd *hcd)
@@ -660,10 +664,36 @@ static inline void r8a66597_port_power(struct r8a66597 *r8a66597, int port,
660{ 664{
661 unsigned long dvstctr_reg = get_dvstctr_reg(port); 665 unsigned long dvstctr_reg = get_dvstctr_reg(port);
662 666
663 if (power) 667 if (r8a66597->pdata->port_power) {
664 r8a66597_bset(r8a66597, VBOUT, dvstctr_reg); 668 r8a66597->pdata->port_power(port, power);
665 else 669 } else {
666 r8a66597_bclr(r8a66597, VBOUT, dvstctr_reg); 670 if (power)
671 r8a66597_bset(r8a66597, VBOUT, dvstctr_reg);
672 else
673 r8a66597_bclr(r8a66597, VBOUT, dvstctr_reg);
674 }
675}
676
677static inline u16 get_xtal_from_pdata(struct r8a66597_platdata *pdata)
678{
679 u16 clock = 0;
680
681 switch (pdata->xtal) {
682 case R8A66597_PLATDATA_XTAL_12MHZ:
683 clock = XTAL12;
684 break;
685 case R8A66597_PLATDATA_XTAL_24MHZ:
686 clock = XTAL24;
687 break;
688 case R8A66597_PLATDATA_XTAL_48MHZ:
689 clock = XTAL48;
690 break;
691 default:
692 printk(KERN_ERR "r8a66597: platdata clock is wrong.\n");
693 break;
694 }
695
696 return clock;
667} 697}
668 698
669#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2) 699#define get_pipectr_addr(pipenum) (PIPE1CTR + (pipenum - 1) * 2)
diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
index cf5e4cf7ea42..274751b4409c 100644
--- a/drivers/usb/host/uhci-hcd.c
+++ b/drivers/usb/host/uhci-hcd.c
@@ -769,7 +769,7 @@ static int uhci_rh_resume(struct usb_hcd *hcd)
769 return rc; 769 return rc;
770} 770}
771 771
772static int uhci_pci_suspend(struct usb_hcd *hcd, pm_message_t message) 772static int uhci_pci_suspend(struct usb_hcd *hcd)
773{ 773{
774 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 774 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
775 int rc = 0; 775 int rc = 0;
@@ -795,10 +795,6 @@ static int uhci_pci_suspend(struct usb_hcd *hcd, pm_message_t message)
795 795
796 /* FIXME: Enable non-PME# remote wakeup? */ 796 /* FIXME: Enable non-PME# remote wakeup? */
797 797
798 /* make sure snapshot being resumed re-enumerates everything */
799 if (message.event == PM_EVENT_PRETHAW)
800 uhci_hc_died(uhci);
801
802done_okay: 798done_okay:
803 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 799 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
804done: 800done:
@@ -806,7 +802,7 @@ done:
806 return rc; 802 return rc;
807} 803}
808 804
809static int uhci_pci_resume(struct usb_hcd *hcd) 805static int uhci_pci_resume(struct usb_hcd *hcd, bool hibernated)
810{ 806{
811 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 807 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
812 808
@@ -820,6 +816,10 @@ static int uhci_pci_resume(struct usb_hcd *hcd)
820 816
821 spin_lock_irq(&uhci->lock); 817 spin_lock_irq(&uhci->lock);
822 818
819 /* Make sure resume from hibernation re-enumerates everything */
820 if (hibernated)
821 uhci_hc_died(uhci);
822
823 /* FIXME: Disable non-PME# remote wakeup? */ 823 /* FIXME: Disable non-PME# remote wakeup? */
824 824
825 /* The firmware or a boot kernel may have changed the controller 825 /* The firmware or a boot kernel may have changed the controller
@@ -940,10 +940,11 @@ static struct pci_driver uhci_pci_driver = {
940 .remove = usb_hcd_pci_remove, 940 .remove = usb_hcd_pci_remove,
941 .shutdown = uhci_shutdown, 941 .shutdown = uhci_shutdown,
942 942
943#ifdef CONFIG_PM 943#ifdef CONFIG_PM_SLEEP
944 .suspend = usb_hcd_pci_suspend, 944 .driver = {
945 .resume = usb_hcd_pci_resume, 945 .pm = &usb_hcd_pci_pm_ops
946#endif /* PM */ 946 },
947#endif
947}; 948};
948 949
949static int __init uhci_hcd_init(void) 950static int __init uhci_hcd_init(void)
@@ -961,7 +962,7 @@ static int __init uhci_hcd_init(void)
961 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL); 962 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
962 if (!errbuf) 963 if (!errbuf)
963 goto errbuf_failed; 964 goto errbuf_failed;
964 uhci_debugfs_root = debugfs_create_dir("uhci", NULL); 965 uhci_debugfs_root = debugfs_create_dir("uhci", usb_debug_root);
965 if (!uhci_debugfs_root) 966 if (!uhci_debugfs_root)
966 goto debug_failed; 967 goto debug_failed;
967 } 968 }
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 3e5807d14ffb..64e57bfe236b 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -260,7 +260,7 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
260 INIT_LIST_HEAD(&qh->node); 260 INIT_LIST_HEAD(&qh->node);
261 261
262 if (udev) { /* Normal QH */ 262 if (udev) { /* Normal QH */
263 qh->type = hep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; 263 qh->type = usb_endpoint_type(&hep->desc);
264 if (qh->type != USB_ENDPOINT_XFER_ISOC) { 264 if (qh->type != USB_ENDPOINT_XFER_ISOC) {
265 qh->dummy_td = uhci_alloc_td(uhci); 265 qh->dummy_td = uhci_alloc_td(uhci);
266 if (!qh->dummy_td) { 266 if (!qh->dummy_td) {
diff --git a/drivers/usb/host/xhci-dbg.c b/drivers/usb/host/xhci-dbg.c
new file mode 100644
index 000000000000..2501c571f855
--- /dev/null
+++ b/drivers/usb/host/xhci-dbg.c
@@ -0,0 +1,485 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "xhci.h"
24
25#define XHCI_INIT_VALUE 0x0
26
27/* Add verbose debugging later, just print everything for now */
28
29void xhci_dbg_regs(struct xhci_hcd *xhci)
30{
31 u32 temp;
32
33 xhci_dbg(xhci, "// xHCI capability registers at %p:\n",
34 xhci->cap_regs);
35 temp = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
36 xhci_dbg(xhci, "// @%p = 0x%x (CAPLENGTH AND HCIVERSION)\n",
37 &xhci->cap_regs->hc_capbase, temp);
38 xhci_dbg(xhci, "// CAPLENGTH: 0x%x\n",
39 (unsigned int) HC_LENGTH(temp));
40#if 0
41 xhci_dbg(xhci, "// HCIVERSION: 0x%x\n",
42 (unsigned int) HC_VERSION(temp));
43#endif
44
45 xhci_dbg(xhci, "// xHCI operational registers at %p:\n", xhci->op_regs);
46
47 temp = xhci_readl(xhci, &xhci->cap_regs->run_regs_off);
48 xhci_dbg(xhci, "// @%p = 0x%x RTSOFF\n",
49 &xhci->cap_regs->run_regs_off,
50 (unsigned int) temp & RTSOFF_MASK);
51 xhci_dbg(xhci, "// xHCI runtime registers at %p:\n", xhci->run_regs);
52
53 temp = xhci_readl(xhci, &xhci->cap_regs->db_off);
54 xhci_dbg(xhci, "// @%p = 0x%x DBOFF\n", &xhci->cap_regs->db_off, temp);
55 xhci_dbg(xhci, "// Doorbell array at %p:\n", xhci->dba);
56}
57
58static void xhci_print_cap_regs(struct xhci_hcd *xhci)
59{
60 u32 temp;
61
62 xhci_dbg(xhci, "xHCI capability registers at %p:\n", xhci->cap_regs);
63
64 temp = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
65 xhci_dbg(xhci, "CAPLENGTH AND HCIVERSION 0x%x:\n",
66 (unsigned int) temp);
67 xhci_dbg(xhci, "CAPLENGTH: 0x%x\n",
68 (unsigned int) HC_LENGTH(temp));
69 xhci_dbg(xhci, "HCIVERSION: 0x%x\n",
70 (unsigned int) HC_VERSION(temp));
71
72 temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
73 xhci_dbg(xhci, "HCSPARAMS 1: 0x%x\n",
74 (unsigned int) temp);
75 xhci_dbg(xhci, " Max device slots: %u\n",
76 (unsigned int) HCS_MAX_SLOTS(temp));
77 xhci_dbg(xhci, " Max interrupters: %u\n",
78 (unsigned int) HCS_MAX_INTRS(temp));
79 xhci_dbg(xhci, " Max ports: %u\n",
80 (unsigned int) HCS_MAX_PORTS(temp));
81
82 temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
83 xhci_dbg(xhci, "HCSPARAMS 2: 0x%x\n",
84 (unsigned int) temp);
85 xhci_dbg(xhci, " Isoc scheduling threshold: %u\n",
86 (unsigned int) HCS_IST(temp));
87 xhci_dbg(xhci, " Maximum allowed segments in event ring: %u\n",
88 (unsigned int) HCS_ERST_MAX(temp));
89
90 temp = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
91 xhci_dbg(xhci, "HCSPARAMS 3 0x%x:\n",
92 (unsigned int) temp);
93 xhci_dbg(xhci, " Worst case U1 device exit latency: %u\n",
94 (unsigned int) HCS_U1_LATENCY(temp));
95 xhci_dbg(xhci, " Worst case U2 device exit latency: %u\n",
96 (unsigned int) HCS_U2_LATENCY(temp));
97
98 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
99 xhci_dbg(xhci, "HCC PARAMS 0x%x:\n", (unsigned int) temp);
100 xhci_dbg(xhci, " HC generates %s bit addresses\n",
101 HCC_64BIT_ADDR(temp) ? "64" : "32");
102 /* FIXME */
103 xhci_dbg(xhci, " FIXME: more HCCPARAMS debugging\n");
104
105 temp = xhci_readl(xhci, &xhci->cap_regs->run_regs_off);
106 xhci_dbg(xhci, "RTSOFF 0x%x:\n", temp & RTSOFF_MASK);
107}
108
109static void xhci_print_command_reg(struct xhci_hcd *xhci)
110{
111 u32 temp;
112
113 temp = xhci_readl(xhci, &xhci->op_regs->command);
114 xhci_dbg(xhci, "USBCMD 0x%x:\n", temp);
115 xhci_dbg(xhci, " HC is %s\n",
116 (temp & CMD_RUN) ? "running" : "being stopped");
117 xhci_dbg(xhci, " HC has %sfinished hard reset\n",
118 (temp & CMD_RESET) ? "not " : "");
119 xhci_dbg(xhci, " Event Interrupts %s\n",
120 (temp & CMD_EIE) ? "enabled " : "disabled");
121 xhci_dbg(xhci, " Host System Error Interrupts %s\n",
122 (temp & CMD_EIE) ? "enabled " : "disabled");
123 xhci_dbg(xhci, " HC has %sfinished light reset\n",
124 (temp & CMD_LRESET) ? "not " : "");
125}
126
127static void xhci_print_status(struct xhci_hcd *xhci)
128{
129 u32 temp;
130
131 temp = xhci_readl(xhci, &xhci->op_regs->status);
132 xhci_dbg(xhci, "USBSTS 0x%x:\n", temp);
133 xhci_dbg(xhci, " Event ring is %sempty\n",
134 (temp & STS_EINT) ? "not " : "");
135 xhci_dbg(xhci, " %sHost System Error\n",
136 (temp & STS_FATAL) ? "WARNING: " : "No ");
137 xhci_dbg(xhci, " HC is %s\n",
138 (temp & STS_HALT) ? "halted" : "running");
139}
140
141static void xhci_print_op_regs(struct xhci_hcd *xhci)
142{
143 xhci_dbg(xhci, "xHCI operational registers at %p:\n", xhci->op_regs);
144 xhci_print_command_reg(xhci);
145 xhci_print_status(xhci);
146}
147
148static void xhci_print_ports(struct xhci_hcd *xhci)
149{
150 u32 __iomem *addr;
151 int i, j;
152 int ports;
153 char *names[NUM_PORT_REGS] = {
154 "status",
155 "power",
156 "link",
157 "reserved",
158 };
159
160 ports = HCS_MAX_PORTS(xhci->hcs_params1);
161 addr = &xhci->op_regs->port_status_base;
162 for (i = 0; i < ports; i++) {
163 for (j = 0; j < NUM_PORT_REGS; ++j) {
164 xhci_dbg(xhci, "%p port %s reg = 0x%x\n",
165 addr, names[j],
166 (unsigned int) xhci_readl(xhci, addr));
167 addr++;
168 }
169 }
170}
171
172void xhci_print_ir_set(struct xhci_hcd *xhci, struct xhci_intr_reg *ir_set, int set_num)
173{
174 void *addr;
175 u32 temp;
176
177 addr = &ir_set->irq_pending;
178 temp = xhci_readl(xhci, addr);
179 if (temp == XHCI_INIT_VALUE)
180 return;
181
182 xhci_dbg(xhci, " %p: ir_set[%i]\n", ir_set, set_num);
183
184 xhci_dbg(xhci, " %p: ir_set.pending = 0x%x\n", addr,
185 (unsigned int)temp);
186
187 addr = &ir_set->irq_control;
188 temp = xhci_readl(xhci, addr);
189 xhci_dbg(xhci, " %p: ir_set.control = 0x%x\n", addr,
190 (unsigned int)temp);
191
192 addr = &ir_set->erst_size;
193 temp = xhci_readl(xhci, addr);
194 xhci_dbg(xhci, " %p: ir_set.erst_size = 0x%x\n", addr,
195 (unsigned int)temp);
196
197 addr = &ir_set->rsvd;
198 temp = xhci_readl(xhci, addr);
199 if (temp != XHCI_INIT_VALUE)
200 xhci_dbg(xhci, " WARN: %p: ir_set.rsvd = 0x%x\n",
201 addr, (unsigned int)temp);
202
203 addr = &ir_set->erst_base[0];
204 temp = xhci_readl(xhci, addr);
205 xhci_dbg(xhci, " %p: ir_set.erst_base[0] = 0x%x\n",
206 addr, (unsigned int) temp);
207
208 addr = &ir_set->erst_base[1];
209 temp = xhci_readl(xhci, addr);
210 xhci_dbg(xhci, " %p: ir_set.erst_base[1] = 0x%x\n",
211 addr, (unsigned int) temp);
212
213 addr = &ir_set->erst_dequeue[0];
214 temp = xhci_readl(xhci, addr);
215 xhci_dbg(xhci, " %p: ir_set.erst_dequeue[0] = 0x%x\n",
216 addr, (unsigned int) temp);
217
218 addr = &ir_set->erst_dequeue[1];
219 temp = xhci_readl(xhci, addr);
220 xhci_dbg(xhci, " %p: ir_set.erst_dequeue[1] = 0x%x\n",
221 addr, (unsigned int) temp);
222}
223
224void xhci_print_run_regs(struct xhci_hcd *xhci)
225{
226 u32 temp;
227 int i;
228
229 xhci_dbg(xhci, "xHCI runtime registers at %p:\n", xhci->run_regs);
230 temp = xhci_readl(xhci, &xhci->run_regs->microframe_index);
231 xhci_dbg(xhci, " %p: Microframe index = 0x%x\n",
232 &xhci->run_regs->microframe_index,
233 (unsigned int) temp);
234 for (i = 0; i < 7; ++i) {
235 temp = xhci_readl(xhci, &xhci->run_regs->rsvd[i]);
236 if (temp != XHCI_INIT_VALUE)
237 xhci_dbg(xhci, " WARN: %p: Rsvd[%i] = 0x%x\n",
238 &xhci->run_regs->rsvd[i],
239 i, (unsigned int) temp);
240 }
241}
242
243void xhci_print_registers(struct xhci_hcd *xhci)
244{
245 xhci_print_cap_regs(xhci);
246 xhci_print_op_regs(xhci);
247 xhci_print_ports(xhci);
248}
249
250void xhci_print_trb_offsets(struct xhci_hcd *xhci, union xhci_trb *trb)
251{
252 int i;
253 for (i = 0; i < 4; ++i)
254 xhci_dbg(xhci, "Offset 0x%x = 0x%x\n",
255 i*4, trb->generic.field[i]);
256}
257
258/**
259 * Debug a transfer request block (TRB).
260 */
261void xhci_debug_trb(struct xhci_hcd *xhci, union xhci_trb *trb)
262{
263 u64 address;
264 u32 type = xhci_readl(xhci, &trb->link.control) & TRB_TYPE_BITMASK;
265
266 switch (type) {
267 case TRB_TYPE(TRB_LINK):
268 xhci_dbg(xhci, "Link TRB:\n");
269 xhci_print_trb_offsets(xhci, trb);
270
271 address = trb->link.segment_ptr[0] +
272 (((u64) trb->link.segment_ptr[1]) << 32);
273 xhci_dbg(xhci, "Next ring segment DMA address = 0x%llx\n", address);
274
275 xhci_dbg(xhci, "Interrupter target = 0x%x\n",
276 GET_INTR_TARGET(trb->link.intr_target));
277 xhci_dbg(xhci, "Cycle bit = %u\n",
278 (unsigned int) (trb->link.control & TRB_CYCLE));
279 xhci_dbg(xhci, "Toggle cycle bit = %u\n",
280 (unsigned int) (trb->link.control & LINK_TOGGLE));
281 xhci_dbg(xhci, "No Snoop bit = %u\n",
282 (unsigned int) (trb->link.control & TRB_NO_SNOOP));
283 break;
284 case TRB_TYPE(TRB_TRANSFER):
285 address = trb->trans_event.buffer[0] +
286 (((u64) trb->trans_event.buffer[1]) << 32);
287 /*
288 * FIXME: look at flags to figure out if it's an address or if
289 * the data is directly in the buffer field.
290 */
291 xhci_dbg(xhci, "DMA address or buffer contents= %llu\n", address);
292 break;
293 case TRB_TYPE(TRB_COMPLETION):
294 address = trb->event_cmd.cmd_trb[0] +
295 (((u64) trb->event_cmd.cmd_trb[1]) << 32);
296 xhci_dbg(xhci, "Command TRB pointer = %llu\n", address);
297 xhci_dbg(xhci, "Completion status = %u\n",
298 (unsigned int) GET_COMP_CODE(trb->event_cmd.status));
299 xhci_dbg(xhci, "Flags = 0x%x\n", (unsigned int) trb->event_cmd.flags);
300 break;
301 default:
302 xhci_dbg(xhci, "Unknown TRB with TRB type ID %u\n",
303 (unsigned int) type>>10);
304 xhci_print_trb_offsets(xhci, trb);
305 break;
306 }
307}
308
309/**
310 * Debug a segment with an xHCI ring.
311 *
312 * @return The Link TRB of the segment, or NULL if there is no Link TRB
313 * (which is a bug, since all segments must have a Link TRB).
314 *
315 * Prints out all TRBs in the segment, even those after the Link TRB.
316 *
317 * XXX: should we print out TRBs that the HC owns? As long as we don't
318 * write, that should be fine... We shouldn't expect that the memory pointed to
319 * by the TRB is valid at all. Do we care about ones the HC owns? Probably,
320 * for HC debugging.
321 */
322void xhci_debug_segment(struct xhci_hcd *xhci, struct xhci_segment *seg)
323{
324 int i;
325 u32 addr = (u32) seg->dma;
326 union xhci_trb *trb = seg->trbs;
327
328 for (i = 0; i < TRBS_PER_SEGMENT; ++i) {
329 trb = &seg->trbs[i];
330 xhci_dbg(xhci, "@%08x %08x %08x %08x %08x\n", addr,
331 (unsigned int) trb->link.segment_ptr[0],
332 (unsigned int) trb->link.segment_ptr[1],
333 (unsigned int) trb->link.intr_target,
334 (unsigned int) trb->link.control);
335 addr += sizeof(*trb);
336 }
337}
338
339void xhci_dbg_ring_ptrs(struct xhci_hcd *xhci, struct xhci_ring *ring)
340{
341 xhci_dbg(xhci, "Ring deq = %p (virt), 0x%llx (dma)\n",
342 ring->dequeue,
343 (unsigned long long)xhci_trb_virt_to_dma(ring->deq_seg,
344 ring->dequeue));
345 xhci_dbg(xhci, "Ring deq updated %u times\n",
346 ring->deq_updates);
347 xhci_dbg(xhci, "Ring enq = %p (virt), 0x%llx (dma)\n",
348 ring->enqueue,
349 (unsigned long long)xhci_trb_virt_to_dma(ring->enq_seg,
350 ring->enqueue));
351 xhci_dbg(xhci, "Ring enq updated %u times\n",
352 ring->enq_updates);
353}
354
355/**
356 * Debugging for an xHCI ring, which is a queue broken into multiple segments.
357 *
358 * Print out each segment in the ring. Check that the DMA address in
359 * each link segment actually matches the segment's stored DMA address.
360 * Check that the link end bit is only set at the end of the ring.
361 * Check that the dequeue and enqueue pointers point to real data in this ring
362 * (not some other ring).
363 */
364void xhci_debug_ring(struct xhci_hcd *xhci, struct xhci_ring *ring)
365{
366 /* FIXME: Throw an error if any segment doesn't have a Link TRB */
367 struct xhci_segment *seg;
368 struct xhci_segment *first_seg = ring->first_seg;
369 xhci_debug_segment(xhci, first_seg);
370
371 if (!ring->enq_updates && !ring->deq_updates) {
372 xhci_dbg(xhci, " Ring has not been updated\n");
373 return;
374 }
375 for (seg = first_seg->next; seg != first_seg; seg = seg->next)
376 xhci_debug_segment(xhci, seg);
377}
378
379void xhci_dbg_erst(struct xhci_hcd *xhci, struct xhci_erst *erst)
380{
381 u32 addr = (u32) erst->erst_dma_addr;
382 int i;
383 struct xhci_erst_entry *entry;
384
385 for (i = 0; i < erst->num_entries; ++i) {
386 entry = &erst->entries[i];
387 xhci_dbg(xhci, "@%08x %08x %08x %08x %08x\n",
388 (unsigned int) addr,
389 (unsigned int) entry->seg_addr[0],
390 (unsigned int) entry->seg_addr[1],
391 (unsigned int) entry->seg_size,
392 (unsigned int) entry->rsvd);
393 addr += sizeof(*entry);
394 }
395}
396
397void xhci_dbg_cmd_ptrs(struct xhci_hcd *xhci)
398{
399 u32 val;
400
401 val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[0]);
402 xhci_dbg(xhci, "// xHC command ring deq ptr low bits + flags = 0x%x\n", val);
403 val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[1]);
404 xhci_dbg(xhci, "// xHC command ring deq ptr high bits = 0x%x\n", val);
405}
406
407void xhci_dbg_ctx(struct xhci_hcd *xhci, struct xhci_device_control *ctx, dma_addr_t dma, unsigned int last_ep)
408{
409 int i, j;
410 int last_ep_ctx = 31;
411 /* Fields are 32 bits wide, DMA addresses are in bytes */
412 int field_size = 32 / 8;
413
414 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - drop flags\n",
415 &ctx->drop_flags, (unsigned long long)dma,
416 ctx->drop_flags);
417 dma += field_size;
418 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - add flags\n",
419 &ctx->add_flags, (unsigned long long)dma,
420 ctx->add_flags);
421 dma += field_size;
422 for (i = 0; i > 6; ++i) {
423 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - rsvd[%d]\n",
424 &ctx->rsvd[i], (unsigned long long)dma,
425 ctx->rsvd[i], i);
426 dma += field_size;
427 }
428
429 xhci_dbg(xhci, "Slot Context:\n");
430 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - dev_info\n",
431 &ctx->slot.dev_info,
432 (unsigned long long)dma, ctx->slot.dev_info);
433 dma += field_size;
434 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - dev_info2\n",
435 &ctx->slot.dev_info2,
436 (unsigned long long)dma, ctx->slot.dev_info2);
437 dma += field_size;
438 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - tt_info\n",
439 &ctx->slot.tt_info,
440 (unsigned long long)dma, ctx->slot.tt_info);
441 dma += field_size;
442 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - dev_state\n",
443 &ctx->slot.dev_state,
444 (unsigned long long)dma, ctx->slot.dev_state);
445 dma += field_size;
446 for (i = 0; i > 4; ++i) {
447 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - rsvd[%d]\n",
448 &ctx->slot.reserved[i], (unsigned long long)dma,
449 ctx->slot.reserved[i], i);
450 dma += field_size;
451 }
452
453 if (last_ep < 31)
454 last_ep_ctx = last_ep + 1;
455 for (i = 0; i < last_ep_ctx; ++i) {
456 xhci_dbg(xhci, "Endpoint %02d Context:\n", i);
457 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - ep_info\n",
458 &ctx->ep[i].ep_info,
459 (unsigned long long)dma, ctx->ep[i].ep_info);
460 dma += field_size;
461 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - ep_info2\n",
462 &ctx->ep[i].ep_info2,
463 (unsigned long long)dma, ctx->ep[i].ep_info2);
464 dma += field_size;
465 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - deq[0]\n",
466 &ctx->ep[i].deq[0],
467 (unsigned long long)dma, ctx->ep[i].deq[0]);
468 dma += field_size;
469 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - deq[1]\n",
470 &ctx->ep[i].deq[1],
471 (unsigned long long)dma, ctx->ep[i].deq[1]);
472 dma += field_size;
473 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - tx_info\n",
474 &ctx->ep[i].tx_info,
475 (unsigned long long)dma, ctx->ep[i].tx_info);
476 dma += field_size;
477 for (j = 0; j < 3; ++j) {
478 xhci_dbg(xhci, "@%p (virt) @%08llx (dma) %#08x - rsvd[%d]\n",
479 &ctx->ep[i].reserved[j],
480 (unsigned long long)dma,
481 ctx->ep[i].reserved[j], j);
482 dma += field_size;
483 }
484 }
485}
diff --git a/drivers/usb/host/xhci-ext-caps.h b/drivers/usb/host/xhci-ext-caps.h
new file mode 100644
index 000000000000..ecc131c3fe33
--- /dev/null
+++ b/drivers/usb/host/xhci-ext-caps.h
@@ -0,0 +1,145 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22/* Up to 16 microframes to halt an HC - one microframe is 125 microsectonds */
23#define XHCI_MAX_HALT_USEC (16*125)
24/* HC not running - set to 1 when run/stop bit is cleared. */
25#define XHCI_STS_HALT (1<<0)
26
27/* HCCPARAMS offset from PCI base address */
28#define XHCI_HCC_PARAMS_OFFSET 0x10
29/* HCCPARAMS contains the first extended capability pointer */
30#define XHCI_HCC_EXT_CAPS(p) (((p)>>16)&0xffff)
31
32/* Command and Status registers offset from the Operational Registers address */
33#define XHCI_CMD_OFFSET 0x00
34#define XHCI_STS_OFFSET 0x04
35
36#define XHCI_MAX_EXT_CAPS 50
37
38/* Capability Register */
39/* bits 7:0 - how long is the Capabilities register */
40#define XHCI_HC_LENGTH(p) (((p)>>00)&0x00ff)
41
42/* Extended capability register fields */
43#define XHCI_EXT_CAPS_ID(p) (((p)>>0)&0xff)
44#define XHCI_EXT_CAPS_NEXT(p) (((p)>>8)&0xff)
45#define XHCI_EXT_CAPS_VAL(p) ((p)>>16)
46/* Extended capability IDs - ID 0 reserved */
47#define XHCI_EXT_CAPS_LEGACY 1
48#define XHCI_EXT_CAPS_PROTOCOL 2
49#define XHCI_EXT_CAPS_PM 3
50#define XHCI_EXT_CAPS_VIRT 4
51#define XHCI_EXT_CAPS_ROUTE 5
52/* IDs 6-9 reserved */
53#define XHCI_EXT_CAPS_DEBUG 10
54/* USB Legacy Support Capability - section 7.1.1 */
55#define XHCI_HC_BIOS_OWNED (1 << 16)
56#define XHCI_HC_OS_OWNED (1 << 24)
57
58/* USB Legacy Support Capability - section 7.1.1 */
59/* Add this offset, plus the value of xECP in HCCPARAMS to the base address */
60#define XHCI_LEGACY_SUPPORT_OFFSET (0x00)
61
62/* USB Legacy Support Control and Status Register - section 7.1.2 */
63/* Add this offset, plus the value of xECP in HCCPARAMS to the base address */
64#define XHCI_LEGACY_CONTROL_OFFSET (0x04)
65/* bits 1:2, 5:12, and 17:19 need to be preserved; bits 21:28 should be zero */
66#define XHCI_LEGACY_DISABLE_SMI ((0x3 << 1) + (0xff << 5) + (0x7 << 17))
67
68/* command register values to disable interrupts and halt the HC */
69/* start/stop HC execution - do not write unless HC is halted*/
70#define XHCI_CMD_RUN (1 << 0)
71/* Event Interrupt Enable - get irq when EINT bit is set in USBSTS register */
72#define XHCI_CMD_EIE (1 << 2)
73/* Host System Error Interrupt Enable - get irq when HSEIE bit set in USBSTS */
74#define XHCI_CMD_HSEIE (1 << 3)
75/* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
76#define XHCI_CMD_EWE (1 << 10)
77
78#define XHCI_IRQS (XHCI_CMD_EIE | XHCI_CMD_HSEIE | XHCI_CMD_EWE)
79
80/* true: Controller Not Ready to accept doorbell or op reg writes after reset */
81#define XHCI_STS_CNR (1 << 11)
82
83#include <linux/io.h>
84
85/**
86 * Return the next extended capability pointer register.
87 *
88 * @base PCI register base address.
89 *
90 * @ext_offset Offset of the 32-bit register that contains the extended
91 * capabilites pointer. If searching for the first extended capability, pass
92 * in XHCI_HCC_PARAMS_OFFSET. If searching for the next extended capability,
93 * pass in the offset of the current extended capability register.
94 *
95 * Returns 0 if there is no next extended capability register or returns the register offset
96 * from the PCI registers base address.
97 */
98static inline int xhci_find_next_cap_offset(void __iomem *base, int ext_offset)
99{
100 u32 next;
101
102 next = readl(base + ext_offset);
103
104 if (ext_offset == XHCI_HCC_PARAMS_OFFSET)
105 /* Find the first extended capability */
106 next = XHCI_HCC_EXT_CAPS(next);
107 else
108 /* Find the next extended capability */
109 next = XHCI_EXT_CAPS_NEXT(next);
110 if (!next)
111 return 0;
112 /*
113 * Address calculation from offset of extended capabilities
114 * (or HCCPARAMS) register - see section 5.3.6 and section 7.
115 */
116 return ext_offset + (next << 2);
117}
118
119/**
120 * Find the offset of the extended capabilities with capability ID id.
121 *
122 * @base PCI MMIO registers base address.
123 * @ext_offset Offset from base of the first extended capability to look at,
124 * or the address of HCCPARAMS.
125 * @id Extended capability ID to search for.
126 *
127 * This uses an arbitrary limit of XHCI_MAX_EXT_CAPS extended capabilities
128 * to make sure that the list doesn't contain a loop.
129 */
130static inline int xhci_find_ext_cap_by_id(void __iomem *base, int ext_offset, int id)
131{
132 u32 val;
133 int limit = XHCI_MAX_EXT_CAPS;
134
135 while (ext_offset && limit > 0) {
136 val = readl(base + ext_offset);
137 if (XHCI_EXT_CAPS_ID(val) == id)
138 break;
139 ext_offset = xhci_find_next_cap_offset(base, ext_offset);
140 limit--;
141 }
142 if (limit > 0)
143 return ext_offset;
144 return 0;
145}
diff --git a/drivers/usb/host/xhci-hcd.c b/drivers/usb/host/xhci-hcd.c
new file mode 100644
index 000000000000..dba3e07ccd09
--- /dev/null
+++ b/drivers/usb/host/xhci-hcd.c
@@ -0,0 +1,1274 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/irq.h>
24#include <linux/module.h>
25
26#include "xhci.h"
27
28#define DRIVER_AUTHOR "Sarah Sharp"
29#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
30
31/* TODO: copied from ehci-hcd.c - can this be refactored? */
32/*
33 * handshake - spin reading hc until handshake completes or fails
34 * @ptr: address of hc register to be read
35 * @mask: bits to look at in result of read
36 * @done: value of those bits when handshake succeeds
37 * @usec: timeout in microseconds
38 *
39 * Returns negative errno, or zero on success
40 *
41 * Success happens when the "mask" bits have the specified value (hardware
42 * handshake done). There are two failure modes: "usec" have passed (major
43 * hardware flakeout), or the register reads as all-ones (hardware removed).
44 */
45static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
46 u32 mask, u32 done, int usec)
47{
48 u32 result;
49
50 do {
51 result = xhci_readl(xhci, ptr);
52 if (result == ~(u32)0) /* card removed */
53 return -ENODEV;
54 result &= mask;
55 if (result == done)
56 return 0;
57 udelay(1);
58 usec--;
59 } while (usec > 0);
60 return -ETIMEDOUT;
61}
62
63/*
64 * Force HC into halt state.
65 *
66 * Disable any IRQs and clear the run/stop bit.
67 * HC will complete any current and actively pipelined transactions, and
68 * should halt within 16 microframes of the run/stop bit being cleared.
69 * Read HC Halted bit in the status register to see when the HC is finished.
70 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
71 */
72int xhci_halt(struct xhci_hcd *xhci)
73{
74 u32 halted;
75 u32 cmd;
76 u32 mask;
77
78 xhci_dbg(xhci, "// Halt the HC\n");
79 /* Disable all interrupts from the host controller */
80 mask = ~(XHCI_IRQS);
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
82 if (!halted)
83 mask &= ~CMD_RUN;
84
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
86 cmd &= mask;
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
88
89 return handshake(xhci, &xhci->op_regs->status,
90 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
91}
92
93/*
94 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
95 *
96 * This resets pipelines, timers, counters, state machines, etc.
97 * Transactions will be terminated immediately, and operational registers
98 * will be set to their defaults.
99 */
100int xhci_reset(struct xhci_hcd *xhci)
101{
102 u32 command;
103 u32 state;
104
105 state = xhci_readl(xhci, &xhci->op_regs->status);
106 BUG_ON((state & STS_HALT) == 0);
107
108 xhci_dbg(xhci, "// Reset the HC\n");
109 command = xhci_readl(xhci, &xhci->op_regs->command);
110 command |= CMD_RESET;
111 xhci_writel(xhci, command, &xhci->op_regs->command);
112 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
113 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
114
115 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
116}
117
118/*
119 * Stop the HC from processing the endpoint queues.
120 */
121static void xhci_quiesce(struct xhci_hcd *xhci)
122{
123 /*
124 * Queues are per endpoint, so we need to disable an endpoint or slot.
125 *
126 * To disable a slot, we need to insert a disable slot command on the
127 * command ring and ring the doorbell. This will also free any internal
128 * resources associated with the slot (which might not be what we want).
129 *
130 * A Release Endpoint command sounds better - doesn't free internal HC
131 * memory, but removes the endpoints from the schedule and releases the
132 * bandwidth, disables the doorbells, and clears the endpoint enable
133 * flag. Usually used prior to a set interface command.
134 *
135 * TODO: Implement after command ring code is done.
136 */
137 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
138 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
139}
140
141#if 0
142/* Set up MSI-X table for entry 0 (may claim other entries later) */
143static int xhci_setup_msix(struct xhci_hcd *xhci)
144{
145 int ret;
146 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
147
148 xhci->msix_count = 0;
149 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
150 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
151 if (!xhci->msix_entries) {
152 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
153 return -ENOMEM;
154 }
155 xhci->msix_entries[0].entry = 0;
156
157 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
158 if (ret) {
159 xhci_err(xhci, "Failed to enable MSI-X\n");
160 goto free_entries;
161 }
162
163 /*
164 * Pass the xhci pointer value as the request_irq "cookie".
165 * If more irqs are added, this will need to be unique for each one.
166 */
167 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
168 "xHCI", xhci_to_hcd(xhci));
169 if (ret) {
170 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
171 goto disable_msix;
172 }
173 xhci_dbg(xhci, "Finished setting up MSI-X\n");
174 return 0;
175
176disable_msix:
177 pci_disable_msix(pdev);
178free_entries:
179 kfree(xhci->msix_entries);
180 xhci->msix_entries = NULL;
181 return ret;
182}
183
184/* XXX: code duplication; can xhci_setup_msix call this? */
185/* Free any IRQs and disable MSI-X */
186static void xhci_cleanup_msix(struct xhci_hcd *xhci)
187{
188 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
189 if (!xhci->msix_entries)
190 return;
191
192 free_irq(xhci->msix_entries[0].vector, xhci);
193 pci_disable_msix(pdev);
194 kfree(xhci->msix_entries);
195 xhci->msix_entries = NULL;
196 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
197}
198#endif
199
200/*
201 * Initialize memory for HCD and xHC (one-time init).
202 *
203 * Program the PAGESIZE register, initialize the device context array, create
204 * device contexts (?), set up a command ring segment (or two?), create event
205 * ring (one for now).
206 */
207int xhci_init(struct usb_hcd *hcd)
208{
209 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
210 int retval = 0;
211
212 xhci_dbg(xhci, "xhci_init\n");
213 spin_lock_init(&xhci->lock);
214 retval = xhci_mem_init(xhci, GFP_KERNEL);
215 xhci_dbg(xhci, "Finished xhci_init\n");
216
217 return retval;
218}
219
220/*
221 * Called in interrupt context when there might be work
222 * queued on the event ring
223 *
224 * xhci->lock must be held by caller.
225 */
226static void xhci_work(struct xhci_hcd *xhci)
227{
228 u32 temp;
229
230 /*
231 * Clear the op reg interrupt status first,
232 * so we can receive interrupts from other MSI-X interrupters.
233 * Write 1 to clear the interrupt status.
234 */
235 temp = xhci_readl(xhci, &xhci->op_regs->status);
236 temp |= STS_EINT;
237 xhci_writel(xhci, temp, &xhci->op_regs->status);
238 /* FIXME when MSI-X is supported and there are multiple vectors */
239 /* Clear the MSI-X event interrupt status */
240
241 /* Acknowledge the interrupt */
242 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
243 temp |= 0x3;
244 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
245 /* Flush posted writes */
246 xhci_readl(xhci, &xhci->ir_set->irq_pending);
247
248 /* FIXME this should be a delayed service routine that clears the EHB */
249 xhci_handle_event(xhci);
250
251 /* Clear the event handler busy flag; the event ring should be empty. */
252 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
253 xhci_writel(xhci, temp & ~ERST_EHB, &xhci->ir_set->erst_dequeue[0]);
254 /* Flush posted writes -- FIXME is this necessary? */
255 xhci_readl(xhci, &xhci->ir_set->irq_pending);
256}
257
258/*-------------------------------------------------------------------------*/
259
260/*
261 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
262 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
263 * indicators of an event TRB error, but we check the status *first* to be safe.
264 */
265irqreturn_t xhci_irq(struct usb_hcd *hcd)
266{
267 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
268 u32 temp, temp2;
269
270 spin_lock(&xhci->lock);
271 /* Check if the xHC generated the interrupt, or the irq is shared */
272 temp = xhci_readl(xhci, &xhci->op_regs->status);
273 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
274 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
275 spin_unlock(&xhci->lock);
276 return IRQ_NONE;
277 }
278
279 if (temp & STS_FATAL) {
280 xhci_warn(xhci, "WARNING: Host System Error\n");
281 xhci_halt(xhci);
282 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
283 spin_unlock(&xhci->lock);
284 return -ESHUTDOWN;
285 }
286
287 xhci_work(xhci);
288 spin_unlock(&xhci->lock);
289
290 return IRQ_HANDLED;
291}
292
293#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
294void xhci_event_ring_work(unsigned long arg)
295{
296 unsigned long flags;
297 int temp;
298 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
299 int i, j;
300
301 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
302
303 spin_lock_irqsave(&xhci->lock, flags);
304 temp = xhci_readl(xhci, &xhci->op_regs->status);
305 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
306 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
307 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
308 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
309 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
310 xhci->error_bitmask = 0;
311 xhci_dbg(xhci, "Event ring:\n");
312 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
313 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
314 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
315 temp &= ERST_PTR_MASK;
316 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
317 xhci_dbg(xhci, "Command ring:\n");
318 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
319 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
320 xhci_dbg_cmd_ptrs(xhci);
321 for (i = 0; i < MAX_HC_SLOTS; ++i) {
322 if (xhci->devs[i]) {
323 for (j = 0; j < 31; ++j) {
324 if (xhci->devs[i]->ep_rings[j]) {
325 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
326 xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
327 }
328 }
329 }
330 }
331
332 if (xhci->noops_submitted != NUM_TEST_NOOPS)
333 if (xhci_setup_one_noop(xhci))
334 xhci_ring_cmd_db(xhci);
335 spin_unlock_irqrestore(&xhci->lock, flags);
336
337 if (!xhci->zombie)
338 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
339 else
340 xhci_dbg(xhci, "Quit polling the event ring.\n");
341}
342#endif
343
344/*
345 * Start the HC after it was halted.
346 *
347 * This function is called by the USB core when the HC driver is added.
348 * Its opposite is xhci_stop().
349 *
350 * xhci_init() must be called once before this function can be called.
351 * Reset the HC, enable device slot contexts, program DCBAAP, and
352 * set command ring pointer and event ring pointer.
353 *
354 * Setup MSI-X vectors and enable interrupts.
355 */
356int xhci_run(struct usb_hcd *hcd)
357{
358 u32 temp;
359 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
360 void (*doorbell)(struct xhci_hcd *) = NULL;
361
362 hcd->uses_new_polling = 1;
363 hcd->poll_rh = 0;
364
365 xhci_dbg(xhci, "xhci_run\n");
366#if 0 /* FIXME: MSI not setup yet */
367 /* Do this at the very last minute */
368 ret = xhci_setup_msix(xhci);
369 if (!ret)
370 return ret;
371
372 return -ENOSYS;
373#endif
374#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
375 init_timer(&xhci->event_ring_timer);
376 xhci->event_ring_timer.data = (unsigned long) xhci;
377 xhci->event_ring_timer.function = xhci_event_ring_work;
378 /* Poll the event ring */
379 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
380 xhci->zombie = 0;
381 xhci_dbg(xhci, "Setting event ring polling timer\n");
382 add_timer(&xhci->event_ring_timer);
383#endif
384
385 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
386 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
387 temp &= ~ER_IRQ_INTERVAL_MASK;
388 temp |= (u32) 160;
389 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
390
391 /* Set the HCD state before we enable the irqs */
392 hcd->state = HC_STATE_RUNNING;
393 temp = xhci_readl(xhci, &xhci->op_regs->command);
394 temp |= (CMD_EIE);
395 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
396 temp);
397 xhci_writel(xhci, temp, &xhci->op_regs->command);
398
399 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
400 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
401 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
402 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
403 &xhci->ir_set->irq_pending);
404 xhci_print_ir_set(xhci, xhci->ir_set, 0);
405
406 if (NUM_TEST_NOOPS > 0)
407 doorbell = xhci_setup_one_noop(xhci);
408
409 xhci_dbg(xhci, "Command ring memory map follows:\n");
410 xhci_debug_ring(xhci, xhci->cmd_ring);
411 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
412 xhci_dbg_cmd_ptrs(xhci);
413
414 xhci_dbg(xhci, "ERST memory map follows:\n");
415 xhci_dbg_erst(xhci, &xhci->erst);
416 xhci_dbg(xhci, "Event ring:\n");
417 xhci_debug_ring(xhci, xhci->event_ring);
418 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
419 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
420 temp &= ERST_PTR_MASK;
421 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
422 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[1]);
423 xhci_dbg(xhci, "ERST deq upper = 0x%x\n", temp);
424
425 temp = xhci_readl(xhci, &xhci->op_regs->command);
426 temp |= (CMD_RUN);
427 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
428 temp);
429 xhci_writel(xhci, temp, &xhci->op_regs->command);
430 /* Flush PCI posted writes */
431 temp = xhci_readl(xhci, &xhci->op_regs->command);
432 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
433 if (doorbell)
434 (*doorbell)(xhci);
435
436 xhci_dbg(xhci, "Finished xhci_run\n");
437 return 0;
438}
439
440/*
441 * Stop xHCI driver.
442 *
443 * This function is called by the USB core when the HC driver is removed.
444 * Its opposite is xhci_run().
445 *
446 * Disable device contexts, disable IRQs, and quiesce the HC.
447 * Reset the HC, finish any completed transactions, and cleanup memory.
448 */
449void xhci_stop(struct usb_hcd *hcd)
450{
451 u32 temp;
452 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
453
454 spin_lock_irq(&xhci->lock);
455 if (HC_IS_RUNNING(hcd->state))
456 xhci_quiesce(xhci);
457 xhci_halt(xhci);
458 xhci_reset(xhci);
459 spin_unlock_irq(&xhci->lock);
460
461#if 0 /* No MSI yet */
462 xhci_cleanup_msix(xhci);
463#endif
464#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
465 /* Tell the event ring poll function not to reschedule */
466 xhci->zombie = 1;
467 del_timer_sync(&xhci->event_ring_timer);
468#endif
469
470 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
471 temp = xhci_readl(xhci, &xhci->op_regs->status);
472 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
473 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
474 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
475 &xhci->ir_set->irq_pending);
476 xhci_print_ir_set(xhci, xhci->ir_set, 0);
477
478 xhci_dbg(xhci, "cleaning up memory\n");
479 xhci_mem_cleanup(xhci);
480 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
481 xhci_readl(xhci, &xhci->op_regs->status));
482}
483
484/*
485 * Shutdown HC (not bus-specific)
486 *
487 * This is called when the machine is rebooting or halting. We assume that the
488 * machine will be powered off, and the HC's internal state will be reset.
489 * Don't bother to free memory.
490 */
491void xhci_shutdown(struct usb_hcd *hcd)
492{
493 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
494
495 spin_lock_irq(&xhci->lock);
496 xhci_halt(xhci);
497 spin_unlock_irq(&xhci->lock);
498
499#if 0
500 xhci_cleanup_msix(xhci);
501#endif
502
503 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
504 xhci_readl(xhci, &xhci->op_regs->status));
505}
506
507/*-------------------------------------------------------------------------*/
508
509/**
510 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
511 * HCDs. Find the index for an endpoint given its descriptor. Use the return
512 * value to right shift 1 for the bitmask.
513 *
514 * Index = (epnum * 2) + direction - 1,
515 * where direction = 0 for OUT, 1 for IN.
516 * For control endpoints, the IN index is used (OUT index is unused), so
517 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
518 */
519unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
520{
521 unsigned int index;
522 if (usb_endpoint_xfer_control(desc))
523 index = (unsigned int) (usb_endpoint_num(desc)*2);
524 else
525 index = (unsigned int) (usb_endpoint_num(desc)*2) +
526 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
527 return index;
528}
529
530/* Find the flag for this endpoint (for use in the control context). Use the
531 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
532 * bit 1, etc.
533 */
534unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
535{
536 return 1 << (xhci_get_endpoint_index(desc) + 1);
537}
538
539/* Compute the last valid endpoint context index. Basically, this is the
540 * endpoint index plus one. For slot contexts with more than valid endpoint,
541 * we find the most significant bit set in the added contexts flags.
542 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
543 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
544 */
545static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
546{
547 return fls(added_ctxs) - 1;
548}
549
550/* Returns 1 if the arguments are OK;
551 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
552 */
553int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
554 struct usb_host_endpoint *ep, int check_ep, const char *func) {
555 if (!hcd || (check_ep && !ep) || !udev) {
556 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
557 func);
558 return -EINVAL;
559 }
560 if (!udev->parent) {
561 printk(KERN_DEBUG "xHCI %s called for root hub\n",
562 func);
563 return 0;
564 }
565 if (!udev->slot_id) {
566 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
567 func);
568 return -EINVAL;
569 }
570 return 1;
571}
572
573/*
574 * non-error returns are a promise to giveback() the urb later
575 * we drop ownership so next owner (or urb unlink) can get it
576 */
577int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
578{
579 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
580 unsigned long flags;
581 int ret = 0;
582 unsigned int slot_id, ep_index;
583
584 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
585 return -EINVAL;
586
587 slot_id = urb->dev->slot_id;
588 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
589
590 spin_lock_irqsave(&xhci->lock, flags);
591 if (!xhci->devs || !xhci->devs[slot_id]) {
592 if (!in_interrupt())
593 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
594 ret = -EINVAL;
595 goto exit;
596 }
597 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
598 if (!in_interrupt())
599 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
600 ret = -ESHUTDOWN;
601 goto exit;
602 }
603 if (usb_endpoint_xfer_control(&urb->ep->desc))
604 ret = xhci_queue_ctrl_tx(xhci, mem_flags, urb,
605 slot_id, ep_index);
606 else if (usb_endpoint_xfer_bulk(&urb->ep->desc))
607 ret = xhci_queue_bulk_tx(xhci, mem_flags, urb,
608 slot_id, ep_index);
609 else
610 ret = -EINVAL;
611exit:
612 spin_unlock_irqrestore(&xhci->lock, flags);
613 return ret;
614}
615
616/*
617 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
618 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
619 * should pick up where it left off in the TD, unless a Set Transfer Ring
620 * Dequeue Pointer is issued.
621 *
622 * The TRBs that make up the buffers for the canceled URB will be "removed" from
623 * the ring. Since the ring is a contiguous structure, they can't be physically
624 * removed. Instead, there are two options:
625 *
626 * 1) If the HC is in the middle of processing the URB to be canceled, we
627 * simply move the ring's dequeue pointer past those TRBs using the Set
628 * Transfer Ring Dequeue Pointer command. This will be the common case,
629 * when drivers timeout on the last submitted URB and attempt to cancel.
630 *
631 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
632 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
633 * HC will need to invalidate the any TRBs it has cached after the stop
634 * endpoint command, as noted in the xHCI 0.95 errata.
635 *
636 * 3) The TD may have completed by the time the Stop Endpoint Command
637 * completes, so software needs to handle that case too.
638 *
639 * This function should protect against the TD enqueueing code ringing the
640 * doorbell while this code is waiting for a Stop Endpoint command to complete.
641 * It also needs to account for multiple cancellations on happening at the same
642 * time for the same endpoint.
643 *
644 * Note that this function can be called in any context, or so says
645 * usb_hcd_unlink_urb()
646 */
647int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
648{
649 unsigned long flags;
650 int ret;
651 struct xhci_hcd *xhci;
652 struct xhci_td *td;
653 unsigned int ep_index;
654 struct xhci_ring *ep_ring;
655
656 xhci = hcd_to_xhci(hcd);
657 spin_lock_irqsave(&xhci->lock, flags);
658 /* Make sure the URB hasn't completed or been unlinked already */
659 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
660 if (ret || !urb->hcpriv)
661 goto done;
662
663 xhci_dbg(xhci, "Cancel URB %p\n", urb);
664 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
665 ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
666 td = (struct xhci_td *) urb->hcpriv;
667
668 ep_ring->cancels_pending++;
669 list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
670 /* Queue a stop endpoint command, but only if this is
671 * the first cancellation to be handled.
672 */
673 if (ep_ring->cancels_pending == 1) {
674 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
675 xhci_ring_cmd_db(xhci);
676 }
677done:
678 spin_unlock_irqrestore(&xhci->lock, flags);
679 return ret;
680}
681
682/* Drop an endpoint from a new bandwidth configuration for this device.
683 * Only one call to this function is allowed per endpoint before
684 * check_bandwidth() or reset_bandwidth() must be called.
685 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
686 * add the endpoint to the schedule with possibly new parameters denoted by a
687 * different endpoint descriptor in usb_host_endpoint.
688 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
689 * not allowed.
690 *
691 * The USB core will not allow URBs to be queued to an endpoint that is being
692 * disabled, so there's no need for mutual exclusion to protect
693 * the xhci->devs[slot_id] structure.
694 */
695int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
696 struct usb_host_endpoint *ep)
697{
698 struct xhci_hcd *xhci;
699 struct xhci_device_control *in_ctx;
700 unsigned int last_ctx;
701 unsigned int ep_index;
702 struct xhci_ep_ctx *ep_ctx;
703 u32 drop_flag;
704 u32 new_add_flags, new_drop_flags, new_slot_info;
705 int ret;
706
707 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
708 if (ret <= 0)
709 return ret;
710 xhci = hcd_to_xhci(hcd);
711 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
712
713 drop_flag = xhci_get_endpoint_flag(&ep->desc);
714 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
715 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
716 __func__, drop_flag);
717 return 0;
718 }
719
720 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
721 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
722 __func__);
723 return -EINVAL;
724 }
725
726 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
727 ep_index = xhci_get_endpoint_index(&ep->desc);
728 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
729 /* If the HC already knows the endpoint is disabled,
730 * or the HCD has noted it is disabled, ignore this request
731 */
732 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
733 in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
734 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
735 __func__, ep);
736 return 0;
737 }
738
739 in_ctx->drop_flags |= drop_flag;
740 new_drop_flags = in_ctx->drop_flags;
741
742 in_ctx->add_flags = ~drop_flag;
743 new_add_flags = in_ctx->add_flags;
744
745 last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
746 /* Update the last valid endpoint context, if we deleted the last one */
747 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
748 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
749 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
750 }
751 new_slot_info = in_ctx->slot.dev_info;
752
753 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
754
755 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
756 (unsigned int) ep->desc.bEndpointAddress,
757 udev->slot_id,
758 (unsigned int) new_drop_flags,
759 (unsigned int) new_add_flags,
760 (unsigned int) new_slot_info);
761 return 0;
762}
763
764/* Add an endpoint to a new possible bandwidth configuration for this device.
765 * Only one call to this function is allowed per endpoint before
766 * check_bandwidth() or reset_bandwidth() must be called.
767 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
768 * add the endpoint to the schedule with possibly new parameters denoted by a
769 * different endpoint descriptor in usb_host_endpoint.
770 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
771 * not allowed.
772 *
773 * The USB core will not allow URBs to be queued to an endpoint until the
774 * configuration or alt setting is installed in the device, so there's no need
775 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
776 */
777int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
778 struct usb_host_endpoint *ep)
779{
780 struct xhci_hcd *xhci;
781 struct xhci_device_control *in_ctx;
782 unsigned int ep_index;
783 struct xhci_ep_ctx *ep_ctx;
784 u32 added_ctxs;
785 unsigned int last_ctx;
786 u32 new_add_flags, new_drop_flags, new_slot_info;
787 int ret = 0;
788
789 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
790 if (ret <= 0)
791 return ret;
792 xhci = hcd_to_xhci(hcd);
793
794 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
795 last_ctx = xhci_last_valid_endpoint(added_ctxs);
796 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
797 /* FIXME when we have to issue an evaluate endpoint command to
798 * deal with ep0 max packet size changing once we get the
799 * descriptors
800 */
801 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
802 __func__, added_ctxs);
803 return 0;
804 }
805
806 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
807 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
808 __func__);
809 return -EINVAL;
810 }
811
812 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
813 ep_index = xhci_get_endpoint_index(&ep->desc);
814 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
815 /* If the HCD has already noted the endpoint is enabled,
816 * ignore this request.
817 */
818 if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
819 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
820 __func__, ep);
821 return 0;
822 }
823
824 /*
825 * Configuration and alternate setting changes must be done in
826 * process context, not interrupt context (or so documenation
827 * for usb_set_interface() and usb_set_configuration() claim).
828 */
829 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
830 udev, ep, GFP_KERNEL) < 0) {
831 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
832 __func__, ep->desc.bEndpointAddress);
833 return -ENOMEM;
834 }
835
836 in_ctx->add_flags |= added_ctxs;
837 new_add_flags = in_ctx->add_flags;
838
839 /* If xhci_endpoint_disable() was called for this endpoint, but the
840 * xHC hasn't been notified yet through the check_bandwidth() call,
841 * this re-adds a new state for the endpoint from the new endpoint
842 * descriptors. We must drop and re-add this endpoint, so we leave the
843 * drop flags alone.
844 */
845 new_drop_flags = in_ctx->drop_flags;
846
847 /* Update the last valid endpoint context, if we just added one past */
848 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
849 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
850 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
851 }
852 new_slot_info = in_ctx->slot.dev_info;
853
854 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
855 (unsigned int) ep->desc.bEndpointAddress,
856 udev->slot_id,
857 (unsigned int) new_drop_flags,
858 (unsigned int) new_add_flags,
859 (unsigned int) new_slot_info);
860 return 0;
861}
862
863static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
864{
865 struct xhci_ep_ctx *ep_ctx;
866 int i;
867
868 /* When a device's add flag and drop flag are zero, any subsequent
869 * configure endpoint command will leave that endpoint's state
870 * untouched. Make sure we don't leave any old state in the input
871 * endpoint contexts.
872 */
873 virt_dev->in_ctx->drop_flags = 0;
874 virt_dev->in_ctx->add_flags = 0;
875 virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
876 /* Endpoint 0 is always valid */
877 virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
878 for (i = 1; i < 31; ++i) {
879 ep_ctx = &virt_dev->in_ctx->ep[i];
880 ep_ctx->ep_info = 0;
881 ep_ctx->ep_info2 = 0;
882 ep_ctx->deq[0] = 0;
883 ep_ctx->deq[1] = 0;
884 ep_ctx->tx_info = 0;
885 }
886}
887
888/* Called after one or more calls to xhci_add_endpoint() or
889 * xhci_drop_endpoint(). If this call fails, the USB core is expected
890 * to call xhci_reset_bandwidth().
891 *
892 * Since we are in the middle of changing either configuration or
893 * installing a new alt setting, the USB core won't allow URBs to be
894 * enqueued for any endpoint on the old config or interface. Nothing
895 * else should be touching the xhci->devs[slot_id] structure, so we
896 * don't need to take the xhci->lock for manipulating that.
897 */
898int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
899{
900 int i;
901 int ret = 0;
902 int timeleft;
903 unsigned long flags;
904 struct xhci_hcd *xhci;
905 struct xhci_virt_device *virt_dev;
906
907 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
908 if (ret <= 0)
909 return ret;
910 xhci = hcd_to_xhci(hcd);
911
912 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
913 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
914 __func__);
915 return -EINVAL;
916 }
917 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
918 virt_dev = xhci->devs[udev->slot_id];
919
920 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
921 virt_dev->in_ctx->add_flags |= SLOT_FLAG;
922 virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
923 virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
924 virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
925 xhci_dbg(xhci, "New Input Control Context:\n");
926 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
927 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
928
929 spin_lock_irqsave(&xhci->lock, flags);
930 ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx_dma,
931 udev->slot_id);
932 if (ret < 0) {
933 spin_unlock_irqrestore(&xhci->lock, flags);
934 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
935 return -ENOMEM;
936 }
937 xhci_ring_cmd_db(xhci);
938 spin_unlock_irqrestore(&xhci->lock, flags);
939
940 /* Wait for the configure endpoint command to complete */
941 timeleft = wait_for_completion_interruptible_timeout(
942 &virt_dev->cmd_completion,
943 USB_CTRL_SET_TIMEOUT);
944 if (timeleft <= 0) {
945 xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
946 timeleft == 0 ? "Timeout" : "Signal");
947 /* FIXME cancel the configure endpoint command */
948 return -ETIME;
949 }
950
951 switch (virt_dev->cmd_status) {
952 case COMP_ENOMEM:
953 dev_warn(&udev->dev, "Not enough host controller resources "
954 "for new device state.\n");
955 ret = -ENOMEM;
956 /* FIXME: can we allocate more resources for the HC? */
957 break;
958 case COMP_BW_ERR:
959 dev_warn(&udev->dev, "Not enough bandwidth "
960 "for new device state.\n");
961 ret = -ENOSPC;
962 /* FIXME: can we go back to the old state? */
963 break;
964 case COMP_TRB_ERR:
965 /* the HCD set up something wrong */
966 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
967 "and endpoint is not disabled.\n");
968 ret = -EINVAL;
969 break;
970 case COMP_SUCCESS:
971 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
972 break;
973 default:
974 xhci_err(xhci, "ERROR: unexpected command completion "
975 "code 0x%x.\n", virt_dev->cmd_status);
976 ret = -EINVAL;
977 break;
978 }
979 if (ret) {
980 /* Callee should call reset_bandwidth() */
981 return ret;
982 }
983
984 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
985 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
986 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
987
988 xhci_zero_in_ctx(virt_dev);
989 /* Free any old rings */
990 for (i = 1; i < 31; ++i) {
991 if (virt_dev->new_ep_rings[i]) {
992 xhci_ring_free(xhci, virt_dev->ep_rings[i]);
993 virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
994 virt_dev->new_ep_rings[i] = NULL;
995 }
996 }
997
998 return ret;
999}
1000
1001void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1002{
1003 struct xhci_hcd *xhci;
1004 struct xhci_virt_device *virt_dev;
1005 int i, ret;
1006
1007 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1008 if (ret <= 0)
1009 return;
1010 xhci = hcd_to_xhci(hcd);
1011
1012 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1013 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1014 __func__);
1015 return;
1016 }
1017 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1018 virt_dev = xhci->devs[udev->slot_id];
1019 /* Free any rings allocated for added endpoints */
1020 for (i = 0; i < 31; ++i) {
1021 if (virt_dev->new_ep_rings[i]) {
1022 xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
1023 virt_dev->new_ep_rings[i] = NULL;
1024 }
1025 }
1026 xhci_zero_in_ctx(virt_dev);
1027}
1028
1029/*
1030 * At this point, the struct usb_device is about to go away, the device has
1031 * disconnected, and all traffic has been stopped and the endpoints have been
1032 * disabled. Free any HC data structures associated with that device.
1033 */
1034void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1035{
1036 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1037 unsigned long flags;
1038
1039 if (udev->slot_id == 0)
1040 return;
1041
1042 spin_lock_irqsave(&xhci->lock, flags);
1043 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
1044 spin_unlock_irqrestore(&xhci->lock, flags);
1045 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1046 return;
1047 }
1048 xhci_ring_cmd_db(xhci);
1049 spin_unlock_irqrestore(&xhci->lock, flags);
1050 /*
1051 * Event command completion handler will free any data structures
1052 * associated with the slot. XXX Can free sleep?
1053 */
1054}
1055
1056/*
1057 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
1058 * timed out, or allocating memory failed. Returns 1 on success.
1059 */
1060int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1061{
1062 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1063 unsigned long flags;
1064 int timeleft;
1065 int ret;
1066
1067 spin_lock_irqsave(&xhci->lock, flags);
1068 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
1069 if (ret) {
1070 spin_unlock_irqrestore(&xhci->lock, flags);
1071 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1072 return 0;
1073 }
1074 xhci_ring_cmd_db(xhci);
1075 spin_unlock_irqrestore(&xhci->lock, flags);
1076
1077 /* XXX: how much time for xHC slot assignment? */
1078 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1079 USB_CTRL_SET_TIMEOUT);
1080 if (timeleft <= 0) {
1081 xhci_warn(xhci, "%s while waiting for a slot\n",
1082 timeleft == 0 ? "Timeout" : "Signal");
1083 /* FIXME cancel the enable slot request */
1084 return 0;
1085 }
1086
1087 if (!xhci->slot_id) {
1088 xhci_err(xhci, "Error while assigning device slot ID\n");
1089 return 0;
1090 }
1091 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
1092 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1093 /* Disable slot, if we can do it without mem alloc */
1094 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
1095 spin_lock_irqsave(&xhci->lock, flags);
1096 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1097 xhci_ring_cmd_db(xhci);
1098 spin_unlock_irqrestore(&xhci->lock, flags);
1099 return 0;
1100 }
1101 udev->slot_id = xhci->slot_id;
1102 /* Is this a LS or FS device under a HS hub? */
1103 /* Hub or peripherial? */
1104 return 1;
1105}
1106
1107/*
1108 * Issue an Address Device command (which will issue a SetAddress request to
1109 * the device).
1110 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1111 * we should only issue and wait on one address command at the same time.
1112 *
1113 * We add one to the device address issued by the hardware because the USB core
1114 * uses address 1 for the root hubs (even though they're not really devices).
1115 */
1116int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1117{
1118 unsigned long flags;
1119 int timeleft;
1120 struct xhci_virt_device *virt_dev;
1121 int ret = 0;
1122 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1123 u32 temp;
1124
1125 if (!udev->slot_id) {
1126 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1127 return -EINVAL;
1128 }
1129
1130 virt_dev = xhci->devs[udev->slot_id];
1131
1132 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1133 if (!udev->config)
1134 xhci_setup_addressable_virt_dev(xhci, udev);
1135 /* Otherwise, assume the core has the device configured how it wants */
1136
1137 spin_lock_irqsave(&xhci->lock, flags);
1138 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx_dma,
1139 udev->slot_id);
1140 if (ret) {
1141 spin_unlock_irqrestore(&xhci->lock, flags);
1142 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1143 return ret;
1144 }
1145 xhci_ring_cmd_db(xhci);
1146 spin_unlock_irqrestore(&xhci->lock, flags);
1147
1148 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1149 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1150 USB_CTRL_SET_TIMEOUT);
1151 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1152 * the SetAddress() "recovery interval" required by USB and aborting the
1153 * command on a timeout.
1154 */
1155 if (timeleft <= 0) {
1156 xhci_warn(xhci, "%s while waiting for a slot\n",
1157 timeleft == 0 ? "Timeout" : "Signal");
1158 /* FIXME cancel the address device command */
1159 return -ETIME;
1160 }
1161
1162 switch (virt_dev->cmd_status) {
1163 case COMP_CTX_STATE:
1164 case COMP_EBADSLT:
1165 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1166 udev->slot_id);
1167 ret = -EINVAL;
1168 break;
1169 case COMP_TX_ERR:
1170 dev_warn(&udev->dev, "Device not responding to set address.\n");
1171 ret = -EPROTO;
1172 break;
1173 case COMP_SUCCESS:
1174 xhci_dbg(xhci, "Successful Address Device command\n");
1175 break;
1176 default:
1177 xhci_err(xhci, "ERROR: unexpected command completion "
1178 "code 0x%x.\n", virt_dev->cmd_status);
1179 ret = -EINVAL;
1180 break;
1181 }
1182 if (ret) {
1183 return ret;
1184 }
1185 temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
1186 xhci_dbg(xhci, "Op regs DCBAA ptr[0] = %#08x\n", temp);
1187 temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[1]);
1188 xhci_dbg(xhci, "Op regs DCBAA ptr[1] = %#08x\n", temp);
1189 xhci_dbg(xhci, "Slot ID %d dcbaa entry[0] @%p = %#08x\n",
1190 udev->slot_id,
1191 &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id],
1192 xhci->dcbaa->dev_context_ptrs[2*udev->slot_id]);
1193 xhci_dbg(xhci, "Slot ID %d dcbaa entry[1] @%p = %#08x\n",
1194 udev->slot_id,
1195 &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1],
1196 xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1]);
1197 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
1198 (unsigned long long)virt_dev->out_ctx_dma);
1199 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1200 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
1201 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1202 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
1203 /*
1204 * USB core uses address 1 for the roothubs, so we add one to the
1205 * address given back to us by the HC.
1206 */
1207 udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
1208 /* Zero the input context control for later use */
1209 virt_dev->in_ctx->add_flags = 0;
1210 virt_dev->in_ctx->drop_flags = 0;
1211 /* Mirror flags in the output context for future ep enable/disable */
1212 virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
1213 virt_dev->out_ctx->drop_flags = 0;
1214
1215 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1216 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1217 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1218
1219 return 0;
1220}
1221
1222int xhci_get_frame(struct usb_hcd *hcd)
1223{
1224 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1225 /* EHCI mods by the periodic size. Why? */
1226 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1227}
1228
1229MODULE_DESCRIPTION(DRIVER_DESC);
1230MODULE_AUTHOR(DRIVER_AUTHOR);
1231MODULE_LICENSE("GPL");
1232
1233static int __init xhci_hcd_init(void)
1234{
1235#ifdef CONFIG_PCI
1236 int retval = 0;
1237
1238 retval = xhci_register_pci();
1239
1240 if (retval < 0) {
1241 printk(KERN_DEBUG "Problem registering PCI driver.");
1242 return retval;
1243 }
1244#endif
1245 /*
1246 * Check the compiler generated sizes of structures that must be laid
1247 * out in specific ways for hardware access.
1248 */
1249 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1250 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
1251 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
1252 /* xhci_device_control has eight fields, and also
1253 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
1254 */
1255 BUILD_BUG_ON(sizeof(struct xhci_device_control) != (8+8+8*31)*32/8);
1256 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
1257 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
1258 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
1259 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
1260 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
1261 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
1262 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
1263 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1264 return 0;
1265}
1266module_init(xhci_hcd_init);
1267
1268static void __exit xhci_hcd_cleanup(void)
1269{
1270#ifdef CONFIG_PCI
1271 xhci_unregister_pci();
1272#endif
1273}
1274module_exit(xhci_hcd_cleanup);
diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
new file mode 100644
index 000000000000..eac5b53aa9e7
--- /dev/null
+++ b/drivers/usb/host/xhci-hub.c
@@ -0,0 +1,308 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <asm/unaligned.h>
24
25#include "xhci.h"
26
27static void xhci_hub_descriptor(struct xhci_hcd *xhci,
28 struct usb_hub_descriptor *desc)
29{
30 int ports;
31 u16 temp;
32
33 ports = HCS_MAX_PORTS(xhci->hcs_params1);
34
35 /* USB 3.0 hubs have a different descriptor, but we fake this for now */
36 desc->bDescriptorType = 0x29;
37 desc->bPwrOn2PwrGood = 10; /* xhci section 5.4.9 says 20ms max */
38 desc->bHubContrCurrent = 0;
39
40 desc->bNbrPorts = ports;
41 temp = 1 + (ports / 8);
42 desc->bDescLength = 7 + 2 * temp;
43
44 /* Why does core/hcd.h define bitmap? It's just confusing. */
45 memset(&desc->DeviceRemovable[0], 0, temp);
46 memset(&desc->DeviceRemovable[temp], 0xff, temp);
47
48 /* Ugh, these should be #defines, FIXME */
49 /* Using table 11-13 in USB 2.0 spec. */
50 temp = 0;
51 /* Bits 1:0 - support port power switching, or power always on */
52 if (HCC_PPC(xhci->hcc_params))
53 temp |= 0x0001;
54 else
55 temp |= 0x0002;
56 /* Bit 2 - root hubs are not part of a compound device */
57 /* Bits 4:3 - individual port over current protection */
58 temp |= 0x0008;
59 /* Bits 6:5 - no TTs in root ports */
60 /* Bit 7 - no port indicators */
61 desc->wHubCharacteristics = (__force __u16) cpu_to_le16(temp);
62}
63
64static unsigned int xhci_port_speed(unsigned int port_status)
65{
66 if (DEV_LOWSPEED(port_status))
67 return 1 << USB_PORT_FEAT_LOWSPEED;
68 if (DEV_HIGHSPEED(port_status))
69 return 1 << USB_PORT_FEAT_HIGHSPEED;
70 if (DEV_SUPERSPEED(port_status))
71 return 1 << USB_PORT_FEAT_SUPERSPEED;
72 /*
73 * FIXME: Yes, we should check for full speed, but the core uses that as
74 * a default in portspeed() in usb/core/hub.c (which is the only place
75 * USB_PORT_FEAT_*SPEED is used).
76 */
77 return 0;
78}
79
80/*
81 * These bits are Read Only (RO) and should be saved and written to the
82 * registers: 0, 3, 10:13, 30
83 * connect status, over-current status, port speed, and device removable.
84 * connect status and port speed are also sticky - meaning they're in
85 * the AUX well and they aren't changed by a hot, warm, or cold reset.
86 */
87#define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
88/*
89 * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
90 * bits 5:8, 9, 14:15, 25:27
91 * link state, port power, port indicator state, "wake on" enable state
92 */
93#define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
94/*
95 * These bits are RW; writing a 1 sets the bit, writing a 0 has no effect:
96 * bit 4 (port reset)
97 */
98#define XHCI_PORT_RW1S ((1<<4))
99/*
100 * These bits are RW; writing a 1 clears the bit, writing a 0 has no effect:
101 * bits 1, 17, 18, 19, 20, 21, 22, 23
102 * port enable/disable, and
103 * change bits: connect, PED, warm port reset changed (reserved zero for USB 2.0 ports),
104 * over-current, reset, link state, and L1 change
105 */
106#define XHCI_PORT_RW1CS ((1<<1) | (0x7f<<17))
107/*
108 * Bit 16 is RW, and writing a '1' to it causes the link state control to be
109 * latched in
110 */
111#define XHCI_PORT_RW ((1<<16))
112/*
113 * These bits are Reserved Zero (RsvdZ) and zero should be written to them:
114 * bits 2, 24, 28:31
115 */
116#define XHCI_PORT_RZ ((1<<2) | (1<<24) | (0xf<<28))
117
118/*
119 * Given a port state, this function returns a value that would result in the
120 * port being in the same state, if the value was written to the port status
121 * control register.
122 * Save Read Only (RO) bits and save read/write bits where
123 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
124 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
125 */
126static u32 xhci_port_state_to_neutral(u32 state)
127{
128 /* Save read-only status and port state */
129 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
130}
131
132int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
133 u16 wIndex, char *buf, u16 wLength)
134{
135 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
136 int ports;
137 unsigned long flags;
138 u32 temp, status;
139 int retval = 0;
140 u32 __iomem *addr;
141 char *port_change_bit;
142
143 ports = HCS_MAX_PORTS(xhci->hcs_params1);
144
145 spin_lock_irqsave(&xhci->lock, flags);
146 switch (typeReq) {
147 case GetHubStatus:
148 /* No power source, over-current reported per port */
149 memset(buf, 0, 4);
150 break;
151 case GetHubDescriptor:
152 xhci_hub_descriptor(xhci, (struct usb_hub_descriptor *) buf);
153 break;
154 case GetPortStatus:
155 if (!wIndex || wIndex > ports)
156 goto error;
157 wIndex--;
158 status = 0;
159 addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
160 temp = xhci_readl(xhci, addr);
161 xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", wIndex, temp);
162
163 /* wPortChange bits */
164 if (temp & PORT_CSC)
165 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
166 if (temp & PORT_PEC)
167 status |= 1 << USB_PORT_FEAT_C_ENABLE;
168 if ((temp & PORT_OCC))
169 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
170 /*
171 * FIXME ignoring suspend, reset, and USB 2.1/3.0 specific
172 * changes
173 */
174 if (temp & PORT_CONNECT) {
175 status |= 1 << USB_PORT_FEAT_CONNECTION;
176 status |= xhci_port_speed(temp);
177 }
178 if (temp & PORT_PE)
179 status |= 1 << USB_PORT_FEAT_ENABLE;
180 if (temp & PORT_OC)
181 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
182 if (temp & PORT_RESET)
183 status |= 1 << USB_PORT_FEAT_RESET;
184 if (temp & PORT_POWER)
185 status |= 1 << USB_PORT_FEAT_POWER;
186 xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
187 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
188 break;
189 case SetPortFeature:
190 wIndex &= 0xff;
191 if (!wIndex || wIndex > ports)
192 goto error;
193 wIndex--;
194 addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(wIndex & 0xff);
195 temp = xhci_readl(xhci, addr);
196 temp = xhci_port_state_to_neutral(temp);
197 switch (wValue) {
198 case USB_PORT_FEAT_POWER:
199 /*
200 * Turn on ports, even if there isn't per-port switching.
201 * HC will report connect events even before this is set.
202 * However, khubd will ignore the roothub events until
203 * the roothub is registered.
204 */
205 xhci_writel(xhci, temp | PORT_POWER, addr);
206
207 temp = xhci_readl(xhci, addr);
208 xhci_dbg(xhci, "set port power, actual port %d status = 0x%x\n", wIndex, temp);
209 break;
210 case USB_PORT_FEAT_RESET:
211 temp = (temp | PORT_RESET);
212 xhci_writel(xhci, temp, addr);
213
214 temp = xhci_readl(xhci, addr);
215 xhci_dbg(xhci, "set port reset, actual port %d status = 0x%x\n", wIndex, temp);
216 break;
217 default:
218 goto error;
219 }
220 temp = xhci_readl(xhci, addr); /* unblock any posted writes */
221 break;
222 case ClearPortFeature:
223 if (!wIndex || wIndex > ports)
224 goto error;
225 wIndex--;
226 addr = &xhci->op_regs->port_status_base +
227 NUM_PORT_REGS*(wIndex & 0xff);
228 temp = xhci_readl(xhci, addr);
229 temp = xhci_port_state_to_neutral(temp);
230 switch (wValue) {
231 case USB_PORT_FEAT_C_RESET:
232 status = PORT_RC;
233 port_change_bit = "reset";
234 break;
235 case USB_PORT_FEAT_C_CONNECTION:
236 status = PORT_CSC;
237 port_change_bit = "connect";
238 break;
239 case USB_PORT_FEAT_C_OVER_CURRENT:
240 status = PORT_OCC;
241 port_change_bit = "over-current";
242 break;
243 default:
244 goto error;
245 }
246 /* Change bits are all write 1 to clear */
247 xhci_writel(xhci, temp | status, addr);
248 temp = xhci_readl(xhci, addr);
249 xhci_dbg(xhci, "clear port %s change, actual port %d status = 0x%x\n",
250 port_change_bit, wIndex, temp);
251 temp = xhci_readl(xhci, addr); /* unblock any posted writes */
252 break;
253 default:
254error:
255 /* "stall" on error */
256 retval = -EPIPE;
257 }
258 spin_unlock_irqrestore(&xhci->lock, flags);
259 return retval;
260}
261
262/*
263 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
264 * Ports are 0-indexed from the HCD point of view,
265 * and 1-indexed from the USB core pointer of view.
266 * xHCI instances can have up to 127 ports, so FIXME if you see more than 15.
267 *
268 * Note that the status change bits will be cleared as soon as a port status
269 * change event is generated, so we use the saved status from that event.
270 */
271int xhci_hub_status_data(struct usb_hcd *hcd, char *buf)
272{
273 unsigned long flags;
274 u32 temp, status;
275 int i, retval;
276 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
277 int ports;
278 u32 __iomem *addr;
279
280 ports = HCS_MAX_PORTS(xhci->hcs_params1);
281
282 /* Initial status is no changes */
283 buf[0] = 0;
284 status = 0;
285 if (ports > 7) {
286 buf[1] = 0;
287 retval = 2;
288 } else {
289 retval = 1;
290 }
291
292 spin_lock_irqsave(&xhci->lock, flags);
293 /* For each port, did anything change? If so, set that bit in buf. */
294 for (i = 0; i < ports; i++) {
295 addr = &xhci->op_regs->port_status_base +
296 NUM_PORT_REGS*i;
297 temp = xhci_readl(xhci, addr);
298 if (temp & (PORT_CSC | PORT_PEC | PORT_OCC)) {
299 if (i < 7)
300 buf[0] |= 1 << (i + 1);
301 else
302 buf[1] |= 1 << (i - 7);
303 status = 1;
304 }
305 }
306 spin_unlock_irqrestore(&xhci->lock, flags);
307 return status ? retval : 0;
308}
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
new file mode 100644
index 000000000000..c8a72de1c508
--- /dev/null
+++ b/drivers/usb/host/xhci-mem.c
@@ -0,0 +1,769 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/usb.h>
24#include <linux/pci.h>
25#include <linux/dmapool.h>
26
27#include "xhci.h"
28
29/*
30 * Allocates a generic ring segment from the ring pool, sets the dma address,
31 * initializes the segment to zero, and sets the private next pointer to NULL.
32 *
33 * Section 4.11.1.1:
34 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
35 */
36static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flags)
37{
38 struct xhci_segment *seg;
39 dma_addr_t dma;
40
41 seg = kzalloc(sizeof *seg, flags);
42 if (!seg)
43 return 0;
44 xhci_dbg(xhci, "Allocating priv segment structure at %p\n", seg);
45
46 seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
47 if (!seg->trbs) {
48 kfree(seg);
49 return 0;
50 }
51 xhci_dbg(xhci, "// Allocating segment at %p (virtual) 0x%llx (DMA)\n",
52 seg->trbs, (unsigned long long)dma);
53
54 memset(seg->trbs, 0, SEGMENT_SIZE);
55 seg->dma = dma;
56 seg->next = NULL;
57
58 return seg;
59}
60
61static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
62{
63 if (!seg)
64 return;
65 if (seg->trbs) {
66 xhci_dbg(xhci, "Freeing DMA segment at %p (virtual) 0x%llx (DMA)\n",
67 seg->trbs, (unsigned long long)seg->dma);
68 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
69 seg->trbs = NULL;
70 }
71 xhci_dbg(xhci, "Freeing priv segment structure at %p\n", seg);
72 kfree(seg);
73}
74
75/*
76 * Make the prev segment point to the next segment.
77 *
78 * Change the last TRB in the prev segment to be a Link TRB which points to the
79 * DMA address of the next segment. The caller needs to set any Link TRB
80 * related flags, such as End TRB, Toggle Cycle, and no snoop.
81 */
82static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
83 struct xhci_segment *next, bool link_trbs)
84{
85 u32 val;
86
87 if (!prev || !next)
88 return;
89 prev->next = next;
90 if (link_trbs) {
91 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr[0] = next->dma;
92
93 /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
94 val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
95 val &= ~TRB_TYPE_BITMASK;
96 val |= TRB_TYPE(TRB_LINK);
97 prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
98 }
99 xhci_dbg(xhci, "Linking segment 0x%llx to segment 0x%llx (DMA)\n",
100 (unsigned long long)prev->dma,
101 (unsigned long long)next->dma);
102}
103
104/* XXX: Do we need the hcd structure in all these functions? */
105void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
106{
107 struct xhci_segment *seg;
108 struct xhci_segment *first_seg;
109
110 if (!ring || !ring->first_seg)
111 return;
112 first_seg = ring->first_seg;
113 seg = first_seg->next;
114 xhci_dbg(xhci, "Freeing ring at %p\n", ring);
115 while (seg != first_seg) {
116 struct xhci_segment *next = seg->next;
117 xhci_segment_free(xhci, seg);
118 seg = next;
119 }
120 xhci_segment_free(xhci, first_seg);
121 ring->first_seg = NULL;
122 kfree(ring);
123}
124
125/**
126 * Create a new ring with zero or more segments.
127 *
128 * Link each segment together into a ring.
129 * Set the end flag and the cycle toggle bit on the last segment.
130 * See section 4.9.1 and figures 15 and 16.
131 */
132static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
133 unsigned int num_segs, bool link_trbs, gfp_t flags)
134{
135 struct xhci_ring *ring;
136 struct xhci_segment *prev;
137
138 ring = kzalloc(sizeof *(ring), flags);
139 xhci_dbg(xhci, "Allocating ring at %p\n", ring);
140 if (!ring)
141 return 0;
142
143 INIT_LIST_HEAD(&ring->td_list);
144 INIT_LIST_HEAD(&ring->cancelled_td_list);
145 if (num_segs == 0)
146 return ring;
147
148 ring->first_seg = xhci_segment_alloc(xhci, flags);
149 if (!ring->first_seg)
150 goto fail;
151 num_segs--;
152
153 prev = ring->first_seg;
154 while (num_segs > 0) {
155 struct xhci_segment *next;
156
157 next = xhci_segment_alloc(xhci, flags);
158 if (!next)
159 goto fail;
160 xhci_link_segments(xhci, prev, next, link_trbs);
161
162 prev = next;
163 num_segs--;
164 }
165 xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
166
167 if (link_trbs) {
168 /* See section 4.9.2.1 and 6.4.4.1 */
169 prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
170 xhci_dbg(xhci, "Wrote link toggle flag to"
171 " segment %p (virtual), 0x%llx (DMA)\n",
172 prev, (unsigned long long)prev->dma);
173 }
174 /* The ring is empty, so the enqueue pointer == dequeue pointer */
175 ring->enqueue = ring->first_seg->trbs;
176 ring->enq_seg = ring->first_seg;
177 ring->dequeue = ring->enqueue;
178 ring->deq_seg = ring->first_seg;
179 /* The ring is initialized to 0. The producer must write 1 to the cycle
180 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
181 * compare CCS to the cycle bit to check ownership, so CCS = 1.
182 */
183 ring->cycle_state = 1;
184
185 return ring;
186
187fail:
188 xhci_ring_free(xhci, ring);
189 return 0;
190}
191
192/* All the xhci_tds in the ring's TD list should be freed at this point */
193void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
194{
195 struct xhci_virt_device *dev;
196 int i;
197
198 /* Slot ID 0 is reserved */
199 if (slot_id == 0 || !xhci->devs[slot_id])
200 return;
201
202 dev = xhci->devs[slot_id];
203 xhci->dcbaa->dev_context_ptrs[2*slot_id] = 0;
204 xhci->dcbaa->dev_context_ptrs[2*slot_id + 1] = 0;
205 if (!dev)
206 return;
207
208 for (i = 0; i < 31; ++i)
209 if (dev->ep_rings[i])
210 xhci_ring_free(xhci, dev->ep_rings[i]);
211
212 if (dev->in_ctx)
213 dma_pool_free(xhci->device_pool,
214 dev->in_ctx, dev->in_ctx_dma);
215 if (dev->out_ctx)
216 dma_pool_free(xhci->device_pool,
217 dev->out_ctx, dev->out_ctx_dma);
218 kfree(xhci->devs[slot_id]);
219 xhci->devs[slot_id] = 0;
220}
221
222int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
223 struct usb_device *udev, gfp_t flags)
224{
225 dma_addr_t dma;
226 struct xhci_virt_device *dev;
227
228 /* Slot ID 0 is reserved */
229 if (slot_id == 0 || xhci->devs[slot_id]) {
230 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
231 return 0;
232 }
233
234 xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
235 if (!xhci->devs[slot_id])
236 return 0;
237 dev = xhci->devs[slot_id];
238
239 /* Allocate the (output) device context that will be used in the HC */
240 dev->out_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
241 if (!dev->out_ctx)
242 goto fail;
243 dev->out_ctx_dma = dma;
244 xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
245 (unsigned long long)dma);
246 memset(dev->out_ctx, 0, sizeof(*dev->out_ctx));
247
248 /* Allocate the (input) device context for address device command */
249 dev->in_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
250 if (!dev->in_ctx)
251 goto fail;
252 dev->in_ctx_dma = dma;
253 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
254 (unsigned long long)dma);
255 memset(dev->in_ctx, 0, sizeof(*dev->in_ctx));
256
257 /* Allocate endpoint 0 ring */
258 dev->ep_rings[0] = xhci_ring_alloc(xhci, 1, true, flags);
259 if (!dev->ep_rings[0])
260 goto fail;
261
262 init_completion(&dev->cmd_completion);
263
264 /*
265 * Point to output device context in dcbaa; skip the output control
266 * context, which is eight 32 bit fields (or 32 bytes long)
267 */
268 xhci->dcbaa->dev_context_ptrs[2*slot_id] =
269 (u32) dev->out_ctx_dma + (32);
270 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
271 slot_id,
272 &xhci->dcbaa->dev_context_ptrs[2*slot_id],
273 (unsigned long long)dev->out_ctx_dma);
274 xhci->dcbaa->dev_context_ptrs[2*slot_id + 1] = 0;
275
276 return 1;
277fail:
278 xhci_free_virt_device(xhci, slot_id);
279 return 0;
280}
281
282/* Setup an xHCI virtual device for a Set Address command */
283int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
284{
285 struct xhci_virt_device *dev;
286 struct xhci_ep_ctx *ep0_ctx;
287 struct usb_device *top_dev;
288
289 dev = xhci->devs[udev->slot_id];
290 /* Slot ID 0 is reserved */
291 if (udev->slot_id == 0 || !dev) {
292 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
293 udev->slot_id);
294 return -EINVAL;
295 }
296 ep0_ctx = &dev->in_ctx->ep[0];
297
298 /* 2) New slot context and endpoint 0 context are valid*/
299 dev->in_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
300
301 /* 3) Only the control endpoint is valid - one endpoint context */
302 dev->in_ctx->slot.dev_info |= LAST_CTX(1);
303
304 switch (udev->speed) {
305 case USB_SPEED_SUPER:
306 dev->in_ctx->slot.dev_info |= (u32) udev->route;
307 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_SS;
308 break;
309 case USB_SPEED_HIGH:
310 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_HS;
311 break;
312 case USB_SPEED_FULL:
313 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_FS;
314 break;
315 case USB_SPEED_LOW:
316 dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_LS;
317 break;
318 case USB_SPEED_VARIABLE:
319 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
320 return -EINVAL;
321 break;
322 default:
323 /* Speed was set earlier, this shouldn't happen. */
324 BUG();
325 }
326 /* Find the root hub port this device is under */
327 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
328 top_dev = top_dev->parent)
329 /* Found device below root hub */;
330 dev->in_ctx->slot.dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
331 xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
332
333 /* Is this a LS/FS device under a HS hub? */
334 /*
335 * FIXME: I don't think this is right, where does the TT info for the
336 * roothub or parent hub come from?
337 */
338 if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
339 udev->tt) {
340 dev->in_ctx->slot.tt_info = udev->tt->hub->slot_id;
341 dev->in_ctx->slot.tt_info |= udev->ttport << 8;
342 }
343 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
344 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
345
346 /* Step 4 - ring already allocated */
347 /* Step 5 */
348 ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
349 /*
350 * See section 4.3 bullet 6:
351 * The default Max Packet size for ep0 is "8 bytes for a USB2
352 * LS/FS/HS device or 512 bytes for a USB3 SS device"
353 * XXX: Not sure about wireless USB devices.
354 */
355 if (udev->speed == USB_SPEED_SUPER)
356 ep0_ctx->ep_info2 |= MAX_PACKET(512);
357 else
358 ep0_ctx->ep_info2 |= MAX_PACKET(8);
359 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
360 ep0_ctx->ep_info2 |= MAX_BURST(0);
361 ep0_ctx->ep_info2 |= ERROR_COUNT(3);
362
363 ep0_ctx->deq[0] =
364 dev->ep_rings[0]->first_seg->dma;
365 ep0_ctx->deq[0] |= dev->ep_rings[0]->cycle_state;
366 ep0_ctx->deq[1] = 0;
367
368 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
369
370 return 0;
371}
372
373/* Return the polling or NAK interval.
374 *
375 * The polling interval is expressed in "microframes". If xHCI's Interval field
376 * is set to N, it will service the endpoint every 2^(Interval)*125us.
377 *
378 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
379 * is set to 0.
380 */
381static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
382 struct usb_host_endpoint *ep)
383{
384 unsigned int interval = 0;
385
386 switch (udev->speed) {
387 case USB_SPEED_HIGH:
388 /* Max NAK rate */
389 if (usb_endpoint_xfer_control(&ep->desc) ||
390 usb_endpoint_xfer_bulk(&ep->desc))
391 interval = ep->desc.bInterval;
392 /* Fall through - SS and HS isoc/int have same decoding */
393 case USB_SPEED_SUPER:
394 if (usb_endpoint_xfer_int(&ep->desc) ||
395 usb_endpoint_xfer_isoc(&ep->desc)) {
396 if (ep->desc.bInterval == 0)
397 interval = 0;
398 else
399 interval = ep->desc.bInterval - 1;
400 if (interval > 15)
401 interval = 15;
402 if (interval != ep->desc.bInterval + 1)
403 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
404 ep->desc.bEndpointAddress, 1 << interval);
405 }
406 break;
407 /* Convert bInterval (in 1-255 frames) to microframes and round down to
408 * nearest power of 2.
409 */
410 case USB_SPEED_FULL:
411 case USB_SPEED_LOW:
412 if (usb_endpoint_xfer_int(&ep->desc) ||
413 usb_endpoint_xfer_isoc(&ep->desc)) {
414 interval = fls(8*ep->desc.bInterval) - 1;
415 if (interval > 10)
416 interval = 10;
417 if (interval < 3)
418 interval = 3;
419 if ((1 << interval) != 8*ep->desc.bInterval)
420 dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
421 ep->desc.bEndpointAddress, 1 << interval);
422 }
423 break;
424 default:
425 BUG();
426 }
427 return EP_INTERVAL(interval);
428}
429
430static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
431 struct usb_host_endpoint *ep)
432{
433 int in;
434 u32 type;
435
436 in = usb_endpoint_dir_in(&ep->desc);
437 if (usb_endpoint_xfer_control(&ep->desc)) {
438 type = EP_TYPE(CTRL_EP);
439 } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
440 if (in)
441 type = EP_TYPE(BULK_IN_EP);
442 else
443 type = EP_TYPE(BULK_OUT_EP);
444 } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
445 if (in)
446 type = EP_TYPE(ISOC_IN_EP);
447 else
448 type = EP_TYPE(ISOC_OUT_EP);
449 } else if (usb_endpoint_xfer_int(&ep->desc)) {
450 if (in)
451 type = EP_TYPE(INT_IN_EP);
452 else
453 type = EP_TYPE(INT_OUT_EP);
454 } else {
455 BUG();
456 }
457 return type;
458}
459
460int xhci_endpoint_init(struct xhci_hcd *xhci,
461 struct xhci_virt_device *virt_dev,
462 struct usb_device *udev,
463 struct usb_host_endpoint *ep,
464 gfp_t mem_flags)
465{
466 unsigned int ep_index;
467 struct xhci_ep_ctx *ep_ctx;
468 struct xhci_ring *ep_ring;
469 unsigned int max_packet;
470 unsigned int max_burst;
471
472 ep_index = xhci_get_endpoint_index(&ep->desc);
473 ep_ctx = &virt_dev->in_ctx->ep[ep_index];
474
475 /* Set up the endpoint ring */
476 virt_dev->new_ep_rings[ep_index] = xhci_ring_alloc(xhci, 1, true, mem_flags);
477 if (!virt_dev->new_ep_rings[ep_index])
478 return -ENOMEM;
479 ep_ring = virt_dev->new_ep_rings[ep_index];
480 ep_ctx->deq[0] = ep_ring->first_seg->dma | ep_ring->cycle_state;
481 ep_ctx->deq[1] = 0;
482
483 ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
484
485 /* FIXME dig Mult and streams info out of ep companion desc */
486
487 /* Allow 3 retries for everything but isoc */
488 if (!usb_endpoint_xfer_isoc(&ep->desc))
489 ep_ctx->ep_info2 = ERROR_COUNT(3);
490 else
491 ep_ctx->ep_info2 = ERROR_COUNT(0);
492
493 ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
494
495 /* Set the max packet size and max burst */
496 switch (udev->speed) {
497 case USB_SPEED_SUPER:
498 max_packet = ep->desc.wMaxPacketSize;
499 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
500 /* dig out max burst from ep companion desc */
501 max_packet = ep->ss_ep_comp->desc.bMaxBurst;
502 ep_ctx->ep_info2 |= MAX_BURST(max_packet);
503 break;
504 case USB_SPEED_HIGH:
505 /* bits 11:12 specify the number of additional transaction
506 * opportunities per microframe (USB 2.0, section 9.6.6)
507 */
508 if (usb_endpoint_xfer_isoc(&ep->desc) ||
509 usb_endpoint_xfer_int(&ep->desc)) {
510 max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
511 ep_ctx->ep_info2 |= MAX_BURST(max_burst);
512 }
513 /* Fall through */
514 case USB_SPEED_FULL:
515 case USB_SPEED_LOW:
516 max_packet = ep->desc.wMaxPacketSize & 0x3ff;
517 ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
518 break;
519 default:
520 BUG();
521 }
522 /* FIXME Debug endpoint context */
523 return 0;
524}
525
526void xhci_endpoint_zero(struct xhci_hcd *xhci,
527 struct xhci_virt_device *virt_dev,
528 struct usb_host_endpoint *ep)
529{
530 unsigned int ep_index;
531 struct xhci_ep_ctx *ep_ctx;
532
533 ep_index = xhci_get_endpoint_index(&ep->desc);
534 ep_ctx = &virt_dev->in_ctx->ep[ep_index];
535
536 ep_ctx->ep_info = 0;
537 ep_ctx->ep_info2 = 0;
538 ep_ctx->deq[0] = 0;
539 ep_ctx->deq[1] = 0;
540 ep_ctx->tx_info = 0;
541 /* Don't free the endpoint ring until the set interface or configuration
542 * request succeeds.
543 */
544}
545
546void xhci_mem_cleanup(struct xhci_hcd *xhci)
547{
548 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
549 int size;
550 int i;
551
552 /* Free the Event Ring Segment Table and the actual Event Ring */
553 xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
554 xhci_writel(xhci, 0, &xhci->ir_set->erst_base[0]);
555 xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
556 xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[0]);
557 xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
558 size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
559 if (xhci->erst.entries)
560 pci_free_consistent(pdev, size,
561 xhci->erst.entries, xhci->erst.erst_dma_addr);
562 xhci->erst.entries = NULL;
563 xhci_dbg(xhci, "Freed ERST\n");
564 if (xhci->event_ring)
565 xhci_ring_free(xhci, xhci->event_ring);
566 xhci->event_ring = NULL;
567 xhci_dbg(xhci, "Freed event ring\n");
568
569 xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[0]);
570 xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[1]);
571 if (xhci->cmd_ring)
572 xhci_ring_free(xhci, xhci->cmd_ring);
573 xhci->cmd_ring = NULL;
574 xhci_dbg(xhci, "Freed command ring\n");
575
576 for (i = 1; i < MAX_HC_SLOTS; ++i)
577 xhci_free_virt_device(xhci, i);
578
579 if (xhci->segment_pool)
580 dma_pool_destroy(xhci->segment_pool);
581 xhci->segment_pool = NULL;
582 xhci_dbg(xhci, "Freed segment pool\n");
583
584 if (xhci->device_pool)
585 dma_pool_destroy(xhci->device_pool);
586 xhci->device_pool = NULL;
587 xhci_dbg(xhci, "Freed device context pool\n");
588
589 xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[0]);
590 xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[1]);
591 if (xhci->dcbaa)
592 pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
593 xhci->dcbaa, xhci->dcbaa->dma);
594 xhci->dcbaa = NULL;
595
596 xhci->page_size = 0;
597 xhci->page_shift = 0;
598}
599
600int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
601{
602 dma_addr_t dma;
603 struct device *dev = xhci_to_hcd(xhci)->self.controller;
604 unsigned int val, val2;
605 struct xhci_segment *seg;
606 u32 page_size;
607 int i;
608
609 page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
610 xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
611 for (i = 0; i < 16; i++) {
612 if ((0x1 & page_size) != 0)
613 break;
614 page_size = page_size >> 1;
615 }
616 if (i < 16)
617 xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
618 else
619 xhci_warn(xhci, "WARN: no supported page size\n");
620 /* Use 4K pages, since that's common and the minimum the HC supports */
621 xhci->page_shift = 12;
622 xhci->page_size = 1 << xhci->page_shift;
623 xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
624
625 /*
626 * Program the Number of Device Slots Enabled field in the CONFIG
627 * register with the max value of slots the HC can handle.
628 */
629 val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
630 xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
631 (unsigned int) val);
632 val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
633 val |= (val2 & ~HCS_SLOTS_MASK);
634 xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
635 (unsigned int) val);
636 xhci_writel(xhci, val, &xhci->op_regs->config_reg);
637
638 /*
639 * Section 5.4.8 - doorbell array must be
640 * "physically contiguous and 64-byte (cache line) aligned".
641 */
642 xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
643 sizeof(*xhci->dcbaa), &dma);
644 if (!xhci->dcbaa)
645 goto fail;
646 memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
647 xhci->dcbaa->dma = dma;
648 xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
649 (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
650 xhci_writel(xhci, dma, &xhci->op_regs->dcbaa_ptr[0]);
651 xhci_writel(xhci, (u32) 0, &xhci->op_regs->dcbaa_ptr[1]);
652
653 /*
654 * Initialize the ring segment pool. The ring must be a contiguous
655 * structure comprised of TRBs. The TRBs must be 16 byte aligned,
656 * however, the command ring segment needs 64-byte aligned segments,
657 * so we pick the greater alignment need.
658 */
659 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
660 SEGMENT_SIZE, 64, xhci->page_size);
661 /* See Table 46 and Note on Figure 55 */
662 /* FIXME support 64-byte contexts */
663 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
664 sizeof(struct xhci_device_control),
665 64, xhci->page_size);
666 if (!xhci->segment_pool || !xhci->device_pool)
667 goto fail;
668
669 /* Set up the command ring to have one segments for now. */
670 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
671 if (!xhci->cmd_ring)
672 goto fail;
673 xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
674 xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
675 (unsigned long long)xhci->cmd_ring->first_seg->dma);
676
677 /* Set the address in the Command Ring Control register */
678 val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[0]);
679 val = (val & ~CMD_RING_ADDR_MASK) |
680 (xhci->cmd_ring->first_seg->dma & CMD_RING_ADDR_MASK) |
681 xhci->cmd_ring->cycle_state;
682 xhci_dbg(xhci, "// Setting command ring address low bits to 0x%x\n", val);
683 xhci_writel(xhci, val, &xhci->op_regs->cmd_ring[0]);
684 xhci_dbg(xhci, "// Setting command ring address high bits to 0x0\n");
685 xhci_writel(xhci, (u32) 0, &xhci->op_regs->cmd_ring[1]);
686 xhci_dbg_cmd_ptrs(xhci);
687
688 val = xhci_readl(xhci, &xhci->cap_regs->db_off);
689 val &= DBOFF_MASK;
690 xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
691 " from cap regs base addr\n", val);
692 xhci->dba = (void *) xhci->cap_regs + val;
693 xhci_dbg_regs(xhci);
694 xhci_print_run_regs(xhci);
695 /* Set ir_set to interrupt register set 0 */
696 xhci->ir_set = (void *) xhci->run_regs->ir_set;
697
698 /*
699 * Event ring setup: Allocate a normal ring, but also setup
700 * the event ring segment table (ERST). Section 4.9.3.
701 */
702 xhci_dbg(xhci, "// Allocating event ring\n");
703 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
704 if (!xhci->event_ring)
705 goto fail;
706
707 xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
708 sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
709 if (!xhci->erst.entries)
710 goto fail;
711 xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
712 (unsigned long long)dma);
713
714 memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
715 xhci->erst.num_entries = ERST_NUM_SEGS;
716 xhci->erst.erst_dma_addr = dma;
717 xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
718 xhci->erst.num_entries,
719 xhci->erst.entries,
720 (unsigned long long)xhci->erst.erst_dma_addr);
721
722 /* set ring base address and size for each segment table entry */
723 for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
724 struct xhci_erst_entry *entry = &xhci->erst.entries[val];
725 entry->seg_addr[0] = seg->dma;
726 entry->seg_addr[1] = 0;
727 entry->seg_size = TRBS_PER_SEGMENT;
728 entry->rsvd = 0;
729 seg = seg->next;
730 }
731
732 /* set ERST count with the number of entries in the segment table */
733 val = xhci_readl(xhci, &xhci->ir_set->erst_size);
734 val &= ERST_SIZE_MASK;
735 val |= ERST_NUM_SEGS;
736 xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
737 val);
738 xhci_writel(xhci, val, &xhci->ir_set->erst_size);
739
740 xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
741 /* set the segment table base address */
742 xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
743 (unsigned long long)xhci->erst.erst_dma_addr);
744 val = xhci_readl(xhci, &xhci->ir_set->erst_base[0]);
745 val &= ERST_PTR_MASK;
746 val |= (xhci->erst.erst_dma_addr & ~ERST_PTR_MASK);
747 xhci_writel(xhci, val, &xhci->ir_set->erst_base[0]);
748 xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
749
750 /* Set the event ring dequeue address */
751 xhci_set_hc_event_deq(xhci);
752 xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
753 xhci_print_ir_set(xhci, xhci->ir_set, 0);
754
755 /*
756 * XXX: Might need to set the Interrupter Moderation Register to
757 * something other than the default (~1ms minimum between interrupts).
758 * See section 5.5.1.2.
759 */
760 init_completion(&xhci->addr_dev);
761 for (i = 0; i < MAX_HC_SLOTS; ++i)
762 xhci->devs[i] = 0;
763
764 return 0;
765fail:
766 xhci_warn(xhci, "Couldn't initialize memory\n");
767 xhci_mem_cleanup(xhci);
768 return -ENOMEM;
769}
diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c
new file mode 100644
index 000000000000..1462709e26c0
--- /dev/null
+++ b/drivers/usb/host/xhci-pci.c
@@ -0,0 +1,166 @@
1/*
2 * xHCI host controller driver PCI Bus Glue.
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/pci.h>
24
25#include "xhci.h"
26
27static const char hcd_name[] = "xhci_hcd";
28
29/* called after powerup, by probe or system-pm "wakeup" */
30static int xhci_pci_reinit(struct xhci_hcd *xhci, struct pci_dev *pdev)
31{
32 /*
33 * TODO: Implement finding debug ports later.
34 * TODO: see if there are any quirks that need to be added to handle
35 * new extended capabilities.
36 */
37
38 /* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
39 if (!pci_set_mwi(pdev))
40 xhci_dbg(xhci, "MWI active\n");
41
42 xhci_dbg(xhci, "Finished xhci_pci_reinit\n");
43 return 0;
44}
45
46/* called during probe() after chip reset completes */
47static int xhci_pci_setup(struct usb_hcd *hcd)
48{
49 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
50 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
51 int retval;
52
53 xhci->cap_regs = hcd->regs;
54 xhci->op_regs = hcd->regs +
55 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
56 xhci->run_regs = hcd->regs +
57 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
58 /* Cache read-only capability registers */
59 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
60 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
61 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
62 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
63 xhci_print_registers(xhci);
64
65 /* Make sure the HC is halted. */
66 retval = xhci_halt(xhci);
67 if (retval)
68 return retval;
69
70 xhci_dbg(xhci, "Resetting HCD\n");
71 /* Reset the internal HC memory state and registers. */
72 retval = xhci_reset(xhci);
73 if (retval)
74 return retval;
75 xhci_dbg(xhci, "Reset complete\n");
76
77 xhci_dbg(xhci, "Calling HCD init\n");
78 /* Initialize HCD and host controller data structures. */
79 retval = xhci_init(hcd);
80 if (retval)
81 return retval;
82 xhci_dbg(xhci, "Called HCD init\n");
83
84 pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
85 xhci_dbg(xhci, "Got SBRN %u\n", (unsigned int) xhci->sbrn);
86
87 /* Find any debug ports */
88 return xhci_pci_reinit(xhci, pdev);
89}
90
91static const struct hc_driver xhci_pci_hc_driver = {
92 .description = hcd_name,
93 .product_desc = "xHCI Host Controller",
94 .hcd_priv_size = sizeof(struct xhci_hcd),
95
96 /*
97 * generic hardware linkage
98 */
99 .irq = xhci_irq,
100 .flags = HCD_MEMORY | HCD_USB3,
101
102 /*
103 * basic lifecycle operations
104 */
105 .reset = xhci_pci_setup,
106 .start = xhci_run,
107 /* suspend and resume implemented later */
108 .stop = xhci_stop,
109 .shutdown = xhci_shutdown,
110
111 /*
112 * managing i/o requests and associated device resources
113 */
114 .urb_enqueue = xhci_urb_enqueue,
115 .urb_dequeue = xhci_urb_dequeue,
116 .alloc_dev = xhci_alloc_dev,
117 .free_dev = xhci_free_dev,
118 .add_endpoint = xhci_add_endpoint,
119 .drop_endpoint = xhci_drop_endpoint,
120 .check_bandwidth = xhci_check_bandwidth,
121 .reset_bandwidth = xhci_reset_bandwidth,
122 .address_device = xhci_address_device,
123
124 /*
125 * scheduling support
126 */
127 .get_frame_number = xhci_get_frame,
128
129 /* Root hub support */
130 .hub_control = xhci_hub_control,
131 .hub_status_data = xhci_hub_status_data,
132};
133
134/*-------------------------------------------------------------------------*/
135
136/* PCI driver selection metadata; PCI hotplugging uses this */
137static const struct pci_device_id pci_ids[] = { {
138 /* handle any USB 3.0 xHCI controller */
139 PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
140 .driver_data = (unsigned long) &xhci_pci_hc_driver,
141 },
142 { /* end: all zeroes */ }
143};
144MODULE_DEVICE_TABLE(pci, pci_ids);
145
146/* pci driver glue; this is a "new style" PCI driver module */
147static struct pci_driver xhci_pci_driver = {
148 .name = (char *) hcd_name,
149 .id_table = pci_ids,
150
151 .probe = usb_hcd_pci_probe,
152 .remove = usb_hcd_pci_remove,
153 /* suspend and resume implemented later */
154
155 .shutdown = usb_hcd_pci_shutdown,
156};
157
158int xhci_register_pci()
159{
160 return pci_register_driver(&xhci_pci_driver);
161}
162
163void xhci_unregister_pci()
164{
165 pci_unregister_driver(&xhci_pci_driver);
166}
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
new file mode 100644
index 000000000000..02d81985c454
--- /dev/null
+++ b/drivers/usb/host/xhci-ring.c
@@ -0,0 +1,1648 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Ring initialization rules:
25 * 1. Each segment is initialized to zero, except for link TRBs.
26 * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
27 * Consumer Cycle State (CCS), depending on ring function.
28 * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
29 *
30 * Ring behavior rules:
31 * 1. A ring is empty if enqueue == dequeue. This means there will always be at
32 * least one free TRB in the ring. This is useful if you want to turn that
33 * into a link TRB and expand the ring.
34 * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
35 * link TRB, then load the pointer with the address in the link TRB. If the
36 * link TRB had its toggle bit set, you may need to update the ring cycle
37 * state (see cycle bit rules). You may have to do this multiple times
38 * until you reach a non-link TRB.
39 * 3. A ring is full if enqueue++ (for the definition of increment above)
40 * equals the dequeue pointer.
41 *
42 * Cycle bit rules:
43 * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
44 * in a link TRB, it must toggle the ring cycle state.
45 * 2. When a producer increments an enqueue pointer and encounters a toggle bit
46 * in a link TRB, it must toggle the ring cycle state.
47 *
48 * Producer rules:
49 * 1. Check if ring is full before you enqueue.
50 * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
51 * Update enqueue pointer between each write (which may update the ring
52 * cycle state).
53 * 3. Notify consumer. If SW is producer, it rings the doorbell for command
54 * and endpoint rings. If HC is the producer for the event ring,
55 * and it generates an interrupt according to interrupt modulation rules.
56 *
57 * Consumer rules:
58 * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
59 * the TRB is owned by the consumer.
60 * 2. Update dequeue pointer (which may update the ring cycle state) and
61 * continue processing TRBs until you reach a TRB which is not owned by you.
62 * 3. Notify the producer. SW is the consumer for the event ring, and it
63 * updates event ring dequeue pointer. HC is the consumer for the command and
64 * endpoint rings; it generates events on the event ring for these.
65 */
66
67#include <linux/scatterlist.h>
68#include "xhci.h"
69
70/*
71 * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
72 * address of the TRB.
73 */
74dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
75 union xhci_trb *trb)
76{
77 unsigned long segment_offset;
78
79 if (!seg || !trb || trb < seg->trbs)
80 return 0;
81 /* offset in TRBs */
82 segment_offset = trb - seg->trbs;
83 if (segment_offset > TRBS_PER_SEGMENT)
84 return 0;
85 return seg->dma + (segment_offset * sizeof(*trb));
86}
87
88/* Does this link TRB point to the first segment in a ring,
89 * or was the previous TRB the last TRB on the last segment in the ERST?
90 */
91static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
92 struct xhci_segment *seg, union xhci_trb *trb)
93{
94 if (ring == xhci->event_ring)
95 return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
96 (seg->next == xhci->event_ring->first_seg);
97 else
98 return trb->link.control & LINK_TOGGLE;
99}
100
101/* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
102 * segment? I.e. would the updated event TRB pointer step off the end of the
103 * event seg?
104 */
105static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
106 struct xhci_segment *seg, union xhci_trb *trb)
107{
108 if (ring == xhci->event_ring)
109 return trb == &seg->trbs[TRBS_PER_SEGMENT];
110 else
111 return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
112}
113
114/* Updates trb to point to the next TRB in the ring, and updates seg if the next
115 * TRB is in a new segment. This does not skip over link TRBs, and it does not
116 * effect the ring dequeue or enqueue pointers.
117 */
118static void next_trb(struct xhci_hcd *xhci,
119 struct xhci_ring *ring,
120 struct xhci_segment **seg,
121 union xhci_trb **trb)
122{
123 if (last_trb(xhci, ring, *seg, *trb)) {
124 *seg = (*seg)->next;
125 *trb = ((*seg)->trbs);
126 } else {
127 *trb = (*trb)++;
128 }
129}
130
131/*
132 * See Cycle bit rules. SW is the consumer for the event ring only.
133 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
134 */
135static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
136{
137 union xhci_trb *next = ++(ring->dequeue);
138
139 ring->deq_updates++;
140 /* Update the dequeue pointer further if that was a link TRB or we're at
141 * the end of an event ring segment (which doesn't have link TRBS)
142 */
143 while (last_trb(xhci, ring, ring->deq_seg, next)) {
144 if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
145 ring->cycle_state = (ring->cycle_state ? 0 : 1);
146 if (!in_interrupt())
147 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
148 ring,
149 (unsigned int) ring->cycle_state);
150 }
151 ring->deq_seg = ring->deq_seg->next;
152 ring->dequeue = ring->deq_seg->trbs;
153 next = ring->dequeue;
154 }
155}
156
157/*
158 * See Cycle bit rules. SW is the consumer for the event ring only.
159 * Don't make a ring full of link TRBs. That would be dumb and this would loop.
160 *
161 * If we've just enqueued a TRB that is in the middle of a TD (meaning the
162 * chain bit is set), then set the chain bit in all the following link TRBs.
163 * If we've enqueued the last TRB in a TD, make sure the following link TRBs
164 * have their chain bit cleared (so that each Link TRB is a separate TD).
165 *
166 * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
167 * set, but other sections talk about dealing with the chain bit set.
168 * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB.
169 */
170static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
171{
172 u32 chain;
173 union xhci_trb *next;
174
175 chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
176 next = ++(ring->enqueue);
177
178 ring->enq_updates++;
179 /* Update the dequeue pointer further if that was a link TRB or we're at
180 * the end of an event ring segment (which doesn't have link TRBS)
181 */
182 while (last_trb(xhci, ring, ring->enq_seg, next)) {
183 if (!consumer) {
184 if (ring != xhci->event_ring) {
185 next->link.control &= ~TRB_CHAIN;
186 next->link.control |= chain;
187 /* Give this link TRB to the hardware */
188 wmb();
189 if (next->link.control & TRB_CYCLE)
190 next->link.control &= (u32) ~TRB_CYCLE;
191 else
192 next->link.control |= (u32) TRB_CYCLE;
193 }
194 /* Toggle the cycle bit after the last ring segment. */
195 if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
196 ring->cycle_state = (ring->cycle_state ? 0 : 1);
197 if (!in_interrupt())
198 xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
199 ring,
200 (unsigned int) ring->cycle_state);
201 }
202 }
203 ring->enq_seg = ring->enq_seg->next;
204 ring->enqueue = ring->enq_seg->trbs;
205 next = ring->enqueue;
206 }
207}
208
209/*
210 * Check to see if there's room to enqueue num_trbs on the ring. See rules
211 * above.
212 * FIXME: this would be simpler and faster if we just kept track of the number
213 * of free TRBs in a ring.
214 */
215static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
216 unsigned int num_trbs)
217{
218 int i;
219 union xhci_trb *enq = ring->enqueue;
220 struct xhci_segment *enq_seg = ring->enq_seg;
221
222 /* Check if ring is empty */
223 if (enq == ring->dequeue)
224 return 1;
225 /* Make sure there's an extra empty TRB available */
226 for (i = 0; i <= num_trbs; ++i) {
227 if (enq == ring->dequeue)
228 return 0;
229 enq++;
230 while (last_trb(xhci, ring, enq_seg, enq)) {
231 enq_seg = enq_seg->next;
232 enq = enq_seg->trbs;
233 }
234 }
235 return 1;
236}
237
238void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
239{
240 u32 temp;
241 dma_addr_t deq;
242
243 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
244 xhci->event_ring->dequeue);
245 if (deq == 0 && !in_interrupt())
246 xhci_warn(xhci, "WARN something wrong with SW event ring "
247 "dequeue ptr.\n");
248 /* Update HC event ring dequeue pointer */
249 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
250 temp &= ERST_PTR_MASK;
251 if (!in_interrupt())
252 xhci_dbg(xhci, "// Write event ring dequeue pointer\n");
253 xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
254 xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp,
255 &xhci->ir_set->erst_dequeue[0]);
256}
257
258/* Ring the host controller doorbell after placing a command on the ring */
259void xhci_ring_cmd_db(struct xhci_hcd *xhci)
260{
261 u32 temp;
262
263 xhci_dbg(xhci, "// Ding dong!\n");
264 temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
265 xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
266 /* Flush PCI posted writes */
267 xhci_readl(xhci, &xhci->dba->doorbell[0]);
268}
269
270static void ring_ep_doorbell(struct xhci_hcd *xhci,
271 unsigned int slot_id,
272 unsigned int ep_index)
273{
274 struct xhci_ring *ep_ring;
275 u32 field;
276 __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
277
278 ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
279 /* Don't ring the doorbell for this endpoint if there are pending
280 * cancellations because the we don't want to interrupt processing.
281 */
282 if (!ep_ring->cancels_pending && !(ep_ring->state & SET_DEQ_PENDING)) {
283 field = xhci_readl(xhci, db_addr) & DB_MASK;
284 xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
285 /* Flush PCI posted writes - FIXME Matthew Wilcox says this
286 * isn't time-critical and we shouldn't make the CPU wait for
287 * the flush.
288 */
289 xhci_readl(xhci, db_addr);
290 }
291}
292
293/*
294 * Find the segment that trb is in. Start searching in start_seg.
295 * If we must move past a segment that has a link TRB with a toggle cycle state
296 * bit set, then we will toggle the value pointed at by cycle_state.
297 */
298static struct xhci_segment *find_trb_seg(
299 struct xhci_segment *start_seg,
300 union xhci_trb *trb, int *cycle_state)
301{
302 struct xhci_segment *cur_seg = start_seg;
303 struct xhci_generic_trb *generic_trb;
304
305 while (cur_seg->trbs > trb ||
306 &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
307 generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
308 if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
309 (generic_trb->field[3] & LINK_TOGGLE))
310 *cycle_state = ~(*cycle_state) & 0x1;
311 cur_seg = cur_seg->next;
312 if (cur_seg == start_seg)
313 /* Looped over the entire list. Oops! */
314 return 0;
315 }
316 return cur_seg;
317}
318
319struct dequeue_state {
320 struct xhci_segment *new_deq_seg;
321 union xhci_trb *new_deq_ptr;
322 int new_cycle_state;
323};
324
325/*
326 * Move the xHC's endpoint ring dequeue pointer past cur_td.
327 * Record the new state of the xHC's endpoint ring dequeue segment,
328 * dequeue pointer, and new consumer cycle state in state.
329 * Update our internal representation of the ring's dequeue pointer.
330 *
331 * We do this in three jumps:
332 * - First we update our new ring state to be the same as when the xHC stopped.
333 * - Then we traverse the ring to find the segment that contains
334 * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
335 * any link TRBs with the toggle cycle bit set.
336 * - Finally we move the dequeue state one TRB further, toggling the cycle bit
337 * if we've moved it past a link TRB with the toggle cycle bit set.
338 */
339static void find_new_dequeue_state(struct xhci_hcd *xhci,
340 unsigned int slot_id, unsigned int ep_index,
341 struct xhci_td *cur_td, struct dequeue_state *state)
342{
343 struct xhci_virt_device *dev = xhci->devs[slot_id];
344 struct xhci_ring *ep_ring = dev->ep_rings[ep_index];
345 struct xhci_generic_trb *trb;
346
347 state->new_cycle_state = 0;
348 state->new_deq_seg = find_trb_seg(cur_td->start_seg,
349 ep_ring->stopped_trb,
350 &state->new_cycle_state);
351 if (!state->new_deq_seg)
352 BUG();
353 /* Dig out the cycle state saved by the xHC during the stop ep cmd */
354 state->new_cycle_state = 0x1 & dev->out_ctx->ep[ep_index].deq[0];
355
356 state->new_deq_ptr = cur_td->last_trb;
357 state->new_deq_seg = find_trb_seg(state->new_deq_seg,
358 state->new_deq_ptr,
359 &state->new_cycle_state);
360 if (!state->new_deq_seg)
361 BUG();
362
363 trb = &state->new_deq_ptr->generic;
364 if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
365 (trb->field[3] & LINK_TOGGLE))
366 state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
367 next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
368
369 /* Don't update the ring cycle state for the producer (us). */
370 ep_ring->dequeue = state->new_deq_ptr;
371 ep_ring->deq_seg = state->new_deq_seg;
372}
373
374static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
375 struct xhci_td *cur_td)
376{
377 struct xhci_segment *cur_seg;
378 union xhci_trb *cur_trb;
379
380 for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
381 true;
382 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
383 if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
384 TRB_TYPE(TRB_LINK)) {
385 /* Unchain any chained Link TRBs, but
386 * leave the pointers intact.
387 */
388 cur_trb->generic.field[3] &= ~TRB_CHAIN;
389 xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
390 xhci_dbg(xhci, "Address = %p (0x%llx dma); "
391 "in seg %p (0x%llx dma)\n",
392 cur_trb,
393 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
394 cur_seg,
395 (unsigned long long)cur_seg->dma);
396 } else {
397 cur_trb->generic.field[0] = 0;
398 cur_trb->generic.field[1] = 0;
399 cur_trb->generic.field[2] = 0;
400 /* Preserve only the cycle bit of this TRB */
401 cur_trb->generic.field[3] &= TRB_CYCLE;
402 cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
403 xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
404 "in seg %p (0x%llx dma)\n",
405 cur_trb,
406 (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
407 cur_seg,
408 (unsigned long long)cur_seg->dma);
409 }
410 if (cur_trb == cur_td->last_trb)
411 break;
412 }
413}
414
415static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
416 unsigned int ep_index, struct xhci_segment *deq_seg,
417 union xhci_trb *deq_ptr, u32 cycle_state);
418
419/*
420 * When we get a command completion for a Stop Endpoint Command, we need to
421 * unlink any cancelled TDs from the ring. There are two ways to do that:
422 *
423 * 1. If the HW was in the middle of processing the TD that needs to be
424 * cancelled, then we must move the ring's dequeue pointer past the last TRB
425 * in the TD with a Set Dequeue Pointer Command.
426 * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
427 * bit cleared) so that the HW will skip over them.
428 */
429static void handle_stopped_endpoint(struct xhci_hcd *xhci,
430 union xhci_trb *trb)
431{
432 unsigned int slot_id;
433 unsigned int ep_index;
434 struct xhci_ring *ep_ring;
435 struct list_head *entry;
436 struct xhci_td *cur_td = 0;
437 struct xhci_td *last_unlinked_td;
438
439 struct dequeue_state deq_state;
440#ifdef CONFIG_USB_HCD_STAT
441 ktime_t stop_time = ktime_get();
442#endif
443
444 memset(&deq_state, 0, sizeof(deq_state));
445 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
446 ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
447 ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
448
449 if (list_empty(&ep_ring->cancelled_td_list))
450 return;
451
452 /* Fix up the ep ring first, so HW stops executing cancelled TDs.
453 * We have the xHCI lock, so nothing can modify this list until we drop
454 * it. We're also in the event handler, so we can't get re-interrupted
455 * if another Stop Endpoint command completes
456 */
457 list_for_each(entry, &ep_ring->cancelled_td_list) {
458 cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
459 xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
460 cur_td->first_trb,
461 (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
462 /*
463 * If we stopped on the TD we need to cancel, then we have to
464 * move the xHC endpoint ring dequeue pointer past this TD.
465 */
466 if (cur_td == ep_ring->stopped_td)
467 find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
468 &deq_state);
469 else
470 td_to_noop(xhci, ep_ring, cur_td);
471 /*
472 * The event handler won't see a completion for this TD anymore,
473 * so remove it from the endpoint ring's TD list. Keep it in
474 * the cancelled TD list for URB completion later.
475 */
476 list_del(&cur_td->td_list);
477 ep_ring->cancels_pending--;
478 }
479 last_unlinked_td = cur_td;
480
481 /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
482 if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
483 xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
484 "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
485 deq_state.new_deq_seg,
486 (unsigned long long)deq_state.new_deq_seg->dma,
487 deq_state.new_deq_ptr,
488 (unsigned long long)xhci_trb_virt_to_dma(deq_state.new_deq_seg, deq_state.new_deq_ptr),
489 deq_state.new_cycle_state);
490 queue_set_tr_deq(xhci, slot_id, ep_index,
491 deq_state.new_deq_seg,
492 deq_state.new_deq_ptr,
493 (u32) deq_state.new_cycle_state);
494 /* Stop the TD queueing code from ringing the doorbell until
495 * this command completes. The HC won't set the dequeue pointer
496 * if the ring is running, and ringing the doorbell starts the
497 * ring running.
498 */
499 ep_ring->state |= SET_DEQ_PENDING;
500 xhci_ring_cmd_db(xhci);
501 } else {
502 /* Otherwise just ring the doorbell to restart the ring */
503 ring_ep_doorbell(xhci, slot_id, ep_index);
504 }
505
506 /*
507 * Drop the lock and complete the URBs in the cancelled TD list.
508 * New TDs to be cancelled might be added to the end of the list before
509 * we can complete all the URBs for the TDs we already unlinked.
510 * So stop when we've completed the URB for the last TD we unlinked.
511 */
512 do {
513 cur_td = list_entry(ep_ring->cancelled_td_list.next,
514 struct xhci_td, cancelled_td_list);
515 list_del(&cur_td->cancelled_td_list);
516
517 /* Clean up the cancelled URB */
518#ifdef CONFIG_USB_HCD_STAT
519 hcd_stat_update(xhci->tp_stat, cur_td->urb->actual_length,
520 ktime_sub(stop_time, cur_td->start_time));
521#endif
522 cur_td->urb->hcpriv = NULL;
523 usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), cur_td->urb);
524
525 xhci_dbg(xhci, "Giveback cancelled URB %p\n", cur_td->urb);
526 spin_unlock(&xhci->lock);
527 /* Doesn't matter what we pass for status, since the core will
528 * just overwrite it (because the URB has been unlinked).
529 */
530 usb_hcd_giveback_urb(xhci_to_hcd(xhci), cur_td->urb, 0);
531 kfree(cur_td);
532
533 spin_lock(&xhci->lock);
534 } while (cur_td != last_unlinked_td);
535
536 /* Return to the event handler with xhci->lock re-acquired */
537}
538
539/*
540 * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
541 * we need to clear the set deq pending flag in the endpoint ring state, so that
542 * the TD queueing code can ring the doorbell again. We also need to ring the
543 * endpoint doorbell to restart the ring, but only if there aren't more
544 * cancellations pending.
545 */
546static void handle_set_deq_completion(struct xhci_hcd *xhci,
547 struct xhci_event_cmd *event,
548 union xhci_trb *trb)
549{
550 unsigned int slot_id;
551 unsigned int ep_index;
552 struct xhci_ring *ep_ring;
553 struct xhci_virt_device *dev;
554
555 slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
556 ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
557 dev = xhci->devs[slot_id];
558 ep_ring = dev->ep_rings[ep_index];
559
560 if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
561 unsigned int ep_state;
562 unsigned int slot_state;
563
564 switch (GET_COMP_CODE(event->status)) {
565 case COMP_TRB_ERR:
566 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
567 "of stream ID configuration\n");
568 break;
569 case COMP_CTX_STATE:
570 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
571 "to incorrect slot or ep state.\n");
572 ep_state = dev->out_ctx->ep[ep_index].ep_info;
573 ep_state &= EP_STATE_MASK;
574 slot_state = dev->out_ctx->slot.dev_state;
575 slot_state = GET_SLOT_STATE(slot_state);
576 xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
577 slot_state, ep_state);
578 break;
579 case COMP_EBADSLT:
580 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
581 "slot %u was not enabled.\n", slot_id);
582 break;
583 default:
584 xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
585 "completion code of %u.\n",
586 GET_COMP_CODE(event->status));
587 break;
588 }
589 /* OK what do we do now? The endpoint state is hosed, and we
590 * should never get to this point if the synchronization between
591 * queueing, and endpoint state are correct. This might happen
592 * if the device gets disconnected after we've finished
593 * cancelling URBs, which might not be an error...
594 */
595 } else {
596 xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq[0] = 0x%x, "
597 "deq[1] = 0x%x.\n",
598 dev->out_ctx->ep[ep_index].deq[0],
599 dev->out_ctx->ep[ep_index].deq[1]);
600 }
601
602 ep_ring->state &= ~SET_DEQ_PENDING;
603 ring_ep_doorbell(xhci, slot_id, ep_index);
604}
605
606
607static void handle_cmd_completion(struct xhci_hcd *xhci,
608 struct xhci_event_cmd *event)
609{
610 int slot_id = TRB_TO_SLOT_ID(event->flags);
611 u64 cmd_dma;
612 dma_addr_t cmd_dequeue_dma;
613
614 cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
615 cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
616 xhci->cmd_ring->dequeue);
617 /* Is the command ring deq ptr out of sync with the deq seg ptr? */
618 if (cmd_dequeue_dma == 0) {
619 xhci->error_bitmask |= 1 << 4;
620 return;
621 }
622 /* Does the DMA address match our internal dequeue pointer address? */
623 if (cmd_dma != (u64) cmd_dequeue_dma) {
624 xhci->error_bitmask |= 1 << 5;
625 return;
626 }
627 switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
628 case TRB_TYPE(TRB_ENABLE_SLOT):
629 if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
630 xhci->slot_id = slot_id;
631 else
632 xhci->slot_id = 0;
633 complete(&xhci->addr_dev);
634 break;
635 case TRB_TYPE(TRB_DISABLE_SLOT):
636 if (xhci->devs[slot_id])
637 xhci_free_virt_device(xhci, slot_id);
638 break;
639 case TRB_TYPE(TRB_CONFIG_EP):
640 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
641 complete(&xhci->devs[slot_id]->cmd_completion);
642 break;
643 case TRB_TYPE(TRB_ADDR_DEV):
644 xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
645 complete(&xhci->addr_dev);
646 break;
647 case TRB_TYPE(TRB_STOP_RING):
648 handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
649 break;
650 case TRB_TYPE(TRB_SET_DEQ):
651 handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
652 break;
653 case TRB_TYPE(TRB_CMD_NOOP):
654 ++xhci->noops_handled;
655 break;
656 default:
657 /* Skip over unknown commands on the event ring */
658 xhci->error_bitmask |= 1 << 6;
659 break;
660 }
661 inc_deq(xhci, xhci->cmd_ring, false);
662}
663
664static void handle_port_status(struct xhci_hcd *xhci,
665 union xhci_trb *event)
666{
667 u32 port_id;
668
669 /* Port status change events always have a successful completion code */
670 if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
671 xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
672 xhci->error_bitmask |= 1 << 8;
673 }
674 /* FIXME: core doesn't care about all port link state changes yet */
675 port_id = GET_PORT_ID(event->generic.field[0]);
676 xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
677
678 /* Update event ring dequeue pointer before dropping the lock */
679 inc_deq(xhci, xhci->event_ring, true);
680 xhci_set_hc_event_deq(xhci);
681
682 spin_unlock(&xhci->lock);
683 /* Pass this up to the core */
684 usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
685 spin_lock(&xhci->lock);
686}
687
688/*
689 * This TD is defined by the TRBs starting at start_trb in start_seg and ending
690 * at end_trb, which may be in another segment. If the suspect DMA address is a
691 * TRB in this TD, this function returns that TRB's segment. Otherwise it
692 * returns 0.
693 */
694static struct xhci_segment *trb_in_td(
695 struct xhci_segment *start_seg,
696 union xhci_trb *start_trb,
697 union xhci_trb *end_trb,
698 dma_addr_t suspect_dma)
699{
700 dma_addr_t start_dma;
701 dma_addr_t end_seg_dma;
702 dma_addr_t end_trb_dma;
703 struct xhci_segment *cur_seg;
704
705 start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
706 cur_seg = start_seg;
707
708 do {
709 /* We may get an event for a Link TRB in the middle of a TD */
710 end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
711 &start_seg->trbs[TRBS_PER_SEGMENT - 1]);
712 /* If the end TRB isn't in this segment, this is set to 0 */
713 end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
714
715 if (end_trb_dma > 0) {
716 /* The end TRB is in this segment, so suspect should be here */
717 if (start_dma <= end_trb_dma) {
718 if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
719 return cur_seg;
720 } else {
721 /* Case for one segment with
722 * a TD wrapped around to the top
723 */
724 if ((suspect_dma >= start_dma &&
725 suspect_dma <= end_seg_dma) ||
726 (suspect_dma >= cur_seg->dma &&
727 suspect_dma <= end_trb_dma))
728 return cur_seg;
729 }
730 return 0;
731 } else {
732 /* Might still be somewhere in this segment */
733 if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
734 return cur_seg;
735 }
736 cur_seg = cur_seg->next;
737 start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
738 } while (1);
739
740}
741
742/*
743 * If this function returns an error condition, it means it got a Transfer
744 * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
745 * At this point, the host controller is probably hosed and should be reset.
746 */
747static int handle_tx_event(struct xhci_hcd *xhci,
748 struct xhci_transfer_event *event)
749{
750 struct xhci_virt_device *xdev;
751 struct xhci_ring *ep_ring;
752 int ep_index;
753 struct xhci_td *td = 0;
754 dma_addr_t event_dma;
755 struct xhci_segment *event_seg;
756 union xhci_trb *event_trb;
757 struct urb *urb = 0;
758 int status = -EINPROGRESS;
759
760 xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)];
761 if (!xdev) {
762 xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
763 return -ENODEV;
764 }
765
766 /* Endpoint ID is 1 based, our index is zero based */
767 ep_index = TRB_TO_EP_ID(event->flags) - 1;
768 ep_ring = xdev->ep_rings[ep_index];
769 if (!ep_ring || (xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
770 xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
771 return -ENODEV;
772 }
773
774 event_dma = event->buffer[0];
775 if (event->buffer[1] != 0)
776 xhci_warn(xhci, "WARN ignoring upper 32-bits of 64-bit TRB dma address\n");
777
778 /* This TRB should be in the TD at the head of this ring's TD list */
779 if (list_empty(&ep_ring->td_list)) {
780 xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
781 TRB_TO_SLOT_ID(event->flags), ep_index);
782 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
783 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
784 xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
785 urb = NULL;
786 goto cleanup;
787 }
788 td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
789
790 /* Is this a TRB in the currently executing TD? */
791 event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
792 td->last_trb, event_dma);
793 if (!event_seg) {
794 /* HC is busted, give up! */
795 xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
796 return -ESHUTDOWN;
797 }
798 event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
799 xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
800 (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
801 xhci_dbg(xhci, "Offset 0x00 (buffer[0]) = 0x%x\n",
802 (unsigned int) event->buffer[0]);
803 xhci_dbg(xhci, "Offset 0x04 (buffer[0]) = 0x%x\n",
804 (unsigned int) event->buffer[1]);
805 xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
806 (unsigned int) event->transfer_len);
807 xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
808 (unsigned int) event->flags);
809
810 /* Look for common error cases */
811 switch (GET_COMP_CODE(event->transfer_len)) {
812 /* Skip codes that require special handling depending on
813 * transfer type
814 */
815 case COMP_SUCCESS:
816 case COMP_SHORT_TX:
817 break;
818 case COMP_STOP:
819 xhci_dbg(xhci, "Stopped on Transfer TRB\n");
820 break;
821 case COMP_STOP_INVAL:
822 xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
823 break;
824 case COMP_STALL:
825 xhci_warn(xhci, "WARN: Stalled endpoint\n");
826 status = -EPIPE;
827 break;
828 case COMP_TRB_ERR:
829 xhci_warn(xhci, "WARN: TRB error on endpoint\n");
830 status = -EILSEQ;
831 break;
832 case COMP_TX_ERR:
833 xhci_warn(xhci, "WARN: transfer error on endpoint\n");
834 status = -EPROTO;
835 break;
836 case COMP_DB_ERR:
837 xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
838 status = -ENOSR;
839 break;
840 default:
841 xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
842 urb = NULL;
843 goto cleanup;
844 }
845 /* Now update the urb's actual_length and give back to the core */
846 /* Was this a control transfer? */
847 if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
848 xhci_debug_trb(xhci, xhci->event_ring->dequeue);
849 switch (GET_COMP_CODE(event->transfer_len)) {
850 case COMP_SUCCESS:
851 if (event_trb == ep_ring->dequeue) {
852 xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
853 status = -ESHUTDOWN;
854 } else if (event_trb != td->last_trb) {
855 xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
856 status = -ESHUTDOWN;
857 } else {
858 xhci_dbg(xhci, "Successful control transfer!\n");
859 status = 0;
860 }
861 break;
862 case COMP_SHORT_TX:
863 xhci_warn(xhci, "WARN: short transfer on control ep\n");
864 status = -EREMOTEIO;
865 break;
866 default:
867 /* Others already handled above */
868 break;
869 }
870 /*
871 * Did we transfer any data, despite the errors that might have
872 * happened? I.e. did we get past the setup stage?
873 */
874 if (event_trb != ep_ring->dequeue) {
875 /* The event was for the status stage */
876 if (event_trb == td->last_trb) {
877 td->urb->actual_length =
878 td->urb->transfer_buffer_length;
879 } else {
880 /* Maybe the event was for the data stage? */
881 if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
882 /* We didn't stop on a link TRB in the middle */
883 td->urb->actual_length =
884 td->urb->transfer_buffer_length -
885 TRB_LEN(event->transfer_len);
886 }
887 }
888 } else {
889 switch (GET_COMP_CODE(event->transfer_len)) {
890 case COMP_SUCCESS:
891 /* Double check that the HW transferred everything. */
892 if (event_trb != td->last_trb) {
893 xhci_warn(xhci, "WARN Successful completion "
894 "on short TX\n");
895 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
896 status = -EREMOTEIO;
897 else
898 status = 0;
899 } else {
900 xhci_dbg(xhci, "Successful bulk transfer!\n");
901 status = 0;
902 }
903 break;
904 case COMP_SHORT_TX:
905 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
906 status = -EREMOTEIO;
907 else
908 status = 0;
909 break;
910 default:
911 /* Others already handled above */
912 break;
913 }
914 dev_dbg(&td->urb->dev->dev,
915 "ep %#x - asked for %d bytes, "
916 "%d bytes untransferred\n",
917 td->urb->ep->desc.bEndpointAddress,
918 td->urb->transfer_buffer_length,
919 TRB_LEN(event->transfer_len));
920 /* Fast path - was this the last TRB in the TD for this URB? */
921 if (event_trb == td->last_trb) {
922 if (TRB_LEN(event->transfer_len) != 0) {
923 td->urb->actual_length =
924 td->urb->transfer_buffer_length -
925 TRB_LEN(event->transfer_len);
926 if (td->urb->actual_length < 0) {
927 xhci_warn(xhci, "HC gave bad length "
928 "of %d bytes left\n",
929 TRB_LEN(event->transfer_len));
930 td->urb->actual_length = 0;
931 }
932 if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
933 status = -EREMOTEIO;
934 else
935 status = 0;
936 } else {
937 td->urb->actual_length = td->urb->transfer_buffer_length;
938 /* Ignore a short packet completion if the
939 * untransferred length was zero.
940 */
941 status = 0;
942 }
943 } else {
944 /* Slow path - walk the list, starting from the dequeue
945 * pointer, to get the actual length transferred.
946 */
947 union xhci_trb *cur_trb;
948 struct xhci_segment *cur_seg;
949
950 td->urb->actual_length = 0;
951 for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
952 cur_trb != event_trb;
953 next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
954 if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
955 TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
956 td->urb->actual_length +=
957 TRB_LEN(cur_trb->generic.field[2]);
958 }
959 /* If the ring didn't stop on a Link or No-op TRB, add
960 * in the actual bytes transferred from the Normal TRB
961 */
962 if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
963 td->urb->actual_length +=
964 TRB_LEN(cur_trb->generic.field[2]) -
965 TRB_LEN(event->transfer_len);
966 }
967 }
968 /* The Endpoint Stop Command completion will take care of
969 * any stopped TDs. A stopped TD may be restarted, so don't update the
970 * ring dequeue pointer or take this TD off any lists yet.
971 */
972 if (GET_COMP_CODE(event->transfer_len) == COMP_STOP_INVAL ||
973 GET_COMP_CODE(event->transfer_len) == COMP_STOP) {
974 ep_ring->stopped_td = td;
975 ep_ring->stopped_trb = event_trb;
976 } else {
977 /* Update ring dequeue pointer */
978 while (ep_ring->dequeue != td->last_trb)
979 inc_deq(xhci, ep_ring, false);
980 inc_deq(xhci, ep_ring, false);
981
982 /* Clean up the endpoint's TD list */
983 urb = td->urb;
984 list_del(&td->td_list);
985 /* Was this TD slated to be cancelled but completed anyway? */
986 if (!list_empty(&td->cancelled_td_list)) {
987 list_del(&td->cancelled_td_list);
988 ep_ring->cancels_pending--;
989 }
990 kfree(td);
991 urb->hcpriv = NULL;
992 }
993cleanup:
994 inc_deq(xhci, xhci->event_ring, true);
995 xhci_set_hc_event_deq(xhci);
996
997 /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
998 if (urb) {
999 usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
1000 spin_unlock(&xhci->lock);
1001 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
1002 spin_lock(&xhci->lock);
1003 }
1004 return 0;
1005}
1006
1007/*
1008 * This function handles all OS-owned events on the event ring. It may drop
1009 * xhci->lock between event processing (e.g. to pass up port status changes).
1010 */
1011void xhci_handle_event(struct xhci_hcd *xhci)
1012{
1013 union xhci_trb *event;
1014 int update_ptrs = 1;
1015 int ret;
1016
1017 if (!xhci->event_ring || !xhci->event_ring->dequeue) {
1018 xhci->error_bitmask |= 1 << 1;
1019 return;
1020 }
1021
1022 event = xhci->event_ring->dequeue;
1023 /* Does the HC or OS own the TRB? */
1024 if ((event->event_cmd.flags & TRB_CYCLE) !=
1025 xhci->event_ring->cycle_state) {
1026 xhci->error_bitmask |= 1 << 2;
1027 return;
1028 }
1029
1030 /* FIXME: Handle more event types. */
1031 switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
1032 case TRB_TYPE(TRB_COMPLETION):
1033 handle_cmd_completion(xhci, &event->event_cmd);
1034 break;
1035 case TRB_TYPE(TRB_PORT_STATUS):
1036 handle_port_status(xhci, event);
1037 update_ptrs = 0;
1038 break;
1039 case TRB_TYPE(TRB_TRANSFER):
1040 ret = handle_tx_event(xhci, &event->trans_event);
1041 if (ret < 0)
1042 xhci->error_bitmask |= 1 << 9;
1043 else
1044 update_ptrs = 0;
1045 break;
1046 default:
1047 xhci->error_bitmask |= 1 << 3;
1048 }
1049
1050 if (update_ptrs) {
1051 /* Update SW and HC event ring dequeue pointer */
1052 inc_deq(xhci, xhci->event_ring, true);
1053 xhci_set_hc_event_deq(xhci);
1054 }
1055 /* Are there more items on the event ring? */
1056 xhci_handle_event(xhci);
1057}
1058
1059/**** Endpoint Ring Operations ****/
1060
1061/*
1062 * Generic function for queueing a TRB on a ring.
1063 * The caller must have checked to make sure there's room on the ring.
1064 */
1065static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
1066 bool consumer,
1067 u32 field1, u32 field2, u32 field3, u32 field4)
1068{
1069 struct xhci_generic_trb *trb;
1070
1071 trb = &ring->enqueue->generic;
1072 trb->field[0] = field1;
1073 trb->field[1] = field2;
1074 trb->field[2] = field3;
1075 trb->field[3] = field4;
1076 inc_enq(xhci, ring, consumer);
1077}
1078
1079/*
1080 * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
1081 * FIXME allocate segments if the ring is full.
1082 */
1083static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
1084 u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
1085{
1086 /* Make sure the endpoint has been added to xHC schedule */
1087 xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
1088 switch (ep_state) {
1089 case EP_STATE_DISABLED:
1090 /*
1091 * USB core changed config/interfaces without notifying us,
1092 * or hardware is reporting the wrong state.
1093 */
1094 xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
1095 return -ENOENT;
1096 case EP_STATE_HALTED:
1097 case EP_STATE_ERROR:
1098 xhci_warn(xhci, "WARN waiting for halt or error on ep "
1099 "to be cleared\n");
1100 /* FIXME event handling code for error needs to clear it */
1101 /* XXX not sure if this should be -ENOENT or not */
1102 return -EINVAL;
1103 case EP_STATE_STOPPED:
1104 case EP_STATE_RUNNING:
1105 break;
1106 default:
1107 xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
1108 /*
1109 * FIXME issue Configure Endpoint command to try to get the HC
1110 * back into a known state.
1111 */
1112 return -EINVAL;
1113 }
1114 if (!room_on_ring(xhci, ep_ring, num_trbs)) {
1115 /* FIXME allocate more room */
1116 xhci_err(xhci, "ERROR no room on ep ring\n");
1117 return -ENOMEM;
1118 }
1119 return 0;
1120}
1121
1122static int prepare_transfer(struct xhci_hcd *xhci,
1123 struct xhci_virt_device *xdev,
1124 unsigned int ep_index,
1125 unsigned int num_trbs,
1126 struct urb *urb,
1127 struct xhci_td **td,
1128 gfp_t mem_flags)
1129{
1130 int ret;
1131
1132 ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
1133 xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK,
1134 num_trbs, mem_flags);
1135 if (ret)
1136 return ret;
1137 *td = kzalloc(sizeof(struct xhci_td), mem_flags);
1138 if (!*td)
1139 return -ENOMEM;
1140 INIT_LIST_HEAD(&(*td)->td_list);
1141 INIT_LIST_HEAD(&(*td)->cancelled_td_list);
1142
1143 ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
1144 if (unlikely(ret)) {
1145 kfree(*td);
1146 return ret;
1147 }
1148
1149 (*td)->urb = urb;
1150 urb->hcpriv = (void *) (*td);
1151 /* Add this TD to the tail of the endpoint ring's TD list */
1152 list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
1153 (*td)->start_seg = xdev->ep_rings[ep_index]->enq_seg;
1154 (*td)->first_trb = xdev->ep_rings[ep_index]->enqueue;
1155
1156 return 0;
1157}
1158
1159static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
1160{
1161 int num_sgs, num_trbs, running_total, temp, i;
1162 struct scatterlist *sg;
1163
1164 sg = NULL;
1165 num_sgs = urb->num_sgs;
1166 temp = urb->transfer_buffer_length;
1167
1168 xhci_dbg(xhci, "count sg list trbs: \n");
1169 num_trbs = 0;
1170 for_each_sg(urb->sg->sg, sg, num_sgs, i) {
1171 unsigned int previous_total_trbs = num_trbs;
1172 unsigned int len = sg_dma_len(sg);
1173
1174 /* Scatter gather list entries may cross 64KB boundaries */
1175 running_total = TRB_MAX_BUFF_SIZE -
1176 (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
1177 if (running_total != 0)
1178 num_trbs++;
1179
1180 /* How many more 64KB chunks to transfer, how many more TRBs? */
1181 while (running_total < sg_dma_len(sg)) {
1182 num_trbs++;
1183 running_total += TRB_MAX_BUFF_SIZE;
1184 }
1185 xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
1186 i, (unsigned long long)sg_dma_address(sg),
1187 len, len, num_trbs - previous_total_trbs);
1188
1189 len = min_t(int, len, temp);
1190 temp -= len;
1191 if (temp == 0)
1192 break;
1193 }
1194 xhci_dbg(xhci, "\n");
1195 if (!in_interrupt())
1196 dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
1197 urb->ep->desc.bEndpointAddress,
1198 urb->transfer_buffer_length,
1199 num_trbs);
1200 return num_trbs;
1201}
1202
1203static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
1204{
1205 if (num_trbs != 0)
1206 dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
1207 "TRBs, %d left\n", __func__,
1208 urb->ep->desc.bEndpointAddress, num_trbs);
1209 if (running_total != urb->transfer_buffer_length)
1210 dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
1211 "queued %#x (%d), asked for %#x (%d)\n",
1212 __func__,
1213 urb->ep->desc.bEndpointAddress,
1214 running_total, running_total,
1215 urb->transfer_buffer_length,
1216 urb->transfer_buffer_length);
1217}
1218
1219static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
1220 unsigned int ep_index, int start_cycle,
1221 struct xhci_generic_trb *start_trb, struct xhci_td *td)
1222{
1223 /*
1224 * Pass all the TRBs to the hardware at once and make sure this write
1225 * isn't reordered.
1226 */
1227 wmb();
1228 start_trb->field[3] |= start_cycle;
1229 ring_ep_doorbell(xhci, slot_id, ep_index);
1230}
1231
1232static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1233 struct urb *urb, int slot_id, unsigned int ep_index)
1234{
1235 struct xhci_ring *ep_ring;
1236 unsigned int num_trbs;
1237 struct xhci_td *td;
1238 struct scatterlist *sg;
1239 int num_sgs;
1240 int trb_buff_len, this_sg_len, running_total;
1241 bool first_trb;
1242 u64 addr;
1243
1244 struct xhci_generic_trb *start_trb;
1245 int start_cycle;
1246
1247 ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
1248 num_trbs = count_sg_trbs_needed(xhci, urb);
1249 num_sgs = urb->num_sgs;
1250
1251 trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
1252 ep_index, num_trbs, urb, &td, mem_flags);
1253 if (trb_buff_len < 0)
1254 return trb_buff_len;
1255 /*
1256 * Don't give the first TRB to the hardware (by toggling the cycle bit)
1257 * until we've finished creating all the other TRBs. The ring's cycle
1258 * state may change as we enqueue the other TRBs, so save it too.
1259 */
1260 start_trb = &ep_ring->enqueue->generic;
1261 start_cycle = ep_ring->cycle_state;
1262
1263 running_total = 0;
1264 /*
1265 * How much data is in the first TRB?
1266 *
1267 * There are three forces at work for TRB buffer pointers and lengths:
1268 * 1. We don't want to walk off the end of this sg-list entry buffer.
1269 * 2. The transfer length that the driver requested may be smaller than
1270 * the amount of memory allocated for this scatter-gather list.
1271 * 3. TRBs buffers can't cross 64KB boundaries.
1272 */
1273 sg = urb->sg->sg;
1274 addr = (u64) sg_dma_address(sg);
1275 this_sg_len = sg_dma_len(sg);
1276 trb_buff_len = TRB_MAX_BUFF_SIZE -
1277 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
1278 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
1279 if (trb_buff_len > urb->transfer_buffer_length)
1280 trb_buff_len = urb->transfer_buffer_length;
1281 xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
1282 trb_buff_len);
1283
1284 first_trb = true;
1285 /* Queue the first TRB, even if it's zero-length */
1286 do {
1287 u32 field = 0;
1288
1289 /* Don't change the cycle bit of the first TRB until later */
1290 if (first_trb)
1291 first_trb = false;
1292 else
1293 field |= ep_ring->cycle_state;
1294
1295 /* Chain all the TRBs together; clear the chain bit in the last
1296 * TRB to indicate it's the last TRB in the chain.
1297 */
1298 if (num_trbs > 1) {
1299 field |= TRB_CHAIN;
1300 } else {
1301 /* FIXME - add check for ZERO_PACKET flag before this */
1302 td->last_trb = ep_ring->enqueue;
1303 field |= TRB_IOC;
1304 }
1305 xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
1306 "64KB boundary at %#x, end dma = %#x\n",
1307 (unsigned int) addr, trb_buff_len, trb_buff_len,
1308 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
1309 (unsigned int) addr + trb_buff_len);
1310 if (TRB_MAX_BUFF_SIZE -
1311 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
1312 xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
1313 xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
1314 (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
1315 (unsigned int) addr + trb_buff_len);
1316 }
1317 queue_trb(xhci, ep_ring, false,
1318 (u32) addr,
1319 (u32) ((u64) addr >> 32),
1320 TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
1321 /* We always want to know if the TRB was short,
1322 * or we won't get an event when it completes.
1323 * (Unless we use event data TRBs, which are a
1324 * waste of space and HC resources.)
1325 */
1326 field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
1327 --num_trbs;
1328 running_total += trb_buff_len;
1329
1330 /* Calculate length for next transfer --
1331 * Are we done queueing all the TRBs for this sg entry?
1332 */
1333 this_sg_len -= trb_buff_len;
1334 if (this_sg_len == 0) {
1335 --num_sgs;
1336 if (num_sgs == 0)
1337 break;
1338 sg = sg_next(sg);
1339 addr = (u64) sg_dma_address(sg);
1340 this_sg_len = sg_dma_len(sg);
1341 } else {
1342 addr += trb_buff_len;
1343 }
1344
1345 trb_buff_len = TRB_MAX_BUFF_SIZE -
1346 (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
1347 trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
1348 if (running_total + trb_buff_len > urb->transfer_buffer_length)
1349 trb_buff_len =
1350 urb->transfer_buffer_length - running_total;
1351 } while (running_total < urb->transfer_buffer_length);
1352
1353 check_trb_math(urb, num_trbs, running_total);
1354 giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
1355 return 0;
1356}
1357
1358/* This is very similar to what ehci-q.c qtd_fill() does */
1359int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1360 struct urb *urb, int slot_id, unsigned int ep_index)
1361{
1362 struct xhci_ring *ep_ring;
1363 struct xhci_td *td;
1364 int num_trbs;
1365 struct xhci_generic_trb *start_trb;
1366 bool first_trb;
1367 int start_cycle;
1368 u32 field;
1369
1370 int running_total, trb_buff_len, ret;
1371 u64 addr;
1372
1373 if (urb->sg)
1374 return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
1375
1376 ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
1377
1378 num_trbs = 0;
1379 /* How much data is (potentially) left before the 64KB boundary? */
1380 running_total = TRB_MAX_BUFF_SIZE -
1381 (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
1382
1383 /* If there's some data on this 64KB chunk, or we have to send a
1384 * zero-length transfer, we need at least one TRB
1385 */
1386 if (running_total != 0 || urb->transfer_buffer_length == 0)
1387 num_trbs++;
1388 /* How many more 64KB chunks to transfer, how many more TRBs? */
1389 while (running_total < urb->transfer_buffer_length) {
1390 num_trbs++;
1391 running_total += TRB_MAX_BUFF_SIZE;
1392 }
1393 /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
1394
1395 if (!in_interrupt())
1396 dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
1397 urb->ep->desc.bEndpointAddress,
1398 urb->transfer_buffer_length,
1399 urb->transfer_buffer_length,
1400 (unsigned long long)urb->transfer_dma,
1401 num_trbs);
1402
1403 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
1404 num_trbs, urb, &td, mem_flags);
1405 if (ret < 0)
1406 return ret;
1407
1408 /*
1409 * Don't give the first TRB to the hardware (by toggling the cycle bit)
1410 * until we've finished creating all the other TRBs. The ring's cycle
1411 * state may change as we enqueue the other TRBs, so save it too.
1412 */
1413 start_trb = &ep_ring->enqueue->generic;
1414 start_cycle = ep_ring->cycle_state;
1415
1416 running_total = 0;
1417 /* How much data is in the first TRB? */
1418 addr = (u64) urb->transfer_dma;
1419 trb_buff_len = TRB_MAX_BUFF_SIZE -
1420 (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
1421 if (urb->transfer_buffer_length < trb_buff_len)
1422 trb_buff_len = urb->transfer_buffer_length;
1423
1424 first_trb = true;
1425
1426 /* Queue the first TRB, even if it's zero-length */
1427 do {
1428 field = 0;
1429
1430 /* Don't change the cycle bit of the first TRB until later */
1431 if (first_trb)
1432 first_trb = false;
1433 else
1434 field |= ep_ring->cycle_state;
1435
1436 /* Chain all the TRBs together; clear the chain bit in the last
1437 * TRB to indicate it's the last TRB in the chain.
1438 */
1439 if (num_trbs > 1) {
1440 field |= TRB_CHAIN;
1441 } else {
1442 /* FIXME - add check for ZERO_PACKET flag before this */
1443 td->last_trb = ep_ring->enqueue;
1444 field |= TRB_IOC;
1445 }
1446 queue_trb(xhci, ep_ring, false,
1447 (u32) addr,
1448 (u32) ((u64) addr >> 32),
1449 TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
1450 /* We always want to know if the TRB was short,
1451 * or we won't get an event when it completes.
1452 * (Unless we use event data TRBs, which are a
1453 * waste of space and HC resources.)
1454 */
1455 field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
1456 --num_trbs;
1457 running_total += trb_buff_len;
1458
1459 /* Calculate length for next transfer */
1460 addr += trb_buff_len;
1461 trb_buff_len = urb->transfer_buffer_length - running_total;
1462 if (trb_buff_len > TRB_MAX_BUFF_SIZE)
1463 trb_buff_len = TRB_MAX_BUFF_SIZE;
1464 } while (running_total < urb->transfer_buffer_length);
1465
1466 check_trb_math(urb, num_trbs, running_total);
1467 giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
1468 return 0;
1469}
1470
1471/* Caller must have locked xhci->lock */
1472int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
1473 struct urb *urb, int slot_id, unsigned int ep_index)
1474{
1475 struct xhci_ring *ep_ring;
1476 int num_trbs;
1477 int ret;
1478 struct usb_ctrlrequest *setup;
1479 struct xhci_generic_trb *start_trb;
1480 int start_cycle;
1481 u32 field;
1482 struct xhci_td *td;
1483
1484 ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
1485
1486 /*
1487 * Need to copy setup packet into setup TRB, so we can't use the setup
1488 * DMA address.
1489 */
1490 if (!urb->setup_packet)
1491 return -EINVAL;
1492
1493 if (!in_interrupt())
1494 xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
1495 slot_id, ep_index);
1496 /* 1 TRB for setup, 1 for status */
1497 num_trbs = 2;
1498 /*
1499 * Don't need to check if we need additional event data and normal TRBs,
1500 * since data in control transfers will never get bigger than 16MB
1501 * XXX: can we get a buffer that crosses 64KB boundaries?
1502 */
1503 if (urb->transfer_buffer_length > 0)
1504 num_trbs++;
1505 ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
1506 urb, &td, mem_flags);
1507 if (ret < 0)
1508 return ret;
1509
1510 /*
1511 * Don't give the first TRB to the hardware (by toggling the cycle bit)
1512 * until we've finished creating all the other TRBs. The ring's cycle
1513 * state may change as we enqueue the other TRBs, so save it too.
1514 */
1515 start_trb = &ep_ring->enqueue->generic;
1516 start_cycle = ep_ring->cycle_state;
1517
1518 /* Queue setup TRB - see section 6.4.1.2.1 */
1519 /* FIXME better way to translate setup_packet into two u32 fields? */
1520 setup = (struct usb_ctrlrequest *) urb->setup_packet;
1521 queue_trb(xhci, ep_ring, false,
1522 /* FIXME endianness is probably going to bite my ass here. */
1523 setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
1524 setup->wIndex | setup->wLength << 16,
1525 TRB_LEN(8) | TRB_INTR_TARGET(0),
1526 /* Immediate data in pointer */
1527 TRB_IDT | TRB_TYPE(TRB_SETUP));
1528
1529 /* If there's data, queue data TRBs */
1530 field = 0;
1531 if (urb->transfer_buffer_length > 0) {
1532 if (setup->bRequestType & USB_DIR_IN)
1533 field |= TRB_DIR_IN;
1534 queue_trb(xhci, ep_ring, false,
1535 lower_32_bits(urb->transfer_dma),
1536 upper_32_bits(urb->transfer_dma),
1537 TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0),
1538 /* Event on short tx */
1539 field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
1540 }
1541
1542 /* Save the DMA address of the last TRB in the TD */
1543 td->last_trb = ep_ring->enqueue;
1544
1545 /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
1546 /* If the device sent data, the status stage is an OUT transfer */
1547 if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
1548 field = 0;
1549 else
1550 field = TRB_DIR_IN;
1551 queue_trb(xhci, ep_ring, false,
1552 0,
1553 0,
1554 TRB_INTR_TARGET(0),
1555 /* Event on completion */
1556 field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
1557
1558 giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
1559 return 0;
1560}
1561
1562/**** Command Ring Operations ****/
1563
1564/* Generic function for queueing a command TRB on the command ring */
1565static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
1566{
1567 if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
1568 if (!in_interrupt())
1569 xhci_err(xhci, "ERR: No room for command on command ring\n");
1570 return -ENOMEM;
1571 }
1572 queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
1573 field4 | xhci->cmd_ring->cycle_state);
1574 return 0;
1575}
1576
1577/* Queue a no-op command on the command ring */
1578static int queue_cmd_noop(struct xhci_hcd *xhci)
1579{
1580 return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
1581}
1582
1583/*
1584 * Place a no-op command on the command ring to test the command and
1585 * event ring.
1586 */
1587void *xhci_setup_one_noop(struct xhci_hcd *xhci)
1588{
1589 if (queue_cmd_noop(xhci) < 0)
1590 return NULL;
1591 xhci->noops_submitted++;
1592 return xhci_ring_cmd_db;
1593}
1594
1595/* Queue a slot enable or disable request on the command ring */
1596int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
1597{
1598 return queue_command(xhci, 0, 0, 0,
1599 TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
1600}
1601
1602/* Queue an address device command TRB */
1603int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1604 u32 slot_id)
1605{
1606 return queue_command(xhci, in_ctx_ptr, 0, 0,
1607 TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
1608}
1609
1610/* Queue a configure endpoint command TRB */
1611int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1612 u32 slot_id)
1613{
1614 return queue_command(xhci, in_ctx_ptr, 0, 0,
1615 TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
1616}
1617
1618int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
1619 unsigned int ep_index)
1620{
1621 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
1622 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
1623 u32 type = TRB_TYPE(TRB_STOP_RING);
1624
1625 return queue_command(xhci, 0, 0, 0,
1626 trb_slot_id | trb_ep_index | type);
1627}
1628
1629/* Set Transfer Ring Dequeue Pointer command.
1630 * This should not be used for endpoints that have streams enabled.
1631 */
1632static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
1633 unsigned int ep_index, struct xhci_segment *deq_seg,
1634 union xhci_trb *deq_ptr, u32 cycle_state)
1635{
1636 dma_addr_t addr;
1637 u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
1638 u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
1639 u32 type = TRB_TYPE(TRB_SET_DEQ);
1640
1641 addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
1642 if (addr == 0)
1643 xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
1644 xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
1645 deq_seg, deq_ptr);
1646 return queue_command(xhci, (u32) addr | cycle_state, 0, 0,
1647 trb_slot_id | trb_ep_index | type);
1648}
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
new file mode 100644
index 000000000000..8936eeb5588b
--- /dev/null
+++ b/drivers/usb/host/xhci.h
@@ -0,0 +1,1157 @@
1/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#ifndef __LINUX_XHCI_HCD_H
24#define __LINUX_XHCI_HCD_H
25
26#include <linux/usb.h>
27#include <linux/timer.h>
28
29#include "../core/hcd.h"
30/* Code sharing between pci-quirks and xhci hcd */
31#include "xhci-ext-caps.h"
32
33/* xHCI PCI Configuration Registers */
34#define XHCI_SBRN_OFFSET (0x60)
35
36/* Max number of USB devices for any host controller - limit in section 6.1 */
37#define MAX_HC_SLOTS 256
38/* Section 5.3.3 - MaxPorts */
39#define MAX_HC_PORTS 127
40
41/*
42 * xHCI register interface.
43 * This corresponds to the eXtensible Host Controller Interface (xHCI)
44 * Revision 0.95 specification
45 *
46 * Registers should always be accessed with double word or quad word accesses.
47 *
48 * Some xHCI implementations may support 64-bit address pointers. Registers
49 * with 64-bit address pointers should be written to with dword accesses by
50 * writing the low dword first (ptr[0]), then the high dword (ptr[1]) second.
51 * xHCI implementations that do not support 64-bit address pointers will ignore
52 * the high dword, and write order is irrelevant.
53 */
54
55/**
56 * struct xhci_cap_regs - xHCI Host Controller Capability Registers.
57 * @hc_capbase: length of the capabilities register and HC version number
58 * @hcs_params1: HCSPARAMS1 - Structural Parameters 1
59 * @hcs_params2: HCSPARAMS2 - Structural Parameters 2
60 * @hcs_params3: HCSPARAMS3 - Structural Parameters 3
61 * @hcc_params: HCCPARAMS - Capability Parameters
62 * @db_off: DBOFF - Doorbell array offset
63 * @run_regs_off: RTSOFF - Runtime register space offset
64 */
65struct xhci_cap_regs {
66 u32 hc_capbase;
67 u32 hcs_params1;
68 u32 hcs_params2;
69 u32 hcs_params3;
70 u32 hcc_params;
71 u32 db_off;
72 u32 run_regs_off;
73 /* Reserved up to (CAPLENGTH - 0x1C) */
74};
75
76/* hc_capbase bitmasks */
77/* bits 7:0 - how long is the Capabilities register */
78#define HC_LENGTH(p) XHCI_HC_LENGTH(p)
79/* bits 31:16 */
80#define HC_VERSION(p) (((p) >> 16) & 0xffff)
81
82/* HCSPARAMS1 - hcs_params1 - bitmasks */
83/* bits 0:7, Max Device Slots */
84#define HCS_MAX_SLOTS(p) (((p) >> 0) & 0xff)
85#define HCS_SLOTS_MASK 0xff
86/* bits 8:18, Max Interrupters */
87#define HCS_MAX_INTRS(p) (((p) >> 8) & 0x7ff)
88/* bits 24:31, Max Ports - max value is 0x7F = 127 ports */
89#define HCS_MAX_PORTS(p) (((p) >> 24) & 0x7f)
90
91/* HCSPARAMS2 - hcs_params2 - bitmasks */
92/* bits 0:3, frames or uframes that SW needs to queue transactions
93 * ahead of the HW to meet periodic deadlines */
94#define HCS_IST(p) (((p) >> 0) & 0xf)
95/* bits 4:7, max number of Event Ring segments */
96#define HCS_ERST_MAX(p) (((p) >> 4) & 0xf)
97/* bit 26 Scratchpad restore - for save/restore HW state - not used yet */
98/* bits 27:31 number of Scratchpad buffers SW must allocate for the HW */
99
100/* HCSPARAMS3 - hcs_params3 - bitmasks */
101/* bits 0:7, Max U1 to U0 latency for the roothub ports */
102#define HCS_U1_LATENCY(p) (((p) >> 0) & 0xff)
103/* bits 16:31, Max U2 to U0 latency for the roothub ports */
104#define HCS_U2_LATENCY(p) (((p) >> 16) & 0xffff)
105
106/* HCCPARAMS - hcc_params - bitmasks */
107/* true: HC can use 64-bit address pointers */
108#define HCC_64BIT_ADDR(p) ((p) & (1 << 0))
109/* true: HC can do bandwidth negotiation */
110#define HCC_BANDWIDTH_NEG(p) ((p) & (1 << 1))
111/* true: HC uses 64-byte Device Context structures
112 * FIXME 64-byte context structures aren't supported yet.
113 */
114#define HCC_64BYTE_CONTEXT(p) ((p) & (1 << 2))
115/* true: HC has port power switches */
116#define HCC_PPC(p) ((p) & (1 << 3))
117/* true: HC has port indicators */
118#define HCS_INDICATOR(p) ((p) & (1 << 4))
119/* true: HC has Light HC Reset Capability */
120#define HCC_LIGHT_RESET(p) ((p) & (1 << 5))
121/* true: HC supports latency tolerance messaging */
122#define HCC_LTC(p) ((p) & (1 << 6))
123/* true: no secondary Stream ID Support */
124#define HCC_NSS(p) ((p) & (1 << 7))
125/* Max size for Primary Stream Arrays - 2^(n+1), where n is bits 12:15 */
126#define HCC_MAX_PSA (1 << ((((p) >> 12) & 0xf) + 1))
127/* Extended Capabilities pointer from PCI base - section 5.3.6 */
128#define HCC_EXT_CAPS(p) XHCI_HCC_EXT_CAPS(p)
129
130/* db_off bitmask - bits 0:1 reserved */
131#define DBOFF_MASK (~0x3)
132
133/* run_regs_off bitmask - bits 0:4 reserved */
134#define RTSOFF_MASK (~0x1f)
135
136
137/* Number of registers per port */
138#define NUM_PORT_REGS 4
139
140/**
141 * struct xhci_op_regs - xHCI Host Controller Operational Registers.
142 * @command: USBCMD - xHC command register
143 * @status: USBSTS - xHC status register
144 * @page_size: This indicates the page size that the host controller
145 * supports. If bit n is set, the HC supports a page size
146 * of 2^(n+12), up to a 128MB page size.
147 * 4K is the minimum page size.
148 * @cmd_ring: CRP - 64-bit Command Ring Pointer
149 * @dcbaa_ptr: DCBAAP - 64-bit Device Context Base Address Array Pointer
150 * @config_reg: CONFIG - Configure Register
151 * @port_status_base: PORTSCn - base address for Port Status and Control
152 * Each port has a Port Status and Control register,
153 * followed by a Port Power Management Status and Control
154 * register, a Port Link Info register, and a reserved
155 * register.
156 * @port_power_base: PORTPMSCn - base address for
157 * Port Power Management Status and Control
158 * @port_link_base: PORTLIn - base address for Port Link Info (current
159 * Link PM state and control) for USB 2.1 and USB 3.0
160 * devices.
161 */
162struct xhci_op_regs {
163 u32 command;
164 u32 status;
165 u32 page_size;
166 u32 reserved1;
167 u32 reserved2;
168 u32 dev_notification;
169 u32 cmd_ring[2];
170 /* rsvd: offset 0x20-2F */
171 u32 reserved3[4];
172 u32 dcbaa_ptr[2];
173 u32 config_reg;
174 /* rsvd: offset 0x3C-3FF */
175 u32 reserved4[241];
176 /* port 1 registers, which serve as a base address for other ports */
177 u32 port_status_base;
178 u32 port_power_base;
179 u32 port_link_base;
180 u32 reserved5;
181 /* registers for ports 2-255 */
182 u32 reserved6[NUM_PORT_REGS*254];
183};
184
185/* USBCMD - USB command - command bitmasks */
186/* start/stop HC execution - do not write unless HC is halted*/
187#define CMD_RUN XHCI_CMD_RUN
188/* Reset HC - resets internal HC state machine and all registers (except
189 * PCI config regs). HC does NOT drive a USB reset on the downstream ports.
190 * The xHCI driver must reinitialize the xHC after setting this bit.
191 */
192#define CMD_RESET (1 << 1)
193/* Event Interrupt Enable - a '1' allows interrupts from the host controller */
194#define CMD_EIE XHCI_CMD_EIE
195/* Host System Error Interrupt Enable - get out-of-band signal for HC errors */
196#define CMD_HSEIE XHCI_CMD_HSEIE
197/* bits 4:6 are reserved (and should be preserved on writes). */
198/* light reset (port status stays unchanged) - reset completed when this is 0 */
199#define CMD_LRESET (1 << 7)
200/* FIXME: ignoring host controller save/restore state for now. */
201#define CMD_CSS (1 << 8)
202#define CMD_CRS (1 << 9)
203/* Enable Wrap Event - '1' means xHC generates an event when MFINDEX wraps. */
204#define CMD_EWE XHCI_CMD_EWE
205/* MFINDEX power management - '1' means xHC can stop MFINDEX counter if all root
206 * hubs are in U3 (selective suspend), disconnect, disabled, or powered-off.
207 * '0' means the xHC can power it off if all ports are in the disconnect,
208 * disabled, or powered-off state.
209 */
210#define CMD_PM_INDEX (1 << 11)
211/* bits 12:31 are reserved (and should be preserved on writes). */
212
213/* USBSTS - USB status - status bitmasks */
214/* HC not running - set to 1 when run/stop bit is cleared. */
215#define STS_HALT XHCI_STS_HALT
216/* serious error, e.g. PCI parity error. The HC will clear the run/stop bit. */
217#define STS_FATAL (1 << 2)
218/* event interrupt - clear this prior to clearing any IP flags in IR set*/
219#define STS_EINT (1 << 3)
220/* port change detect */
221#define STS_PORT (1 << 4)
222/* bits 5:7 reserved and zeroed */
223/* save state status - '1' means xHC is saving state */
224#define STS_SAVE (1 << 8)
225/* restore state status - '1' means xHC is restoring state */
226#define STS_RESTORE (1 << 9)
227/* true: save or restore error */
228#define STS_SRE (1 << 10)
229/* true: Controller Not Ready to accept doorbell or op reg writes after reset */
230#define STS_CNR XHCI_STS_CNR
231/* true: internal Host Controller Error - SW needs to reset and reinitialize */
232#define STS_HCE (1 << 12)
233/* bits 13:31 reserved and should be preserved */
234
235/*
236 * DNCTRL - Device Notification Control Register - dev_notification bitmasks
237 * Generate a device notification event when the HC sees a transaction with a
238 * notification type that matches a bit set in this bit field.
239 */
240#define DEV_NOTE_MASK (0xffff)
241#define ENABLE_DEV_NOTE(x) (1 << x)
242/* Most of the device notification types should only be used for debug.
243 * SW does need to pay attention to function wake notifications.
244 */
245#define DEV_NOTE_FWAKE ENABLE_DEV_NOTE(1)
246
247/* CRCR - Command Ring Control Register - cmd_ring bitmasks */
248/* bit 0 is the command ring cycle state */
249/* stop ring operation after completion of the currently executing command */
250#define CMD_RING_PAUSE (1 << 1)
251/* stop ring immediately - abort the currently executing command */
252#define CMD_RING_ABORT (1 << 2)
253/* true: command ring is running */
254#define CMD_RING_RUNNING (1 << 3)
255/* bits 4:5 reserved and should be preserved */
256/* Command Ring pointer - bit mask for the lower 32 bits. */
257#define CMD_RING_ADDR_MASK (0xffffffc0)
258
259/* CONFIG - Configure Register - config_reg bitmasks */
260/* bits 0:7 - maximum number of device slots enabled (NumSlotsEn) */
261#define MAX_DEVS(p) ((p) & 0xff)
262/* bits 8:31 - reserved and should be preserved */
263
264/* PORTSC - Port Status and Control Register - port_status_base bitmasks */
265/* true: device connected */
266#define PORT_CONNECT (1 << 0)
267/* true: port enabled */
268#define PORT_PE (1 << 1)
269/* bit 2 reserved and zeroed */
270/* true: port has an over-current condition */
271#define PORT_OC (1 << 3)
272/* true: port reset signaling asserted */
273#define PORT_RESET (1 << 4)
274/* Port Link State - bits 5:8
275 * A read gives the current link PM state of the port,
276 * a write with Link State Write Strobe set sets the link state.
277 */
278/* true: port has power (see HCC_PPC) */
279#define PORT_POWER (1 << 9)
280/* bits 10:13 indicate device speed:
281 * 0 - undefined speed - port hasn't be initialized by a reset yet
282 * 1 - full speed
283 * 2 - low speed
284 * 3 - high speed
285 * 4 - super speed
286 * 5-15 reserved
287 */
288#define DEV_SPEED_MASK (0xf << 10)
289#define XDEV_FS (0x1 << 10)
290#define XDEV_LS (0x2 << 10)
291#define XDEV_HS (0x3 << 10)
292#define XDEV_SS (0x4 << 10)
293#define DEV_UNDEFSPEED(p) (((p) & DEV_SPEED_MASK) == (0x0<<10))
294#define DEV_FULLSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_FS)
295#define DEV_LOWSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_LS)
296#define DEV_HIGHSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_HS)
297#define DEV_SUPERSPEED(p) (((p) & DEV_SPEED_MASK) == XDEV_SS)
298/* Bits 20:23 in the Slot Context are the speed for the device */
299#define SLOT_SPEED_FS (XDEV_FS << 10)
300#define SLOT_SPEED_LS (XDEV_LS << 10)
301#define SLOT_SPEED_HS (XDEV_HS << 10)
302#define SLOT_SPEED_SS (XDEV_SS << 10)
303/* Port Indicator Control */
304#define PORT_LED_OFF (0 << 14)
305#define PORT_LED_AMBER (1 << 14)
306#define PORT_LED_GREEN (2 << 14)
307#define PORT_LED_MASK (3 << 14)
308/* Port Link State Write Strobe - set this when changing link state */
309#define PORT_LINK_STROBE (1 << 16)
310/* true: connect status change */
311#define PORT_CSC (1 << 17)
312/* true: port enable change */
313#define PORT_PEC (1 << 18)
314/* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
315 * into an enabled state, and the device into the default state. A "warm" reset
316 * also resets the link, forcing the device through the link training sequence.
317 * SW can also look at the Port Reset register to see when warm reset is done.
318 */
319#define PORT_WRC (1 << 19)
320/* true: over-current change */
321#define PORT_OCC (1 << 20)
322/* true: reset change - 1 to 0 transition of PORT_RESET */
323#define PORT_RC (1 << 21)
324/* port link status change - set on some port link state transitions:
325 * Transition Reason
326 * ------------------------------------------------------------------------------
327 * - U3 to Resume Wakeup signaling from a device
328 * - Resume to Recovery to U0 USB 3.0 device resume
329 * - Resume to U0 USB 2.0 device resume
330 * - U3 to Recovery to U0 Software resume of USB 3.0 device complete
331 * - U3 to U0 Software resume of USB 2.0 device complete
332 * - U2 to U0 L1 resume of USB 2.1 device complete
333 * - U0 to U0 (???) L1 entry rejection by USB 2.1 device
334 * - U0 to disabled L1 entry error with USB 2.1 device
335 * - Any state to inactive Error on USB 3.0 port
336 */
337#define PORT_PLC (1 << 22)
338/* port configure error change - port failed to configure its link partner */
339#define PORT_CEC (1 << 23)
340/* bit 24 reserved */
341/* wake on connect (enable) */
342#define PORT_WKCONN_E (1 << 25)
343/* wake on disconnect (enable) */
344#define PORT_WKDISC_E (1 << 26)
345/* wake on over-current (enable) */
346#define PORT_WKOC_E (1 << 27)
347/* bits 28:29 reserved */
348/* true: device is removable - for USB 3.0 roothub emulation */
349#define PORT_DEV_REMOVE (1 << 30)
350/* Initiate a warm port reset - complete when PORT_WRC is '1' */
351#define PORT_WR (1 << 31)
352
353/* Port Power Management Status and Control - port_power_base bitmasks */
354/* Inactivity timer value for transitions into U1, in microseconds.
355 * Timeout can be up to 127us. 0xFF means an infinite timeout.
356 */
357#define PORT_U1_TIMEOUT(p) ((p) & 0xff)
358/* Inactivity timer value for transitions into U2 */
359#define PORT_U2_TIMEOUT(p) (((p) & 0xff) << 8)
360/* Bits 24:31 for port testing */
361
362
363/**
364 * struct xhci_intr_reg - Interrupt Register Set
365 * @irq_pending: IMAN - Interrupt Management Register. Used to enable
366 * interrupts and check for pending interrupts.
367 * @irq_control: IMOD - Interrupt Moderation Register.
368 * Used to throttle interrupts.
369 * @erst_size: Number of segments in the Event Ring Segment Table (ERST).
370 * @erst_base: ERST base address.
371 * @erst_dequeue: Event ring dequeue pointer.
372 *
373 * Each interrupter (defined by a MSI-X vector) has an event ring and an Event
374 * Ring Segment Table (ERST) associated with it. The event ring is comprised of
375 * multiple segments of the same size. The HC places events on the ring and
376 * "updates the Cycle bit in the TRBs to indicate to software the current
377 * position of the Enqueue Pointer." The HCD (Linux) processes those events and
378 * updates the dequeue pointer.
379 */
380struct xhci_intr_reg {
381 u32 irq_pending;
382 u32 irq_control;
383 u32 erst_size;
384 u32 rsvd;
385 u32 erst_base[2];
386 u32 erst_dequeue[2];
387};
388
389/* irq_pending bitmasks */
390#define ER_IRQ_PENDING(p) ((p) & 0x1)
391/* bits 2:31 need to be preserved */
392/* THIS IS BUGGY - FIXME - IP IS WRITE 1 TO CLEAR */
393#define ER_IRQ_CLEAR(p) ((p) & 0xfffffffe)
394#define ER_IRQ_ENABLE(p) ((ER_IRQ_CLEAR(p)) | 0x2)
395#define ER_IRQ_DISABLE(p) ((ER_IRQ_CLEAR(p)) & ~(0x2))
396
397/* irq_control bitmasks */
398/* Minimum interval between interrupts (in 250ns intervals). The interval
399 * between interrupts will be longer if there are no events on the event ring.
400 * Default is 4000 (1 ms).
401 */
402#define ER_IRQ_INTERVAL_MASK (0xffff)
403/* Counter used to count down the time to the next interrupt - HW use only */
404#define ER_IRQ_COUNTER_MASK (0xffff << 16)
405
406/* erst_size bitmasks */
407/* Preserve bits 16:31 of erst_size */
408#define ERST_SIZE_MASK (0xffff << 16)
409
410/* erst_dequeue bitmasks */
411/* Dequeue ERST Segment Index (DESI) - Segment number (or alias)
412 * where the current dequeue pointer lies. This is an optional HW hint.
413 */
414#define ERST_DESI_MASK (0x7)
415/* Event Handler Busy (EHB) - is the event ring scheduled to be serviced by
416 * a work queue (or delayed service routine)?
417 */
418#define ERST_EHB (1 << 3)
419#define ERST_PTR_MASK (0xf)
420
421/**
422 * struct xhci_run_regs
423 * @microframe_index:
424 * MFINDEX - current microframe number
425 *
426 * Section 5.5 Host Controller Runtime Registers:
427 * "Software should read and write these registers using only Dword (32 bit)
428 * or larger accesses"
429 */
430struct xhci_run_regs {
431 u32 microframe_index;
432 u32 rsvd[7];
433 struct xhci_intr_reg ir_set[128];
434};
435
436/**
437 * struct doorbell_array
438 *
439 * Section 5.6
440 */
441struct xhci_doorbell_array {
442 u32 doorbell[256];
443};
444
445#define DB_TARGET_MASK 0xFFFFFF00
446#define DB_STREAM_ID_MASK 0x0000FFFF
447#define DB_TARGET_HOST 0x0
448#define DB_STREAM_ID_HOST 0x0
449#define DB_MASK (0xff << 8)
450
451/* Endpoint Target - bits 0:7 */
452#define EPI_TO_DB(p) (((p) + 1) & 0xff)
453
454
455/**
456 * struct xhci_slot_ctx
457 * @dev_info: Route string, device speed, hub info, and last valid endpoint
458 * @dev_info2: Max exit latency for device number, root hub port number
459 * @tt_info: tt_info is used to construct split transaction tokens
460 * @dev_state: slot state and device address
461 *
462 * Slot Context - section 6.2.1.1. This assumes the HC uses 32-byte context
463 * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
464 * reserved at the end of the slot context for HC internal use.
465 */
466struct xhci_slot_ctx {
467 u32 dev_info;
468 u32 dev_info2;
469 u32 tt_info;
470 u32 dev_state;
471 /* offset 0x10 to 0x1f reserved for HC internal use */
472 u32 reserved[4];
473};
474
475/* dev_info bitmasks */
476/* Route String - 0:19 */
477#define ROUTE_STRING_MASK (0xfffff)
478/* Device speed - values defined by PORTSC Device Speed field - 20:23 */
479#define DEV_SPEED (0xf << 20)
480/* bit 24 reserved */
481/* Is this LS/FS device connected through a HS hub? - bit 25 */
482#define DEV_MTT (0x1 << 25)
483/* Set if the device is a hub - bit 26 */
484#define DEV_HUB (0x1 << 26)
485/* Index of the last valid endpoint context in this device context - 27:31 */
486#define LAST_CTX_MASK (0x1f << 27)
487#define LAST_CTX(p) ((p) << 27)
488#define LAST_CTX_TO_EP_NUM(p) (((p) >> 27) - 1)
489#define SLOT_FLAG (1 << 0)
490#define EP0_FLAG (1 << 1)
491
492/* dev_info2 bitmasks */
493/* Max Exit Latency (ms) - worst case time to wake up all links in dev path */
494#define MAX_EXIT (0xffff)
495/* Root hub port number that is needed to access the USB device */
496#define ROOT_HUB_PORT(p) (((p) & 0xff) << 16)
497
498/* tt_info bitmasks */
499/*
500 * TT Hub Slot ID - for low or full speed devices attached to a high-speed hub
501 * The Slot ID of the hub that isolates the high speed signaling from
502 * this low or full-speed device. '0' if attached to root hub port.
503 */
504#define TT_SLOT (0xff)
505/*
506 * The number of the downstream facing port of the high-speed hub
507 * '0' if the device is not low or full speed.
508 */
509#define TT_PORT (0xff << 8)
510
511/* dev_state bitmasks */
512/* USB device address - assigned by the HC */
513#define DEV_ADDR_MASK (0xff)
514/* bits 8:26 reserved */
515/* Slot state */
516#define SLOT_STATE (0x1f << 27)
517#define GET_SLOT_STATE(p) (((p) & (0x1f << 27)) >> 27)
518
519
520/**
521 * struct xhci_ep_ctx
522 * @ep_info: endpoint state, streams, mult, and interval information.
523 * @ep_info2: information on endpoint type, max packet size, max burst size,
524 * error count, and whether the HC will force an event for all
525 * transactions.
526 * @deq: 64-bit ring dequeue pointer address. If the endpoint only
527 * defines one stream, this points to the endpoint transfer ring.
528 * Otherwise, it points to a stream context array, which has a
529 * ring pointer for each flow.
530 * @tx_info:
531 * Average TRB lengths for the endpoint ring and
532 * max payload within an Endpoint Service Interval Time (ESIT).
533 *
534 * Endpoint Context - section 6.2.1.2. This assumes the HC uses 32-byte context
535 * structures. If the HC uses 64-byte contexts, there is an additional 32 bytes
536 * reserved at the end of the endpoint context for HC internal use.
537 */
538struct xhci_ep_ctx {
539 u32 ep_info;
540 u32 ep_info2;
541 u32 deq[2];
542 u32 tx_info;
543 /* offset 0x14 - 0x1f reserved for HC internal use */
544 u32 reserved[3];
545};
546
547/* ep_info bitmasks */
548/*
549 * Endpoint State - bits 0:2
550 * 0 - disabled
551 * 1 - running
552 * 2 - halted due to halt condition - ok to manipulate endpoint ring
553 * 3 - stopped
554 * 4 - TRB error
555 * 5-7 - reserved
556 */
557#define EP_STATE_MASK (0xf)
558#define EP_STATE_DISABLED 0
559#define EP_STATE_RUNNING 1
560#define EP_STATE_HALTED 2
561#define EP_STATE_STOPPED 3
562#define EP_STATE_ERROR 4
563/* Mult - Max number of burtst within an interval, in EP companion desc. */
564#define EP_MULT(p) ((p & 0x3) << 8)
565/* bits 10:14 are Max Primary Streams */
566/* bit 15 is Linear Stream Array */
567/* Interval - period between requests to an endpoint - 125u increments. */
568#define EP_INTERVAL(p) ((p & 0xff) << 16)
569
570/* ep_info2 bitmasks */
571/*
572 * Force Event - generate transfer events for all TRBs for this endpoint
573 * This will tell the HC to ignore the IOC and ISP flags (for debugging only).
574 */
575#define FORCE_EVENT (0x1)
576#define ERROR_COUNT(p) (((p) & 0x3) << 1)
577#define EP_TYPE(p) ((p) << 3)
578#define ISOC_OUT_EP 1
579#define BULK_OUT_EP 2
580#define INT_OUT_EP 3
581#define CTRL_EP 4
582#define ISOC_IN_EP 5
583#define BULK_IN_EP 6
584#define INT_IN_EP 7
585/* bit 6 reserved */
586/* bit 7 is Host Initiate Disable - for disabling stream selection */
587#define MAX_BURST(p) (((p)&0xff) << 8)
588#define MAX_PACKET(p) (((p)&0xffff) << 16)
589
590
591/**
592 * struct xhci_device_control
593 * Input/Output context; see section 6.2.5.
594 *
595 * @drop_context: set the bit of the endpoint context you want to disable
596 * @add_context: set the bit of the endpoint context you want to enable
597 */
598struct xhci_device_control {
599 u32 drop_flags;
600 u32 add_flags;
601 u32 rsvd[6];
602 struct xhci_slot_ctx slot;
603 struct xhci_ep_ctx ep[31];
604};
605
606/* drop context bitmasks */
607#define DROP_EP(x) (0x1 << x)
608/* add context bitmasks */
609#define ADD_EP(x) (0x1 << x)
610
611
612struct xhci_virt_device {
613 /*
614 * Commands to the hardware are passed an "input context" that
615 * tells the hardware what to change in its data structures.
616 * The hardware will return changes in an "output context" that
617 * software must allocate for the hardware. We need to keep
618 * track of input and output contexts separately because
619 * these commands might fail and we don't trust the hardware.
620 */
621 struct xhci_device_control *out_ctx;
622 dma_addr_t out_ctx_dma;
623 /* Used for addressing devices and configuration changes */
624 struct xhci_device_control *in_ctx;
625 dma_addr_t in_ctx_dma;
626 /* FIXME when stream support is added */
627 struct xhci_ring *ep_rings[31];
628 /* Temporary storage in case the configure endpoint command fails and we
629 * have to restore the device state to the previous state
630 */
631 struct xhci_ring *new_ep_rings[31];
632 struct completion cmd_completion;
633 /* Status of the last command issued for this device */
634 u32 cmd_status;
635};
636
637
638/**
639 * struct xhci_device_context_array
640 * @dev_context_ptr array of 64-bit DMA addresses for device contexts
641 */
642struct xhci_device_context_array {
643 /* 64-bit device addresses; we only write 32-bit addresses */
644 u32 dev_context_ptrs[2*MAX_HC_SLOTS];
645 /* private xHCD pointers */
646 dma_addr_t dma;
647};
648/* TODO: write function to set the 64-bit device DMA address */
649/*
650 * TODO: change this to be dynamically sized at HC mem init time since the HC
651 * might not be able to handle the maximum number of devices possible.
652 */
653
654
655struct xhci_stream_ctx {
656 /* 64-bit stream ring address, cycle state, and stream type */
657 u32 stream_ring[2];
658 /* offset 0x14 - 0x1f reserved for HC internal use */
659 u32 reserved[2];
660};
661
662
663struct xhci_transfer_event {
664 /* 64-bit buffer address, or immediate data */
665 u32 buffer[2];
666 u32 transfer_len;
667 /* This field is interpreted differently based on the type of TRB */
668 u32 flags;
669};
670
671/** Transfer Event bit fields **/
672#define TRB_TO_EP_ID(p) (((p) >> 16) & 0x1f)
673
674/* Completion Code - only applicable for some types of TRBs */
675#define COMP_CODE_MASK (0xff << 24)
676#define GET_COMP_CODE(p) (((p) & COMP_CODE_MASK) >> 24)
677#define COMP_SUCCESS 1
678/* Data Buffer Error */
679#define COMP_DB_ERR 2
680/* Babble Detected Error */
681#define COMP_BABBLE 3
682/* USB Transaction Error */
683#define COMP_TX_ERR 4
684/* TRB Error - some TRB field is invalid */
685#define COMP_TRB_ERR 5
686/* Stall Error - USB device is stalled */
687#define COMP_STALL 6
688/* Resource Error - HC doesn't have memory for that device configuration */
689#define COMP_ENOMEM 7
690/* Bandwidth Error - not enough room in schedule for this dev config */
691#define COMP_BW_ERR 8
692/* No Slots Available Error - HC ran out of device slots */
693#define COMP_ENOSLOTS 9
694/* Invalid Stream Type Error */
695#define COMP_STREAM_ERR 10
696/* Slot Not Enabled Error - doorbell rung for disabled device slot */
697#define COMP_EBADSLT 11
698/* Endpoint Not Enabled Error */
699#define COMP_EBADEP 12
700/* Short Packet */
701#define COMP_SHORT_TX 13
702/* Ring Underrun - doorbell rung for an empty isoc OUT ep ring */
703#define COMP_UNDERRUN 14
704/* Ring Overrun - isoc IN ep ring is empty when ep is scheduled to RX */
705#define COMP_OVERRUN 15
706/* Virtual Function Event Ring Full Error */
707#define COMP_VF_FULL 16
708/* Parameter Error - Context parameter is invalid */
709#define COMP_EINVAL 17
710/* Bandwidth Overrun Error - isoc ep exceeded its allocated bandwidth */
711#define COMP_BW_OVER 18
712/* Context State Error - illegal context state transition requested */
713#define COMP_CTX_STATE 19
714/* No Ping Response Error - HC didn't get PING_RESPONSE in time to TX */
715#define COMP_PING_ERR 20
716/* Event Ring is full */
717#define COMP_ER_FULL 21
718/* Missed Service Error - HC couldn't service an isoc ep within interval */
719#define COMP_MISSED_INT 23
720/* Successfully stopped command ring */
721#define COMP_CMD_STOP 24
722/* Successfully aborted current command and stopped command ring */
723#define COMP_CMD_ABORT 25
724/* Stopped - transfer was terminated by a stop endpoint command */
725#define COMP_STOP 26
726/* Same as COMP_EP_STOPPED, but the transfered length in the event is invalid */
727#define COMP_STOP_INVAL 27
728/* Control Abort Error - Debug Capability - control pipe aborted */
729#define COMP_DBG_ABORT 28
730/* TRB type 29 and 30 reserved */
731/* Isoc Buffer Overrun - an isoc IN ep sent more data than could fit in TD */
732#define COMP_BUFF_OVER 31
733/* Event Lost Error - xHC has an "internal event overrun condition" */
734#define COMP_ISSUES 32
735/* Undefined Error - reported when other error codes don't apply */
736#define COMP_UNKNOWN 33
737/* Invalid Stream ID Error */
738#define COMP_STRID_ERR 34
739/* Secondary Bandwidth Error - may be returned by a Configure Endpoint cmd */
740/* FIXME - check for this */
741#define COMP_2ND_BW_ERR 35
742/* Split Transaction Error */
743#define COMP_SPLIT_ERR 36
744
745struct xhci_link_trb {
746 /* 64-bit segment pointer*/
747 u32 segment_ptr[2];
748 u32 intr_target;
749 u32 control;
750};
751
752/* control bitfields */
753#define LINK_TOGGLE (0x1<<1)
754
755/* Command completion event TRB */
756struct xhci_event_cmd {
757 /* Pointer to command TRB, or the value passed by the event data trb */
758 u32 cmd_trb[2];
759 u32 status;
760 u32 flags;
761};
762
763/* flags bitmasks */
764/* bits 16:23 are the virtual function ID */
765/* bits 24:31 are the slot ID */
766#define TRB_TO_SLOT_ID(p) (((p) & (0xff<<24)) >> 24)
767#define SLOT_ID_FOR_TRB(p) (((p) & 0xff) << 24)
768
769/* Stop Endpoint TRB - ep_index to endpoint ID for this TRB */
770#define TRB_TO_EP_INDEX(p) ((((p) & (0x1f << 16)) >> 16) - 1)
771#define EP_ID_FOR_TRB(p) ((((p) + 1) & 0x1f) << 16)
772
773
774/* Port Status Change Event TRB fields */
775/* Port ID - bits 31:24 */
776#define GET_PORT_ID(p) (((p) & (0xff << 24)) >> 24)
777
778/* Normal TRB fields */
779/* transfer_len bitmasks - bits 0:16 */
780#define TRB_LEN(p) ((p) & 0x1ffff)
781/* TD size - number of bytes remaining in the TD (including this TRB):
782 * bits 17 - 21. Shift the number of bytes by 10. */
783#define TD_REMAINDER(p) ((((p) >> 10) & 0x1f) << 17)
784/* Interrupter Target - which MSI-X vector to target the completion event at */
785#define TRB_INTR_TARGET(p) (((p) & 0x3ff) << 22)
786#define GET_INTR_TARGET(p) (((p) >> 22) & 0x3ff)
787
788/* Cycle bit - indicates TRB ownership by HC or HCD */
789#define TRB_CYCLE (1<<0)
790/*
791 * Force next event data TRB to be evaluated before task switch.
792 * Used to pass OS data back after a TD completes.
793 */
794#define TRB_ENT (1<<1)
795/* Interrupt on short packet */
796#define TRB_ISP (1<<2)
797/* Set PCIe no snoop attribute */
798#define TRB_NO_SNOOP (1<<3)
799/* Chain multiple TRBs into a TD */
800#define TRB_CHAIN (1<<4)
801/* Interrupt on completion */
802#define TRB_IOC (1<<5)
803/* The buffer pointer contains immediate data */
804#define TRB_IDT (1<<6)
805
806
807/* Control transfer TRB specific fields */
808#define TRB_DIR_IN (1<<16)
809
810struct xhci_generic_trb {
811 u32 field[4];
812};
813
814union xhci_trb {
815 struct xhci_link_trb link;
816 struct xhci_transfer_event trans_event;
817 struct xhci_event_cmd event_cmd;
818 struct xhci_generic_trb generic;
819};
820
821/* TRB bit mask */
822#define TRB_TYPE_BITMASK (0xfc00)
823#define TRB_TYPE(p) ((p) << 10)
824/* TRB type IDs */
825/* bulk, interrupt, isoc scatter/gather, and control data stage */
826#define TRB_NORMAL 1
827/* setup stage for control transfers */
828#define TRB_SETUP 2
829/* data stage for control transfers */
830#define TRB_DATA 3
831/* status stage for control transfers */
832#define TRB_STATUS 4
833/* isoc transfers */
834#define TRB_ISOC 5
835/* TRB for linking ring segments */
836#define TRB_LINK 6
837#define TRB_EVENT_DATA 7
838/* Transfer Ring No-op (not for the command ring) */
839#define TRB_TR_NOOP 8
840/* Command TRBs */
841/* Enable Slot Command */
842#define TRB_ENABLE_SLOT 9
843/* Disable Slot Command */
844#define TRB_DISABLE_SLOT 10
845/* Address Device Command */
846#define TRB_ADDR_DEV 11
847/* Configure Endpoint Command */
848#define TRB_CONFIG_EP 12
849/* Evaluate Context Command */
850#define TRB_EVAL_CONTEXT 13
851/* Reset Transfer Ring Command */
852#define TRB_RESET_RING 14
853/* Stop Transfer Ring Command */
854#define TRB_STOP_RING 15
855/* Set Transfer Ring Dequeue Pointer Command */
856#define TRB_SET_DEQ 16
857/* Reset Device Command */
858#define TRB_RESET_DEV 17
859/* Force Event Command (opt) */
860#define TRB_FORCE_EVENT 18
861/* Negotiate Bandwidth Command (opt) */
862#define TRB_NEG_BANDWIDTH 19
863/* Set Latency Tolerance Value Command (opt) */
864#define TRB_SET_LT 20
865/* Get port bandwidth Command */
866#define TRB_GET_BW 21
867/* Force Header Command - generate a transaction or link management packet */
868#define TRB_FORCE_HEADER 22
869/* No-op Command - not for transfer rings */
870#define TRB_CMD_NOOP 23
871/* TRB IDs 24-31 reserved */
872/* Event TRBS */
873/* Transfer Event */
874#define TRB_TRANSFER 32
875/* Command Completion Event */
876#define TRB_COMPLETION 33
877/* Port Status Change Event */
878#define TRB_PORT_STATUS 34
879/* Bandwidth Request Event (opt) */
880#define TRB_BANDWIDTH_EVENT 35
881/* Doorbell Event (opt) */
882#define TRB_DOORBELL 36
883/* Host Controller Event */
884#define TRB_HC_EVENT 37
885/* Device Notification Event - device sent function wake notification */
886#define TRB_DEV_NOTE 38
887/* MFINDEX Wrap Event - microframe counter wrapped */
888#define TRB_MFINDEX_WRAP 39
889/* TRB IDs 40-47 reserved, 48-63 is vendor-defined */
890
891/*
892 * TRBS_PER_SEGMENT must be a multiple of 4,
893 * since the command ring is 64-byte aligned.
894 * It must also be greater than 16.
895 */
896#define TRBS_PER_SEGMENT 64
897#define SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
898/* TRB buffer pointers can't cross 64KB boundaries */
899#define TRB_MAX_BUFF_SHIFT 16
900#define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
901
902struct xhci_segment {
903 union xhci_trb *trbs;
904 /* private to HCD */
905 struct xhci_segment *next;
906 dma_addr_t dma;
907};
908
909struct xhci_td {
910 struct list_head td_list;
911 struct list_head cancelled_td_list;
912 struct urb *urb;
913 struct xhci_segment *start_seg;
914 union xhci_trb *first_trb;
915 union xhci_trb *last_trb;
916};
917
918struct xhci_ring {
919 struct xhci_segment *first_seg;
920 union xhci_trb *enqueue;
921 struct xhci_segment *enq_seg;
922 unsigned int enq_updates;
923 union xhci_trb *dequeue;
924 struct xhci_segment *deq_seg;
925 unsigned int deq_updates;
926 struct list_head td_list;
927 /* ---- Related to URB cancellation ---- */
928 struct list_head cancelled_td_list;
929 unsigned int cancels_pending;
930 unsigned int state;
931#define SET_DEQ_PENDING (1 << 0)
932 /* The TRB that was last reported in a stopped endpoint ring */
933 union xhci_trb *stopped_trb;
934 struct xhci_td *stopped_td;
935 /*
936 * Write the cycle state into the TRB cycle field to give ownership of
937 * the TRB to the host controller (if we are the producer), or to check
938 * if we own the TRB (if we are the consumer). See section 4.9.1.
939 */
940 u32 cycle_state;
941};
942
943struct xhci_erst_entry {
944 /* 64-bit event ring segment address */
945 u32 seg_addr[2];
946 u32 seg_size;
947 /* Set to zero */
948 u32 rsvd;
949};
950
951struct xhci_erst {
952 struct xhci_erst_entry *entries;
953 unsigned int num_entries;
954 /* xhci->event_ring keeps track of segment dma addresses */
955 dma_addr_t erst_dma_addr;
956 /* Num entries the ERST can contain */
957 unsigned int erst_size;
958};
959
960/*
961 * Each segment table entry is 4*32bits long. 1K seems like an ok size:
962 * (1K bytes * 8bytes/bit) / (4*32 bits) = 64 segment entries in the table,
963 * meaning 64 ring segments.
964 * Initial allocated size of the ERST, in number of entries */
965#define ERST_NUM_SEGS 1
966/* Initial allocated size of the ERST, in number of entries */
967#define ERST_SIZE 64
968/* Initial number of event segment rings allocated */
969#define ERST_ENTRIES 1
970/* Poll every 60 seconds */
971#define POLL_TIMEOUT 60
972/* XXX: Make these module parameters */
973
974
975/* There is one ehci_hci structure per controller */
976struct xhci_hcd {
977 /* glue to PCI and HCD framework */
978 struct xhci_cap_regs __iomem *cap_regs;
979 struct xhci_op_regs __iomem *op_regs;
980 struct xhci_run_regs __iomem *run_regs;
981 struct xhci_doorbell_array __iomem *dba;
982 /* Our HCD's current interrupter register set */
983 struct xhci_intr_reg __iomem *ir_set;
984
985 /* Cached register copies of read-only HC data */
986 __u32 hcs_params1;
987 __u32 hcs_params2;
988 __u32 hcs_params3;
989 __u32 hcc_params;
990
991 spinlock_t lock;
992
993 /* packed release number */
994 u8 sbrn;
995 u16 hci_version;
996 u8 max_slots;
997 u8 max_interrupters;
998 u8 max_ports;
999 u8 isoc_threshold;
1000 int event_ring_max;
1001 int addr_64;
1002 /* 4KB min, 128MB max */
1003 int page_size;
1004 /* Valid values are 12 to 20, inclusive */
1005 int page_shift;
1006 /* only one MSI vector for now, but might need more later */
1007 int msix_count;
1008 struct msix_entry *msix_entries;
1009 /* data structures */
1010 struct xhci_device_context_array *dcbaa;
1011 struct xhci_ring *cmd_ring;
1012 struct xhci_ring *event_ring;
1013 struct xhci_erst erst;
1014 /* slot enabling and address device helpers */
1015 struct completion addr_dev;
1016 int slot_id;
1017 /* Internal mirror of the HW's dcbaa */
1018 struct xhci_virt_device *devs[MAX_HC_SLOTS];
1019
1020 /* DMA pools */
1021 struct dma_pool *device_pool;
1022 struct dma_pool *segment_pool;
1023
1024#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
1025 /* Poll the rings - for debugging */
1026 struct timer_list event_ring_timer;
1027 int zombie;
1028#endif
1029 /* Statistics */
1030 int noops_submitted;
1031 int noops_handled;
1032 int error_bitmask;
1033};
1034
1035/* For testing purposes */
1036#define NUM_TEST_NOOPS 0
1037
1038/* convert between an HCD pointer and the corresponding EHCI_HCD */
1039static inline struct xhci_hcd *hcd_to_xhci(struct usb_hcd *hcd)
1040{
1041 return (struct xhci_hcd *) (hcd->hcd_priv);
1042}
1043
1044static inline struct usb_hcd *xhci_to_hcd(struct xhci_hcd *xhci)
1045{
1046 return container_of((void *) xhci, struct usb_hcd, hcd_priv);
1047}
1048
1049#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
1050#define XHCI_DEBUG 1
1051#else
1052#define XHCI_DEBUG 0
1053#endif
1054
1055#define xhci_dbg(xhci, fmt, args...) \
1056 do { if (XHCI_DEBUG) dev_dbg(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
1057#define xhci_info(xhci, fmt, args...) \
1058 do { if (XHCI_DEBUG) dev_info(xhci_to_hcd(xhci)->self.controller , fmt , ## args); } while (0)
1059#define xhci_err(xhci, fmt, args...) \
1060 dev_err(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
1061#define xhci_warn(xhci, fmt, args...) \
1062 dev_warn(xhci_to_hcd(xhci)->self.controller , fmt , ## args)
1063
1064/* TODO: copied from ehci.h - can be refactored? */
1065/* xHCI spec says all registers are little endian */
1066static inline unsigned int xhci_readl(const struct xhci_hcd *xhci,
1067 __u32 __iomem *regs)
1068{
1069 return readl(regs);
1070}
1071static inline void xhci_writel(struct xhci_hcd *xhci,
1072 const unsigned int val, __u32 __iomem *regs)
1073{
1074 if (!in_interrupt())
1075 xhci_dbg(xhci,
1076 "`MEM_WRITE_DWORD(3'b000, 32'h%p, 32'h%0x, 4'hf);\n",
1077 regs, val);
1078 writel(val, regs);
1079}
1080
1081/* xHCI debugging */
1082void xhci_print_ir_set(struct xhci_hcd *xhci, struct xhci_intr_reg *ir_set, int set_num);
1083void xhci_print_registers(struct xhci_hcd *xhci);
1084void xhci_dbg_regs(struct xhci_hcd *xhci);
1085void xhci_print_run_regs(struct xhci_hcd *xhci);
1086void xhci_print_trb_offsets(struct xhci_hcd *xhci, union xhci_trb *trb);
1087void xhci_debug_trb(struct xhci_hcd *xhci, union xhci_trb *trb);
1088void xhci_debug_segment(struct xhci_hcd *xhci, struct xhci_segment *seg);
1089void xhci_debug_ring(struct xhci_hcd *xhci, struct xhci_ring *ring);
1090void xhci_dbg_erst(struct xhci_hcd *xhci, struct xhci_erst *erst);
1091void xhci_dbg_cmd_ptrs(struct xhci_hcd *xhci);
1092void xhci_dbg_ring_ptrs(struct xhci_hcd *xhci, struct xhci_ring *ring);
1093void xhci_dbg_ctx(struct xhci_hcd *xhci, struct xhci_device_control *ctx, dma_addr_t dma, unsigned int last_ep);
1094
1095/* xHCI memory managment */
1096void xhci_mem_cleanup(struct xhci_hcd *xhci);
1097int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
1098void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id);
1099int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, struct usb_device *udev, gfp_t flags);
1100int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev);
1101unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc);
1102unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
1103void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
1104int xhci_endpoint_init(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev,
1105 struct usb_device *udev, struct usb_host_endpoint *ep,
1106 gfp_t mem_flags);
1107void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring);
1108
1109#ifdef CONFIG_PCI
1110/* xHCI PCI glue */
1111int xhci_register_pci(void);
1112void xhci_unregister_pci(void);
1113#endif
1114
1115/* xHCI host controller glue */
1116int xhci_halt(struct xhci_hcd *xhci);
1117int xhci_reset(struct xhci_hcd *xhci);
1118int xhci_init(struct usb_hcd *hcd);
1119int xhci_run(struct usb_hcd *hcd);
1120void xhci_stop(struct usb_hcd *hcd);
1121void xhci_shutdown(struct usb_hcd *hcd);
1122int xhci_get_frame(struct usb_hcd *hcd);
1123irqreturn_t xhci_irq(struct usb_hcd *hcd);
1124int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev);
1125void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev);
1126int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev);
1127int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
1128int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
1129int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
1130int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, struct usb_host_endpoint *ep);
1131int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
1132void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
1133
1134/* xHCI ring, segment, TRB, and TD functions */
1135dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg, union xhci_trb *trb);
1136void xhci_ring_cmd_db(struct xhci_hcd *xhci);
1137void *xhci_setup_one_noop(struct xhci_hcd *xhci);
1138void xhci_handle_event(struct xhci_hcd *xhci);
1139void xhci_set_hc_event_deq(struct xhci_hcd *xhci);
1140int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id);
1141int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1142 u32 slot_id);
1143int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
1144 unsigned int ep_index);
1145int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
1146 int slot_id, unsigned int ep_index);
1147int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags, struct urb *urb,
1148 int slot_id, unsigned int ep_index);
1149int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
1150 u32 slot_id);
1151
1152/* xHCI roothub code */
1153int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, u16 wIndex,
1154 char *buf, u16 wLength);
1155int xhci_hub_status_data(struct usb_hcd *hcd, char *buf);
1156
1157#endif /* __LINUX_XHCI_HCD_H */
diff --git a/drivers/usb/misc/iowarrior.c b/drivers/usb/misc/iowarrior.c
index a4ef77ef917d..90e1a8dedfa9 100644
--- a/drivers/usb/misc/iowarrior.c
+++ b/drivers/usb/misc/iowarrior.c
@@ -18,6 +18,7 @@
18#include <linux/init.h> 18#include <linux/init.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/smp_lock.h>
21#include <linux/poll.h> 22#include <linux/poll.h>
22#include <linux/usb/iowarrior.h> 23#include <linux/usb/iowarrior.h>
23 24
@@ -726,12 +727,18 @@ static const struct file_operations iowarrior_fops = {
726 .poll = iowarrior_poll, 727 .poll = iowarrior_poll,
727}; 728};
728 729
730static char *iowarrior_nodename(struct device *dev)
731{
732 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
733}
734
729/* 735/*
730 * usb class driver info in order to get a minor number from the usb core, 736 * usb class driver info in order to get a minor number from the usb core,
731 * and to have the device registered with devfs and the driver core 737 * and to have the device registered with devfs and the driver core
732 */ 738 */
733static struct usb_class_driver iowarrior_class = { 739static struct usb_class_driver iowarrior_class = {
734 .name = "iowarrior%d", 740 .name = "iowarrior%d",
741 .nodename = iowarrior_nodename,
735 .fops = &iowarrior_fops, 742 .fops = &iowarrior_fops,
736 .minor_base = IOWARRIOR_MINOR_BASE, 743 .minor_base = IOWARRIOR_MINOR_BASE,
737}; 744};
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index ab0f3226158b..c1e2433f640d 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -266,12 +266,18 @@ static const struct file_operations tower_fops = {
266 .llseek = tower_llseek, 266 .llseek = tower_llseek,
267}; 267};
268 268
269static char *legousbtower_nodename(struct device *dev)
270{
271 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
272}
273
269/* 274/*
270 * usb class driver info in order to get a minor number from the usb core, 275 * usb class driver info in order to get a minor number from the usb core,
271 * and to have the device registered with the driver core 276 * and to have the device registered with the driver core
272 */ 277 */
273static struct usb_class_driver tower_class = { 278static struct usb_class_driver tower_class = {
274 .name = "legousbtower%d", 279 .name = "legousbtower%d",
280 .nodename = legousbtower_nodename,
275 .fops = &tower_fops, 281 .fops = &tower_fops,
276 .minor_base = LEGO_USB_TOWER_MINOR_BASE, 282 .minor_base = LEGO_USB_TOWER_MINOR_BASE,
277}; 283};
diff --git a/drivers/usb/misc/rio500.c b/drivers/usb/misc/rio500.c
index deb95bb49fd1..d645f3899fe1 100644
--- a/drivers/usb/misc/rio500.c
+++ b/drivers/usb/misc/rio500.c
@@ -32,6 +32,7 @@
32#include <linux/kernel.h> 32#include <linux/kernel.h>
33#include <linux/signal.h> 33#include <linux/signal.h>
34#include <linux/sched.h> 34#include <linux/sched.h>
35#include <linux/smp_lock.h>
35#include <linux/errno.h> 36#include <linux/errno.h>
36#include <linux/random.h> 37#include <linux/random.h>
37#include <linux/poll.h> 38#include <linux/poll.h>
diff --git a/drivers/usb/misc/sisusbvga/Kconfig b/drivers/usb/misc/sisusbvga/Kconfig
index 7603cbe0865d..30ea7ca6846e 100644
--- a/drivers/usb/misc/sisusbvga/Kconfig
+++ b/drivers/usb/misc/sisusbvga/Kconfig
@@ -1,7 +1,7 @@
1 1
2config USB_SISUSBVGA 2config USB_SISUSBVGA
3 tristate "USB 2.0 SVGA dongle support (Net2280/SiS315)" 3 tristate "USB 2.0 SVGA dongle support (Net2280/SiS315)"
4 depends on USB && USB_EHCI_HCD 4 depends on USB && (USB_MUSB_HDRC || USB_EHCI_HCD)
5 ---help--- 5 ---help---
6 Say Y here if you intend to attach a USB2VGA dongle based on a 6 Say Y here if you intend to attach a USB2VGA dongle based on a
7 Net2280 and a SiS315 chip. 7 Net2280 and a SiS315 chip.
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index e0ff9ccd866b..29092b8e59ce 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -16,6 +16,7 @@
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/smp_lock.h>
19#include <linux/errno.h> 20#include <linux/errno.h>
20#include <linux/mutex.h> 21#include <linux/mutex.h>
21#include <asm/uaccess.h> 22#include <asm/uaccess.h>
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index 5f1a19d1497d..a9f06d76960f 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -1072,23 +1072,34 @@ static int unlink1 (struct usbtest_dev *dev, int pipe, int size, int async)
1072 */ 1072 */
1073 msleep (jiffies % (2 * INTERRUPT_RATE)); 1073 msleep (jiffies % (2 * INTERRUPT_RATE));
1074 if (async) { 1074 if (async) {
1075retry: 1075 while (!completion_done(&completion)) {
1076 retval = usb_unlink_urb (urb); 1076 retval = usb_unlink_urb(urb);
1077 if (retval == -EBUSY || retval == -EIDRM) { 1077
1078 /* we can't unlink urbs while they're completing. 1078 switch (retval) {
1079 * or if they've completed, and we haven't resubmitted. 1079 case -EBUSY:
1080 * "normal" drivers would prevent resubmission, but 1080 case -EIDRM:
1081 * since we're testing unlink paths, we can't. 1081 /* we can't unlink urbs while they're completing
1082 */ 1082 * or if they've completed, and we haven't
1083 ERROR(dev, "unlink retry\n"); 1083 * resubmitted. "normal" drivers would prevent
1084 goto retry; 1084 * resubmission, but since we're testing unlink
1085 * paths, we can't.
1086 */
1087 ERROR(dev, "unlink retry\n");
1088 continue;
1089 case 0:
1090 case -EINPROGRESS:
1091 break;
1092
1093 default:
1094 dev_err(&dev->intf->dev,
1095 "unlink fail %d\n", retval);
1096 return retval;
1097 }
1098
1099 break;
1085 } 1100 }
1086 } else 1101 } else
1087 usb_kill_urb (urb); 1102 usb_kill_urb (urb);
1088 if (!(retval == 0 || retval == -EINPROGRESS)) {
1089 dev_err(&dev->intf->dev, "unlink fail %d\n", retval);
1090 return retval;
1091 }
1092 1103
1093 wait_for_completion (&completion); 1104 wait_for_completion (&completion);
1094 retval = urb->status; 1105 retval = urb->status;
diff --git a/drivers/usb/mon/mon_bin.c b/drivers/usb/mon/mon_bin.c
index f8d9045d668a..0f7a30b7d2d1 100644
--- a/drivers/usb/mon/mon_bin.c
+++ b/drivers/usb/mon/mon_bin.c
@@ -1261,7 +1261,7 @@ static int mon_alloc_buff(struct mon_pgmap *map, int npages)
1261 return -ENOMEM; 1261 return -ENOMEM;
1262 } 1262 }
1263 map[n].ptr = (unsigned char *) vaddr; 1263 map[n].ptr = (unsigned char *) vaddr;
1264 map[n].pg = virt_to_page(vaddr); 1264 map[n].pg = virt_to_page((void *) vaddr);
1265 } 1265 }
1266 return 0; 1266 return 0;
1267} 1267}
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 1f715436d6d3..a7eb4c99342c 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -733,7 +733,7 @@ int __init mon_text_init(void)
733{ 733{
734 struct dentry *mondir; 734 struct dentry *mondir;
735 735
736 mondir = debugfs_create_dir("usbmon", NULL); 736 mondir = debugfs_create_dir("usbmon", usb_debug_root);
737 if (IS_ERR(mondir)) { 737 if (IS_ERR(mondir)) {
738 printk(KERN_NOTICE TAG ": debugfs is not available\n"); 738 printk(KERN_NOTICE TAG ": debugfs is not available\n");
739 return -ENODEV; 739 return -ENODEV;
diff --git a/drivers/usb/musb/Kconfig b/drivers/usb/musb/Kconfig
index b66e8544d8b9..70073b157f0a 100644
--- a/drivers/usb/musb/Kconfig
+++ b/drivers/usb/musb/Kconfig
@@ -10,6 +10,7 @@ comment "Enable Host or Gadget support to see Inventra options"
10config USB_MUSB_HDRC 10config USB_MUSB_HDRC
11 depends on (USB || USB_GADGET) && HAVE_CLK 11 depends on (USB || USB_GADGET) && HAVE_CLK
12 depends on !SUPERH 12 depends on !SUPERH
13 select NOP_USB_XCEIV if ARCH_DAVINCI
13 select TWL4030_USB if MACH_OMAP_3430SDP 14 select TWL4030_USB if MACH_OMAP_3430SDP
14 select USB_OTG_UTILS 15 select USB_OTG_UTILS
15 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)' 16 tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
@@ -55,6 +56,7 @@ comment "Blackfin high speed USB Support"
55config USB_TUSB6010 56config USB_TUSB6010
56 boolean "TUSB 6010 support" 57 boolean "TUSB 6010 support"
57 depends on USB_MUSB_HDRC && !USB_MUSB_SOC 58 depends on USB_MUSB_HDRC && !USB_MUSB_SOC
59 select NOP_USB_XCEIV
58 default y 60 default y
59 help 61 help
60 The TUSB 6010 chip, from Texas Instruments, connects a discrete 62 The TUSB 6010 chip, from Texas Instruments, connects a discrete
diff --git a/drivers/usb/musb/blackfin.c b/drivers/usb/musb/blackfin.c
index 786134852092..f2f66ebc7362 100644
--- a/drivers/usb/musb/blackfin.c
+++ b/drivers/usb/musb/blackfin.c
@@ -143,7 +143,7 @@ static void musb_conn_timer_handler(unsigned long _musb)
143 u16 val; 143 u16 val;
144 144
145 spin_lock_irqsave(&musb->lock, flags); 145 spin_lock_irqsave(&musb->lock, flags);
146 switch (musb->xceiv.state) { 146 switch (musb->xceiv->state) {
147 case OTG_STATE_A_IDLE: 147 case OTG_STATE_A_IDLE:
148 case OTG_STATE_A_WAIT_BCON: 148 case OTG_STATE_A_WAIT_BCON:
149 /* Start a new session */ 149 /* Start a new session */
@@ -154,7 +154,7 @@ static void musb_conn_timer_handler(unsigned long _musb)
154 val = musb_readw(musb->mregs, MUSB_DEVCTL); 154 val = musb_readw(musb->mregs, MUSB_DEVCTL);
155 if (!(val & MUSB_DEVCTL_BDEVICE)) { 155 if (!(val & MUSB_DEVCTL_BDEVICE)) {
156 gpio_set_value(musb->config->gpio_vrsel, 1); 156 gpio_set_value(musb->config->gpio_vrsel, 1);
157 musb->xceiv.state = OTG_STATE_A_WAIT_BCON; 157 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
158 } else { 158 } else {
159 gpio_set_value(musb->config->gpio_vrsel, 0); 159 gpio_set_value(musb->config->gpio_vrsel, 0);
160 160
@@ -247,6 +247,11 @@ int __init musb_platform_init(struct musb *musb)
247 } 247 }
248 gpio_direction_output(musb->config->gpio_vrsel, 0); 248 gpio_direction_output(musb->config->gpio_vrsel, 0);
249 249
250 usb_nop_xceiv_register();
251 musb->xceiv = otg_get_transceiver();
252 if (!musb->xceiv)
253 return -ENODEV;
254
250 if (ANOMALY_05000346) { 255 if (ANOMALY_05000346) {
251 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value); 256 bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
252 SSYNC(); 257 SSYNC();
@@ -291,7 +296,7 @@ int __init musb_platform_init(struct musb *musb)
291 musb_conn_timer_handler, (unsigned long) musb); 296 musb_conn_timer_handler, (unsigned long) musb);
292 } 297 }
293 if (is_peripheral_enabled(musb)) 298 if (is_peripheral_enabled(musb))
294 musb->xceiv.set_power = bfin_set_power; 299 musb->xceiv->set_power = bfin_set_power;
295 300
296 musb->isr = blackfin_interrupt; 301 musb->isr = blackfin_interrupt;
297 302
diff --git a/drivers/usb/musb/cppi_dma.c b/drivers/usb/musb/cppi_dma.c
index 1976e9b41800..c3577bbbae6c 100644
--- a/drivers/usb/musb/cppi_dma.c
+++ b/drivers/usb/musb/cppi_dma.c
@@ -6,6 +6,7 @@
6 * The TUSB6020, using VLYNQ, has CPPI that looks much like DaVinci. 6 * The TUSB6020, using VLYNQ, has CPPI that looks much like DaVinci.
7 */ 7 */
8 8
9#include <linux/platform_device.h>
9#include <linux/usb.h> 10#include <linux/usb.h>
10 11
11#include "musb_core.h" 12#include "musb_core.h"
@@ -1145,17 +1146,27 @@ static bool cppi_rx_scan(struct cppi *cppi, unsigned ch)
1145 return completed; 1146 return completed;
1146} 1147}
1147 1148
1148void cppi_completion(struct musb *musb, u32 rx, u32 tx) 1149irqreturn_t cppi_interrupt(int irq, void *dev_id)
1149{ 1150{
1150 void __iomem *tibase; 1151 struct musb *musb = dev_id;
1151 int i, index;
1152 struct cppi *cppi; 1152 struct cppi *cppi;
1153 void __iomem *tibase;
1153 struct musb_hw_ep *hw_ep = NULL; 1154 struct musb_hw_ep *hw_ep = NULL;
1155 u32 rx, tx;
1156 int i, index;
1154 1157
1155 cppi = container_of(musb->dma_controller, struct cppi, controller); 1158 cppi = container_of(musb->dma_controller, struct cppi, controller);
1156 1159
1157 tibase = musb->ctrl_base; 1160 tibase = musb->ctrl_base;
1158 1161
1162 tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
1163 rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
1164
1165 if (!tx && !rx)
1166 return IRQ_NONE;
1167
1168 DBG(4, "CPPI IRQ Tx%x Rx%x\n", tx, rx);
1169
1159 /* process TX channels */ 1170 /* process TX channels */
1160 for (index = 0; tx; tx = tx >> 1, index++) { 1171 for (index = 0; tx; tx = tx >> 1, index++) {
1161 struct cppi_channel *tx_ch; 1172 struct cppi_channel *tx_ch;
@@ -1273,6 +1284,8 @@ void cppi_completion(struct musb *musb, u32 rx, u32 tx)
1273 1284
1274 /* write to CPPI EOI register to re-enable interrupts */ 1285 /* write to CPPI EOI register to re-enable interrupts */
1275 musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0); 1286 musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0);
1287
1288 return IRQ_HANDLED;
1276} 1289}
1277 1290
1278/* Instantiate a software object representing a DMA controller. */ 1291/* Instantiate a software object representing a DMA controller. */
@@ -1280,6 +1293,9 @@ struct dma_controller *__init
1280dma_controller_create(struct musb *musb, void __iomem *mregs) 1293dma_controller_create(struct musb *musb, void __iomem *mregs)
1281{ 1294{
1282 struct cppi *controller; 1295 struct cppi *controller;
1296 struct device *dev = musb->controller;
1297 struct platform_device *pdev = to_platform_device(dev);
1298 int irq = platform_get_irq(pdev, 1);
1283 1299
1284 controller = kzalloc(sizeof *controller, GFP_KERNEL); 1300 controller = kzalloc(sizeof *controller, GFP_KERNEL);
1285 if (!controller) 1301 if (!controller)
@@ -1310,6 +1326,15 @@ dma_controller_create(struct musb *musb, void __iomem *mregs)
1310 return NULL; 1326 return NULL;
1311 } 1327 }
1312 1328
1329 if (irq > 0) {
1330 if (request_irq(irq, cppi_interrupt, 0, "cppi-dma", musb)) {
1331 dev_err(dev, "request_irq %d failed!\n", irq);
1332 dma_controller_destroy(&controller->controller);
1333 return NULL;
1334 }
1335 controller->irq = irq;
1336 }
1337
1313 return &controller->controller; 1338 return &controller->controller;
1314} 1339}
1315 1340
@@ -1322,6 +1347,9 @@ void dma_controller_destroy(struct dma_controller *c)
1322 1347
1323 cppi = container_of(c, struct cppi, controller); 1348 cppi = container_of(c, struct cppi, controller);
1324 1349
1350 if (cppi->irq)
1351 free_irq(cppi->irq, cppi->musb);
1352
1325 /* assert: caller stopped the controller first */ 1353 /* assert: caller stopped the controller first */
1326 dma_pool_destroy(cppi->pool); 1354 dma_pool_destroy(cppi->pool);
1327 1355
diff --git a/drivers/usb/musb/cppi_dma.h b/drivers/usb/musb/cppi_dma.h
index 729b4071787b..59bf949e589b 100644
--- a/drivers/usb/musb/cppi_dma.h
+++ b/drivers/usb/musb/cppi_dma.h
@@ -5,7 +5,6 @@
5 5
6#include <linux/slab.h> 6#include <linux/slab.h>
7#include <linux/list.h> 7#include <linux/list.h>
8#include <linux/smp_lock.h>
9#include <linux/errno.h> 8#include <linux/errno.h>
10#include <linux/dmapool.h> 9#include <linux/dmapool.h>
11 10
@@ -119,6 +118,8 @@ struct cppi {
119 void __iomem *mregs; /* Mentor regs */ 118 void __iomem *mregs; /* Mentor regs */
120 void __iomem *tibase; /* TI/CPPI regs */ 119 void __iomem *tibase; /* TI/CPPI regs */
121 120
121 int irq;
122
122 struct cppi_channel tx[4]; 123 struct cppi_channel tx[4];
123 struct cppi_channel rx[4]; 124 struct cppi_channel rx[4];
124 125
@@ -127,7 +128,7 @@ struct cppi {
127 struct list_head tx_complete; 128 struct list_head tx_complete;
128}; 129};
129 130
130/* irq handling hook */ 131/* CPPI IRQ handler */
131extern void cppi_completion(struct musb *, u32 rx, u32 tx); 132extern irqreturn_t cppi_interrupt(int, void *);
132 133
133#endif /* end of ifndef _CPPI_DMA_H_ */ 134#endif /* end of ifndef _CPPI_DMA_H_ */
diff --git a/drivers/usb/musb/davinci.c b/drivers/usb/musb/davinci.c
index 10d11ab113ab..e16ff605c458 100644
--- a/drivers/usb/musb/davinci.c
+++ b/drivers/usb/musb/davinci.c
@@ -35,13 +35,14 @@
35#include <mach/hardware.h> 35#include <mach/hardware.h>
36#include <mach/memory.h> 36#include <mach/memory.h>
37#include <mach/gpio.h> 37#include <mach/gpio.h>
38#include <mach/cputype.h>
38 39
39#include <asm/mach-types.h> 40#include <asm/mach-types.h>
40 41
41#include "musb_core.h" 42#include "musb_core.h"
42 43
43#ifdef CONFIG_MACH_DAVINCI_EVM 44#ifdef CONFIG_MACH_DAVINCI_EVM
44#define GPIO_nVBUS_DRV 87 45#define GPIO_nVBUS_DRV 144
45#endif 46#endif
46 47
47#include "davinci.h" 48#include "davinci.h"
@@ -215,7 +216,7 @@ static void otg_timer(unsigned long _musb)
215 DBG(7, "poll devctl %02x (%s)\n", devctl, otg_state_string(musb)); 216 DBG(7, "poll devctl %02x (%s)\n", devctl, otg_state_string(musb));
216 217
217 spin_lock_irqsave(&musb->lock, flags); 218 spin_lock_irqsave(&musb->lock, flags);
218 switch (musb->xceiv.state) { 219 switch (musb->xceiv->state) {
219 case OTG_STATE_A_WAIT_VFALL: 220 case OTG_STATE_A_WAIT_VFALL:
220 /* Wait till VBUS falls below SessionEnd (~0.2V); the 1.3 RTL 221 /* Wait till VBUS falls below SessionEnd (~0.2V); the 1.3 RTL
221 * seems to mis-handle session "start" otherwise (or in our 222 * seems to mis-handle session "start" otherwise (or in our
@@ -226,7 +227,7 @@ static void otg_timer(unsigned long _musb)
226 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ); 227 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
227 break; 228 break;
228 } 229 }
229 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE; 230 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
230 musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG, 231 musb_writel(musb->ctrl_base, DAVINCI_USB_INT_SET_REG,
231 MUSB_INTR_VBUSERROR << DAVINCI_USB_USBINT_SHIFT); 232 MUSB_INTR_VBUSERROR << DAVINCI_USB_USBINT_SHIFT);
232 break; 233 break;
@@ -251,7 +252,7 @@ static void otg_timer(unsigned long _musb)
251 if (devctl & MUSB_DEVCTL_BDEVICE) 252 if (devctl & MUSB_DEVCTL_BDEVICE)
252 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ); 253 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
253 else 254 else
254 musb->xceiv.state = OTG_STATE_A_IDLE; 255 musb->xceiv->state = OTG_STATE_A_IDLE;
255 break; 256 break;
256 default: 257 default:
257 break; 258 break;
@@ -265,6 +266,7 @@ static irqreturn_t davinci_interrupt(int irq, void *__hci)
265 irqreturn_t retval = IRQ_NONE; 266 irqreturn_t retval = IRQ_NONE;
266 struct musb *musb = __hci; 267 struct musb *musb = __hci;
267 void __iomem *tibase = musb->ctrl_base; 268 void __iomem *tibase = musb->ctrl_base;
269 struct cppi *cppi;
268 u32 tmp; 270 u32 tmp;
269 271
270 spin_lock_irqsave(&musb->lock, flags); 272 spin_lock_irqsave(&musb->lock, flags);
@@ -281,16 +283,9 @@ static irqreturn_t davinci_interrupt(int irq, void *__hci)
281 /* CPPI interrupts share the same IRQ line, but have their own 283 /* CPPI interrupts share the same IRQ line, but have their own
282 * mask, state, "vector", and EOI registers. 284 * mask, state, "vector", and EOI registers.
283 */ 285 */
284 if (is_cppi_enabled()) { 286 cppi = container_of(musb->dma_controller, struct cppi, controller);
285 u32 cppi_tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG); 287 if (is_cppi_enabled() && musb->dma_controller && !cppi->irq)
286 u32 cppi_rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG); 288 retval = cppi_interrupt(irq, __hci);
287
288 if (cppi_tx || cppi_rx) {
289 DBG(4, "CPPI IRQ t%x r%x\n", cppi_tx, cppi_rx);
290 cppi_completion(musb, cppi_rx, cppi_tx);
291 retval = IRQ_HANDLED;
292 }
293 }
294 289
295 /* ack and handle non-CPPI interrupts */ 290 /* ack and handle non-CPPI interrupts */
296 tmp = musb_readl(tibase, DAVINCI_USB_INT_SRC_MASKED_REG); 291 tmp = musb_readl(tibase, DAVINCI_USB_INT_SRC_MASKED_REG);
@@ -331,25 +326,26 @@ static irqreturn_t davinci_interrupt(int irq, void *__hci)
331 * to stop registering in devctl. 326 * to stop registering in devctl.
332 */ 327 */
333 musb->int_usb &= ~MUSB_INTR_VBUSERROR; 328 musb->int_usb &= ~MUSB_INTR_VBUSERROR;
334 musb->xceiv.state = OTG_STATE_A_WAIT_VFALL; 329 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
335 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ); 330 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
336 WARNING("VBUS error workaround (delay coming)\n"); 331 WARNING("VBUS error workaround (delay coming)\n");
337 } else if (is_host_enabled(musb) && drvvbus) { 332 } else if (is_host_enabled(musb) && drvvbus) {
338 musb->is_active = 1;
339 MUSB_HST_MODE(musb); 333 MUSB_HST_MODE(musb);
340 musb->xceiv.default_a = 1; 334 musb->xceiv->default_a = 1;
341 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE; 335 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
342 portstate(musb->port1_status |= USB_PORT_STAT_POWER); 336 portstate(musb->port1_status |= USB_PORT_STAT_POWER);
343 del_timer(&otg_workaround); 337 del_timer(&otg_workaround);
344 } else { 338 } else {
345 musb->is_active = 0; 339 musb->is_active = 0;
346 MUSB_DEV_MODE(musb); 340 MUSB_DEV_MODE(musb);
347 musb->xceiv.default_a = 0; 341 musb->xceiv->default_a = 0;
348 musb->xceiv.state = OTG_STATE_B_IDLE; 342 musb->xceiv->state = OTG_STATE_B_IDLE;
349 portstate(musb->port1_status &= ~USB_PORT_STAT_POWER); 343 portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
350 } 344 }
351 345
352 /* NOTE: this must complete poweron within 100 msec */ 346 /* NOTE: this must complete poweron within 100 msec
347 * (OTG_TIME_A_WAIT_VRISE) but we don't check for that.
348 */
353 davinci_source_power(musb, drvvbus, 0); 349 davinci_source_power(musb, drvvbus, 0);
354 DBG(2, "VBUS %s (%s)%s, devctl %02x\n", 350 DBG(2, "VBUS %s (%s)%s, devctl %02x\n",
355 drvvbus ? "on" : "off", 351 drvvbus ? "on" : "off",
@@ -367,17 +363,12 @@ static irqreturn_t davinci_interrupt(int irq, void *__hci)
367 363
368 /* poll for ID change */ 364 /* poll for ID change */
369 if (is_otg_enabled(musb) 365 if (is_otg_enabled(musb)
370 && musb->xceiv.state == OTG_STATE_B_IDLE) 366 && musb->xceiv->state == OTG_STATE_B_IDLE)
371 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ); 367 mod_timer(&otg_workaround, jiffies + POLL_SECONDS * HZ);
372 368
373 spin_unlock_irqrestore(&musb->lock, flags); 369 spin_unlock_irqrestore(&musb->lock, flags);
374 370
375 /* REVISIT we sometimes get unhandled IRQs 371 return retval;
376 * (e.g. ep0). not clear why...
377 */
378 if (retval != IRQ_HANDLED)
379 DBG(5, "unhandled? %08x\n", tmp);
380 return IRQ_HANDLED;
381} 372}
382 373
383int musb_platform_set_mode(struct musb *musb, u8 mode) 374int musb_platform_set_mode(struct musb *musb, u8 mode)
@@ -391,6 +382,11 @@ int __init musb_platform_init(struct musb *musb)
391 void __iomem *tibase = musb->ctrl_base; 382 void __iomem *tibase = musb->ctrl_base;
392 u32 revision; 383 u32 revision;
393 384
385 usb_nop_xceiv_register();
386 musb->xceiv = otg_get_transceiver();
387 if (!musb->xceiv)
388 return -ENODEV;
389
394 musb->mregs += DAVINCI_BASE_OFFSET; 390 musb->mregs += DAVINCI_BASE_OFFSET;
395 391
396 clk_enable(musb->clock); 392 clk_enable(musb->clock);
@@ -398,7 +394,7 @@ int __init musb_platform_init(struct musb *musb)
398 /* returns zero if e.g. not clocked */ 394 /* returns zero if e.g. not clocked */
399 revision = musb_readl(tibase, DAVINCI_USB_VERSION_REG); 395 revision = musb_readl(tibase, DAVINCI_USB_VERSION_REG);
400 if (revision == 0) 396 if (revision == 0)
401 return -ENODEV; 397 goto fail;
402 398
403 if (is_host_enabled(musb)) 399 if (is_host_enabled(musb))
404 setup_timer(&otg_workaround, otg_timer, (unsigned long) musb); 400 setup_timer(&otg_workaround, otg_timer, (unsigned long) musb);
@@ -417,6 +413,21 @@ int __init musb_platform_init(struct musb *musb)
417 __raw_writel(phy_ctrl, USB_PHY_CTRL); 413 __raw_writel(phy_ctrl, USB_PHY_CTRL);
418 } 414 }
419 415
416 /* On dm355, the default-A state machine needs DRVVBUS control.
417 * If we won't be a host, there's no need to turn it on.
418 */
419 if (cpu_is_davinci_dm355()) {
420 u32 deepsleep = __raw_readl(DM355_DEEPSLEEP);
421
422 if (is_host_enabled(musb)) {
423 deepsleep &= ~DRVVBUS_OVERRIDE;
424 } else {
425 deepsleep &= ~DRVVBUS_FORCE;
426 deepsleep |= DRVVBUS_OVERRIDE;
427 }
428 __raw_writel(deepsleep, DM355_DEEPSLEEP);
429 }
430
420 /* reset the controller */ 431 /* reset the controller */
421 musb_writel(tibase, DAVINCI_USB_CTRL_REG, 0x1); 432 musb_writel(tibase, DAVINCI_USB_CTRL_REG, 0x1);
422 433
@@ -432,6 +443,10 @@ int __init musb_platform_init(struct musb *musb)
432 443
433 musb->isr = davinci_interrupt; 444 musb->isr = davinci_interrupt;
434 return 0; 445 return 0;
446
447fail:
448 usb_nop_xceiv_unregister();
449 return -ENODEV;
435} 450}
436 451
437int musb_platform_exit(struct musb *musb) 452int musb_platform_exit(struct musb *musb)
@@ -439,10 +454,19 @@ int musb_platform_exit(struct musb *musb)
439 if (is_host_enabled(musb)) 454 if (is_host_enabled(musb))
440 del_timer_sync(&otg_workaround); 455 del_timer_sync(&otg_workaround);
441 456
457 /* force VBUS off */
458 if (cpu_is_davinci_dm355()) {
459 u32 deepsleep = __raw_readl(DM355_DEEPSLEEP);
460
461 deepsleep &= ~DRVVBUS_FORCE;
462 deepsleep |= DRVVBUS_OVERRIDE;
463 __raw_writel(deepsleep, DM355_DEEPSLEEP);
464 }
465
442 davinci_source_power(musb, 0 /*off*/, 1); 466 davinci_source_power(musb, 0 /*off*/, 1);
443 467
444 /* delay, to avoid problems with module reload */ 468 /* delay, to avoid problems with module reload */
445 if (is_host_enabled(musb) && musb->xceiv.default_a) { 469 if (is_host_enabled(musb) && musb->xceiv->default_a) {
446 int maxdelay = 30; 470 int maxdelay = 30;
447 u8 devctl, warn = 0; 471 u8 devctl, warn = 0;
448 472
@@ -471,5 +495,7 @@ int musb_platform_exit(struct musb *musb)
471 495
472 clk_disable(musb->clock); 496 clk_disable(musb->clock);
473 497
498 usb_nop_xceiv_unregister();
499
474 return 0; 500 return 0;
475} 501}
diff --git a/drivers/usb/musb/musb_core.c b/drivers/usb/musb/musb_core.c
index 4000cf6d1e81..554a414f65d1 100644
--- a/drivers/usb/musb/musb_core.c
+++ b/drivers/usb/musb/musb_core.c
@@ -112,6 +112,7 @@
112#include "davinci.h" 112#include "davinci.h"
113#endif 113#endif
114 114
115#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
115 116
116 117
117unsigned musb_debug; 118unsigned musb_debug;
@@ -267,7 +268,7 @@ void musb_load_testpacket(struct musb *musb)
267 268
268const char *otg_state_string(struct musb *musb) 269const char *otg_state_string(struct musb *musb)
269{ 270{
270 switch (musb->xceiv.state) { 271 switch (musb->xceiv->state) {
271 case OTG_STATE_A_IDLE: return "a_idle"; 272 case OTG_STATE_A_IDLE: return "a_idle";
272 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise"; 273 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
273 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon"; 274 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
@@ -288,12 +289,6 @@ const char *otg_state_string(struct musb *musb)
288#ifdef CONFIG_USB_MUSB_OTG 289#ifdef CONFIG_USB_MUSB_OTG
289 290
290/* 291/*
291 * See also USB_OTG_1-3.pdf 6.6.5 Timers
292 * REVISIT: Are the other timers done in the hardware?
293 */
294#define TB_ASE0_BRST 100 /* Min 3.125 ms */
295
296/*
297 * Handles OTG hnp timeouts, such as b_ase0_brst 292 * Handles OTG hnp timeouts, such as b_ase0_brst
298 */ 293 */
299void musb_otg_timer_func(unsigned long data) 294void musb_otg_timer_func(unsigned long data)
@@ -302,16 +297,18 @@ void musb_otg_timer_func(unsigned long data)
302 unsigned long flags; 297 unsigned long flags;
303 298
304 spin_lock_irqsave(&musb->lock, flags); 299 spin_lock_irqsave(&musb->lock, flags);
305 switch (musb->xceiv.state) { 300 switch (musb->xceiv->state) {
306 case OTG_STATE_B_WAIT_ACON: 301 case OTG_STATE_B_WAIT_ACON:
307 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n"); 302 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
308 musb_g_disconnect(musb); 303 musb_g_disconnect(musb);
309 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 304 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
310 musb->is_active = 0; 305 musb->is_active = 0;
311 break; 306 break;
307 case OTG_STATE_A_SUSPEND:
312 case OTG_STATE_A_WAIT_BCON: 308 case OTG_STATE_A_WAIT_BCON:
313 DBG(1, "HNP: a_wait_bcon timeout; back to a_host\n"); 309 DBG(1, "HNP: %s timeout\n", otg_state_string(musb));
314 musb_hnp_stop(musb); 310 musb_set_vbus(musb, 0);
311 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
315 break; 312 break;
316 default: 313 default:
317 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb)); 314 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb));
@@ -320,10 +317,8 @@ void musb_otg_timer_func(unsigned long data)
320 spin_unlock_irqrestore(&musb->lock, flags); 317 spin_unlock_irqrestore(&musb->lock, flags);
321} 318}
322 319
323static DEFINE_TIMER(musb_otg_timer, musb_otg_timer_func, 0, 0);
324
325/* 320/*
326 * Stops the B-device HNP state. Caller must take care of locking. 321 * Stops the HNP transition. Caller must take care of locking.
327 */ 322 */
328void musb_hnp_stop(struct musb *musb) 323void musb_hnp_stop(struct musb *musb)
329{ 324{
@@ -331,20 +326,17 @@ void musb_hnp_stop(struct musb *musb)
331 void __iomem *mbase = musb->mregs; 326 void __iomem *mbase = musb->mregs;
332 u8 reg; 327 u8 reg;
333 328
334 switch (musb->xceiv.state) { 329 DBG(1, "HNP: stop from %s\n", otg_state_string(musb));
330
331 switch (musb->xceiv->state) {
335 case OTG_STATE_A_PERIPHERAL: 332 case OTG_STATE_A_PERIPHERAL:
336 case OTG_STATE_A_WAIT_VFALL:
337 case OTG_STATE_A_WAIT_BCON:
338 DBG(1, "HNP: Switching back to A-host\n");
339 musb_g_disconnect(musb); 333 musb_g_disconnect(musb);
340 musb->xceiv.state = OTG_STATE_A_IDLE; 334 DBG(1, "HNP: back to %s\n", otg_state_string(musb));
341 MUSB_HST_MODE(musb);
342 musb->is_active = 0;
343 break; 335 break;
344 case OTG_STATE_B_HOST: 336 case OTG_STATE_B_HOST:
345 DBG(1, "HNP: Disabling HR\n"); 337 DBG(1, "HNP: Disabling HR\n");
346 hcd->self.is_b_host = 0; 338 hcd->self.is_b_host = 0;
347 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 339 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
348 MUSB_DEV_MODE(musb); 340 MUSB_DEV_MODE(musb);
349 reg = musb_readb(mbase, MUSB_POWER); 341 reg = musb_readb(mbase, MUSB_POWER);
350 reg |= MUSB_POWER_SUSPENDM; 342 reg |= MUSB_POWER_SUSPENDM;
@@ -402,7 +394,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
402 394
403 if (devctl & MUSB_DEVCTL_HM) { 395 if (devctl & MUSB_DEVCTL_HM) {
404#ifdef CONFIG_USB_MUSB_HDRC_HCD 396#ifdef CONFIG_USB_MUSB_HDRC_HCD
405 switch (musb->xceiv.state) { 397 switch (musb->xceiv->state) {
406 case OTG_STATE_A_SUSPEND: 398 case OTG_STATE_A_SUSPEND:
407 /* remote wakeup? later, GetPortStatus 399 /* remote wakeup? later, GetPortStatus
408 * will stop RESUME signaling 400 * will stop RESUME signaling
@@ -425,12 +417,12 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
425 musb->rh_timer = jiffies 417 musb->rh_timer = jiffies
426 + msecs_to_jiffies(20); 418 + msecs_to_jiffies(20);
427 419
428 musb->xceiv.state = OTG_STATE_A_HOST; 420 musb->xceiv->state = OTG_STATE_A_HOST;
429 musb->is_active = 1; 421 musb->is_active = 1;
430 usb_hcd_resume_root_hub(musb_to_hcd(musb)); 422 usb_hcd_resume_root_hub(musb_to_hcd(musb));
431 break; 423 break;
432 case OTG_STATE_B_WAIT_ACON: 424 case OTG_STATE_B_WAIT_ACON:
433 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 425 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
434 musb->is_active = 1; 426 musb->is_active = 1;
435 MUSB_DEV_MODE(musb); 427 MUSB_DEV_MODE(musb);
436 break; 428 break;
@@ -441,11 +433,11 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
441 } 433 }
442#endif 434#endif
443 } else { 435 } else {
444 switch (musb->xceiv.state) { 436 switch (musb->xceiv->state) {
445#ifdef CONFIG_USB_MUSB_HDRC_HCD 437#ifdef CONFIG_USB_MUSB_HDRC_HCD
446 case OTG_STATE_A_SUSPEND: 438 case OTG_STATE_A_SUSPEND:
447 /* possibly DISCONNECT is upcoming */ 439 /* possibly DISCONNECT is upcoming */
448 musb->xceiv.state = OTG_STATE_A_HOST; 440 musb->xceiv->state = OTG_STATE_A_HOST;
449 usb_hcd_resume_root_hub(musb_to_hcd(musb)); 441 usb_hcd_resume_root_hub(musb_to_hcd(musb));
450 break; 442 break;
451#endif 443#endif
@@ -490,7 +482,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
490 */ 482 */
491 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION); 483 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
492 musb->ep0_stage = MUSB_EP0_START; 484 musb->ep0_stage = MUSB_EP0_START;
493 musb->xceiv.state = OTG_STATE_A_IDLE; 485 musb->xceiv->state = OTG_STATE_A_IDLE;
494 MUSB_HST_MODE(musb); 486 MUSB_HST_MODE(musb);
495 musb_set_vbus(musb, 1); 487 musb_set_vbus(musb, 1);
496 488
@@ -516,7 +508,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
516 * REVISIT: do delays from lots of DEBUG_KERNEL checks 508 * REVISIT: do delays from lots of DEBUG_KERNEL checks
517 * make trouble here, keeping VBUS < 4.4V ? 509 * make trouble here, keeping VBUS < 4.4V ?
518 */ 510 */
519 switch (musb->xceiv.state) { 511 switch (musb->xceiv->state) {
520 case OTG_STATE_A_HOST: 512 case OTG_STATE_A_HOST:
521 /* recovery is dicey once we've gotten past the 513 /* recovery is dicey once we've gotten past the
522 * initial stages of enumeration, but if VBUS 514 * initial stages of enumeration, but if VBUS
@@ -594,37 +586,40 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
594 if (devctl & MUSB_DEVCTL_LSDEV) 586 if (devctl & MUSB_DEVCTL_LSDEV)
595 musb->port1_status |= USB_PORT_STAT_LOW_SPEED; 587 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
596 588
597 if (hcd->status_urb)
598 usb_hcd_poll_rh_status(hcd);
599 else
600 usb_hcd_resume_root_hub(hcd);
601
602 MUSB_HST_MODE(musb);
603
604 /* indicate new connection to OTG machine */ 589 /* indicate new connection to OTG machine */
605 switch (musb->xceiv.state) { 590 switch (musb->xceiv->state) {
606 case OTG_STATE_B_PERIPHERAL: 591 case OTG_STATE_B_PERIPHERAL:
607 if (int_usb & MUSB_INTR_SUSPEND) { 592 if (int_usb & MUSB_INTR_SUSPEND) {
608 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n"); 593 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
609 musb->xceiv.state = OTG_STATE_B_HOST;
610 hcd->self.is_b_host = 1;
611 int_usb &= ~MUSB_INTR_SUSPEND; 594 int_usb &= ~MUSB_INTR_SUSPEND;
595 goto b_host;
612 } else 596 } else
613 DBG(1, "CONNECT as b_peripheral???\n"); 597 DBG(1, "CONNECT as b_peripheral???\n");
614 break; 598 break;
615 case OTG_STATE_B_WAIT_ACON: 599 case OTG_STATE_B_WAIT_ACON:
616 DBG(1, "HNP: Waiting to switch to b_host state\n"); 600 DBG(1, "HNP: CONNECT, now b_host\n");
617 musb->xceiv.state = OTG_STATE_B_HOST; 601b_host:
602 musb->xceiv->state = OTG_STATE_B_HOST;
618 hcd->self.is_b_host = 1; 603 hcd->self.is_b_host = 1;
604 musb->ignore_disconnect = 0;
605 del_timer(&musb->otg_timer);
619 break; 606 break;
620 default: 607 default:
621 if ((devctl & MUSB_DEVCTL_VBUS) 608 if ((devctl & MUSB_DEVCTL_VBUS)
622 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) { 609 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
623 musb->xceiv.state = OTG_STATE_A_HOST; 610 musb->xceiv->state = OTG_STATE_A_HOST;
624 hcd->self.is_b_host = 0; 611 hcd->self.is_b_host = 0;
625 } 612 }
626 break; 613 break;
627 } 614 }
615
616 /* poke the root hub */
617 MUSB_HST_MODE(musb);
618 if (hcd->status_urb)
619 usb_hcd_poll_rh_status(hcd);
620 else
621 usb_hcd_resume_root_hub(hcd);
622
628 DBG(1, "CONNECT (%s) devctl %02x\n", 623 DBG(1, "CONNECT (%s) devctl %02x\n",
629 otg_state_string(musb), devctl); 624 otg_state_string(musb), devctl);
630 } 625 }
@@ -650,7 +645,7 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
650 } 645 }
651 } else if (is_peripheral_capable()) { 646 } else if (is_peripheral_capable()) {
652 DBG(1, "BUS RESET as %s\n", otg_state_string(musb)); 647 DBG(1, "BUS RESET as %s\n", otg_state_string(musb));
653 switch (musb->xceiv.state) { 648 switch (musb->xceiv->state) {
654#ifdef CONFIG_USB_OTG 649#ifdef CONFIG_USB_OTG
655 case OTG_STATE_A_SUSPEND: 650 case OTG_STATE_A_SUSPEND:
656 /* We need to ignore disconnect on suspend 651 /* We need to ignore disconnect on suspend
@@ -661,24 +656,27 @@ static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
661 musb_g_reset(musb); 656 musb_g_reset(musb);
662 /* FALLTHROUGH */ 657 /* FALLTHROUGH */
663 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */ 658 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
664 DBG(1, "HNP: Setting timer as %s\n", 659 /* never use invalid T(a_wait_bcon) */
665 otg_state_string(musb)); 660 DBG(1, "HNP: in %s, %d msec timeout\n",
666 musb_otg_timer.data = (unsigned long)musb; 661 otg_state_string(musb),
667 mod_timer(&musb_otg_timer, jiffies 662 TA_WAIT_BCON(musb));
668 + msecs_to_jiffies(100)); 663 mod_timer(&musb->otg_timer, jiffies
664 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
669 break; 665 break;
670 case OTG_STATE_A_PERIPHERAL: 666 case OTG_STATE_A_PERIPHERAL:
671 musb_hnp_stop(musb); 667 musb->ignore_disconnect = 0;
668 del_timer(&musb->otg_timer);
669 musb_g_reset(musb);
672 break; 670 break;
673 case OTG_STATE_B_WAIT_ACON: 671 case OTG_STATE_B_WAIT_ACON:
674 DBG(1, "HNP: RESET (%s), to b_peripheral\n", 672 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
675 otg_state_string(musb)); 673 otg_state_string(musb));
676 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 674 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
677 musb_g_reset(musb); 675 musb_g_reset(musb);
678 break; 676 break;
679#endif 677#endif
680 case OTG_STATE_B_IDLE: 678 case OTG_STATE_B_IDLE:
681 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 679 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
682 /* FALLTHROUGH */ 680 /* FALLTHROUGH */
683 case OTG_STATE_B_PERIPHERAL: 681 case OTG_STATE_B_PERIPHERAL:
684 musb_g_reset(musb); 682 musb_g_reset(musb);
@@ -763,7 +761,7 @@ static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
763 MUSB_MODE(musb), devctl); 761 MUSB_MODE(musb), devctl);
764 handled = IRQ_HANDLED; 762 handled = IRQ_HANDLED;
765 763
766 switch (musb->xceiv.state) { 764 switch (musb->xceiv->state) {
767#ifdef CONFIG_USB_MUSB_HDRC_HCD 765#ifdef CONFIG_USB_MUSB_HDRC_HCD
768 case OTG_STATE_A_HOST: 766 case OTG_STATE_A_HOST:
769 case OTG_STATE_A_SUSPEND: 767 case OTG_STATE_A_SUSPEND:
@@ -776,7 +774,16 @@ static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
776#endif /* HOST */ 774#endif /* HOST */
777#ifdef CONFIG_USB_MUSB_OTG 775#ifdef CONFIG_USB_MUSB_OTG
778 case OTG_STATE_B_HOST: 776 case OTG_STATE_B_HOST:
779 musb_hnp_stop(musb); 777 /* REVISIT this behaves for "real disconnect"
778 * cases; make sure the other transitions from
779 * from B_HOST act right too. The B_HOST code
780 * in hnp_stop() is currently not used...
781 */
782 musb_root_disconnect(musb);
783 musb_to_hcd(musb)->self.is_b_host = 0;
784 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
785 MUSB_DEV_MODE(musb);
786 musb_g_disconnect(musb);
780 break; 787 break;
781 case OTG_STATE_A_PERIPHERAL: 788 case OTG_STATE_A_PERIPHERAL:
782 musb_hnp_stop(musb); 789 musb_hnp_stop(musb);
@@ -805,26 +812,35 @@ static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
805 otg_state_string(musb), devctl, power); 812 otg_state_string(musb), devctl, power);
806 handled = IRQ_HANDLED; 813 handled = IRQ_HANDLED;
807 814
808 switch (musb->xceiv.state) { 815 switch (musb->xceiv->state) {
809#ifdef CONFIG_USB_MUSB_OTG 816#ifdef CONFIG_USB_MUSB_OTG
810 case OTG_STATE_A_PERIPHERAL: 817 case OTG_STATE_A_PERIPHERAL:
811 /* 818 /* We also come here if the cable is removed, since
812 * We cannot stop HNP here, devctl BDEVICE might be 819 * this silicon doesn't report ID-no-longer-grounded.
813 * still set. 820 *
821 * We depend on T(a_wait_bcon) to shut us down, and
822 * hope users don't do anything dicey during this
823 * undesired detour through A_WAIT_BCON.
814 */ 824 */
825 musb_hnp_stop(musb);
826 usb_hcd_resume_root_hub(musb_to_hcd(musb));
827 musb_root_disconnect(musb);
828 musb_platform_try_idle(musb, jiffies
829 + msecs_to_jiffies(musb->a_wait_bcon
830 ? : OTG_TIME_A_WAIT_BCON));
815 break; 831 break;
816#endif 832#endif
817 case OTG_STATE_B_PERIPHERAL: 833 case OTG_STATE_B_PERIPHERAL:
818 musb_g_suspend(musb); 834 musb_g_suspend(musb);
819 musb->is_active = is_otg_enabled(musb) 835 musb->is_active = is_otg_enabled(musb)
820 && musb->xceiv.gadget->b_hnp_enable; 836 && musb->xceiv->gadget->b_hnp_enable;
821 if (musb->is_active) { 837 if (musb->is_active) {
822#ifdef CONFIG_USB_MUSB_OTG 838#ifdef CONFIG_USB_MUSB_OTG
823 musb->xceiv.state = OTG_STATE_B_WAIT_ACON; 839 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
824 DBG(1, "HNP: Setting timer for b_ase0_brst\n"); 840 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
825 musb_otg_timer.data = (unsigned long)musb; 841 mod_timer(&musb->otg_timer, jiffies
826 mod_timer(&musb_otg_timer, jiffies 842 + msecs_to_jiffies(
827 + msecs_to_jiffies(TB_ASE0_BRST)); 843 OTG_TIME_B_ASE0_BRST));
828#endif 844#endif
829 } 845 }
830 break; 846 break;
@@ -834,9 +850,9 @@ static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
834 + msecs_to_jiffies(musb->a_wait_bcon)); 850 + msecs_to_jiffies(musb->a_wait_bcon));
835 break; 851 break;
836 case OTG_STATE_A_HOST: 852 case OTG_STATE_A_HOST:
837 musb->xceiv.state = OTG_STATE_A_SUSPEND; 853 musb->xceiv->state = OTG_STATE_A_SUSPEND;
838 musb->is_active = is_otg_enabled(musb) 854 musb->is_active = is_otg_enabled(musb)
839 && musb->xceiv.host->b_hnp_enable; 855 && musb->xceiv->host->b_hnp_enable;
840 break; 856 break;
841 case OTG_STATE_B_HOST: 857 case OTG_STATE_B_HOST:
842 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */ 858 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
@@ -1068,14 +1084,13 @@ static struct fifo_cfg __initdata mode_4_cfg[] = {
1068{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, }, 1084{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1069{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, }, 1085{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1070{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, }, 1086{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
1071{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 512, }, 1087{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1072{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 512, }, 1088{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1073{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 512, }, 1089{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1074{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 512, }, 1090{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1075{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 512, }, 1091{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1076{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 512, }, 1092{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1077{ .hw_ep_num = 13, .style = FIFO_TX, .maxpacket = 512, }, 1093{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
1078{ .hw_ep_num = 13, .style = FIFO_RX, .maxpacket = 512, },
1079{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, }, 1094{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1080{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, }, 1095{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1081}; 1096};
@@ -1335,11 +1350,11 @@ static int __init musb_core_init(u16 musb_type, struct musb *musb)
1335 } 1350 }
1336 if (reg & MUSB_CONFIGDATA_HBRXE) { 1351 if (reg & MUSB_CONFIGDATA_HBRXE) {
1337 strcat(aInfo, ", HB-ISO Rx"); 1352 strcat(aInfo, ", HB-ISO Rx");
1338 strcat(aInfo, " (X)"); /* no driver support */ 1353 musb->hb_iso_rx = true;
1339 } 1354 }
1340 if (reg & MUSB_CONFIGDATA_HBTXE) { 1355 if (reg & MUSB_CONFIGDATA_HBTXE) {
1341 strcat(aInfo, ", HB-ISO Tx"); 1356 strcat(aInfo, ", HB-ISO Tx");
1342 strcat(aInfo, " (X)"); /* no driver support */ 1357 musb->hb_iso_tx = true;
1343 } 1358 }
1344 if (reg & MUSB_CONFIGDATA_SOFTCONE) 1359 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1345 strcat(aInfo, ", SoftConn"); 1360 strcat(aInfo, ", SoftConn");
@@ -1481,13 +1496,7 @@ static irqreturn_t generic_interrupt(int irq, void *__hci)
1481 1496
1482 spin_unlock_irqrestore(&musb->lock, flags); 1497 spin_unlock_irqrestore(&musb->lock, flags);
1483 1498
1484 /* REVISIT we sometimes get spurious IRQs on g_ep0 1499 return retval;
1485 * not clear why...
1486 */
1487 if (retval != IRQ_HANDLED)
1488 DBG(5, "spurious?\n");
1489
1490 return IRQ_HANDLED;
1491} 1500}
1492 1501
1493#else 1502#else
@@ -1687,8 +1696,9 @@ musb_vbus_store(struct device *dev, struct device_attribute *attr,
1687 } 1696 }
1688 1697
1689 spin_lock_irqsave(&musb->lock, flags); 1698 spin_lock_irqsave(&musb->lock, flags);
1690 musb->a_wait_bcon = val; 1699 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1691 if (musb->xceiv.state == OTG_STATE_A_WAIT_BCON) 1700 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
1701 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
1692 musb->is_active = 0; 1702 musb->is_active = 0;
1693 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val)); 1703 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1694 spin_unlock_irqrestore(&musb->lock, flags); 1704 spin_unlock_irqrestore(&musb->lock, flags);
@@ -1706,10 +1716,13 @@ musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1706 1716
1707 spin_lock_irqsave(&musb->lock, flags); 1717 spin_lock_irqsave(&musb->lock, flags);
1708 val = musb->a_wait_bcon; 1718 val = musb->a_wait_bcon;
1719 /* FIXME get_vbus_status() is normally #defined as false...
1720 * and is effectively TUSB-specific.
1721 */
1709 vbus = musb_platform_get_vbus_status(musb); 1722 vbus = musb_platform_get_vbus_status(musb);
1710 spin_unlock_irqrestore(&musb->lock, flags); 1723 spin_unlock_irqrestore(&musb->lock, flags);
1711 1724
1712 return sprintf(buf, "Vbus %s, timeout %lu\n", 1725 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
1713 vbus ? "on" : "off", val); 1726 vbus ? "on" : "off", val);
1714} 1727}
1715static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store); 1728static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
@@ -1749,8 +1762,8 @@ static void musb_irq_work(struct work_struct *data)
1749 struct musb *musb = container_of(data, struct musb, irq_work); 1762 struct musb *musb = container_of(data, struct musb, irq_work);
1750 static int old_state; 1763 static int old_state;
1751 1764
1752 if (musb->xceiv.state != old_state) { 1765 if (musb->xceiv->state != old_state) {
1753 old_state = musb->xceiv.state; 1766 old_state = musb->xceiv->state;
1754 sysfs_notify(&musb->controller->kobj, NULL, "mode"); 1767 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1755 } 1768 }
1756} 1769}
@@ -1782,6 +1795,7 @@ allocate_instance(struct device *dev,
1782 hcd->uses_new_polling = 1; 1795 hcd->uses_new_polling = 1;
1783 1796
1784 musb->vbuserr_retry = VBUSERR_RETRY_COUNT; 1797 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
1798 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
1785#else 1799#else
1786 musb = kzalloc(sizeof *musb, GFP_KERNEL); 1800 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1787 if (!musb) 1801 if (!musb)
@@ -1847,7 +1861,7 @@ static void musb_free(struct musb *musb)
1847 } 1861 }
1848 1862
1849#ifdef CONFIG_USB_MUSB_OTG 1863#ifdef CONFIG_USB_MUSB_OTG
1850 put_device(musb->xceiv.dev); 1864 put_device(musb->xceiv->dev);
1851#endif 1865#endif
1852 1866
1853#ifdef CONFIG_USB_MUSB_HDRC_HCD 1867#ifdef CONFIG_USB_MUSB_HDRC_HCD
@@ -1928,10 +1942,18 @@ bad_config:
1928 } 1942 }
1929 } 1943 }
1930 1944
1931 /* assume vbus is off */ 1945 /* The musb_platform_init() call:
1932 1946 * - adjusts musb->mregs and musb->isr if needed,
1933 /* platform adjusts musb->mregs and musb->isr if needed, 1947 * - may initialize an integrated tranceiver
1934 * and activates clocks 1948 * - initializes musb->xceiv, usually by otg_get_transceiver()
1949 * - activates clocks.
1950 * - stops powering VBUS
1951 * - assigns musb->board_set_vbus if host mode is enabled
1952 *
1953 * There are various transciever configurations. Blackfin,
1954 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1955 * external/discrete ones in various flavors (twl4030 family,
1956 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
1935 */ 1957 */
1936 musb->isr = generic_interrupt; 1958 musb->isr = generic_interrupt;
1937 status = musb_platform_init(musb); 1959 status = musb_platform_init(musb);
@@ -1968,6 +1990,10 @@ bad_config:
1968 if (status < 0) 1990 if (status < 0)
1969 goto fail2; 1991 goto fail2;
1970 1992
1993#ifdef CONFIG_USB_OTG
1994 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
1995#endif
1996
1971 /* Init IRQ workqueue before request_irq */ 1997 /* Init IRQ workqueue before request_irq */
1972 INIT_WORK(&musb->irq_work, musb_irq_work); 1998 INIT_WORK(&musb->irq_work, musb_irq_work);
1973 1999
@@ -1999,17 +2025,17 @@ bad_config:
1999 ? "DMA" : "PIO", 2025 ? "DMA" : "PIO",
2000 musb->nIrq); 2026 musb->nIrq);
2001 2027
2002#ifdef CONFIG_USB_MUSB_HDRC_HCD 2028 /* host side needs more setup */
2003 /* host side needs more setup, except for no-host modes */ 2029 if (is_host_enabled(musb)) {
2004 if (musb->board_mode != MUSB_PERIPHERAL) {
2005 struct usb_hcd *hcd = musb_to_hcd(musb); 2030 struct usb_hcd *hcd = musb_to_hcd(musb);
2006 2031
2007 if (musb->board_mode == MUSB_OTG) 2032 otg_set_host(musb->xceiv, &hcd->self);
2033
2034 if (is_otg_enabled(musb))
2008 hcd->self.otg_port = 1; 2035 hcd->self.otg_port = 1;
2009 musb->xceiv.host = &hcd->self; 2036 musb->xceiv->host = &hcd->self;
2010 hcd->power_budget = 2 * (plat->power ? : 250); 2037 hcd->power_budget = 2 * (plat->power ? : 250);
2011 } 2038 }
2012#endif /* CONFIG_USB_MUSB_HDRC_HCD */
2013 2039
2014 /* For the host-only role, we can activate right away. 2040 /* For the host-only role, we can activate right away.
2015 * (We expect the ID pin to be forcibly grounded!!) 2041 * (We expect the ID pin to be forcibly grounded!!)
@@ -2017,8 +2043,8 @@ bad_config:
2017 */ 2043 */
2018 if (!is_otg_enabled(musb) && is_host_enabled(musb)) { 2044 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2019 MUSB_HST_MODE(musb); 2045 MUSB_HST_MODE(musb);
2020 musb->xceiv.default_a = 1; 2046 musb->xceiv->default_a = 1;
2021 musb->xceiv.state = OTG_STATE_A_IDLE; 2047 musb->xceiv->state = OTG_STATE_A_IDLE;
2022 2048
2023 status = usb_add_hcd(musb_to_hcd(musb), -1, 0); 2049 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2024 if (status) 2050 if (status)
@@ -2033,8 +2059,8 @@ bad_config:
2033 2059
2034 } else /* peripheral is enabled */ { 2060 } else /* peripheral is enabled */ {
2035 MUSB_DEV_MODE(musb); 2061 MUSB_DEV_MODE(musb);
2036 musb->xceiv.default_a = 0; 2062 musb->xceiv->default_a = 0;
2037 musb->xceiv.state = OTG_STATE_B_IDLE; 2063 musb->xceiv->state = OTG_STATE_B_IDLE;
2038 2064
2039 status = musb_gadget_setup(musb); 2065 status = musb_gadget_setup(musb);
2040 if (status) 2066 if (status)
diff --git a/drivers/usb/musb/musb_core.h b/drivers/usb/musb/musb_core.h
index efb39b5e55b5..381d648a36b8 100644
--- a/drivers/usb/musb/musb_core.h
+++ b/drivers/usb/musb/musb_core.h
@@ -38,8 +38,8 @@
38#include <linux/slab.h> 38#include <linux/slab.h>
39#include <linux/list.h> 39#include <linux/list.h>
40#include <linux/interrupt.h> 40#include <linux/interrupt.h>
41#include <linux/smp_lock.h>
42#include <linux/errno.h> 41#include <linux/errno.h>
42#include <linux/timer.h>
43#include <linux/clk.h> 43#include <linux/clk.h>
44#include <linux/device.h> 44#include <linux/device.h>
45#include <linux/usb/ch9.h> 45#include <linux/usb/ch9.h>
@@ -171,7 +171,8 @@ enum musb_h_ep0_state {
171 171
172/* peripheral side ep0 states */ 172/* peripheral side ep0 states */
173enum musb_g_ep0_state { 173enum musb_g_ep0_state {
174 MUSB_EP0_STAGE_SETUP, /* idle, waiting for setup */ 174 MUSB_EP0_STAGE_IDLE, /* idle, waiting for SETUP */
175 MUSB_EP0_STAGE_SETUP, /* received SETUP */
175 MUSB_EP0_STAGE_TX, /* IN data */ 176 MUSB_EP0_STAGE_TX, /* IN data */
176 MUSB_EP0_STAGE_RX, /* OUT data */ 177 MUSB_EP0_STAGE_RX, /* OUT data */
177 MUSB_EP0_STAGE_STATUSIN, /* (after OUT data) */ 178 MUSB_EP0_STAGE_STATUSIN, /* (after OUT data) */
@@ -179,10 +180,15 @@ enum musb_g_ep0_state {
179 MUSB_EP0_STAGE_ACKWAIT, /* after zlp, before statusin */ 180 MUSB_EP0_STAGE_ACKWAIT, /* after zlp, before statusin */
180} __attribute__ ((packed)); 181} __attribute__ ((packed));
181 182
182/* OTG protocol constants */ 183/*
184 * OTG protocol constants. See USB OTG 1.3 spec,
185 * sections 5.5 "Device Timings" and 6.6.5 "Timers".
186 */
183#define OTG_TIME_A_WAIT_VRISE 100 /* msec (max) */ 187#define OTG_TIME_A_WAIT_VRISE 100 /* msec (max) */
184#define OTG_TIME_A_WAIT_BCON 0 /* 0=infinite; min 1000 msec */ 188#define OTG_TIME_A_WAIT_BCON 1100 /* min 1 second */
185#define OTG_TIME_A_IDLE_BDIS 200 /* msec (min) */ 189#define OTG_TIME_A_AIDL_BDIS 200 /* min 200 msec */
190#define OTG_TIME_B_ASE0_BRST 100 /* min 3.125 ms */
191
186 192
187/*************************** REGISTER ACCESS ********************************/ 193/*************************** REGISTER ACCESS ********************************/
188 194
@@ -331,6 +337,8 @@ struct musb {
331 struct list_head control; /* of musb_qh */ 337 struct list_head control; /* of musb_qh */
332 struct list_head in_bulk; /* of musb_qh */ 338 struct list_head in_bulk; /* of musb_qh */
333 struct list_head out_bulk; /* of musb_qh */ 339 struct list_head out_bulk; /* of musb_qh */
340
341 struct timer_list otg_timer;
334#endif 342#endif
335 343
336 /* called with IRQs blocked; ON/nonzero implies starting a session, 344 /* called with IRQs blocked; ON/nonzero implies starting a session,
@@ -355,7 +363,7 @@ struct musb {
355 u16 int_rx; 363 u16 int_rx;
356 u16 int_tx; 364 u16 int_tx;
357 365
358 struct otg_transceiver xceiv; 366 struct otg_transceiver *xceiv;
359 367
360 int nIrq; 368 int nIrq;
361 unsigned irq_wake:1; 369 unsigned irq_wake:1;
@@ -386,6 +394,9 @@ struct musb {
386 unsigned is_multipoint:1; 394 unsigned is_multipoint:1;
387 unsigned ignore_disconnect:1; /* during bus resets */ 395 unsigned ignore_disconnect:1; /* during bus resets */
388 396
397 unsigned hb_iso_rx:1; /* high bandwidth iso rx? */
398 unsigned hb_iso_tx:1; /* high bandwidth iso tx? */
399
389#ifdef C_MP_TX 400#ifdef C_MP_TX
390 unsigned bulk_split:1; 401 unsigned bulk_split:1;
391#define can_bulk_split(musb,type) \ 402#define can_bulk_split(musb,type) \
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index f79440cdfe7e..8b3c4e2ed7b8 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -310,7 +310,7 @@ static void txstate(struct musb *musb, struct musb_request *req)
310 /* setup DMA, then program endpoint CSR */ 310 /* setup DMA, then program endpoint CSR */
311 request_size = min(request->length, 311 request_size = min(request->length,
312 musb_ep->dma->max_len); 312 musb_ep->dma->max_len);
313 if (request_size <= musb_ep->packet_sz) 313 if (request_size < musb_ep->packet_sz)
314 musb_ep->dma->desired_mode = 0; 314 musb_ep->dma->desired_mode = 0;
315 else 315 else
316 musb_ep->dma->desired_mode = 1; 316 musb_ep->dma->desired_mode = 1;
@@ -349,7 +349,8 @@ static void txstate(struct musb *musb, struct musb_request *req)
349#elif defined(CONFIG_USB_TI_CPPI_DMA) 349#elif defined(CONFIG_USB_TI_CPPI_DMA)
350 /* program endpoint CSR first, then setup DMA */ 350 /* program endpoint CSR first, then setup DMA */
351 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY); 351 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
352 csr |= MUSB_TXCSR_MODE | MUSB_TXCSR_DMAENAB; 352 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
353 MUSB_TXCSR_MODE;
353 musb_writew(epio, MUSB_TXCSR, 354 musb_writew(epio, MUSB_TXCSR,
354 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN) 355 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
355 | csr); 356 | csr);
@@ -1405,7 +1406,7 @@ static int musb_gadget_wakeup(struct usb_gadget *gadget)
1405 1406
1406 spin_lock_irqsave(&musb->lock, flags); 1407 spin_lock_irqsave(&musb->lock, flags);
1407 1408
1408 switch (musb->xceiv.state) { 1409 switch (musb->xceiv->state) {
1409 case OTG_STATE_B_PERIPHERAL: 1410 case OTG_STATE_B_PERIPHERAL:
1410 /* NOTE: OTG state machine doesn't include B_SUSPENDED; 1411 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1411 * that's part of the standard usb 1.1 state machine, and 1412 * that's part of the standard usb 1.1 state machine, and
@@ -1507,9 +1508,9 @@ static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1507{ 1508{
1508 struct musb *musb = gadget_to_musb(gadget); 1509 struct musb *musb = gadget_to_musb(gadget);
1509 1510
1510 if (!musb->xceiv.set_power) 1511 if (!musb->xceiv->set_power)
1511 return -EOPNOTSUPP; 1512 return -EOPNOTSUPP;
1512 return otg_set_power(&musb->xceiv, mA); 1513 return otg_set_power(musb->xceiv, mA);
1513} 1514}
1514 1515
1515static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on) 1516static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
@@ -1732,11 +1733,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1732 1733
1733 spin_lock_irqsave(&musb->lock, flags); 1734 spin_lock_irqsave(&musb->lock, flags);
1734 1735
1735 /* REVISIT always use otg_set_peripheral(), handling 1736 otg_set_peripheral(musb->xceiv, &musb->g);
1736 * issues including the root hub one below ...
1737 */
1738 musb->xceiv.gadget = &musb->g;
1739 musb->xceiv.state = OTG_STATE_B_IDLE;
1740 musb->is_active = 1; 1737 musb->is_active = 1;
1741 1738
1742 /* FIXME this ignores the softconnect flag. Drivers are 1739 /* FIXME this ignores the softconnect flag. Drivers are
@@ -1748,6 +1745,8 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1748 if (!is_otg_enabled(musb)) 1745 if (!is_otg_enabled(musb))
1749 musb_start(musb); 1746 musb_start(musb);
1750 1747
1748 otg_set_peripheral(musb->xceiv, &musb->g);
1749
1751 spin_unlock_irqrestore(&musb->lock, flags); 1750 spin_unlock_irqrestore(&musb->lock, flags);
1752 1751
1753 if (is_otg_enabled(musb)) { 1752 if (is_otg_enabled(musb)) {
@@ -1761,8 +1760,7 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1761 if (retval < 0) { 1760 if (retval < 0) {
1762 DBG(1, "add_hcd failed, %d\n", retval); 1761 DBG(1, "add_hcd failed, %d\n", retval);
1763 spin_lock_irqsave(&musb->lock, flags); 1762 spin_lock_irqsave(&musb->lock, flags);
1764 musb->xceiv.gadget = NULL; 1763 otg_set_peripheral(musb->xceiv, NULL);
1765 musb->xceiv.state = OTG_STATE_UNDEFINED;
1766 musb->gadget_driver = NULL; 1764 musb->gadget_driver = NULL;
1767 musb->g.dev.driver = NULL; 1765 musb->g.dev.driver = NULL;
1768 spin_unlock_irqrestore(&musb->lock, flags); 1766 spin_unlock_irqrestore(&musb->lock, flags);
@@ -1845,8 +1843,9 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1845 1843
1846 (void) musb_gadget_vbus_draw(&musb->g, 0); 1844 (void) musb_gadget_vbus_draw(&musb->g, 0);
1847 1845
1848 musb->xceiv.state = OTG_STATE_UNDEFINED; 1846 musb->xceiv->state = OTG_STATE_UNDEFINED;
1849 stop_activity(musb, driver); 1847 stop_activity(musb, driver);
1848 otg_set_peripheral(musb->xceiv, NULL);
1850 1849
1851 DBG(3, "unregistering driver %s\n", driver->function); 1850 DBG(3, "unregistering driver %s\n", driver->function);
1852 spin_unlock_irqrestore(&musb->lock, flags); 1851 spin_unlock_irqrestore(&musb->lock, flags);
@@ -1882,7 +1881,7 @@ EXPORT_SYMBOL(usb_gadget_unregister_driver);
1882void musb_g_resume(struct musb *musb) 1881void musb_g_resume(struct musb *musb)
1883{ 1882{
1884 musb->is_suspended = 0; 1883 musb->is_suspended = 0;
1885 switch (musb->xceiv.state) { 1884 switch (musb->xceiv->state) {
1886 case OTG_STATE_B_IDLE: 1885 case OTG_STATE_B_IDLE:
1887 break; 1886 break;
1888 case OTG_STATE_B_WAIT_ACON: 1887 case OTG_STATE_B_WAIT_ACON:
@@ -1908,10 +1907,10 @@ void musb_g_suspend(struct musb *musb)
1908 devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 1907 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1909 DBG(3, "devctl %02x\n", devctl); 1908 DBG(3, "devctl %02x\n", devctl);
1910 1909
1911 switch (musb->xceiv.state) { 1910 switch (musb->xceiv->state) {
1912 case OTG_STATE_B_IDLE: 1911 case OTG_STATE_B_IDLE:
1913 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) 1912 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
1914 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 1913 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
1915 break; 1914 break;
1916 case OTG_STATE_B_PERIPHERAL: 1915 case OTG_STATE_B_PERIPHERAL:
1917 musb->is_suspended = 1; 1916 musb->is_suspended = 1;
@@ -1957,22 +1956,24 @@ void musb_g_disconnect(struct musb *musb)
1957 spin_lock(&musb->lock); 1956 spin_lock(&musb->lock);
1958 } 1957 }
1959 1958
1960 switch (musb->xceiv.state) { 1959 switch (musb->xceiv->state) {
1961 default: 1960 default:
1962#ifdef CONFIG_USB_MUSB_OTG 1961#ifdef CONFIG_USB_MUSB_OTG
1963 DBG(2, "Unhandled disconnect %s, setting a_idle\n", 1962 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
1964 otg_state_string(musb)); 1963 otg_state_string(musb));
1965 musb->xceiv.state = OTG_STATE_A_IDLE; 1964 musb->xceiv->state = OTG_STATE_A_IDLE;
1965 MUSB_HST_MODE(musb);
1966 break; 1966 break;
1967 case OTG_STATE_A_PERIPHERAL: 1967 case OTG_STATE_A_PERIPHERAL:
1968 musb->xceiv.state = OTG_STATE_A_WAIT_VFALL; 1968 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
1969 MUSB_HST_MODE(musb);
1969 break; 1970 break;
1970 case OTG_STATE_B_WAIT_ACON: 1971 case OTG_STATE_B_WAIT_ACON:
1971 case OTG_STATE_B_HOST: 1972 case OTG_STATE_B_HOST:
1972#endif 1973#endif
1973 case OTG_STATE_B_PERIPHERAL: 1974 case OTG_STATE_B_PERIPHERAL:
1974 case OTG_STATE_B_IDLE: 1975 case OTG_STATE_B_IDLE:
1975 musb->xceiv.state = OTG_STATE_B_IDLE; 1976 musb->xceiv->state = OTG_STATE_B_IDLE;
1976 break; 1977 break;
1977 case OTG_STATE_B_SRP_INIT: 1978 case OTG_STATE_B_SRP_INIT:
1978 break; 1979 break;
@@ -2028,10 +2029,10 @@ __acquires(musb->lock)
2028 * or else after HNP, as A-Device 2029 * or else after HNP, as A-Device
2029 */ 2030 */
2030 if (devctl & MUSB_DEVCTL_BDEVICE) { 2031 if (devctl & MUSB_DEVCTL_BDEVICE) {
2031 musb->xceiv.state = OTG_STATE_B_PERIPHERAL; 2032 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2032 musb->g.is_a_peripheral = 0; 2033 musb->g.is_a_peripheral = 0;
2033 } else if (is_otg_enabled(musb)) { 2034 } else if (is_otg_enabled(musb)) {
2034 musb->xceiv.state = OTG_STATE_A_PERIPHERAL; 2035 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2035 musb->g.is_a_peripheral = 1; 2036 musb->g.is_a_peripheral = 1;
2036 } else 2037 } else
2037 WARN_ON(1); 2038 WARN_ON(1);
diff --git a/drivers/usb/musb/musb_gadget_ep0.c b/drivers/usb/musb/musb_gadget_ep0.c
index 3f5e30ddfa27..40ed50ecedff 100644
--- a/drivers/usb/musb/musb_gadget_ep0.c
+++ b/drivers/usb/musb/musb_gadget_ep0.c
@@ -4,6 +4,7 @@
4 * Copyright 2005 Mentor Graphics Corporation 4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments 5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation 6 * Copyright (C) 2006-2007 Nokia Corporation
7 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
7 * 8 *
8 * This program is free software; you can redistribute it and/or 9 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License 10 * modify it under the terms of the GNU General Public License
@@ -58,7 +59,8 @@
58static char *decode_ep0stage(u8 stage) 59static char *decode_ep0stage(u8 stage)
59{ 60{
60 switch (stage) { 61 switch (stage) {
61 case MUSB_EP0_STAGE_SETUP: return "idle"; 62 case MUSB_EP0_STAGE_IDLE: return "idle";
63 case MUSB_EP0_STAGE_SETUP: return "setup";
62 case MUSB_EP0_STAGE_TX: return "in"; 64 case MUSB_EP0_STAGE_TX: return "in";
63 case MUSB_EP0_STAGE_RX: return "out"; 65 case MUSB_EP0_STAGE_RX: return "out";
64 case MUSB_EP0_STAGE_ACKWAIT: return "wait"; 66 case MUSB_EP0_STAGE_ACKWAIT: return "wait";
@@ -628,7 +630,7 @@ irqreturn_t musb_g_ep0_irq(struct musb *musb)
628 musb_writew(regs, MUSB_CSR0, 630 musb_writew(regs, MUSB_CSR0,
629 csr & ~MUSB_CSR0_P_SENTSTALL); 631 csr & ~MUSB_CSR0_P_SENTSTALL);
630 retval = IRQ_HANDLED; 632 retval = IRQ_HANDLED;
631 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 633 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
632 csr = musb_readw(regs, MUSB_CSR0); 634 csr = musb_readw(regs, MUSB_CSR0);
633 } 635 }
634 636
@@ -636,7 +638,18 @@ irqreturn_t musb_g_ep0_irq(struct musb *musb)
636 if (csr & MUSB_CSR0_P_SETUPEND) { 638 if (csr & MUSB_CSR0_P_SETUPEND) {
637 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDSETUPEND); 639 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDSETUPEND);
638 retval = IRQ_HANDLED; 640 retval = IRQ_HANDLED;
639 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 641 /* Transition into the early status phase */
642 switch (musb->ep0_state) {
643 case MUSB_EP0_STAGE_TX:
644 musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
645 break;
646 case MUSB_EP0_STAGE_RX:
647 musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
648 break;
649 default:
650 ERR("SetupEnd came in a wrong ep0stage %s",
651 decode_ep0stage(musb->ep0_state));
652 }
640 csr = musb_readw(regs, MUSB_CSR0); 653 csr = musb_readw(regs, MUSB_CSR0);
641 /* NOTE: request may need completion */ 654 /* NOTE: request may need completion */
642 } 655 }
@@ -697,11 +710,31 @@ irqreturn_t musb_g_ep0_irq(struct musb *musb)
697 if (req) 710 if (req)
698 musb_g_ep0_giveback(musb, req); 711 musb_g_ep0_giveback(musb, req);
699 } 712 }
713
714 /*
715 * In case when several interrupts can get coalesced,
716 * check to see if we've already received a SETUP packet...
717 */
718 if (csr & MUSB_CSR0_RXPKTRDY)
719 goto setup;
720
721 retval = IRQ_HANDLED;
722 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
723 break;
724
725 case MUSB_EP0_STAGE_IDLE:
726 /*
727 * This state is typically (but not always) indiscernible
728 * from the status states since the corresponding interrupts
729 * tend to happen within too little period of time (with only
730 * a zero-length packet in between) and so get coalesced...
731 */
700 retval = IRQ_HANDLED; 732 retval = IRQ_HANDLED;
701 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 733 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
702 /* FALLTHROUGH */ 734 /* FALLTHROUGH */
703 735
704 case MUSB_EP0_STAGE_SETUP: 736 case MUSB_EP0_STAGE_SETUP:
737setup:
705 if (csr & MUSB_CSR0_RXPKTRDY) { 738 if (csr & MUSB_CSR0_RXPKTRDY) {
706 struct usb_ctrlrequest setup; 739 struct usb_ctrlrequest setup;
707 int handled = 0; 740 int handled = 0;
@@ -783,7 +816,7 @@ irqreturn_t musb_g_ep0_irq(struct musb *musb)
783stall: 816stall:
784 DBG(3, "stall (%d)\n", handled); 817 DBG(3, "stall (%d)\n", handled);
785 musb->ackpend |= MUSB_CSR0_P_SENDSTALL; 818 musb->ackpend |= MUSB_CSR0_P_SENDSTALL;
786 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 819 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
787finish: 820finish:
788 musb_writew(regs, MUSB_CSR0, 821 musb_writew(regs, MUSB_CSR0,
789 musb->ackpend); 822 musb->ackpend);
@@ -803,7 +836,7 @@ finish:
803 /* "can't happen" */ 836 /* "can't happen" */
804 WARN_ON(1); 837 WARN_ON(1);
805 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SENDSTALL); 838 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SENDSTALL);
806 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 839 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
807 break; 840 break;
808 } 841 }
809 842
@@ -959,7 +992,7 @@ static int musb_g_ep0_halt(struct usb_ep *e, int value)
959 992
960 csr |= MUSB_CSR0_P_SENDSTALL; 993 csr |= MUSB_CSR0_P_SENDSTALL;
961 musb_writew(regs, MUSB_CSR0, csr); 994 musb_writew(regs, MUSB_CSR0, csr);
962 musb->ep0_state = MUSB_EP0_STAGE_SETUP; 995 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
963 musb->ackpend = 0; 996 musb->ackpend = 0;
964 break; 997 break;
965 default: 998 default:
diff --git a/drivers/usb/musb/musb_host.c b/drivers/usb/musb/musb_host.c
index db1b57415ec7..cf94511485f2 100644
--- a/drivers/usb/musb/musb_host.c
+++ b/drivers/usb/musb/musb_host.c
@@ -181,6 +181,19 @@ static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
181 musb_writew(ep->regs, MUSB_TXCSR, txcsr); 181 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
182} 182}
183 183
184static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
185{
186 if (is_in != 0 || ep->is_shared_fifo)
187 ep->in_qh = qh;
188 if (is_in == 0 || ep->is_shared_fifo)
189 ep->out_qh = qh;
190}
191
192static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
193{
194 return is_in ? ep->in_qh : ep->out_qh;
195}
196
184/* 197/*
185 * Start the URB at the front of an endpoint's queue 198 * Start the URB at the front of an endpoint's queue
186 * end must be claimed from the caller. 199 * end must be claimed from the caller.
@@ -210,7 +223,6 @@ musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
210 case USB_ENDPOINT_XFER_CONTROL: 223 case USB_ENDPOINT_XFER_CONTROL:
211 /* control transfers always start with SETUP */ 224 /* control transfers always start with SETUP */
212 is_in = 0; 225 is_in = 0;
213 hw_ep->out_qh = qh;
214 musb->ep0_stage = MUSB_EP0_START; 226 musb->ep0_stage = MUSB_EP0_START;
215 buf = urb->setup_packet; 227 buf = urb->setup_packet;
216 len = 8; 228 len = 8;
@@ -239,10 +251,7 @@ musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
239 epnum, buf + offset, len); 251 epnum, buf + offset, len);
240 252
241 /* Configure endpoint */ 253 /* Configure endpoint */
242 if (is_in || hw_ep->is_shared_fifo) 254 musb_ep_set_qh(hw_ep, is_in, qh);
243 hw_ep->in_qh = qh;
244 else
245 hw_ep->out_qh = qh;
246 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len); 255 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
247 256
248 /* transmit may have more work: start it when it is time */ 257 /* transmit may have more work: start it when it is time */
@@ -286,9 +295,8 @@ start:
286 } 295 }
287} 296}
288 297
289/* caller owns controller lock, irqs are blocked */ 298/* Context: caller owns controller lock, IRQs are blocked */
290static void 299static void musb_giveback(struct musb *musb, struct urb *urb, int status)
291__musb_giveback(struct musb *musb, struct urb *urb, int status)
292__releases(musb->lock) 300__releases(musb->lock)
293__acquires(musb->lock) 301__acquires(musb->lock)
294{ 302{
@@ -321,51 +329,48 @@ __acquires(musb->lock)
321 spin_lock(&musb->lock); 329 spin_lock(&musb->lock);
322} 330}
323 331
324/* for bulk/interrupt endpoints only */ 332/* For bulk/interrupt endpoints only */
325static inline void 333static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
326musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb) 334 struct urb *urb)
327{ 335{
328 struct usb_device *udev = urb->dev; 336 void __iomem *epio = qh->hw_ep->regs;
329 u16 csr; 337 u16 csr;
330 void __iomem *epio = ep->regs;
331 struct musb_qh *qh;
332 338
333 /* FIXME: the current Mentor DMA code seems to have 339 /*
340 * FIXME: the current Mentor DMA code seems to have
334 * problems getting toggle correct. 341 * problems getting toggle correct.
335 */ 342 */
336 343
337 if (is_in || ep->is_shared_fifo) 344 if (is_in)
338 qh = ep->in_qh; 345 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
339 else 346 else
340 qh = ep->out_qh; 347 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
341 348
342 if (!is_in) { 349 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
343 csr = musb_readw(epio, MUSB_TXCSR);
344 usb_settoggle(udev, qh->epnum, 1,
345 (csr & MUSB_TXCSR_H_DATATOGGLE)
346 ? 1 : 0);
347 } else {
348 csr = musb_readw(epio, MUSB_RXCSR);
349 usb_settoggle(udev, qh->epnum, 0,
350 (csr & MUSB_RXCSR_H_DATATOGGLE)
351 ? 1 : 0);
352 }
353} 350}
354 351
355/* caller owns controller lock, irqs are blocked */ 352/*
356static struct musb_qh * 353 * Advance this hardware endpoint's queue, completing the specified URB and
357musb_giveback(struct musb_qh *qh, struct urb *urb, int status) 354 * advancing to either the next URB queued to that qh, or else invalidating
355 * that qh and advancing to the next qh scheduled after the current one.
356 *
357 * Context: caller owns controller lock, IRQs are blocked
358 */
359static void musb_advance_schedule(struct musb *musb, struct urb *urb,
360 struct musb_hw_ep *hw_ep, int is_in)
358{ 361{
362 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
359 struct musb_hw_ep *ep = qh->hw_ep; 363 struct musb_hw_ep *ep = qh->hw_ep;
360 struct musb *musb = ep->musb;
361 int is_in = usb_pipein(urb->pipe);
362 int ready = qh->is_ready; 364 int ready = qh->is_ready;
365 int status;
366
367 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
363 368
364 /* save toggle eagerly, for paranoia */ 369 /* save toggle eagerly, for paranoia */
365 switch (qh->type) { 370 switch (qh->type) {
366 case USB_ENDPOINT_XFER_BULK: 371 case USB_ENDPOINT_XFER_BULK:
367 case USB_ENDPOINT_XFER_INT: 372 case USB_ENDPOINT_XFER_INT:
368 musb_save_toggle(ep, is_in, urb); 373 musb_save_toggle(qh, is_in, urb);
369 break; 374 break;
370 case USB_ENDPOINT_XFER_ISOC: 375 case USB_ENDPOINT_XFER_ISOC:
371 if (status == 0 && urb->error_count) 376 if (status == 0 && urb->error_count)
@@ -374,7 +379,7 @@ musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
374 } 379 }
375 380
376 qh->is_ready = 0; 381 qh->is_ready = 0;
377 __musb_giveback(musb, urb, status); 382 musb_giveback(musb, urb, status);
378 qh->is_ready = ready; 383 qh->is_ready = ready;
379 384
380 /* reclaim resources (and bandwidth) ASAP; deschedule it, and 385 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
@@ -388,11 +393,8 @@ musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
388 else 393 else
389 ep->tx_reinit = 1; 394 ep->tx_reinit = 1;
390 395
391 /* clobber old pointers to this qh */ 396 /* Clobber old pointers to this qh */
392 if (is_in || ep->is_shared_fifo) 397 musb_ep_set_qh(ep, is_in, NULL);
393 ep->in_qh = NULL;
394 else
395 ep->out_qh = NULL;
396 qh->hep->hcpriv = NULL; 398 qh->hep->hcpriv = NULL;
397 399
398 switch (qh->type) { 400 switch (qh->type) {
@@ -421,36 +423,10 @@ musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
421 break; 423 break;
422 } 424 }
423 } 425 }
424 return qh;
425}
426
427/*
428 * Advance this hardware endpoint's queue, completing the specified urb and
429 * advancing to either the next urb queued to that qh, or else invalidating
430 * that qh and advancing to the next qh scheduled after the current one.
431 *
432 * Context: caller owns controller lock, irqs are blocked
433 */
434static void
435musb_advance_schedule(struct musb *musb, struct urb *urb,
436 struct musb_hw_ep *hw_ep, int is_in)
437{
438 struct musb_qh *qh;
439
440 if (is_in || hw_ep->is_shared_fifo)
441 qh = hw_ep->in_qh;
442 else
443 qh = hw_ep->out_qh;
444
445 if (urb->status == -EINPROGRESS)
446 qh = musb_giveback(qh, urb, 0);
447 else
448 qh = musb_giveback(qh, urb, urb->status);
449 426
450 if (qh != NULL && qh->is_ready) { 427 if (qh != NULL && qh->is_ready) {
451 DBG(4, "... next ep%d %cX urb %p\n", 428 DBG(4, "... next ep%d %cX urb %p\n",
452 hw_ep->epnum, is_in ? 'R' : 'T', 429 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
453 next_urb(qh));
454 musb_start_urb(musb, is_in, qh); 430 musb_start_urb(musb, is_in, qh);
455 } 431 }
456} 432}
@@ -629,7 +605,8 @@ musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
629 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg); 605 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
630 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg); 606 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
631 /* NOTE: bulk combining rewrites high bits of maxpacket */ 607 /* NOTE: bulk combining rewrites high bits of maxpacket */
632 musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket); 608 musb_writew(ep->regs, MUSB_RXMAXP,
609 qh->maxpacket | ((qh->hb_mult - 1) << 11));
633 610
634 ep->rx_reinit = 0; 611 ep->rx_reinit = 0;
635} 612}
@@ -651,9 +628,10 @@ static bool musb_tx_dma_program(struct dma_controller *dma,
651 csr = musb_readw(epio, MUSB_TXCSR); 628 csr = musb_readw(epio, MUSB_TXCSR);
652 if (length > pkt_size) { 629 if (length > pkt_size) {
653 mode = 1; 630 mode = 1;
654 csr |= MUSB_TXCSR_AUTOSET 631 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
655 | MUSB_TXCSR_DMAMODE 632 /* autoset shouldn't be set in high bandwidth */
656 | MUSB_TXCSR_DMAENAB; 633 if (qh->hb_mult == 1)
634 csr |= MUSB_TXCSR_AUTOSET;
657 } else { 635 } else {
658 mode = 0; 636 mode = 0;
659 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE); 637 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
@@ -703,15 +681,8 @@ static void musb_ep_program(struct musb *musb, u8 epnum,
703 void __iomem *mbase = musb->mregs; 681 void __iomem *mbase = musb->mregs;
704 struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 682 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
705 void __iomem *epio = hw_ep->regs; 683 void __iomem *epio = hw_ep->regs;
706 struct musb_qh *qh; 684 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
707 u16 packet_sz; 685 u16 packet_sz = qh->maxpacket;
708
709 if (!is_out || hw_ep->is_shared_fifo)
710 qh = hw_ep->in_qh;
711 else
712 qh = hw_ep->out_qh;
713
714 packet_sz = qh->maxpacket;
715 686
716 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s " 687 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
717 "h_addr%02x h_port%02x bytes %d\n", 688 "h_addr%02x h_port%02x bytes %d\n",
@@ -1129,17 +1100,14 @@ void musb_host_tx(struct musb *musb, u8 epnum)
1129 u16 tx_csr; 1100 u16 tx_csr;
1130 size_t length = 0; 1101 size_t length = 0;
1131 size_t offset = 0; 1102 size_t offset = 0;
1132 struct urb *urb;
1133 struct musb_hw_ep *hw_ep = musb->endpoints + epnum; 1103 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1134 void __iomem *epio = hw_ep->regs; 1104 void __iomem *epio = hw_ep->regs;
1135 struct musb_qh *qh = hw_ep->is_shared_fifo ? hw_ep->in_qh 1105 struct musb_qh *qh = hw_ep->out_qh;
1136 : hw_ep->out_qh; 1106 struct urb *urb = next_urb(qh);
1137 u32 status = 0; 1107 u32 status = 0;
1138 void __iomem *mbase = musb->mregs; 1108 void __iomem *mbase = musb->mregs;
1139 struct dma_channel *dma; 1109 struct dma_channel *dma;
1140 1110
1141 urb = next_urb(qh);
1142
1143 musb_ep_select(mbase, epnum); 1111 musb_ep_select(mbase, epnum);
1144 tx_csr = musb_readw(epio, MUSB_TXCSR); 1112 tx_csr = musb_readw(epio, MUSB_TXCSR);
1145 1113
@@ -1427,7 +1395,7 @@ static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
1427 urb->actual_length += dma->actual_len; 1395 urb->actual_length += dma->actual_len;
1428 dma->actual_len = 0L; 1396 dma->actual_len = 0L;
1429 } 1397 }
1430 musb_save_toggle(ep, 1, urb); 1398 musb_save_toggle(cur_qh, 1, urb);
1431 1399
1432 /* move cur_qh to end of queue */ 1400 /* move cur_qh to end of queue */
1433 list_move_tail(&cur_qh->ring, &musb->in_bulk); 1401 list_move_tail(&cur_qh->ring, &musb->in_bulk);
@@ -1531,6 +1499,10 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1531 /* packet error reported later */ 1499 /* packet error reported later */
1532 iso_err = true; 1500 iso_err = true;
1533 } 1501 }
1502 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1503 DBG(3, "end %d high bandwidth incomplete ISO packet RX\n",
1504 epnum);
1505 status = -EPROTO;
1534 } 1506 }
1535 1507
1536 /* faults abort the transfer */ 1508 /* faults abort the transfer */
@@ -1738,7 +1710,11 @@ void musb_host_rx(struct musb *musb, u8 epnum)
1738 val &= ~MUSB_RXCSR_H_AUTOREQ; 1710 val &= ~MUSB_RXCSR_H_AUTOREQ;
1739 else 1711 else
1740 val |= MUSB_RXCSR_H_AUTOREQ; 1712 val |= MUSB_RXCSR_H_AUTOREQ;
1741 val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB; 1713 val |= MUSB_RXCSR_DMAENAB;
1714
1715 /* autoclear shouldn't be set in high bandwidth */
1716 if (qh->hb_mult == 1)
1717 val |= MUSB_RXCSR_AUTOCLEAR;
1742 1718
1743 musb_writew(epio, MUSB_RXCSR, 1719 musb_writew(epio, MUSB_RXCSR,
1744 MUSB_RXCSR_H_WZC_BITS | val); 1720 MUSB_RXCSR_H_WZC_BITS | val);
@@ -1817,19 +1793,17 @@ static int musb_schedule(
1817 epnum++, hw_ep++) { 1793 epnum++, hw_ep++) {
1818 int diff; 1794 int diff;
1819 1795
1820 if (is_in || hw_ep->is_shared_fifo) { 1796 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
1821 if (hw_ep->in_qh != NULL)
1822 continue;
1823 } else if (hw_ep->out_qh != NULL)
1824 continue; 1797 continue;
1825 1798
1826 if (hw_ep == musb->bulk_ep) 1799 if (hw_ep == musb->bulk_ep)
1827 continue; 1800 continue;
1828 1801
1829 if (is_in) 1802 if (is_in)
1830 diff = hw_ep->max_packet_sz_rx - qh->maxpacket; 1803 diff = hw_ep->max_packet_sz_rx;
1831 else 1804 else
1832 diff = hw_ep->max_packet_sz_tx - qh->maxpacket; 1805 diff = hw_ep->max_packet_sz_tx;
1806 diff -= (qh->maxpacket * qh->hb_mult);
1833 1807
1834 if (diff >= 0 && best_diff > diff) { 1808 if (diff >= 0 && best_diff > diff) {
1835 best_diff = diff; 1809 best_diff = diff;
@@ -1932,15 +1906,27 @@ static int musb_urb_enqueue(
1932 qh->is_ready = 1; 1906 qh->is_ready = 1;
1933 1907
1934 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize); 1908 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1909 qh->type = usb_endpoint_type(epd);
1935 1910
1936 /* no high bandwidth support yet */ 1911 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
1937 if (qh->maxpacket & ~0x7ff) { 1912 * Some musb cores don't support high bandwidth ISO transfers; and
1938 ret = -EMSGSIZE; 1913 * we don't (yet!) support high bandwidth interrupt transfers.
1939 goto done; 1914 */
1915 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
1916 if (qh->hb_mult > 1) {
1917 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
1918
1919 if (ok)
1920 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
1921 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
1922 if (!ok) {
1923 ret = -EMSGSIZE;
1924 goto done;
1925 }
1926 qh->maxpacket &= 0x7ff;
1940 } 1927 }
1941 1928
1942 qh->epnum = usb_endpoint_num(epd); 1929 qh->epnum = usb_endpoint_num(epd);
1943 qh->type = usb_endpoint_type(epd);
1944 1930
1945 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */ 1931 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1946 qh->addr_reg = (u8) usb_pipedevice(urb->pipe); 1932 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
@@ -2052,14 +2038,15 @@ done:
2052 * called with controller locked, irqs blocked 2038 * called with controller locked, irqs blocked
2053 * that hardware queue advances to the next transfer, unless prevented 2039 * that hardware queue advances to the next transfer, unless prevented
2054 */ 2040 */
2055static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in) 2041static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2056{ 2042{
2057 struct musb_hw_ep *ep = qh->hw_ep; 2043 struct musb_hw_ep *ep = qh->hw_ep;
2058 void __iomem *epio = ep->regs; 2044 void __iomem *epio = ep->regs;
2059 unsigned hw_end = ep->epnum; 2045 unsigned hw_end = ep->epnum;
2060 void __iomem *regs = ep->musb->mregs; 2046 void __iomem *regs = ep->musb->mregs;
2061 u16 csr; 2047 int is_in = usb_pipein(urb->pipe);
2062 int status = 0; 2048 int status = 0;
2049 u16 csr;
2063 2050
2064 musb_ep_select(regs, hw_end); 2051 musb_ep_select(regs, hw_end);
2065 2052
@@ -2112,14 +2099,14 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2112{ 2099{
2113 struct musb *musb = hcd_to_musb(hcd); 2100 struct musb *musb = hcd_to_musb(hcd);
2114 struct musb_qh *qh; 2101 struct musb_qh *qh;
2115 struct list_head *sched;
2116 unsigned long flags; 2102 unsigned long flags;
2103 int is_in = usb_pipein(urb->pipe);
2117 int ret; 2104 int ret;
2118 2105
2119 DBG(4, "urb=%p, dev%d ep%d%s\n", urb, 2106 DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
2120 usb_pipedevice(urb->pipe), 2107 usb_pipedevice(urb->pipe),
2121 usb_pipeendpoint(urb->pipe), 2108 usb_pipeendpoint(urb->pipe),
2122 usb_pipein(urb->pipe) ? "in" : "out"); 2109 is_in ? "in" : "out");
2123 2110
2124 spin_lock_irqsave(&musb->lock, flags); 2111 spin_lock_irqsave(&musb->lock, flags);
2125 ret = usb_hcd_check_unlink_urb(hcd, urb, status); 2112 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
@@ -2130,47 +2117,25 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2130 if (!qh) 2117 if (!qh)
2131 goto done; 2118 goto done;
2132 2119
2133 /* Any URB not actively programmed into endpoint hardware can be 2120 /*
2121 * Any URB not actively programmed into endpoint hardware can be
2134 * immediately given back; that's any URB not at the head of an 2122 * immediately given back; that's any URB not at the head of an
2135 * endpoint queue, unless someday we get real DMA queues. And even 2123 * endpoint queue, unless someday we get real DMA queues. And even
2136 * if it's at the head, it might not be known to the hardware... 2124 * if it's at the head, it might not be known to the hardware...
2137 * 2125 *
2138 * Otherwise abort current transfer, pending dma, etc.; urb->status 2126 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2139 * has already been updated. This is a synchronous abort; it'd be 2127 * has already been updated. This is a synchronous abort; it'd be
2140 * OK to hold off until after some IRQ, though. 2128 * OK to hold off until after some IRQ, though.
2129 *
2130 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2141 */ 2131 */
2142 if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list) 2132 if (!qh->is_ready
2143 ret = -EINPROGRESS; 2133 || urb->urb_list.prev != &qh->hep->urb_list
2144 else { 2134 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2145 switch (qh->type) {
2146 case USB_ENDPOINT_XFER_CONTROL:
2147 sched = &musb->control;
2148 break;
2149 case USB_ENDPOINT_XFER_BULK:
2150 if (qh->mux == 1) {
2151 if (usb_pipein(urb->pipe))
2152 sched = &musb->in_bulk;
2153 else
2154 sched = &musb->out_bulk;
2155 break;
2156 }
2157 default:
2158 /* REVISIT when we get a schedule tree, periodic
2159 * transfers won't always be at the head of a
2160 * singleton queue...
2161 */
2162 sched = NULL;
2163 break;
2164 }
2165 }
2166
2167 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2168 if (ret < 0 || (sched && qh != first_qh(sched))) {
2169 int ready = qh->is_ready; 2135 int ready = qh->is_ready;
2170 2136
2171 ret = 0;
2172 qh->is_ready = 0; 2137 qh->is_ready = 0;
2173 __musb_giveback(musb, urb, 0); 2138 musb_giveback(musb, urb, 0);
2174 qh->is_ready = ready; 2139 qh->is_ready = ready;
2175 2140
2176 /* If nothing else (usually musb_giveback) is using it 2141 /* If nothing else (usually musb_giveback) is using it
@@ -2182,7 +2147,7 @@ static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2182 kfree(qh); 2147 kfree(qh);
2183 } 2148 }
2184 } else 2149 } else
2185 ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN); 2150 ret = musb_cleanup_urb(urb, qh);
2186done: 2151done:
2187 spin_unlock_irqrestore(&musb->lock, flags); 2152 spin_unlock_irqrestore(&musb->lock, flags);
2188 return ret; 2153 return ret;
@@ -2192,13 +2157,11 @@ done:
2192static void 2157static void
2193musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep) 2158musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2194{ 2159{
2195 u8 epnum = hep->desc.bEndpointAddress; 2160 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2196 unsigned long flags; 2161 unsigned long flags;
2197 struct musb *musb = hcd_to_musb(hcd); 2162 struct musb *musb = hcd_to_musb(hcd);
2198 u8 is_in = epnum & USB_DIR_IN;
2199 struct musb_qh *qh; 2163 struct musb_qh *qh;
2200 struct urb *urb; 2164 struct urb *urb;
2201 struct list_head *sched;
2202 2165
2203 spin_lock_irqsave(&musb->lock, flags); 2166 spin_lock_irqsave(&musb->lock, flags);
2204 2167
@@ -2206,31 +2169,11 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2206 if (qh == NULL) 2169 if (qh == NULL)
2207 goto exit; 2170 goto exit;
2208 2171
2209 switch (qh->type) { 2172 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2210 case USB_ENDPOINT_XFER_CONTROL:
2211 sched = &musb->control;
2212 break;
2213 case USB_ENDPOINT_XFER_BULK:
2214 if (qh->mux == 1) {
2215 if (is_in)
2216 sched = &musb->in_bulk;
2217 else
2218 sched = &musb->out_bulk;
2219 break;
2220 }
2221 default:
2222 /* REVISIT when we get a schedule tree, periodic transfers
2223 * won't always be at the head of a singleton queue...
2224 */
2225 sched = NULL;
2226 break;
2227 }
2228
2229 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2230 2173
2231 /* kick first urb off the hardware, if needed */ 2174 /* Kick the first URB off the hardware, if needed */
2232 qh->is_ready = 0; 2175 qh->is_ready = 0;
2233 if (!sched || qh == first_qh(sched)) { 2176 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2234 urb = next_urb(qh); 2177 urb = next_urb(qh);
2235 2178
2236 /* make software (then hardware) stop ASAP */ 2179 /* make software (then hardware) stop ASAP */
@@ -2238,7 +2181,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2238 urb->status = -ESHUTDOWN; 2181 urb->status = -ESHUTDOWN;
2239 2182
2240 /* cleanup */ 2183 /* cleanup */
2241 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN); 2184 musb_cleanup_urb(urb, qh);
2242 2185
2243 /* Then nuke all the others ... and advance the 2186 /* Then nuke all the others ... and advance the
2244 * queue on hw_ep (e.g. bulk ring) when we're done. 2187 * queue on hw_ep (e.g. bulk ring) when we're done.
@@ -2254,7 +2197,7 @@ musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2254 * will activate any of these as it advances. 2197 * will activate any of these as it advances.
2255 */ 2198 */
2256 while (!list_empty(&hep->urb_list)) 2199 while (!list_empty(&hep->urb_list))
2257 __musb_giveback(musb, next_urb(qh), -ESHUTDOWN); 2200 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2258 2201
2259 hep->hcpriv = NULL; 2202 hep->hcpriv = NULL;
2260 list_del(&qh->ring); 2203 list_del(&qh->ring);
@@ -2292,13 +2235,30 @@ static void musb_h_stop(struct usb_hcd *hcd)
2292static int musb_bus_suspend(struct usb_hcd *hcd) 2235static int musb_bus_suspend(struct usb_hcd *hcd)
2293{ 2236{
2294 struct musb *musb = hcd_to_musb(hcd); 2237 struct musb *musb = hcd_to_musb(hcd);
2238 u8 devctl;
2239
2240 if (!is_host_active(musb))
2241 return 0;
2295 2242
2296 if (musb->xceiv.state == OTG_STATE_A_SUSPEND) 2243 switch (musb->xceiv->state) {
2244 case OTG_STATE_A_SUSPEND:
2297 return 0; 2245 return 0;
2246 case OTG_STATE_A_WAIT_VRISE:
2247 /* ID could be grounded even if there's no device
2248 * on the other end of the cable. NOTE that the
2249 * A_WAIT_VRISE timers are messy with MUSB...
2250 */
2251 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2252 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2253 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2254 break;
2255 default:
2256 break;
2257 }
2298 2258
2299 if (is_host_active(musb) && musb->is_active) { 2259 if (musb->is_active) {
2300 WARNING("trying to suspend as %s is_active=%i\n", 2260 WARNING("trying to suspend as %s while active\n",
2301 otg_state_string(musb), musb->is_active); 2261 otg_state_string(musb));
2302 return -EBUSY; 2262 return -EBUSY;
2303 } else 2263 } else
2304 return 0; 2264 return 0;
diff --git a/drivers/usb/musb/musb_host.h b/drivers/usb/musb/musb_host.h
index 0b7fbcd21963..14b00776638d 100644
--- a/drivers/usb/musb/musb_host.h
+++ b/drivers/usb/musb/musb_host.h
@@ -67,6 +67,7 @@ struct musb_qh {
67 u8 is_ready; /* safe to modify hw_ep */ 67 u8 is_ready; /* safe to modify hw_ep */
68 u8 type; /* XFERTYPE_* */ 68 u8 type; /* XFERTYPE_* */
69 u8 epnum; 69 u8 epnum;
70 u8 hb_mult; /* high bandwidth pkts per uf */
70 u16 maxpacket; 71 u16 maxpacket;
71 u16 frame; /* for periodic schedule */ 72 u16 frame; /* for periodic schedule */
72 unsigned iso_idx; /* in urb->iso_frame_desc[] */ 73 unsigned iso_idx; /* in urb->iso_frame_desc[] */
diff --git a/drivers/usb/musb/musb_virthub.c b/drivers/usb/musb/musb_virthub.c
index bf677acc83db..bfe5fe4ebfee 100644
--- a/drivers/usb/musb/musb_virthub.c
+++ b/drivers/usb/musb/musb_virthub.c
@@ -78,18 +78,22 @@ static void musb_port_suspend(struct musb *musb, bool do_suspend)
78 DBG(3, "Root port suspended, power %02x\n", power); 78 DBG(3, "Root port suspended, power %02x\n", power);
79 79
80 musb->port1_status |= USB_PORT_STAT_SUSPEND; 80 musb->port1_status |= USB_PORT_STAT_SUSPEND;
81 switch (musb->xceiv.state) { 81 switch (musb->xceiv->state) {
82 case OTG_STATE_A_HOST: 82 case OTG_STATE_A_HOST:
83 musb->xceiv.state = OTG_STATE_A_SUSPEND; 83 musb->xceiv->state = OTG_STATE_A_SUSPEND;
84 musb->is_active = is_otg_enabled(musb) 84 musb->is_active = is_otg_enabled(musb)
85 && musb->xceiv.host->b_hnp_enable; 85 && musb->xceiv->host->b_hnp_enable;
86 if (musb->is_active)
87 mod_timer(&musb->otg_timer, jiffies
88 + msecs_to_jiffies(
89 OTG_TIME_A_AIDL_BDIS));
86 musb_platform_try_idle(musb, 0); 90 musb_platform_try_idle(musb, 0);
87 break; 91 break;
88#ifdef CONFIG_USB_MUSB_OTG 92#ifdef CONFIG_USB_MUSB_OTG
89 case OTG_STATE_B_HOST: 93 case OTG_STATE_B_HOST:
90 musb->xceiv.state = OTG_STATE_B_WAIT_ACON; 94 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
91 musb->is_active = is_otg_enabled(musb) 95 musb->is_active = is_otg_enabled(musb)
92 && musb->xceiv.host->b_hnp_enable; 96 && musb->xceiv->host->b_hnp_enable;
93 musb_platform_try_idle(musb, 0); 97 musb_platform_try_idle(musb, 0);
94 break; 98 break;
95#endif 99#endif
@@ -116,7 +120,7 @@ static void musb_port_reset(struct musb *musb, bool do_reset)
116 void __iomem *mbase = musb->mregs; 120 void __iomem *mbase = musb->mregs;
117 121
118#ifdef CONFIG_USB_MUSB_OTG 122#ifdef CONFIG_USB_MUSB_OTG
119 if (musb->xceiv.state == OTG_STATE_B_IDLE) { 123 if (musb->xceiv->state == OTG_STATE_B_IDLE) {
120 DBG(2, "HNP: Returning from HNP; no hub reset from b_idle\n"); 124 DBG(2, "HNP: Returning from HNP; no hub reset from b_idle\n");
121 musb->port1_status &= ~USB_PORT_STAT_RESET; 125 musb->port1_status &= ~USB_PORT_STAT_RESET;
122 return; 126 return;
@@ -186,14 +190,23 @@ void musb_root_disconnect(struct musb *musb)
186 usb_hcd_poll_rh_status(musb_to_hcd(musb)); 190 usb_hcd_poll_rh_status(musb_to_hcd(musb));
187 musb->is_active = 0; 191 musb->is_active = 0;
188 192
189 switch (musb->xceiv.state) { 193 switch (musb->xceiv->state) {
190 case OTG_STATE_A_HOST:
191 case OTG_STATE_A_SUSPEND: 194 case OTG_STATE_A_SUSPEND:
192 musb->xceiv.state = OTG_STATE_A_WAIT_BCON; 195#ifdef CONFIG_USB_MUSB_OTG
196 if (is_otg_enabled(musb)
197 && musb->xceiv->host->b_hnp_enable) {
198 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
199 musb->g.is_a_peripheral = 1;
200 break;
201 }
202#endif
203 /* FALLTHROUGH */
204 case OTG_STATE_A_HOST:
205 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
193 musb->is_active = 0; 206 musb->is_active = 0;
194 break; 207 break;
195 case OTG_STATE_A_WAIT_VFALL: 208 case OTG_STATE_A_WAIT_VFALL:
196 musb->xceiv.state = OTG_STATE_B_IDLE; 209 musb->xceiv->state = OTG_STATE_B_IDLE;
197 break; 210 break;
198 default: 211 default:
199 DBG(1, "host disconnect (%s)\n", otg_state_string(musb)); 212 DBG(1, "host disconnect (%s)\n", otg_state_string(musb));
@@ -332,7 +345,7 @@ int musb_hub_control(
332 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16; 345 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
333 usb_hcd_poll_rh_status(musb_to_hcd(musb)); 346 usb_hcd_poll_rh_status(musb_to_hcd(musb));
334 /* NOTE: it might really be A_WAIT_BCON ... */ 347 /* NOTE: it might really be A_WAIT_BCON ... */
335 musb->xceiv.state = OTG_STATE_A_HOST; 348 musb->xceiv->state = OTG_STATE_A_HOST;
336 } 349 }
337 350
338 put_unaligned(cpu_to_le32(musb->port1_status 351 put_unaligned(cpu_to_le32(musb->port1_status
diff --git a/drivers/usb/musb/omap2430.c b/drivers/usb/musb/omap2430.c
index 60924ce08493..34875201ee04 100644
--- a/drivers/usb/musb/omap2430.c
+++ b/drivers/usb/musb/omap2430.c
@@ -44,7 +44,6 @@
44#define get_cpu_rev() 2 44#define get_cpu_rev() 2
45#endif 45#endif
46 46
47#define MUSB_TIMEOUT_A_WAIT_BCON 1100
48 47
49static struct timer_list musb_idle_timer; 48static struct timer_list musb_idle_timer;
50 49
@@ -61,17 +60,17 @@ static void musb_do_idle(unsigned long _musb)
61 60
62 devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 61 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
63 62
64 switch (musb->xceiv.state) { 63 switch (musb->xceiv->state) {
65 case OTG_STATE_A_WAIT_BCON: 64 case OTG_STATE_A_WAIT_BCON:
66 devctl &= ~MUSB_DEVCTL_SESSION; 65 devctl &= ~MUSB_DEVCTL_SESSION;
67 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); 66 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
68 67
69 devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 68 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
70 if (devctl & MUSB_DEVCTL_BDEVICE) { 69 if (devctl & MUSB_DEVCTL_BDEVICE) {
71 musb->xceiv.state = OTG_STATE_B_IDLE; 70 musb->xceiv->state = OTG_STATE_B_IDLE;
72 MUSB_DEV_MODE(musb); 71 MUSB_DEV_MODE(musb);
73 } else { 72 } else {
74 musb->xceiv.state = OTG_STATE_A_IDLE; 73 musb->xceiv->state = OTG_STATE_A_IDLE;
75 MUSB_HST_MODE(musb); 74 MUSB_HST_MODE(musb);
76 } 75 }
77 break; 76 break;
@@ -89,7 +88,7 @@ static void musb_do_idle(unsigned long _musb)
89 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16; 88 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
90 usb_hcd_poll_rh_status(musb_to_hcd(musb)); 89 usb_hcd_poll_rh_status(musb_to_hcd(musb));
91 /* NOTE: it might really be A_WAIT_BCON ... */ 90 /* NOTE: it might really be A_WAIT_BCON ... */
92 musb->xceiv.state = OTG_STATE_A_HOST; 91 musb->xceiv->state = OTG_STATE_A_HOST;
93 } 92 }
94 break; 93 break;
95#endif 94#endif
@@ -97,9 +96,9 @@ static void musb_do_idle(unsigned long _musb)
97 case OTG_STATE_A_HOST: 96 case OTG_STATE_A_HOST:
98 devctl = musb_readb(musb->mregs, MUSB_DEVCTL); 97 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
99 if (devctl & MUSB_DEVCTL_BDEVICE) 98 if (devctl & MUSB_DEVCTL_BDEVICE)
100 musb->xceiv.state = OTG_STATE_B_IDLE; 99 musb->xceiv->state = OTG_STATE_B_IDLE;
101 else 100 else
102 musb->xceiv.state = OTG_STATE_A_WAIT_BCON; 101 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
103#endif 102#endif
104 default: 103 default:
105 break; 104 break;
@@ -118,7 +117,7 @@ void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
118 117
119 /* Never idle if active, or when VBUS timeout is not set as host */ 118 /* Never idle if active, or when VBUS timeout is not set as host */
120 if (musb->is_active || ((musb->a_wait_bcon == 0) 119 if (musb->is_active || ((musb->a_wait_bcon == 0)
121 && (musb->xceiv.state == OTG_STATE_A_WAIT_BCON))) { 120 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
122 DBG(4, "%s active, deleting timer\n", otg_state_string(musb)); 121 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
123 del_timer(&musb_idle_timer); 122 del_timer(&musb_idle_timer);
124 last_timer = jiffies; 123 last_timer = jiffies;
@@ -163,8 +162,8 @@ static void omap_set_vbus(struct musb *musb, int is_on)
163 162
164 if (is_on) { 163 if (is_on) {
165 musb->is_active = 1; 164 musb->is_active = 1;
166 musb->xceiv.default_a = 1; 165 musb->xceiv->default_a = 1;
167 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE; 166 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
168 devctl |= MUSB_DEVCTL_SESSION; 167 devctl |= MUSB_DEVCTL_SESSION;
169 168
170 MUSB_HST_MODE(musb); 169 MUSB_HST_MODE(musb);
@@ -175,8 +174,8 @@ static void omap_set_vbus(struct musb *musb, int is_on)
175 * jumping right to B_IDLE... 174 * jumping right to B_IDLE...
176 */ 175 */
177 176
178 musb->xceiv.default_a = 0; 177 musb->xceiv->default_a = 0;
179 musb->xceiv.state = OTG_STATE_B_IDLE; 178 musb->xceiv->state = OTG_STATE_B_IDLE;
180 devctl &= ~MUSB_DEVCTL_SESSION; 179 devctl &= ~MUSB_DEVCTL_SESSION;
181 180
182 MUSB_DEV_MODE(musb); 181 MUSB_DEV_MODE(musb);
@@ -188,10 +187,6 @@ static void omap_set_vbus(struct musb *musb, int is_on)
188 otg_state_string(musb), 187 otg_state_string(musb),
189 musb_readb(musb->mregs, MUSB_DEVCTL)); 188 musb_readb(musb->mregs, MUSB_DEVCTL));
190} 189}
191static int omap_set_power(struct otg_transceiver *x, unsigned mA)
192{
193 return 0;
194}
195 190
196static int musb_platform_resume(struct musb *musb); 191static int musb_platform_resume(struct musb *musb);
197 192
@@ -202,24 +197,6 @@ int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
202 devctl |= MUSB_DEVCTL_SESSION; 197 devctl |= MUSB_DEVCTL_SESSION;
203 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); 198 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
204 199
205 switch (musb_mode) {
206#ifdef CONFIG_USB_MUSB_HDRC_HCD
207 case MUSB_HOST:
208 otg_set_host(&musb->xceiv, musb->xceiv.host);
209 break;
210#endif
211#ifdef CONFIG_USB_GADGET_MUSB_HDRC
212 case MUSB_PERIPHERAL:
213 otg_set_peripheral(&musb->xceiv, musb->xceiv.gadget);
214 break;
215#endif
216#ifdef CONFIG_USB_MUSB_OTG
217 case MUSB_OTG:
218 break;
219#endif
220 default:
221 return -EINVAL;
222 }
223 return 0; 200 return 0;
224} 201}
225 202
@@ -231,6 +208,16 @@ int __init musb_platform_init(struct musb *musb)
231 omap_cfg_reg(AE5_2430_USB0HS_STP); 208 omap_cfg_reg(AE5_2430_USB0HS_STP);
232#endif 209#endif
233 210
211 /* We require some kind of external transceiver, hooked
212 * up through ULPI. TWL4030-family PMICs include one,
213 * which needs a driver, drivers aren't always needed.
214 */
215 musb->xceiv = otg_get_transceiver();
216 if (!musb->xceiv) {
217 pr_err("HS USB OTG: no transceiver configured\n");
218 return -ENODEV;
219 }
220
234 musb_platform_resume(musb); 221 musb_platform_resume(musb);
235 222
236 l = omap_readl(OTG_SYSCONFIG); 223 l = omap_readl(OTG_SYSCONFIG);
@@ -240,7 +227,12 @@ int __init musb_platform_init(struct musb *musb)
240 l &= ~AUTOIDLE; /* disable auto idle */ 227 l &= ~AUTOIDLE; /* disable auto idle */
241 l &= ~NOIDLE; /* remove possible noidle */ 228 l &= ~NOIDLE; /* remove possible noidle */
242 l |= SMARTIDLE; /* enable smart idle */ 229 l |= SMARTIDLE; /* enable smart idle */
243 l |= AUTOIDLE; /* enable auto idle */ 230 /*
231 * MUSB AUTOIDLE don't work in 3430.
232 * Workaround by Richard Woodruff/TI
233 */
234 if (!cpu_is_omap3430())
235 l |= AUTOIDLE; /* enable auto idle */
244 omap_writel(l, OTG_SYSCONFIG); 236 omap_writel(l, OTG_SYSCONFIG);
245 237
246 l = omap_readl(OTG_INTERFSEL); 238 l = omap_readl(OTG_INTERFSEL);
@@ -257,9 +249,6 @@ int __init musb_platform_init(struct musb *musb)
257 249
258 if (is_host_enabled(musb)) 250 if (is_host_enabled(musb))
259 musb->board_set_vbus = omap_set_vbus; 251 musb->board_set_vbus = omap_set_vbus;
260 if (is_peripheral_enabled(musb))
261 musb->xceiv.set_power = omap_set_power;
262 musb->a_wait_bcon = MUSB_TIMEOUT_A_WAIT_BCON;
263 252
264 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb); 253 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
265 254
@@ -282,8 +271,7 @@ int musb_platform_suspend(struct musb *musb)
282 l |= ENABLEWAKEUP; /* enable wakeup */ 271 l |= ENABLEWAKEUP; /* enable wakeup */
283 omap_writel(l, OTG_SYSCONFIG); 272 omap_writel(l, OTG_SYSCONFIG);
284 273
285 if (musb->xceiv.set_suspend) 274 otg_set_suspend(musb->xceiv, 1);
286 musb->xceiv.set_suspend(&musb->xceiv, 1);
287 275
288 if (musb->set_clock) 276 if (musb->set_clock)
289 musb->set_clock(musb->clock, 0); 277 musb->set_clock(musb->clock, 0);
@@ -300,8 +288,7 @@ static int musb_platform_resume(struct musb *musb)
300 if (!musb->clock) 288 if (!musb->clock)
301 return 0; 289 return 0;
302 290
303 if (musb->xceiv.set_suspend) 291 otg_set_suspend(musb->xceiv, 0);
304 musb->xceiv.set_suspend(&musb->xceiv, 0);
305 292
306 if (musb->set_clock) 293 if (musb->set_clock)
307 musb->set_clock(musb->clock, 1); 294 musb->set_clock(musb->clock, 1);
diff --git a/drivers/usb/musb/tusb6010.c b/drivers/usb/musb/tusb6010.c
index 4ac1477d3569..88b587c703e9 100644
--- a/drivers/usb/musb/tusb6010.c
+++ b/drivers/usb/musb/tusb6010.c
@@ -259,6 +259,8 @@ void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
259 tusb_fifo_read_unaligned(fifo, buf, len); 259 tusb_fifo_read_unaligned(fifo, buf, len);
260} 260}
261 261
262static struct musb *the_musb;
263
262#ifdef CONFIG_USB_GADGET_MUSB_HDRC 264#ifdef CONFIG_USB_GADGET_MUSB_HDRC
263 265
264/* This is used by gadget drivers, and OTG transceiver logic, allowing 266/* This is used by gadget drivers, and OTG transceiver logic, allowing
@@ -269,7 +271,7 @@ void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *buf)
269 */ 271 */
270static int tusb_draw_power(struct otg_transceiver *x, unsigned mA) 272static int tusb_draw_power(struct otg_transceiver *x, unsigned mA)
271{ 273{
272 struct musb *musb = container_of(x, struct musb, xceiv); 274 struct musb *musb = the_musb;
273 void __iomem *tbase = musb->ctrl_base; 275 void __iomem *tbase = musb->ctrl_base;
274 u32 reg; 276 u32 reg;
275 277
@@ -419,7 +421,7 @@ static void musb_do_idle(unsigned long _musb)
419 421
420 spin_lock_irqsave(&musb->lock, flags); 422 spin_lock_irqsave(&musb->lock, flags);
421 423
422 switch (musb->xceiv.state) { 424 switch (musb->xceiv->state) {
423 case OTG_STATE_A_WAIT_BCON: 425 case OTG_STATE_A_WAIT_BCON:
424 if ((musb->a_wait_bcon != 0) 426 if ((musb->a_wait_bcon != 0)
425 && (musb->idle_timeout == 0 427 && (musb->idle_timeout == 0
@@ -483,7 +485,7 @@ void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
483 485
484 /* Never idle if active, or when VBUS timeout is not set as host */ 486 /* Never idle if active, or when VBUS timeout is not set as host */
485 if (musb->is_active || ((musb->a_wait_bcon == 0) 487 if (musb->is_active || ((musb->a_wait_bcon == 0)
486 && (musb->xceiv.state == OTG_STATE_A_WAIT_BCON))) { 488 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
487 DBG(4, "%s active, deleting timer\n", otg_state_string(musb)); 489 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
488 del_timer(&musb_idle_timer); 490 del_timer(&musb_idle_timer);
489 last_timer = jiffies; 491 last_timer = jiffies;
@@ -532,8 +534,8 @@ static void tusb_source_power(struct musb *musb, int is_on)
532 if (musb->set_clock) 534 if (musb->set_clock)
533 musb->set_clock(musb->clock, 1); 535 musb->set_clock(musb->clock, 1);
534 timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE); 536 timer = OTG_TIMER_MS(OTG_TIME_A_WAIT_VRISE);
535 musb->xceiv.default_a = 1; 537 musb->xceiv->default_a = 1;
536 musb->xceiv.state = OTG_STATE_A_WAIT_VRISE; 538 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
537 devctl |= MUSB_DEVCTL_SESSION; 539 devctl |= MUSB_DEVCTL_SESSION;
538 540
539 conf |= TUSB_DEV_CONF_USB_HOST_MODE; 541 conf |= TUSB_DEV_CONF_USB_HOST_MODE;
@@ -546,24 +548,24 @@ static void tusb_source_power(struct musb *musb, int is_on)
546 /* If ID pin is grounded, we want to be a_idle */ 548 /* If ID pin is grounded, we want to be a_idle */
547 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT); 549 otg_stat = musb_readl(tbase, TUSB_DEV_OTG_STAT);
548 if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) { 550 if (!(otg_stat & TUSB_DEV_OTG_STAT_ID_STATUS)) {
549 switch (musb->xceiv.state) { 551 switch (musb->xceiv->state) {
550 case OTG_STATE_A_WAIT_VRISE: 552 case OTG_STATE_A_WAIT_VRISE:
551 case OTG_STATE_A_WAIT_BCON: 553 case OTG_STATE_A_WAIT_BCON:
552 musb->xceiv.state = OTG_STATE_A_WAIT_VFALL; 554 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
553 break; 555 break;
554 case OTG_STATE_A_WAIT_VFALL: 556 case OTG_STATE_A_WAIT_VFALL:
555 musb->xceiv.state = OTG_STATE_A_IDLE; 557 musb->xceiv->state = OTG_STATE_A_IDLE;
556 break; 558 break;
557 default: 559 default:
558 musb->xceiv.state = OTG_STATE_A_IDLE; 560 musb->xceiv->state = OTG_STATE_A_IDLE;
559 } 561 }
560 musb->is_active = 0; 562 musb->is_active = 0;
561 musb->xceiv.default_a = 1; 563 musb->xceiv->default_a = 1;
562 MUSB_HST_MODE(musb); 564 MUSB_HST_MODE(musb);
563 } else { 565 } else {
564 musb->is_active = 0; 566 musb->is_active = 0;
565 musb->xceiv.default_a = 0; 567 musb->xceiv->default_a = 0;
566 musb->xceiv.state = OTG_STATE_B_IDLE; 568 musb->xceiv->state = OTG_STATE_B_IDLE;
567 MUSB_DEV_MODE(musb); 569 MUSB_DEV_MODE(musb);
568 } 570 }
569 571
@@ -674,7 +676,7 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
674 else 676 else
675 default_a = is_host_enabled(musb); 677 default_a = is_host_enabled(musb);
676 DBG(2, "Default-%c\n", default_a ? 'A' : 'B'); 678 DBG(2, "Default-%c\n", default_a ? 'A' : 'B');
677 musb->xceiv.default_a = default_a; 679 musb->xceiv->default_a = default_a;
678 tusb_source_power(musb, default_a); 680 tusb_source_power(musb, default_a);
679 681
680 /* Don't allow idling immediately */ 682 /* Don't allow idling immediately */
@@ -686,7 +688,7 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
686 if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) { 688 if (int_src & TUSB_INT_SRC_VBUS_SENSE_CHNG) {
687 689
688 /* B-dev state machine: no vbus ~= disconnect */ 690 /* B-dev state machine: no vbus ~= disconnect */
689 if ((is_otg_enabled(musb) && !musb->xceiv.default_a) 691 if ((is_otg_enabled(musb) && !musb->xceiv->default_a)
690 || !is_host_enabled(musb)) { 692 || !is_host_enabled(musb)) {
691#ifdef CONFIG_USB_MUSB_HDRC_HCD 693#ifdef CONFIG_USB_MUSB_HDRC_HCD
692 /* ? musb_root_disconnect(musb); */ 694 /* ? musb_root_disconnect(musb); */
@@ -701,9 +703,9 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
701 703
702 if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) { 704 if (otg_stat & TUSB_DEV_OTG_STAT_SESS_END) {
703 DBG(1, "Forcing disconnect (no interrupt)\n"); 705 DBG(1, "Forcing disconnect (no interrupt)\n");
704 if (musb->xceiv.state != OTG_STATE_B_IDLE) { 706 if (musb->xceiv->state != OTG_STATE_B_IDLE) {
705 /* INTR_DISCONNECT can hide... */ 707 /* INTR_DISCONNECT can hide... */
706 musb->xceiv.state = OTG_STATE_B_IDLE; 708 musb->xceiv->state = OTG_STATE_B_IDLE;
707 musb->int_usb |= MUSB_INTR_DISCONNECT; 709 musb->int_usb |= MUSB_INTR_DISCONNECT;
708 } 710 }
709 musb->is_active = 0; 711 musb->is_active = 0;
@@ -717,7 +719,7 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
717 DBG(2, "vbus change, %s, otg %03x\n", 719 DBG(2, "vbus change, %s, otg %03x\n",
718 otg_state_string(musb), otg_stat); 720 otg_state_string(musb), otg_stat);
719 721
720 switch (musb->xceiv.state) { 722 switch (musb->xceiv->state) {
721 case OTG_STATE_A_IDLE: 723 case OTG_STATE_A_IDLE:
722 DBG(2, "Got SRP, turning on VBUS\n"); 724 DBG(2, "Got SRP, turning on VBUS\n");
723 musb_set_vbus(musb, 1); 725 musb_set_vbus(musb, 1);
@@ -765,7 +767,7 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
765 767
766 DBG(4, "%s timer, %03x\n", otg_state_string(musb), otg_stat); 768 DBG(4, "%s timer, %03x\n", otg_state_string(musb), otg_stat);
767 769
768 switch (musb->xceiv.state) { 770 switch (musb->xceiv->state) {
769 case OTG_STATE_A_WAIT_VRISE: 771 case OTG_STATE_A_WAIT_VRISE:
770 /* VBUS has probably been valid for a while now, 772 /* VBUS has probably been valid for a while now,
771 * but may well have bounced out of range a bit 773 * but may well have bounced out of range a bit
@@ -777,7 +779,7 @@ tusb_otg_ints(struct musb *musb, u32 int_src, void __iomem *tbase)
777 DBG(2, "devctl %02x\n", devctl); 779 DBG(2, "devctl %02x\n", devctl);
778 break; 780 break;
779 } 781 }
780 musb->xceiv.state = OTG_STATE_A_WAIT_BCON; 782 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
781 musb->is_active = 0; 783 musb->is_active = 0;
782 idle_timeout = jiffies 784 idle_timeout = jiffies
783 + msecs_to_jiffies(musb->a_wait_bcon); 785 + msecs_to_jiffies(musb->a_wait_bcon);
@@ -1093,9 +1095,14 @@ int __init musb_platform_init(struct musb *musb)
1093{ 1095{
1094 struct platform_device *pdev; 1096 struct platform_device *pdev;
1095 struct resource *mem; 1097 struct resource *mem;
1096 void __iomem *sync; 1098 void __iomem *sync = NULL;
1097 int ret; 1099 int ret;
1098 1100
1101 usb_nop_xceiv_register();
1102 musb->xceiv = otg_get_transceiver();
1103 if (!musb->xceiv)
1104 return -ENODEV;
1105
1099 pdev = to_platform_device(musb->controller); 1106 pdev = to_platform_device(musb->controller);
1100 1107
1101 /* dma address for async dma */ 1108 /* dma address for async dma */
@@ -1106,14 +1113,16 @@ int __init musb_platform_init(struct musb *musb)
1106 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1113 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1107 if (!mem) { 1114 if (!mem) {
1108 pr_debug("no sync dma resource?\n"); 1115 pr_debug("no sync dma resource?\n");
1109 return -ENODEV; 1116 ret = -ENODEV;
1117 goto done;
1110 } 1118 }
1111 musb->sync = mem->start; 1119 musb->sync = mem->start;
1112 1120
1113 sync = ioremap(mem->start, mem->end - mem->start + 1); 1121 sync = ioremap(mem->start, mem->end - mem->start + 1);
1114 if (!sync) { 1122 if (!sync) {
1115 pr_debug("ioremap for sync failed\n"); 1123 pr_debug("ioremap for sync failed\n");
1116 return -ENOMEM; 1124 ret = -ENOMEM;
1125 goto done;
1117 } 1126 }
1118 musb->sync_va = sync; 1127 musb->sync_va = sync;
1119 1128
@@ -1126,28 +1135,37 @@ int __init musb_platform_init(struct musb *musb)
1126 if (ret) { 1135 if (ret) {
1127 printk(KERN_ERR "Could not start tusb6010 (%d)\n", 1136 printk(KERN_ERR "Could not start tusb6010 (%d)\n",
1128 ret); 1137 ret);
1129 return -ENODEV; 1138 goto done;
1130 } 1139 }
1131 musb->isr = tusb_interrupt; 1140 musb->isr = tusb_interrupt;
1132 1141
1133 if (is_host_enabled(musb)) 1142 if (is_host_enabled(musb))
1134 musb->board_set_vbus = tusb_source_power; 1143 musb->board_set_vbus = tusb_source_power;
1135 if (is_peripheral_enabled(musb)) 1144 if (is_peripheral_enabled(musb)) {
1136 musb->xceiv.set_power = tusb_draw_power; 1145 musb->xceiv->set_power = tusb_draw_power;
1146 the_musb = musb;
1147 }
1137 1148
1138 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb); 1149 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
1139 1150
1151done:
1152 if (ret < 0) {
1153 if (sync)
1154 iounmap(sync);
1155 usb_nop_xceiv_unregister();
1156 }
1140 return ret; 1157 return ret;
1141} 1158}
1142 1159
1143int musb_platform_exit(struct musb *musb) 1160int musb_platform_exit(struct musb *musb)
1144{ 1161{
1145 del_timer_sync(&musb_idle_timer); 1162 del_timer_sync(&musb_idle_timer);
1163 the_musb = NULL;
1146 1164
1147 if (musb->board_set_power) 1165 if (musb->board_set_power)
1148 musb->board_set_power(0); 1166 musb->board_set_power(0);
1149 1167
1150 iounmap(musb->sync_va); 1168 iounmap(musb->sync_va);
1151 1169 usb_nop_xceiv_unregister();
1152 return 0; 1170 return 0;
1153} 1171}
diff --git a/drivers/usb/otg/nop-usb-xceiv.c b/drivers/usb/otg/nop-usb-xceiv.c
index c567168f89af..af456b48985f 100644
--- a/drivers/usb/otg/nop-usb-xceiv.c
+++ b/drivers/usb/otg/nop-usb-xceiv.c
@@ -22,8 +22,8 @@
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * 23 *
24 * Current status: 24 * Current status:
25 * this is to add "nop" transceiver for all those phy which is 25 * This provides a "nop" transceiver for PHYs which are
26 * autonomous such as isp1504 etc. 26 * autonomous such as isp1504, isp1707, etc.
27 */ 27 */
28 28
29#include <linux/module.h> 29#include <linux/module.h>
@@ -36,30 +36,26 @@ struct nop_usb_xceiv {
36 struct device *dev; 36 struct device *dev;
37}; 37};
38 38
39static u64 nop_xceiv_dmamask = DMA_BIT_MASK(32); 39static struct platform_device *pd;
40
41static struct platform_device nop_xceiv_device = {
42 .name = "nop_usb_xceiv",
43 .id = -1,
44 .dev = {
45 .dma_mask = &nop_xceiv_dmamask,
46 .coherent_dma_mask = DMA_BIT_MASK(32),
47 .platform_data = NULL,
48 },
49};
50 40
51void usb_nop_xceiv_register(void) 41void usb_nop_xceiv_register(void)
52{ 42{
53 if (platform_device_register(&nop_xceiv_device) < 0) { 43 if (pd)
44 return;
45 pd = platform_device_register_simple("nop_usb_xceiv", -1, NULL, 0);
46 if (!pd) {
54 printk(KERN_ERR "Unable to register usb nop transceiver\n"); 47 printk(KERN_ERR "Unable to register usb nop transceiver\n");
55 return; 48 return;
56 } 49 }
57} 50}
51EXPORT_SYMBOL(usb_nop_xceiv_register);
58 52
59void usb_nop_xceiv_unregister(void) 53void usb_nop_xceiv_unregister(void)
60{ 54{
61 platform_device_unregister(&nop_xceiv_device); 55 platform_device_unregister(pd);
56 pd = NULL;
62} 57}
58EXPORT_SYMBOL(usb_nop_xceiv_unregister);
63 59
64static inline struct nop_usb_xceiv *xceiv_to_nop(struct otg_transceiver *x) 60static inline struct nop_usb_xceiv *xceiv_to_nop(struct otg_transceiver *x)
65{ 61{
diff --git a/drivers/usb/otg/twl4030-usb.c b/drivers/usb/otg/twl4030-usb.c
index d9478d0e1c8b..9e3e7a5c258b 100644
--- a/drivers/usb/otg/twl4030-usb.c
+++ b/drivers/usb/otg/twl4030-usb.c
@@ -217,6 +217,7 @@
217 217
218/* In module TWL4030_MODULE_PM_MASTER */ 218/* In module TWL4030_MODULE_PM_MASTER */
219#define PROTECT_KEY 0x0E 219#define PROTECT_KEY 0x0E
220#define STS_HW_CONDITIONS 0x0F
220 221
221/* In module TWL4030_MODULE_PM_RECEIVER */ 222/* In module TWL4030_MODULE_PM_RECEIVER */
222#define VUSB_DEDICATED1 0x7D 223#define VUSB_DEDICATED1 0x7D
@@ -351,15 +352,26 @@ static enum linkstat twl4030_usb_linkstat(struct twl4030_usb *twl)
351 int status; 352 int status;
352 int linkstat = USB_LINK_UNKNOWN; 353 int linkstat = USB_LINK_UNKNOWN;
353 354
354 /* STS_HW_CONDITIONS */ 355 /*
355 status = twl4030_readb(twl, TWL4030_MODULE_PM_MASTER, 0x0f); 356 * For ID/VBUS sensing, see manual section 15.4.8 ...
357 * except when using only battery backup power, two
358 * comparators produce VBUS_PRES and ID_PRES signals,
359 * which don't match docs elsewhere. But ... BIT(7)
360 * and BIT(2) of STS_HW_CONDITIONS, respectively, do
361 * seem to match up. If either is true the USB_PRES
362 * signal is active, the OTG module is activated, and
363 * its interrupt may be raised (may wake the system).
364 */
365 status = twl4030_readb(twl, TWL4030_MODULE_PM_MASTER,
366 STS_HW_CONDITIONS);
356 if (status < 0) 367 if (status < 0)
357 dev_err(twl->dev, "USB link status err %d\n", status); 368 dev_err(twl->dev, "USB link status err %d\n", status);
358 else if (status & BIT(7)) 369 else if (status & (BIT(7) | BIT(2))) {
359 linkstat = USB_LINK_VBUS; 370 if (status & BIT(2))
360 else if (status & BIT(2)) 371 linkstat = USB_LINK_ID;
361 linkstat = USB_LINK_ID; 372 else
362 else 373 linkstat = USB_LINK_VBUS;
374 } else
363 linkstat = USB_LINK_NONE; 375 linkstat = USB_LINK_NONE;
364 376
365 dev_dbg(twl->dev, "HW_CONDITIONS 0x%02x/%d; link %d\n", 377 dev_dbg(twl->dev, "HW_CONDITIONS 0x%02x/%d; link %d\n",
@@ -641,7 +653,7 @@ static int twl4030_set_host(struct otg_transceiver *x, struct usb_bus *host)
641 return 0; 653 return 0;
642} 654}
643 655
644static int __init twl4030_usb_probe(struct platform_device *pdev) 656static int __devinit twl4030_usb_probe(struct platform_device *pdev)
645{ 657{
646 struct twl4030_usb_data *pdata = pdev->dev.platform_data; 658 struct twl4030_usb_data *pdata = pdev->dev.platform_data;
647 struct twl4030_usb *twl; 659 struct twl4030_usb *twl;
diff --git a/drivers/usb/serial/aircable.c b/drivers/usb/serial/aircable.c
index 6d106e74265e..2cbfab3716e5 100644
--- a/drivers/usb/serial/aircable.c
+++ b/drivers/usb/serial/aircable.c
@@ -364,7 +364,7 @@ static int aircable_attach(struct usb_serial *serial)
364 return 0; 364 return 0;
365} 365}
366 366
367static void aircable_shutdown(struct usb_serial *serial) 367static void aircable_release(struct usb_serial *serial)
368{ 368{
369 369
370 struct usb_serial_port *port = serial->port[0]; 370 struct usb_serial_port *port = serial->port[0];
@@ -375,7 +375,6 @@ static void aircable_shutdown(struct usb_serial *serial)
375 if (priv) { 375 if (priv) {
376 serial_buf_free(priv->tx_buf); 376 serial_buf_free(priv->tx_buf);
377 serial_buf_free(priv->rx_buf); 377 serial_buf_free(priv->rx_buf);
378 usb_set_serial_port_data(port, NULL);
379 kfree(priv); 378 kfree(priv);
380 } 379 }
381} 380}
@@ -601,7 +600,7 @@ static struct usb_serial_driver aircable_device = {
601 .num_ports = 1, 600 .num_ports = 1,
602 .attach = aircable_attach, 601 .attach = aircable_attach,
603 .probe = aircable_probe, 602 .probe = aircable_probe,
604 .shutdown = aircable_shutdown, 603 .release = aircable_release,
605 .write = aircable_write, 604 .write = aircable_write,
606 .write_room = aircable_write_room, 605 .write_room = aircable_write_room,
607 .write_bulk_callback = aircable_write_bulk_callback, 606 .write_bulk_callback = aircable_write_bulk_callback,
diff --git a/drivers/usb/serial/belkin_sa.c b/drivers/usb/serial/belkin_sa.c
index b7eacad4d48c..7033b031b443 100644
--- a/drivers/usb/serial/belkin_sa.c
+++ b/drivers/usb/serial/belkin_sa.c
@@ -90,11 +90,10 @@ static int debug;
90 90
91/* function prototypes for a Belkin USB Serial Adapter F5U103 */ 91/* function prototypes for a Belkin USB Serial Adapter F5U103 */
92static int belkin_sa_startup(struct usb_serial *serial); 92static int belkin_sa_startup(struct usb_serial *serial);
93static void belkin_sa_shutdown(struct usb_serial *serial); 93static void belkin_sa_release(struct usb_serial *serial);
94static int belkin_sa_open(struct tty_struct *tty, 94static int belkin_sa_open(struct tty_struct *tty,
95 struct usb_serial_port *port, struct file *filp); 95 struct usb_serial_port *port, struct file *filp);
96static void belkin_sa_close(struct tty_struct *tty, 96static void belkin_sa_close(struct usb_serial_port *port);
97 struct usb_serial_port *port, struct file *filp);
98static void belkin_sa_read_int_callback(struct urb *urb); 97static void belkin_sa_read_int_callback(struct urb *urb);
99static void belkin_sa_set_termios(struct tty_struct *tty, 98static void belkin_sa_set_termios(struct tty_struct *tty,
100 struct usb_serial_port *port, struct ktermios * old); 99 struct usb_serial_port *port, struct ktermios * old);
@@ -143,7 +142,7 @@ static struct usb_serial_driver belkin_device = {
143 .tiocmget = belkin_sa_tiocmget, 142 .tiocmget = belkin_sa_tiocmget,
144 .tiocmset = belkin_sa_tiocmset, 143 .tiocmset = belkin_sa_tiocmset,
145 .attach = belkin_sa_startup, 144 .attach = belkin_sa_startup,
146 .shutdown = belkin_sa_shutdown, 145 .release = belkin_sa_release,
147}; 146};
148 147
149 148
@@ -198,14 +197,13 @@ static int belkin_sa_startup(struct usb_serial *serial)
198} 197}
199 198
200 199
201static void belkin_sa_shutdown(struct usb_serial *serial) 200static void belkin_sa_release(struct usb_serial *serial)
202{ 201{
203 struct belkin_sa_private *priv; 202 struct belkin_sa_private *priv;
204 int i; 203 int i;
205 204
206 dbg("%s", __func__); 205 dbg("%s", __func__);
207 206
208 /* stop reads and writes on all ports */
209 for (i = 0; i < serial->num_ports; ++i) { 207 for (i = 0; i < serial->num_ports; ++i) {
210 /* My special items, the standard routines free my urbs */ 208 /* My special items, the standard routines free my urbs */
211 priv = usb_get_serial_port_data(serial->port[i]); 209 priv = usb_get_serial_port_data(serial->port[i]);
@@ -244,8 +242,7 @@ exit:
244} /* belkin_sa_open */ 242} /* belkin_sa_open */
245 243
246 244
247static void belkin_sa_close(struct tty_struct *tty, 245static void belkin_sa_close(struct usb_serial_port *port)
248 struct usb_serial_port *port, struct file *filp)
249{ 246{
250 dbg("%s port %d", __func__, port->number); 247 dbg("%s port %d", __func__, port->number);
251 248
diff --git a/drivers/usb/serial/bus.c b/drivers/usb/serial/bus.c
index 83bbb5bca2ef..ba555c528cc6 100644
--- a/drivers/usb/serial/bus.c
+++ b/drivers/usb/serial/bus.c
@@ -59,23 +59,22 @@ static int usb_serial_device_probe(struct device *dev)
59 retval = -ENODEV; 59 retval = -ENODEV;
60 goto exit; 60 goto exit;
61 } 61 }
62 if (port->dev_state != PORT_REGISTERING)
63 goto exit;
62 64
63 driver = port->serial->type; 65 driver = port->serial->type;
64 if (driver->port_probe) { 66 if (driver->port_probe) {
65 if (!try_module_get(driver->driver.owner)) {
66 dev_err(dev, "module get failed, exiting\n");
67 retval = -EIO;
68 goto exit;
69 }
70 retval = driver->port_probe(port); 67 retval = driver->port_probe(port);
71 module_put(driver->driver.owner);
72 if (retval) 68 if (retval)
73 goto exit; 69 goto exit;
74 } 70 }
75 71
76 retval = device_create_file(dev, &dev_attr_port_number); 72 retval = device_create_file(dev, &dev_attr_port_number);
77 if (retval) 73 if (retval) {
74 if (driver->port_remove)
75 retval = driver->port_remove(port);
78 goto exit; 76 goto exit;
77 }
79 78
80 minor = port->number; 79 minor = port->number;
81 tty_register_device(usb_serial_tty_driver, minor, dev); 80 tty_register_device(usb_serial_tty_driver, minor, dev);
@@ -98,19 +97,15 @@ static int usb_serial_device_remove(struct device *dev)
98 if (!port) 97 if (!port)
99 return -ENODEV; 98 return -ENODEV;
100 99
100 if (port->dev_state != PORT_UNREGISTERING)
101 return retval;
102
101 device_remove_file(&port->dev, &dev_attr_port_number); 103 device_remove_file(&port->dev, &dev_attr_port_number);
102 104
103 driver = port->serial->type; 105 driver = port->serial->type;
104 if (driver->port_remove) { 106 if (driver->port_remove)
105 if (!try_module_get(driver->driver.owner)) {
106 dev_err(dev, "module get failed, exiting\n");
107 retval = -EIO;
108 goto exit;
109 }
110 retval = driver->port_remove(port); 107 retval = driver->port_remove(port);
111 module_put(driver->driver.owner); 108
112 }
113exit:
114 minor = port->number; 109 minor = port->number;
115 tty_unregister_device(usb_serial_tty_driver, minor); 110 tty_unregister_device(usb_serial_tty_driver, minor);
116 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", 111 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index ab4cc277aa65..2830766f5b39 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -262,32 +262,40 @@ error: kfree(priv);
262 return r; 262 return r;
263} 263}
264 264
265static void ch341_close(struct tty_struct *tty, struct usb_serial_port *port, 265static int ch341_carrier_raised(struct usb_serial_port *port)
266 struct file *filp) 266{
267 struct ch341_private *priv = usb_get_serial_port_data(port);
268 if (priv->line_status & CH341_BIT_DCD)
269 return 1;
270 return 0;
271}
272
273static void ch341_dtr_rts(struct usb_serial_port *port, int on)
267{ 274{
268 struct ch341_private *priv = usb_get_serial_port_data(port); 275 struct ch341_private *priv = usb_get_serial_port_data(port);
269 unsigned long flags; 276 unsigned long flags;
270 unsigned int c_cflag;
271 277
272 dbg("%s - port %d", __func__, port->number); 278 dbg("%s - port %d", __func__, port->number);
279 /* drop DTR and RTS */
280 spin_lock_irqsave(&priv->lock, flags);
281 if (on)
282 priv->line_control |= CH341_BIT_RTS | CH341_BIT_DTR;
283 else
284 priv->line_control &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
285 spin_unlock_irqrestore(&priv->lock, flags);
286 ch341_set_handshake(port->serial->dev, priv->line_control);
287 wake_up_interruptible(&priv->delta_msr_wait);
288}
289
290static void ch341_close(struct usb_serial_port *port)
291{
292 dbg("%s - port %d", __func__, port->number);
273 293
274 /* shutdown our urbs */ 294 /* shutdown our urbs */
275 dbg("%s - shutting down urbs", __func__); 295 dbg("%s - shutting down urbs", __func__);
276 usb_kill_urb(port->write_urb); 296 usb_kill_urb(port->write_urb);
277 usb_kill_urb(port->read_urb); 297 usb_kill_urb(port->read_urb);
278 usb_kill_urb(port->interrupt_in_urb); 298 usb_kill_urb(port->interrupt_in_urb);
279
280 if (tty) {
281 c_cflag = tty->termios->c_cflag;
282 if (c_cflag & HUPCL) {
283 /* drop DTR and RTS */
284 spin_lock_irqsave(&priv->lock, flags);
285 priv->line_control = 0;
286 spin_unlock_irqrestore(&priv->lock, flags);
287 ch341_set_handshake(port->serial->dev, 0);
288 }
289 }
290 wake_up_interruptible(&priv->delta_msr_wait);
291} 299}
292 300
293 301
@@ -302,7 +310,6 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
302 dbg("ch341_open()"); 310 dbg("ch341_open()");
303 311
304 priv->baud_rate = DEFAULT_BAUD_RATE; 312 priv->baud_rate = DEFAULT_BAUD_RATE;
305 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
306 313
307 r = ch341_configure(serial->dev, priv); 314 r = ch341_configure(serial->dev, priv);
308 if (r) 315 if (r)
@@ -322,7 +329,7 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
322 if (r) { 329 if (r) {
323 dev_err(&port->dev, "%s - failed submitting interrupt urb," 330 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
324 " error %d\n", __func__, r); 331 " error %d\n", __func__, r);
325 ch341_close(tty, port, NULL); 332 ch341_close(port);
326 return -EPROTO; 333 return -EPROTO;
327 } 334 }
328 335
@@ -343,9 +350,6 @@ static void ch341_set_termios(struct tty_struct *tty,
343 350
344 dbg("ch341_set_termios()"); 351 dbg("ch341_set_termios()");
345 352
346 if (!tty || !tty->termios)
347 return;
348
349 baud_rate = tty_get_baud_rate(tty); 353 baud_rate = tty_get_baud_rate(tty);
350 354
351 priv->baud_rate = baud_rate; 355 priv->baud_rate = baud_rate;
@@ -568,6 +572,8 @@ static struct usb_serial_driver ch341_device = {
568 .usb_driver = &ch341_driver, 572 .usb_driver = &ch341_driver,
569 .num_ports = 1, 573 .num_ports = 1,
570 .open = ch341_open, 574 .open = ch341_open,
575 .dtr_rts = ch341_dtr_rts,
576 .carrier_raised = ch341_carrier_raised,
571 .close = ch341_close, 577 .close = ch341_close,
572 .ioctl = ch341_ioctl, 578 .ioctl = ch341_ioctl,
573 .set_termios = ch341_set_termios, 579 .set_termios = ch341_set_termios,
diff --git a/drivers/usb/serial/console.c b/drivers/usb/serial/console.c
index 19e24045b137..0e4f2e41ace5 100644
--- a/drivers/usb/serial/console.c
+++ b/drivers/usb/serial/console.c
@@ -169,7 +169,11 @@ static int usb_console_setup(struct console *co, char *options)
169 kfree(tty); 169 kfree(tty);
170 } 170 }
171 } 171 }
172 172 /* Now that any required fake tty operations are completed restore
173 * the tty port count */
174 --port->port.count;
175 /* The console is special in terms of closing the device so
176 * indicate this port is now acting as a system console. */
173 port->console = 1; 177 port->console = 1;
174 retval = 0; 178 retval = 0;
175 179
@@ -182,7 +186,7 @@ free_tty:
182 kfree(tty); 186 kfree(tty);
183reset_open_count: 187reset_open_count:
184 port->port.count = 0; 188 port->port.count = 0;
185goto out; 189 goto out;
186} 190}
187 191
188static void usb_console_write(struct console *co, 192static void usb_console_write(struct console *co,
@@ -202,7 +206,7 @@ static void usb_console_write(struct console *co,
202 206
203 dbg("%s - port %d, %d byte(s)", __func__, port->number, count); 207 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
204 208
205 if (!port->port.count) { 209 if (!port->console) {
206 dbg("%s - port not opened", __func__); 210 dbg("%s - port not opened", __func__);
207 return; 211 return;
208 } 212 }
@@ -298,8 +302,7 @@ void usb_serial_console_exit(void)
298{ 302{
299 if (usbcons_info.port) { 303 if (usbcons_info.port) {
300 unregister_console(&usbcons); 304 unregister_console(&usbcons);
301 if (usbcons_info.port->port.count) 305 usbcons_info.port->console = 0;
302 usbcons_info.port->port.count--;
303 usbcons_info.port = NULL; 306 usbcons_info.port = NULL;
304 } 307 }
305} 308}
diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index e8d5133ce9c8..e9a40b820fd4 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Silicon Laboratories CP2101/CP2102 USB to RS232 serial adaptor driver 2 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
3 * 3 *
4 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk) 4 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
5 * 5 *
@@ -27,44 +27,48 @@
27/* 27/*
28 * Version Information 28 * Version Information
29 */ 29 */
30#define DRIVER_VERSION "v0.08" 30#define DRIVER_VERSION "v0.09"
31#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver" 31#define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
32 32
33/* 33/*
34 * Function Prototypes 34 * Function Prototypes
35 */ 35 */
36static int cp2101_open(struct tty_struct *, struct usb_serial_port *, 36static int cp210x_open(struct tty_struct *, struct usb_serial_port *,
37 struct file *); 37 struct file *);
38static void cp2101_cleanup(struct usb_serial_port *); 38static void cp210x_cleanup(struct usb_serial_port *);
39static void cp2101_close(struct tty_struct *, struct usb_serial_port *, 39static void cp210x_close(struct usb_serial_port *);
40 struct file*); 40static void cp210x_get_termios(struct tty_struct *,
41static void cp2101_get_termios(struct tty_struct *,
42 struct usb_serial_port *port); 41 struct usb_serial_port *port);
43static void cp2101_get_termios_port(struct usb_serial_port *port, 42static void cp210x_get_termios_port(struct usb_serial_port *port,
44 unsigned int *cflagp, unsigned int *baudp); 43 unsigned int *cflagp, unsigned int *baudp);
45static void cp2101_set_termios(struct tty_struct *, struct usb_serial_port *, 44static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
46 struct ktermios*); 45 struct ktermios*);
47static int cp2101_tiocmget(struct tty_struct *, struct file *); 46static int cp210x_tiocmget(struct tty_struct *, struct file *);
48static int cp2101_tiocmset(struct tty_struct *, struct file *, 47static int cp210x_tiocmset(struct tty_struct *, struct file *,
49 unsigned int, unsigned int); 48 unsigned int, unsigned int);
50static int cp2101_tiocmset_port(struct usb_serial_port *port, struct file *, 49static int cp210x_tiocmset_port(struct usb_serial_port *port, struct file *,
51 unsigned int, unsigned int); 50 unsigned int, unsigned int);
52static void cp2101_break_ctl(struct tty_struct *, int); 51static void cp210x_break_ctl(struct tty_struct *, int);
53static int cp2101_startup(struct usb_serial *); 52static int cp210x_startup(struct usb_serial *);
54static void cp2101_shutdown(struct usb_serial *); 53static void cp210x_disconnect(struct usb_serial *);
55 54
56static int debug; 55static int debug;
57 56
58static struct usb_device_id id_table [] = { 57static struct usb_device_id id_table [] = {
59 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */ 58 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
60 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */ 59 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
60 { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
61 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */ 61 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
62 { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
62 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */ 63 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
63 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */ 64 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
64 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */ 65 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
65 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */ 66 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
66 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */ 67 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
67 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */ 68 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
69 { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
70 { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
71 { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
68 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */ 72 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
69 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */ 73 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
70 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */ 74 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
@@ -85,10 +89,12 @@ static struct usb_device_id id_table [] = {
85 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */ 89 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
86 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */ 90 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
87 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */ 91 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
92 { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
88 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */ 93 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
89 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */ 94 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
90 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demostration module */ 95 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demostration module */
91 { USB_DEVICE(0x10c4, 0x8293) }, /* Telegesys ETRX2USB */ 96 { USB_DEVICE(0x10c4, 0x8293) }, /* Telegesys ETRX2USB */
97 { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
92 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */ 98 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
93 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */ 99 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
94 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */ 100 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
@@ -99,7 +105,9 @@ static struct usb_device_id id_table [] = {
99 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */ 105 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
100 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */ 106 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
101 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */ 107 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
108 { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
102 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */ 109 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
110 { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
103 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */ 111 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
104 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */ 112 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
105 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */ 113 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
@@ -108,53 +116,70 @@ static struct usb_device_id id_table [] = {
108 116
109MODULE_DEVICE_TABLE(usb, id_table); 117MODULE_DEVICE_TABLE(usb, id_table);
110 118
111static struct usb_driver cp2101_driver = { 119static struct usb_driver cp210x_driver = {
112 .name = "cp2101", 120 .name = "cp210x",
113 .probe = usb_serial_probe, 121 .probe = usb_serial_probe,
114 .disconnect = usb_serial_disconnect, 122 .disconnect = usb_serial_disconnect,
115 .id_table = id_table, 123 .id_table = id_table,
116 .no_dynamic_id = 1, 124 .no_dynamic_id = 1,
117}; 125};
118 126
119static struct usb_serial_driver cp2101_device = { 127static struct usb_serial_driver cp210x_device = {
120 .driver = { 128 .driver = {
121 .owner = THIS_MODULE, 129 .owner = THIS_MODULE,
122 .name = "cp2101", 130 .name = "cp210x",
123 }, 131 },
124 .usb_driver = &cp2101_driver, 132 .usb_driver = &cp210x_driver,
125 .id_table = id_table, 133 .id_table = id_table,
126 .num_ports = 1, 134 .num_ports = 1,
127 .open = cp2101_open, 135 .open = cp210x_open,
128 .close = cp2101_close, 136 .close = cp210x_close,
129 .break_ctl = cp2101_break_ctl, 137 .break_ctl = cp210x_break_ctl,
130 .set_termios = cp2101_set_termios, 138 .set_termios = cp210x_set_termios,
131 .tiocmget = cp2101_tiocmget, 139 .tiocmget = cp210x_tiocmget,
132 .tiocmset = cp2101_tiocmset, 140 .tiocmset = cp210x_tiocmset,
133 .attach = cp2101_startup, 141 .attach = cp210x_startup,
134 .shutdown = cp2101_shutdown, 142 .disconnect = cp210x_disconnect,
135}; 143};
136 144
137/* Config request types */ 145/* Config request types */
138#define REQTYPE_HOST_TO_DEVICE 0x41 146#define REQTYPE_HOST_TO_DEVICE 0x41
139#define REQTYPE_DEVICE_TO_HOST 0xc1 147#define REQTYPE_DEVICE_TO_HOST 0xc1
140 148
141/* Config SET requests. To GET, add 1 to the request number */ 149/* Config request codes */
142#define CP2101_UART 0x00 /* Enable / Disable */ 150#define CP210X_IFC_ENABLE 0x00
143#define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */ 151#define CP210X_SET_BAUDDIV 0x01
144#define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */ 152#define CP210X_GET_BAUDDIV 0x02
145#define CP2101_BREAK 0x05 /* On / Off */ 153#define CP210X_SET_LINE_CTL 0x03
146#define CP2101_CONTROL 0x07 /* Flow control line states */ 154#define CP210X_GET_LINE_CTL 0x04
147#define CP2101_MODEMCTL 0x13 /* Modem controls */ 155#define CP210X_SET_BREAK 0x05
148#define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */ 156#define CP210X_IMM_CHAR 0x06
149 157#define CP210X_SET_MHS 0x07
150/* CP2101_UART */ 158#define CP210X_GET_MDMSTS 0x08
159#define CP210X_SET_XON 0x09
160#define CP210X_SET_XOFF 0x0A
161#define CP210X_SET_EVENTMASK 0x0B
162#define CP210X_GET_EVENTMASK 0x0C
163#define CP210X_SET_CHAR 0x0D
164#define CP210X_GET_CHARS 0x0E
165#define CP210X_GET_PROPS 0x0F
166#define CP210X_GET_COMM_STATUS 0x10
167#define CP210X_RESET 0x11
168#define CP210X_PURGE 0x12
169#define CP210X_SET_FLOW 0x13
170#define CP210X_GET_FLOW 0x14
171#define CP210X_EMBED_EVENTS 0x15
172#define CP210X_GET_EVENTSTATE 0x16
173#define CP210X_SET_CHARS 0x19
174
175/* CP210X_IFC_ENABLE */
151#define UART_ENABLE 0x0001 176#define UART_ENABLE 0x0001
152#define UART_DISABLE 0x0000 177#define UART_DISABLE 0x0000
153 178
154/* CP2101_BAUDRATE */ 179/* CP210X_(SET|GET)_BAUDDIV */
155#define BAUD_RATE_GEN_FREQ 0x384000 180#define BAUD_RATE_GEN_FREQ 0x384000
156 181
157/* CP2101_BITS */ 182/* CP210X_(SET|GET)_LINE_CTL */
158#define BITS_DATA_MASK 0X0f00 183#define BITS_DATA_MASK 0X0f00
159#define BITS_DATA_5 0X0500 184#define BITS_DATA_5 0X0500
160#define BITS_DATA_6 0X0600 185#define BITS_DATA_6 0X0600
@@ -174,11 +199,11 @@ static struct usb_serial_driver cp2101_device = {
174#define BITS_STOP_1_5 0x0001 199#define BITS_STOP_1_5 0x0001
175#define BITS_STOP_2 0x0002 200#define BITS_STOP_2 0x0002
176 201
177/* CP2101_BREAK */ 202/* CP210X_SET_BREAK */
178#define BREAK_ON 0x0000 203#define BREAK_ON 0x0000
179#define BREAK_OFF 0x0001 204#define BREAK_OFF 0x0001
180 205
181/* CP2101_CONTROL */ 206/* CP210X_(SET_MHS|GET_MDMSTS) */
182#define CONTROL_DTR 0x0001 207#define CONTROL_DTR 0x0001
183#define CONTROL_RTS 0x0002 208#define CONTROL_RTS 0x0002
184#define CONTROL_CTS 0x0010 209#define CONTROL_CTS 0x0010
@@ -189,13 +214,13 @@ static struct usb_serial_driver cp2101_device = {
189#define CONTROL_WRITE_RTS 0x0200 214#define CONTROL_WRITE_RTS 0x0200
190 215
191/* 216/*
192 * cp2101_get_config 217 * cp210x_get_config
193 * Reads from the CP2101 configuration registers 218 * Reads from the CP210x configuration registers
194 * 'size' is specified in bytes. 219 * 'size' is specified in bytes.
195 * 'data' is a pointer to a pre-allocated array of integers large 220 * 'data' is a pointer to a pre-allocated array of integers large
196 * enough to hold 'size' bytes (with 4 bytes to each integer) 221 * enough to hold 'size' bytes (with 4 bytes to each integer)
197 */ 222 */
198static int cp2101_get_config(struct usb_serial_port *port, u8 request, 223static int cp210x_get_config(struct usb_serial_port *port, u8 request,
199 unsigned int *data, int size) 224 unsigned int *data, int size)
200{ 225{
201 struct usb_serial *serial = port->serial; 226 struct usb_serial *serial = port->serial;
@@ -211,9 +236,6 @@ static int cp2101_get_config(struct usb_serial_port *port, u8 request,
211 return -ENOMEM; 236 return -ENOMEM;
212 } 237 }
213 238
214 /* For get requests, the request number must be incremented */
215 request++;
216
217 /* Issue the request, attempting to read 'size' bytes */ 239 /* Issue the request, attempting to read 'size' bytes */
218 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 240 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
219 request, REQTYPE_DEVICE_TO_HOST, 0x0000, 241 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
@@ -236,12 +258,12 @@ static int cp2101_get_config(struct usb_serial_port *port, u8 request,
236} 258}
237 259
238/* 260/*
239 * cp2101_set_config 261 * cp210x_set_config
240 * Writes to the CP2101 configuration registers 262 * Writes to the CP210x configuration registers
241 * Values less than 16 bits wide are sent directly 263 * Values less than 16 bits wide are sent directly
242 * 'size' is specified in bytes. 264 * 'size' is specified in bytes.
243 */ 265 */
244static int cp2101_set_config(struct usb_serial_port *port, u8 request, 266static int cp210x_set_config(struct usb_serial_port *port, u8 request,
245 unsigned int *data, int size) 267 unsigned int *data, int size)
246{ 268{
247 struct usb_serial *serial = port->serial; 269 struct usb_serial *serial = port->serial;
@@ -292,21 +314,21 @@ static int cp2101_set_config(struct usb_serial_port *port, u8 request,
292} 314}
293 315
294/* 316/*
295 * cp2101_set_config_single 317 * cp210x_set_config_single
296 * Convenience function for calling cp2101_set_config on single data values 318 * Convenience function for calling cp210x_set_config on single data values
297 * without requiring an integer pointer 319 * without requiring an integer pointer
298 */ 320 */
299static inline int cp2101_set_config_single(struct usb_serial_port *port, 321static inline int cp210x_set_config_single(struct usb_serial_port *port,
300 u8 request, unsigned int data) 322 u8 request, unsigned int data)
301{ 323{
302 return cp2101_set_config(port, request, &data, 2); 324 return cp210x_set_config(port, request, &data, 2);
303} 325}
304 326
305/* 327/*
306 * cp2101_quantise_baudrate 328 * cp210x_quantise_baudrate
307 * Quantises the baud rate as per AN205 Table 1 329 * Quantises the baud rate as per AN205 Table 1
308 */ 330 */
309static unsigned int cp2101_quantise_baudrate(unsigned int baud) { 331static unsigned int cp210x_quantise_baudrate(unsigned int baud) {
310 if (baud <= 56) baud = 0; 332 if (baud <= 56) baud = 0;
311 else if (baud <= 300) baud = 300; 333 else if (baud <= 300) baud = 300;
312 else if (baud <= 600) baud = 600; 334 else if (baud <= 600) baud = 600;
@@ -343,7 +365,7 @@ static unsigned int cp2101_quantise_baudrate(unsigned int baud) {
343 return baud; 365 return baud;
344} 366}
345 367
346static int cp2101_open(struct tty_struct *tty, struct usb_serial_port *port, 368static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port,
347 struct file *filp) 369 struct file *filp)
348{ 370{
349 struct usb_serial *serial = port->serial; 371 struct usb_serial *serial = port->serial;
@@ -351,7 +373,7 @@ static int cp2101_open(struct tty_struct *tty, struct usb_serial_port *port,
351 373
352 dbg("%s - port %d", __func__, port->number); 374 dbg("%s - port %d", __func__, port->number);
353 375
354 if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) { 376 if (cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_ENABLE)) {
355 dev_err(&port->dev, "%s - Unable to enable UART\n", 377 dev_err(&port->dev, "%s - Unable to enable UART\n",
356 __func__); 378 __func__);
357 return -EPROTO; 379 return -EPROTO;
@@ -373,17 +395,17 @@ static int cp2101_open(struct tty_struct *tty, struct usb_serial_port *port,
373 } 395 }
374 396
375 /* Configure the termios structure */ 397 /* Configure the termios structure */
376 cp2101_get_termios(tty, port); 398 cp210x_get_termios(tty, port);
377 399
378 /* Set the DTR and RTS pins low */ 400 /* Set the DTR and RTS pins low */
379 cp2101_tiocmset_port(tty ? (struct usb_serial_port *) tty->driver_data 401 cp210x_tiocmset_port(tty ? (struct usb_serial_port *) tty->driver_data
380 : port, 402 : port,
381 NULL, TIOCM_DTR | TIOCM_RTS, 0); 403 NULL, TIOCM_DTR | TIOCM_RTS, 0);
382 404
383 return 0; 405 return 0;
384} 406}
385 407
386static void cp2101_cleanup(struct usb_serial_port *port) 408static void cp210x_cleanup(struct usb_serial_port *port)
387{ 409{
388 struct usb_serial *serial = port->serial; 410 struct usb_serial *serial = port->serial;
389 411
@@ -398,8 +420,7 @@ static void cp2101_cleanup(struct usb_serial_port *port)
398 } 420 }
399} 421}
400 422
401static void cp2101_close(struct tty_struct *tty, struct usb_serial_port *port, 423static void cp210x_close(struct usb_serial_port *port)
402 struct file *filp)
403{ 424{
404 dbg("%s - port %d", __func__, port->number); 425 dbg("%s - port %d", __func__, port->number);
405 426
@@ -410,23 +431,23 @@ static void cp2101_close(struct tty_struct *tty, struct usb_serial_port *port,
410 431
411 mutex_lock(&port->serial->disc_mutex); 432 mutex_lock(&port->serial->disc_mutex);
412 if (!port->serial->disconnected) 433 if (!port->serial->disconnected)
413 cp2101_set_config_single(port, CP2101_UART, UART_DISABLE); 434 cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_DISABLE);
414 mutex_unlock(&port->serial->disc_mutex); 435 mutex_unlock(&port->serial->disc_mutex);
415} 436}
416 437
417/* 438/*
418 * cp2101_get_termios 439 * cp210x_get_termios
419 * Reads the baud rate, data bits, parity, stop bits and flow control mode 440 * Reads the baud rate, data bits, parity, stop bits and flow control mode
420 * from the device, corrects any unsupported values, and configures the 441 * from the device, corrects any unsupported values, and configures the
421 * termios structure to reflect the state of the device 442 * termios structure to reflect the state of the device
422 */ 443 */
423static void cp2101_get_termios(struct tty_struct *tty, 444static void cp210x_get_termios(struct tty_struct *tty,
424 struct usb_serial_port *port) 445 struct usb_serial_port *port)
425{ 446{
426 unsigned int baud; 447 unsigned int baud;
427 448
428 if (tty) { 449 if (tty) {
429 cp2101_get_termios_port(tty->driver_data, 450 cp210x_get_termios_port(tty->driver_data,
430 &tty->termios->c_cflag, &baud); 451 &tty->termios->c_cflag, &baud);
431 tty_encode_baud_rate(tty, baud, baud); 452 tty_encode_baud_rate(tty, baud, baud);
432 } 453 }
@@ -434,15 +455,15 @@ static void cp2101_get_termios(struct tty_struct *tty,
434 else { 455 else {
435 unsigned int cflag; 456 unsigned int cflag;
436 cflag = 0; 457 cflag = 0;
437 cp2101_get_termios_port(port, &cflag, &baud); 458 cp210x_get_termios_port(port, &cflag, &baud);
438 } 459 }
439} 460}
440 461
441/* 462/*
442 * cp2101_get_termios_port 463 * cp210x_get_termios_port
443 * This is the heart of cp2101_get_termios which always uses a &usb_serial_port. 464 * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
444 */ 465 */
445static void cp2101_get_termios_port(struct usb_serial_port *port, 466static void cp210x_get_termios_port(struct usb_serial_port *port,
446 unsigned int *cflagp, unsigned int *baudp) 467 unsigned int *cflagp, unsigned int *baudp)
447{ 468{
448 unsigned int cflag, modem_ctl[4]; 469 unsigned int cflag, modem_ctl[4];
@@ -451,17 +472,17 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
451 472
452 dbg("%s - port %d", __func__, port->number); 473 dbg("%s - port %d", __func__, port->number);
453 474
454 cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2); 475 cp210x_get_config(port, CP210X_GET_BAUDDIV, &baud, 2);
455 /* Convert to baudrate */ 476 /* Convert to baudrate */
456 if (baud) 477 if (baud)
457 baud = cp2101_quantise_baudrate((BAUD_RATE_GEN_FREQ + baud/2)/ baud); 478 baud = cp210x_quantise_baudrate((BAUD_RATE_GEN_FREQ + baud/2)/ baud);
458 479
459 dbg("%s - baud rate = %d", __func__, baud); 480 dbg("%s - baud rate = %d", __func__, baud);
460 *baudp = baud; 481 *baudp = baud;
461 482
462 cflag = *cflagp; 483 cflag = *cflagp;
463 484
464 cp2101_get_config(port, CP2101_BITS, &bits, 2); 485 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
465 cflag &= ~CSIZE; 486 cflag &= ~CSIZE;
466 switch (bits & BITS_DATA_MASK) { 487 switch (bits & BITS_DATA_MASK) {
467 case BITS_DATA_5: 488 case BITS_DATA_5:
@@ -486,14 +507,14 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
486 cflag |= CS8; 507 cflag |= CS8;
487 bits &= ~BITS_DATA_MASK; 508 bits &= ~BITS_DATA_MASK;
488 bits |= BITS_DATA_8; 509 bits |= BITS_DATA_8;
489 cp2101_set_config(port, CP2101_BITS, &bits, 2); 510 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
490 break; 511 break;
491 default: 512 default:
492 dbg("%s - Unknown number of data bits, using 8", __func__); 513 dbg("%s - Unknown number of data bits, using 8", __func__);
493 cflag |= CS8; 514 cflag |= CS8;
494 bits &= ~BITS_DATA_MASK; 515 bits &= ~BITS_DATA_MASK;
495 bits |= BITS_DATA_8; 516 bits |= BITS_DATA_8;
496 cp2101_set_config(port, CP2101_BITS, &bits, 2); 517 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
497 break; 518 break;
498 } 519 }
499 520
@@ -516,20 +537,20 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
516 __func__); 537 __func__);
517 cflag &= ~PARENB; 538 cflag &= ~PARENB;
518 bits &= ~BITS_PARITY_MASK; 539 bits &= ~BITS_PARITY_MASK;
519 cp2101_set_config(port, CP2101_BITS, &bits, 2); 540 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
520 break; 541 break;
521 case BITS_PARITY_SPACE: 542 case BITS_PARITY_SPACE:
522 dbg("%s - parity = SPACE (not supported, disabling parity)", 543 dbg("%s - parity = SPACE (not supported, disabling parity)",
523 __func__); 544 __func__);
524 cflag &= ~PARENB; 545 cflag &= ~PARENB;
525 bits &= ~BITS_PARITY_MASK; 546 bits &= ~BITS_PARITY_MASK;
526 cp2101_set_config(port, CP2101_BITS, &bits, 2); 547 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
527 break; 548 break;
528 default: 549 default:
529 dbg("%s - Unknown parity mode, disabling parity", __func__); 550 dbg("%s - Unknown parity mode, disabling parity", __func__);
530 cflag &= ~PARENB; 551 cflag &= ~PARENB;
531 bits &= ~BITS_PARITY_MASK; 552 bits &= ~BITS_PARITY_MASK;
532 cp2101_set_config(port, CP2101_BITS, &bits, 2); 553 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
533 break; 554 break;
534 } 555 }
535 556
@@ -542,7 +563,7 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
542 dbg("%s - stop bits = 1.5 (not supported, using 1 stop bit)", 563 dbg("%s - stop bits = 1.5 (not supported, using 1 stop bit)",
543 __func__); 564 __func__);
544 bits &= ~BITS_STOP_MASK; 565 bits &= ~BITS_STOP_MASK;
545 cp2101_set_config(port, CP2101_BITS, &bits, 2); 566 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
546 break; 567 break;
547 case BITS_STOP_2: 568 case BITS_STOP_2:
548 dbg("%s - stop bits = 2", __func__); 569 dbg("%s - stop bits = 2", __func__);
@@ -552,11 +573,11 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
552 dbg("%s - Unknown number of stop bits, using 1 stop bit", 573 dbg("%s - Unknown number of stop bits, using 1 stop bit",
553 __func__); 574 __func__);
554 bits &= ~BITS_STOP_MASK; 575 bits &= ~BITS_STOP_MASK;
555 cp2101_set_config(port, CP2101_BITS, &bits, 2); 576 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
556 break; 577 break;
557 } 578 }
558 579
559 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16); 580 cp210x_get_config(port, CP210X_GET_FLOW, modem_ctl, 16);
560 if (modem_ctl[0] & 0x0008) { 581 if (modem_ctl[0] & 0x0008) {
561 dbg("%s - flow control = CRTSCTS", __func__); 582 dbg("%s - flow control = CRTSCTS", __func__);
562 cflag |= CRTSCTS; 583 cflag |= CRTSCTS;
@@ -568,7 +589,7 @@ static void cp2101_get_termios_port(struct usb_serial_port *port,
568 *cflagp = cflag; 589 *cflagp = cflag;
569} 590}
570 591
571static void cp2101_set_termios(struct tty_struct *tty, 592static void cp210x_set_termios(struct tty_struct *tty,
572 struct usb_serial_port *port, struct ktermios *old_termios) 593 struct usb_serial_port *port, struct ktermios *old_termios)
573{ 594{
574 unsigned int cflag, old_cflag; 595 unsigned int cflag, old_cflag;
@@ -583,13 +604,13 @@ static void cp2101_set_termios(struct tty_struct *tty,
583 tty->termios->c_cflag &= ~CMSPAR; 604 tty->termios->c_cflag &= ~CMSPAR;
584 cflag = tty->termios->c_cflag; 605 cflag = tty->termios->c_cflag;
585 old_cflag = old_termios->c_cflag; 606 old_cflag = old_termios->c_cflag;
586 baud = cp2101_quantise_baudrate(tty_get_baud_rate(tty)); 607 baud = cp210x_quantise_baudrate(tty_get_baud_rate(tty));
587 608
588 /* If the baud rate is to be updated*/ 609 /* If the baud rate is to be updated*/
589 if (baud != tty_termios_baud_rate(old_termios) && baud != 0) { 610 if (baud != tty_termios_baud_rate(old_termios) && baud != 0) {
590 dbg("%s - Setting baud rate to %d baud", __func__, 611 dbg("%s - Setting baud rate to %d baud", __func__,
591 baud); 612 baud);
592 if (cp2101_set_config_single(port, CP2101_BAUDRATE, 613 if (cp210x_set_config_single(port, CP210X_SET_BAUDDIV,
593 ((BAUD_RATE_GEN_FREQ + baud/2) / baud))) { 614 ((BAUD_RATE_GEN_FREQ + baud/2) / baud))) {
594 dbg("Baud rate requested not supported by device\n"); 615 dbg("Baud rate requested not supported by device\n");
595 baud = tty_termios_baud_rate(old_termios); 616 baud = tty_termios_baud_rate(old_termios);
@@ -600,7 +621,7 @@ static void cp2101_set_termios(struct tty_struct *tty,
600 621
601 /* If the number of data bits is to be updated */ 622 /* If the number of data bits is to be updated */
602 if ((cflag & CSIZE) != (old_cflag & CSIZE)) { 623 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
603 cp2101_get_config(port, CP2101_BITS, &bits, 2); 624 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
604 bits &= ~BITS_DATA_MASK; 625 bits &= ~BITS_DATA_MASK;
605 switch (cflag & CSIZE) { 626 switch (cflag & CSIZE) {
606 case CS5: 627 case CS5:
@@ -624,19 +645,19 @@ static void cp2101_set_termios(struct tty_struct *tty,
624 dbg("%s - data bits = 9", __func__); 645 dbg("%s - data bits = 9", __func__);
625 break;*/ 646 break;*/
626 default: 647 default:
627 dbg("cp2101 driver does not " 648 dbg("cp210x driver does not "
628 "support the number of bits requested," 649 "support the number of bits requested,"
629 " using 8 bit mode\n"); 650 " using 8 bit mode\n");
630 bits |= BITS_DATA_8; 651 bits |= BITS_DATA_8;
631 break; 652 break;
632 } 653 }
633 if (cp2101_set_config(port, CP2101_BITS, &bits, 2)) 654 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
634 dbg("Number of data bits requested " 655 dbg("Number of data bits requested "
635 "not supported by device\n"); 656 "not supported by device\n");
636 } 657 }
637 658
638 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) { 659 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
639 cp2101_get_config(port, CP2101_BITS, &bits, 2); 660 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
640 bits &= ~BITS_PARITY_MASK; 661 bits &= ~BITS_PARITY_MASK;
641 if (cflag & PARENB) { 662 if (cflag & PARENB) {
642 if (cflag & PARODD) { 663 if (cflag & PARODD) {
@@ -647,13 +668,13 @@ static void cp2101_set_termios(struct tty_struct *tty,
647 dbg("%s - parity = EVEN", __func__); 668 dbg("%s - parity = EVEN", __func__);
648 } 669 }
649 } 670 }
650 if (cp2101_set_config(port, CP2101_BITS, &bits, 2)) 671 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
651 dbg("Parity mode not supported " 672 dbg("Parity mode not supported "
652 "by device\n"); 673 "by device\n");
653 } 674 }
654 675
655 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) { 676 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
656 cp2101_get_config(port, CP2101_BITS, &bits, 2); 677 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
657 bits &= ~BITS_STOP_MASK; 678 bits &= ~BITS_STOP_MASK;
658 if (cflag & CSTOPB) { 679 if (cflag & CSTOPB) {
659 bits |= BITS_STOP_2; 680 bits |= BITS_STOP_2;
@@ -662,13 +683,13 @@ static void cp2101_set_termios(struct tty_struct *tty,
662 bits |= BITS_STOP_1; 683 bits |= BITS_STOP_1;
663 dbg("%s - stop bits = 1", __func__); 684 dbg("%s - stop bits = 1", __func__);
664 } 685 }
665 if (cp2101_set_config(port, CP2101_BITS, &bits, 2)) 686 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
666 dbg("Number of stop bits requested " 687 dbg("Number of stop bits requested "
667 "not supported by device\n"); 688 "not supported by device\n");
668 } 689 }
669 690
670 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) { 691 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
671 cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16); 692 cp210x_get_config(port, CP210X_GET_FLOW, modem_ctl, 16);
672 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x", 693 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
673 __func__, modem_ctl[0], modem_ctl[1], 694 __func__, modem_ctl[0], modem_ctl[1],
674 modem_ctl[2], modem_ctl[3]); 695 modem_ctl[2], modem_ctl[3]);
@@ -688,19 +709,19 @@ static void cp2101_set_termios(struct tty_struct *tty,
688 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x", 709 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
689 __func__, modem_ctl[0], modem_ctl[1], 710 __func__, modem_ctl[0], modem_ctl[1],
690 modem_ctl[2], modem_ctl[3]); 711 modem_ctl[2], modem_ctl[3]);
691 cp2101_set_config(port, CP2101_MODEMCTL, modem_ctl, 16); 712 cp210x_set_config(port, CP210X_SET_FLOW, modem_ctl, 16);
692 } 713 }
693 714
694} 715}
695 716
696static int cp2101_tiocmset (struct tty_struct *tty, struct file *file, 717static int cp210x_tiocmset (struct tty_struct *tty, struct file *file,
697 unsigned int set, unsigned int clear) 718 unsigned int set, unsigned int clear)
698{ 719{
699 struct usb_serial_port *port = tty->driver_data; 720 struct usb_serial_port *port = tty->driver_data;
700 return cp2101_tiocmset_port(port, file, set, clear); 721 return cp210x_tiocmset_port(port, file, set, clear);
701} 722}
702 723
703static int cp2101_tiocmset_port(struct usb_serial_port *port, struct file *file, 724static int cp210x_tiocmset_port(struct usb_serial_port *port, struct file *file,
704 unsigned int set, unsigned int clear) 725 unsigned int set, unsigned int clear)
705{ 726{
706 unsigned int control = 0; 727 unsigned int control = 0;
@@ -726,10 +747,10 @@ static int cp2101_tiocmset_port(struct usb_serial_port *port, struct file *file,
726 747
727 dbg("%s - control = 0x%.4x", __func__, control); 748 dbg("%s - control = 0x%.4x", __func__, control);
728 749
729 return cp2101_set_config(port, CP2101_CONTROL, &control, 2); 750 return cp210x_set_config(port, CP210X_SET_MHS, &control, 2);
730} 751}
731 752
732static int cp2101_tiocmget (struct tty_struct *tty, struct file *file) 753static int cp210x_tiocmget (struct tty_struct *tty, struct file *file)
733{ 754{
734 struct usb_serial_port *port = tty->driver_data; 755 struct usb_serial_port *port = tty->driver_data;
735 unsigned int control; 756 unsigned int control;
@@ -737,7 +758,7 @@ static int cp2101_tiocmget (struct tty_struct *tty, struct file *file)
737 758
738 dbg("%s - port %d", __func__, port->number); 759 dbg("%s - port %d", __func__, port->number);
739 760
740 cp2101_get_config(port, CP2101_CONTROL, &control, 1); 761 cp210x_get_config(port, CP210X_GET_MDMSTS, &control, 1);
741 762
742 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0) 763 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
743 |((control & CONTROL_RTS) ? TIOCM_RTS : 0) 764 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
@@ -751,7 +772,7 @@ static int cp2101_tiocmget (struct tty_struct *tty, struct file *file)
751 return result; 772 return result;
752} 773}
753 774
754static void cp2101_break_ctl (struct tty_struct *tty, int break_state) 775static void cp210x_break_ctl (struct tty_struct *tty, int break_state)
755{ 776{
756 struct usb_serial_port *port = tty->driver_data; 777 struct usb_serial_port *port = tty->driver_data;
757 unsigned int state; 778 unsigned int state;
@@ -763,17 +784,17 @@ static void cp2101_break_ctl (struct tty_struct *tty, int break_state)
763 state = BREAK_ON; 784 state = BREAK_ON;
764 dbg("%s - turning break %s", __func__, 785 dbg("%s - turning break %s", __func__,
765 state == BREAK_OFF ? "off" : "on"); 786 state == BREAK_OFF ? "off" : "on");
766 cp2101_set_config(port, CP2101_BREAK, &state, 2); 787 cp210x_set_config(port, CP210X_SET_BREAK, &state, 2);
767} 788}
768 789
769static int cp2101_startup(struct usb_serial *serial) 790static int cp210x_startup(struct usb_serial *serial)
770{ 791{
771 /* CP2101 buffers behave strangely unless device is reset */ 792 /* cp210x buffers behave strangely unless device is reset */
772 usb_reset_device(serial->dev); 793 usb_reset_device(serial->dev);
773 return 0; 794 return 0;
774} 795}
775 796
776static void cp2101_shutdown(struct usb_serial *serial) 797static void cp210x_disconnect(struct usb_serial *serial)
777{ 798{
778 int i; 799 int i;
779 800
@@ -781,21 +802,21 @@ static void cp2101_shutdown(struct usb_serial *serial)
781 802
782 /* Stop reads and writes on all ports */ 803 /* Stop reads and writes on all ports */
783 for (i = 0; i < serial->num_ports; ++i) 804 for (i = 0; i < serial->num_ports; ++i)
784 cp2101_cleanup(serial->port[i]); 805 cp210x_cleanup(serial->port[i]);
785} 806}
786 807
787static int __init cp2101_init(void) 808static int __init cp210x_init(void)
788{ 809{
789 int retval; 810 int retval;
790 811
791 retval = usb_serial_register(&cp2101_device); 812 retval = usb_serial_register(&cp210x_device);
792 if (retval) 813 if (retval)
793 return retval; /* Failed to register */ 814 return retval; /* Failed to register */
794 815
795 retval = usb_register(&cp2101_driver); 816 retval = usb_register(&cp210x_driver);
796 if (retval) { 817 if (retval) {
797 /* Failed to register */ 818 /* Failed to register */
798 usb_serial_deregister(&cp2101_device); 819 usb_serial_deregister(&cp210x_device);
799 return retval; 820 return retval;
800 } 821 }
801 822
@@ -805,14 +826,14 @@ static int __init cp2101_init(void)
805 return 0; 826 return 0;
806} 827}
807 828
808static void __exit cp2101_exit(void) 829static void __exit cp210x_exit(void)
809{ 830{
810 usb_deregister(&cp2101_driver); 831 usb_deregister(&cp210x_driver);
811 usb_serial_deregister(&cp2101_device); 832 usb_serial_deregister(&cp210x_device);
812} 833}
813 834
814module_init(cp2101_init); 835module_init(cp210x_init);
815module_exit(cp2101_exit); 836module_exit(cp210x_exit);
816 837
817MODULE_DESCRIPTION(DRIVER_DESC); 838MODULE_DESCRIPTION(DRIVER_DESC);
818MODULE_VERSION(DRIVER_VERSION); 839MODULE_VERSION(DRIVER_VERSION);
diff --git a/drivers/usb/serial/cyberjack.c b/drivers/usb/serial/cyberjack.c
index dd501bb63ed6..336523fd7366 100644
--- a/drivers/usb/serial/cyberjack.c
+++ b/drivers/usb/serial/cyberjack.c
@@ -58,11 +58,11 @@ static int debug;
58 58
59/* Function prototypes */ 59/* Function prototypes */
60static int cyberjack_startup(struct usb_serial *serial); 60static int cyberjack_startup(struct usb_serial *serial);
61static void cyberjack_shutdown(struct usb_serial *serial); 61static void cyberjack_disconnect(struct usb_serial *serial);
62static void cyberjack_release(struct usb_serial *serial);
62static int cyberjack_open(struct tty_struct *tty, 63static int cyberjack_open(struct tty_struct *tty,
63 struct usb_serial_port *port, struct file *filp); 64 struct usb_serial_port *port, struct file *filp);
64static void cyberjack_close(struct tty_struct *tty, 65static void cyberjack_close(struct usb_serial_port *port);
65 struct usb_serial_port *port, struct file *filp);
66static int cyberjack_write(struct tty_struct *tty, 66static int cyberjack_write(struct tty_struct *tty,
67 struct usb_serial_port *port, const unsigned char *buf, int count); 67 struct usb_serial_port *port, const unsigned char *buf, int count);
68static int cyberjack_write_room(struct tty_struct *tty); 68static int cyberjack_write_room(struct tty_struct *tty);
@@ -95,7 +95,8 @@ static struct usb_serial_driver cyberjack_device = {
95 .id_table = id_table, 95 .id_table = id_table,
96 .num_ports = 1, 96 .num_ports = 1,
97 .attach = cyberjack_startup, 97 .attach = cyberjack_startup,
98 .shutdown = cyberjack_shutdown, 98 .disconnect = cyberjack_disconnect,
99 .release = cyberjack_release,
99 .open = cyberjack_open, 100 .open = cyberjack_open,
100 .close = cyberjack_close, 101 .close = cyberjack_close,
101 .write = cyberjack_write, 102 .write = cyberjack_write,
@@ -149,17 +150,25 @@ static int cyberjack_startup(struct usb_serial *serial)
149 return 0; 150 return 0;
150} 151}
151 152
152static void cyberjack_shutdown(struct usb_serial *serial) 153static void cyberjack_disconnect(struct usb_serial *serial)
153{ 154{
154 int i; 155 int i;
155 156
156 dbg("%s", __func__); 157 dbg("%s", __func__);
157 158
158 for (i = 0; i < serial->num_ports; ++i) { 159 for (i = 0; i < serial->num_ports; ++i)
159 usb_kill_urb(serial->port[i]->interrupt_in_urb); 160 usb_kill_urb(serial->port[i]->interrupt_in_urb);
161}
162
163static void cyberjack_release(struct usb_serial *serial)
164{
165 int i;
166
167 dbg("%s", __func__);
168
169 for (i = 0; i < serial->num_ports; ++i) {
160 /* My special items, the standard routines free my urbs */ 170 /* My special items, the standard routines free my urbs */
161 kfree(usb_get_serial_port_data(serial->port[i])); 171 kfree(usb_get_serial_port_data(serial->port[i]));
162 usb_set_serial_port_data(serial->port[i], NULL);
163 } 172 }
164} 173}
165 174
@@ -185,8 +194,7 @@ static int cyberjack_open(struct tty_struct *tty,
185 return result; 194 return result;
186} 195}
187 196
188static void cyberjack_close(struct tty_struct *tty, 197static void cyberjack_close(struct usb_serial_port *port)
189 struct usb_serial_port *port, struct file *filp)
190{ 198{
191 dbg("%s - port %d", __func__, port->number); 199 dbg("%s - port %d", __func__, port->number);
192 200
diff --git a/drivers/usb/serial/cypress_m8.c b/drivers/usb/serial/cypress_m8.c
index e568710b263f..59adfe123110 100644
--- a/drivers/usb/serial/cypress_m8.c
+++ b/drivers/usb/serial/cypress_m8.c
@@ -171,11 +171,11 @@ struct cypress_buf {
171static int cypress_earthmate_startup(struct usb_serial *serial); 171static int cypress_earthmate_startup(struct usb_serial *serial);
172static int cypress_hidcom_startup(struct usb_serial *serial); 172static int cypress_hidcom_startup(struct usb_serial *serial);
173static int cypress_ca42v2_startup(struct usb_serial *serial); 173static int cypress_ca42v2_startup(struct usb_serial *serial);
174static void cypress_shutdown(struct usb_serial *serial); 174static void cypress_release(struct usb_serial *serial);
175static int cypress_open(struct tty_struct *tty, 175static int cypress_open(struct tty_struct *tty,
176 struct usb_serial_port *port, struct file *filp); 176 struct usb_serial_port *port, struct file *filp);
177static void cypress_close(struct tty_struct *tty, 177static void cypress_close(struct usb_serial_port *port);
178 struct usb_serial_port *port, struct file *filp); 178static void cypress_dtr_rts(struct usb_serial_port *port, int on);
179static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port, 179static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
180 const unsigned char *buf, int count); 180 const unsigned char *buf, int count);
181static void cypress_send(struct usb_serial_port *port); 181static void cypress_send(struct usb_serial_port *port);
@@ -215,9 +215,10 @@ static struct usb_serial_driver cypress_earthmate_device = {
215 .id_table = id_table_earthmate, 215 .id_table = id_table_earthmate,
216 .num_ports = 1, 216 .num_ports = 1,
217 .attach = cypress_earthmate_startup, 217 .attach = cypress_earthmate_startup,
218 .shutdown = cypress_shutdown, 218 .release = cypress_release,
219 .open = cypress_open, 219 .open = cypress_open,
220 .close = cypress_close, 220 .close = cypress_close,
221 .dtr_rts = cypress_dtr_rts,
221 .write = cypress_write, 222 .write = cypress_write,
222 .write_room = cypress_write_room, 223 .write_room = cypress_write_room,
223 .ioctl = cypress_ioctl, 224 .ioctl = cypress_ioctl,
@@ -241,9 +242,10 @@ static struct usb_serial_driver cypress_hidcom_device = {
241 .id_table = id_table_cyphidcomrs232, 242 .id_table = id_table_cyphidcomrs232,
242 .num_ports = 1, 243 .num_ports = 1,
243 .attach = cypress_hidcom_startup, 244 .attach = cypress_hidcom_startup,
244 .shutdown = cypress_shutdown, 245 .release = cypress_release,
245 .open = cypress_open, 246 .open = cypress_open,
246 .close = cypress_close, 247 .close = cypress_close,
248 .dtr_rts = cypress_dtr_rts,
247 .write = cypress_write, 249 .write = cypress_write,
248 .write_room = cypress_write_room, 250 .write_room = cypress_write_room,
249 .ioctl = cypress_ioctl, 251 .ioctl = cypress_ioctl,
@@ -267,9 +269,10 @@ static struct usb_serial_driver cypress_ca42v2_device = {
267 .id_table = id_table_nokiaca42v2, 269 .id_table = id_table_nokiaca42v2,
268 .num_ports = 1, 270 .num_ports = 1,
269 .attach = cypress_ca42v2_startup, 271 .attach = cypress_ca42v2_startup,
270 .shutdown = cypress_shutdown, 272 .release = cypress_release,
271 .open = cypress_open, 273 .open = cypress_open,
272 .close = cypress_close, 274 .close = cypress_close,
275 .dtr_rts = cypress_dtr_rts,
273 .write = cypress_write, 276 .write = cypress_write,
274 .write_room = cypress_write_room, 277 .write_room = cypress_write_room,
275 .ioctl = cypress_ioctl, 278 .ioctl = cypress_ioctl,
@@ -613,7 +616,7 @@ static int cypress_ca42v2_startup(struct usb_serial *serial)
613} /* cypress_ca42v2_startup */ 616} /* cypress_ca42v2_startup */
614 617
615 618
616static void cypress_shutdown(struct usb_serial *serial) 619static void cypress_release(struct usb_serial *serial)
617{ 620{
618 struct cypress_private *priv; 621 struct cypress_private *priv;
619 622
@@ -626,7 +629,6 @@ static void cypress_shutdown(struct usb_serial *serial)
626 if (priv) { 629 if (priv) {
627 cypress_buf_free(priv->buf); 630 cypress_buf_free(priv->buf);
628 kfree(priv); 631 kfree(priv);
629 usb_set_serial_port_data(serial->port[0], NULL);
630 } 632 }
631} 633}
632 634
@@ -656,11 +658,7 @@ static int cypress_open(struct tty_struct *tty,
656 priv->rx_flags = 0; 658 priv->rx_flags = 0;
657 spin_unlock_irqrestore(&priv->lock, flags); 659 spin_unlock_irqrestore(&priv->lock, flags);
658 660
659 /* raise both lines and set termios */ 661 /* Set termios */
660 spin_lock_irqsave(&priv->lock, flags);
661 priv->line_control = CONTROL_DTR | CONTROL_RTS;
662 priv->cmd_ctrl = 1;
663 spin_unlock_irqrestore(&priv->lock, flags);
664 result = cypress_write(tty, port, NULL, 0); 662 result = cypress_write(tty, port, NULL, 0);
665 663
666 if (result) { 664 if (result) {
@@ -694,76 +692,42 @@ static int cypress_open(struct tty_struct *tty,
694 __func__, result); 692 __func__, result);
695 cypress_set_dead(port); 693 cypress_set_dead(port);
696 } 694 }
697 695 port->port.drain_delay = 256;
698 return result; 696 return result;
699} /* cypress_open */ 697} /* cypress_open */
700 698
699static void cypress_dtr_rts(struct usb_serial_port *port, int on)
700{
701 struct cypress_private *priv = usb_get_serial_port_data(port);
702 /* drop dtr and rts */
703 priv = usb_get_serial_port_data(port);
704 spin_lock_irq(&priv->lock);
705 if (on == 0)
706 priv->line_control = 0;
707 else
708 priv->line_control = CONTROL_DTR | CONTROL_RTS;
709 priv->cmd_ctrl = 1;
710 spin_unlock_irq(&priv->lock);
711 cypress_write(NULL, port, NULL, 0);
712}
701 713
702static void cypress_close(struct tty_struct *tty, 714static void cypress_close(struct usb_serial_port *port)
703 struct usb_serial_port *port, struct file *filp)
704{ 715{
705 struct cypress_private *priv = usb_get_serial_port_data(port); 716 struct cypress_private *priv = usb_get_serial_port_data(port);
706 unsigned int c_cflag;
707 int bps;
708 long timeout;
709 wait_queue_t wait;
710 717
711 dbg("%s - port %d", __func__, port->number); 718 dbg("%s - port %d", __func__, port->number);
712 719
713 /* wait for data to drain from buffer */
714 spin_lock_irq(&priv->lock);
715 timeout = CYPRESS_CLOSING_WAIT;
716 init_waitqueue_entry(&wait, current);
717 add_wait_queue(&tty->write_wait, &wait);
718 for (;;) {
719 set_current_state(TASK_INTERRUPTIBLE);
720 if (cypress_buf_data_avail(priv->buf) == 0
721 || timeout == 0 || signal_pending(current)
722 /* without mutex, allowed due to harmless failure mode */
723 || port->serial->disconnected)
724 break;
725 spin_unlock_irq(&priv->lock);
726 timeout = schedule_timeout(timeout);
727 spin_lock_irq(&priv->lock);
728 }
729 set_current_state(TASK_RUNNING);
730 remove_wait_queue(&tty->write_wait, &wait);
731 /* clear out any remaining data in the buffer */
732 cypress_buf_clear(priv->buf);
733 spin_unlock_irq(&priv->lock);
734
735 /* writing is potentially harmful, lock must be taken */ 720 /* writing is potentially harmful, lock must be taken */
736 mutex_lock(&port->serial->disc_mutex); 721 mutex_lock(&port->serial->disc_mutex);
737 if (port->serial->disconnected) { 722 if (port->serial->disconnected) {
738 mutex_unlock(&port->serial->disc_mutex); 723 mutex_unlock(&port->serial->disc_mutex);
739 return; 724 return;
740 } 725 }
741 /* wait for characters to drain from device */ 726 cypress_buf_clear(priv->buf);
742 if (tty) {
743 bps = tty_get_baud_rate(tty);
744 if (bps > 1200)
745 timeout = max((HZ * 2560) / bps, HZ / 10);
746 else
747 timeout = 2 * HZ;
748 schedule_timeout_interruptible(timeout);
749 }
750
751 dbg("%s - stopping urbs", __func__); 727 dbg("%s - stopping urbs", __func__);
752 usb_kill_urb(port->interrupt_in_urb); 728 usb_kill_urb(port->interrupt_in_urb);
753 usb_kill_urb(port->interrupt_out_urb); 729 usb_kill_urb(port->interrupt_out_urb);
754 730
755 if (tty) {
756 c_cflag = tty->termios->c_cflag;
757 if (c_cflag & HUPCL) {
758 /* drop dtr and rts */
759 priv = usb_get_serial_port_data(port);
760 spin_lock_irq(&priv->lock);
761 priv->line_control = 0;
762 priv->cmd_ctrl = 1;
763 spin_unlock_irq(&priv->lock);
764 cypress_write(tty, port, NULL, 0);
765 }
766 }
767 731
768 if (stats) 732 if (stats)
769 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n", 733 dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
@@ -1264,8 +1228,8 @@ static void cypress_read_int_callback(struct urb *urb)
1264 /* precursor to disconnect so just go away */ 1228 /* precursor to disconnect so just go away */
1265 return; 1229 return;
1266 case -EPIPE: 1230 case -EPIPE:
1267 usb_clear_halt(port->serial->dev, 0x81); 1231 /* Can't call usb_clear_halt while in_interrupt */
1268 break; 1232 /* FALLS THROUGH */
1269 default: 1233 default:
1270 /* something ugly is going on... */ 1234 /* something ugly is going on... */
1271 dev_err(&urb->dev->dev, 1235 dev_err(&urb->dev->dev,
diff --git a/drivers/usb/serial/digi_acceleport.c b/drivers/usb/serial/digi_acceleport.c
index 38ba4ea8b6bf..f4808091c47c 100644
--- a/drivers/usb/serial/digi_acceleport.c
+++ b/drivers/usb/serial/digi_acceleport.c
@@ -422,7 +422,6 @@ struct digi_port {
422 int dp_throttled; 422 int dp_throttled;
423 int dp_throttle_restart; 423 int dp_throttle_restart;
424 wait_queue_head_t dp_flush_wait; 424 wait_queue_head_t dp_flush_wait;
425 int dp_in_close; /* close in progress */
426 wait_queue_head_t dp_close_wait; /* wait queue for close */ 425 wait_queue_head_t dp_close_wait; /* wait queue for close */
427 struct work_struct dp_wakeup_work; 426 struct work_struct dp_wakeup_work;
428 struct usb_serial_port *dp_port; 427 struct usb_serial_port *dp_port;
@@ -456,11 +455,13 @@ static int digi_write_room(struct tty_struct *tty);
456static int digi_chars_in_buffer(struct tty_struct *tty); 455static int digi_chars_in_buffer(struct tty_struct *tty);
457static int digi_open(struct tty_struct *tty, struct usb_serial_port *port, 456static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
458 struct file *filp); 457 struct file *filp);
459static void digi_close(struct tty_struct *tty, struct usb_serial_port *port, 458static void digi_close(struct usb_serial_port *port);
460 struct file *filp); 459static int digi_carrier_raised(struct usb_serial_port *port);
460static void digi_dtr_rts(struct usb_serial_port *port, int on);
461static int digi_startup_device(struct usb_serial *serial); 461static int digi_startup_device(struct usb_serial *serial);
462static int digi_startup(struct usb_serial *serial); 462static int digi_startup(struct usb_serial *serial);
463static void digi_shutdown(struct usb_serial *serial); 463static void digi_disconnect(struct usb_serial *serial);
464static void digi_release(struct usb_serial *serial);
464static void digi_read_bulk_callback(struct urb *urb); 465static void digi_read_bulk_callback(struct urb *urb);
465static int digi_read_inb_callback(struct urb *urb); 466static int digi_read_inb_callback(struct urb *urb);
466static int digi_read_oob_callback(struct urb *urb); 467static int digi_read_oob_callback(struct urb *urb);
@@ -510,6 +511,8 @@ static struct usb_serial_driver digi_acceleport_2_device = {
510 .num_ports = 3, 511 .num_ports = 3,
511 .open = digi_open, 512 .open = digi_open,
512 .close = digi_close, 513 .close = digi_close,
514 .dtr_rts = digi_dtr_rts,
515 .carrier_raised = digi_carrier_raised,
513 .write = digi_write, 516 .write = digi_write,
514 .write_room = digi_write_room, 517 .write_room = digi_write_room,
515 .write_bulk_callback = digi_write_bulk_callback, 518 .write_bulk_callback = digi_write_bulk_callback,
@@ -522,7 +525,8 @@ static struct usb_serial_driver digi_acceleport_2_device = {
522 .tiocmget = digi_tiocmget, 525 .tiocmget = digi_tiocmget,
523 .tiocmset = digi_tiocmset, 526 .tiocmset = digi_tiocmset,
524 .attach = digi_startup, 527 .attach = digi_startup,
525 .shutdown = digi_shutdown, 528 .disconnect = digi_disconnect,
529 .release = digi_release,
526}; 530};
527 531
528static struct usb_serial_driver digi_acceleport_4_device = { 532static struct usb_serial_driver digi_acceleport_4_device = {
@@ -548,7 +552,8 @@ static struct usb_serial_driver digi_acceleport_4_device = {
548 .tiocmget = digi_tiocmget, 552 .tiocmget = digi_tiocmget,
549 .tiocmset = digi_tiocmset, 553 .tiocmset = digi_tiocmset,
550 .attach = digi_startup, 554 .attach = digi_startup,
551 .shutdown = digi_shutdown, 555 .disconnect = digi_disconnect,
556 .release = digi_release,
552}; 557};
553 558
554 559
@@ -1328,6 +1333,19 @@ static int digi_chars_in_buffer(struct tty_struct *tty)
1328 1333
1329} 1334}
1330 1335
1336static void digi_dtr_rts(struct usb_serial_port *port, int on)
1337{
1338 /* Adjust DTR and RTS */
1339 digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1340}
1341
1342static int digi_carrier_raised(struct usb_serial_port *port)
1343{
1344 struct digi_port *priv = usb_get_serial_port_data(port);
1345 if (priv->dp_modem_signals & TIOCM_CD)
1346 return 1;
1347 return 0;
1348}
1331 1349
1332static int digi_open(struct tty_struct *tty, struct usb_serial_port *port, 1350static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
1333 struct file *filp) 1351 struct file *filp)
@@ -1336,7 +1354,6 @@ static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
1336 unsigned char buf[32]; 1354 unsigned char buf[32];
1337 struct digi_port *priv = usb_get_serial_port_data(port); 1355 struct digi_port *priv = usb_get_serial_port_data(port);
1338 struct ktermios not_termios; 1356 struct ktermios not_termios;
1339 unsigned long flags = 0;
1340 1357
1341 dbg("digi_open: TOP: port=%d, open_count=%d", 1358 dbg("digi_open: TOP: port=%d, open_count=%d",
1342 priv->dp_port_num, port->port.count); 1359 priv->dp_port_num, port->port.count);
@@ -1345,26 +1362,6 @@ static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
1345 if (digi_startup_device(port->serial) != 0) 1362 if (digi_startup_device(port->serial) != 0)
1346 return -ENXIO; 1363 return -ENXIO;
1347 1364
1348 spin_lock_irqsave(&priv->dp_port_lock, flags);
1349
1350 /* don't wait on a close in progress for non-blocking opens */
1351 if (priv->dp_in_close && (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0) {
1352 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1353 return -EAGAIN;
1354 }
1355
1356 /* wait for a close in progress to finish */
1357 while (priv->dp_in_close) {
1358 cond_wait_interruptible_timeout_irqrestore(
1359 &priv->dp_close_wait, DIGI_RETRY_TIMEOUT,
1360 &priv->dp_port_lock, flags);
1361 if (signal_pending(current))
1362 return -EINTR;
1363 spin_lock_irqsave(&priv->dp_port_lock, flags);
1364 }
1365
1366 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1367
1368 /* read modem signals automatically whenever they change */ 1365 /* read modem signals automatically whenever they change */
1369 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS; 1366 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1370 buf[1] = priv->dp_port_num; 1367 buf[1] = priv->dp_port_num;
@@ -1387,16 +1384,11 @@ static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
1387 not_termios.c_iflag = ~tty->termios->c_iflag; 1384 not_termios.c_iflag = ~tty->termios->c_iflag;
1388 digi_set_termios(tty, port, &not_termios); 1385 digi_set_termios(tty, port, &not_termios);
1389 } 1386 }
1390
1391 /* set DTR and RTS */
1392 digi_set_modem_signals(port, TIOCM_DTR|TIOCM_RTS, 1);
1393
1394 return 0; 1387 return 0;
1395} 1388}
1396 1389
1397 1390
1398static void digi_close(struct tty_struct *tty, struct usb_serial_port *port, 1391static void digi_close(struct usb_serial_port *port)
1399 struct file *filp)
1400{ 1392{
1401 DEFINE_WAIT(wait); 1393 DEFINE_WAIT(wait);
1402 int ret; 1394 int ret;
@@ -1411,28 +1403,9 @@ static void digi_close(struct tty_struct *tty, struct usb_serial_port *port,
1411 if (port->serial->disconnected) 1403 if (port->serial->disconnected)
1412 goto exit; 1404 goto exit;
1413 1405
1414 /* do cleanup only after final close on this port */
1415 spin_lock_irq(&priv->dp_port_lock);
1416 priv->dp_in_close = 1;
1417 spin_unlock_irq(&priv->dp_port_lock);
1418
1419 /* tell line discipline to process only XON/XOFF */
1420 tty->closing = 1;
1421
1422 /* wait for output to drain */
1423 if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0)
1424 tty_wait_until_sent(tty, DIGI_CLOSE_TIMEOUT);
1425
1426 /* flush driver and line discipline buffers */
1427 tty_driver_flush_buffer(tty);
1428 tty_ldisc_flush(tty);
1429
1430 if (port->serial->dev) { 1406 if (port->serial->dev) {
1431 /* wait for transmit idle */ 1407 /* FIXME: Transmit idle belongs in the wait_unti_sent path */
1432 if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0) 1408 digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1433 digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1434 /* drop DTR and RTS */
1435 digi_set_modem_signals(port, 0, 0);
1436 1409
1437 /* disable input flow control */ 1410 /* disable input flow control */
1438 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; 1411 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
@@ -1477,11 +1450,9 @@ static void digi_close(struct tty_struct *tty, struct usb_serial_port *port,
1477 /* shutdown any outstanding bulk writes */ 1450 /* shutdown any outstanding bulk writes */
1478 usb_kill_urb(port->write_urb); 1451 usb_kill_urb(port->write_urb);
1479 } 1452 }
1480 tty->closing = 0;
1481exit: 1453exit:
1482 spin_lock_irq(&priv->dp_port_lock); 1454 spin_lock_irq(&priv->dp_port_lock);
1483 priv->dp_write_urb_in_use = 0; 1455 priv->dp_write_urb_in_use = 0;
1484 priv->dp_in_close = 0;
1485 wake_up_interruptible(&priv->dp_close_wait); 1456 wake_up_interruptible(&priv->dp_close_wait);
1486 spin_unlock_irq(&priv->dp_port_lock); 1457 spin_unlock_irq(&priv->dp_port_lock);
1487 mutex_unlock(&port->serial->disc_mutex); 1458 mutex_unlock(&port->serial->disc_mutex);
@@ -1560,7 +1531,6 @@ static int digi_startup(struct usb_serial *serial)
1560 priv->dp_throttled = 0; 1531 priv->dp_throttled = 0;
1561 priv->dp_throttle_restart = 0; 1532 priv->dp_throttle_restart = 0;
1562 init_waitqueue_head(&priv->dp_flush_wait); 1533 init_waitqueue_head(&priv->dp_flush_wait);
1563 priv->dp_in_close = 0;
1564 init_waitqueue_head(&priv->dp_close_wait); 1534 init_waitqueue_head(&priv->dp_close_wait);
1565 INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock); 1535 INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1566 priv->dp_port = serial->port[i]; 1536 priv->dp_port = serial->port[i];
@@ -1589,16 +1559,23 @@ static int digi_startup(struct usb_serial *serial)
1589} 1559}
1590 1560
1591 1561
1592static void digi_shutdown(struct usb_serial *serial) 1562static void digi_disconnect(struct usb_serial *serial)
1593{ 1563{
1594 int i; 1564 int i;
1595 dbg("digi_shutdown: TOP, in_interrupt()=%ld", in_interrupt()); 1565 dbg("digi_disconnect: TOP, in_interrupt()=%ld", in_interrupt());
1596 1566
1597 /* stop reads and writes on all ports */ 1567 /* stop reads and writes on all ports */
1598 for (i = 0; i < serial->type->num_ports + 1; i++) { 1568 for (i = 0; i < serial->type->num_ports + 1; i++) {
1599 usb_kill_urb(serial->port[i]->read_urb); 1569 usb_kill_urb(serial->port[i]->read_urb);
1600 usb_kill_urb(serial->port[i]->write_urb); 1570 usb_kill_urb(serial->port[i]->write_urb);
1601 } 1571 }
1572}
1573
1574
1575static void digi_release(struct usb_serial *serial)
1576{
1577 int i;
1578 dbg("digi_release: TOP, in_interrupt()=%ld", in_interrupt());
1602 1579
1603 /* free the private data structures for all ports */ 1580 /* free the private data structures for all ports */
1604 /* number of regular ports + 1 for the out-of-band port */ 1581 /* number of regular ports + 1 for the out-of-band port */
diff --git a/drivers/usb/serial/empeg.c b/drivers/usb/serial/empeg.c
index c709ec474a80..80cb3471adbe 100644
--- a/drivers/usb/serial/empeg.c
+++ b/drivers/usb/serial/empeg.c
@@ -81,8 +81,7 @@ static int debug;
81/* function prototypes for an empeg-car player */ 81/* function prototypes for an empeg-car player */
82static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port, 82static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
83 struct file *filp); 83 struct file *filp);
84static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port, 84static void empeg_close(struct usb_serial_port *port);
85 struct file *filp);
86static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port, 85static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port,
87 const unsigned char *buf, 86 const unsigned char *buf,
88 int count); 87 int count);
@@ -91,7 +90,6 @@ static int empeg_chars_in_buffer(struct tty_struct *tty);
91static void empeg_throttle(struct tty_struct *tty); 90static void empeg_throttle(struct tty_struct *tty);
92static void empeg_unthrottle(struct tty_struct *tty); 91static void empeg_unthrottle(struct tty_struct *tty);
93static int empeg_startup(struct usb_serial *serial); 92static int empeg_startup(struct usb_serial *serial);
94static void empeg_shutdown(struct usb_serial *serial);
95static void empeg_set_termios(struct tty_struct *tty, 93static void empeg_set_termios(struct tty_struct *tty,
96 struct usb_serial_port *port, struct ktermios *old_termios); 94 struct usb_serial_port *port, struct ktermios *old_termios);
97static void empeg_write_bulk_callback(struct urb *urb); 95static void empeg_write_bulk_callback(struct urb *urb);
@@ -125,7 +123,6 @@ static struct usb_serial_driver empeg_device = {
125 .throttle = empeg_throttle, 123 .throttle = empeg_throttle,
126 .unthrottle = empeg_unthrottle, 124 .unthrottle = empeg_unthrottle,
127 .attach = empeg_startup, 125 .attach = empeg_startup,
128 .shutdown = empeg_shutdown,
129 .set_termios = empeg_set_termios, 126 .set_termios = empeg_set_termios,
130 .write = empeg_write, 127 .write = empeg_write,
131 .write_room = empeg_write_room, 128 .write_room = empeg_write_room,
@@ -181,8 +178,7 @@ static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port,
181} 178}
182 179
183 180
184static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port, 181static void empeg_close(struct usb_serial_port *port)
185 struct file *filp)
186{ 182{
187 dbg("%s - port %d", __func__, port->number); 183 dbg("%s - port %d", __func__, port->number);
188 184
@@ -429,12 +425,6 @@ static int empeg_startup(struct usb_serial *serial)
429} 425}
430 426
431 427
432static void empeg_shutdown(struct usb_serial *serial)
433{
434 dbg("%s", __func__);
435}
436
437
438static void empeg_set_termios(struct tty_struct *tty, 428static void empeg_set_termios(struct tty_struct *tty,
439 struct usb_serial_port *port, struct ktermios *old_termios) 429 struct usb_serial_port *port, struct ktermios *old_termios)
440{ 430{
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index d9fcdaedf389..60c64cc5be2a 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -33,6 +33,7 @@
33#include <linux/errno.h> 33#include <linux/errno.h>
34#include <linux/init.h> 34#include <linux/init.h>
35#include <linux/slab.h> 35#include <linux/slab.h>
36#include <linux/smp_lock.h>
36#include <linux/tty.h> 37#include <linux/tty.h>
37#include <linux/tty_driver.h> 38#include <linux/tty_driver.h>
38#include <linux/tty_flip.h> 39#include <linux/tty_flip.h>
@@ -47,7 +48,7 @@
47/* 48/*
48 * Version Information 49 * Version Information
49 */ 50 */
50#define DRIVER_VERSION "v1.4.3" 51#define DRIVER_VERSION "v1.5.0"
51#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>" 52#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Bill Ryder <bryder@sgi.com>, Kuba Ober <kuba@mareimbrium.org>"
52#define DRIVER_DESC "USB FTDI Serial Converters Driver" 53#define DRIVER_DESC "USB FTDI Serial Converters Driver"
53 54
@@ -82,17 +83,20 @@ struct ftdi_private {
82 int rx_processed; 83 int rx_processed;
83 unsigned long rx_bytes; 84 unsigned long rx_bytes;
84 85
85 __u16 interface; /* FT2232C port interface (0 for FT232/245) */ 86 __u16 interface; /* FT2232C, FT2232H or FT4232H port interface
87 (0 for FT232/245) */
86 88
87 speed_t force_baud; /* if non-zero, force the baud rate to 89 speed_t force_baud; /* if non-zero, force the baud rate to
88 this value */ 90 this value */
89 int force_rtscts; /* if non-zero, force RTS-CTS to always 91 int force_rtscts; /* if non-zero, force RTS-CTS to always
90 be enabled */ 92 be enabled */
91 93
94 unsigned int latency; /* latency setting in use */
92 spinlock_t tx_lock; /* spinlock for transmit state */ 95 spinlock_t tx_lock; /* spinlock for transmit state */
93 unsigned long tx_bytes; 96 unsigned long tx_bytes;
94 unsigned long tx_outstanding_bytes; 97 unsigned long tx_outstanding_bytes;
95 unsigned long tx_outstanding_urbs; 98 unsigned long tx_outstanding_urbs;
99 unsigned short max_packet_size;
96}; 100};
97 101
98/* struct ftdi_sio_quirk is used by devices requiring special attention. */ 102/* struct ftdi_sio_quirk is used by devices requiring special attention. */
@@ -104,6 +108,7 @@ struct ftdi_sio_quirk {
104 108
105static int ftdi_jtag_probe(struct usb_serial *serial); 109static int ftdi_jtag_probe(struct usb_serial *serial);
106static int ftdi_mtxorb_hack_setup(struct usb_serial *serial); 110static int ftdi_mtxorb_hack_setup(struct usb_serial *serial);
111static int ftdi_NDI_device_setup(struct usb_serial *serial);
107static void ftdi_USB_UIRT_setup(struct ftdi_private *priv); 112static void ftdi_USB_UIRT_setup(struct ftdi_private *priv);
108static void ftdi_HE_TIRA1_setup(struct ftdi_private *priv); 113static void ftdi_HE_TIRA1_setup(struct ftdi_private *priv);
109 114
@@ -115,6 +120,10 @@ static struct ftdi_sio_quirk ftdi_mtxorb_hack_quirk = {
115 .probe = ftdi_mtxorb_hack_setup, 120 .probe = ftdi_mtxorb_hack_setup,
116}; 121};
117 122
123static struct ftdi_sio_quirk ftdi_NDI_device_quirk = {
124 .probe = ftdi_NDI_device_setup,
125};
126
118static struct ftdi_sio_quirk ftdi_USB_UIRT_quirk = { 127static struct ftdi_sio_quirk ftdi_USB_UIRT_quirk = {
119 .port_probe = ftdi_USB_UIRT_setup, 128 .port_probe = ftdi_USB_UIRT_setup,
120}; 129};
@@ -163,6 +172,7 @@ static struct usb_device_id id_table_combined [] = {
163 { USB_DEVICE(FTDI_VID, FTDI_8U232AM_ALT_PID) }, 172 { USB_DEVICE(FTDI_VID, FTDI_8U232AM_ALT_PID) },
164 { USB_DEVICE(FTDI_VID, FTDI_232RL_PID) }, 173 { USB_DEVICE(FTDI_VID, FTDI_232RL_PID) },
165 { USB_DEVICE(FTDI_VID, FTDI_8U2232C_PID) }, 174 { USB_DEVICE(FTDI_VID, FTDI_8U2232C_PID) },
175 { USB_DEVICE(FTDI_VID, FTDI_4232H_PID) },
166 { USB_DEVICE(FTDI_VID, FTDI_MICRO_CHAMELEON_PID) }, 176 { USB_DEVICE(FTDI_VID, FTDI_MICRO_CHAMELEON_PID) },
167 { USB_DEVICE(FTDI_VID, FTDI_RELAIS_PID) }, 177 { USB_DEVICE(FTDI_VID, FTDI_RELAIS_PID) },
168 { USB_DEVICE(FTDI_VID, FTDI_OPENDCC_PID) }, 178 { USB_DEVICE(FTDI_VID, FTDI_OPENDCC_PID) },
@@ -187,6 +197,7 @@ static struct usb_device_id id_table_combined [] = {
187 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_4_PID) }, 197 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_4_PID) },
188 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_5_PID) }, 198 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_5_PID) },
189 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_6_PID) }, 199 { USB_DEVICE(FTDI_VID, FTDI_MTXORB_6_PID) },
200 { USB_DEVICE(FTDI_VID, FTDI_R2000KU_TRUE_RNG) },
190 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0100_PID) }, 201 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0100_PID) },
191 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0101_PID) }, 202 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0101_PID) },
192 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0102_PID) }, 203 { USB_DEVICE(MTXORB_VID, MTXORB_FTDI_RANGE_0102_PID) },
@@ -575,6 +586,9 @@ static struct usb_device_id id_table_combined [] = {
575 { USB_DEVICE(FTDI_VID, FTDI_CCSICDU20_0_PID) }, 586 { USB_DEVICE(FTDI_VID, FTDI_CCSICDU20_0_PID) },
576 { USB_DEVICE(FTDI_VID, FTDI_CCSICDU40_1_PID) }, 587 { USB_DEVICE(FTDI_VID, FTDI_CCSICDU40_1_PID) },
577 { USB_DEVICE(FTDI_VID, FTDI_CCSMACHX_2_PID) }, 588 { USB_DEVICE(FTDI_VID, FTDI_CCSMACHX_2_PID) },
589 { USB_DEVICE(FTDI_VID, FTDI_CCSLOAD_N_GO_3_PID) },
590 { USB_DEVICE(FTDI_VID, FTDI_CCSICDU64_4_PID) },
591 { USB_DEVICE(FTDI_VID, FTDI_CCSPRIME8_5_PID) },
578 { USB_DEVICE(FTDI_VID, INSIDE_ACCESSO) }, 592 { USB_DEVICE(FTDI_VID, INSIDE_ACCESSO) },
579 { USB_DEVICE(INTREPID_VID, INTREPID_VALUECAN_PID) }, 593 { USB_DEVICE(INTREPID_VID, INTREPID_VALUECAN_PID) },
580 { USB_DEVICE(INTREPID_VID, INTREPID_NEOVI_PID) }, 594 { USB_DEVICE(INTREPID_VID, INTREPID_NEOVI_PID) },
@@ -640,6 +654,16 @@ static struct usb_device_id id_table_combined [] = {
640 { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13S_PID) }, 654 { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13S_PID) },
641 { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13U_PID) }, 655 { USB_DEVICE(FTDI_VID, FTDI_TACTRIX_OPENPORT_13U_PID) },
642 { USB_DEVICE(ELEKTOR_VID, ELEKTOR_FT323R_PID) }, 656 { USB_DEVICE(ELEKTOR_VID, ELEKTOR_FT323R_PID) },
657 { USB_DEVICE(FTDI_VID, FTDI_NDI_HUC_PID),
658 .driver_info = (kernel_ulong_t)&ftdi_NDI_device_quirk },
659 { USB_DEVICE(FTDI_VID, FTDI_NDI_SPECTRA_SCU_PID),
660 .driver_info = (kernel_ulong_t)&ftdi_NDI_device_quirk },
661 { USB_DEVICE(FTDI_VID, FTDI_NDI_FUTURE_2_PID),
662 .driver_info = (kernel_ulong_t)&ftdi_NDI_device_quirk },
663 { USB_DEVICE(FTDI_VID, FTDI_NDI_FUTURE_3_PID),
664 .driver_info = (kernel_ulong_t)&ftdi_NDI_device_quirk },
665 { USB_DEVICE(FTDI_VID, FTDI_NDI_AURORA_SCU_PID),
666 .driver_info = (kernel_ulong_t)&ftdi_NDI_device_quirk },
643 { USB_DEVICE(TELLDUS_VID, TELLDUS_TELLSTICK_PID) }, 667 { USB_DEVICE(TELLDUS_VID, TELLDUS_TELLSTICK_PID) },
644 { USB_DEVICE(FTDI_VID, FTDI_MAXSTREAM_PID) }, 668 { USB_DEVICE(FTDI_VID, FTDI_MAXSTREAM_PID) },
645 { USB_DEVICE(FTDI_VID, FTDI_PHI_FISCO_PID) }, 669 { USB_DEVICE(FTDI_VID, FTDI_PHI_FISCO_PID) },
@@ -656,6 +680,8 @@ static struct usb_device_id id_table_combined [] = {
656 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 680 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
657 { USB_DEVICE(FTDI_VID, LMI_LM3S_EVAL_BOARD_PID), 681 { USB_DEVICE(FTDI_VID, LMI_LM3S_EVAL_BOARD_PID),
658 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 682 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
683 { USB_DEVICE(FTDI_VID, FTDI_TURTELIZER_PID),
684 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
659 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID_USB60F) }, 685 { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID_USB60F) },
660 { USB_DEVICE(FTDI_VID, FTDI_REU_TINY_PID) }, 686 { USB_DEVICE(FTDI_VID, FTDI_REU_TINY_PID) },
661 { USB_DEVICE(PAPOUCH_VID, PAPOUCH_QUIDO4x4_PID) }, 687 { USB_DEVICE(PAPOUCH_VID, PAPOUCH_QUIDO4x4_PID) },
@@ -663,7 +689,6 @@ static struct usb_device_id id_table_combined [] = {
663 { USB_DEVICE(FTDI_VID, FTDI_DOMINTELL_DUSB_PID) }, 689 { USB_DEVICE(FTDI_VID, FTDI_DOMINTELL_DUSB_PID) },
664 { USB_DEVICE(ALTI2_VID, ALTI2_N3_PID) }, 690 { USB_DEVICE(ALTI2_VID, ALTI2_N3_PID) },
665 { USB_DEVICE(FTDI_VID, DIEBOLD_BCS_SE923_PID) }, 691 { USB_DEVICE(FTDI_VID, DIEBOLD_BCS_SE923_PID) },
666 { USB_DEVICE(FTDI_VID, FTDI_NDI_HUC_PID) },
667 { USB_DEVICE(ATMEL_VID, STK541_PID) }, 692 { USB_DEVICE(ATMEL_VID, STK541_PID) },
668 { USB_DEVICE(DE_VID, STB_PID) }, 693 { USB_DEVICE(DE_VID, STB_PID) },
669 { USB_DEVICE(DE_VID, WHT_PID) }, 694 { USB_DEVICE(DE_VID, WHT_PID) },
@@ -672,6 +697,7 @@ static struct usb_device_id id_table_combined [] = {
672 { USB_DEVICE(JETI_VID, JETI_SPC1201_PID) }, 697 { USB_DEVICE(JETI_VID, JETI_SPC1201_PID) },
673 { USB_DEVICE(MARVELL_VID, MARVELL_SHEEVAPLUG_PID), 698 { USB_DEVICE(MARVELL_VID, MARVELL_SHEEVAPLUG_PID),
674 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk }, 699 .driver_info = (kernel_ulong_t)&ftdi_jtag_quirk },
700 { USB_DEVICE(LARSENBRUSGAARD_VID, LB_ALTITRACK_PID) },
675 { }, /* Optional parameter entry */ 701 { }, /* Optional parameter entry */
676 { } /* Terminating entry */ 702 { } /* Terminating entry */
677}; 703};
@@ -692,12 +718,13 @@ static const char *ftdi_chip_name[] = {
692 [FT232BM] = "FT232BM", 718 [FT232BM] = "FT232BM",
693 [FT2232C] = "FT2232C", 719 [FT2232C] = "FT2232C",
694 [FT232RL] = "FT232RL", 720 [FT232RL] = "FT232RL",
721 [FT2232H] = "FT2232H",
722 [FT4232H] = "FT4232H"
695}; 723};
696 724
697 725
698/* Constants for read urb and write urb */ 726/* Constants for read urb and write urb */
699#define BUFSZ 512 727#define BUFSZ 512
700#define PKTSZ 64
701 728
702/* rx_flags */ 729/* rx_flags */
703#define THROTTLED 0x01 730#define THROTTLED 0x01
@@ -714,13 +741,12 @@ static const char *ftdi_chip_name[] = {
714/* function prototypes for a FTDI serial converter */ 741/* function prototypes for a FTDI serial converter */
715static int ftdi_sio_probe(struct usb_serial *serial, 742static int ftdi_sio_probe(struct usb_serial *serial,
716 const struct usb_device_id *id); 743 const struct usb_device_id *id);
717static void ftdi_shutdown(struct usb_serial *serial);
718static int ftdi_sio_port_probe(struct usb_serial_port *port); 744static int ftdi_sio_port_probe(struct usb_serial_port *port);
719static int ftdi_sio_port_remove(struct usb_serial_port *port); 745static int ftdi_sio_port_remove(struct usb_serial_port *port);
720static int ftdi_open(struct tty_struct *tty, 746static int ftdi_open(struct tty_struct *tty,
721 struct usb_serial_port *port, struct file *filp); 747 struct usb_serial_port *port, struct file *filp);
722static void ftdi_close(struct tty_struct *tty, 748static void ftdi_close(struct usb_serial_port *port);
723 struct usb_serial_port *port, struct file *filp); 749static void ftdi_dtr_rts(struct usb_serial_port *port, int on);
724static int ftdi_write(struct tty_struct *tty, struct usb_serial_port *port, 750static int ftdi_write(struct tty_struct *tty, struct usb_serial_port *port,
725 const unsigned char *buf, int count); 751 const unsigned char *buf, int count);
726static int ftdi_write_room(struct tty_struct *tty); 752static int ftdi_write_room(struct tty_struct *tty);
@@ -743,6 +769,8 @@ static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base);
743static unsigned short int ftdi_232am_baud_to_divisor(int baud); 769static unsigned short int ftdi_232am_baud_to_divisor(int baud);
744static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base); 770static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base);
745static __u32 ftdi_232bm_baud_to_divisor(int baud); 771static __u32 ftdi_232bm_baud_to_divisor(int baud);
772static __u32 ftdi_2232h_baud_base_to_divisor(int baud, int base);
773static __u32 ftdi_2232h_baud_to_divisor(int baud);
746 774
747static struct usb_serial_driver ftdi_sio_device = { 775static struct usb_serial_driver ftdi_sio_device = {
748 .driver = { 776 .driver = {
@@ -758,6 +786,7 @@ static struct usb_serial_driver ftdi_sio_device = {
758 .port_remove = ftdi_sio_port_remove, 786 .port_remove = ftdi_sio_port_remove,
759 .open = ftdi_open, 787 .open = ftdi_open,
760 .close = ftdi_close, 788 .close = ftdi_close,
789 .dtr_rts = ftdi_dtr_rts,
761 .throttle = ftdi_throttle, 790 .throttle = ftdi_throttle,
762 .unthrottle = ftdi_unthrottle, 791 .unthrottle = ftdi_unthrottle,
763 .write = ftdi_write, 792 .write = ftdi_write,
@@ -770,7 +799,6 @@ static struct usb_serial_driver ftdi_sio_device = {
770 .ioctl = ftdi_ioctl, 799 .ioctl = ftdi_ioctl,
771 .set_termios = ftdi_set_termios, 800 .set_termios = ftdi_set_termios,
772 .break_ctl = ftdi_break_ctl, 801 .break_ctl = ftdi_break_ctl,
773 .shutdown = ftdi_shutdown,
774}; 802};
775 803
776 804
@@ -836,6 +864,36 @@ static __u32 ftdi_232bm_baud_to_divisor(int baud)
836 return ftdi_232bm_baud_base_to_divisor(baud, 48000000); 864 return ftdi_232bm_baud_base_to_divisor(baud, 48000000);
837} 865}
838 866
867static __u32 ftdi_2232h_baud_base_to_divisor(int baud, int base)
868{
869 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
870 __u32 divisor;
871 int divisor3;
872
873 /* hi-speed baud rate is 10-bit sampling instead of 16-bit */
874 divisor3 = (base / 10 / baud) * 8;
875
876 divisor = divisor3 >> 3;
877 divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
878 /* Deal with special cases for highest baud rates. */
879 if (divisor == 1)
880 divisor = 0;
881 else if (divisor == 0x4001)
882 divisor = 1;
883 /*
884 * Set this bit to turn off a divide by 2.5 on baud rate generator
885 * This enables baud rates up to 12Mbaud but cannot reach below 1200
886 * baud with this bit set
887 */
888 divisor |= 0x00020000;
889 return divisor;
890}
891
892static __u32 ftdi_2232h_baud_to_divisor(int baud)
893{
894 return ftdi_2232h_baud_base_to_divisor(baud, 120000000);
895}
896
839#define set_mctrl(port, set) update_mctrl((port), (set), 0) 897#define set_mctrl(port, set) update_mctrl((port), (set), 0)
840#define clear_mctrl(port, clear) update_mctrl((port), 0, (clear)) 898#define clear_mctrl(port, clear) update_mctrl((port), 0, (clear))
841 899
@@ -986,6 +1044,29 @@ static __u32 get_ftdi_divisor(struct tty_struct *tty,
986 case FT2232C: /* FT2232C chip */ 1044 case FT2232C: /* FT2232C chip */
987 case FT232RL: 1045 case FT232RL:
988 if (baud <= 3000000) { 1046 if (baud <= 3000000) {
1047 __u16 product_id = le16_to_cpu(
1048 port->serial->dev->descriptor.idProduct);
1049 if (((FTDI_NDI_HUC_PID == product_id) ||
1050 (FTDI_NDI_SPECTRA_SCU_PID == product_id) ||
1051 (FTDI_NDI_FUTURE_2_PID == product_id) ||
1052 (FTDI_NDI_FUTURE_3_PID == product_id) ||
1053 (FTDI_NDI_AURORA_SCU_PID == product_id)) &&
1054 (baud == 19200)) {
1055 baud = 1200000;
1056 }
1057 div_value = ftdi_232bm_baud_to_divisor(baud);
1058 } else {
1059 dbg("%s - Baud rate too high!", __func__);
1060 div_value = ftdi_232bm_baud_to_divisor(9600);
1061 div_okay = 0;
1062 baud = 9600;
1063 }
1064 break;
1065 case FT2232H: /* FT2232H chip */
1066 case FT4232H: /* FT4232H chip */
1067 if ((baud <= 12000000) & (baud >= 1200)) {
1068 div_value = ftdi_2232h_baud_to_divisor(baud);
1069 } else if (baud < 1200) {
989 div_value = ftdi_232bm_baud_to_divisor(baud); 1070 div_value = ftdi_232bm_baud_to_divisor(baud);
990 } else { 1071 } else {
991 dbg("%s - Baud rate too high!", __func__); 1072 dbg("%s - Baud rate too high!", __func__);
@@ -1037,7 +1118,54 @@ static int change_speed(struct tty_struct *tty, struct usb_serial_port *port)
1037 return rv; 1118 return rv;
1038} 1119}
1039 1120
1121static int write_latency_timer(struct usb_serial_port *port)
1122{
1123 struct ftdi_private *priv = usb_get_serial_port_data(port);
1124 struct usb_device *udev = port->serial->dev;
1125 char buf[1];
1126 int rv = 0;
1127 int l = priv->latency;
1128
1129 if (priv->flags & ASYNC_LOW_LATENCY)
1130 l = 1;
1131
1132 dbg("%s: setting latency timer = %i", __func__, l);
1133
1134 rv = usb_control_msg(udev,
1135 usb_sndctrlpipe(udev, 0),
1136 FTDI_SIO_SET_LATENCY_TIMER_REQUEST,
1137 FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,
1138 l, priv->interface,
1139 buf, 0, WDR_TIMEOUT);
1140
1141 if (rv < 0)
1142 dev_err(&port->dev, "Unable to write latency timer: %i\n", rv);
1143 return rv;
1144}
1145
1146static int read_latency_timer(struct usb_serial_port *port)
1147{
1148 struct ftdi_private *priv = usb_get_serial_port_data(port);
1149 struct usb_device *udev = port->serial->dev;
1150 unsigned short latency = 0;
1151 int rv = 0;
1152
1153
1154 dbg("%s", __func__);
1040 1155
1156 rv = usb_control_msg(udev,
1157 usb_rcvctrlpipe(udev, 0),
1158 FTDI_SIO_GET_LATENCY_TIMER_REQUEST,
1159 FTDI_SIO_GET_LATENCY_TIMER_REQUEST_TYPE,
1160 0, priv->interface,
1161 (char *) &latency, 1, WDR_TIMEOUT);
1162
1163 if (rv < 0) {
1164 dev_err(&port->dev, "Unable to read latency timer: %i\n", rv);
1165 return -EIO;
1166 }
1167 return latency;
1168}
1041 1169
1042static int get_serial_info(struct usb_serial_port *port, 1170static int get_serial_info(struct usb_serial_port *port,
1043 struct serial_struct __user *retinfo) 1171 struct serial_struct __user *retinfo)
@@ -1097,6 +1225,7 @@ static int set_serial_info(struct tty_struct *tty,
1097 priv->custom_divisor = new_serial.custom_divisor; 1225 priv->custom_divisor = new_serial.custom_divisor;
1098 1226
1099 tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0; 1227 tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1228 write_latency_timer(port);
1100 1229
1101check_and_exit: 1230check_and_exit:
1102 if ((old_priv.flags & ASYNC_SPD_MASK) != 1231 if ((old_priv.flags & ASYNC_SPD_MASK) !=
@@ -1146,14 +1275,29 @@ static void ftdi_determine_type(struct usb_serial_port *port)
1146 if (interfaces > 1) { 1275 if (interfaces > 1) {
1147 int inter; 1276 int inter;
1148 1277
1149 /* Multiple interfaces. Assume FT2232C. */ 1278 /* Multiple interfaces.*/
1150 priv->chip_type = FT2232C; 1279 if (version == 0x0800) {
1280 priv->chip_type = FT4232H;
1281 /* Hi-speed - baud clock runs at 120MHz */
1282 priv->baud_base = 120000000 / 2;
1283 } else if (version == 0x0700) {
1284 priv->chip_type = FT2232H;
1285 /* Hi-speed - baud clock runs at 120MHz */
1286 priv->baud_base = 120000000 / 2;
1287 } else
1288 priv->chip_type = FT2232C;
1289
1151 /* Determine interface code. */ 1290 /* Determine interface code. */
1152 inter = serial->interface->altsetting->desc.bInterfaceNumber; 1291 inter = serial->interface->altsetting->desc.bInterfaceNumber;
1153 if (inter == 0) 1292 if (inter == 0) {
1154 priv->interface = PIT_SIOA; 1293 priv->interface = INTERFACE_A;
1155 else 1294 } else if (inter == 1) {
1156 priv->interface = PIT_SIOB; 1295 priv->interface = INTERFACE_B;
1296 } else if (inter == 2) {
1297 priv->interface = INTERFACE_C;
1298 } else if (inter == 3) {
1299 priv->interface = INTERFACE_D;
1300 }
1157 /* BM-type devices have a bug where bcdDevice gets set 1301 /* BM-type devices have a bug where bcdDevice gets set
1158 * to 0x200 when iSerialNumber is 0. */ 1302 * to 0x200 when iSerialNumber is 0. */
1159 if (version < 0x500) { 1303 if (version < 0x500) {
@@ -1181,6 +1325,45 @@ static void ftdi_determine_type(struct usb_serial_port *port)
1181} 1325}
1182 1326
1183 1327
1328/* Determine the maximum packet size for the device. This depends on the chip
1329 * type and the USB host capabilities. The value should be obtained from the
1330 * device descriptor as the chip will use the appropriate values for the host.*/
1331static void ftdi_set_max_packet_size(struct usb_serial_port *port)
1332{
1333 struct ftdi_private *priv = usb_get_serial_port_data(port);
1334 struct usb_serial *serial = port->serial;
1335 struct usb_device *udev = serial->dev;
1336
1337 struct usb_interface *interface = serial->interface;
1338 struct usb_endpoint_descriptor *ep_desc = &interface->cur_altsetting->endpoint[1].desc;
1339
1340 unsigned num_endpoints;
1341 int i = 0;
1342
1343 num_endpoints = interface->cur_altsetting->desc.bNumEndpoints;
1344 dev_info(&udev->dev, "Number of endpoints %d\n", num_endpoints);
1345
1346 /* NOTE: some customers have programmed FT232R/FT245R devices
1347 * with an endpoint size of 0 - not good. In this case, we
1348 * want to override the endpoint descriptor setting and use a
1349 * value of 64 for wMaxPacketSize */
1350 for (i = 0; i < num_endpoints; i++) {
1351 dev_info(&udev->dev, "Endpoint %d MaxPacketSize %d\n", i+1,
1352 interface->cur_altsetting->endpoint[i].desc.wMaxPacketSize);
1353 ep_desc = &interface->cur_altsetting->endpoint[i].desc;
1354 if (ep_desc->wMaxPacketSize == 0) {
1355 ep_desc->wMaxPacketSize = cpu_to_le16(0x40);
1356 dev_info(&udev->dev, "Overriding wMaxPacketSize on endpoint %d\n", i);
1357 }
1358 }
1359
1360 /* set max packet size based on descriptor */
1361 priv->max_packet_size = ep_desc->wMaxPacketSize;
1362
1363 dev_info(&udev->dev, "Setting MaxPacketSize %d\n", priv->max_packet_size);
1364}
1365
1366
1184/* 1367/*
1185 * *************************************************************************** 1368 * ***************************************************************************
1186 * Sysfs Attribute 1369 * Sysfs Attribute
@@ -1192,27 +1375,13 @@ static ssize_t show_latency_timer(struct device *dev,
1192{ 1375{
1193 struct usb_serial_port *port = to_usb_serial_port(dev); 1376 struct usb_serial_port *port = to_usb_serial_port(dev);
1194 struct ftdi_private *priv = usb_get_serial_port_data(port); 1377 struct ftdi_private *priv = usb_get_serial_port_data(port);
1195 struct usb_device *udev = port->serial->dev; 1378 if (priv->flags & ASYNC_LOW_LATENCY)
1196 unsigned short latency = 0; 1379 return sprintf(buf, "1\n");
1197 int rv = 0; 1380 else
1198 1381 return sprintf(buf, "%i\n", priv->latency);
1199
1200 dbg("%s", __func__);
1201
1202 rv = usb_control_msg(udev,
1203 usb_rcvctrlpipe(udev, 0),
1204 FTDI_SIO_GET_LATENCY_TIMER_REQUEST,
1205 FTDI_SIO_GET_LATENCY_TIMER_REQUEST_TYPE,
1206 0, priv->interface,
1207 (char *) &latency, 1, WDR_TIMEOUT);
1208
1209 if (rv < 0) {
1210 dev_err(dev, "Unable to read latency timer: %i\n", rv);
1211 return -EIO;
1212 }
1213 return sprintf(buf, "%i\n", latency);
1214} 1382}
1215 1383
1384
1216/* Write a new value of the latency timer, in units of milliseconds. */ 1385/* Write a new value of the latency timer, in units of milliseconds. */
1217static ssize_t store_latency_timer(struct device *dev, 1386static ssize_t store_latency_timer(struct device *dev,
1218 struct device_attribute *attr, const char *valbuf, 1387 struct device_attribute *attr, const char *valbuf,
@@ -1220,25 +1389,13 @@ static ssize_t store_latency_timer(struct device *dev,
1220{ 1389{
1221 struct usb_serial_port *port = to_usb_serial_port(dev); 1390 struct usb_serial_port *port = to_usb_serial_port(dev);
1222 struct ftdi_private *priv = usb_get_serial_port_data(port); 1391 struct ftdi_private *priv = usb_get_serial_port_data(port);
1223 struct usb_device *udev = port->serial->dev;
1224 char buf[1];
1225 int v = simple_strtoul(valbuf, NULL, 10); 1392 int v = simple_strtoul(valbuf, NULL, 10);
1226 int rv = 0; 1393 int rv = 0;
1227 1394
1228 dbg("%s: setting latency timer = %i", __func__, v); 1395 priv->latency = v;
1229 1396 rv = write_latency_timer(port);
1230 rv = usb_control_msg(udev, 1397 if (rv < 0)
1231 usb_sndctrlpipe(udev, 0),
1232 FTDI_SIO_SET_LATENCY_TIMER_REQUEST,
1233 FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,
1234 v, priv->interface,
1235 buf, 0, WDR_TIMEOUT);
1236
1237 if (rv < 0) {
1238 dev_err(dev, "Unable to write latency timer: %i\n", rv);
1239 return -EIO; 1398 return -EIO;
1240 }
1241
1242 return count; 1399 return count;
1243} 1400}
1244 1401
@@ -1290,7 +1447,9 @@ static int create_sysfs_attrs(struct usb_serial_port *port)
1290 if ((!retval) && 1447 if ((!retval) &&
1291 (priv->chip_type == FT232BM || 1448 (priv->chip_type == FT232BM ||
1292 priv->chip_type == FT2232C || 1449 priv->chip_type == FT2232C ||
1293 priv->chip_type == FT232RL)) { 1450 priv->chip_type == FT232RL ||
1451 priv->chip_type == FT2232H ||
1452 priv->chip_type == FT4232H)) {
1294 retval = device_create_file(&port->dev, 1453 retval = device_create_file(&port->dev,
1295 &dev_attr_latency_timer); 1454 &dev_attr_latency_timer);
1296 } 1455 }
@@ -1309,7 +1468,9 @@ static void remove_sysfs_attrs(struct usb_serial_port *port)
1309 device_remove_file(&port->dev, &dev_attr_event_char); 1468 device_remove_file(&port->dev, &dev_attr_event_char);
1310 if (priv->chip_type == FT232BM || 1469 if (priv->chip_type == FT232BM ||
1311 priv->chip_type == FT2232C || 1470 priv->chip_type == FT2232C ||
1312 priv->chip_type == FT232RL) { 1471 priv->chip_type == FT232RL ||
1472 priv->chip_type == FT2232H ||
1473 priv->chip_type == FT4232H) {
1313 device_remove_file(&port->dev, &dev_attr_latency_timer); 1474 device_remove_file(&port->dev, &dev_attr_latency_timer);
1314 } 1475 }
1315 } 1476 }
@@ -1392,6 +1553,8 @@ static int ftdi_sio_port_probe(struct usb_serial_port *port)
1392 usb_set_serial_port_data(port, priv); 1553 usb_set_serial_port_data(port, priv);
1393 1554
1394 ftdi_determine_type(port); 1555 ftdi_determine_type(port);
1556 ftdi_set_max_packet_size(port);
1557 read_latency_timer(port);
1395 create_sysfs_attrs(port); 1558 create_sysfs_attrs(port);
1396 return 0; 1559 return 0;
1397} 1560}
@@ -1422,6 +1585,39 @@ static void ftdi_HE_TIRA1_setup(struct ftdi_private *priv)
1422} /* ftdi_HE_TIRA1_setup */ 1585} /* ftdi_HE_TIRA1_setup */
1423 1586
1424/* 1587/*
1588 * Module parameter to control latency timer for NDI FTDI-based USB devices.
1589 * If this value is not set in modprobe.conf.local its value will be set to 1ms.
1590 */
1591static int ndi_latency_timer = 1;
1592
1593/* Setup for the NDI FTDI-based USB devices, which requires hardwired
1594 * baudrate (19200 gets mapped to 1200000).
1595 *
1596 * Called from usbserial:serial_probe.
1597 */
1598static int ftdi_NDI_device_setup(struct usb_serial *serial)
1599{
1600 struct usb_device *udev = serial->dev;
1601 int latency = ndi_latency_timer;
1602 int rv = 0;
1603 char buf[1];
1604
1605 if (latency == 0)
1606 latency = 1;
1607 if (latency > 99)
1608 latency = 99;
1609
1610 dbg("%s setting NDI device latency to %d", __func__, latency);
1611 dev_info(&udev->dev, "NDI device with a latency value of %d", latency);
1612
1613 rv = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1614 FTDI_SIO_SET_LATENCY_TIMER_REQUEST,
1615 FTDI_SIO_SET_LATENCY_TIMER_REQUEST_TYPE,
1616 latency, 0, buf, 0, WDR_TIMEOUT);
1617 return 0;
1618}
1619
1620/*
1425 * First port on JTAG adaptors such as Olimex arm-usb-ocd or the FIC/OpenMoko 1621 * First port on JTAG adaptors such as Olimex arm-usb-ocd or the FIC/OpenMoko
1426 * Neo1973 Debug Board is reserved for JTAG interface and can be accessed from 1622 * Neo1973 Debug Board is reserved for JTAG interface and can be accessed from
1427 * userspace using openocd. 1623 * userspace using openocd.
@@ -1460,18 +1656,6 @@ static int ftdi_mtxorb_hack_setup(struct usb_serial *serial)
1460 return 0; 1656 return 0;
1461} 1657}
1462 1658
1463/* ftdi_shutdown is called from usbserial:usb_serial_disconnect
1464 * it is called when the usb device is disconnected
1465 *
1466 * usbserial:usb_serial_disconnect
1467 * calls __serial_close for each open of the port
1468 * shutdown is called then (ie ftdi_shutdown)
1469 */
1470static void ftdi_shutdown(struct usb_serial *serial)
1471{
1472 dbg("%s", __func__);
1473}
1474
1475static void ftdi_sio_priv_release(struct kref *k) 1659static void ftdi_sio_priv_release(struct kref *k)
1476{ 1660{
1477 struct ftdi_private *priv = container_of(k, struct ftdi_private, kref); 1661 struct ftdi_private *priv = container_of(k, struct ftdi_private, kref);
@@ -1514,6 +1698,8 @@ static int ftdi_open(struct tty_struct *tty,
1514 if (tty) 1698 if (tty)
1515 tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0; 1699 tty->low_latency = (priv->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1516 1700
1701 write_latency_timer(port);
1702
1517 /* No error checking for this (will get errors later anyway) */ 1703 /* No error checking for this (will get errors later anyway) */
1518 /* See ftdi_sio.h for description of what is reset */ 1704 /* See ftdi_sio.h for description of what is reset */
1519 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1705 usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
@@ -1529,11 +1715,6 @@ static int ftdi_open(struct tty_struct *tty,
1529 if (tty) 1715 if (tty)
1530 ftdi_set_termios(tty, port, tty->termios); 1716 ftdi_set_termios(tty, port, tty->termios);
1531 1717
1532 /* FIXME: Flow control might be enabled, so it should be checked -
1533 we have no control of defaults! */
1534 /* Turn on RTS and DTR since we are not flow controlling by default */
1535 set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1536
1537 /* Not throttled */ 1718 /* Not throttled */
1538 spin_lock_irqsave(&priv->rx_lock, flags); 1719 spin_lock_irqsave(&priv->rx_lock, flags);
1539 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED); 1720 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
@@ -1558,6 +1739,30 @@ static int ftdi_open(struct tty_struct *tty,
1558} /* ftdi_open */ 1739} /* ftdi_open */
1559 1740
1560 1741
1742static void ftdi_dtr_rts(struct usb_serial_port *port, int on)
1743{
1744 struct ftdi_private *priv = usb_get_serial_port_data(port);
1745 char buf[1];
1746
1747 mutex_lock(&port->serial->disc_mutex);
1748 if (!port->serial->disconnected) {
1749 /* Disable flow control */
1750 if (!on && usb_control_msg(port->serial->dev,
1751 usb_sndctrlpipe(port->serial->dev, 0),
1752 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
1753 FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1754 0, priv->interface, buf, 0,
1755 WDR_TIMEOUT) < 0) {
1756 dev_err(&port->dev, "error from flowcontrol urb\n");
1757 }
1758 /* drop RTS and DTR */
1759 if (on)
1760 set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1761 else
1762 clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1763 }
1764 mutex_unlock(&port->serial->disc_mutex);
1765}
1561 1766
1562/* 1767/*
1563 * usbserial:__serial_close only calls ftdi_close if the point is open 1768 * usbserial:__serial_close only calls ftdi_close if the point is open
@@ -1567,31 +1772,12 @@ static int ftdi_open(struct tty_struct *tty,
1567 * 1772 *
1568 */ 1773 */
1569 1774
1570static void ftdi_close(struct tty_struct *tty, 1775static void ftdi_close(struct usb_serial_port *port)
1571 struct usb_serial_port *port, struct file *filp)
1572{ /* ftdi_close */ 1776{ /* ftdi_close */
1573 unsigned int c_cflag = tty->termios->c_cflag;
1574 struct ftdi_private *priv = usb_get_serial_port_data(port); 1777 struct ftdi_private *priv = usb_get_serial_port_data(port);
1575 char buf[1];
1576 1778
1577 dbg("%s", __func__); 1779 dbg("%s", __func__);
1578 1780
1579 mutex_lock(&port->serial->disc_mutex);
1580 if (c_cflag & HUPCL && !port->serial->disconnected) {
1581 /* Disable flow control */
1582 if (usb_control_msg(port->serial->dev,
1583 usb_sndctrlpipe(port->serial->dev, 0),
1584 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
1585 FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1586 0, priv->interface, buf, 0,
1587 WDR_TIMEOUT) < 0) {
1588 dev_err(&port->dev, "error from flowcontrol urb\n");
1589 }
1590
1591 /* drop RTS and DTR */
1592 clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
1593 } /* Note change no line if hupcl is off */
1594 mutex_unlock(&port->serial->disc_mutex);
1595 1781
1596 /* cancel any scheduled reading */ 1782 /* cancel any scheduled reading */
1597 cancel_delayed_work_sync(&priv->rx_work); 1783 cancel_delayed_work_sync(&priv->rx_work);
@@ -1644,8 +1830,8 @@ static int ftdi_write(struct tty_struct *tty, struct usb_serial_port *port,
1644 if (data_offset > 0) { 1830 if (data_offset > 0) {
1645 /* Original sio needs control bytes too... */ 1831 /* Original sio needs control bytes too... */
1646 transfer_size += (data_offset * 1832 transfer_size += (data_offset *
1647 ((count + (PKTSZ - 1 - data_offset)) / 1833 ((count + (priv->max_packet_size - 1 - data_offset)) /
1648 (PKTSZ - data_offset))); 1834 (priv->max_packet_size - data_offset)));
1649 } 1835 }
1650 1836
1651 buffer = kmalloc(transfer_size, GFP_ATOMIC); 1837 buffer = kmalloc(transfer_size, GFP_ATOMIC);
@@ -1667,7 +1853,7 @@ static int ftdi_write(struct tty_struct *tty, struct usb_serial_port *port,
1667 if (data_offset > 0) { 1853 if (data_offset > 0) {
1668 /* Original sio requires control byte at start of 1854 /* Original sio requires control byte at start of
1669 each packet. */ 1855 each packet. */
1670 int user_pktsz = PKTSZ - data_offset; 1856 int user_pktsz = priv->max_packet_size - data_offset;
1671 int todo = count; 1857 int todo = count;
1672 unsigned char *first_byte = buffer; 1858 unsigned char *first_byte = buffer;
1673 const unsigned char *current_position = buf; 1859 const unsigned char *current_position = buf;
@@ -1748,11 +1934,6 @@ static void ftdi_write_bulk_callback(struct urb *urb)
1748 1934
1749 dbg("%s - port %d", __func__, port->number); 1935 dbg("%s - port %d", __func__, port->number);
1750 1936
1751 if (status) {
1752 dbg("nonzero write bulk status received: %d", status);
1753 return;
1754 }
1755
1756 priv = usb_get_serial_port_data(port); 1937 priv = usb_get_serial_port_data(port);
1757 if (!priv) { 1938 if (!priv) {
1758 dbg("%s - bad port private data pointer - exiting", __func__); 1939 dbg("%s - bad port private data pointer - exiting", __func__);
@@ -1763,13 +1944,18 @@ static void ftdi_write_bulk_callback(struct urb *urb)
1763 data_offset = priv->write_offset; 1944 data_offset = priv->write_offset;
1764 if (data_offset > 0) { 1945 if (data_offset > 0) {
1765 /* Subtract the control bytes */ 1946 /* Subtract the control bytes */
1766 countback -= (data_offset * DIV_ROUND_UP(countback, PKTSZ)); 1947 countback -= (data_offset * DIV_ROUND_UP(countback, priv->max_packet_size));
1767 } 1948 }
1768 spin_lock_irqsave(&priv->tx_lock, flags); 1949 spin_lock_irqsave(&priv->tx_lock, flags);
1769 --priv->tx_outstanding_urbs; 1950 --priv->tx_outstanding_urbs;
1770 priv->tx_outstanding_bytes -= countback; 1951 priv->tx_outstanding_bytes -= countback;
1771 spin_unlock_irqrestore(&priv->tx_lock, flags); 1952 spin_unlock_irqrestore(&priv->tx_lock, flags);
1772 1953
1954 if (status) {
1955 dbg("nonzero write bulk status received: %d", status);
1956 return;
1957 }
1958
1773 usb_serial_port_softint(port); 1959 usb_serial_port_softint(port);
1774} /* ftdi_write_bulk_callback */ 1960} /* ftdi_write_bulk_callback */
1775 1961
@@ -1865,7 +2051,7 @@ static void ftdi_read_bulk_callback(struct urb *urb)
1865 2051
1866 /* count data bytes, but not status bytes */ 2052 /* count data bytes, but not status bytes */
1867 countread = urb->actual_length; 2053 countread = urb->actual_length;
1868 countread -= 2 * DIV_ROUND_UP(countread, PKTSZ); 2054 countread -= 2 * DIV_ROUND_UP(countread, priv->max_packet_size);
1869 spin_lock_irqsave(&priv->rx_lock, flags); 2055 spin_lock_irqsave(&priv->rx_lock, flags);
1870 priv->rx_bytes += countread; 2056 priv->rx_bytes += countread;
1871 spin_unlock_irqrestore(&priv->rx_lock, flags); 2057 spin_unlock_irqrestore(&priv->rx_lock, flags);
@@ -1938,7 +2124,7 @@ static void ftdi_process_read(struct work_struct *work)
1938 2124
1939 need_flip = 0; 2125 need_flip = 0;
1940 for (packet_offset = priv->rx_processed; 2126 for (packet_offset = priv->rx_processed;
1941 packet_offset < urb->actual_length; packet_offset += PKTSZ) { 2127 packet_offset < urb->actual_length; packet_offset += priv->max_packet_size) {
1942 int length; 2128 int length;
1943 2129
1944 /* Compare new line status to the old one, signal if different/ 2130 /* Compare new line status to the old one, signal if different/
@@ -1953,7 +2139,7 @@ static void ftdi_process_read(struct work_struct *work)
1953 priv->prev_status = new_status; 2139 priv->prev_status = new_status;
1954 } 2140 }
1955 2141
1956 length = min_t(u32, PKTSZ, urb->actual_length-packet_offset)-2; 2142 length = min_t(u32, priv->max_packet_size, urb->actual_length-packet_offset)-2;
1957 if (length < 0) { 2143 if (length < 0) {
1958 dev_err(&port->dev, "%s - bad packet length: %d\n", 2144 dev_err(&port->dev, "%s - bad packet length: %d\n",
1959 __func__, length+2); 2145 __func__, length+2);
@@ -1984,6 +2170,7 @@ static void ftdi_process_read(struct work_struct *work)
1984 if (data[packet_offset+1] & FTDI_RS_BI) { 2170 if (data[packet_offset+1] & FTDI_RS_BI) {
1985 error_flag = TTY_BREAK; 2171 error_flag = TTY_BREAK;
1986 dbg("BREAK received"); 2172 dbg("BREAK received");
2173 usb_serial_handle_break(port);
1987 } 2174 }
1988 if (data[packet_offset+1] & FTDI_RS_PE) { 2175 if (data[packet_offset+1] & FTDI_RS_PE) {
1989 error_flag = TTY_PARITY; 2176 error_flag = TTY_PARITY;
@@ -1998,8 +2185,11 @@ static void ftdi_process_read(struct work_struct *work)
1998 /* Note that the error flag is duplicated for 2185 /* Note that the error flag is duplicated for
1999 every character received since we don't know 2186 every character received since we don't know
2000 which character it applied to */ 2187 which character it applied to */
2001 tty_insert_flip_char(tty, 2188 if (!usb_serial_handle_sysrq_char(tty, port,
2002 data[packet_offset + i], error_flag); 2189 data[packet_offset + i]))
2190 tty_insert_flip_char(tty,
2191 data[packet_offset + i],
2192 error_flag);
2003 } 2193 }
2004 need_flip = 1; 2194 need_flip = 1;
2005 } 2195 }
@@ -2305,6 +2495,8 @@ static int ftdi_tiocmget(struct tty_struct *tty, struct file *file)
2305 case FT232BM: 2495 case FT232BM:
2306 case FT2232C: 2496 case FT2232C:
2307 case FT232RL: 2497 case FT232RL:
2498 case FT2232H:
2499 case FT4232H:
2308 /* the 8U232AM returns a two byte value (the sio is a 1 byte 2500 /* the 8U232AM returns a two byte value (the sio is a 1 byte
2309 value) - in the same format as the data returned from the in 2501 value) - in the same format as the data returned from the in
2310 point */ 2502 point */
@@ -2494,3 +2686,5 @@ MODULE_PARM_DESC(vendor, "User specified vendor ID (default="
2494module_param(product, ushort, 0); 2686module_param(product, ushort, 0);
2495MODULE_PARM_DESC(product, "User specified product ID"); 2687MODULE_PARM_DESC(product, "User specified product ID");
2496 2688
2689module_param(ndi_latency_timer, int, S_IRUGO | S_IWUSR);
2690MODULE_PARM_DESC(ndi_latency_timer, "NDI device latency timer override");
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index 12330fa1c095..c9fbd7415092 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -10,7 +10,7 @@
10 * The device is based on the FTDI FT8U100AX chip. It has a DB25 on one side, 10 * The device is based on the FTDI FT8U100AX chip. It has a DB25 on one side,
11 * USB on the other. 11 * USB on the other.
12 * 12 *
13 * Thanx to FTDI (http://www.ftdi.co.uk) for so kindly providing details 13 * Thanx to FTDI (http://www.ftdichip.com) for so kindly providing details
14 * of the protocol required to talk to the device and ongoing assistence 14 * of the protocol required to talk to the device and ongoing assistence
15 * during development. 15 * during development.
16 * 16 *
@@ -28,11 +28,15 @@
28#define FTDI_8U232AM_ALT_PID 0x6006 /* FTDI's alternate PID for above */ 28#define FTDI_8U232AM_ALT_PID 0x6006 /* FTDI's alternate PID for above */
29#define FTDI_8U2232C_PID 0x6010 /* Dual channel device */ 29#define FTDI_8U2232C_PID 0x6010 /* Dual channel device */
30#define FTDI_232RL_PID 0xFBFA /* Product ID for FT232RL */ 30#define FTDI_232RL_PID 0xFBFA /* Product ID for FT232RL */
31#define FTDI_4232H_PID 0x6011 /* Quad channel hi-speed device */
31#define FTDI_RELAIS_PID 0xFA10 /* Relais device from Rudolf Gugler */ 32#define FTDI_RELAIS_PID 0xFA10 /* Relais device from Rudolf Gugler */
32#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */ 33#define FTDI_NF_RIC_VID 0x0DCD /* Vendor Id */
33#define FTDI_NF_RIC_PID 0x0001 /* Product Id */ 34#define FTDI_NF_RIC_PID 0x0001 /* Product Id */
34#define FTDI_USBX_707_PID 0xF857 /* ADSTech IR Blaster USBX-707 */ 35#define FTDI_USBX_707_PID 0xF857 /* ADSTech IR Blaster USBX-707 */
35 36
37/* Larsen and Brusgaard AltiTrack/USBtrack */
38#define LARSENBRUSGAARD_VID 0x0FD8
39#define LB_ALTITRACK_PID 0x0001
36 40
37/* www.canusb.com Lawicel CANUSB device */ 41/* www.canusb.com Lawicel CANUSB device */
38#define FTDI_CANUSB_PID 0xFFA8 /* Product Id */ 42#define FTDI_CANUSB_PID 0xFFA8 /* Product Id */
@@ -502,6 +506,7 @@
502 * 506 *
503 * Armin Laeuger originally sent the PID for the UM 100 module. 507 * Armin Laeuger originally sent the PID for the UM 100 module.
504 */ 508 */
509#define FTDI_R2000KU_TRUE_RNG 0xFB80 /* R2000KU TRUE RNG */
505#define FTDI_ELV_UR100_PID 0xFB58 /* USB-RS232-Umsetzer (UR 100) */ 510#define FTDI_ELV_UR100_PID 0xFB58 /* USB-RS232-Umsetzer (UR 100) */
506#define FTDI_ELV_UM100_PID 0xFB5A /* USB-Modul UM 100 */ 511#define FTDI_ELV_UM100_PID 0xFB5A /* USB-Modul UM 100 */
507#define FTDI_ELV_UO100_PID 0xFB5B /* USB-Modul UO 100 */ 512#define FTDI_ELV_UO100_PID 0xFB5B /* USB-Modul UO 100 */
@@ -610,6 +615,9 @@
610#define FTDI_CCSICDU20_0_PID 0xF9D0 615#define FTDI_CCSICDU20_0_PID 0xF9D0
611#define FTDI_CCSICDU40_1_PID 0xF9D1 616#define FTDI_CCSICDU40_1_PID 0xF9D1
612#define FTDI_CCSMACHX_2_PID 0xF9D2 617#define FTDI_CCSMACHX_2_PID 0xF9D2
618#define FTDI_CCSLOAD_N_GO_3_PID 0xF9D3
619#define FTDI_CCSICDU64_4_PID 0xF9D4
620#define FTDI_CCSPRIME8_5_PID 0xF9D5
613 621
614/* Inside Accesso contactless reader (http://www.insidefr.com) */ 622/* Inside Accesso contactless reader (http://www.insidefr.com) */
615#define INSIDE_ACCESSO 0xFAD0 623#define INSIDE_ACCESSO 0xFAD0
@@ -732,6 +740,15 @@
732#define FTDI_PYRAMID_PID 0xE6C8 /* Pyramid Appliance Display */ 740#define FTDI_PYRAMID_PID 0xE6C8 /* Pyramid Appliance Display */
733 741
734/* 742/*
743 * NDI (www.ndigital.com) product ids
744 */
745#define FTDI_NDI_HUC_PID 0xDA70 /* NDI Host USB Converter */
746#define FTDI_NDI_SPECTRA_SCU_PID 0xDA71 /* NDI Spectra SCU */
747#define FTDI_NDI_FUTURE_2_PID 0xDA72 /* NDI future device #2 */
748#define FTDI_NDI_FUTURE_3_PID 0xDA73 /* NDI future device #3 */
749#define FTDI_NDI_AURORA_SCU_PID 0xDA74 /* NDI Aurora SCU */
750
751/*
735 * Posiflex inc retail equipment (http://www.posiflex.com.tw) 752 * Posiflex inc retail equipment (http://www.posiflex.com.tw)
736 */ 753 */
737#define POSIFLEX_VID 0x0d3a /* Vendor ID */ 754#define POSIFLEX_VID 0x0d3a /* Vendor ID */
@@ -844,9 +861,6 @@
844#define TML_VID 0x1B91 /* Vendor ID */ 861#define TML_VID 0x1B91 /* Vendor ID */
845#define TML_USB_SERIAL_PID 0x0064 /* USB - Serial Converter */ 862#define TML_USB_SERIAL_PID 0x0064 /* USB - Serial Converter */
846 863
847/* NDI Polaris System */
848#define FTDI_NDI_HUC_PID 0xDA70
849
850/* Propox devices */ 864/* Propox devices */
851#define FTDI_PROPOX_JTAGCABLEII_PID 0xD738 865#define FTDI_PROPOX_JTAGCABLEII_PID 0xD738
852 866
@@ -873,6 +887,11 @@
873#define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */ 887#define FTDI_SIO_SET_LATENCY_TIMER 9 /* Set the latency timer */
874#define FTDI_SIO_GET_LATENCY_TIMER 10 /* Get the latency timer */ 888#define FTDI_SIO_GET_LATENCY_TIMER 10 /* Get the latency timer */
875 889
890/* Interface indicies for FT2232, FT2232H and FT4232H devices*/
891#define INTERFACE_A 1
892#define INTERFACE_B 2
893#define INTERFACE_C 3
894#define INTERFACE_D 4
876 895
877/* 896/*
878 * FIC / OpenMoko, Inc. http://wiki.openmoko.org/wiki/Neo1973_Debug_Board_v3 897 * FIC / OpenMoko, Inc. http://wiki.openmoko.org/wiki/Neo1973_Debug_Board_v3
@@ -925,6 +944,8 @@
925#define MARVELL_VID 0x9e88 944#define MARVELL_VID 0x9e88
926#define MARVELL_SHEEVAPLUG_PID 0x9e8f 945#define MARVELL_SHEEVAPLUG_PID 0x9e8f
927 946
947#define FTDI_TURTELIZER_PID 0xBDC8 /* JTAG/RS-232 adapter by egnite GmBH */
948
928/* 949/*
929 * BmRequestType: 1100 0000b 950 * BmRequestType: 1100 0000b
930 * bRequest: FTDI_E2_READ 951 * bRequest: FTDI_E2_READ
@@ -1036,6 +1057,8 @@ typedef enum {
1036 FT232BM = 3, 1057 FT232BM = 3,
1037 FT2232C = 4, 1058 FT2232C = 4,
1038 FT232RL = 5, 1059 FT232RL = 5,
1060 FT2232H = 6,
1061 FT4232H = 7
1039} ftdi_chip_type_t; 1062} ftdi_chip_type_t;
1040 1063
1041typedef enum { 1064typedef enum {
diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
index 586d30ff450b..8839f1c70b7f 100644
--- a/drivers/usb/serial/garmin_gps.c
+++ b/drivers/usb/serial/garmin_gps.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Garmin GPS driver 2 * Garmin GPS driver
3 * 3 *
4 * Copyright (C) 2006,2007 Hermann Kneissel herkne@users.sourceforge.net 4 * Copyright (C) 2006-2009 Hermann Kneissel herkne@users.sourceforge.net
5 * 5 *
6 * The latest version of the driver can be found at 6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/ 7 * http://sourceforge.net/projects/garmin-gps/
@@ -51,7 +51,7 @@ static int debug;
51 */ 51 */
52 52
53#define VERSION_MAJOR 0 53#define VERSION_MAJOR 0
54#define VERSION_MINOR 31 54#define VERSION_MINOR 33
55 55
56#define _STR(s) #s 56#define _STR(s) #s
57#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b) 57#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
@@ -129,7 +129,6 @@ struct garmin_data {
129 __u8 state; 129 __u8 state;
130 __u16 flags; 130 __u16 flags;
131 __u8 mode; 131 __u8 mode;
132 __u8 ignorePkts;
133 __u8 count; 132 __u8 count;
134 __u8 pkt_id; 133 __u8 pkt_id;
135 __u32 serial_num; 134 __u32 serial_num;
@@ -141,8 +140,6 @@ struct garmin_data {
141 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */ 140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
142 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */ 141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
143 __u8 privpkt[4*6]; 142 __u8 privpkt[4*6];
144 atomic_t req_count;
145 atomic_t resp_count;
146 spinlock_t lock; 143 spinlock_t lock;
147 struct list_head pktlist; 144 struct list_head pktlist;
148}; 145};
@@ -170,6 +167,8 @@ struct garmin_data {
170#define FLAGS_BULK_IN_ACTIVE 0x0020 167#define FLAGS_BULK_IN_ACTIVE 0x0020
171#define FLAGS_BULK_IN_RESTART 0x0010 168#define FLAGS_BULK_IN_RESTART 0x0010
172#define FLAGS_THROTTLED 0x0008 169#define FLAGS_THROTTLED 0x0008
170#define APP_REQ_SEEN 0x0004
171#define APP_RESP_SEEN 0x0002
173#define CLEAR_HALT_REQUIRED 0x0001 172#define CLEAR_HALT_REQUIRED 0x0001
174 173
175#define FLAGS_QUEUING 0x0100 174#define FLAGS_QUEUING 0x0100
@@ -184,20 +183,16 @@ struct garmin_data {
184 183
185 184
186/* function prototypes */ 185/* function prototypes */
187static void gsp_next_packet(struct garmin_data *garmin_data_p); 186static int gsp_next_packet(struct garmin_data *garmin_data_p);
188static int garmin_write_bulk(struct usb_serial_port *port, 187static int garmin_write_bulk(struct usb_serial_port *port,
189 const unsigned char *buf, int count, 188 const unsigned char *buf, int count,
190 int dismiss_ack); 189 int dismiss_ack);
191 190
192/* some special packets to be send or received */ 191/* some special packets to be send or received */
193static unsigned char const GARMIN_START_SESSION_REQ[] 192static unsigned char const GARMIN_START_SESSION_REQ[]
194 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 }; 193 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
195static unsigned char const GARMIN_START_SESSION_REQ2[]
196 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
197static unsigned char const GARMIN_START_SESSION_REPLY[] 194static unsigned char const GARMIN_START_SESSION_REPLY[]
198 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 }; 195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
199static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
200 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
201static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[] 196static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
202 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }; 197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
203static unsigned char const GARMIN_APP_LAYER_REPLY[] 198static unsigned char const GARMIN_APP_LAYER_REPLY[]
@@ -233,13 +228,6 @@ static struct usb_driver garmin_driver = {
233}; 228};
234 229
235 230
236static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
237{
238 return atomic_read(&garmin_data_p->req_count) ==
239 atomic_read(&garmin_data_p->resp_count);
240}
241
242
243static inline int getLayerId(const __u8 *usbPacket) 231static inline int getLayerId(const __u8 *usbPacket)
244{ 232{
245 return __le32_to_cpup((__le32 *)(usbPacket)); 233 return __le32_to_cpup((__le32 *)(usbPacket));
@@ -325,8 +313,11 @@ static int pkt_add(struct garmin_data *garmin_data_p,
325 state = garmin_data_p->state; 313 state = garmin_data_p->state;
326 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 314 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
327 315
316 dbg("%s - added: pkt: %d - %d bytes",
317 __func__, pkt->seq, data_length);
318
328 /* in serial mode, if someone is waiting for data from 319 /* in serial mode, if someone is waiting for data from
329 the device, iconvert and send the next packet to tty. */ 320 the device, convert and send the next packet to tty. */
330 if (result && (state == STATE_GSP_WAIT_DATA)) 321 if (result && (state == STATE_GSP_WAIT_DATA))
331 gsp_next_packet(garmin_data_p); 322 gsp_next_packet(garmin_data_p);
332 } 323 }
@@ -411,7 +402,7 @@ static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
411/* 402/*
412 * called for a complete packet received from tty layer 403 * called for a complete packet received from tty layer
413 * 404 *
414 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting 405 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
415 * at GSP_INITIAL_OFFSET. 406 * at GSP_INITIAL_OFFSET.
416 * 407 *
417 * count - number of bytes in the input buffer including space reserved for 408 * count - number of bytes in the input buffer including space reserved for
@@ -501,7 +492,6 @@ static int gsp_receive(struct garmin_data *garmin_data_p,
501 unsigned long flags; 492 unsigned long flags;
502 int offs = 0; 493 int offs = 0;
503 int ack_or_nak_seen = 0; 494 int ack_or_nak_seen = 0;
504 int i = 0;
505 __u8 *dest; 495 __u8 *dest;
506 int size; 496 int size;
507 /* dleSeen: set if last byte read was a DLE */ 497 /* dleSeen: set if last byte read was a DLE */
@@ -519,8 +509,8 @@ static int gsp_receive(struct garmin_data *garmin_data_p,
519 skip = garmin_data_p->flags & FLAGS_GSP_SKIP; 509 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
520 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 510 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
521 511
522 dbg("%s - dle=%d skip=%d size=%d count=%d", 512 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
523 __func__, dleSeen, skip, size, count); 513 __func__, dleSeen, skip, size, count); */
524 514
525 if (size == 0) 515 if (size == 0)
526 size = GSP_INITIAL_OFFSET; 516 size = GSP_INITIAL_OFFSET;
@@ -568,7 +558,6 @@ static int gsp_receive(struct garmin_data *garmin_data_p,
568 } else if (!skip) { 558 } else if (!skip) {
569 559
570 if (dleSeen) { 560 if (dleSeen) {
571 dbg("non-masked DLE at %d - restarting", i);
572 size = GSP_INITIAL_OFFSET; 561 size = GSP_INITIAL_OFFSET;
573 dleSeen = 0; 562 dleSeen = 0;
574 } 563 }
@@ -599,19 +588,19 @@ static int gsp_receive(struct garmin_data *garmin_data_p,
599 else 588 else
600 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN; 589 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
601 590
602 if (ack_or_nak_seen)
603 garmin_data_p->state = STATE_GSP_WAIT_DATA;
604
605 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 591 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
606 592
607 if (ack_or_nak_seen) 593 if (ack_or_nak_seen) {
608 gsp_next_packet(garmin_data_p); 594 if (gsp_next_packet(garmin_data_p) > 0)
595 garmin_data_p->state = STATE_ACTIVE;
596 else
597 garmin_data_p->state = STATE_GSP_WAIT_DATA;
598 }
609 return count; 599 return count;
610} 600}
611 601
612 602
613 603
614
615/* 604/*
616 * Sends a usb packet to the tty 605 * Sends a usb packet to the tty
617 * 606 *
@@ -733,29 +722,28 @@ static int gsp_send(struct garmin_data *garmin_data_p,
733} 722}
734 723
735 724
736
737
738
739/* 725/*
740 * Process the next pending data packet - if there is one 726 * Process the next pending data packet - if there is one
741 */ 727 */
742static void gsp_next_packet(struct garmin_data *garmin_data_p) 728static int gsp_next_packet(struct garmin_data *garmin_data_p)
743{ 729{
730 int result = 0;
744 struct garmin_packet *pkt = NULL; 731 struct garmin_packet *pkt = NULL;
745 732
746 while ((pkt = pkt_pop(garmin_data_p)) != NULL) { 733 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
747 dbg("%s - next pkt: %d", __func__, pkt->seq); 734 dbg("%s - next pkt: %d", __func__, pkt->seq);
748 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) { 735 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
736 if (result > 0) {
749 kfree(pkt); 737 kfree(pkt);
750 return; 738 return result;
751 } 739 }
752 kfree(pkt); 740 kfree(pkt);
753 } 741 }
742 return result;
754} 743}
755 744
756 745
757 746
758
759/****************************************************************************** 747/******************************************************************************
760 * garmin native mode 748 * garmin native mode
761 ******************************************************************************/ 749 ******************************************************************************/
@@ -888,14 +876,6 @@ static int garmin_clear(struct garmin_data *garmin_data_p)
888 unsigned long flags; 876 unsigned long flags;
889 int status = 0; 877 int status = 0;
890 878
891 struct usb_serial_port *port = garmin_data_p->port;
892
893 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
894 /* send a terminate command */
895 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
896 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
897 }
898
899 /* flush all queued data */ 879 /* flush all queued data */
900 pkt_clear(garmin_data_p); 880 pkt_clear(garmin_data_p);
901 881
@@ -908,16 +888,12 @@ static int garmin_clear(struct garmin_data *garmin_data_p)
908} 888}
909 889
910 890
911
912
913
914
915static int garmin_init_session(struct usb_serial_port *port) 891static int garmin_init_session(struct usb_serial_port *port)
916{ 892{
917 unsigned long flags;
918 struct usb_serial *serial = port->serial; 893 struct usb_serial *serial = port->serial;
919 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 894 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
920 int status = 0; 895 int status = 0;
896 int i = 0;
921 897
922 if (status == 0) { 898 if (status == 0) {
923 usb_kill_urb(port->interrupt_in_urb); 899 usb_kill_urb(port->interrupt_in_urb);
@@ -931,30 +907,25 @@ static int garmin_init_session(struct usb_serial_port *port)
931 __func__, status); 907 __func__, status);
932 } 908 }
933 909
910 /*
911 * using the initialization method from gpsbabel. See comments in
912 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
913 */
934 if (status == 0) { 914 if (status == 0) {
935 dbg("%s - starting session ...", __func__); 915 dbg("%s - starting session ...", __func__);
936 garmin_data_p->state = STATE_ACTIVE; 916 garmin_data_p->state = STATE_ACTIVE;
937 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
938 sizeof(GARMIN_START_SESSION_REQ), 0);
939 917
940 if (status >= 0) { 918 for (i = 0; i < 3; i++) {
941
942 spin_lock_irqsave(&garmin_data_p->lock, flags);
943 garmin_data_p->ignorePkts++;
944 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
945
946 /* not needed, but the win32 driver does it too ... */
947 status = garmin_write_bulk(port, 919 status = garmin_write_bulk(port,
948 GARMIN_START_SESSION_REQ2, 920 GARMIN_START_SESSION_REQ,
949 sizeof(GARMIN_START_SESSION_REQ2), 0); 921 sizeof(GARMIN_START_SESSION_REQ), 0);
950 if (status >= 0) { 922
951 status = 0; 923 if (status < 0)
952 spin_lock_irqsave(&garmin_data_p->lock, flags); 924 break;
953 garmin_data_p->ignorePkts++;
954 spin_unlock_irqrestore(&garmin_data_p->lock,
955 flags);
956 }
957 } 925 }
926
927 if (status > 0)
928 status = 0;
958 } 929 }
959 930
960 return status; 931 return status;
@@ -962,8 +933,6 @@ static int garmin_init_session(struct usb_serial_port *port)
962 933
963 934
964 935
965
966
967static int garmin_open(struct tty_struct *tty, 936static int garmin_open(struct tty_struct *tty,
968 struct usb_serial_port *port, struct file *filp) 937 struct usb_serial_port *port, struct file *filp)
969{ 938{
@@ -977,8 +946,6 @@ static int garmin_open(struct tty_struct *tty,
977 garmin_data_p->mode = initial_mode; 946 garmin_data_p->mode = initial_mode;
978 garmin_data_p->count = 0; 947 garmin_data_p->count = 0;
979 garmin_data_p->flags = 0; 948 garmin_data_p->flags = 0;
980 atomic_set(&garmin_data_p->req_count, 0);
981 atomic_set(&garmin_data_p->resp_count, 0);
982 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 949 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
983 950
984 /* shutdown any bulk reads that might be going on */ 951 /* shutdown any bulk reads that might be going on */
@@ -993,8 +960,7 @@ static int garmin_open(struct tty_struct *tty,
993} 960}
994 961
995 962
996static void garmin_close(struct tty_struct *tty, 963static void garmin_close(struct usb_serial_port *port)
997 struct usb_serial_port *port, struct file *filp)
998{ 964{
999 struct usb_serial *serial = port->serial; 965 struct usb_serial *serial = port->serial;
1000 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 966 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
@@ -1007,6 +973,7 @@ static void garmin_close(struct tty_struct *tty,
1007 return; 973 return;
1008 974
1009 mutex_lock(&port->serial->disc_mutex); 975 mutex_lock(&port->serial->disc_mutex);
976
1010 if (!port->serial->disconnected) 977 if (!port->serial->disconnected)
1011 garmin_clear(garmin_data_p); 978 garmin_clear(garmin_data_p);
1012 979
@@ -1014,25 +981,17 @@ static void garmin_close(struct tty_struct *tty,
1014 usb_kill_urb(port->read_urb); 981 usb_kill_urb(port->read_urb);
1015 usb_kill_urb(port->write_urb); 982 usb_kill_urb(port->write_urb);
1016 983
1017 if (!port->serial->disconnected) { 984 /* keep reset state so we know that we must start a new session */
1018 if (noResponseFromAppLayer(garmin_data_p) || 985 if (garmin_data_p->state != STATE_RESET)
1019 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1020 process_resetdev_request(port);
1021 garmin_data_p->state = STATE_RESET;
1022 } else {
1023 garmin_data_p->state = STATE_DISCONNECTED;
1024 }
1025 } else {
1026 garmin_data_p->state = STATE_DISCONNECTED; 986 garmin_data_p->state = STATE_DISCONNECTED;
1027 } 987
1028 mutex_unlock(&port->serial->disc_mutex); 988 mutex_unlock(&port->serial->disc_mutex);
1029} 989}
1030 990
991
1031static void garmin_write_bulk_callback(struct urb *urb) 992static void garmin_write_bulk_callback(struct urb *urb)
1032{ 993{
1033 unsigned long flags;
1034 struct usb_serial_port *port = urb->context; 994 struct usb_serial_port *port = urb->context;
1035 int status = urb->status;
1036 995
1037 if (port) { 996 if (port) {
1038 struct garmin_data *garmin_data_p = 997 struct garmin_data *garmin_data_p =
@@ -1040,20 +999,13 @@ static void garmin_write_bulk_callback(struct urb *urb)
1040 999
1041 dbg("%s - port %d", __func__, port->number); 1000 dbg("%s - port %d", __func__, port->number);
1042 1001
1043 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer) 1002 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
1044 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1045 gsp_send_ack(garmin_data_p,
1046 ((__u8 *)urb->transfer_buffer)[4]);
1047 }
1048 1003
1049 if (status) { 1004 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1050 dbg("%s - nonzero write bulk status received: %d", 1005 gsp_send_ack(garmin_data_p,
1051 __func__, status); 1006 ((__u8 *)urb->transfer_buffer)[4]);
1052 spin_lock_irqsave(&garmin_data_p->lock, flags); 1007 }
1053 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1054 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1055 } 1008 }
1056
1057 usb_serial_port_softint(port); 1009 usb_serial_port_softint(port);
1058 } 1010 }
1059 1011
@@ -1109,7 +1061,11 @@ static int garmin_write_bulk(struct usb_serial_port *port,
1109 urb->transfer_flags |= URB_ZERO_PACKET; 1061 urb->transfer_flags |= URB_ZERO_PACKET;
1110 1062
1111 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) { 1063 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1112 atomic_inc(&garmin_data_p->req_count); 1064
1065 spin_lock_irqsave(&garmin_data_p->lock, flags);
1066 garmin_data_p->flags |= APP_REQ_SEEN;
1067 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1068
1113 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1069 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1114 pkt_clear(garmin_data_p); 1070 pkt_clear(garmin_data_p);
1115 garmin_data_p->state = STATE_GSP_WAIT_DATA; 1071 garmin_data_p->state = STATE_GSP_WAIT_DATA;
@@ -1141,6 +1097,9 @@ static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1141 1097
1142 usb_serial_debug_data(debug, &port->dev, __func__, count, buf); 1098 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1143 1099
1100 if (garmin_data_p->state == STATE_RESET)
1101 return -EIO;
1102
1144 /* check for our private packets */ 1103 /* check for our private packets */
1145 if (count >= GARMIN_PKTHDR_LENGTH) { 1104 if (count >= GARMIN_PKTHDR_LENGTH) {
1146 len = PRIVPKTSIZ; 1105 len = PRIVPKTSIZ;
@@ -1185,7 +1144,7 @@ static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1185 break; 1144 break;
1186 1145
1187 case PRIV_PKTID_RESET_REQ: 1146 case PRIV_PKTID_RESET_REQ:
1188 atomic_inc(&garmin_data_p->req_count); 1147 process_resetdev_request(port);
1189 break; 1148 break;
1190 1149
1191 case PRIV_PKTID_SET_DEF_MODE: 1150 case PRIV_PKTID_SET_DEF_MODE:
@@ -1201,8 +1160,6 @@ static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1201 } 1160 }
1202 } 1161 }
1203 1162
1204 garmin_data_p->ignorePkts = 0;
1205
1206 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1163 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1207 return gsp_receive(garmin_data_p, buf, count); 1164 return gsp_receive(garmin_data_p, buf, count);
1208 } else { /* MODE_NATIVE */ 1165 } else { /* MODE_NATIVE */
@@ -1225,31 +1182,33 @@ static int garmin_write_room(struct tty_struct *tty)
1225static void garmin_read_process(struct garmin_data *garmin_data_p, 1182static void garmin_read_process(struct garmin_data *garmin_data_p,
1226 unsigned char *data, unsigned data_length) 1183 unsigned char *data, unsigned data_length)
1227{ 1184{
1185 unsigned long flags;
1186
1228 if (garmin_data_p->flags & FLAGS_DROP_DATA) { 1187 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1229 /* abort-transfer cmd is actice */ 1188 /* abort-transfer cmd is actice */
1230 dbg("%s - pkt dropped", __func__); 1189 dbg("%s - pkt dropped", __func__);
1231 } else if (garmin_data_p->state != STATE_DISCONNECTED && 1190 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1232 garmin_data_p->state != STATE_RESET) { 1191 garmin_data_p->state != STATE_RESET) {
1233 1192
1234 /* remember any appl.layer packets, so we know
1235 if a reset is required or not when closing
1236 the device */
1237 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1238 sizeof(GARMIN_APP_LAYER_REPLY))) {
1239 atomic_inc(&garmin_data_p->resp_count);
1240 }
1241
1242 /* if throttling is active or postprecessing is required 1193 /* if throttling is active or postprecessing is required
1243 put the received data in the input queue, otherwise 1194 put the received data in the input queue, otherwise
1244 send it directly to the tty port */ 1195 send it directly to the tty port */
1245 if (garmin_data_p->flags & FLAGS_QUEUING) { 1196 if (garmin_data_p->flags & FLAGS_QUEUING) {
1246 pkt_add(garmin_data_p, data, data_length); 1197 pkt_add(garmin_data_p, data, data_length);
1247 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1198 } else if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1248 if (getLayerId(data) == GARMIN_LAYERID_APPL) 1199
1200 spin_lock_irqsave(&garmin_data_p->lock, flags);
1201 garmin_data_p->flags |= APP_RESP_SEEN;
1202 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1203
1204 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1249 pkt_add(garmin_data_p, data, data_length); 1205 pkt_add(garmin_data_p, data, data_length);
1250 } else { 1206 } else {
1251 send_to_tty(garmin_data_p->port, data, data_length); 1207 send_to_tty(garmin_data_p->port, data,
1208 data_length);
1209 }
1252 } 1210 }
1211 /* ignore system layer packets ... */
1253 } 1212 }
1254} 1213}
1255 1214
@@ -1364,8 +1323,6 @@ static void garmin_read_int_callback(struct urb *urb)
1364 } else { 1323 } else {
1365 spin_lock_irqsave(&garmin_data_p->lock, flags); 1324 spin_lock_irqsave(&garmin_data_p->lock, flags);
1366 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE; 1325 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1367 /* do not send this packet to the user */
1368 garmin_data_p->ignorePkts = 1;
1369 spin_unlock_irqrestore(&garmin_data_p->lock, 1326 spin_unlock_irqrestore(&garmin_data_p->lock,
1370 flags); 1327 flags);
1371 } 1328 }
@@ -1392,17 +1349,7 @@ static void garmin_read_int_callback(struct urb *urb)
1392 __func__, garmin_data_p->serial_num); 1349 __func__, garmin_data_p->serial_num);
1393 } 1350 }
1394 1351
1395 if (garmin_data_p->ignorePkts) { 1352 garmin_read_process(garmin_data_p, data, urb->actual_length);
1396 /* this reply belongs to a request generated by the driver,
1397 ignore it. */
1398 dbg("%s - pkt ignored (%d)",
1399 __func__, garmin_data_p->ignorePkts);
1400 spin_lock_irqsave(&garmin_data_p->lock, flags);
1401 garmin_data_p->ignorePkts--;
1402 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1403 } else {
1404 garmin_read_process(garmin_data_p, data, urb->actual_length);
1405 }
1406 1353
1407 port->interrupt_in_urb->dev = port->serial->dev; 1354 port->interrupt_in_urb->dev = port->serial->dev;
1408 retval = usb_submit_urb(urb, GFP_ATOMIC); 1355 retval = usb_submit_urb(urb, GFP_ATOMIC);
@@ -1528,7 +1475,7 @@ static int garmin_attach(struct usb_serial *serial)
1528} 1475}
1529 1476
1530 1477
1531static void garmin_shutdown(struct usb_serial *serial) 1478static void garmin_disconnect(struct usb_serial *serial)
1532{ 1479{
1533 struct usb_serial_port *port = serial->port[0]; 1480 struct usb_serial_port *port = serial->port[0];
1534 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1481 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
@@ -1537,8 +1484,17 @@ static void garmin_shutdown(struct usb_serial *serial)
1537 1484
1538 usb_kill_urb(port->interrupt_in_urb); 1485 usb_kill_urb(port->interrupt_in_urb);
1539 del_timer_sync(&garmin_data_p->timer); 1486 del_timer_sync(&garmin_data_p->timer);
1487}
1488
1489
1490static void garmin_release(struct usb_serial *serial)
1491{
1492 struct usb_serial_port *port = serial->port[0];
1493 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1494
1495 dbg("%s", __func__);
1496
1540 kfree(garmin_data_p); 1497 kfree(garmin_data_p);
1541 usb_set_serial_port_data(port, NULL);
1542} 1498}
1543 1499
1544 1500
@@ -1557,7 +1513,8 @@ static struct usb_serial_driver garmin_device = {
1557 .throttle = garmin_throttle, 1513 .throttle = garmin_throttle,
1558 .unthrottle = garmin_unthrottle, 1514 .unthrottle = garmin_unthrottle,
1559 .attach = garmin_attach, 1515 .attach = garmin_attach,
1560 .shutdown = garmin_shutdown, 1516 .disconnect = garmin_disconnect,
1517 .release = garmin_release,
1561 .write = garmin_write, 1518 .write = garmin_write,
1562 .write_room = garmin_write_room, 1519 .write_room = garmin_write_room,
1563 .write_bulk_callback = garmin_write_bulk_callback, 1520 .write_bulk_callback = garmin_write_bulk_callback,
diff --git a/drivers/usb/serial/generic.c b/drivers/usb/serial/generic.c
index 4cec9906ccf3..ce57f6a32bdf 100644
--- a/drivers/usb/serial/generic.c
+++ b/drivers/usb/serial/generic.c
@@ -63,7 +63,8 @@ struct usb_serial_driver usb_serial_generic_device = {
63 .id_table = generic_device_ids, 63 .id_table = generic_device_ids,
64 .usb_driver = &generic_driver, 64 .usb_driver = &generic_driver,
65 .num_ports = 1, 65 .num_ports = 1,
66 .shutdown = usb_serial_generic_shutdown, 66 .disconnect = usb_serial_generic_disconnect,
67 .release = usb_serial_generic_release,
67 .throttle = usb_serial_generic_throttle, 68 .throttle = usb_serial_generic_throttle,
68 .unthrottle = usb_serial_generic_unthrottle, 69 .unthrottle = usb_serial_generic_unthrottle,
69 .resume = usb_serial_generic_resume, 70 .resume = usb_serial_generic_resume,
@@ -184,13 +185,94 @@ int usb_serial_generic_resume(struct usb_serial *serial)
184} 185}
185EXPORT_SYMBOL_GPL(usb_serial_generic_resume); 186EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
186 187
187void usb_serial_generic_close(struct tty_struct *tty, 188void usb_serial_generic_close(struct usb_serial_port *port)
188 struct usb_serial_port *port, struct file *filp)
189{ 189{
190 dbg("%s - port %d", __func__, port->number); 190 dbg("%s - port %d", __func__, port->number);
191 generic_cleanup(port); 191 generic_cleanup(port);
192} 192}
193 193
194static int usb_serial_multi_urb_write(struct tty_struct *tty,
195 struct usb_serial_port *port, const unsigned char *buf, int count)
196{
197 unsigned long flags;
198 struct urb *urb;
199 unsigned char *buffer;
200 int status;
201 int towrite;
202 int bwrite = 0;
203
204 dbg("%s - port %d", __func__, port->number);
205
206 if (count == 0)
207 dbg("%s - write request of 0 bytes", __func__);
208
209 while (count > 0) {
210 towrite = (count > port->bulk_out_size) ?
211 port->bulk_out_size : count;
212 spin_lock_irqsave(&port->lock, flags);
213 if (port->urbs_in_flight >
214 port->serial->type->max_in_flight_urbs) {
215 spin_unlock_irqrestore(&port->lock, flags);
216 dbg("%s - write limit hit\n", __func__);
217 return bwrite;
218 }
219 port->tx_bytes_flight += towrite;
220 port->urbs_in_flight++;
221 spin_unlock_irqrestore(&port->lock, flags);
222
223 buffer = kmalloc(towrite, GFP_ATOMIC);
224 if (!buffer) {
225 dev_err(&port->dev,
226 "%s ran out of kernel memory for urb ...\n", __func__);
227 goto error_no_buffer;
228 }
229
230 urb = usb_alloc_urb(0, GFP_ATOMIC);
231 if (!urb) {
232 dev_err(&port->dev, "%s - no more free urbs\n",
233 __func__);
234 goto error_no_urb;
235 }
236
237 /* Copy data */
238 memcpy(buffer, buf + bwrite, towrite);
239 usb_serial_debug_data(debug, &port->dev, __func__,
240 towrite, buffer);
241 /* fill the buffer and send it */
242 usb_fill_bulk_urb(urb, port->serial->dev,
243 usb_sndbulkpipe(port->serial->dev,
244 port->bulk_out_endpointAddress),
245 buffer, towrite,
246 usb_serial_generic_write_bulk_callback, port);
247
248 status = usb_submit_urb(urb, GFP_ATOMIC);
249 if (status) {
250 dev_err(&port->dev,
251 "%s - failed submitting write urb, error %d\n",
252 __func__, status);
253 goto error;
254 }
255
256 /* This urb is the responsibility of the host driver now */
257 usb_free_urb(urb);
258 dbg("%s write: %d", __func__, towrite);
259 count -= towrite;
260 bwrite += towrite;
261 }
262 return bwrite;
263
264error:
265 usb_free_urb(urb);
266error_no_urb:
267 kfree(buffer);
268error_no_buffer:
269 spin_lock_irqsave(&port->lock, flags);
270 port->urbs_in_flight--;
271 port->tx_bytes_flight -= towrite;
272 spin_unlock_irqrestore(&port->lock, flags);
273 return bwrite;
274}
275
194int usb_serial_generic_write(struct tty_struct *tty, 276int usb_serial_generic_write(struct tty_struct *tty,
195 struct usb_serial_port *port, const unsigned char *buf, int count) 277 struct usb_serial_port *port, const unsigned char *buf, int count)
196{ 278{
@@ -208,6 +290,11 @@ int usb_serial_generic_write(struct tty_struct *tty,
208 /* only do something if we have a bulk out endpoint */ 290 /* only do something if we have a bulk out endpoint */
209 if (serial->num_bulk_out) { 291 if (serial->num_bulk_out) {
210 unsigned long flags; 292 unsigned long flags;
293
294 if (serial->type->max_in_flight_urbs)
295 return usb_serial_multi_urb_write(tty, port,
296 buf, count);
297
211 spin_lock_irqsave(&port->lock, flags); 298 spin_lock_irqsave(&port->lock, flags);
212 if (port->write_urb_busy) { 299 if (port->write_urb_busy) {
213 spin_unlock_irqrestore(&port->lock, flags); 300 spin_unlock_irqrestore(&port->lock, flags);
@@ -253,20 +340,26 @@ int usb_serial_generic_write(struct tty_struct *tty,
253 /* no bulk out, so return 0 bytes written */ 340 /* no bulk out, so return 0 bytes written */
254 return 0; 341 return 0;
255} 342}
343EXPORT_SYMBOL_GPL(usb_serial_generic_write);
256 344
257int usb_serial_generic_write_room(struct tty_struct *tty) 345int usb_serial_generic_write_room(struct tty_struct *tty)
258{ 346{
259 struct usb_serial_port *port = tty->driver_data; 347 struct usb_serial_port *port = tty->driver_data;
260 struct usb_serial *serial = port->serial; 348 struct usb_serial *serial = port->serial;
349 unsigned long flags;
261 int room = 0; 350 int room = 0;
262 351
263 dbg("%s - port %d", __func__, port->number); 352 dbg("%s - port %d", __func__, port->number);
264 353 spin_lock_irqsave(&port->lock, flags);
265 /* FIXME: Locking */ 354 if (serial->type->max_in_flight_urbs) {
266 if (serial->num_bulk_out) { 355 if (port->urbs_in_flight < serial->type->max_in_flight_urbs)
267 if (!(port->write_urb_busy)) 356 room = port->bulk_out_size *
268 room = port->bulk_out_size; 357 (serial->type->max_in_flight_urbs -
358 port->urbs_in_flight);
359 } else if (serial->num_bulk_out && !(port->write_urb_busy)) {
360 room = port->bulk_out_size;
269 } 361 }
362 spin_unlock_irqrestore(&port->lock, flags);
270 363
271 dbg("%s - returns %d", __func__, room); 364 dbg("%s - returns %d", __func__, room);
272 return room; 365 return room;
@@ -277,11 +370,16 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
277 struct usb_serial_port *port = tty->driver_data; 370 struct usb_serial_port *port = tty->driver_data;
278 struct usb_serial *serial = port->serial; 371 struct usb_serial *serial = port->serial;
279 int chars = 0; 372 int chars = 0;
373 unsigned long flags;
280 374
281 dbg("%s - port %d", __func__, port->number); 375 dbg("%s - port %d", __func__, port->number);
282 376
283 /* FIXME: Locking */ 377 if (serial->type->max_in_flight_urbs) {
284 if (serial->num_bulk_out) { 378 spin_lock_irqsave(&port->lock, flags);
379 chars = port->tx_bytes_flight;
380 spin_unlock_irqrestore(&port->lock, flags);
381 } else if (serial->num_bulk_out) {
382 /* FIXME: Locking */
285 if (port->write_urb_busy) 383 if (port->write_urb_busy)
286 chars = port->write_urb->transfer_buffer_length; 384 chars = port->write_urb->transfer_buffer_length;
287 } 385 }
@@ -291,7 +389,8 @@ int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
291} 389}
292 390
293 391
294static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags) 392void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
393 gfp_t mem_flags)
295{ 394{
296 struct urb *urb = port->read_urb; 395 struct urb *urb = port->read_urb;
297 struct usb_serial *serial = port->serial; 396 struct usb_serial *serial = port->serial;
@@ -312,25 +411,35 @@ static void resubmit_read_urb(struct usb_serial_port *port, gfp_t mem_flags)
312 "%s - failed resubmitting read urb, error %d\n", 411 "%s - failed resubmitting read urb, error %d\n",
313 __func__, result); 412 __func__, result);
314} 413}
414EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb);
315 415
316/* Push data to tty layer and resubmit the bulk read URB */ 416/* Push data to tty layer and resubmit the bulk read URB */
317static void flush_and_resubmit_read_urb(struct usb_serial_port *port) 417static void flush_and_resubmit_read_urb(struct usb_serial_port *port)
318{ 418{
319 struct urb *urb = port->read_urb; 419 struct urb *urb = port->read_urb;
320 struct tty_struct *tty = tty_port_tty_get(&port->port); 420 struct tty_struct *tty = tty_port_tty_get(&port->port);
321 int room; 421 char *ch = (char *)urb->transfer_buffer;
322 422 int i;
323 /* Push data to tty */ 423
324 if (tty && urb->actual_length) { 424 if (!tty)
325 room = tty_buffer_request_room(tty, urb->actual_length); 425 goto done;
326 if (room) { 426
327 tty_insert_flip_string(tty, urb->transfer_buffer, room); 427 /* The per character mucking around with sysrq path it too slow for
328 tty_flip_buffer_push(tty); 428 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
429 where the USB serial is not a console anyway */
430 if (!port->console || !port->sysrq)
431 tty_insert_flip_string(tty, ch, urb->actual_length);
432 else {
433 /* Push data to tty */
434 for (i = 0; i < urb->actual_length; i++, ch++) {
435 if (!usb_serial_handle_sysrq_char(tty, port, *ch))
436 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
329 } 437 }
330 } 438 }
439 tty_flip_buffer_push(tty);
331 tty_kref_put(tty); 440 tty_kref_put(tty);
332 441done:
333 resubmit_read_urb(port, GFP_ATOMIC); 442 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
334} 443}
335 444
336void usb_serial_generic_read_bulk_callback(struct urb *urb) 445void usb_serial_generic_read_bulk_callback(struct urb *urb)
@@ -364,12 +473,24 @@ EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
364 473
365void usb_serial_generic_write_bulk_callback(struct urb *urb) 474void usb_serial_generic_write_bulk_callback(struct urb *urb)
366{ 475{
476 unsigned long flags;
367 struct usb_serial_port *port = urb->context; 477 struct usb_serial_port *port = urb->context;
368 int status = urb->status; 478 int status = urb->status;
369 479
370 dbg("%s - port %d", __func__, port->number); 480 dbg("%s - port %d", __func__, port->number);
371 481
372 port->write_urb_busy = 0; 482 if (port->serial->type->max_in_flight_urbs) {
483 spin_lock_irqsave(&port->lock, flags);
484 --port->urbs_in_flight;
485 port->tx_bytes_flight -= urb->transfer_buffer_length;
486 if (port->urbs_in_flight < 0)
487 port->urbs_in_flight = 0;
488 spin_unlock_irqrestore(&port->lock, flags);
489 } else {
490 /* Handle the case for single urb mode */
491 port->write_urb_busy = 0;
492 }
493
373 if (status) { 494 if (status) {
374 dbg("%s - nonzero write bulk status received: %d", 495 dbg("%s - nonzero write bulk status received: %d",
375 __func__, status); 496 __func__, status);
@@ -409,11 +530,37 @@ void usb_serial_generic_unthrottle(struct tty_struct *tty)
409 530
410 if (was_throttled) { 531 if (was_throttled) {
411 /* Resume reading from device */ 532 /* Resume reading from device */
412 resubmit_read_urb(port, GFP_KERNEL); 533 usb_serial_generic_resubmit_read_urb(port, GFP_KERNEL);
534 }
535}
536
537int usb_serial_handle_sysrq_char(struct tty_struct *tty,
538 struct usb_serial_port *port, unsigned int ch)
539{
540 if (port->sysrq && port->console) {
541 if (ch && time_before(jiffies, port->sysrq)) {
542 handle_sysrq(ch, tty);
543 port->sysrq = 0;
544 return 1;
545 }
546 port->sysrq = 0;
547 }
548 return 0;
549}
550EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
551
552int usb_serial_handle_break(struct usb_serial_port *port)
553{
554 if (!port->sysrq) {
555 port->sysrq = jiffies + HZ*5;
556 return 1;
413 } 557 }
558 port->sysrq = 0;
559 return 0;
414} 560}
561EXPORT_SYMBOL_GPL(usb_serial_handle_break);
415 562
416void usb_serial_generic_shutdown(struct usb_serial *serial) 563void usb_serial_generic_disconnect(struct usb_serial *serial)
417{ 564{
418 int i; 565 int i;
419 566
@@ -424,3 +571,7 @@ void usb_serial_generic_shutdown(struct usb_serial *serial)
424 generic_cleanup(serial->port[i]); 571 generic_cleanup(serial->port[i]);
425} 572}
426 573
574void usb_serial_generic_release(struct usb_serial *serial)
575{
576 dbg("%s", __func__);
577}
diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c
index fb4a73d090f6..0191693625d6 100644
--- a/drivers/usb/serial/io_edgeport.c
+++ b/drivers/usb/serial/io_edgeport.c
@@ -207,8 +207,7 @@ static void edge_bulk_out_cmd_callback(struct urb *urb);
207/* function prototypes for the usbserial callbacks */ 207/* function prototypes for the usbserial callbacks */
208static int edge_open(struct tty_struct *tty, struct usb_serial_port *port, 208static int edge_open(struct tty_struct *tty, struct usb_serial_port *port,
209 struct file *filp); 209 struct file *filp);
210static void edge_close(struct tty_struct *tty, struct usb_serial_port *port, 210static void edge_close(struct usb_serial_port *port);
211 struct file *filp);
212static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 211static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
213 const unsigned char *buf, int count); 212 const unsigned char *buf, int count);
214static int edge_write_room(struct tty_struct *tty); 213static int edge_write_room(struct tty_struct *tty);
@@ -225,7 +224,8 @@ static int edge_tiocmget(struct tty_struct *tty, struct file *file);
225static int edge_tiocmset(struct tty_struct *tty, struct file *file, 224static int edge_tiocmset(struct tty_struct *tty, struct file *file,
226 unsigned int set, unsigned int clear); 225 unsigned int set, unsigned int clear);
227static int edge_startup(struct usb_serial *serial); 226static int edge_startup(struct usb_serial *serial);
228static void edge_shutdown(struct usb_serial *serial); 227static void edge_disconnect(struct usb_serial *serial);
228static void edge_release(struct usb_serial *serial);
229 229
230#include "io_tables.h" /* all of the devices that this driver supports */ 230#include "io_tables.h" /* all of the devices that this driver supports */
231 231
@@ -965,7 +965,7 @@ static int edge_open(struct tty_struct *tty,
965 965
966 if (!edge_port->txfifo.fifo) { 966 if (!edge_port->txfifo.fifo) {
967 dbg("%s - no memory", __func__); 967 dbg("%s - no memory", __func__);
968 edge_close(tty, port, filp); 968 edge_close(port);
969 return -ENOMEM; 969 return -ENOMEM;
970 } 970 }
971 971
@@ -975,7 +975,7 @@ static int edge_open(struct tty_struct *tty,
975 975
976 if (!edge_port->write_urb) { 976 if (!edge_port->write_urb) {
977 dbg("%s - no memory", __func__); 977 dbg("%s - no memory", __func__);
978 edge_close(tty, port, filp); 978 edge_close(port);
979 return -ENOMEM; 979 return -ENOMEM;
980 } 980 }
981 981
@@ -1099,8 +1099,7 @@ static void block_until_tx_empty(struct edgeport_port *edge_port)
1099 * edge_close 1099 * edge_close
1100 * this function is called by the tty driver when a port is closed 1100 * this function is called by the tty driver when a port is closed
1101 *****************************************************************************/ 1101 *****************************************************************************/
1102static void edge_close(struct tty_struct *tty, 1102static void edge_close(struct usb_serial_port *port)
1103 struct usb_serial_port *port, struct file *filp)
1104{ 1103{
1105 struct edgeport_serial *edge_serial; 1104 struct edgeport_serial *edge_serial;
1106 struct edgeport_port *edge_port; 1105 struct edgeport_port *edge_port;
@@ -3195,21 +3194,16 @@ static int edge_startup(struct usb_serial *serial)
3195 3194
3196 3195
3197/**************************************************************************** 3196/****************************************************************************
3198 * edge_shutdown 3197 * edge_disconnect
3199 * This function is called whenever the device is removed from the usb bus. 3198 * This function is called whenever the device is removed from the usb bus.
3200 ****************************************************************************/ 3199 ****************************************************************************/
3201static void edge_shutdown(struct usb_serial *serial) 3200static void edge_disconnect(struct usb_serial *serial)
3202{ 3201{
3203 struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 3202 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3204 int i;
3205 3203
3206 dbg("%s", __func__); 3204 dbg("%s", __func__);
3207 3205
3208 /* stop reads and writes on all ports */ 3206 /* stop reads and writes on all ports */
3209 for (i = 0; i < serial->num_ports; ++i) {
3210 kfree(usb_get_serial_port_data(serial->port[i]));
3211 usb_set_serial_port_data(serial->port[i], NULL);
3212 }
3213 /* free up our endpoint stuff */ 3207 /* free up our endpoint stuff */
3214 if (edge_serial->is_epic) { 3208 if (edge_serial->is_epic) {
3215 usb_kill_urb(edge_serial->interrupt_read_urb); 3209 usb_kill_urb(edge_serial->interrupt_read_urb);
@@ -3220,9 +3214,24 @@ static void edge_shutdown(struct usb_serial *serial)
3220 usb_free_urb(edge_serial->read_urb); 3214 usb_free_urb(edge_serial->read_urb);
3221 kfree(edge_serial->bulk_in_buffer); 3215 kfree(edge_serial->bulk_in_buffer);
3222 } 3216 }
3217}
3218
3219
3220/****************************************************************************
3221 * edge_release
3222 * This function is called when the device structure is deallocated.
3223 ****************************************************************************/
3224static void edge_release(struct usb_serial *serial)
3225{
3226 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3227 int i;
3228
3229 dbg("%s", __func__);
3230
3231 for (i = 0; i < serial->num_ports; ++i)
3232 kfree(usb_get_serial_port_data(serial->port[i]));
3223 3233
3224 kfree(edge_serial); 3234 kfree(edge_serial);
3225 usb_set_serial_data(serial, NULL);
3226} 3235}
3227 3236
3228 3237
diff --git a/drivers/usb/serial/io_tables.h b/drivers/usb/serial/io_tables.h
index 7eb9d67b81b6..9241d3147513 100644
--- a/drivers/usb/serial/io_tables.h
+++ b/drivers/usb/serial/io_tables.h
@@ -117,7 +117,8 @@ static struct usb_serial_driver edgeport_2port_device = {
117 .throttle = edge_throttle, 117 .throttle = edge_throttle,
118 .unthrottle = edge_unthrottle, 118 .unthrottle = edge_unthrottle,
119 .attach = edge_startup, 119 .attach = edge_startup,
120 .shutdown = edge_shutdown, 120 .disconnect = edge_disconnect,
121 .release = edge_release,
121 .ioctl = edge_ioctl, 122 .ioctl = edge_ioctl,
122 .set_termios = edge_set_termios, 123 .set_termios = edge_set_termios,
123 .tiocmget = edge_tiocmget, 124 .tiocmget = edge_tiocmget,
@@ -145,7 +146,8 @@ static struct usb_serial_driver edgeport_4port_device = {
145 .throttle = edge_throttle, 146 .throttle = edge_throttle,
146 .unthrottle = edge_unthrottle, 147 .unthrottle = edge_unthrottle,
147 .attach = edge_startup, 148 .attach = edge_startup,
148 .shutdown = edge_shutdown, 149 .disconnect = edge_disconnect,
150 .release = edge_release,
149 .ioctl = edge_ioctl, 151 .ioctl = edge_ioctl,
150 .set_termios = edge_set_termios, 152 .set_termios = edge_set_termios,
151 .tiocmget = edge_tiocmget, 153 .tiocmget = edge_tiocmget,
@@ -173,7 +175,8 @@ static struct usb_serial_driver edgeport_8port_device = {
173 .throttle = edge_throttle, 175 .throttle = edge_throttle,
174 .unthrottle = edge_unthrottle, 176 .unthrottle = edge_unthrottle,
175 .attach = edge_startup, 177 .attach = edge_startup,
176 .shutdown = edge_shutdown, 178 .disconnect = edge_disconnect,
179 .release = edge_release,
177 .ioctl = edge_ioctl, 180 .ioctl = edge_ioctl,
178 .set_termios = edge_set_termios, 181 .set_termios = edge_set_termios,
179 .tiocmget = edge_tiocmget, 182 .tiocmget = edge_tiocmget,
@@ -200,7 +203,8 @@ static struct usb_serial_driver epic_device = {
200 .throttle = edge_throttle, 203 .throttle = edge_throttle,
201 .unthrottle = edge_unthrottle, 204 .unthrottle = edge_unthrottle,
202 .attach = edge_startup, 205 .attach = edge_startup,
203 .shutdown = edge_shutdown, 206 .disconnect = edge_disconnect,
207 .release = edge_release,
204 .ioctl = edge_ioctl, 208 .ioctl = edge_ioctl,
205 .set_termios = edge_set_termios, 209 .set_termios = edge_set_termios,
206 .tiocmget = edge_tiocmget, 210 .tiocmget = edge_tiocmget,
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c
index 513b25e044c1..e8bc42f92e79 100644
--- a/drivers/usb/serial/io_ti.c
+++ b/drivers/usb/serial/io_ti.c
@@ -102,7 +102,7 @@ struct edgeport_port {
102 __u8 shadow_mcr; 102 __u8 shadow_mcr;
103 __u8 shadow_lsr; 103 __u8 shadow_lsr;
104 __u8 lsr_mask; 104 __u8 lsr_mask;
105 __u32 ump_read_timeout; /* Number of miliseconds the UMP will 105 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
106 wait without data before completing 106 wait without data before completing
107 a read short */ 107 a read short */
108 int baud_rate; 108 int baud_rate;
@@ -2009,8 +2009,7 @@ release_es_lock:
2009 return status; 2009 return status;
2010} 2010}
2011 2011
2012static void edge_close(struct tty_struct *tty, 2012static void edge_close(struct usb_serial_port *port)
2013 struct usb_serial_port *port, struct file *filp)
2014{ 2013{
2015 struct edgeport_serial *edge_serial; 2014 struct edgeport_serial *edge_serial;
2016 struct edgeport_port *edge_port; 2015 struct edgeport_port *edge_port;
@@ -2664,7 +2663,7 @@ cleanup:
2664 return -ENOMEM; 2663 return -ENOMEM;
2665} 2664}
2666 2665
2667static void edge_shutdown(struct usb_serial *serial) 2666static void edge_disconnect(struct usb_serial *serial)
2668{ 2667{
2669 int i; 2668 int i;
2670 struct edgeport_port *edge_port; 2669 struct edgeport_port *edge_port;
@@ -2674,12 +2673,22 @@ static void edge_shutdown(struct usb_serial *serial)
2674 for (i = 0; i < serial->num_ports; ++i) { 2673 for (i = 0; i < serial->num_ports; ++i) {
2675 edge_port = usb_get_serial_port_data(serial->port[i]); 2674 edge_port = usb_get_serial_port_data(serial->port[i]);
2676 edge_remove_sysfs_attrs(edge_port->port); 2675 edge_remove_sysfs_attrs(edge_port->port);
2676 }
2677}
2678
2679static void edge_release(struct usb_serial *serial)
2680{
2681 int i;
2682 struct edgeport_port *edge_port;
2683
2684 dbg("%s", __func__);
2685
2686 for (i = 0; i < serial->num_ports; ++i) {
2687 edge_port = usb_get_serial_port_data(serial->port[i]);
2677 edge_buf_free(edge_port->ep_out_buf); 2688 edge_buf_free(edge_port->ep_out_buf);
2678 kfree(edge_port); 2689 kfree(edge_port);
2679 usb_set_serial_port_data(serial->port[i], NULL);
2680 } 2690 }
2681 kfree(usb_get_serial_data(serial)); 2691 kfree(usb_get_serial_data(serial));
2682 usb_set_serial_data(serial, NULL);
2683} 2692}
2684 2693
2685 2694
@@ -2916,7 +2925,8 @@ static struct usb_serial_driver edgeport_1port_device = {
2916 .throttle = edge_throttle, 2925 .throttle = edge_throttle,
2917 .unthrottle = edge_unthrottle, 2926 .unthrottle = edge_unthrottle,
2918 .attach = edge_startup, 2927 .attach = edge_startup,
2919 .shutdown = edge_shutdown, 2928 .disconnect = edge_disconnect,
2929 .release = edge_release,
2920 .port_probe = edge_create_sysfs_attrs, 2930 .port_probe = edge_create_sysfs_attrs,
2921 .ioctl = edge_ioctl, 2931 .ioctl = edge_ioctl,
2922 .set_termios = edge_set_termios, 2932 .set_termios = edge_set_termios,
@@ -2945,7 +2955,8 @@ static struct usb_serial_driver edgeport_2port_device = {
2945 .throttle = edge_throttle, 2955 .throttle = edge_throttle,
2946 .unthrottle = edge_unthrottle, 2956 .unthrottle = edge_unthrottle,
2947 .attach = edge_startup, 2957 .attach = edge_startup,
2948 .shutdown = edge_shutdown, 2958 .disconnect = edge_disconnect,
2959 .release = edge_release,
2949 .port_probe = edge_create_sysfs_attrs, 2960 .port_probe = edge_create_sysfs_attrs,
2950 .ioctl = edge_ioctl, 2961 .ioctl = edge_ioctl,
2951 .set_termios = edge_set_termios, 2962 .set_termios = edge_set_termios,
diff --git a/drivers/usb/serial/ipaq.c b/drivers/usb/serial/ipaq.c
index cd62825a9ac3..2545d45ce16f 100644
--- a/drivers/usb/serial/ipaq.c
+++ b/drivers/usb/serial/ipaq.c
@@ -76,11 +76,9 @@ static int initial_wait;
76/* Function prototypes for an ipaq */ 76/* Function prototypes for an ipaq */
77static int ipaq_open(struct tty_struct *tty, 77static int ipaq_open(struct tty_struct *tty,
78 struct usb_serial_port *port, struct file *filp); 78 struct usb_serial_port *port, struct file *filp);
79static void ipaq_close(struct tty_struct *tty, 79static void ipaq_close(struct usb_serial_port *port);
80 struct usb_serial_port *port, struct file *filp);
81static int ipaq_calc_num_ports(struct usb_serial *serial); 80static int ipaq_calc_num_ports(struct usb_serial *serial);
82static int ipaq_startup(struct usb_serial *serial); 81static int ipaq_startup(struct usb_serial *serial);
83static void ipaq_shutdown(struct usb_serial *serial);
84static int ipaq_write(struct tty_struct *tty, struct usb_serial_port *port, 82static int ipaq_write(struct tty_struct *tty, struct usb_serial_port *port,
85 const unsigned char *buf, int count); 83 const unsigned char *buf, int count);
86static int ipaq_write_bulk(struct usb_serial_port *port, 84static int ipaq_write_bulk(struct usb_serial_port *port,
@@ -577,7 +575,6 @@ static struct usb_serial_driver ipaq_device = {
577 .close = ipaq_close, 575 .close = ipaq_close,
578 .attach = ipaq_startup, 576 .attach = ipaq_startup,
579 .calc_num_ports = ipaq_calc_num_ports, 577 .calc_num_ports = ipaq_calc_num_ports,
580 .shutdown = ipaq_shutdown,
581 .write = ipaq_write, 578 .write = ipaq_write,
582 .write_room = ipaq_write_room, 579 .write_room = ipaq_write_room,
583 .chars_in_buffer = ipaq_chars_in_buffer, 580 .chars_in_buffer = ipaq_chars_in_buffer,
@@ -714,8 +711,7 @@ error:
714} 711}
715 712
716 713
717static void ipaq_close(struct tty_struct *tty, 714static void ipaq_close(struct usb_serial_port *port)
718 struct usb_serial_port *port, struct file *filp)
719{ 715{
720 struct ipaq_private *priv = usb_get_serial_port_data(port); 716 struct ipaq_private *priv = usb_get_serial_port_data(port);
721 717
@@ -992,11 +988,6 @@ static int ipaq_startup(struct usb_serial *serial)
992 return usb_reset_configuration(serial->dev); 988 return usb_reset_configuration(serial->dev);
993} 989}
994 990
995static void ipaq_shutdown(struct usb_serial *serial)
996{
997 dbg("%s", __func__);
998}
999
1000static int __init ipaq_init(void) 991static int __init ipaq_init(void)
1001{ 992{
1002 int retval; 993 int retval;
diff --git a/drivers/usb/serial/ipw.c b/drivers/usb/serial/ipw.c
index da2a2b46644a..29ad038b9c8d 100644
--- a/drivers/usb/serial/ipw.c
+++ b/drivers/usb/serial/ipw.c
@@ -302,23 +302,17 @@ static int ipw_open(struct tty_struct *tty,
302 return 0; 302 return 0;
303} 303}
304 304
305static void ipw_close(struct tty_struct *tty, 305static void ipw_dtr_rts(struct usb_serial_port *port, int on)
306 struct usb_serial_port *port, struct file *filp)
307{ 306{
308 struct usb_device *dev = port->serial->dev; 307 struct usb_device *dev = port->serial->dev;
309 int result; 308 int result;
310 309
311 if (tty_hung_up_p(filp)) {
312 dbg("%s: tty_hung_up_p ...", __func__);
313 return;
314 }
315
316 /*--1: drop the dtr */ 310 /*--1: drop the dtr */
317 dbg("%s:dropping dtr", __func__); 311 dbg("%s:dropping dtr", __func__);
318 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 312 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
319 IPW_SIO_SET_PIN, 313 IPW_SIO_SET_PIN,
320 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT, 314 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
321 IPW_PIN_CLRDTR, 315 on ? IPW_PIN_SETDTR : IPW_PIN_CLRDTR,
322 0, 316 0,
323 NULL, 317 NULL,
324 0, 318 0,
@@ -332,7 +326,7 @@ static void ipw_close(struct tty_struct *tty,
332 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 326 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
333 IPW_SIO_SET_PIN, USB_TYPE_VENDOR | 327 IPW_SIO_SET_PIN, USB_TYPE_VENDOR |
334 USB_RECIP_INTERFACE | USB_DIR_OUT, 328 USB_RECIP_INTERFACE | USB_DIR_OUT,
335 IPW_PIN_CLRRTS, 329 on ? IPW_PIN_SETRTS : IPW_PIN_CLRRTS,
336 0, 330 0,
337 NULL, 331 NULL,
338 0, 332 0,
@@ -340,7 +334,12 @@ static void ipw_close(struct tty_struct *tty,
340 if (result < 0) 334 if (result < 0)
341 dev_err(&port->dev, 335 dev_err(&port->dev,
342 "dropping rts failed (error = %d)\n", result); 336 "dropping rts failed (error = %d)\n", result);
337}
343 338
339static void ipw_close(struct usb_serial_port *port)
340{
341 struct usb_device *dev = port->serial->dev;
342 int result;
344 343
345 /*--3: purge */ 344 /*--3: purge */
346 dbg("%s:sending purge", __func__); 345 dbg("%s:sending purge", __func__);
@@ -461,6 +460,7 @@ static struct usb_serial_driver ipw_device = {
461 .num_ports = 1, 460 .num_ports = 1,
462 .open = ipw_open, 461 .open = ipw_open,
463 .close = ipw_close, 462 .close = ipw_close,
463 .dtr_rts = ipw_dtr_rts,
464 .port_probe = ipw_probe, 464 .port_probe = ipw_probe,
465 .port_remove = ipw_disconnect, 465 .port_remove = ipw_disconnect,
466 .write = ipw_write, 466 .write = ipw_write,
diff --git a/drivers/usb/serial/ir-usb.c b/drivers/usb/serial/ir-usb.c
index 4e2cda93da59..66009b6b763a 100644
--- a/drivers/usb/serial/ir-usb.c
+++ b/drivers/usb/serial/ir-usb.c
@@ -88,8 +88,7 @@ static int xbof = -1;
88static int ir_startup (struct usb_serial *serial); 88static int ir_startup (struct usb_serial *serial);
89static int ir_open(struct tty_struct *tty, struct usb_serial_port *port, 89static int ir_open(struct tty_struct *tty, struct usb_serial_port *port,
90 struct file *filep); 90 struct file *filep);
91static void ir_close(struct tty_struct *tty, struct usb_serial_port *port, 91static void ir_close(struct usb_serial_port *port);
92 struct file *filep);
93static int ir_write(struct tty_struct *tty, struct usb_serial_port *port, 92static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
94 const unsigned char *buf, int count); 93 const unsigned char *buf, int count);
95static void ir_write_bulk_callback (struct urb *urb); 94static void ir_write_bulk_callback (struct urb *urb);
@@ -346,8 +345,7 @@ static int ir_open(struct tty_struct *tty,
346 return result; 345 return result;
347} 346}
348 347
349static void ir_close(struct tty_struct *tty, 348static void ir_close(struct usb_serial_port *port)
350 struct usb_serial_port *port, struct file * filp)
351{ 349{
352 dbg("%s - port %d", __func__, port->number); 350 dbg("%s - port %d", __func__, port->number);
353 351
diff --git a/drivers/usb/serial/iuu_phoenix.c b/drivers/usb/serial/iuu_phoenix.c
index 4473d442b2aa..96873a7a32b0 100644
--- a/drivers/usb/serial/iuu_phoenix.c
+++ b/drivers/usb/serial/iuu_phoenix.c
@@ -40,7 +40,7 @@ static int debug;
40/* 40/*
41 * Version Information 41 * Version Information
42 */ 42 */
43#define DRIVER_VERSION "v0.5" 43#define DRIVER_VERSION "v0.10"
44#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver" 44#define DRIVER_DESC "Infinity USB Unlimited Phoenix driver"
45 45
46static struct usb_device_id id_table[] = { 46static struct usb_device_id id_table[] = {
@@ -70,7 +70,6 @@ static void read_rxcmd_callback(struct urb *urb);
70struct iuu_private { 70struct iuu_private {
71 spinlock_t lock; /* store irq state */ 71 spinlock_t lock; /* store irq state */
72 wait_queue_head_t delta_msr_wait; 72 wait_queue_head_t delta_msr_wait;
73 u8 line_control;
74 u8 line_status; 73 u8 line_status;
75 u8 termios_initialized; 74 u8 termios_initialized;
76 int tiostatus; /* store IUART SIGNAL for tiocmget call */ 75 int tiostatus; /* store IUART SIGNAL for tiocmget call */
@@ -122,8 +121,8 @@ static int iuu_startup(struct usb_serial *serial)
122 return 0; 121 return 0;
123} 122}
124 123
125/* Shutdown function */ 124/* Release function */
126static void iuu_shutdown(struct usb_serial *serial) 125static void iuu_release(struct usb_serial *serial)
127{ 126{
128 struct usb_serial_port *port = serial->port[0]; 127 struct usb_serial_port *port = serial->port[0];
129 struct iuu_private *priv = usb_get_serial_port_data(port); 128 struct iuu_private *priv = usb_get_serial_port_data(port);
@@ -651,32 +650,33 @@ static int iuu_bulk_write(struct usb_serial_port *port)
651 unsigned long flags; 650 unsigned long flags;
652 int result; 651 int result;
653 int i; 652 int i;
653 int buf_len;
654 char *buf_ptr = port->write_urb->transfer_buffer; 654 char *buf_ptr = port->write_urb->transfer_buffer;
655 dbg("%s - enter", __func__); 655 dbg("%s - enter", __func__);
656 656
657 spin_lock_irqsave(&priv->lock, flags);
657 *buf_ptr++ = IUU_UART_ESC; 658 *buf_ptr++ = IUU_UART_ESC;
658 *buf_ptr++ = IUU_UART_TX; 659 *buf_ptr++ = IUU_UART_TX;
659 *buf_ptr++ = priv->writelen; 660 *buf_ptr++ = priv->writelen;
660 661
661 memcpy(buf_ptr, priv->writebuf, 662 memcpy(buf_ptr, priv->writebuf, priv->writelen);
662 priv->writelen); 663 buf_len = priv->writelen;
664 priv->writelen = 0;
665 spin_unlock_irqrestore(&priv->lock, flags);
663 if (debug == 1) { 666 if (debug == 1) {
664 for (i = 0; i < priv->writelen; i++) 667 for (i = 0; i < buf_len; i++)
665 sprintf(priv->dbgbuf + i*2 , 668 sprintf(priv->dbgbuf + i*2 ,
666 "%02X", priv->writebuf[i]); 669 "%02X", priv->writebuf[i]);
667 priv->dbgbuf[priv->writelen+i*2] = 0; 670 priv->dbgbuf[buf_len+i*2] = 0;
668 dbg("%s - writing %i chars : %s", __func__, 671 dbg("%s - writing %i chars : %s", __func__,
669 priv->writelen, priv->dbgbuf); 672 buf_len, priv->dbgbuf);
670 } 673 }
671 usb_fill_bulk_urb(port->write_urb, port->serial->dev, 674 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
672 usb_sndbulkpipe(port->serial->dev, 675 usb_sndbulkpipe(port->serial->dev,
673 port->bulk_out_endpointAddress), 676 port->bulk_out_endpointAddress),
674 port->write_urb->transfer_buffer, priv->writelen + 3, 677 port->write_urb->transfer_buffer, buf_len + 3,
675 iuu_rxcmd, port); 678 iuu_rxcmd, port);
676 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 679 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
677 spin_lock_irqsave(&priv->lock, flags);
678 priv->writelen = 0;
679 spin_unlock_irqrestore(&priv->lock, flags);
680 usb_serial_port_softint(port); 680 usb_serial_port_softint(port);
681 return result; 681 return result;
682} 682}
@@ -770,14 +770,10 @@ static int iuu_uart_write(struct tty_struct *tty, struct usb_serial_port *port,
770 return -ENOMEM; 770 return -ENOMEM;
771 771
772 spin_lock_irqsave(&priv->lock, flags); 772 spin_lock_irqsave(&priv->lock, flags);
773 if (priv->writelen > 0) { 773
774 /* buffer already filled but not commited */
775 spin_unlock_irqrestore(&priv->lock, flags);
776 return 0;
777 }
778 /* fill the buffer */ 774 /* fill the buffer */
779 memcpy(priv->writebuf, buf, count); 775 memcpy(priv->writebuf + priv->writelen, buf, count);
780 priv->writelen = count; 776 priv->writelen += count;
781 spin_unlock_irqrestore(&priv->lock, flags); 777 spin_unlock_irqrestore(&priv->lock, flags);
782 778
783 return count; 779 return count;
@@ -819,7 +815,7 @@ static int iuu_uart_on(struct usb_serial_port *port)
819 buf[0] = IUU_UART_ENABLE; 815 buf[0] = IUU_UART_ENABLE;
820 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF); 816 buf[1] = (u8) ((IUU_BAUD_9600 >> 8) & 0x00FF);
821 buf[2] = (u8) (0x00FF & IUU_BAUD_9600); 817 buf[2] = (u8) (0x00FF & IUU_BAUD_9600);
822 buf[3] = (u8) (0x0F0 & IUU_TWO_STOP_BITS) | (0x07 & IUU_PARITY_EVEN); 818 buf[3] = (u8) (0x0F0 & IUU_ONE_STOP_BIT) | (0x07 & IUU_PARITY_EVEN);
823 819
824 status = bulk_immediate(port, buf, 4); 820 status = bulk_immediate(port, buf, 4);
825 if (status != IUU_OPERATION_OK) { 821 if (status != IUU_OPERATION_OK) {
@@ -946,19 +942,59 @@ static int iuu_uart_baud(struct usb_serial_port *port, u32 baud,
946 return status; 942 return status;
947} 943}
948 944
949static int set_control_lines(struct usb_device *dev, u8 value) 945static void iuu_set_termios(struct tty_struct *tty,
946 struct usb_serial_port *port, struct ktermios *old_termios)
950{ 947{
951 return 0; 948 const u32 supported_mask = CMSPAR|PARENB|PARODD;
949
950 unsigned int cflag = tty->termios->c_cflag;
951 int status;
952 u32 actual;
953 u32 parity;
954 int csize = CS7;
955 int baud = 9600; /* Fixed for the moment */
956 u32 newval = cflag & supported_mask;
957
958 /* compute the parity parameter */
959 parity = 0;
960 if (cflag & CMSPAR) { /* Using mark space */
961 if (cflag & PARODD)
962 parity |= IUU_PARITY_SPACE;
963 else
964 parity |= IUU_PARITY_MARK;
965 } else if (!(cflag & PARENB)) {
966 parity |= IUU_PARITY_NONE;
967 csize = CS8;
968 } else if (cflag & PARODD)
969 parity |= IUU_PARITY_ODD;
970 else
971 parity |= IUU_PARITY_EVEN;
972
973 parity |= (cflag & CSTOPB ? IUU_TWO_STOP_BITS : IUU_ONE_STOP_BIT);
974
975 /* set it */
976 status = iuu_uart_baud(port,
977 (clockmode == 2) ? 16457 : 9600 * boost / 100,
978 &actual, parity);
979
980 /* set the termios value to the real one, so the user now what has
981 * changed. We support few fields so its easies to copy the old hw
982 * settings back over and then adjust them
983 */
984 if (old_termios)
985 tty_termios_copy_hw(tty->termios, old_termios);
986 if (status != 0) /* Set failed - return old bits */
987 return;
988 /* Re-encode speed, parity and csize */
989 tty_encode_baud_rate(tty, baud, baud);
990 tty->termios->c_cflag &= ~(supported_mask|CSIZE);
991 tty->termios->c_cflag |= newval | csize;
952} 992}
953 993
954static void iuu_close(struct tty_struct *tty, 994static void iuu_close(struct usb_serial_port *port)
955 struct usb_serial_port *port, struct file *filp)
956{ 995{
957 /* iuu_led (port,255,0,0,0); */ 996 /* iuu_led (port,255,0,0,0); */
958 struct usb_serial *serial; 997 struct usb_serial *serial;
959 struct iuu_private *priv = usb_get_serial_port_data(port);
960 unsigned long flags;
961 unsigned int c_cflag;
962 998
963 serial = port->serial; 999 serial = port->serial;
964 if (!serial) 1000 if (!serial)
@@ -968,17 +1004,6 @@ static void iuu_close(struct tty_struct *tty,
968 1004
969 iuu_uart_off(port); 1005 iuu_uart_off(port);
970 if (serial->dev) { 1006 if (serial->dev) {
971 if (tty) {
972 c_cflag = tty->termios->c_cflag;
973 if (c_cflag & HUPCL) {
974 /* drop DTR and RTS */
975 priv = usb_get_serial_port_data(port);
976 spin_lock_irqsave(&priv->lock, flags);
977 priv->line_control = 0;
978 spin_unlock_irqrestore(&priv->lock, flags);
979 set_control_lines(port->serial->dev, 0);
980 }
981 }
982 /* free writebuf */ 1007 /* free writebuf */
983 /* shutdown our urbs */ 1008 /* shutdown our urbs */
984 dbg("%s - shutting down urbs", __func__); 1009 dbg("%s - shutting down urbs", __func__);
@@ -1154,7 +1179,7 @@ static int iuu_open(struct tty_struct *tty,
1154 if (result) { 1179 if (result) {
1155 dev_err(&port->dev, "%s - failed submitting read urb," 1180 dev_err(&port->dev, "%s - failed submitting read urb,"
1156 " error %d\n", __func__, result); 1181 " error %d\n", __func__, result);
1157 iuu_close(tty, port, NULL); 1182 iuu_close(port);
1158 return -EPROTO; 1183 return -EPROTO;
1159 } else { 1184 } else {
1160 dbg("%s - rxcmd OK", __func__); 1185 dbg("%s - rxcmd OK", __func__);
@@ -1175,8 +1200,9 @@ static struct usb_serial_driver iuu_device = {
1175 .read_bulk_callback = iuu_uart_read_callback, 1200 .read_bulk_callback = iuu_uart_read_callback,
1176 .tiocmget = iuu_tiocmget, 1201 .tiocmget = iuu_tiocmget,
1177 .tiocmset = iuu_tiocmset, 1202 .tiocmset = iuu_tiocmset,
1203 .set_termios = iuu_set_termios,
1178 .attach = iuu_startup, 1204 .attach = iuu_startup,
1179 .shutdown = iuu_shutdown, 1205 .release = iuu_release,
1180}; 1206};
1181 1207
1182static int __init iuu_init(void) 1208static int __init iuu_init(void)
diff --git a/drivers/usb/serial/keyspan.c b/drivers/usb/serial/keyspan.c
index 00daa8f7759a..2594b8743d3f 100644
--- a/drivers/usb/serial/keyspan.c
+++ b/drivers/usb/serial/keyspan.c
@@ -1298,8 +1298,16 @@ static inline void stop_urb(struct urb *urb)
1298 usb_kill_urb(urb); 1298 usb_kill_urb(urb);
1299} 1299}
1300 1300
1301static void keyspan_close(struct tty_struct *tty, 1301static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1302 struct usb_serial_port *port, struct file *filp) 1302{
1303 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1304
1305 p_priv->rts_state = on;
1306 p_priv->dtr_state = on;
1307 keyspan_send_setup(port, 0);
1308}
1309
1310static void keyspan_close(struct usb_serial_port *port)
1303{ 1311{
1304 int i; 1312 int i;
1305 struct usb_serial *serial = port->serial; 1313 struct usb_serial *serial = port->serial;
@@ -1336,7 +1344,6 @@ static void keyspan_close(struct tty_struct *tty,
1336 stop_urb(p_priv->out_urbs[i]); 1344 stop_urb(p_priv->out_urbs[i]);
1337 } 1345 }
1338 } 1346 }
1339 tty_port_tty_set(&port->port, NULL);
1340} 1347}
1341 1348
1342/* download the firmware to a pre-renumeration device */ 1349/* download the firmware to a pre-renumeration device */
@@ -2682,7 +2689,7 @@ static int keyspan_startup(struct usb_serial *serial)
2682 return 0; 2689 return 0;
2683} 2690}
2684 2691
2685static void keyspan_shutdown(struct usb_serial *serial) 2692static void keyspan_disconnect(struct usb_serial *serial)
2686{ 2693{
2687 int i, j; 2694 int i, j;
2688 struct usb_serial_port *port; 2695 struct usb_serial_port *port;
@@ -2722,6 +2729,17 @@ static void keyspan_shutdown(struct usb_serial *serial)
2722 usb_free_urb(p_priv->out_urbs[j]); 2729 usb_free_urb(p_priv->out_urbs[j]);
2723 } 2730 }
2724 } 2731 }
2732}
2733
2734static void keyspan_release(struct usb_serial *serial)
2735{
2736 int i;
2737 struct usb_serial_port *port;
2738 struct keyspan_serial_private *s_priv;
2739
2740 dbg("%s", __func__);
2741
2742 s_priv = usb_get_serial_data(serial);
2725 2743
2726 /* dbg("Freeing serial->private."); */ 2744 /* dbg("Freeing serial->private."); */
2727 kfree(s_priv); 2745 kfree(s_priv);
diff --git a/drivers/usb/serial/keyspan.h b/drivers/usb/serial/keyspan.h
index 38b4582e0734..3107ed15af64 100644
--- a/drivers/usb/serial/keyspan.h
+++ b/drivers/usb/serial/keyspan.h
@@ -38,11 +38,11 @@
38static int keyspan_open (struct tty_struct *tty, 38static int keyspan_open (struct tty_struct *tty,
39 struct usb_serial_port *port, 39 struct usb_serial_port *port,
40 struct file *filp); 40 struct file *filp);
41static void keyspan_close (struct tty_struct *tty, 41static void keyspan_close (struct usb_serial_port *port);
42 struct usb_serial_port *port, 42static void keyspan_dtr_rts (struct usb_serial_port *port, int on);
43 struct file *filp);
44static int keyspan_startup (struct usb_serial *serial); 43static int keyspan_startup (struct usb_serial *serial);
45static void keyspan_shutdown (struct usb_serial *serial); 44static void keyspan_disconnect (struct usb_serial *serial);
45static void keyspan_release (struct usb_serial *serial);
46static int keyspan_write_room (struct tty_struct *tty); 46static int keyspan_write_room (struct tty_struct *tty);
47 47
48static int keyspan_write (struct tty_struct *tty, 48static int keyspan_write (struct tty_struct *tty,
@@ -562,6 +562,7 @@ static struct usb_serial_driver keyspan_1port_device = {
562 .num_ports = 1, 562 .num_ports = 1,
563 .open = keyspan_open, 563 .open = keyspan_open,
564 .close = keyspan_close, 564 .close = keyspan_close,
565 .dtr_rts = keyspan_dtr_rts,
565 .write = keyspan_write, 566 .write = keyspan_write,
566 .write_room = keyspan_write_room, 567 .write_room = keyspan_write_room,
567 .set_termios = keyspan_set_termios, 568 .set_termios = keyspan_set_termios,
@@ -569,7 +570,8 @@ static struct usb_serial_driver keyspan_1port_device = {
569 .tiocmget = keyspan_tiocmget, 570 .tiocmget = keyspan_tiocmget,
570 .tiocmset = keyspan_tiocmset, 571 .tiocmset = keyspan_tiocmset,
571 .attach = keyspan_startup, 572 .attach = keyspan_startup,
572 .shutdown = keyspan_shutdown, 573 .disconnect = keyspan_disconnect,
574 .release = keyspan_release,
573}; 575};
574 576
575static struct usb_serial_driver keyspan_2port_device = { 577static struct usb_serial_driver keyspan_2port_device = {
@@ -582,6 +584,7 @@ static struct usb_serial_driver keyspan_2port_device = {
582 .num_ports = 2, 584 .num_ports = 2,
583 .open = keyspan_open, 585 .open = keyspan_open,
584 .close = keyspan_close, 586 .close = keyspan_close,
587 .dtr_rts = keyspan_dtr_rts,
585 .write = keyspan_write, 588 .write = keyspan_write,
586 .write_room = keyspan_write_room, 589 .write_room = keyspan_write_room,
587 .set_termios = keyspan_set_termios, 590 .set_termios = keyspan_set_termios,
@@ -589,7 +592,8 @@ static struct usb_serial_driver keyspan_2port_device = {
589 .tiocmget = keyspan_tiocmget, 592 .tiocmget = keyspan_tiocmget,
590 .tiocmset = keyspan_tiocmset, 593 .tiocmset = keyspan_tiocmset,
591 .attach = keyspan_startup, 594 .attach = keyspan_startup,
592 .shutdown = keyspan_shutdown, 595 .disconnect = keyspan_disconnect,
596 .release = keyspan_release,
593}; 597};
594 598
595static struct usb_serial_driver keyspan_4port_device = { 599static struct usb_serial_driver keyspan_4port_device = {
@@ -602,6 +606,7 @@ static struct usb_serial_driver keyspan_4port_device = {
602 .num_ports = 4, 606 .num_ports = 4,
603 .open = keyspan_open, 607 .open = keyspan_open,
604 .close = keyspan_close, 608 .close = keyspan_close,
609 .dtr_rts = keyspan_dtr_rts,
605 .write = keyspan_write, 610 .write = keyspan_write,
606 .write_room = keyspan_write_room, 611 .write_room = keyspan_write_room,
607 .set_termios = keyspan_set_termios, 612 .set_termios = keyspan_set_termios,
@@ -609,7 +614,8 @@ static struct usb_serial_driver keyspan_4port_device = {
609 .tiocmget = keyspan_tiocmget, 614 .tiocmget = keyspan_tiocmget,
610 .tiocmset = keyspan_tiocmset, 615 .tiocmset = keyspan_tiocmset,
611 .attach = keyspan_startup, 616 .attach = keyspan_startup,
612 .shutdown = keyspan_shutdown, 617 .disconnect = keyspan_disconnect,
618 .release = keyspan_release,
613}; 619};
614 620
615#endif 621#endif
diff --git a/drivers/usb/serial/keyspan_pda.c b/drivers/usb/serial/keyspan_pda.c
index bf1ae247da66..d0b12e40c2b1 100644
--- a/drivers/usb/serial/keyspan_pda.c
+++ b/drivers/usb/serial/keyspan_pda.c
@@ -651,6 +651,35 @@ static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
651} 651}
652 652
653 653
654static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
655{
656 struct usb_serial *serial = port->serial;
657
658 if (serial->dev) {
659 if (on)
660 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
661 else
662 keyspan_pda_set_modem_info(serial, 0);
663 }
664}
665
666static int keyspan_pda_carrier_raised(struct usb_serial_port *port)
667{
668 struct usb_serial *serial = port->serial;
669 unsigned char modembits;
670
671 /* If we can read the modem status and the DCD is low then
672 carrier is not raised yet */
673 if (keyspan_pda_get_modem_info(serial, &modembits) >= 0) {
674 if (!(modembits & (1>>6)))
675 return 0;
676 }
677 /* Carrier raised, or we failed (eg disconnected) so
678 progress accordingly */
679 return 1;
680}
681
682
654static int keyspan_pda_open(struct tty_struct *tty, 683static int keyspan_pda_open(struct tty_struct *tty,
655 struct usb_serial_port *port, struct file *filp) 684 struct usb_serial_port *port, struct file *filp)
656{ 685{
@@ -682,13 +711,6 @@ static int keyspan_pda_open(struct tty_struct *tty,
682 priv->tx_room = room; 711 priv->tx_room = room;
683 priv->tx_throttled = room ? 0 : 1; 712 priv->tx_throttled = room ? 0 : 1;
684 713
685 /* the normal serial device seems to always turn on DTR and RTS here,
686 so do the same */
687 if (tty && (tty->termios->c_cflag & CBAUD))
688 keyspan_pda_set_modem_info(serial, (1<<7) | (1<<2));
689 else
690 keyspan_pda_set_modem_info(serial, 0);
691
692 /*Start reading from the device*/ 714 /*Start reading from the device*/
693 port->interrupt_in_urb->dev = serial->dev; 715 port->interrupt_in_urb->dev = serial->dev;
694 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 716 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
@@ -700,19 +722,11 @@ static int keyspan_pda_open(struct tty_struct *tty,
700error: 722error:
701 return rc; 723 return rc;
702} 724}
703 725static void keyspan_pda_close(struct usb_serial_port *port)
704
705static void keyspan_pda_close(struct tty_struct *tty,
706 struct usb_serial_port *port, struct file *filp)
707{ 726{
708 struct usb_serial *serial = port->serial; 727 struct usb_serial *serial = port->serial;
709 728
710 if (serial->dev) { 729 if (serial->dev) {
711 /* the normal serial device seems to always shut
712 off DTR and RTS now */
713 if (tty->termios->c_cflag & HUPCL)
714 keyspan_pda_set_modem_info(serial, 0);
715
716 /* shutdown our bulk reads and writes */ 730 /* shutdown our bulk reads and writes */
717 usb_kill_urb(port->write_urb); 731 usb_kill_urb(port->write_urb);
718 usb_kill_urb(port->interrupt_in_urb); 732 usb_kill_urb(port->interrupt_in_urb);
@@ -795,7 +809,7 @@ static int keyspan_pda_startup(struct usb_serial *serial)
795 return 0; 809 return 0;
796} 810}
797 811
798static void keyspan_pda_shutdown(struct usb_serial *serial) 812static void keyspan_pda_release(struct usb_serial *serial)
799{ 813{
800 dbg("%s", __func__); 814 dbg("%s", __func__);
801 815
@@ -839,6 +853,8 @@ static struct usb_serial_driver keyspan_pda_device = {
839 .usb_driver = &keyspan_pda_driver, 853 .usb_driver = &keyspan_pda_driver,
840 .id_table = id_table_std, 854 .id_table = id_table_std,
841 .num_ports = 1, 855 .num_ports = 1,
856 .dtr_rts = keyspan_pda_dtr_rts,
857 .carrier_raised = keyspan_pda_carrier_raised,
842 .open = keyspan_pda_open, 858 .open = keyspan_pda_open,
843 .close = keyspan_pda_close, 859 .close = keyspan_pda_close,
844 .write = keyspan_pda_write, 860 .write = keyspan_pda_write,
@@ -853,7 +869,7 @@ static struct usb_serial_driver keyspan_pda_device = {
853 .tiocmget = keyspan_pda_tiocmget, 869 .tiocmget = keyspan_pda_tiocmget,
854 .tiocmset = keyspan_pda_tiocmset, 870 .tiocmset = keyspan_pda_tiocmset,
855 .attach = keyspan_pda_startup, 871 .attach = keyspan_pda_startup,
856 .shutdown = keyspan_pda_shutdown, 872 .release = keyspan_pda_release,
857}; 873};
858 874
859 875
diff --git a/drivers/usb/serial/kl5kusb105.c b/drivers/usb/serial/kl5kusb105.c
index fcd9082f3e7f..0f44bb8e8d4f 100644
--- a/drivers/usb/serial/kl5kusb105.c
+++ b/drivers/usb/serial/kl5kusb105.c
@@ -73,11 +73,11 @@ static int debug;
73 * Function prototypes 73 * Function prototypes
74 */ 74 */
75static int klsi_105_startup(struct usb_serial *serial); 75static int klsi_105_startup(struct usb_serial *serial);
76static void klsi_105_shutdown(struct usb_serial *serial); 76static void klsi_105_disconnect(struct usb_serial *serial);
77static void klsi_105_release(struct usb_serial *serial);
77static int klsi_105_open(struct tty_struct *tty, 78static int klsi_105_open(struct tty_struct *tty,
78 struct usb_serial_port *port, struct file *filp); 79 struct usb_serial_port *port, struct file *filp);
79static void klsi_105_close(struct tty_struct *tty, 80static void klsi_105_close(struct usb_serial_port *port);
80 struct usb_serial_port *port, struct file *filp);
81static int klsi_105_write(struct tty_struct *tty, 81static int klsi_105_write(struct tty_struct *tty,
82 struct usb_serial_port *port, const unsigned char *buf, int count); 82 struct usb_serial_port *port, const unsigned char *buf, int count);
83static void klsi_105_write_bulk_callback(struct urb *urb); 83static void klsi_105_write_bulk_callback(struct urb *urb);
@@ -132,7 +132,8 @@ static struct usb_serial_driver kl5kusb105d_device = {
132 .tiocmget = klsi_105_tiocmget, 132 .tiocmget = klsi_105_tiocmget,
133 .tiocmset = klsi_105_tiocmset, 133 .tiocmset = klsi_105_tiocmset,
134 .attach = klsi_105_startup, 134 .attach = klsi_105_startup,
135 .shutdown = klsi_105_shutdown, 135 .disconnect = klsi_105_disconnect,
136 .release = klsi_105_release,
136 .throttle = klsi_105_throttle, 137 .throttle = klsi_105_throttle,
137 .unthrottle = klsi_105_unthrottle, 138 .unthrottle = klsi_105_unthrottle,
138}; 139};
@@ -316,7 +317,7 @@ err_cleanup:
316} /* klsi_105_startup */ 317} /* klsi_105_startup */
317 318
318 319
319static void klsi_105_shutdown(struct usb_serial *serial) 320static void klsi_105_disconnect(struct usb_serial *serial)
320{ 321{
321 int i; 322 int i;
322 323
@@ -326,33 +327,36 @@ static void klsi_105_shutdown(struct usb_serial *serial)
326 for (i = 0; i < serial->num_ports; ++i) { 327 for (i = 0; i < serial->num_ports; ++i) {
327 struct klsi_105_private *priv = 328 struct klsi_105_private *priv =
328 usb_get_serial_port_data(serial->port[i]); 329 usb_get_serial_port_data(serial->port[i]);
329 unsigned long flags;
330 330
331 if (priv) { 331 if (priv) {
332 /* kill our write urb pool */ 332 /* kill our write urb pool */
333 int j; 333 int j;
334 struct urb **write_urbs = priv->write_urb_pool; 334 struct urb **write_urbs = priv->write_urb_pool;
335 spin_lock_irqsave(&priv->lock, flags);
336 335
337 for (j = 0; j < NUM_URBS; j++) { 336 for (j = 0; j < NUM_URBS; j++) {
338 if (write_urbs[j]) { 337 if (write_urbs[j]) {
339 /* FIXME - uncomment the following 338 usb_kill_urb(write_urbs[j]);
340 * usb_kill_urb call when the host
341 * controllers get fixed to set
342 * urb->dev = NULL after the urb is
343 * finished. Otherwise this call
344 * oopses. */
345 /* usb_kill_urb(write_urbs[j]); */
346 kfree(write_urbs[j]->transfer_buffer);
347 usb_free_urb(write_urbs[j]); 339 usb_free_urb(write_urbs[j]);
348 } 340 }
349 } 341 }
350 spin_unlock_irqrestore(&priv->lock, flags);
351 kfree(priv);
352 usb_set_serial_port_data(serial->port[i], NULL);
353 } 342 }
354 } 343 }
355} /* klsi_105_shutdown */ 344} /* klsi_105_disconnect */
345
346
347static void klsi_105_release(struct usb_serial *serial)
348{
349 int i;
350
351 dbg("%s", __func__);
352
353 for (i = 0; i < serial->num_ports; ++i) {
354 struct klsi_105_private *priv =
355 usb_get_serial_port_data(serial->port[i]);
356
357 kfree(priv);
358 }
359} /* klsi_105_release */
356 360
357static int klsi_105_open(struct tty_struct *tty, 361static int klsi_105_open(struct tty_struct *tty,
358 struct usb_serial_port *port, struct file *filp) 362 struct usb_serial_port *port, struct file *filp)
@@ -447,8 +451,7 @@ exit:
447} /* klsi_105_open */ 451} /* klsi_105_open */
448 452
449 453
450static void klsi_105_close(struct tty_struct *tty, 454static void klsi_105_close(struct usb_serial_port *port)
451 struct usb_serial_port *port, struct file *filp)
452{ 455{
453 struct klsi_105_private *priv = usb_get_serial_port_data(port); 456 struct klsi_105_private *priv = usb_get_serial_port_data(port);
454 int rc; 457 int rc;
diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
index c148544953b3..6db0e561f680 100644
--- a/drivers/usb/serial/kobil_sct.c
+++ b/drivers/usb/serial/kobil_sct.c
@@ -69,11 +69,10 @@ static int debug;
69 69
70/* Function prototypes */ 70/* Function prototypes */
71static int kobil_startup(struct usb_serial *serial); 71static int kobil_startup(struct usb_serial *serial);
72static void kobil_shutdown(struct usb_serial *serial); 72static void kobil_release(struct usb_serial *serial);
73static int kobil_open(struct tty_struct *tty, 73static int kobil_open(struct tty_struct *tty,
74 struct usb_serial_port *port, struct file *filp); 74 struct usb_serial_port *port, struct file *filp);
75static void kobil_close(struct tty_struct *tty, struct usb_serial_port *port, 75static void kobil_close(struct usb_serial_port *port);
76 struct file *filp);
77static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port, 76static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
78 const unsigned char *buf, int count); 77 const unsigned char *buf, int count);
79static int kobil_write_room(struct tty_struct *tty); 78static int kobil_write_room(struct tty_struct *tty);
@@ -118,7 +117,7 @@ static struct usb_serial_driver kobil_device = {
118 .id_table = id_table, 117 .id_table = id_table,
119 .num_ports = 1, 118 .num_ports = 1,
120 .attach = kobil_startup, 119 .attach = kobil_startup,
121 .shutdown = kobil_shutdown, 120 .release = kobil_release,
122 .ioctl = kobil_ioctl, 121 .ioctl = kobil_ioctl,
123 .set_termios = kobil_set_termios, 122 .set_termios = kobil_set_termios,
124 .tiocmget = kobil_tiocmget, 123 .tiocmget = kobil_tiocmget,
@@ -202,17 +201,13 @@ static int kobil_startup(struct usb_serial *serial)
202} 201}
203 202
204 203
205static void kobil_shutdown(struct usb_serial *serial) 204static void kobil_release(struct usb_serial *serial)
206{ 205{
207 int i; 206 int i;
208 dbg("%s - port %d", __func__, serial->port[0]->number); 207 dbg("%s - port %d", __func__, serial->port[0]->number);
209 208
210 for (i = 0; i < serial->num_ports; ++i) { 209 for (i = 0; i < serial->num_ports; ++i)
211 while (serial->port[i]->port.count > 0)
212 kobil_close(NULL, serial->port[i], NULL);
213 kfree(usb_get_serial_port_data(serial->port[i])); 210 kfree(usb_get_serial_port_data(serial->port[i]));
214 usb_set_serial_port_data(serial->port[i], NULL);
215 }
216} 211}
217 212
218 213
@@ -346,11 +341,11 @@ static int kobil_open(struct tty_struct *tty,
346} 341}
347 342
348 343
349static void kobil_close(struct tty_struct *tty, 344static void kobil_close(struct usb_serial_port *port)
350 struct usb_serial_port *port, struct file *filp)
351{ 345{
352 dbg("%s - port %d", __func__, port->number); 346 dbg("%s - port %d", __func__, port->number);
353 347
348 /* FIXME: Add rts/dtr methods */
354 if (port->write_urb) { 349 if (port->write_urb) {
355 usb_kill_urb(port->write_urb); 350 usb_kill_urb(port->write_urb);
356 usb_free_urb(port->write_urb); 351 usb_free_urb(port->write_urb);
diff --git a/drivers/usb/serial/mct_u232.c b/drivers/usb/serial/mct_u232.c
index 82930a7d5093..d8825e159aa5 100644
--- a/drivers/usb/serial/mct_u232.c
+++ b/drivers/usb/serial/mct_u232.c
@@ -92,11 +92,11 @@ static int debug;
92 * Function prototypes 92 * Function prototypes
93 */ 93 */
94static int mct_u232_startup(struct usb_serial *serial); 94static int mct_u232_startup(struct usb_serial *serial);
95static void mct_u232_shutdown(struct usb_serial *serial); 95static void mct_u232_release(struct usb_serial *serial);
96static int mct_u232_open(struct tty_struct *tty, 96static int mct_u232_open(struct tty_struct *tty,
97 struct usb_serial_port *port, struct file *filp); 97 struct usb_serial_port *port, struct file *filp);
98static void mct_u232_close(struct tty_struct *tty, 98static void mct_u232_close(struct usb_serial_port *port);
99 struct usb_serial_port *port, struct file *filp); 99static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
100static void mct_u232_read_int_callback(struct urb *urb); 100static void mct_u232_read_int_callback(struct urb *urb);
101static void mct_u232_set_termios(struct tty_struct *tty, 101static void mct_u232_set_termios(struct tty_struct *tty,
102 struct usb_serial_port *port, struct ktermios *old); 102 struct usb_serial_port *port, struct ktermios *old);
@@ -140,6 +140,7 @@ static struct usb_serial_driver mct_u232_device = {
140 .num_ports = 1, 140 .num_ports = 1,
141 .open = mct_u232_open, 141 .open = mct_u232_open,
142 .close = mct_u232_close, 142 .close = mct_u232_close,
143 .dtr_rts = mct_u232_dtr_rts,
143 .throttle = mct_u232_throttle, 144 .throttle = mct_u232_throttle,
144 .unthrottle = mct_u232_unthrottle, 145 .unthrottle = mct_u232_unthrottle,
145 .read_int_callback = mct_u232_read_int_callback, 146 .read_int_callback = mct_u232_read_int_callback,
@@ -148,7 +149,7 @@ static struct usb_serial_driver mct_u232_device = {
148 .tiocmget = mct_u232_tiocmget, 149 .tiocmget = mct_u232_tiocmget,
149 .tiocmset = mct_u232_tiocmset, 150 .tiocmset = mct_u232_tiocmset,
150 .attach = mct_u232_startup, 151 .attach = mct_u232_startup,
151 .shutdown = mct_u232_shutdown, 152 .release = mct_u232_release,
152}; 153};
153 154
154 155
@@ -406,7 +407,7 @@ static int mct_u232_startup(struct usb_serial *serial)
406} /* mct_u232_startup */ 407} /* mct_u232_startup */
407 408
408 409
409static void mct_u232_shutdown(struct usb_serial *serial) 410static void mct_u232_release(struct usb_serial *serial)
410{ 411{
411 struct mct_u232_private *priv; 412 struct mct_u232_private *priv;
412 int i; 413 int i;
@@ -416,12 +417,9 @@ static void mct_u232_shutdown(struct usb_serial *serial)
416 for (i = 0; i < serial->num_ports; ++i) { 417 for (i = 0; i < serial->num_ports; ++i) {
417 /* My special items, the standard routines free my urbs */ 418 /* My special items, the standard routines free my urbs */
418 priv = usb_get_serial_port_data(serial->port[i]); 419 priv = usb_get_serial_port_data(serial->port[i]);
419 if (priv) { 420 kfree(priv);
420 usb_set_serial_port_data(serial->port[i], NULL);
421 kfree(priv);
422 }
423 } 421 }
424} /* mct_u232_shutdown */ 422} /* mct_u232_release */
425 423
426static int mct_u232_open(struct tty_struct *tty, 424static int mct_u232_open(struct tty_struct *tty,
427 struct usb_serial_port *port, struct file *filp) 425 struct usb_serial_port *port, struct file *filp)
@@ -496,29 +494,29 @@ error:
496 return retval; 494 return retval;
497} /* mct_u232_open */ 495} /* mct_u232_open */
498 496
499 497static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
500static void mct_u232_close(struct tty_struct *tty,
501 struct usb_serial_port *port, struct file *filp)
502{ 498{
503 unsigned int c_cflag;
504 unsigned int control_state; 499 unsigned int control_state;
505 struct mct_u232_private *priv = usb_get_serial_port_data(port); 500 struct mct_u232_private *priv = usb_get_serial_port_data(port);
506 dbg("%s port %d", __func__, port->number);
507 501
508 if (tty) { 502 mutex_lock(&port->serial->disc_mutex);
509 c_cflag = tty->termios->c_cflag; 503 if (!port->serial->disconnected) {
510 mutex_lock(&port->serial->disc_mutex); 504 /* drop DTR and RTS */
511 if (c_cflag & HUPCL && !port->serial->disconnected) { 505 spin_lock_irq(&priv->lock);
512 /* drop DTR and RTS */ 506 if (on)
513 spin_lock_irq(&priv->lock); 507 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
508 else
514 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS); 509 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
515 control_state = priv->control_state; 510 control_state = priv->control_state;
516 spin_unlock_irq(&priv->lock); 511 spin_unlock_irq(&priv->lock);
517 mct_u232_set_modem_ctrl(port->serial, control_state); 512 mct_u232_set_modem_ctrl(port->serial, control_state);
518 }
519 mutex_unlock(&port->serial->disc_mutex);
520 } 513 }
514 mutex_unlock(&port->serial->disc_mutex);
515}
521 516
517static void mct_u232_close(struct usb_serial_port *port)
518{
519 dbg("%s port %d", __func__, port->number);
522 520
523 if (port->serial->dev) { 521 if (port->serial->dev) {
524 /* shutdown our urbs */ 522 /* shutdown our urbs */
diff --git a/drivers/usb/serial/mos7720.c b/drivers/usb/serial/mos7720.c
index 24e3b5d4b4d4..ccd4dd340d2c 100644
--- a/drivers/usb/serial/mos7720.c
+++ b/drivers/usb/serial/mos7720.c
@@ -521,7 +521,7 @@ static int mos7720_chars_in_buffer(struct tty_struct *tty)
521 mos7720_port = usb_get_serial_port_data(port); 521 mos7720_port = usb_get_serial_port_data(port);
522 if (mos7720_port == NULL) { 522 if (mos7720_port == NULL) {
523 dbg("%s:leaving ...........", __func__); 523 dbg("%s:leaving ...........", __func__);
524 return -ENODEV; 524 return 0;
525 } 525 }
526 526
527 for (i = 0; i < NUM_URBS; ++i) { 527 for (i = 0; i < NUM_URBS; ++i) {
@@ -533,8 +533,7 @@ static int mos7720_chars_in_buffer(struct tty_struct *tty)
533 return chars; 533 return chars;
534} 534}
535 535
536static void mos7720_close(struct tty_struct *tty, 536static void mos7720_close(struct usb_serial_port *port)
537 struct usb_serial_port *port, struct file *filp)
538{ 537{
539 struct usb_serial *serial; 538 struct usb_serial *serial;
540 struct moschip_port *mos7720_port; 539 struct moschip_port *mos7720_port;
@@ -1522,19 +1521,16 @@ static int mos7720_startup(struct usb_serial *serial)
1522 return 0; 1521 return 0;
1523} 1522}
1524 1523
1525static void mos7720_shutdown(struct usb_serial *serial) 1524static void mos7720_release(struct usb_serial *serial)
1526{ 1525{
1527 int i; 1526 int i;
1528 1527
1529 /* free private structure allocated for serial port */ 1528 /* free private structure allocated for serial port */
1530 for (i = 0; i < serial->num_ports; ++i) { 1529 for (i = 0; i < serial->num_ports; ++i)
1531 kfree(usb_get_serial_port_data(serial->port[i])); 1530 kfree(usb_get_serial_port_data(serial->port[i]));
1532 usb_set_serial_port_data(serial->port[i], NULL);
1533 }
1534 1531
1535 /* free private structure allocated for serial device */ 1532 /* free private structure allocated for serial device */
1536 kfree(usb_get_serial_data(serial)); 1533 kfree(usb_get_serial_data(serial));
1537 usb_set_serial_data(serial, NULL);
1538} 1534}
1539 1535
1540static struct usb_driver usb_driver = { 1536static struct usb_driver usb_driver = {
@@ -1559,7 +1555,7 @@ static struct usb_serial_driver moschip7720_2port_driver = {
1559 .throttle = mos7720_throttle, 1555 .throttle = mos7720_throttle,
1560 .unthrottle = mos7720_unthrottle, 1556 .unthrottle = mos7720_unthrottle,
1561 .attach = mos7720_startup, 1557 .attach = mos7720_startup,
1562 .shutdown = mos7720_shutdown, 1558 .release = mos7720_release,
1563 .ioctl = mos7720_ioctl, 1559 .ioctl = mos7720_ioctl,
1564 .set_termios = mos7720_set_termios, 1560 .set_termios = mos7720_set_termios,
1565 .write = mos7720_write, 1561 .write = mos7720_write,
diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index 84fb1dcd30dc..c31940a307f8 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -26,6 +26,7 @@
26#include <linux/errno.h> 26#include <linux/errno.h>
27#include <linux/init.h> 27#include <linux/init.h>
28#include <linux/slab.h> 28#include <linux/slab.h>
29#include <linux/smp_lock.h>
29#include <linux/tty.h> 30#include <linux/tty.h>
30#include <linux/tty_driver.h> 31#include <linux/tty_driver.h>
31#include <linux/tty_flip.h> 32#include <linux/tty_flip.h>
@@ -238,7 +239,7 @@ static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
238{ 239{
239 struct usb_device *dev = port->serial->dev; 240 struct usb_device *dev = port->serial->dev;
240 val = val & 0x00ff; 241 val = val & 0x00ff;
241 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val); 242 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
242 243
243 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 244 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
244 MCS_WR_RTYPE, val, reg, NULL, 0, 245 MCS_WR_RTYPE, val, reg, NULL, 0,
@@ -260,7 +261,7 @@ static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
260 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 261 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
261 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH, 262 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
262 MOS_WDR_TIMEOUT); 263 MOS_WDR_TIMEOUT);
263 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val); 264 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
264 *val = (*val) & 0x00ff; 265 *val = (*val) & 0x00ff;
265 return ret; 266 return ret;
266} 267}
@@ -282,18 +283,18 @@ static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
282 if (port->serial->num_ports == 4) { 283 if (port->serial->num_ports == 4) {
283 val |= (((__u16) port->number - 284 val |= (((__u16) port->number -
284 (__u16) (port->serial->minor)) + 1) << 8; 285 (__u16) (port->serial->minor)) + 1) << 8;
285 dbg("mos7840_set_uart_reg application number is %x\n", val); 286 dbg("mos7840_set_uart_reg application number is %x", val);
286 } else { 287 } else {
287 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 288 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
288 val |= (((__u16) port->number - 289 val |= (((__u16) port->number -
289 (__u16) (port->serial->minor)) + 1) << 8; 290 (__u16) (port->serial->minor)) + 1) << 8;
290 dbg("mos7840_set_uart_reg application number is %x\n", 291 dbg("mos7840_set_uart_reg application number is %x",
291 val); 292 val);
292 } else { 293 } else {
293 val |= 294 val |=
294 (((__u16) port->number - 295 (((__u16) port->number -
295 (__u16) (port->serial->minor)) + 2) << 8; 296 (__u16) (port->serial->minor)) + 2) << 8;
296 dbg("mos7840_set_uart_reg application number is %x\n", 297 dbg("mos7840_set_uart_reg application number is %x",
297 val); 298 val);
298 } 299 }
299 } 300 }
@@ -315,24 +316,24 @@ static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
315 int ret = 0; 316 int ret = 0;
316 __u16 Wval; 317 __u16 Wval;
317 318
318 /* dbg("application number is %4x \n", 319 /* dbg("application number is %4x",
319 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */ 320 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
320 /* Wval is same as application number */ 321 /* Wval is same as application number */
321 if (port->serial->num_ports == 4) { 322 if (port->serial->num_ports == 4) {
322 Wval = 323 Wval =
323 (((__u16) port->number - (__u16) (port->serial->minor)) + 324 (((__u16) port->number - (__u16) (port->serial->minor)) +
324 1) << 8; 325 1) << 8;
325 dbg("mos7840_get_uart_reg application number is %x\n", Wval); 326 dbg("mos7840_get_uart_reg application number is %x", Wval);
326 } else { 327 } else {
327 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 328 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
328 Wval = (((__u16) port->number - 329 Wval = (((__u16) port->number -
329 (__u16) (port->serial->minor)) + 1) << 8; 330 (__u16) (port->serial->minor)) + 1) << 8;
330 dbg("mos7840_get_uart_reg application number is %x\n", 331 dbg("mos7840_get_uart_reg application number is %x",
331 Wval); 332 Wval);
332 } else { 333 } else {
333 Wval = (((__u16) port->number - 334 Wval = (((__u16) port->number -
334 (__u16) (port->serial->minor)) + 2) << 8; 335 (__u16) (port->serial->minor)) + 2) << 8;
335 dbg("mos7840_get_uart_reg application number is %x\n", 336 dbg("mos7840_get_uart_reg application number is %x",
336 Wval); 337 Wval);
337 } 338 }
338 } 339 }
@@ -346,11 +347,11 @@ static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
346static void mos7840_dump_serial_port(struct moschip_port *mos7840_port) 347static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
347{ 348{
348 349
349 dbg("***************************************\n"); 350 dbg("***************************************");
350 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset); 351 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
351 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset); 352 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
352 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset); 353 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
353 dbg("***************************************\n"); 354 dbg("***************************************");
354 355
355} 356}
356 357
@@ -474,12 +475,12 @@ static void mos7840_control_callback(struct urb *urb)
474 goto exit; 475 goto exit;
475 } 476 }
476 477
477 dbg("%s urb buffer size is %d\n", __func__, urb->actual_length); 478 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
478 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__, 479 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
479 mos7840_port->MsrLsr, mos7840_port->port_num); 480 mos7840_port->MsrLsr, mos7840_port->port_num);
480 data = urb->transfer_buffer; 481 data = urb->transfer_buffer;
481 regval = (__u8) data[0]; 482 regval = (__u8) data[0];
482 dbg("%s data is %x\n", __func__, regval); 483 dbg("%s data is %x", __func__, regval);
483 if (mos7840_port->MsrLsr == 0) 484 if (mos7840_port->MsrLsr == 0)
484 mos7840_handle_new_msr(mos7840_port, regval); 485 mos7840_handle_new_msr(mos7840_port, regval);
485 else if (mos7840_port->MsrLsr == 1) 486 else if (mos7840_port->MsrLsr == 1)
@@ -538,7 +539,7 @@ static void mos7840_interrupt_callback(struct urb *urb)
538 __u16 wval, wreg = 0; 539 __u16 wval, wreg = 0;
539 int status = urb->status; 540 int status = urb->status;
540 541
541 dbg("%s", " : Entering\n"); 542 dbg("%s", " : Entering");
542 543
543 switch (status) { 544 switch (status) {
544 case 0: 545 case 0:
@@ -570,7 +571,7 @@ static void mos7840_interrupt_callback(struct urb *urb)
570 * Byte 5 FIFO status for both */ 571 * Byte 5 FIFO status for both */
571 572
572 if (length && length > 5) { 573 if (length && length > 5) {
573 dbg("%s \n", "Wrong data !!!"); 574 dbg("%s", "Wrong data !!!");
574 return; 575 return;
575 } 576 }
576 577
@@ -587,17 +588,17 @@ static void mos7840_interrupt_callback(struct urb *urb)
587 (__u16) (serial->minor)) + 1) << 8; 588 (__u16) (serial->minor)) + 1) << 8;
588 if (mos7840_port->open) { 589 if (mos7840_port->open) {
589 if (sp[i] & 0x01) { 590 if (sp[i] & 0x01) {
590 dbg("SP%d No Interrupt !!!\n", i); 591 dbg("SP%d No Interrupt !!!", i);
591 } else { 592 } else {
592 switch (sp[i] & 0x0f) { 593 switch (sp[i] & 0x0f) {
593 case SERIAL_IIR_RLS: 594 case SERIAL_IIR_RLS:
594 dbg("Serial Port %d: Receiver status error or ", i); 595 dbg("Serial Port %d: Receiver status error or ", i);
595 dbg("address bit detected in 9-bit mode\n"); 596 dbg("address bit detected in 9-bit mode");
596 mos7840_port->MsrLsr = 1; 597 mos7840_port->MsrLsr = 1;
597 wreg = LINE_STATUS_REGISTER; 598 wreg = LINE_STATUS_REGISTER;
598 break; 599 break;
599 case SERIAL_IIR_MS: 600 case SERIAL_IIR_MS:
600 dbg("Serial Port %d: Modem status change\n", i); 601 dbg("Serial Port %d: Modem status change", i);
601 mos7840_port->MsrLsr = 0; 602 mos7840_port->MsrLsr = 0;
602 wreg = MODEM_STATUS_REGISTER; 603 wreg = MODEM_STATUS_REGISTER;
603 break; 604 break;
@@ -689,7 +690,7 @@ static void mos7840_bulk_in_callback(struct urb *urb)
689 690
690 mos7840_port = urb->context; 691 mos7840_port = urb->context;
691 if (!mos7840_port) { 692 if (!mos7840_port) {
692 dbg("%s", "NULL mos7840_port pointer \n"); 693 dbg("%s", "NULL mos7840_port pointer");
693 mos7840_port->read_urb_busy = false; 694 mos7840_port->read_urb_busy = false;
694 return; 695 return;
695 } 696 }
@@ -702,41 +703,41 @@ static void mos7840_bulk_in_callback(struct urb *urb)
702 703
703 port = (struct usb_serial_port *)mos7840_port->port; 704 port = (struct usb_serial_port *)mos7840_port->port;
704 if (mos7840_port_paranoia_check(port, __func__)) { 705 if (mos7840_port_paranoia_check(port, __func__)) {
705 dbg("%s", "Port Paranoia failed \n"); 706 dbg("%s", "Port Paranoia failed");
706 mos7840_port->read_urb_busy = false; 707 mos7840_port->read_urb_busy = false;
707 return; 708 return;
708 } 709 }
709 710
710 serial = mos7840_get_usb_serial(port, __func__); 711 serial = mos7840_get_usb_serial(port, __func__);
711 if (!serial) { 712 if (!serial) {
712 dbg("%s\n", "Bad serial pointer "); 713 dbg("%s", "Bad serial pointer");
713 mos7840_port->read_urb_busy = false; 714 mos7840_port->read_urb_busy = false;
714 return; 715 return;
715 } 716 }
716 717
717 dbg("%s\n", "Entering... \n"); 718 dbg("%s", "Entering... ");
718 719
719 data = urb->transfer_buffer; 720 data = urb->transfer_buffer;
720 721
721 dbg("%s", "Entering ........... \n"); 722 dbg("%s", "Entering ...........");
722 723
723 if (urb->actual_length) { 724 if (urb->actual_length) {
724 tty = tty_port_tty_get(&mos7840_port->port->port); 725 tty = tty_port_tty_get(&mos7840_port->port->port);
725 if (tty) { 726 if (tty) {
726 tty_buffer_request_room(tty, urb->actual_length); 727 tty_buffer_request_room(tty, urb->actual_length);
727 tty_insert_flip_string(tty, data, urb->actual_length); 728 tty_insert_flip_string(tty, data, urb->actual_length);
728 dbg(" %s \n", data); 729 dbg(" %s ", data);
729 tty_flip_buffer_push(tty); 730 tty_flip_buffer_push(tty);
730 tty_kref_put(tty); 731 tty_kref_put(tty);
731 } 732 }
732 mos7840_port->icount.rx += urb->actual_length; 733 mos7840_port->icount.rx += urb->actual_length;
733 smp_wmb(); 734 smp_wmb();
734 dbg("mos7840_port->icount.rx is %d:\n", 735 dbg("mos7840_port->icount.rx is %d:",
735 mos7840_port->icount.rx); 736 mos7840_port->icount.rx);
736 } 737 }
737 738
738 if (!mos7840_port->read_urb) { 739 if (!mos7840_port->read_urb) {
739 dbg("%s", "URB KILLED !!!\n"); 740 dbg("%s", "URB KILLED !!!");
740 mos7840_port->read_urb_busy = false; 741 mos7840_port->read_urb_busy = false;
741 return; 742 return;
742 } 743 }
@@ -777,16 +778,16 @@ static void mos7840_bulk_out_data_callback(struct urb *urb)
777 spin_unlock(&mos7840_port->pool_lock); 778 spin_unlock(&mos7840_port->pool_lock);
778 779
779 if (status) { 780 if (status) {
780 dbg("nonzero write bulk status received:%d\n", status); 781 dbg("nonzero write bulk status received:%d", status);
781 return; 782 return;
782 } 783 }
783 784
784 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) { 785 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
785 dbg("%s", "Port Paranoia failed \n"); 786 dbg("%s", "Port Paranoia failed");
786 return; 787 return;
787 } 788 }
788 789
789 dbg("%s \n", "Entering ........."); 790 dbg("%s", "Entering .........");
790 791
791 tty = tty_port_tty_get(&mos7840_port->port->port); 792 tty = tty_port_tty_get(&mos7840_port->port->port);
792 if (tty && mos7840_port->open) 793 if (tty && mos7840_port->open)
@@ -830,15 +831,17 @@ static int mos7840_open(struct tty_struct *tty,
830 struct moschip_port *mos7840_port; 831 struct moschip_port *mos7840_port;
831 struct moschip_port *port0; 832 struct moschip_port *port0;
832 833
834 dbg ("%s enter", __func__);
835
833 if (mos7840_port_paranoia_check(port, __func__)) { 836 if (mos7840_port_paranoia_check(port, __func__)) {
834 dbg("%s", "Port Paranoia failed \n"); 837 dbg("%s", "Port Paranoia failed");
835 return -ENODEV; 838 return -ENODEV;
836 } 839 }
837 840
838 serial = port->serial; 841 serial = port->serial;
839 842
840 if (mos7840_serial_paranoia_check(serial, __func__)) { 843 if (mos7840_serial_paranoia_check(serial, __func__)) {
841 dbg("%s", "Serial Paranoia failed \n"); 844 dbg("%s", "Serial Paranoia failed");
842 return -ENODEV; 845 return -ENODEV;
843 } 846 }
844 847
@@ -891,20 +894,20 @@ static int mos7840_open(struct tty_struct *tty,
891 Data = 0x0; 894 Data = 0x0;
892 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 895 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
893 if (status < 0) { 896 if (status < 0) {
894 dbg("Reading Spreg failed\n"); 897 dbg("Reading Spreg failed");
895 return -1; 898 return -1;
896 } 899 }
897 Data |= 0x80; 900 Data |= 0x80;
898 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 901 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
899 if (status < 0) { 902 if (status < 0) {
900 dbg("writing Spreg failed\n"); 903 dbg("writing Spreg failed");
901 return -1; 904 return -1;
902 } 905 }
903 906
904 Data &= ~0x80; 907 Data &= ~0x80;
905 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 908 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
906 if (status < 0) { 909 if (status < 0) {
907 dbg("writing Spreg failed\n"); 910 dbg("writing Spreg failed");
908 return -1; 911 return -1;
909 } 912 }
910 /* End of block to be checked */ 913 /* End of block to be checked */
@@ -913,7 +916,7 @@ static int mos7840_open(struct tty_struct *tty,
913 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 916 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
914 &Data); 917 &Data);
915 if (status < 0) { 918 if (status < 0) {
916 dbg("Reading Controlreg failed\n"); 919 dbg("Reading Controlreg failed");
917 return -1; 920 return -1;
918 } 921 }
919 Data |= 0x08; /* Driver done bit */ 922 Data |= 0x08; /* Driver done bit */
@@ -921,7 +924,7 @@ static int mos7840_open(struct tty_struct *tty,
921 status = mos7840_set_reg_sync(port, 924 status = mos7840_set_reg_sync(port,
922 mos7840_port->ControlRegOffset, Data); 925 mos7840_port->ControlRegOffset, Data);
923 if (status < 0) { 926 if (status < 0) {
924 dbg("writing Controlreg failed\n"); 927 dbg("writing Controlreg failed");
925 return -1; 928 return -1;
926 } 929 }
927 /* do register settings here */ 930 /* do register settings here */
@@ -932,21 +935,21 @@ static int mos7840_open(struct tty_struct *tty,
932 Data = 0x00; 935 Data = 0x00;
933 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 936 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
934 if (status < 0) { 937 if (status < 0) {
935 dbg("disableing interrupts failed\n"); 938 dbg("disabling interrupts failed");
936 return -1; 939 return -1;
937 } 940 }
938 /* Set FIFO_CONTROL_REGISTER to the default value */ 941 /* Set FIFO_CONTROL_REGISTER to the default value */
939 Data = 0x00; 942 Data = 0x00;
940 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 943 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
941 if (status < 0) { 944 if (status < 0) {
942 dbg("Writing FIFO_CONTROL_REGISTER failed\n"); 945 dbg("Writing FIFO_CONTROL_REGISTER failed");
943 return -1; 946 return -1;
944 } 947 }
945 948
946 Data = 0xcf; 949 Data = 0xcf;
947 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 950 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
948 if (status < 0) { 951 if (status < 0) {
949 dbg("Writing FIFO_CONTROL_REGISTER failed\n"); 952 dbg("Writing FIFO_CONTROL_REGISTER failed");
950 return -1; 953 return -1;
951 } 954 }
952 955
@@ -1043,12 +1046,12 @@ static int mos7840_open(struct tty_struct *tty,
1043 * (can't set it up in mos7840_startup as the * 1046 * (can't set it up in mos7840_startup as the *
1044 * structures were not set up at that time.) */ 1047 * structures were not set up at that time.) */
1045 1048
1046 dbg("port number is %d \n", port->number); 1049 dbg("port number is %d", port->number);
1047 dbg("serial number is %d \n", port->serial->minor); 1050 dbg("serial number is %d", port->serial->minor);
1048 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress); 1051 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1049 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress); 1052 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1050 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress); 1053 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1051 dbg("port's number in the device is %d\n", mos7840_port->port_num); 1054 dbg("port's number in the device is %d", mos7840_port->port_num);
1052 mos7840_port->read_urb = port->read_urb; 1055 mos7840_port->read_urb = port->read_urb;
1053 1056
1054 /* set up our bulk in urb */ 1057 /* set up our bulk in urb */
@@ -1061,7 +1064,7 @@ static int mos7840_open(struct tty_struct *tty,
1061 mos7840_port->read_urb->transfer_buffer_length, 1064 mos7840_port->read_urb->transfer_buffer_length,
1062 mos7840_bulk_in_callback, mos7840_port); 1065 mos7840_bulk_in_callback, mos7840_port);
1063 1066
1064 dbg("mos7840_open: bulkin endpoint is %d\n", 1067 dbg("mos7840_open: bulkin endpoint is %d",
1065 port->bulk_in_endpointAddress); 1068 port->bulk_in_endpointAddress);
1066 mos7840_port->read_urb_busy = true; 1069 mos7840_port->read_urb_busy = true;
1067 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 1070 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
@@ -1087,9 +1090,11 @@ static int mos7840_open(struct tty_struct *tty,
1087 mos7840_port->icount.tx = 0; 1090 mos7840_port->icount.tx = 0;
1088 mos7840_port->icount.rx = 0; 1091 mos7840_port->icount.rx = 0;
1089 1092
1090 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", 1093 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1091 serial, mos7840_port, port); 1094 serial, mos7840_port, port);
1092 1095
1096 dbg ("%s leave", __func__);
1097
1093 return 0; 1098 return 0;
1094 1099
1095} 1100}
@@ -1112,16 +1117,16 @@ static int mos7840_chars_in_buffer(struct tty_struct *tty)
1112 unsigned long flags; 1117 unsigned long flags;
1113 struct moschip_port *mos7840_port; 1118 struct moschip_port *mos7840_port;
1114 1119
1115 dbg("%s \n", " mos7840_chars_in_buffer:entering ..........."); 1120 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
1116 1121
1117 if (mos7840_port_paranoia_check(port, __func__)) { 1122 if (mos7840_port_paranoia_check(port, __func__)) {
1118 dbg("%s", "Invalid port \n"); 1123 dbg("%s", "Invalid port");
1119 return 0; 1124 return 0;
1120 } 1125 }
1121 1126
1122 mos7840_port = mos7840_get_port_private(port); 1127 mos7840_port = mos7840_get_port_private(port);
1123 if (mos7840_port == NULL) { 1128 if (mos7840_port == NULL) {
1124 dbg("%s \n", "mos7840_break:leaving ..........."); 1129 dbg("%s", "mos7840_break:leaving ...........");
1125 return 0; 1130 return 0;
1126 } 1131 }
1127 1132
@@ -1135,54 +1140,12 @@ static int mos7840_chars_in_buffer(struct tty_struct *tty)
1135 1140
1136} 1141}
1137 1142
1138/************************************************************************
1139 *
1140 * mos7840_block_until_tx_empty
1141 *
1142 * This function will block the close until one of the following:
1143 * 1. TX count are 0
1144 * 2. The mos7840 has stopped
1145 * 3. A timeout of 3 seconds without activity has expired
1146 *
1147 ************************************************************************/
1148static void mos7840_block_until_tx_empty(struct tty_struct *tty,
1149 struct moschip_port *mos7840_port)
1150{
1151 int timeout = HZ / 10;
1152 int wait = 30;
1153 int count;
1154
1155 while (1) {
1156
1157 count = mos7840_chars_in_buffer(tty);
1158
1159 /* Check for Buffer status */
1160 if (count <= 0)
1161 return;
1162
1163 /* Block the thread for a while */
1164 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1165 timeout);
1166
1167 /* No activity.. count down section */
1168 wait--;
1169 if (wait == 0) {
1170 dbg("%s - TIMEOUT", __func__);
1171 return;
1172 } else {
1173 /* Reset timeout value back to seconds */
1174 wait = 30;
1175 }
1176 }
1177}
1178
1179/***************************************************************************** 1143/*****************************************************************************
1180 * mos7840_close 1144 * mos7840_close
1181 * this function is called by the tty driver when a port is closed 1145 * this function is called by the tty driver when a port is closed
1182 *****************************************************************************/ 1146 *****************************************************************************/
1183 1147
1184static void mos7840_close(struct tty_struct *tty, 1148static void mos7840_close(struct usb_serial_port *port)
1185 struct usb_serial_port *port, struct file *filp)
1186{ 1149{
1187 struct usb_serial *serial; 1150 struct usb_serial *serial;
1188 struct moschip_port *mos7840_port; 1151 struct moschip_port *mos7840_port;
@@ -1190,16 +1153,16 @@ static void mos7840_close(struct tty_struct *tty,
1190 int j; 1153 int j;
1191 __u16 Data; 1154 __u16 Data;
1192 1155
1193 dbg("%s\n", "mos7840_close:entering..."); 1156 dbg("%s", "mos7840_close:entering...");
1194 1157
1195 if (mos7840_port_paranoia_check(port, __func__)) { 1158 if (mos7840_port_paranoia_check(port, __func__)) {
1196 dbg("%s", "Port Paranoia failed \n"); 1159 dbg("%s", "Port Paranoia failed");
1197 return; 1160 return;
1198 } 1161 }
1199 1162
1200 serial = mos7840_get_usb_serial(port, __func__); 1163 serial = mos7840_get_usb_serial(port, __func__);
1201 if (!serial) { 1164 if (!serial) {
1202 dbg("%s", "Serial Paranoia failed \n"); 1165 dbg("%s", "Serial Paranoia failed");
1203 return; 1166 return;
1204 } 1167 }
1205 1168
@@ -1223,35 +1186,31 @@ static void mos7840_close(struct tty_struct *tty,
1223 } 1186 }
1224 } 1187 }
1225 1188
1226 if (serial->dev)
1227 /* flush and block until tx is empty */
1228 mos7840_block_until_tx_empty(tty, mos7840_port);
1229
1230 /* While closing port, shutdown all bulk read, write * 1189 /* While closing port, shutdown all bulk read, write *
1231 * and interrupt read if they exists */ 1190 * and interrupt read if they exists */
1232 if (serial->dev) { 1191 if (serial->dev) {
1233 if (mos7840_port->write_urb) { 1192 if (mos7840_port->write_urb) {
1234 dbg("%s", "Shutdown bulk write\n"); 1193 dbg("%s", "Shutdown bulk write");
1235 usb_kill_urb(mos7840_port->write_urb); 1194 usb_kill_urb(mos7840_port->write_urb);
1236 } 1195 }
1237 if (mos7840_port->read_urb) { 1196 if (mos7840_port->read_urb) {
1238 dbg("%s", "Shutdown bulk read\n"); 1197 dbg("%s", "Shutdown bulk read");
1239 usb_kill_urb(mos7840_port->read_urb); 1198 usb_kill_urb(mos7840_port->read_urb);
1240 mos7840_port->read_urb_busy = false; 1199 mos7840_port->read_urb_busy = false;
1241 } 1200 }
1242 if ((&mos7840_port->control_urb)) { 1201 if ((&mos7840_port->control_urb)) {
1243 dbg("%s", "Shutdown control read\n"); 1202 dbg("%s", "Shutdown control read");
1244 /*/ usb_kill_urb (mos7840_port->control_urb); */ 1203 /*/ usb_kill_urb (mos7840_port->control_urb); */
1245 } 1204 }
1246 } 1205 }
1247/* if(mos7840_port->ctrl_buf != NULL) */ 1206/* if(mos7840_port->ctrl_buf != NULL) */
1248/* kfree(mos7840_port->ctrl_buf); */ 1207/* kfree(mos7840_port->ctrl_buf); */
1249 port0->open_ports--; 1208 port0->open_ports--;
1250 dbg("mos7840_num_open_ports in close%d:in port%d\n", 1209 dbg("mos7840_num_open_ports in close%d:in port%d",
1251 port0->open_ports, port->number); 1210 port0->open_ports, port->number);
1252 if (port0->open_ports == 0) { 1211 if (port0->open_ports == 0) {
1253 if (serial->port[0]->interrupt_in_urb) { 1212 if (serial->port[0]->interrupt_in_urb) {
1254 dbg("%s", "Shutdown interrupt_in_urb\n"); 1213 dbg("%s", "Shutdown interrupt_in_urb");
1255 usb_kill_urb(serial->port[0]->interrupt_in_urb); 1214 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1256 } 1215 }
1257 } 1216 }
@@ -1271,7 +1230,7 @@ static void mos7840_close(struct tty_struct *tty,
1271 1230
1272 mos7840_port->open = 0; 1231 mos7840_port->open = 0;
1273 1232
1274 dbg("%s \n", "Leaving ............"); 1233 dbg("%s", "Leaving ............");
1275} 1234}
1276 1235
1277/************************************************************************ 1236/************************************************************************
@@ -1326,17 +1285,17 @@ static void mos7840_break(struct tty_struct *tty, int break_state)
1326 struct usb_serial *serial; 1285 struct usb_serial *serial;
1327 struct moschip_port *mos7840_port; 1286 struct moschip_port *mos7840_port;
1328 1287
1329 dbg("%s \n", "Entering ..........."); 1288 dbg("%s", "Entering ...........");
1330 dbg("mos7840_break: Start\n"); 1289 dbg("mos7840_break: Start");
1331 1290
1332 if (mos7840_port_paranoia_check(port, __func__)) { 1291 if (mos7840_port_paranoia_check(port, __func__)) {
1333 dbg("%s", "Port Paranoia failed \n"); 1292 dbg("%s", "Port Paranoia failed");
1334 return; 1293 return;
1335 } 1294 }
1336 1295
1337 serial = mos7840_get_usb_serial(port, __func__); 1296 serial = mos7840_get_usb_serial(port, __func__);
1338 if (!serial) { 1297 if (!serial) {
1339 dbg("%s", "Serial Paranoia failed \n"); 1298 dbg("%s", "Serial Paranoia failed");
1340 return; 1299 return;
1341 } 1300 }
1342 1301
@@ -1356,7 +1315,7 @@ static void mos7840_break(struct tty_struct *tty, int break_state)
1356 1315
1357 /* FIXME: no locking on shadowLCR anywhere in driver */ 1316 /* FIXME: no locking on shadowLCR anywhere in driver */
1358 mos7840_port->shadowLCR = data; 1317 mos7840_port->shadowLCR = data;
1359 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n", 1318 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1360 mos7840_port->shadowLCR); 1319 mos7840_port->shadowLCR);
1361 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, 1320 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1362 mos7840_port->shadowLCR); 1321 mos7840_port->shadowLCR);
@@ -1380,17 +1339,17 @@ static int mos7840_write_room(struct tty_struct *tty)
1380 unsigned long flags; 1339 unsigned long flags;
1381 struct moschip_port *mos7840_port; 1340 struct moschip_port *mos7840_port;
1382 1341
1383 dbg("%s \n", " mos7840_write_room:entering ..........."); 1342 dbg("%s", " mos7840_write_room:entering ...........");
1384 1343
1385 if (mos7840_port_paranoia_check(port, __func__)) { 1344 if (mos7840_port_paranoia_check(port, __func__)) {
1386 dbg("%s", "Invalid port \n"); 1345 dbg("%s", "Invalid port");
1387 dbg("%s \n", " mos7840_write_room:leaving ..........."); 1346 dbg("%s", " mos7840_write_room:leaving ...........");
1388 return -1; 1347 return -1;
1389 } 1348 }
1390 1349
1391 mos7840_port = mos7840_get_port_private(port); 1350 mos7840_port = mos7840_get_port_private(port);
1392 if (mos7840_port == NULL) { 1351 if (mos7840_port == NULL) {
1393 dbg("%s \n", "mos7840_break:leaving ..........."); 1352 dbg("%s", "mos7840_break:leaving ...........");
1394 return -1; 1353 return -1;
1395 } 1354 }
1396 1355
@@ -1430,16 +1389,16 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1430 /* __u16 Data; */ 1389 /* __u16 Data; */
1431 const unsigned char *current_position = data; 1390 const unsigned char *current_position = data;
1432 unsigned char *data1; 1391 unsigned char *data1;
1433 dbg("%s \n", "entering ..........."); 1392 dbg("%s", "entering ...........");
1434 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x\n", 1393 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1435 mos7840_port->shadowLCR); */ 1394 mos7840_port->shadowLCR); */
1436 1395
1437#ifdef NOTMOS7840 1396#ifdef NOTMOS7840
1438 Data = 0x00; 1397 Data = 0x00;
1439 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1398 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1440 mos7840_port->shadowLCR = Data; 1399 mos7840_port->shadowLCR = Data;
1441 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data); 1400 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1442 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n", 1401 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1443 mos7840_port->shadowLCR); 1402 mos7840_port->shadowLCR);
1444 1403
1445 /* Data = 0x03; */ 1404 /* Data = 0x03; */
@@ -1453,32 +1412,32 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1453 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ 1412 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1454 Data = 0x00; 1413 Data = 0x00;
1455 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); 1414 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1456 dbg("mos7840_write:DLL value is %x\n", Data); 1415 dbg("mos7840_write:DLL value is %x", Data);
1457 1416
1458 Data = 0x0; 1417 Data = 0x0;
1459 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); 1418 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1460 dbg("mos7840_write:DLM value is %x\n", Data); 1419 dbg("mos7840_write:DLM value is %x", Data);
1461 1420
1462 Data = Data & ~SERIAL_LCR_DLAB; 1421 Data = Data & ~SERIAL_LCR_DLAB;
1463 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n", 1422 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1464 mos7840_port->shadowLCR); 1423 mos7840_port->shadowLCR);
1465 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1424 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1466#endif 1425#endif
1467 1426
1468 if (mos7840_port_paranoia_check(port, __func__)) { 1427 if (mos7840_port_paranoia_check(port, __func__)) {
1469 dbg("%s", "Port Paranoia failed \n"); 1428 dbg("%s", "Port Paranoia failed");
1470 return -1; 1429 return -1;
1471 } 1430 }
1472 1431
1473 serial = port->serial; 1432 serial = port->serial;
1474 if (mos7840_serial_paranoia_check(serial, __func__)) { 1433 if (mos7840_serial_paranoia_check(serial, __func__)) {
1475 dbg("%s", "Serial Paranoia failed \n"); 1434 dbg("%s", "Serial Paranoia failed");
1476 return -1; 1435 return -1;
1477 } 1436 }
1478 1437
1479 mos7840_port = mos7840_get_port_private(port); 1438 mos7840_port = mos7840_get_port_private(port);
1480 if (mos7840_port == NULL) { 1439 if (mos7840_port == NULL) {
1481 dbg("%s", "mos7840_port is NULL\n"); 1440 dbg("%s", "mos7840_port is NULL");
1482 return -1; 1441 return -1;
1483 } 1442 }
1484 1443
@@ -1490,7 +1449,7 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1490 if (!mos7840_port->busy[i]) { 1449 if (!mos7840_port->busy[i]) {
1491 mos7840_port->busy[i] = 1; 1450 mos7840_port->busy[i] = 1;
1492 urb = mos7840_port->write_urb_pool[i]; 1451 urb = mos7840_port->write_urb_pool[i];
1493 dbg("\nURB:%d", i); 1452 dbg("URB:%d", i);
1494 break; 1453 break;
1495 } 1454 }
1496 } 1455 }
@@ -1525,7 +1484,7 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1525 mos7840_bulk_out_data_callback, mos7840_port); 1484 mos7840_bulk_out_data_callback, mos7840_port);
1526 1485
1527 data1 = urb->transfer_buffer; 1486 data1 = urb->transfer_buffer;
1528 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress); 1487 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
1529 1488
1530 /* send it down the pipe */ 1489 /* send it down the pipe */
1531 status = usb_submit_urb(urb, GFP_ATOMIC); 1490 status = usb_submit_urb(urb, GFP_ATOMIC);
@@ -1540,7 +1499,7 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1540 bytes_sent = transfer_size; 1499 bytes_sent = transfer_size;
1541 mos7840_port->icount.tx += transfer_size; 1500 mos7840_port->icount.tx += transfer_size;
1542 smp_wmb(); 1501 smp_wmb();
1543 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx); 1502 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
1544exit: 1503exit:
1545 return bytes_sent; 1504 return bytes_sent;
1546 1505
@@ -1559,11 +1518,11 @@ static void mos7840_throttle(struct tty_struct *tty)
1559 int status; 1518 int status;
1560 1519
1561 if (mos7840_port_paranoia_check(port, __func__)) { 1520 if (mos7840_port_paranoia_check(port, __func__)) {
1562 dbg("%s", "Invalid port \n"); 1521 dbg("%s", "Invalid port");
1563 return; 1522 return;
1564 } 1523 }
1565 1524
1566 dbg("- port %d\n", port->number); 1525 dbg("- port %d", port->number);
1567 1526
1568 mos7840_port = mos7840_get_port_private(port); 1527 mos7840_port = mos7840_get_port_private(port);
1569 1528
@@ -1571,11 +1530,11 @@ static void mos7840_throttle(struct tty_struct *tty)
1571 return; 1530 return;
1572 1531
1573 if (!mos7840_port->open) { 1532 if (!mos7840_port->open) {
1574 dbg("%s\n", "port not opened"); 1533 dbg("%s", "port not opened");
1575 return; 1534 return;
1576 } 1535 }
1577 1536
1578 dbg("%s", "Entering .......... \n"); 1537 dbg("%s", "Entering ..........");
1579 1538
1580 /* if we are implementing XON/XOFF, send the stop character */ 1539 /* if we are implementing XON/XOFF, send the stop character */
1581 if (I_IXOFF(tty)) { 1540 if (I_IXOFF(tty)) {
@@ -1609,7 +1568,7 @@ static void mos7840_unthrottle(struct tty_struct *tty)
1609 struct moschip_port *mos7840_port = mos7840_get_port_private(port); 1568 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1610 1569
1611 if (mos7840_port_paranoia_check(port, __func__)) { 1570 if (mos7840_port_paranoia_check(port, __func__)) {
1612 dbg("%s", "Invalid port \n"); 1571 dbg("%s", "Invalid port");
1613 return; 1572 return;
1614 } 1573 }
1615 1574
@@ -1621,7 +1580,7 @@ static void mos7840_unthrottle(struct tty_struct *tty)
1621 return; 1580 return;
1622 } 1581 }
1623 1582
1624 dbg("%s", "Entering .......... \n"); 1583 dbg("%s", "Entering ..........");
1625 1584
1626 /* if we are implementing XON/XOFF, send the start character */ 1585 /* if we are implementing XON/XOFF, send the start character */
1627 if (I_IXOFF(tty)) { 1586 if (I_IXOFF(tty)) {
@@ -1706,7 +1665,7 @@ static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
1706 1665
1707 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); 1666 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1708 if (status < 0) { 1667 if (status < 0) {
1709 dbg("setting MODEM_CONTROL_REGISTER Failed\n"); 1668 dbg("setting MODEM_CONTROL_REGISTER Failed");
1710 return status; 1669 return status;
1711 } 1670 }
1712 1671
@@ -1775,11 +1734,11 @@ static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1775 custom++; 1734 custom++;
1776 *divisor = custom; 1735 *divisor = custom;
1777 1736
1778 dbg(" Baud %d = %d\n", baudrate, custom); 1737 dbg(" Baud %d = %d", baudrate, custom);
1779 return 0; 1738 return 0;
1780 } 1739 }
1781 1740
1782 dbg("%s\n", " Baud calculation Failed..."); 1741 dbg("%s", " Baud calculation Failed...");
1783 return -1; 1742 return -1;
1784#endif 1743#endif
1785} 1744}
@@ -1805,16 +1764,16 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1805 1764
1806 port = (struct usb_serial_port *)mos7840_port->port; 1765 port = (struct usb_serial_port *)mos7840_port->port;
1807 if (mos7840_port_paranoia_check(port, __func__)) { 1766 if (mos7840_port_paranoia_check(port, __func__)) {
1808 dbg("%s", "Invalid port \n"); 1767 dbg("%s", "Invalid port");
1809 return -1; 1768 return -1;
1810 } 1769 }
1811 1770
1812 if (mos7840_serial_paranoia_check(port->serial, __func__)) { 1771 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1813 dbg("%s", "Invalid Serial \n"); 1772 dbg("%s", "Invalid Serial");
1814 return -1; 1773 return -1;
1815 } 1774 }
1816 1775
1817 dbg("%s", "Entering .......... \n"); 1776 dbg("%s", "Entering ..........");
1818 1777
1819 number = mos7840_port->port->number - mos7840_port->port->serial->minor; 1778 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1820 1779
@@ -1830,7 +1789,7 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1830 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1789 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1831 Data); 1790 Data);
1832 if (status < 0) { 1791 if (status < 0) {
1833 dbg("Writing spreg failed in set_serial_baud\n"); 1792 dbg("Writing spreg failed in set_serial_baud");
1834 return -1; 1793 return -1;
1835 } 1794 }
1836#endif 1795#endif
@@ -1843,7 +1802,7 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1843 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1802 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1844 Data); 1803 Data);
1845 if (status < 0) { 1804 if (status < 0) {
1846 dbg("Writing spreg failed in set_serial_baud\n"); 1805 dbg("Writing spreg failed in set_serial_baud");
1847 return -1; 1806 return -1;
1848 } 1807 }
1849#endif 1808#endif
@@ -1858,14 +1817,14 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1858 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, 1817 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1859 &Data); 1818 &Data);
1860 if (status < 0) { 1819 if (status < 0) {
1861 dbg("reading spreg failed in set_serial_baud\n"); 1820 dbg("reading spreg failed in set_serial_baud");
1862 return -1; 1821 return -1;
1863 } 1822 }
1864 Data = (Data & 0x8f) | clk_sel_val; 1823 Data = (Data & 0x8f) | clk_sel_val;
1865 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, 1824 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1866 Data); 1825 Data);
1867 if (status < 0) { 1826 if (status < 0) {
1868 dbg("Writing spreg failed in set_serial_baud\n"); 1827 dbg("Writing spreg failed in set_serial_baud");
1869 return -1; 1828 return -1;
1870 } 1829 }
1871 /* Calculate the Divisor */ 1830 /* Calculate the Divisor */
@@ -1881,11 +1840,11 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1881 1840
1882 /* Write the divisor */ 1841 /* Write the divisor */
1883 Data = (unsigned char)(divisor & 0xff); 1842 Data = (unsigned char)(divisor & 0xff);
1884 dbg("set_serial_baud Value to write DLL is %x\n", Data); 1843 dbg("set_serial_baud Value to write DLL is %x", Data);
1885 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 1844 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1886 1845
1887 Data = (unsigned char)((divisor & 0xff00) >> 8); 1846 Data = (unsigned char)((divisor & 0xff00) >> 8);
1888 dbg("set_serial_baud Value to write DLM is %x\n", Data); 1847 dbg("set_serial_baud Value to write DLM is %x", Data);
1889 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 1848 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1890 1849
1891 /* Disable access to divisor latch */ 1850 /* Disable access to divisor latch */
@@ -1923,12 +1882,12 @@ static void mos7840_change_port_settings(struct tty_struct *tty,
1923 port = (struct usb_serial_port *)mos7840_port->port; 1882 port = (struct usb_serial_port *)mos7840_port->port;
1924 1883
1925 if (mos7840_port_paranoia_check(port, __func__)) { 1884 if (mos7840_port_paranoia_check(port, __func__)) {
1926 dbg("%s", "Invalid port \n"); 1885 dbg("%s", "Invalid port");
1927 return; 1886 return;
1928 } 1887 }
1929 1888
1930 if (mos7840_serial_paranoia_check(port->serial, __func__)) { 1889 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1931 dbg("%s", "Invalid Serial \n"); 1890 dbg("%s", "Invalid Serial");
1932 return; 1891 return;
1933 } 1892 }
1934 1893
@@ -1941,7 +1900,7 @@ static void mos7840_change_port_settings(struct tty_struct *tty,
1941 return; 1900 return;
1942 } 1901 }
1943 1902
1944 dbg("%s", "Entering .......... \n"); 1903 dbg("%s", "Entering ..........");
1945 1904
1946 lData = LCR_BITS_8; 1905 lData = LCR_BITS_8;
1947 lStop = LCR_STOP_1; 1906 lStop = LCR_STOP_1;
@@ -2001,7 +1960,7 @@ static void mos7840_change_port_settings(struct tty_struct *tty,
2001 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 1960 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2002 mos7840_port->shadowLCR |= (lData | lParity | lStop); 1961 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2003 1962
2004 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n", 1963 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
2005 mos7840_port->shadowLCR); 1964 mos7840_port->shadowLCR);
2006 /* Disable Interrupts */ 1965 /* Disable Interrupts */
2007 Data = 0x00; 1966 Data = 0x00;
@@ -2043,7 +2002,7 @@ static void mos7840_change_port_settings(struct tty_struct *tty,
2043 2002
2044 if (!baud) { 2003 if (!baud) {
2045 /* pick a default, any default... */ 2004 /* pick a default, any default... */
2046 dbg("%s\n", "Picked default baud..."); 2005 dbg("%s", "Picked default baud...");
2047 baud = 9600; 2006 baud = 9600;
2048 } 2007 }
2049 2008
@@ -2066,7 +2025,7 @@ static void mos7840_change_port_settings(struct tty_struct *tty,
2066 } 2025 }
2067 wake_up(&mos7840_port->delta_msr_wait); 2026 wake_up(&mos7840_port->delta_msr_wait);
2068 mos7840_port->delta_msr_cond = 1; 2027 mos7840_port->delta_msr_cond = 1;
2069 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n", 2028 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2070 mos7840_port->shadowLCR); 2029 mos7840_port->shadowLCR);
2071 2030
2072 return; 2031 return;
@@ -2086,16 +2045,16 @@ static void mos7840_set_termios(struct tty_struct *tty,
2086 unsigned int cflag; 2045 unsigned int cflag;
2087 struct usb_serial *serial; 2046 struct usb_serial *serial;
2088 struct moschip_port *mos7840_port; 2047 struct moschip_port *mos7840_port;
2089 dbg("mos7840_set_termios: START\n"); 2048 dbg("mos7840_set_termios: START");
2090 if (mos7840_port_paranoia_check(port, __func__)) { 2049 if (mos7840_port_paranoia_check(port, __func__)) {
2091 dbg("%s", "Invalid port \n"); 2050 dbg("%s", "Invalid port");
2092 return; 2051 return;
2093 } 2052 }
2094 2053
2095 serial = port->serial; 2054 serial = port->serial;
2096 2055
2097 if (mos7840_serial_paranoia_check(serial, __func__)) { 2056 if (mos7840_serial_paranoia_check(serial, __func__)) {
2098 dbg("%s", "Invalid Serial \n"); 2057 dbg("%s", "Invalid Serial");
2099 return; 2058 return;
2100 } 2059 }
2101 2060
@@ -2109,7 +2068,7 @@ static void mos7840_set_termios(struct tty_struct *tty,
2109 return; 2068 return;
2110 } 2069 }
2111 2070
2112 dbg("%s\n", "setting termios - "); 2071 dbg("%s", "setting termios - ");
2113 2072
2114 cflag = tty->termios->c_cflag; 2073 cflag = tty->termios->c_cflag;
2115 2074
@@ -2124,7 +2083,7 @@ static void mos7840_set_termios(struct tty_struct *tty,
2124 mos7840_change_port_settings(tty, mos7840_port, old_termios); 2083 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2125 2084
2126 if (!mos7840_port->read_urb) { 2085 if (!mos7840_port->read_urb) {
2127 dbg("%s", "URB KILLED !!!!!\n"); 2086 dbg("%s", "URB KILLED !!!!!");
2128 return; 2087 return;
2129 } 2088 }
2130 2089
@@ -2190,7 +2149,7 @@ static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2190 2149
2191 port = (struct usb_serial_port *)mos7840_port->port; 2150 port = (struct usb_serial_port *)mos7840_port->port;
2192 if (mos7840_port_paranoia_check(port, __func__)) { 2151 if (mos7840_port_paranoia_check(port, __func__)) {
2193 dbg("%s", "Invalid port \n"); 2152 dbg("%s", "Invalid port");
2194 return -1; 2153 return -1;
2195 } 2154 }
2196 2155
@@ -2235,7 +2194,7 @@ static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2235 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 2194 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2236 unlock_kernel(); 2195 unlock_kernel();
2237 if (status < 0) { 2196 if (status < 0) {
2238 dbg("setting MODEM_CONTROL_REGISTER Failed\n"); 2197 dbg("setting MODEM_CONTROL_REGISTER Failed");
2239 return -1; 2198 return -1;
2240 } 2199 }
2241 2200
@@ -2320,7 +2279,7 @@ static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
2320 int mosret = 0; 2279 int mosret = 0;
2321 2280
2322 if (mos7840_port_paranoia_check(port, __func__)) { 2281 if (mos7840_port_paranoia_check(port, __func__)) {
2323 dbg("%s", "Invalid port \n"); 2282 dbg("%s", "Invalid port");
2324 return -1; 2283 return -1;
2325 } 2284 }
2326 2285
@@ -2420,9 +2379,8 @@ static int mos7840_calc_num_ports(struct usb_serial *serial)
2420{ 2379{
2421 int mos7840_num_ports = 0; 2380 int mos7840_num_ports = 0;
2422 2381
2423 dbg("numberofendpoints: %d \n", 2382 dbg("numberofendpoints: cur %d, alt %d",
2424 (int)serial->interface->cur_altsetting->desc.bNumEndpoints); 2383 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
2425 dbg("numberofendpoints: %d \n",
2426 (int)serial->interface->altsetting->desc.bNumEndpoints); 2384 (int)serial->interface->altsetting->desc.bNumEndpoints);
2427 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) { 2385 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2428 mos7840_num_ports = serial->num_ports = 2; 2386 mos7840_num_ports = serial->num_ports = 2;
@@ -2431,7 +2389,7 @@ static int mos7840_calc_num_ports(struct usb_serial *serial)
2431 serial->num_bulk_out = 4; 2389 serial->num_bulk_out = 4;
2432 mos7840_num_ports = serial->num_ports = 4; 2390 mos7840_num_ports = serial->num_ports = 4;
2433 } 2391 }
2434 2392 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
2435 return mos7840_num_ports; 2393 return mos7840_num_ports;
2436} 2394}
2437 2395
@@ -2446,22 +2404,24 @@ static int mos7840_startup(struct usb_serial *serial)
2446 int i, status; 2404 int i, status;
2447 2405
2448 __u16 Data; 2406 __u16 Data;
2449 dbg("%s \n", " mos7840_startup :entering.........."); 2407 dbg("%s", "mos7840_startup :Entering..........");
2450 2408
2451 if (!serial) { 2409 if (!serial) {
2452 dbg("%s\n", "Invalid Handler"); 2410 dbg("%s", "Invalid Handler");
2453 return -1; 2411 return -1;
2454 } 2412 }
2455 2413
2456 dev = serial->dev; 2414 dev = serial->dev;
2457 2415
2458 dbg("%s\n", "Entering..."); 2416 dbg("%s", "Entering...");
2417 dbg ("mos7840_startup: serial = %p", serial);
2459 2418
2460 /* we set up the pointers to the endpoints in the mos7840_open * 2419 /* we set up the pointers to the endpoints in the mos7840_open *
2461 * function, as the structures aren't created yet. */ 2420 * function, as the structures aren't created yet. */
2462 2421
2463 /* set up port private structures */ 2422 /* set up port private structures */
2464 for (i = 0; i < serial->num_ports; ++i) { 2423 for (i = 0; i < serial->num_ports; ++i) {
2424 dbg ("mos7840_startup: configuring port %d............", i);
2465 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 2425 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2466 if (mos7840_port == NULL) { 2426 if (mos7840_port == NULL) {
2467 dev_err(&dev->dev, "%s - Out of memory\n", __func__); 2427 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
@@ -2519,10 +2479,10 @@ static int mos7840_startup(struct usb_serial *serial)
2519 status = mos7840_get_reg_sync(serial->port[i], 2479 status = mos7840_get_reg_sync(serial->port[i],
2520 mos7840_port->ControlRegOffset, &Data); 2480 mos7840_port->ControlRegOffset, &Data);
2521 if (status < 0) { 2481 if (status < 0) {
2522 dbg("Reading ControlReg failed status-0x%x\n", status); 2482 dbg("Reading ControlReg failed status-0x%x", status);
2523 break; 2483 break;
2524 } else 2484 } else
2525 dbg("ControlReg Reading success val is %x, status%d\n", 2485 dbg("ControlReg Reading success val is %x, status%d",
2526 Data, status); 2486 Data, status);
2527 Data |= 0x08; /* setting driver done bit */ 2487 Data |= 0x08; /* setting driver done bit */
2528 Data |= 0x04; /* sp1_bit to have cts change reflect in 2488 Data |= 0x04; /* sp1_bit to have cts change reflect in
@@ -2532,10 +2492,10 @@ static int mos7840_startup(struct usb_serial *serial)
2532 status = mos7840_set_reg_sync(serial->port[i], 2492 status = mos7840_set_reg_sync(serial->port[i],
2533 mos7840_port->ControlRegOffset, Data); 2493 mos7840_port->ControlRegOffset, Data);
2534 if (status < 0) { 2494 if (status < 0) {
2535 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status); 2495 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
2536 break; 2496 break;
2537 } else 2497 } else
2538 dbg("ControlReg Writing success(rx_disable) status%d\n", 2498 dbg("ControlReg Writing success(rx_disable) status%d",
2539 status); 2499 status);
2540 2500
2541 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 2501 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
@@ -2544,48 +2504,48 @@ static int mos7840_startup(struct usb_serial *serial)
2544 status = mos7840_set_reg_sync(serial->port[i], 2504 status = mos7840_set_reg_sync(serial->port[i],
2545 (__u16) (mos7840_port->DcrRegOffset + 0), Data); 2505 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2546 if (status < 0) { 2506 if (status < 0) {
2547 dbg("Writing DCR0 failed status-0x%x\n", status); 2507 dbg("Writing DCR0 failed status-0x%x", status);
2548 break; 2508 break;
2549 } else 2509 } else
2550 dbg("DCR0 Writing success status%d\n", status); 2510 dbg("DCR0 Writing success status%d", status);
2551 2511
2552 Data = 0x05; 2512 Data = 0x05;
2553 status = mos7840_set_reg_sync(serial->port[i], 2513 status = mos7840_set_reg_sync(serial->port[i],
2554 (__u16) (mos7840_port->DcrRegOffset + 1), Data); 2514 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2555 if (status < 0) { 2515 if (status < 0) {
2556 dbg("Writing DCR1 failed status-0x%x\n", status); 2516 dbg("Writing DCR1 failed status-0x%x", status);
2557 break; 2517 break;
2558 } else 2518 } else
2559 dbg("DCR1 Writing success status%d\n", status); 2519 dbg("DCR1 Writing success status%d", status);
2560 2520
2561 Data = 0x24; 2521 Data = 0x24;
2562 status = mos7840_set_reg_sync(serial->port[i], 2522 status = mos7840_set_reg_sync(serial->port[i],
2563 (__u16) (mos7840_port->DcrRegOffset + 2), Data); 2523 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2564 if (status < 0) { 2524 if (status < 0) {
2565 dbg("Writing DCR2 failed status-0x%x\n", status); 2525 dbg("Writing DCR2 failed status-0x%x", status);
2566 break; 2526 break;
2567 } else 2527 } else
2568 dbg("DCR2 Writing success status%d\n", status); 2528 dbg("DCR2 Writing success status%d", status);
2569 2529
2570 /* write values in clkstart0x0 and clkmulti 0x20 */ 2530 /* write values in clkstart0x0 and clkmulti 0x20 */
2571 Data = 0x0; 2531 Data = 0x0;
2572 status = mos7840_set_reg_sync(serial->port[i], 2532 status = mos7840_set_reg_sync(serial->port[i],
2573 CLK_START_VALUE_REGISTER, Data); 2533 CLK_START_VALUE_REGISTER, Data);
2574 if (status < 0) { 2534 if (status < 0) {
2575 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); 2535 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
2576 break; 2536 break;
2577 } else 2537 } else
2578 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status); 2538 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
2579 2539
2580 Data = 0x20; 2540 Data = 0x20;
2581 status = mos7840_set_reg_sync(serial->port[i], 2541 status = mos7840_set_reg_sync(serial->port[i],
2582 CLK_MULTI_REGISTER, Data); 2542 CLK_MULTI_REGISTER, Data);
2583 if (status < 0) { 2543 if (status < 0) {
2584 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n", 2544 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2585 status); 2545 status);
2586 goto error; 2546 goto error;
2587 } else 2547 } else
2588 dbg("CLK_MULTI_REGISTER Writing success status%d\n", 2548 dbg("CLK_MULTI_REGISTER Writing success status%d",
2589 status); 2549 status);
2590 2550
2591 /* write value 0x0 to scratchpad register */ 2551 /* write value 0x0 to scratchpad register */
@@ -2593,11 +2553,11 @@ static int mos7840_startup(struct usb_serial *serial)
2593 status = mos7840_set_uart_reg(serial->port[i], 2553 status = mos7840_set_uart_reg(serial->port[i],
2594 SCRATCH_PAD_REGISTER, Data); 2554 SCRATCH_PAD_REGISTER, Data);
2595 if (status < 0) { 2555 if (status < 0) {
2596 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", 2556 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2597 status); 2557 status);
2598 break; 2558 break;
2599 } else 2559 } else
2600 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n", 2560 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2601 status); 2561 status);
2602 2562
2603 /* Zero Length flag register */ 2563 /* Zero Length flag register */
@@ -2608,30 +2568,30 @@ static int mos7840_startup(struct usb_serial *serial)
2608 status = mos7840_set_reg_sync(serial->port[i], 2568 status = mos7840_set_reg_sync(serial->port[i],
2609 (__u16) (ZLP_REG1 + 2569 (__u16) (ZLP_REG1 +
2610 ((__u16)mos7840_port->port_num)), Data); 2570 ((__u16)mos7840_port->port_num)), Data);
2611 dbg("ZLIP offset%x\n", 2571 dbg("ZLIP offset %x",
2612 (__u16) (ZLP_REG1 + 2572 (__u16) (ZLP_REG1 +
2613 ((__u16) mos7840_port->port_num))); 2573 ((__u16) mos7840_port->port_num)));
2614 if (status < 0) { 2574 if (status < 0) {
2615 dbg("Writing ZLP_REG%d failed status-0x%x\n", 2575 dbg("Writing ZLP_REG%d failed status-0x%x",
2616 i + 2, status); 2576 i + 2, status);
2617 break; 2577 break;
2618 } else 2578 } else
2619 dbg("ZLP_REG%d Writing success status%d\n", 2579 dbg("ZLP_REG%d Writing success status%d",
2620 i + 2, status); 2580 i + 2, status);
2621 } else { 2581 } else {
2622 Data = 0xff; 2582 Data = 0xff;
2623 status = mos7840_set_reg_sync(serial->port[i], 2583 status = mos7840_set_reg_sync(serial->port[i],
2624 (__u16) (ZLP_REG1 + 2584 (__u16) (ZLP_REG1 +
2625 ((__u16)mos7840_port->port_num) - 0x1), Data); 2585 ((__u16)mos7840_port->port_num) - 0x1), Data);
2626 dbg("ZLIP offset%x\n", 2586 dbg("ZLIP offset %x",
2627 (__u16) (ZLP_REG1 + 2587 (__u16) (ZLP_REG1 +
2628 ((__u16) mos7840_port->port_num) - 0x1)); 2588 ((__u16) mos7840_port->port_num) - 0x1));
2629 if (status < 0) { 2589 if (status < 0) {
2630 dbg("Writing ZLP_REG%d failed status-0x%x\n", 2590 dbg("Writing ZLP_REG%d failed status-0x%x",
2631 i + 1, status); 2591 i + 1, status);
2632 break; 2592 break;
2633 } else 2593 } else
2634 dbg("ZLP_REG%d Writing success status%d\n", 2594 dbg("ZLP_REG%d Writing success status%d",
2635 i + 1, status); 2595 i + 1, status);
2636 2596
2637 } 2597 }
@@ -2645,15 +2605,16 @@ static int mos7840_startup(struct usb_serial *serial)
2645 goto error; 2605 goto error;
2646 } 2606 }
2647 } 2607 }
2608 dbg ("mos7840_startup: all ports configured...........");
2648 2609
2649 /* Zero Length flag enable */ 2610 /* Zero Length flag enable */
2650 Data = 0x0f; 2611 Data = 0x0f;
2651 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 2612 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2652 if (status < 0) { 2613 if (status < 0) {
2653 dbg("Writing ZLP_REG5 failed status-0x%x\n", status); 2614 dbg("Writing ZLP_REG5 failed status-0x%x", status);
2654 goto error; 2615 goto error;
2655 } else 2616 } else
2656 dbg("ZLP_REG5 Writing success status%d\n", status); 2617 dbg("ZLP_REG5 Writing success status%d", status);
2657 2618
2658 /* setting configuration feature to one */ 2619 /* setting configuration feature to one */
2659 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2620 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
@@ -2673,19 +2634,19 @@ error:
2673} 2634}
2674 2635
2675/**************************************************************************** 2636/****************************************************************************
2676 * mos7840_shutdown 2637 * mos7840_disconnect
2677 * This function is called whenever the device is removed from the usb bus. 2638 * This function is called whenever the device is removed from the usb bus.
2678 ****************************************************************************/ 2639 ****************************************************************************/
2679 2640
2680static void mos7840_shutdown(struct usb_serial *serial) 2641static void mos7840_disconnect(struct usb_serial *serial)
2681{ 2642{
2682 int i; 2643 int i;
2683 unsigned long flags; 2644 unsigned long flags;
2684 struct moschip_port *mos7840_port; 2645 struct moschip_port *mos7840_port;
2685 dbg("%s \n", " shutdown :entering.........."); 2646 dbg("%s", " disconnect :entering..........");
2686 2647
2687 if (!serial) { 2648 if (!serial) {
2688 dbg("%s", "Invalid Handler \n"); 2649 dbg("%s", "Invalid Handler");
2689 return; 2650 return;
2690 } 2651 }
2691 2652
@@ -2702,14 +2663,45 @@ static void mos7840_shutdown(struct usb_serial *serial)
2702 mos7840_port->zombie = 1; 2663 mos7840_port->zombie = 1;
2703 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 2664 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2704 usb_kill_urb(mos7840_port->control_urb); 2665 usb_kill_urb(mos7840_port->control_urb);
2666 }
2667 }
2668
2669 dbg("%s", "Thank u :: ");
2670
2671}
2672
2673/****************************************************************************
2674 * mos7840_release
2675 * This function is called when the usb_serial structure is freed.
2676 ****************************************************************************/
2677
2678static void mos7840_release(struct usb_serial *serial)
2679{
2680 int i;
2681 struct moschip_port *mos7840_port;
2682 dbg("%s", " release :entering..........");
2683
2684 if (!serial) {
2685 dbg("%s", "Invalid Handler");
2686 return;
2687 }
2688
2689 /* check for the ports to be closed,close the ports and disconnect */
2690
2691 /* free private structure allocated for serial port *
2692 * stop reads and writes on all ports */
2693
2694 for (i = 0; i < serial->num_ports; ++i) {
2695 mos7840_port = mos7840_get_port_private(serial->port[i]);
2696 dbg("mos7840_port %d = %p", i, mos7840_port);
2697 if (mos7840_port) {
2705 kfree(mos7840_port->ctrl_buf); 2698 kfree(mos7840_port->ctrl_buf);
2706 kfree(mos7840_port->dr); 2699 kfree(mos7840_port->dr);
2707 kfree(mos7840_port); 2700 kfree(mos7840_port);
2708 } 2701 }
2709 mos7840_set_port_private(serial->port[i], NULL);
2710 } 2702 }
2711 2703
2712 dbg("%s\n", "Thank u :: "); 2704 dbg("%s", "Thank u :: ");
2713 2705
2714} 2706}
2715 2707
@@ -2747,7 +2739,8 @@ static struct usb_serial_driver moschip7840_4port_device = {
2747 .tiocmget = mos7840_tiocmget, 2739 .tiocmget = mos7840_tiocmget,
2748 .tiocmset = mos7840_tiocmset, 2740 .tiocmset = mos7840_tiocmset,
2749 .attach = mos7840_startup, 2741 .attach = mos7840_startup,
2750 .shutdown = mos7840_shutdown, 2742 .disconnect = mos7840_disconnect,
2743 .release = mos7840_release,
2751 .read_bulk_callback = mos7840_bulk_in_callback, 2744 .read_bulk_callback = mos7840_bulk_in_callback,
2752 .read_int_callback = mos7840_interrupt_callback, 2745 .read_int_callback = mos7840_interrupt_callback,
2753}; 2746};
@@ -2760,7 +2753,7 @@ static int __init moschip7840_init(void)
2760{ 2753{
2761 int retval; 2754 int retval;
2762 2755
2763 dbg("%s \n", " mos7840_init :entering.........."); 2756 dbg("%s", " mos7840_init :entering..........");
2764 2757
2765 /* Register with the usb serial */ 2758 /* Register with the usb serial */
2766 retval = usb_serial_register(&moschip7840_4port_device); 2759 retval = usb_serial_register(&moschip7840_4port_device);
@@ -2768,14 +2761,14 @@ static int __init moschip7840_init(void)
2768 if (retval) 2761 if (retval)
2769 goto failed_port_device_register; 2762 goto failed_port_device_register;
2770 2763
2771 dbg("%s\n", "Entring..."); 2764 dbg("%s", "Entering...");
2772 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 2765 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2773 DRIVER_DESC "\n"); 2766 DRIVER_DESC "\n");
2774 2767
2775 /* Register with the usb */ 2768 /* Register with the usb */
2776 retval = usb_register(&io_driver); 2769 retval = usb_register(&io_driver);
2777 if (retval == 0) { 2770 if (retval == 0) {
2778 dbg("%s\n", "Leaving..."); 2771 dbg("%s", "Leaving...");
2779 return 0; 2772 return 0;
2780 } 2773 }
2781 usb_serial_deregister(&moschip7840_4port_device); 2774 usb_serial_deregister(&moschip7840_4port_device);
@@ -2790,13 +2783,13 @@ failed_port_device_register:
2790static void __exit moschip7840_exit(void) 2783static void __exit moschip7840_exit(void)
2791{ 2784{
2792 2785
2793 dbg("%s \n", " mos7840_exit :entering.........."); 2786 dbg("%s", " mos7840_exit :entering..........");
2794 2787
2795 usb_deregister(&io_driver); 2788 usb_deregister(&io_driver);
2796 2789
2797 usb_serial_deregister(&moschip7840_4port_device); 2790 usb_serial_deregister(&moschip7840_4port_device);
2798 2791
2799 dbg("%s\n", "Entring..."); 2792 dbg("%s", "Entering...");
2800} 2793}
2801 2794
2802module_init(moschip7840_init); 2795module_init(moschip7840_init);
diff --git a/drivers/usb/serial/navman.c b/drivers/usb/serial/navman.c
index bcdcbb822705..f5f3751a888c 100644
--- a/drivers/usb/serial/navman.c
+++ b/drivers/usb/serial/navman.c
@@ -98,8 +98,7 @@ static int navman_open(struct tty_struct *tty,
98 return result; 98 return result;
99} 99}
100 100
101static void navman_close(struct tty_struct *tty, 101static void navman_close(struct usb_serial_port *port)
102 struct usb_serial_port *port, struct file *filp)
103{ 102{
104 dbg("%s - port %d", __func__, port->number); 103 dbg("%s - port %d", __func__, port->number);
105 104
diff --git a/drivers/usb/serial/omninet.c b/drivers/usb/serial/omninet.c
index df6539712726..56857ddbd70b 100644
--- a/drivers/usb/serial/omninet.c
+++ b/drivers/usb/serial/omninet.c
@@ -66,14 +66,14 @@ static int debug;
66/* function prototypes */ 66/* function prototypes */
67static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port, 67static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port,
68 struct file *filp); 68 struct file *filp);
69static void omninet_close(struct tty_struct *tty, struct usb_serial_port *port, 69static void omninet_close(struct usb_serial_port *port);
70 struct file *filp);
71static void omninet_read_bulk_callback(struct urb *urb); 70static void omninet_read_bulk_callback(struct urb *urb);
72static void omninet_write_bulk_callback(struct urb *urb); 71static void omninet_write_bulk_callback(struct urb *urb);
73static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port, 72static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
74 const unsigned char *buf, int count); 73 const unsigned char *buf, int count);
75static int omninet_write_room(struct tty_struct *tty); 74static int omninet_write_room(struct tty_struct *tty);
76static void omninet_shutdown(struct usb_serial *serial); 75static void omninet_disconnect(struct usb_serial *serial);
76static void omninet_release(struct usb_serial *serial);
77static int omninet_attach(struct usb_serial *serial); 77static int omninet_attach(struct usb_serial *serial);
78 78
79static struct usb_device_id id_table[] = { 79static struct usb_device_id id_table[] = {
@@ -109,7 +109,8 @@ static struct usb_serial_driver zyxel_omninet_device = {
109 .write_room = omninet_write_room, 109 .write_room = omninet_write_room,
110 .read_bulk_callback = omninet_read_bulk_callback, 110 .read_bulk_callback = omninet_read_bulk_callback,
111 .write_bulk_callback = omninet_write_bulk_callback, 111 .write_bulk_callback = omninet_write_bulk_callback,
112 .shutdown = omninet_shutdown, 112 .disconnect = omninet_disconnect,
113 .release = omninet_release,
113}; 114};
114 115
115 116
@@ -189,8 +190,7 @@ static int omninet_open(struct tty_struct *tty,
189 return result; 190 return result;
190} 191}
191 192
192static void omninet_close(struct tty_struct *tty, 193static void omninet_close(struct usb_serial_port *port)
193 struct usb_serial_port *port, struct file *filp)
194{ 194{
195 dbg("%s - port %d", __func__, port->number); 195 dbg("%s - port %d", __func__, port->number);
196 usb_kill_urb(port->read_urb); 196 usb_kill_urb(port->read_urb);
@@ -347,13 +347,22 @@ static void omninet_write_bulk_callback(struct urb *urb)
347} 347}
348 348
349 349
350static void omninet_shutdown(struct usb_serial *serial) 350static void omninet_disconnect(struct usb_serial *serial)
351{ 351{
352 struct usb_serial_port *wport = serial->port[1]; 352 struct usb_serial_port *wport = serial->port[1];
353 struct usb_serial_port *port = serial->port[0]; 353
354 dbg("%s", __func__); 354 dbg("%s", __func__);
355 355
356 usb_kill_urb(wport->write_urb); 356 usb_kill_urb(wport->write_urb);
357}
358
359
360static void omninet_release(struct usb_serial *serial)
361{
362 struct usb_serial_port *port = serial->port[0];
363
364 dbg("%s", __func__);
365
357 kfree(usb_get_serial_port_data(port)); 366 kfree(usb_get_serial_port_data(port));
358} 367}
359 368
diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c
index b500ad10b758..336bba79ad32 100644
--- a/drivers/usb/serial/opticon.c
+++ b/drivers/usb/serial/opticon.c
@@ -173,8 +173,7 @@ static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port,
173 return result; 173 return result;
174} 174}
175 175
176static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port, 176static void opticon_close(struct usb_serial_port *port)
177 struct file *filp)
178{ 177{
179 struct opticon_private *priv = usb_get_serial_data(port->serial); 178 struct opticon_private *priv = usb_get_serial_data(port->serial);
180 179
@@ -464,7 +463,7 @@ error:
464 return retval; 463 return retval;
465} 464}
466 465
467static void opticon_shutdown(struct usb_serial *serial) 466static void opticon_disconnect(struct usb_serial *serial)
468{ 467{
469 struct opticon_private *priv = usb_get_serial_data(serial); 468 struct opticon_private *priv = usb_get_serial_data(serial);
470 469
@@ -472,9 +471,16 @@ static void opticon_shutdown(struct usb_serial *serial)
472 471
473 usb_kill_urb(priv->bulk_read_urb); 472 usb_kill_urb(priv->bulk_read_urb);
474 usb_free_urb(priv->bulk_read_urb); 473 usb_free_urb(priv->bulk_read_urb);
474}
475
476static void opticon_release(struct usb_serial *serial)
477{
478 struct opticon_private *priv = usb_get_serial_data(serial);
479
480 dbg("%s", __func__);
481
475 kfree(priv->bulk_in_buffer); 482 kfree(priv->bulk_in_buffer);
476 kfree(priv); 483 kfree(priv);
477 usb_set_serial_data(serial, NULL);
478} 484}
479 485
480static int opticon_suspend(struct usb_interface *intf, pm_message_t message) 486static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
@@ -525,7 +531,8 @@ static struct usb_serial_driver opticon_device = {
525 .close = opticon_close, 531 .close = opticon_close,
526 .write = opticon_write, 532 .write = opticon_write,
527 .write_room = opticon_write_room, 533 .write_room = opticon_write_room,
528 .shutdown = opticon_shutdown, 534 .disconnect = opticon_disconnect,
535 .release = opticon_release,
529 .throttle = opticon_throttle, 536 .throttle = opticon_throttle,
530 .unthrottle = opticon_unthrottle, 537 .unthrottle = opticon_unthrottle,
531 .ioctl = opticon_ioctl, 538 .ioctl = opticon_ioctl,
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index 7817b82889ca..98262dd552bb 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -43,12 +43,16 @@
43#include <linux/usb/serial.h> 43#include <linux/usb/serial.h>
44 44
45/* Function prototypes */ 45/* Function prototypes */
46static int option_probe(struct usb_serial *serial,
47 const struct usb_device_id *id);
46static int option_open(struct tty_struct *tty, struct usb_serial_port *port, 48static int option_open(struct tty_struct *tty, struct usb_serial_port *port,
47 struct file *filp); 49 struct file *filp);
48static void option_close(struct tty_struct *tty, struct usb_serial_port *port, 50static void option_close(struct usb_serial_port *port);
49 struct file *filp); 51static void option_dtr_rts(struct usb_serial_port *port, int on);
52
50static int option_startup(struct usb_serial *serial); 53static int option_startup(struct usb_serial *serial);
51static void option_shutdown(struct usb_serial *serial); 54static void option_disconnect(struct usb_serial *serial);
55static void option_release(struct usb_serial *serial);
52static int option_write_room(struct tty_struct *tty); 56static int option_write_room(struct tty_struct *tty);
53 57
54static void option_instat_callback(struct urb *urb); 58static void option_instat_callback(struct urb *urb);
@@ -61,7 +65,7 @@ static void option_set_termios(struct tty_struct *tty,
61static int option_tiocmget(struct tty_struct *tty, struct file *file); 65static int option_tiocmget(struct tty_struct *tty, struct file *file);
62static int option_tiocmset(struct tty_struct *tty, struct file *file, 66static int option_tiocmset(struct tty_struct *tty, struct file *file,
63 unsigned int set, unsigned int clear); 67 unsigned int set, unsigned int clear);
64static int option_send_setup(struct tty_struct *tty, struct usb_serial_port *port); 68static int option_send_setup(struct usb_serial_port *port);
65static int option_suspend(struct usb_serial *serial, pm_message_t message); 69static int option_suspend(struct usb_serial *serial, pm_message_t message);
66static int option_resume(struct usb_serial *serial); 70static int option_resume(struct usb_serial *serial);
67 71
@@ -201,9 +205,10 @@ static int option_resume(struct usb_serial *serial);
201#define NOVATELWIRELESS_PRODUCT_MC727 0x4100 205#define NOVATELWIRELESS_PRODUCT_MC727 0x4100
202#define NOVATELWIRELESS_PRODUCT_MC950D 0x4400 206#define NOVATELWIRELESS_PRODUCT_MC950D 0x4400
203#define NOVATELWIRELESS_PRODUCT_U727 0x5010 207#define NOVATELWIRELESS_PRODUCT_U727 0x5010
208#define NOVATELWIRELESS_PRODUCT_MC760 0x6000
209#define NOVATELWIRELESS_PRODUCT_OVMC760 0x6002
204 210
205/* FUTURE NOVATEL PRODUCTS */ 211/* FUTURE NOVATEL PRODUCTS */
206#define NOVATELWIRELESS_PRODUCT_EVDO_FULLSPEED 0X6000
207#define NOVATELWIRELESS_PRODUCT_EVDO_HIGHSPEED 0X6001 212#define NOVATELWIRELESS_PRODUCT_EVDO_HIGHSPEED 0X6001
208#define NOVATELWIRELESS_PRODUCT_HSPA_FULLSPEED 0X7000 213#define NOVATELWIRELESS_PRODUCT_HSPA_FULLSPEED 0X7000
209#define NOVATELWIRELESS_PRODUCT_HSPA_HIGHSPEED 0X7001 214#define NOVATELWIRELESS_PRODUCT_HSPA_HIGHSPEED 0X7001
@@ -303,6 +308,19 @@ static int option_resume(struct usb_serial *serial);
303#define DLINK_VENDOR_ID 0x1186 308#define DLINK_VENDOR_ID 0x1186
304#define DLINK_PRODUCT_DWM_652 0x3e04 309#define DLINK_PRODUCT_DWM_652 0x3e04
305 310
311#define QISDA_VENDOR_ID 0x1da5
312#define QISDA_PRODUCT_H21_4512 0x4512
313#define QISDA_PRODUCT_H21_4523 0x4523
314#define QISDA_PRODUCT_H20_4515 0x4515
315#define QISDA_PRODUCT_H20_4519 0x4519
316
317
318/* TOSHIBA PRODUCTS */
319#define TOSHIBA_VENDOR_ID 0x0930
320#define TOSHIBA_PRODUCT_HSDPA_MINICARD 0x1302
321
322#define ALINK_VENDOR_ID 0x1e0e
323#define ALINK_PRODUCT_3GU 0x9200
306 324
307static struct usb_device_id option_ids[] = { 325static struct usb_device_id option_ids[] = {
308 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COLT) }, 326 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COLT) },
@@ -421,7 +439,8 @@ static struct usb_device_id option_ids[] = {
421 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_MC950D) }, /* Novatel MC930D/MC950D */ 439 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_MC950D) }, /* Novatel MC930D/MC950D */
422 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_MC727) }, /* Novatel MC727/U727/USB727 */ 440 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_MC727) }, /* Novatel MC727/U727/USB727 */
423 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_U727) }, /* Novatel MC727/U727/USB727 */ 441 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_U727) }, /* Novatel MC727/U727/USB727 */
424 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_EVDO_FULLSPEED) }, /* Novatel EVDO product */ 442 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_MC760) }, /* Novatel MC760/U760/USB760 */
443 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_OVMC760) }, /* Novatel Ovation MC760 */
425 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_HSPA_FULLSPEED) }, /* Novatel HSPA product */ 444 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_HSPA_FULLSPEED) }, /* Novatel HSPA product */
426 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_EVDO_EMBEDDED_FULLSPEED) }, /* Novatel EVDO Embedded product */ 445 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_EVDO_EMBEDDED_FULLSPEED) }, /* Novatel EVDO Embedded product */
427 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_HSPA_EMBEDDED_FULLSPEED) }, /* Novatel HSPA Embedded product */ 446 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID, NOVATELWIRELESS_PRODUCT_HSPA_EMBEDDED_FULLSPEED) }, /* Novatel HSPA Embedded product */
@@ -521,7 +540,13 @@ static struct usb_device_id option_ids[] = {
521 { USB_DEVICE(ZTE_VENDOR_ID, ZTE_PRODUCT_CDMA_TECH) }, 540 { USB_DEVICE(ZTE_VENDOR_ID, ZTE_PRODUCT_CDMA_TECH) },
522 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_H10) }, 541 { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_H10) },
523 { USB_DEVICE(DLINK_VENDOR_ID, DLINK_PRODUCT_DWM_652) }, 542 { USB_DEVICE(DLINK_VENDOR_ID, DLINK_PRODUCT_DWM_652) },
524 { USB_DEVICE(0x1da5, 0x4515) }, /* BenQ H20 */ 543 { USB_DEVICE(QISDA_VENDOR_ID, QISDA_PRODUCT_H21_4512) },
544 { USB_DEVICE(QISDA_VENDOR_ID, QISDA_PRODUCT_H21_4523) },
545 { USB_DEVICE(QISDA_VENDOR_ID, QISDA_PRODUCT_H20_4515) },
546 { USB_DEVICE(QISDA_VENDOR_ID, QISDA_PRODUCT_H20_4519) },
547 { USB_DEVICE(TOSHIBA_VENDOR_ID, TOSHIBA_PRODUCT_HSDPA_MINICARD ) }, /* Toshiba 3G HSDPA == Novatel Expedite EU870D MiniCard */
548 { USB_DEVICE(ALINK_VENDOR_ID, 0x9000) },
549 { USB_DEVICE_AND_INTERFACE_INFO(ALINK_VENDOR_ID, ALINK_PRODUCT_3GU, 0xff, 0xff, 0xff) },
525 { } /* Terminating entry */ 550 { } /* Terminating entry */
526}; 551};
527MODULE_DEVICE_TABLE(usb, option_ids); 552MODULE_DEVICE_TABLE(usb, option_ids);
@@ -549,8 +574,10 @@ static struct usb_serial_driver option_1port_device = {
549 .usb_driver = &option_driver, 574 .usb_driver = &option_driver,
550 .id_table = option_ids, 575 .id_table = option_ids,
551 .num_ports = 1, 576 .num_ports = 1,
577 .probe = option_probe,
552 .open = option_open, 578 .open = option_open,
553 .close = option_close, 579 .close = option_close,
580 .dtr_rts = option_dtr_rts,
554 .write = option_write, 581 .write = option_write,
555 .write_room = option_write_room, 582 .write_room = option_write_room,
556 .chars_in_buffer = option_chars_in_buffer, 583 .chars_in_buffer = option_chars_in_buffer,
@@ -558,7 +585,8 @@ static struct usb_serial_driver option_1port_device = {
558 .tiocmget = option_tiocmget, 585 .tiocmget = option_tiocmget,
559 .tiocmset = option_tiocmset, 586 .tiocmset = option_tiocmset,
560 .attach = option_startup, 587 .attach = option_startup,
561 .shutdown = option_shutdown, 588 .disconnect = option_disconnect,
589 .release = option_release,
562 .read_int_callback = option_instat_callback, 590 .read_int_callback = option_instat_callback,
563 .suspend = option_suspend, 591 .suspend = option_suspend,
564 .resume = option_resume, 592 .resume = option_resume,
@@ -624,13 +652,25 @@ static void __exit option_exit(void)
624module_init(option_init); 652module_init(option_init);
625module_exit(option_exit); 653module_exit(option_exit);
626 654
655static int option_probe(struct usb_serial *serial,
656 const struct usb_device_id *id)
657{
658 /* D-Link DWM 652 still exposes CD-Rom emulation interface in modem mode */
659 if (serial->dev->descriptor.idVendor == DLINK_VENDOR_ID &&
660 serial->dev->descriptor.idProduct == DLINK_PRODUCT_DWM_652 &&
661 serial->interface->cur_altsetting->desc.bInterfaceClass == 0x8)
662 return -ENODEV;
663
664 return 0;
665}
666
627static void option_set_termios(struct tty_struct *tty, 667static void option_set_termios(struct tty_struct *tty,
628 struct usb_serial_port *port, struct ktermios *old_termios) 668 struct usb_serial_port *port, struct ktermios *old_termios)
629{ 669{
630 dbg("%s", __func__); 670 dbg("%s", __func__);
631 /* Doesn't support option setting */ 671 /* Doesn't support option setting */
632 tty_termios_copy_hw(tty->termios, old_termios); 672 tty_termios_copy_hw(tty->termios, old_termios);
633 option_send_setup(tty, port); 673 option_send_setup(port);
634} 674}
635 675
636static int option_tiocmget(struct tty_struct *tty, struct file *file) 676static int option_tiocmget(struct tty_struct *tty, struct file *file)
@@ -669,7 +709,7 @@ static int option_tiocmset(struct tty_struct *tty, struct file *file,
669 portdata->rts_state = 0; 709 portdata->rts_state = 0;
670 if (clear & TIOCM_DTR) 710 if (clear & TIOCM_DTR)
671 portdata->dtr_state = 0; 711 portdata->dtr_state = 0;
672 return option_send_setup(tty, port); 712 return option_send_setup(port);
673} 713}
674 714
675/* Write */ 715/* Write */
@@ -708,7 +748,6 @@ static int option_write(struct tty_struct *tty, struct usb_serial_port *port,
708 memcpy(this_urb->transfer_buffer, buf, todo); 748 memcpy(this_urb->transfer_buffer, buf, todo);
709 this_urb->transfer_buffer_length = todo; 749 this_urb->transfer_buffer_length = todo;
710 750
711 this_urb->dev = port->serial->dev;
712 err = usb_submit_urb(this_urb, GFP_ATOMIC); 751 err = usb_submit_urb(this_urb, GFP_ATOMIC);
713 if (err) { 752 if (err) {
714 dbg("usb_submit_urb %p (write bulk) failed " 753 dbg("usb_submit_urb %p (write bulk) failed "
@@ -836,7 +875,6 @@ static void option_instat_callback(struct urb *urb)
836 875
837 /* Resubmit urb so we continue receiving IRQ data */ 876 /* Resubmit urb so we continue receiving IRQ data */
838 if (status != -ESHUTDOWN && status != -ENOENT) { 877 if (status != -ESHUTDOWN && status != -ENOENT) {
839 urb->dev = serial->dev;
840 err = usb_submit_urb(urb, GFP_ATOMIC); 878 err = usb_submit_urb(urb, GFP_ATOMIC);
841 if (err) 879 if (err)
842 dbg("%s: resubmit intr urb failed. (%d)", 880 dbg("%s: resubmit intr urb failed. (%d)",
@@ -897,27 +935,11 @@ static int option_open(struct tty_struct *tty,
897 935
898 dbg("%s", __func__); 936 dbg("%s", __func__);
899 937
900 /* Set some sane defaults */ 938 /* Start reading from the IN endpoint */
901 portdata->rts_state = 1;
902 portdata->dtr_state = 1;
903
904 /* Reset low level data toggle and start reading from endpoints */
905 for (i = 0; i < N_IN_URB; i++) { 939 for (i = 0; i < N_IN_URB; i++) {
906 urb = portdata->in_urbs[i]; 940 urb = portdata->in_urbs[i];
907 if (!urb) 941 if (!urb)
908 continue; 942 continue;
909 if (urb->dev != serial->dev) {
910 dbg("%s: dev %p != %p", __func__,
911 urb->dev, serial->dev);
912 continue;
913 }
914
915 /*
916 * make sure endpoint data toggle is synchronized with the
917 * device
918 */
919 usb_clear_halt(urb->dev, urb->pipe);
920
921 err = usb_submit_urb(urb, GFP_KERNEL); 943 err = usb_submit_urb(urb, GFP_KERNEL);
922 if (err) { 944 if (err) {
923 dbg("%s: submit urb %d failed (%d) %d", 945 dbg("%s: submit urb %d failed (%d) %d",
@@ -926,47 +948,43 @@ static int option_open(struct tty_struct *tty,
926 } 948 }
927 } 949 }
928 950
929 /* Reset low level data toggle on out endpoints */ 951 option_send_setup(port);
930 for (i = 0; i < N_OUT_URB; i++) {
931 urb = portdata->out_urbs[i];
932 if (!urb)
933 continue;
934 urb->dev = serial->dev;
935 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
936 usb_pipeout(urb->pipe), 0); */
937 }
938
939 option_send_setup(tty, port);
940 952
941 return 0; 953 return 0;
942} 954}
943 955
944static void option_close(struct tty_struct *tty, 956static void option_dtr_rts(struct usb_serial_port *port, int on)
945 struct usb_serial_port *port, struct file *filp)
946{ 957{
947 int i;
948 struct usb_serial *serial = port->serial; 958 struct usb_serial *serial = port->serial;
949 struct option_port_private *portdata; 959 struct option_port_private *portdata;
950 960
951 dbg("%s", __func__); 961 dbg("%s", __func__);
952 portdata = usb_get_serial_port_data(port); 962 portdata = usb_get_serial_port_data(port);
963 mutex_lock(&serial->disc_mutex);
964 portdata->rts_state = on;
965 portdata->dtr_state = on;
966 if (serial->dev)
967 option_send_setup(port);
968 mutex_unlock(&serial->disc_mutex);
969}
953 970
954 portdata->rts_state = 0;
955 portdata->dtr_state = 0;
956 971
957 if (serial->dev) { 972static void option_close(struct usb_serial_port *port)
958 mutex_lock(&serial->disc_mutex); 973{
959 if (!serial->disconnected) 974 int i;
960 option_send_setup(tty, port); 975 struct usb_serial *serial = port->serial;
961 mutex_unlock(&serial->disc_mutex); 976 struct option_port_private *portdata;
962 977
978 dbg("%s", __func__);
979 portdata = usb_get_serial_port_data(port);
980
981 if (serial->dev) {
963 /* Stop reading/writing urbs */ 982 /* Stop reading/writing urbs */
964 for (i = 0; i < N_IN_URB; i++) 983 for (i = 0; i < N_IN_URB; i++)
965 usb_kill_urb(portdata->in_urbs[i]); 984 usb_kill_urb(portdata->in_urbs[i]);
966 for (i = 0; i < N_OUT_URB; i++) 985 for (i = 0; i < N_OUT_URB; i++)
967 usb_kill_urb(portdata->out_urbs[i]); 986 usb_kill_urb(portdata->out_urbs[i]);
968 } 987 }
969 tty_port_tty_set(&port->port, NULL);
970} 988}
971 989
972/* Helper functions used by option_setup_urbs */ 990/* Helper functions used by option_setup_urbs */
@@ -1032,28 +1050,24 @@ static void option_setup_urbs(struct usb_serial *serial)
1032 * This is exactly the same as SET_CONTROL_LINE_STATE from the PSTN 1050 * This is exactly the same as SET_CONTROL_LINE_STATE from the PSTN
1033 * CDC. 1051 * CDC.
1034*/ 1052*/
1035static int option_send_setup(struct tty_struct *tty, 1053static int option_send_setup(struct usb_serial_port *port)
1036 struct usb_serial_port *port)
1037{ 1054{
1038 struct usb_serial *serial = port->serial; 1055 struct usb_serial *serial = port->serial;
1039 struct option_port_private *portdata; 1056 struct option_port_private *portdata;
1040 int ifNum = serial->interface->cur_altsetting->desc.bInterfaceNumber; 1057 int ifNum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
1058 int val = 0;
1041 dbg("%s", __func__); 1059 dbg("%s", __func__);
1042 1060
1043 portdata = usb_get_serial_port_data(port); 1061 portdata = usb_get_serial_port_data(port);
1044 1062
1045 if (tty) { 1063 if (portdata->dtr_state)
1046 int val = 0; 1064 val |= 0x01;
1047 if (portdata->dtr_state) 1065 if (portdata->rts_state)
1048 val |= 0x01; 1066 val |= 0x02;
1049 if (portdata->rts_state)
1050 val |= 0x02;
1051 1067
1052 return usb_control_msg(serial->dev, 1068 return usb_control_msg(serial->dev,
1053 usb_rcvctrlpipe(serial->dev, 0), 1069 usb_rcvctrlpipe(serial->dev, 0),
1054 0x22, 0x21, val, ifNum, NULL, 0, USB_CTRL_SET_TIMEOUT); 1070 0x22, 0x21, val, ifNum, NULL, 0, USB_CTRL_SET_TIMEOUT);
1055 }
1056 return 0;
1057} 1071}
1058 1072
1059static int option_startup(struct usb_serial *serial) 1073static int option_startup(struct usb_serial *serial)
@@ -1129,7 +1143,14 @@ static void stop_read_write_urbs(struct usb_serial *serial)
1129 } 1143 }
1130} 1144}
1131 1145
1132static void option_shutdown(struct usb_serial *serial) 1146static void option_disconnect(struct usb_serial *serial)
1147{
1148 dbg("%s", __func__);
1149
1150 stop_read_write_urbs(serial);
1151}
1152
1153static void option_release(struct usb_serial *serial)
1133{ 1154{
1134 int i, j; 1155 int i, j;
1135 struct usb_serial_port *port; 1156 struct usb_serial_port *port;
@@ -1137,8 +1158,6 @@ static void option_shutdown(struct usb_serial *serial)
1137 1158
1138 dbg("%s", __func__); 1159 dbg("%s", __func__);
1139 1160
1140 stop_read_write_urbs(serial);
1141
1142 /* Now free them */ 1161 /* Now free them */
1143 for (i = 0; i < serial->num_ports; ++i) { 1162 for (i = 0; i < serial->num_ports; ++i) {
1144 port = serial->port[i]; 1163 port = serial->port[i];
@@ -1191,7 +1210,6 @@ static int option_resume(struct usb_serial *serial)
1191 dbg("%s: No interrupt URB for port %d\n", __func__, i); 1210 dbg("%s: No interrupt URB for port %d\n", __func__, i);
1192 continue; 1211 continue;
1193 } 1212 }
1194 port->interrupt_in_urb->dev = serial->dev;
1195 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 1213 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
1196 dbg("Submitted interrupt URB for port %d (result %d)", i, err); 1214 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
1197 if (err < 0) { 1215 if (err < 0) {
diff --git a/drivers/usb/serial/oti6858.c b/drivers/usb/serial/oti6858.c
index ba551f00f16f..3cece27325e7 100644
--- a/drivers/usb/serial/oti6858.c
+++ b/drivers/usb/serial/oti6858.c
@@ -143,8 +143,7 @@ struct oti6858_control_pkt {
143/* function prototypes */ 143/* function prototypes */
144static int oti6858_open(struct tty_struct *tty, 144static int oti6858_open(struct tty_struct *tty,
145 struct usb_serial_port *port, struct file *filp); 145 struct usb_serial_port *port, struct file *filp);
146static void oti6858_close(struct tty_struct *tty, 146static void oti6858_close(struct usb_serial_port *port);
147 struct usb_serial_port *port, struct file *filp);
148static void oti6858_set_termios(struct tty_struct *tty, 147static void oti6858_set_termios(struct tty_struct *tty,
149 struct usb_serial_port *port, struct ktermios *old); 148 struct usb_serial_port *port, struct ktermios *old);
150static int oti6858_ioctl(struct tty_struct *tty, struct file *file, 149static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
@@ -160,7 +159,7 @@ static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
160static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, 159static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
161 unsigned int set, unsigned int clear); 160 unsigned int set, unsigned int clear);
162static int oti6858_startup(struct usb_serial *serial); 161static int oti6858_startup(struct usb_serial *serial);
163static void oti6858_shutdown(struct usb_serial *serial); 162static void oti6858_release(struct usb_serial *serial);
164 163
165/* functions operating on buffers */ 164/* functions operating on buffers */
166static struct oti6858_buf *oti6858_buf_alloc(unsigned int size); 165static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
@@ -195,7 +194,7 @@ static struct usb_serial_driver oti6858_device = {
195 .write_room = oti6858_write_room, 194 .write_room = oti6858_write_room,
196 .chars_in_buffer = oti6858_chars_in_buffer, 195 .chars_in_buffer = oti6858_chars_in_buffer,
197 .attach = oti6858_startup, 196 .attach = oti6858_startup,
198 .shutdown = oti6858_shutdown, 197 .release = oti6858_release,
199}; 198};
200 199
201struct oti6858_private { 200struct oti6858_private {
@@ -622,67 +621,30 @@ static int oti6858_open(struct tty_struct *tty,
622 if (result != 0) { 621 if (result != 0) {
623 dev_err(&port->dev, "%s(): usb_submit_urb() failed" 622 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
624 " with error %d\n", __func__, result); 623 " with error %d\n", __func__, result);
625 oti6858_close(tty, port, NULL); 624 oti6858_close(port);
626 return -EPROTO; 625 return -EPROTO;
627 } 626 }
628 627
629 /* setup termios */ 628 /* setup termios */
630 if (tty) 629 if (tty)
631 oti6858_set_termios(tty, port, &tmp_termios); 630 oti6858_set_termios(tty, port, &tmp_termios);
632 631 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
633 return 0; 632 return 0;
634} 633}
635 634
636static void oti6858_close(struct tty_struct *tty, 635static void oti6858_close(struct usb_serial_port *port)
637 struct usb_serial_port *port, struct file *filp)
638{ 636{
639 struct oti6858_private *priv = usb_get_serial_port_data(port); 637 struct oti6858_private *priv = usb_get_serial_port_data(port);
640 unsigned long flags; 638 unsigned long flags;
641 long timeout;
642 wait_queue_t wait;
643 639
644 dbg("%s(port = %d)", __func__, port->number); 640 dbg("%s(port = %d)", __func__, port->number);
645 641
646 /* wait for data to drain from the buffer */
647 spin_lock_irqsave(&priv->lock, flags); 642 spin_lock_irqsave(&priv->lock, flags);
648 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
649 init_waitqueue_entry(&wait, current);
650 add_wait_queue(&tty->write_wait, &wait);
651 dbg("%s(): entering wait loop", __func__);
652 for (;;) {
653 set_current_state(TASK_INTERRUPTIBLE);
654 if (oti6858_buf_data_avail(priv->buf) == 0
655 || timeout == 0 || signal_pending(current)
656 || port->serial->disconnected)
657 break;
658 spin_unlock_irqrestore(&priv->lock, flags);
659 timeout = schedule_timeout(timeout);
660 spin_lock_irqsave(&priv->lock, flags);
661 }
662 set_current_state(TASK_RUNNING);
663 remove_wait_queue(&tty->write_wait, &wait);
664 dbg("%s(): after wait loop", __func__);
665
666 /* clear out any remaining data in the buffer */ 643 /* clear out any remaining data in the buffer */
667 oti6858_buf_clear(priv->buf); 644 oti6858_buf_clear(priv->buf);
668 spin_unlock_irqrestore(&priv->lock, flags); 645 spin_unlock_irqrestore(&priv->lock, flags);
669 646
670 /* wait for characters to drain from the device */ 647 dbg("%s(): after buf_clear()", __func__);
671 /* (this is long enough for the entire 256 byte */
672 /* pl2303 hardware buffer to drain with no flow */
673 /* control for data rates of 1200 bps or more, */
674 /* for lower rates we should really know how much */
675 /* data is in the buffer to compute a delay */
676 /* that is not unnecessarily long) */
677 /* FIXME
678 bps = tty_get_baud_rate(tty);
679 if (bps > 1200)
680 timeout = max((HZ*2560)/bps,HZ/10);
681 else
682 */
683 timeout = 2*HZ;
684 schedule_timeout_interruptible(timeout);
685 dbg("%s(): after schedule_timeout_interruptible()", __func__);
686 648
687 /* cancel scheduled setup */ 649 /* cancel scheduled setup */
688 cancel_delayed_work(&priv->delayed_setup_work); 650 cancel_delayed_work(&priv->delayed_setup_work);
@@ -694,15 +656,6 @@ static void oti6858_close(struct tty_struct *tty,
694 usb_kill_urb(port->write_urb); 656 usb_kill_urb(port->write_urb);
695 usb_kill_urb(port->read_urb); 657 usb_kill_urb(port->read_urb);
696 usb_kill_urb(port->interrupt_in_urb); 658 usb_kill_urb(port->interrupt_in_urb);
697
698 /*
699 if (tty && (tty->termios->c_cflag) & HUPCL) {
700 // drop DTR and RTS
701 spin_lock_irqsave(&priv->lock, flags);
702 priv->pending_setup.control &= ~CONTROL_MASK;
703 spin_unlock_irqrestore(&priv->lock, flags);
704 }
705 */
706} 659}
707 660
708static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, 661static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
@@ -829,7 +782,7 @@ static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
829} 782}
830 783
831 784
832static void oti6858_shutdown(struct usb_serial *serial) 785static void oti6858_release(struct usb_serial *serial)
833{ 786{
834 struct oti6858_private *priv; 787 struct oti6858_private *priv;
835 int i; 788 int i;
@@ -841,7 +794,6 @@ static void oti6858_shutdown(struct usb_serial *serial)
841 if (priv) { 794 if (priv) {
842 oti6858_buf_free(priv->buf); 795 oti6858_buf_free(priv->buf);
843 kfree(priv); 796 kfree(priv);
844 usb_set_serial_port_data(serial->port[i], NULL);
845 } 797 }
846 } 798 }
847} 799}
diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index 751a533a4347..7d15bfa7c2db 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -94,6 +94,7 @@ static struct usb_device_id id_table [] = {
94 { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) }, 94 { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
95 { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) }, 95 { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
96 { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) }, 96 { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
97 { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
97 { } /* Terminating entry */ 98 { } /* Terminating entry */
98}; 99};
99 100
@@ -652,69 +653,41 @@ static void pl2303_set_termios(struct tty_struct *tty,
652 kfree(buf); 653 kfree(buf);
653} 654}
654 655
655static void pl2303_close(struct tty_struct *tty, 656static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
656 struct usb_serial_port *port, struct file *filp) 657{
658 struct pl2303_private *priv = usb_get_serial_port_data(port);
659 unsigned long flags;
660 u8 control;
661
662 spin_lock_irqsave(&priv->lock, flags);
663 /* Change DTR and RTS */
664 if (on)
665 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
666 else
667 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
668 control = priv->line_control;
669 spin_unlock_irqrestore(&priv->lock, flags);
670 set_control_lines(port->serial->dev, control);
671}
672
673static void pl2303_close(struct usb_serial_port *port)
657{ 674{
658 struct pl2303_private *priv = usb_get_serial_port_data(port); 675 struct pl2303_private *priv = usb_get_serial_port_data(port);
659 unsigned long flags; 676 unsigned long flags;
660 unsigned int c_cflag;
661 int bps;
662 long timeout;
663 wait_queue_t wait;
664 677
665 dbg("%s - port %d", __func__, port->number); 678 dbg("%s - port %d", __func__, port->number);
666 679
667 /* wait for data to drain from the buffer */
668 spin_lock_irqsave(&priv->lock, flags); 680 spin_lock_irqsave(&priv->lock, flags);
669 timeout = PL2303_CLOSING_WAIT;
670 init_waitqueue_entry(&wait, current);
671 add_wait_queue(&tty->write_wait, &wait);
672 for (;;) {
673 set_current_state(TASK_INTERRUPTIBLE);
674 if (pl2303_buf_data_avail(priv->buf) == 0 ||
675 timeout == 0 || signal_pending(current) ||
676 port->serial->disconnected)
677 break;
678 spin_unlock_irqrestore(&priv->lock, flags);
679 timeout = schedule_timeout(timeout);
680 spin_lock_irqsave(&priv->lock, flags);
681 }
682 set_current_state(TASK_RUNNING);
683 remove_wait_queue(&tty->write_wait, &wait);
684 /* clear out any remaining data in the buffer */ 681 /* clear out any remaining data in the buffer */
685 pl2303_buf_clear(priv->buf); 682 pl2303_buf_clear(priv->buf);
686 spin_unlock_irqrestore(&priv->lock, flags); 683 spin_unlock_irqrestore(&priv->lock, flags);
687 684
688 /* wait for characters to drain from the device */
689 /* (this is long enough for the entire 256 byte */
690 /* pl2303 hardware buffer to drain with no flow */
691 /* control for data rates of 1200 bps or more, */
692 /* for lower rates we should really know how much */
693 /* data is in the buffer to compute a delay */
694 /* that is not unnecessarily long) */
695 bps = tty_get_baud_rate(tty);
696 if (bps > 1200)
697 timeout = max((HZ*2560)/bps, HZ/10);
698 else
699 timeout = 2*HZ;
700 schedule_timeout_interruptible(timeout);
701
702 /* shutdown our urbs */ 685 /* shutdown our urbs */
703 dbg("%s - shutting down urbs", __func__); 686 dbg("%s - shutting down urbs", __func__);
704 usb_kill_urb(port->write_urb); 687 usb_kill_urb(port->write_urb);
705 usb_kill_urb(port->read_urb); 688 usb_kill_urb(port->read_urb);
706 usb_kill_urb(port->interrupt_in_urb); 689 usb_kill_urb(port->interrupt_in_urb);
707 690
708 if (tty) {
709 c_cflag = tty->termios->c_cflag;
710 if (c_cflag & HUPCL) {
711 /* drop DTR and RTS */
712 spin_lock_irqsave(&priv->lock, flags);
713 priv->line_control = 0;
714 spin_unlock_irqrestore(&priv->lock, flags);
715 set_control_lines(port->serial->dev, 0);
716 }
717 }
718} 691}
719 692
720static int pl2303_open(struct tty_struct *tty, 693static int pl2303_open(struct tty_struct *tty,
@@ -748,7 +721,7 @@ static int pl2303_open(struct tty_struct *tty,
748 if (result) { 721 if (result) {
749 dev_err(&port->dev, "%s - failed submitting read urb," 722 dev_err(&port->dev, "%s - failed submitting read urb,"
750 " error %d\n", __func__, result); 723 " error %d\n", __func__, result);
751 pl2303_close(tty, port, NULL); 724 pl2303_close(port);
752 return -EPROTO; 725 return -EPROTO;
753 } 726 }
754 727
@@ -758,9 +731,10 @@ static int pl2303_open(struct tty_struct *tty,
758 if (result) { 731 if (result) {
759 dev_err(&port->dev, "%s - failed submitting interrupt urb," 732 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
760 " error %d\n", __func__, result); 733 " error %d\n", __func__, result);
761 pl2303_close(tty, port, NULL); 734 pl2303_close(port);
762 return -EPROTO; 735 return -EPROTO;
763 } 736 }
737 port->port.drain_delay = 256;
764 return 0; 738 return 0;
765} 739}
766 740
@@ -821,6 +795,14 @@ static int pl2303_tiocmget(struct tty_struct *tty, struct file *file)
821 return result; 795 return result;
822} 796}
823 797
798static int pl2303_carrier_raised(struct usb_serial_port *port)
799{
800 struct pl2303_private *priv = usb_get_serial_port_data(port);
801 if (priv->line_status & UART_DCD)
802 return 1;
803 return 0;
804}
805
824static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 806static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
825{ 807{
826 struct pl2303_private *priv = usb_get_serial_port_data(port); 808 struct pl2303_private *priv = usb_get_serial_port_data(port);
@@ -897,7 +879,7 @@ static void pl2303_break_ctl(struct tty_struct *tty, int break_state)
897 dbg("%s - error sending break = %d", __func__, result); 879 dbg("%s - error sending break = %d", __func__, result);
898} 880}
899 881
900static void pl2303_shutdown(struct usb_serial *serial) 882static void pl2303_release(struct usb_serial *serial)
901{ 883{
902 int i; 884 int i;
903 struct pl2303_private *priv; 885 struct pl2303_private *priv;
@@ -909,7 +891,6 @@ static void pl2303_shutdown(struct usb_serial *serial)
909 if (priv) { 891 if (priv) {
910 pl2303_buf_free(priv->buf); 892 pl2303_buf_free(priv->buf);
911 kfree(priv); 893 kfree(priv);
912 usb_set_serial_port_data(serial->port[i], NULL);
913 } 894 }
914 } 895 }
915} 896}
@@ -946,6 +927,8 @@ static void pl2303_update_line_status(struct usb_serial_port *port,
946 spin_lock_irqsave(&priv->lock, flags); 927 spin_lock_irqsave(&priv->lock, flags);
947 priv->line_status = data[status_idx]; 928 priv->line_status = data[status_idx];
948 spin_unlock_irqrestore(&priv->lock, flags); 929 spin_unlock_irqrestore(&priv->lock, flags);
930 if (priv->line_status & UART_BREAK_ERROR)
931 usb_serial_handle_break(port);
949 wake_up_interruptible(&priv->delta_msr_wait); 932 wake_up_interruptible(&priv->delta_msr_wait);
950} 933}
951 934
@@ -989,18 +972,46 @@ exit:
989 __func__, retval); 972 __func__, retval);
990} 973}
991 974
975static void pl2303_push_data(struct tty_struct *tty,
976 struct usb_serial_port *port, struct urb *urb,
977 u8 line_status)
978{
979 unsigned char *data = urb->transfer_buffer;
980 /* get tty_flag from status */
981 char tty_flag = TTY_NORMAL;
982 /* break takes precedence over parity, */
983 /* which takes precedence over framing errors */
984 if (line_status & UART_BREAK_ERROR)
985 tty_flag = TTY_BREAK;
986 else if (line_status & UART_PARITY_ERROR)
987 tty_flag = TTY_PARITY;
988 else if (line_status & UART_FRAME_ERROR)
989 tty_flag = TTY_FRAME;
990 dbg("%s - tty_flag = %d", __func__, tty_flag);
991
992 tty_buffer_request_room(tty, urb->actual_length + 1);
993 /* overrun is special, not associated with a char */
994 if (line_status & UART_OVERRUN_ERROR)
995 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
996 if (port->console && port->sysrq) {
997 int i;
998 for (i = 0; i < urb->actual_length; ++i)
999 if (!usb_serial_handle_sysrq_char(tty, port, data[i]))
1000 tty_insert_flip_char(tty, data[i], tty_flag);
1001 } else
1002 tty_insert_flip_string(tty, data, urb->actual_length);
1003 tty_flip_buffer_push(tty);
1004}
1005
992static void pl2303_read_bulk_callback(struct urb *urb) 1006static void pl2303_read_bulk_callback(struct urb *urb)
993{ 1007{
994 struct usb_serial_port *port = urb->context; 1008 struct usb_serial_port *port = urb->context;
995 struct pl2303_private *priv = usb_get_serial_port_data(port); 1009 struct pl2303_private *priv = usb_get_serial_port_data(port);
996 struct tty_struct *tty; 1010 struct tty_struct *tty;
997 unsigned char *data = urb->transfer_buffer;
998 unsigned long flags; 1011 unsigned long flags;
999 int i;
1000 int result; 1012 int result;
1001 int status = urb->status; 1013 int status = urb->status;
1002 u8 line_status; 1014 u8 line_status;
1003 char tty_flag;
1004 1015
1005 dbg("%s - port %d", __func__, port->number); 1016 dbg("%s - port %d", __func__, port->number);
1006 1017
@@ -1028,10 +1039,7 @@ static void pl2303_read_bulk_callback(struct urb *urb)
1028 } 1039 }
1029 1040
1030 usb_serial_debug_data(debug, &port->dev, __func__, 1041 usb_serial_debug_data(debug, &port->dev, __func__,
1031 urb->actual_length, data); 1042 urb->actual_length, urb->transfer_buffer);
1032
1033 /* get tty_flag from status */
1034 tty_flag = TTY_NORMAL;
1035 1043
1036 spin_lock_irqsave(&priv->lock, flags); 1044 spin_lock_irqsave(&priv->lock, flags);
1037 line_status = priv->line_status; 1045 line_status = priv->line_status;
@@ -1039,25 +1047,9 @@ static void pl2303_read_bulk_callback(struct urb *urb)
1039 spin_unlock_irqrestore(&priv->lock, flags); 1047 spin_unlock_irqrestore(&priv->lock, flags);
1040 wake_up_interruptible(&priv->delta_msr_wait); 1048 wake_up_interruptible(&priv->delta_msr_wait);
1041 1049
1042 /* break takes precedence over parity, */
1043 /* which takes precedence over framing errors */
1044 if (line_status & UART_BREAK_ERROR)
1045 tty_flag = TTY_BREAK;
1046 else if (line_status & UART_PARITY_ERROR)
1047 tty_flag = TTY_PARITY;
1048 else if (line_status & UART_FRAME_ERROR)
1049 tty_flag = TTY_FRAME;
1050 dbg("%s - tty_flag = %d", __func__, tty_flag);
1051
1052 tty = tty_port_tty_get(&port->port); 1050 tty = tty_port_tty_get(&port->port);
1053 if (tty && urb->actual_length) { 1051 if (tty && urb->actual_length) {
1054 tty_buffer_request_room(tty, urb->actual_length + 1); 1052 pl2303_push_data(tty, port, urb, line_status);
1055 /* overrun is special, not associated with a char */
1056 if (line_status & UART_OVERRUN_ERROR)
1057 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1058 for (i = 0; i < urb->actual_length; ++i)
1059 tty_insert_flip_char(tty, data[i], tty_flag);
1060 tty_flip_buffer_push(tty);
1061 } 1053 }
1062 tty_kref_put(tty); 1054 tty_kref_put(tty);
1063 /* Schedule the next read _if_ we are still open */ 1055 /* Schedule the next read _if_ we are still open */
@@ -1125,6 +1117,8 @@ static struct usb_serial_driver pl2303_device = {
1125 .num_ports = 1, 1117 .num_ports = 1,
1126 .open = pl2303_open, 1118 .open = pl2303_open,
1127 .close = pl2303_close, 1119 .close = pl2303_close,
1120 .dtr_rts = pl2303_dtr_rts,
1121 .carrier_raised = pl2303_carrier_raised,
1128 .write = pl2303_write, 1122 .write = pl2303_write,
1129 .ioctl = pl2303_ioctl, 1123 .ioctl = pl2303_ioctl,
1130 .break_ctl = pl2303_break_ctl, 1124 .break_ctl = pl2303_break_ctl,
@@ -1137,7 +1131,7 @@ static struct usb_serial_driver pl2303_device = {
1137 .write_room = pl2303_write_room, 1131 .write_room = pl2303_write_room,
1138 .chars_in_buffer = pl2303_chars_in_buffer, 1132 .chars_in_buffer = pl2303_chars_in_buffer,
1139 .attach = pl2303_startup, 1133 .attach = pl2303_startup,
1140 .shutdown = pl2303_shutdown, 1134 .release = pl2303_release,
1141}; 1135};
1142 1136
1143static int __init pl2303_init(void) 1137static int __init pl2303_init(void)
diff --git a/drivers/usb/serial/pl2303.h b/drivers/usb/serial/pl2303.h
index 1d7a22e3a9fd..12aac7d2462d 100644
--- a/drivers/usb/serial/pl2303.h
+++ b/drivers/usb/serial/pl2303.h
@@ -122,3 +122,7 @@
122/* Hewlett-Packard LD220-HP POS Pole Display */ 122/* Hewlett-Packard LD220-HP POS Pole Display */
123#define HP_VENDOR_ID 0x03f0 123#define HP_VENDOR_ID 0x03f0
124#define HP_LD220_PRODUCT_ID 0x3524 124#define HP_LD220_PRODUCT_ID 0x3524
125
126/* Cressi Edy (diving computer) PC interface */
127#define CRESSI_VENDOR_ID 0x04b8
128#define CRESSI_EDY_PRODUCT_ID 0x0521
diff --git a/drivers/usb/serial/sierra.c b/drivers/usb/serial/sierra.c
index 913225c61610..f48d05e0acc1 100644
--- a/drivers/usb/serial/sierra.c
+++ b/drivers/usb/serial/sierra.c
@@ -1,7 +1,10 @@
1/* 1/*
2 USB Driver for Sierra Wireless 2 USB Driver for Sierra Wireless
3 3
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com> 4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
5
6 Copyright (C) 2008, 2009 Elina Pasheva, Matthew Safar, Rory Filer
7 <linux@sierrawireless.com>
5 8
6 IMPORTANT DISCLAIMER: This driver is not commercially supported by 9 IMPORTANT DISCLAIMER: This driver is not commercially supported by
7 Sierra Wireless. Use at your own risk. 10 Sierra Wireless. Use at your own risk.
@@ -14,8 +17,8 @@
14 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org> 17 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
15*/ 18*/
16 19
17#define DRIVER_VERSION "v.1.3.3" 20#define DRIVER_VERSION "v.1.3.7"
18#define DRIVER_AUTHOR "Kevin Lloyd <klloyd@sierrawireless.com>" 21#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
19#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems" 22#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
20 23
21#include <linux/kernel.h> 24#include <linux/kernel.h>
@@ -26,23 +29,32 @@
26#include <linux/module.h> 29#include <linux/module.h>
27#include <linux/usb.h> 30#include <linux/usb.h>
28#include <linux/usb/serial.h> 31#include <linux/usb/serial.h>
29#include <linux/usb/ch9.h>
30 32
31#define SWIMS_USB_REQUEST_SetPower 0x00 33#define SWIMS_USB_REQUEST_SetPower 0x00
32#define SWIMS_USB_REQUEST_SetNmea 0x07 34#define SWIMS_USB_REQUEST_SetNmea 0x07
33 35
34/* per port private data */ 36#define N_IN_URB 8
35#define N_IN_URB 4 37#define N_OUT_URB 64
36#define N_OUT_URB 4
37#define IN_BUFLEN 4096 38#define IN_BUFLEN 4096
38 39
40#define MAX_TRANSFER (PAGE_SIZE - 512)
41/* MAX_TRANSFER is chosen so that the VM is not stressed by
42 allocations > PAGE_SIZE and the number of packets in a page
43 is an integer 512 is the largest possible packet on EHCI */
44
39static int debug; 45static int debug;
40static int nmea; 46static int nmea;
41 47
48/* Used in interface blacklisting */
49struct sierra_iface_info {
50 const u32 infolen; /* number of interface numbers on blacklist */
51 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
52};
53
42static int sierra_set_power_state(struct usb_device *udev, __u16 swiState) 54static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
43{ 55{
44 int result; 56 int result;
45 dev_dbg(&udev->dev, "%s", __func__); 57 dev_dbg(&udev->dev, "%s\n", __func__);
46 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 58 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
47 SWIMS_USB_REQUEST_SetPower, /* __u8 request */ 59 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
48 USB_TYPE_VENDOR, /* __u8 request type */ 60 USB_TYPE_VENDOR, /* __u8 request type */
@@ -57,7 +69,7 @@ static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
57static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable) 69static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
58{ 70{
59 int result; 71 int result;
60 dev_dbg(&udev->dev, "%s", __func__); 72 dev_dbg(&udev->dev, "%s\n", __func__);
61 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 73 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
62 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */ 74 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
63 USB_TYPE_VENDOR, /* __u8 request type */ 75 USB_TYPE_VENDOR, /* __u8 request type */
@@ -71,18 +83,39 @@ static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
71 83
72static int sierra_calc_num_ports(struct usb_serial *serial) 84static int sierra_calc_num_ports(struct usb_serial *serial)
73{ 85{
74 int result; 86 int num_ports = 0;
75 int *num_ports = usb_get_serial_data(serial); 87 u8 ifnum, numendpoints;
76 dev_dbg(&serial->dev->dev, "%s", __func__);
77 88
78 result = *num_ports; 89 dev_dbg(&serial->dev->dev, "%s\n", __func__);
79 90
80 if (result) { 91 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
81 kfree(num_ports); 92 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
82 usb_set_serial_data(serial, NULL);
83 }
84 93
85 return result; 94 /* Dummy interface present on some SKUs should be ignored */
95 if (ifnum == 0x99)
96 num_ports = 0;
97 else if (numendpoints <= 3)
98 num_ports = 1;
99 else
100 num_ports = (numendpoints-1)/2;
101 return num_ports;
102}
103
104static int is_blacklisted(const u8 ifnum,
105 const struct sierra_iface_info *blacklist)
106{
107 const u8 *info;
108 int i;
109
110 if (blacklist) {
111 info = blacklist->ifaceinfo;
112
113 for (i = 0; i < blacklist->infolen; i++) {
114 if (info[i] == ifnum)
115 return 1;
116 }
117 }
118 return 0;
86} 119}
87 120
88static int sierra_calc_interface(struct usb_serial *serial) 121static int sierra_calc_interface(struct usb_serial *serial)
@@ -90,7 +123,7 @@ static int sierra_calc_interface(struct usb_serial *serial)
90 int interface; 123 int interface;
91 struct usb_interface *p_interface; 124 struct usb_interface *p_interface;
92 struct usb_host_interface *p_host_interface; 125 struct usb_host_interface *p_host_interface;
93 dev_dbg(&serial->dev->dev, "%s", __func__); 126 dev_dbg(&serial->dev->dev, "%s\n", __func__);
94 127
95 /* Get the interface structure pointer from the serial struct */ 128 /* Get the interface structure pointer from the serial struct */
96 p_interface = serial->interface; 129 p_interface = serial->interface;
@@ -111,23 +144,12 @@ static int sierra_probe(struct usb_serial *serial,
111{ 144{
112 int result = 0; 145 int result = 0;
113 struct usb_device *udev; 146 struct usb_device *udev;
114 int *num_ports;
115 u8 ifnum; 147 u8 ifnum;
116 u8 numendpoints;
117
118 dev_dbg(&serial->dev->dev, "%s", __func__);
119 148
120 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
121 if (!num_ports)
122 return -ENOMEM;
123
124 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
125 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
126 udev = serial->dev; 149 udev = serial->dev;
150 dev_dbg(&udev->dev, "%s\n", __func__);
127 151
128 /* Figure out the interface number from the serial structure */
129 ifnum = sierra_calc_interface(serial); 152 ifnum = sierra_calc_interface(serial);
130
131 /* 153 /*
132 * If this interface supports more than 1 alternate 154 * If this interface supports more than 1 alternate
133 * select the 2nd one 155 * select the 2nd one
@@ -139,58 +161,77 @@ static int sierra_probe(struct usb_serial *serial,
139 usb_set_interface(udev, ifnum, 1); 161 usb_set_interface(udev, ifnum, 1);
140 } 162 }
141 163
142 /* Dummy interface present on some SKUs should be ignored */ 164 /* ifnum could have changed - by calling usb_set_interface */
143 if (ifnum == 0x99) 165 ifnum = sierra_calc_interface(serial);
144 *num_ports = 0;
145 else if (numendpoints <= 3)
146 *num_ports = 1;
147 else
148 *num_ports = (numendpoints-1)/2;
149 166
150 /* 167 if (is_blacklisted(ifnum,
151 * save off our num_ports info so that we can use it in the 168 (struct sierra_iface_info *)id->driver_info)) {
152 * calc_num_ports callback 169 dev_dbg(&serial->dev->dev,
153 */ 170 "Ignoring blacklisted interface #%d\n", ifnum);
154 usb_set_serial_data(serial, (void *)num_ports); 171 return -ENODEV;
172 }
155 173
156 return result; 174 return result;
157} 175}
158 176
177static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
178static const struct sierra_iface_info direct_ip_interface_blacklist = {
179 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
180 .ifaceinfo = direct_ip_non_serial_ifaces,
181};
182
159static struct usb_device_id id_table [] = { 183static struct usb_device_id id_table [] = {
184 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
185 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
186 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
187
160 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */ 188 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
161 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */ 189 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
162 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */ 190 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
163 { USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
164 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */ 191 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
165 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
166 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */ 192 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
193 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
194 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
195 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
167 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */ 196 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
168 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */ 197 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
198 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
169 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */ 199 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
170 /* Sierra Wireless C597 */ 200 /* Sierra Wireless C597 */
171 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) }, 201 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
172 /* Sierra Wireless Device */ 202 /* Sierra Wireless T598 */
173 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) }, 203 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
174 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */ 204 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
175 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */ 205 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
176 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */ 206 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
207 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
177 208
178 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */ 209 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
179 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
180 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */ 210 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
211 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
212 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
213 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
214 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
181 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */ 215 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
182 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */ 216 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
183 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */ 217 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
184 { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */ 218 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
185 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */ 219 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
186 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */ 220 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
221 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
187 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */ 222 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
188 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */ 223 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
224 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
225 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
226 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
227 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
189 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */ 228 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
190 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */ 229 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
191 { USB_DEVICE(0x1199, 0x683C) }, /* Sierra Wireless MC8790 */ 230 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
192 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8790 */ 231 { USB_DEVICE(0x1199, 0x683C) },
193 { USB_DEVICE(0x1199, 0x683E) }, /* Sierra Wireless MC8790 */ 232 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
233 /* Sierra Wireless MC8790, MC8791, MC8792 */
234 { USB_DEVICE(0x1199, 0x683E) },
194 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */ 235 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
195 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */ 236 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
196 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */ 237 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
@@ -201,15 +242,16 @@ static struct usb_device_id id_table [] = {
201 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */ 242 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
202 /* Sierra Wireless C885 */ 243 /* Sierra Wireless C885 */
203 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)}, 244 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
204 /* Sierra Wireless Device */ 245 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
205 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)}, 246 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
206 /* Sierra Wireless Device */ 247 /* Sierra Wireless C22/C33 */
207 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)}, 248 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
208 /* Sierra Wireless Device */ 249 /* Sierra Wireless HSPA Non-Composite Device */
209 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)}, 250 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
210 251 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
211 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */ 252 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
212 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */ 253 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
254 },
213 255
214 { } 256 { }
215}; 257};
@@ -229,7 +271,6 @@ struct sierra_port_private {
229 271
230 /* Input endpoints and buffers for this port */ 272 /* Input endpoints and buffers for this port */
231 struct urb *in_urbs[N_IN_URB]; 273 struct urb *in_urbs[N_IN_URB];
232 char *in_buffer[N_IN_URB];
233 274
234 /* Settings for the port */ 275 /* Settings for the port */
235 int rts_state; /* Handshaking pins (outputs) */ 276 int rts_state; /* Handshaking pins (outputs) */
@@ -240,66 +281,59 @@ struct sierra_port_private {
240 int ri_state; 281 int ri_state;
241}; 282};
242 283
243static int sierra_send_setup(struct tty_struct *tty, 284static int sierra_send_setup(struct usb_serial_port *port)
244 struct usb_serial_port *port)
245{ 285{
246 struct usb_serial *serial = port->serial; 286 struct usb_serial *serial = port->serial;
247 struct sierra_port_private *portdata; 287 struct sierra_port_private *portdata;
248 __u16 interface = 0; 288 __u16 interface = 0;
289 int val = 0;
249 290
250 dev_dbg(&port->dev, "%s", __func__); 291 dev_dbg(&port->dev, "%s\n", __func__);
251 292
252 portdata = usb_get_serial_port_data(port); 293 portdata = usb_get_serial_port_data(port);
253 294
254 if (tty) { 295 if (portdata->dtr_state)
255 int val = 0; 296 val |= 0x01;
256 if (portdata->dtr_state) 297 if (portdata->rts_state)
257 val |= 0x01; 298 val |= 0x02;
258 if (portdata->rts_state)
259 val |= 0x02;
260
261 /* If composite device then properly report interface */
262 if (serial->num_ports == 1) {
263 interface = sierra_calc_interface(serial);
264
265 /* Control message is sent only to interfaces with
266 * interrupt_in endpoints
267 */
268 if (port->interrupt_in_urb) {
269 /* send control message */
270 return usb_control_msg(serial->dev,
271 usb_rcvctrlpipe(serial->dev, 0),
272 0x22, 0x21, val, interface,
273 NULL, 0, USB_CTRL_SET_TIMEOUT);
274 }
275 }
276
277 /* Otherwise the need to do non-composite mapping */
278 else {
279 if (port->bulk_out_endpointAddress == 2)
280 interface = 0;
281 else if (port->bulk_out_endpointAddress == 4)
282 interface = 1;
283 else if (port->bulk_out_endpointAddress == 5)
284 interface = 2;
285 299
300 /* If composite device then properly report interface */
301 if (serial->num_ports == 1) {
302 interface = sierra_calc_interface(serial);
303 /* Control message is sent only to interfaces with
304 * interrupt_in endpoints
305 */
306 if (port->interrupt_in_urb) {
307 /* send control message */
286 return usb_control_msg(serial->dev, 308 return usb_control_msg(serial->dev,
287 usb_rcvctrlpipe(serial->dev, 0), 309 usb_rcvctrlpipe(serial->dev, 0),
288 0x22, 0x21, val, interface, 310 0x22, 0x21, val, interface,
289 NULL, 0, USB_CTRL_SET_TIMEOUT); 311 NULL, 0, USB_CTRL_SET_TIMEOUT);
290
291 } 312 }
292 } 313 }
293 314
315 /* Otherwise the need to do non-composite mapping */
316 else {
317 if (port->bulk_out_endpointAddress == 2)
318 interface = 0;
319 else if (port->bulk_out_endpointAddress == 4)
320 interface = 1;
321 else if (port->bulk_out_endpointAddress == 5)
322 interface = 2;
323 return usb_control_msg(serial->dev,
324 usb_rcvctrlpipe(serial->dev, 0),
325 0x22, 0x21, val, interface,
326 NULL, 0, USB_CTRL_SET_TIMEOUT);
327 }
294 return 0; 328 return 0;
295} 329}
296 330
297static void sierra_set_termios(struct tty_struct *tty, 331static void sierra_set_termios(struct tty_struct *tty,
298 struct usb_serial_port *port, struct ktermios *old_termios) 332 struct usb_serial_port *port, struct ktermios *old_termios)
299{ 333{
300 dev_dbg(&port->dev, "%s", __func__); 334 dev_dbg(&port->dev, "%s\n", __func__);
301 tty_termios_copy_hw(tty->termios, old_termios); 335 tty_termios_copy_hw(tty->termios, old_termios);
302 sierra_send_setup(tty, port); 336 sierra_send_setup(port);
303} 337}
304 338
305static int sierra_tiocmget(struct tty_struct *tty, struct file *file) 339static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
@@ -308,7 +342,7 @@ static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
308 unsigned int value; 342 unsigned int value;
309 struct sierra_port_private *portdata; 343 struct sierra_port_private *portdata;
310 344
311 dev_dbg(&port->dev, "%s", __func__); 345 dev_dbg(&port->dev, "%s\n", __func__);
312 portdata = usb_get_serial_port_data(port); 346 portdata = usb_get_serial_port_data(port);
313 347
314 value = ((portdata->rts_state) ? TIOCM_RTS : 0) | 348 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
@@ -338,7 +372,18 @@ static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
338 portdata->rts_state = 0; 372 portdata->rts_state = 0;
339 if (clear & TIOCM_DTR) 373 if (clear & TIOCM_DTR)
340 portdata->dtr_state = 0; 374 portdata->dtr_state = 0;
341 return sierra_send_setup(tty, port); 375 return sierra_send_setup(port);
376}
377
378static void sierra_release_urb(struct urb *urb)
379{
380 struct usb_serial_port *port;
381 if (urb) {
382 port = urb->context;
383 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
384 kfree(urb->transfer_buffer);
385 usb_free_urb(urb);
386 }
342} 387}
343 388
344static void sierra_outdat_callback(struct urb *urb) 389static void sierra_outdat_callback(struct urb *urb)
@@ -348,14 +393,14 @@ static void sierra_outdat_callback(struct urb *urb)
348 int status = urb->status; 393 int status = urb->status;
349 unsigned long flags; 394 unsigned long flags;
350 395
351 dev_dbg(&port->dev, "%s - port %d", __func__, port->number); 396 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
352 397
353 /* free up the transfer buffer, as usb_free_urb() does not do this */ 398 /* free up the transfer buffer, as usb_free_urb() does not do this */
354 kfree(urb->transfer_buffer); 399 kfree(urb->transfer_buffer);
355 400
356 if (status) 401 if (status)
357 dev_dbg(&port->dev, "%s - nonzero write bulk status " 402 dev_dbg(&port->dev, "%s - nonzero write bulk status "
358 "received: %d", __func__, status); 403 "received: %d\n", __func__, status);
359 404
360 spin_lock_irqsave(&portdata->lock, flags); 405 spin_lock_irqsave(&portdata->lock, flags);
361 --portdata->outstanding_urbs; 406 --portdata->outstanding_urbs;
@@ -373,50 +418,61 @@ static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
373 unsigned long flags; 418 unsigned long flags;
374 unsigned char *buffer; 419 unsigned char *buffer;
375 struct urb *urb; 420 struct urb *urb;
376 int status; 421 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
422 int retval = 0;
423
424 /* verify that we actually have some data to write */
425 if (count == 0)
426 return 0;
377 427
378 portdata = usb_get_serial_port_data(port); 428 portdata = usb_get_serial_port_data(port);
379 429
380 dev_dbg(&port->dev, "%s: write (%d chars)", __func__, count); 430 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
381 431
382 spin_lock_irqsave(&portdata->lock, flags); 432 spin_lock_irqsave(&portdata->lock, flags);
433 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
434 portdata->outstanding_urbs);
383 if (portdata->outstanding_urbs > N_OUT_URB) { 435 if (portdata->outstanding_urbs > N_OUT_URB) {
384 spin_unlock_irqrestore(&portdata->lock, flags); 436 spin_unlock_irqrestore(&portdata->lock, flags);
385 dev_dbg(&port->dev, "%s - write limit hit\n", __func__); 437 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
386 return 0; 438 return 0;
387 } 439 }
388 portdata->outstanding_urbs++; 440 portdata->outstanding_urbs++;
441 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
442 portdata->outstanding_urbs);
389 spin_unlock_irqrestore(&portdata->lock, flags); 443 spin_unlock_irqrestore(&portdata->lock, flags);
390 444
391 buffer = kmalloc(count, GFP_ATOMIC); 445 buffer = kmalloc(writesize, GFP_ATOMIC);
392 if (!buffer) { 446 if (!buffer) {
393 dev_err(&port->dev, "out of memory\n"); 447 dev_err(&port->dev, "out of memory\n");
394 count = -ENOMEM; 448 retval = -ENOMEM;
395 goto error_no_buffer; 449 goto error_no_buffer;
396 } 450 }
397 451
398 urb = usb_alloc_urb(0, GFP_ATOMIC); 452 urb = usb_alloc_urb(0, GFP_ATOMIC);
399 if (!urb) { 453 if (!urb) {
400 dev_err(&port->dev, "no more free urbs\n"); 454 dev_err(&port->dev, "no more free urbs\n");
401 count = -ENOMEM; 455 retval = -ENOMEM;
402 goto error_no_urb; 456 goto error_no_urb;
403 } 457 }
404 458
405 memcpy(buffer, buf, count); 459 memcpy(buffer, buf, writesize);
406 460
407 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); 461 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
408 462
409 usb_fill_bulk_urb(urb, serial->dev, 463 usb_fill_bulk_urb(urb, serial->dev,
410 usb_sndbulkpipe(serial->dev, 464 usb_sndbulkpipe(serial->dev,
411 port->bulk_out_endpointAddress), 465 port->bulk_out_endpointAddress),
412 buffer, count, sierra_outdat_callback, port); 466 buffer, writesize, sierra_outdat_callback, port);
467
468 /* Handle the need to send a zero length packet */
469 urb->transfer_flags |= URB_ZERO_PACKET;
413 470
414 /* send it down the pipe */ 471 /* send it down the pipe */
415 status = usb_submit_urb(urb, GFP_ATOMIC); 472 retval = usb_submit_urb(urb, GFP_ATOMIC);
416 if (status) { 473 if (retval) {
417 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed " 474 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
418 "with status = %d\n", __func__, status); 475 "with status = %d\n", __func__, retval);
419 count = status;
420 goto error; 476 goto error;
421 } 477 }
422 478
@@ -424,7 +480,7 @@ static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
424 * really free it when it is finished with it */ 480 * really free it when it is finished with it */
425 usb_free_urb(urb); 481 usb_free_urb(urb);
426 482
427 return count; 483 return writesize;
428error: 484error:
429 usb_free_urb(urb); 485 usb_free_urb(urb);
430error_no_urb: 486error_no_urb:
@@ -432,8 +488,10 @@ error_no_urb:
432error_no_buffer: 488error_no_buffer:
433 spin_lock_irqsave(&portdata->lock, flags); 489 spin_lock_irqsave(&portdata->lock, flags);
434 --portdata->outstanding_urbs; 490 --portdata->outstanding_urbs;
491 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
492 portdata->outstanding_urbs);
435 spin_unlock_irqrestore(&portdata->lock, flags); 493 spin_unlock_irqrestore(&portdata->lock, flags);
436 return count; 494 return retval;
437} 495}
438 496
439static void sierra_indat_callback(struct urb *urb) 497static void sierra_indat_callback(struct urb *urb)
@@ -445,33 +503,39 @@ static void sierra_indat_callback(struct urb *urb)
445 unsigned char *data = urb->transfer_buffer; 503 unsigned char *data = urb->transfer_buffer;
446 int status = urb->status; 504 int status = urb->status;
447 505
448 dbg("%s: %p", __func__, urb);
449
450 endpoint = usb_pipeendpoint(urb->pipe); 506 endpoint = usb_pipeendpoint(urb->pipe);
451 port = urb->context; 507 port = urb->context;
508
509 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
452 510
453 if (status) { 511 if (status) {
454 dev_dbg(&port->dev, "%s: nonzero status: %d on" 512 dev_dbg(&port->dev, "%s: nonzero status: %d on"
455 " endpoint %02x.", __func__, status, endpoint); 513 " endpoint %02x\n", __func__, status, endpoint);
456 } else { 514 } else {
457 if (urb->actual_length) { 515 if (urb->actual_length) {
458 tty = tty_port_tty_get(&port->port); 516 tty = tty_port_tty_get(&port->port);
517
459 tty_buffer_request_room(tty, urb->actual_length); 518 tty_buffer_request_room(tty, urb->actual_length);
460 tty_insert_flip_string(tty, data, urb->actual_length); 519 tty_insert_flip_string(tty, data, urb->actual_length);
461 tty_flip_buffer_push(tty); 520 tty_flip_buffer_push(tty);
521
462 tty_kref_put(tty); 522 tty_kref_put(tty);
463 } else 523 usb_serial_debug_data(debug, &port->dev, __func__,
524 urb->actual_length, data);
525 } else {
464 dev_dbg(&port->dev, "%s: empty read urb" 526 dev_dbg(&port->dev, "%s: empty read urb"
465 " received", __func__); 527 " received\n", __func__);
466
467 /* Resubmit urb so we continue receiving */
468 if (port->port.count && status != -ESHUTDOWN) {
469 err = usb_submit_urb(urb, GFP_ATOMIC);
470 if (err)
471 dev_err(&port->dev, "resubmit read urb failed."
472 "(%d)\n", err);
473 } 528 }
474 } 529 }
530
531 /* Resubmit urb so we continue receiving */
532 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
533 err = usb_submit_urb(urb, GFP_ATOMIC);
534 if (err)
535 dev_err(&port->dev, "resubmit read urb failed."
536 "(%d)\n", err);
537 }
538
475 return; 539 return;
476} 540}
477 541
@@ -483,8 +547,7 @@ static void sierra_instat_callback(struct urb *urb)
483 struct sierra_port_private *portdata = usb_get_serial_port_data(port); 547 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
484 struct usb_serial *serial = port->serial; 548 struct usb_serial *serial = port->serial;
485 549
486 dev_dbg(&port->dev, "%s", __func__); 550 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
487 dev_dbg(&port->dev, "%s: urb %p port %p has data %p", __func__,
488 urb, port, portdata); 551 urb, port, portdata);
489 552
490 if (status == 0) { 553 if (status == 0) {
@@ -504,7 +567,7 @@ static void sierra_instat_callback(struct urb *urb)
504 sizeof(struct usb_ctrlrequest)); 567 sizeof(struct usb_ctrlrequest));
505 struct tty_struct *tty; 568 struct tty_struct *tty;
506 569
507 dev_dbg(&port->dev, "%s: signal x%x", __func__, 570 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
508 signals); 571 signals);
509 572
510 old_dcd_state = portdata->dcd_state; 573 old_dcd_state = portdata->dcd_state;
@@ -519,20 +582,20 @@ static void sierra_instat_callback(struct urb *urb)
519 tty_hangup(tty); 582 tty_hangup(tty);
520 tty_kref_put(tty); 583 tty_kref_put(tty);
521 } else { 584 } else {
522 dev_dbg(&port->dev, "%s: type %x req %x", 585 dev_dbg(&port->dev, "%s: type %x req %x\n",
523 __func__, req_pkt->bRequestType, 586 __func__, req_pkt->bRequestType,
524 req_pkt->bRequest); 587 req_pkt->bRequest);
525 } 588 }
526 } else 589 } else
527 dev_dbg(&port->dev, "%s: error %d", __func__, status); 590 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
528 591
529 /* Resubmit urb so we continue receiving IRQ data */ 592 /* Resubmit urb so we continue receiving IRQ data */
530 if (status != -ESHUTDOWN) { 593 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
531 urb->dev = serial->dev; 594 urb->dev = serial->dev;
532 err = usb_submit_urb(urb, GFP_ATOMIC); 595 err = usb_submit_urb(urb, GFP_ATOMIC);
533 if (err) 596 if (err)
534 dev_dbg(&port->dev, "%s: resubmit intr urb " 597 dev_err(&port->dev, "%s: resubmit intr urb "
535 "failed. (%d)", __func__, err); 598 "failed. (%d)\n", __func__, err);
536 } 599 }
537} 600}
538 601
@@ -542,7 +605,7 @@ static int sierra_write_room(struct tty_struct *tty)
542 struct sierra_port_private *portdata = usb_get_serial_port_data(port); 605 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
543 unsigned long flags; 606 unsigned long flags;
544 607
545 dev_dbg(&port->dev, "%s - port %d", __func__, port->number); 608 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
546 609
547 /* try to give a good number back based on if we have any free urbs at 610 /* try to give a good number back based on if we have any free urbs at
548 * this point in time */ 611 * this point in time */
@@ -557,67 +620,99 @@ static int sierra_write_room(struct tty_struct *tty)
557 return 2048; 620 return 2048;
558} 621}
559 622
560static int sierra_open(struct tty_struct *tty, 623static void sierra_stop_rx_urbs(struct usb_serial_port *port)
561 struct usb_serial_port *port, struct file *filp)
562{ 624{
563 struct sierra_port_private *portdata;
564 struct usb_serial *serial = port->serial;
565 int i; 625 int i;
566 struct urb *urb; 626 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
567 int result;
568 627
569 portdata = usb_get_serial_port_data(port); 628 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
629 usb_kill_urb(portdata->in_urbs[i]);
570 630
571 dev_dbg(&port->dev, "%s", __func__); 631 usb_kill_urb(port->interrupt_in_urb);
632}
572 633
573 /* Set some sane defaults */ 634static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
574 portdata->rts_state = 1; 635{
575 portdata->dtr_state = 1; 636 int ok_cnt;
637 int err = -EINVAL;
638 int i;
639 struct urb *urb;
640 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
576 641
577 /* Reset low level data toggle and start reading from endpoints */ 642 ok_cnt = 0;
578 for (i = 0; i < N_IN_URB; i++) { 643 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
579 urb = portdata->in_urbs[i]; 644 urb = portdata->in_urbs[i];
580 if (!urb) 645 if (!urb)
581 continue; 646 continue;
582 if (urb->dev != serial->dev) { 647 err = usb_submit_urb(urb, mem_flags);
583 dev_dbg(&port->dev, "%s: dev %p != %p", 648 if (err) {
584 __func__, urb->dev, serial->dev); 649 dev_err(&port->dev, "%s: submit urb failed: %d\n",
585 continue; 650 __func__, err);
651 } else {
652 ok_cnt++;
586 } 653 }
654 }
587 655
588 /* 656 if (ok_cnt && port->interrupt_in_urb) {
589 * make sure endpoint data toggle is synchronized with the 657 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
590 * device 658 if (err) {
591 */ 659 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
592 usb_clear_halt(urb->dev, urb->pipe); 660 __func__, err);
593
594 result = usb_submit_urb(urb, GFP_KERNEL);
595 if (result) {
596 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
597 i, result, urb->transfer_buffer_length);
598 } 661 }
599 } 662 }
600 663
601 sierra_send_setup(tty, port); 664 if (ok_cnt > 0) /* at least one rx urb submitted */
665 return 0;
666 else
667 return err;
668}
602 669
603 /* start up the interrupt endpoint if we have one */ 670static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
604 if (port->interrupt_in_urb) { 671 int dir, void *ctx, int len,
605 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 672 gfp_t mem_flags,
606 if (result) 673 usb_complete_t callback)
607 dev_err(&port->dev, "submit irq_in urb failed %d\n", 674{
608 result); 675 struct urb *urb;
676 u8 *buf;
677
678 if (endpoint == -1)
679 return NULL;
680
681 urb = usb_alloc_urb(0, mem_flags);
682 if (urb == NULL) {
683 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
684 __func__, endpoint);
685 return NULL;
609 } 686 }
610 return 0; 687
688 buf = kmalloc(len, mem_flags);
689 if (buf) {
690 /* Fill URB using supplied data */
691 usb_fill_bulk_urb(urb, serial->dev,
692 usb_sndbulkpipe(serial->dev, endpoint) | dir,
693 buf, len, callback, ctx);
694
695 /* debug */
696 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
697 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
698 } else {
699 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
700 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
701
702 sierra_release_urb(urb);
703 urb = NULL;
704 }
705
706 return urb;
611} 707}
612 708
613static void sierra_close(struct tty_struct *tty, 709static void sierra_close(struct usb_serial_port *port)
614 struct usb_serial_port *port, struct file *filp)
615{ 710{
616 int i; 711 int i;
617 struct usb_serial *serial = port->serial; 712 struct usb_serial *serial = port->serial;
618 struct sierra_port_private *portdata; 713 struct sierra_port_private *portdata;
619 714
620 dev_dbg(&port->dev, "%s", __func__); 715 dev_dbg(&port->dev, "%s\n", __func__);
621 portdata = usb_get_serial_port_data(port); 716 portdata = usb_get_serial_port_data(port);
622 717
623 portdata->rts_state = 0; 718 portdata->rts_state = 0;
@@ -626,27 +721,85 @@ static void sierra_close(struct tty_struct *tty,
626 if (serial->dev) { 721 if (serial->dev) {
627 mutex_lock(&serial->disc_mutex); 722 mutex_lock(&serial->disc_mutex);
628 if (!serial->disconnected) 723 if (!serial->disconnected)
629 sierra_send_setup(tty, port); 724 sierra_send_setup(port);
630 mutex_unlock(&serial->disc_mutex); 725 mutex_unlock(&serial->disc_mutex);
631 726
632 /* Stop reading/writing urbs */ 727 /* Stop reading urbs */
633 for (i = 0; i < N_IN_URB; i++) 728 sierra_stop_rx_urbs(port);
634 usb_kill_urb(portdata->in_urbs[i]); 729 /* .. and release them */
730 for (i = 0; i < N_IN_URB; i++) {
731 sierra_release_urb(portdata->in_urbs[i]);
732 portdata->in_urbs[i] = NULL;
733 }
635 } 734 }
735}
636 736
637 usb_kill_urb(port->interrupt_in_urb); 737static int sierra_open(struct tty_struct *tty,
638 tty_port_tty_set(&port->port, NULL); 738 struct usb_serial_port *port, struct file *filp)
739{
740 struct sierra_port_private *portdata;
741 struct usb_serial *serial = port->serial;
742 int i;
743 int err;
744 int endpoint;
745 struct urb *urb;
746
747 portdata = usb_get_serial_port_data(port);
748
749 dev_dbg(&port->dev, "%s\n", __func__);
750
751 /* Set some sane defaults */
752 portdata->rts_state = 1;
753 portdata->dtr_state = 1;
754
755
756 endpoint = port->bulk_in_endpointAddress;
757 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
758 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
759 IN_BUFLEN, GFP_KERNEL,
760 sierra_indat_callback);
761 portdata->in_urbs[i] = urb;
762 }
763 /* clear halt condition */
764 usb_clear_halt(serial->dev,
765 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
766
767 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
768 if (err) {
769 /* get rid of everything as in close */
770 sierra_close(port);
771 return err;
772 }
773 sierra_send_setup(port);
774
775 return 0;
776}
777
778
779static void sierra_dtr_rts(struct usb_serial_port *port, int on)
780{
781 struct usb_serial *serial = port->serial;
782 struct sierra_port_private *portdata;
783
784 portdata = usb_get_serial_port_data(port);
785 portdata->rts_state = on;
786 portdata->dtr_state = on;
787
788 if (serial->dev) {
789 mutex_lock(&serial->disc_mutex);
790 if (!serial->disconnected)
791 sierra_send_setup(port);
792 mutex_unlock(&serial->disc_mutex);
793 }
639} 794}
640 795
641static int sierra_startup(struct usb_serial *serial) 796static int sierra_startup(struct usb_serial *serial)
642{ 797{
643 struct usb_serial_port *port; 798 struct usb_serial_port *port;
644 struct sierra_port_private *portdata; 799 struct sierra_port_private *portdata;
645 struct urb *urb;
646 int i; 800 int i;
647 int j;
648 801
649 dev_dbg(&serial->dev->dev, "%s", __func__); 802 dev_dbg(&serial->dev->dev, "%s\n", __func__);
650 803
651 /* Set Device mode to D0 */ 804 /* Set Device mode to D0 */
652 sierra_set_power_state(serial->dev, 0x0000); 805 sierra_set_power_state(serial->dev, 0x0000);
@@ -661,51 +814,25 @@ static int sierra_startup(struct usb_serial *serial)
661 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 814 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
662 if (!portdata) { 815 if (!portdata) {
663 dev_dbg(&port->dev, "%s: kmalloc for " 816 dev_dbg(&port->dev, "%s: kmalloc for "
664 "sierra_port_private (%d) failed!.", 817 "sierra_port_private (%d) failed!.\n",
665 __func__, i); 818 __func__, i);
666 return -ENOMEM; 819 return -ENOMEM;
667 } 820 }
668 spin_lock_init(&portdata->lock); 821 spin_lock_init(&portdata->lock);
669 for (j = 0; j < N_IN_URB; j++) { 822 /* Set the port private data pointer */
670 portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
671 if (!portdata->in_buffer[j]) {
672 for (--j; j >= 0; j--)
673 kfree(portdata->in_buffer[j]);
674 kfree(portdata);
675 return -ENOMEM;
676 }
677 }
678
679 usb_set_serial_port_data(port, portdata); 823 usb_set_serial_port_data(port, portdata);
680
681 /* initialize the in urbs */
682 for (j = 0; j < N_IN_URB; ++j) {
683 urb = usb_alloc_urb(0, GFP_KERNEL);
684 if (urb == NULL) {
685 dev_dbg(&port->dev, "%s: alloc for in "
686 "port failed.", __func__);
687 continue;
688 }
689 /* Fill URB using supplied data. */
690 usb_fill_bulk_urb(urb, serial->dev,
691 usb_rcvbulkpipe(serial->dev,
692 port->bulk_in_endpointAddress),
693 portdata->in_buffer[j], IN_BUFLEN,
694 sierra_indat_callback, port);
695 portdata->in_urbs[j] = urb;
696 }
697 } 824 }
698 825
699 return 0; 826 return 0;
700} 827}
701 828
702static void sierra_shutdown(struct usb_serial *serial) 829static void sierra_release(struct usb_serial *serial)
703{ 830{
704 int i, j; 831 int i;
705 struct usb_serial_port *port; 832 struct usb_serial_port *port;
706 struct sierra_port_private *portdata; 833 struct sierra_port_private *portdata;
707 834
708 dev_dbg(&serial->dev->dev, "%s", __func__); 835 dev_dbg(&serial->dev->dev, "%s\n", __func__);
709 836
710 for (i = 0; i < serial->num_ports; ++i) { 837 for (i = 0; i < serial->num_ports; ++i) {
711 port = serial->port[i]; 838 port = serial->port[i];
@@ -714,14 +841,7 @@ static void sierra_shutdown(struct usb_serial *serial)
714 portdata = usb_get_serial_port_data(port); 841 portdata = usb_get_serial_port_data(port);
715 if (!portdata) 842 if (!portdata)
716 continue; 843 continue;
717
718 for (j = 0; j < N_IN_URB; j++) {
719 usb_kill_urb(portdata->in_urbs[j]);
720 usb_free_urb(portdata->in_urbs[j]);
721 kfree(portdata->in_buffer[j]);
722 }
723 kfree(portdata); 844 kfree(portdata);
724 usb_set_serial_port_data(port, NULL);
725 } 845 }
726} 846}
727 847
@@ -737,13 +857,14 @@ static struct usb_serial_driver sierra_device = {
737 .probe = sierra_probe, 857 .probe = sierra_probe,
738 .open = sierra_open, 858 .open = sierra_open,
739 .close = sierra_close, 859 .close = sierra_close,
860 .dtr_rts = sierra_dtr_rts,
740 .write = sierra_write, 861 .write = sierra_write,
741 .write_room = sierra_write_room, 862 .write_room = sierra_write_room,
742 .set_termios = sierra_set_termios, 863 .set_termios = sierra_set_termios,
743 .tiocmget = sierra_tiocmget, 864 .tiocmget = sierra_tiocmget,
744 .tiocmset = sierra_tiocmset, 865 .tiocmset = sierra_tiocmset,
745 .attach = sierra_startup, 866 .attach = sierra_startup,
746 .shutdown = sierra_shutdown, 867 .release = sierra_release,
747 .read_int_callback = sierra_instat_callback, 868 .read_int_callback = sierra_instat_callback,
748}; 869};
749 870
diff --git a/drivers/usb/serial/spcp8x5.c b/drivers/usb/serial/spcp8x5.c
index 5e7528cc81a8..3c249d8e8b8e 100644
--- a/drivers/usb/serial/spcp8x5.c
+++ b/drivers/usb/serial/spcp8x5.c
@@ -356,7 +356,7 @@ cleanup:
356} 356}
357 357
358/* call when the device plug out. free all the memory alloced by probe */ 358/* call when the device plug out. free all the memory alloced by probe */
359static void spcp8x5_shutdown(struct usb_serial *serial) 359static void spcp8x5_release(struct usb_serial *serial)
360{ 360{
361 int i; 361 int i;
362 struct spcp8x5_private *priv; 362 struct spcp8x5_private *priv;
@@ -366,7 +366,6 @@ static void spcp8x5_shutdown(struct usb_serial *serial)
366 if (priv) { 366 if (priv) {
367 free_ringbuf(priv->buf); 367 free_ringbuf(priv->buf);
368 kfree(priv); 368 kfree(priv);
369 usb_set_serial_port_data(serial->port[i] , NULL);
370 } 369 }
371 } 370 }
372} 371}
@@ -446,66 +445,47 @@ static void spcp8x5_set_workMode(struct usb_device *dev, u16 value,
446 "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret); 445 "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret);
447} 446}
448 447
448static int spcp8x5_carrier_raised(struct usb_serial_port *port)
449{
450 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
451 if (priv->line_status & MSR_STATUS_LINE_DCD)
452 return 1;
453 return 0;
454}
455
456static void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
457{
458 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
459 unsigned long flags;
460 u8 control;
461
462 spin_lock_irqsave(&priv->lock, flags);
463 if (on)
464 priv->line_control = MCR_CONTROL_LINE_DTR
465 | MCR_CONTROL_LINE_RTS;
466 else
467 priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
468 | MCR_CONTROL_LINE_RTS);
469 control = priv->line_control;
470 spin_unlock_irqrestore(&priv->lock, flags);
471 spcp8x5_set_ctrlLine(port->serial->dev, control , priv->type);
472}
473
449/* close the serial port. We should wait for data sending to device 1st and 474/* close the serial port. We should wait for data sending to device 1st and
450 * then kill all urb. */ 475 * then kill all urb. */
451static void spcp8x5_close(struct tty_struct *tty, 476static void spcp8x5_close(struct usb_serial_port *port)
452 struct usb_serial_port *port, struct file *filp)
453{ 477{
454 struct spcp8x5_private *priv = usb_get_serial_port_data(port); 478 struct spcp8x5_private *priv = usb_get_serial_port_data(port);
455 unsigned long flags; 479 unsigned long flags;
456 unsigned int c_cflag;
457 int bps;
458 long timeout;
459 wait_queue_t wait;
460 int result; 480 int result;
461 481
462 dbg("%s - port %d", __func__, port->number); 482 dbg("%s - port %d", __func__, port->number);
463 483
464 /* wait for data to drain from the buffer */
465 spin_lock_irqsave(&priv->lock, flags); 484 spin_lock_irqsave(&priv->lock, flags);
466 timeout = SPCP8x5_CLOSING_WAIT;
467 init_waitqueue_entry(&wait, current);
468 add_wait_queue(&tty->write_wait, &wait);
469 for (;;) {
470 set_current_state(TASK_INTERRUPTIBLE);
471 if (ringbuf_avail_data(priv->buf) == 0 ||
472 timeout == 0 || signal_pending(current))
473 break;
474 spin_unlock_irqrestore(&priv->lock, flags);
475 timeout = schedule_timeout(timeout);
476 spin_lock_irqsave(&priv->lock, flags);
477 }
478 set_current_state(TASK_RUNNING);
479 remove_wait_queue(&tty->write_wait, &wait);
480
481 /* clear out any remaining data in the buffer */ 485 /* clear out any remaining data in the buffer */
482 clear_ringbuf(priv->buf); 486 clear_ringbuf(priv->buf);
483 spin_unlock_irqrestore(&priv->lock, flags); 487 spin_unlock_irqrestore(&priv->lock, flags);
484 488
485 /* wait for characters to drain from the device (this is long enough
486 * for the entire all byte spcp8x5 hardware buffer to drain with no
487 * flow control for data rates of 1200 bps or more, for lower rates we
488 * should really know how much data is in the buffer to compute a delay
489 * that is not unnecessarily long) */
490 bps = tty_get_baud_rate(tty);
491 if (bps > 1200)
492 timeout = max((HZ*2560) / bps, HZ/10);
493 else
494 timeout = 2*HZ;
495 set_current_state(TASK_INTERRUPTIBLE);
496 schedule_timeout(timeout);
497
498 /* clear control lines */
499 if (tty) {
500 c_cflag = tty->termios->c_cflag;
501 if (c_cflag & HUPCL) {
502 spin_lock_irqsave(&priv->lock, flags);
503 priv->line_control = 0;
504 spin_unlock_irqrestore(&priv->lock, flags);
505 spcp8x5_set_ctrlLine(port->serial->dev, 0 , priv->type);
506 }
507 }
508
509 /* kill urb */ 489 /* kill urb */
510 if (port->write_urb != NULL) { 490 if (port->write_urb != NULL) {
511 result = usb_unlink_urb(port->write_urb); 491 result = usb_unlink_urb(port->write_urb);
@@ -665,13 +645,6 @@ static int spcp8x5_open(struct tty_struct *tty,
665 if (ret) 645 if (ret)
666 return ret; 646 return ret;
667 647
668 spin_lock_irqsave(&priv->lock, flags);
669 if (tty && (tty->termios->c_cflag & CBAUD))
670 priv->line_control = MCR_DTR | MCR_RTS;
671 else
672 priv->line_control = 0;
673 spin_unlock_irqrestore(&priv->lock, flags);
674
675 spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type); 648 spcp8x5_set_ctrlLine(serial->dev, priv->line_control , priv->type);
676 649
677 /* Setup termios */ 650 /* Setup termios */
@@ -691,9 +664,10 @@ static int spcp8x5_open(struct tty_struct *tty,
691 port->read_urb->dev = serial->dev; 664 port->read_urb->dev = serial->dev;
692 ret = usb_submit_urb(port->read_urb, GFP_KERNEL); 665 ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
693 if (ret) { 666 if (ret) {
694 spcp8x5_close(tty, port, NULL); 667 spcp8x5_close(port);
695 return -EPROTO; 668 return -EPROTO;
696 } 669 }
670 port->port.drain_delay = 256;
697 return 0; 671 return 0;
698} 672}
699 673
@@ -1033,6 +1007,8 @@ static struct usb_serial_driver spcp8x5_device = {
1033 .num_ports = 1, 1007 .num_ports = 1,
1034 .open = spcp8x5_open, 1008 .open = spcp8x5_open,
1035 .close = spcp8x5_close, 1009 .close = spcp8x5_close,
1010 .dtr_rts = spcp8x5_dtr_rts,
1011 .carrier_raised = spcp8x5_carrier_raised,
1036 .write = spcp8x5_write, 1012 .write = spcp8x5_write,
1037 .set_termios = spcp8x5_set_termios, 1013 .set_termios = spcp8x5_set_termios,
1038 .ioctl = spcp8x5_ioctl, 1014 .ioctl = spcp8x5_ioctl,
@@ -1043,7 +1019,7 @@ static struct usb_serial_driver spcp8x5_device = {
1043 .write_bulk_callback = spcp8x5_write_bulk_callback, 1019 .write_bulk_callback = spcp8x5_write_bulk_callback,
1044 .chars_in_buffer = spcp8x5_chars_in_buffer, 1020 .chars_in_buffer = spcp8x5_chars_in_buffer,
1045 .attach = spcp8x5_startup, 1021 .attach = spcp8x5_startup,
1046 .shutdown = spcp8x5_shutdown, 1022 .release = spcp8x5_release,
1047}; 1023};
1048 1024
1049static int __init spcp8x5_init(void) 1025static int __init spcp8x5_init(void)
diff --git a/drivers/usb/serial/symbolserial.c b/drivers/usb/serial/symbolserial.c
index 69879e437940..6157fac9366b 100644
--- a/drivers/usb/serial/symbolserial.c
+++ b/drivers/usb/serial/symbolserial.c
@@ -152,8 +152,7 @@ static int symbol_open(struct tty_struct *tty, struct usb_serial_port *port,
152 return result; 152 return result;
153} 153}
154 154
155static void symbol_close(struct tty_struct *tty, struct usb_serial_port *port, 155static void symbol_close(struct usb_serial_port *port)
156 struct file *filp)
157{ 156{
158 struct symbol_private *priv = usb_get_serial_data(port->serial); 157 struct symbol_private *priv = usb_get_serial_data(port->serial);
159 158
@@ -268,7 +267,7 @@ error:
268 return retval; 267 return retval;
269} 268}
270 269
271static void symbol_shutdown(struct usb_serial *serial) 270static void symbol_disconnect(struct usb_serial *serial)
272{ 271{
273 struct symbol_private *priv = usb_get_serial_data(serial); 272 struct symbol_private *priv = usb_get_serial_data(serial);
274 273
@@ -276,9 +275,16 @@ static void symbol_shutdown(struct usb_serial *serial)
276 275
277 usb_kill_urb(priv->int_urb); 276 usb_kill_urb(priv->int_urb);
278 usb_free_urb(priv->int_urb); 277 usb_free_urb(priv->int_urb);
278}
279
280static void symbol_release(struct usb_serial *serial)
281{
282 struct symbol_private *priv = usb_get_serial_data(serial);
283
284 dbg("%s", __func__);
285
279 kfree(priv->int_buffer); 286 kfree(priv->int_buffer);
280 kfree(priv); 287 kfree(priv);
281 usb_set_serial_data(serial, NULL);
282} 288}
283 289
284static struct usb_driver symbol_driver = { 290static struct usb_driver symbol_driver = {
@@ -300,7 +306,8 @@ static struct usb_serial_driver symbol_device = {
300 .attach = symbol_startup, 306 .attach = symbol_startup,
301 .open = symbol_open, 307 .open = symbol_open,
302 .close = symbol_close, 308 .close = symbol_close,
303 .shutdown = symbol_shutdown, 309 .disconnect = symbol_disconnect,
310 .release = symbol_release,
304 .throttle = symbol_throttle, 311 .throttle = symbol_throttle,
305 .unthrottle = symbol_unthrottle, 312 .unthrottle = symbol_unthrottle,
306}; 313};
diff --git a/drivers/usb/serial/ti_usb_3410_5052.c b/drivers/usb/serial/ti_usb_3410_5052.c
index 0a64bac306ee..3bc609fe2242 100644
--- a/drivers/usb/serial/ti_usb_3410_5052.c
+++ b/drivers/usb/serial/ti_usb_3410_5052.c
@@ -97,11 +97,10 @@ struct ti_device {
97/* Function Declarations */ 97/* Function Declarations */
98 98
99static int ti_startup(struct usb_serial *serial); 99static int ti_startup(struct usb_serial *serial);
100static void ti_shutdown(struct usb_serial *serial); 100static void ti_release(struct usb_serial *serial);
101static int ti_open(struct tty_struct *tty, struct usb_serial_port *port, 101static int ti_open(struct tty_struct *tty, struct usb_serial_port *port,
102 struct file *file); 102 struct file *file);
103static void ti_close(struct tty_struct *tty, struct usb_serial_port *port, 103static void ti_close(struct usb_serial_port *port);
104 struct file *file);
105static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, 104static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
106 const unsigned char *data, int count); 105 const unsigned char *data, int count);
107static int ti_write_room(struct tty_struct *tty); 106static int ti_write_room(struct tty_struct *tty);
@@ -192,7 +191,6 @@ static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
192 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, 191 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
193 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, 192 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
194 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, 193 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
195 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
196}; 194};
197 195
198static struct usb_device_id ti_id_table_combined[14+2*TI_EXTRA_VID_PID_COUNT+1] = { 196static struct usb_device_id ti_id_table_combined[14+2*TI_EXTRA_VID_PID_COUNT+1] = {
@@ -231,7 +229,7 @@ static struct usb_serial_driver ti_1port_device = {
231 .id_table = ti_id_table_3410, 229 .id_table = ti_id_table_3410,
232 .num_ports = 1, 230 .num_ports = 1,
233 .attach = ti_startup, 231 .attach = ti_startup,
234 .shutdown = ti_shutdown, 232 .release = ti_release,
235 .open = ti_open, 233 .open = ti_open,
236 .close = ti_close, 234 .close = ti_close,
237 .write = ti_write, 235 .write = ti_write,
@@ -259,7 +257,7 @@ static struct usb_serial_driver ti_2port_device = {
259 .id_table = ti_id_table_5052, 257 .id_table = ti_id_table_5052,
260 .num_ports = 2, 258 .num_ports = 2,
261 .attach = ti_startup, 259 .attach = ti_startup,
262 .shutdown = ti_shutdown, 260 .release = ti_release,
263 .open = ti_open, 261 .open = ti_open,
264 .close = ti_close, 262 .close = ti_close,
265 .write = ti_write, 263 .write = ti_write,
@@ -474,7 +472,7 @@ free_tdev:
474} 472}
475 473
476 474
477static void ti_shutdown(struct usb_serial *serial) 475static void ti_release(struct usb_serial *serial)
478{ 476{
479 int i; 477 int i;
480 struct ti_device *tdev = usb_get_serial_data(serial); 478 struct ti_device *tdev = usb_get_serial_data(serial);
@@ -487,12 +485,10 @@ static void ti_shutdown(struct usb_serial *serial)
487 if (tport) { 485 if (tport) {
488 ti_buf_free(tport->tp_write_buf); 486 ti_buf_free(tport->tp_write_buf);
489 kfree(tport); 487 kfree(tport);
490 usb_set_serial_port_data(serial->port[i], NULL);
491 } 488 }
492 } 489 }
493 490
494 kfree(tdev); 491 kfree(tdev);
495 usb_set_serial_data(serial, NULL);
496} 492}
497 493
498 494
@@ -647,8 +643,7 @@ release_lock:
647} 643}
648 644
649 645
650static void ti_close(struct tty_struct *tty, struct usb_serial_port *port, 646static void ti_close(struct usb_serial_port *port)
651 struct file *file)
652{ 647{
653 struct ti_device *tdev; 648 struct ti_device *tdev;
654 struct ti_port *tport; 649 struct ti_port *tport;
@@ -732,7 +727,7 @@ static int ti_write_room(struct tty_struct *tty)
732 dbg("%s - port %d", __func__, port->number); 727 dbg("%s - port %d", __func__, port->number);
733 728
734 if (tport == NULL) 729 if (tport == NULL)
735 return -ENODEV; 730 return 0;
736 731
737 spin_lock_irqsave(&tport->tp_lock, flags); 732 spin_lock_irqsave(&tport->tp_lock, flags);
738 room = ti_buf_space_avail(tport->tp_write_buf); 733 room = ti_buf_space_avail(tport->tp_write_buf);
@@ -753,7 +748,7 @@ static int ti_chars_in_buffer(struct tty_struct *tty)
753 dbg("%s - port %d", __func__, port->number); 748 dbg("%s - port %d", __func__, port->number);
754 749
755 if (tport == NULL) 750 if (tport == NULL)
756 return -ENODEV; 751 return 0;
757 752
758 spin_lock_irqsave(&tport->tp_lock, flags); 753 spin_lock_irqsave(&tport->tp_lock, flags);
759 chars = ti_buf_data_avail(tport->tp_write_buf); 754 chars = ti_buf_data_avail(tport->tp_write_buf);
@@ -1662,7 +1657,7 @@ static int ti_do_download(struct usb_device *dev, int pipe,
1662 u8 cs = 0; 1657 u8 cs = 0;
1663 int done; 1658 int done;
1664 struct ti_firmware_header *header; 1659 struct ti_firmware_header *header;
1665 int status; 1660 int status = 0;
1666 int len; 1661 int len;
1667 1662
1668 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++) 1663 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index f331e2bde88a..bd7581b3a48a 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -21,6 +21,7 @@
21#include <linux/errno.h> 21#include <linux/errno.h>
22#include <linux/init.h> 22#include <linux/init.h>
23#include <linux/slab.h> 23#include <linux/slab.h>
24#include <linux/smp_lock.h>
24#include <linux/tty.h> 25#include <linux/tty.h>
25#include <linux/tty_driver.h> 26#include <linux/tty_driver.h>
26#include <linux/tty_flip.h> 27#include <linux/tty_flip.h>
@@ -141,6 +142,14 @@ static void destroy_serial(struct kref *kref)
141 if (serial->minor != SERIAL_TTY_NO_MINOR) 142 if (serial->minor != SERIAL_TTY_NO_MINOR)
142 return_serial(serial); 143 return_serial(serial);
143 144
145 serial->type->release(serial);
146
147 for (i = 0; i < serial->num_ports; ++i) {
148 port = serial->port[i];
149 if (port)
150 put_device(&port->dev);
151 }
152
144 /* If this is a "fake" port, we have to clean it up here, as it will 153 /* If this is a "fake" port, we have to clean it up here, as it will
145 * not get cleaned up in port_release() as it was never registered with 154 * not get cleaned up in port_release() as it was never registered with
146 * the driver core */ 155 * the driver core */
@@ -148,9 +157,8 @@ static void destroy_serial(struct kref *kref)
148 for (i = serial->num_ports; 157 for (i = serial->num_ports;
149 i < serial->num_port_pointers; ++i) { 158 i < serial->num_port_pointers; ++i) {
150 port = serial->port[i]; 159 port = serial->port[i];
151 if (!port) 160 if (port)
152 continue; 161 port_free(port);
153 port_free(port);
154 } 162 }
155 } 163 }
156 164
@@ -213,7 +221,8 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
213 tty->driver_data = port; 221 tty->driver_data = port;
214 tty_port_tty_set(&port->port, tty); 222 tty_port_tty_set(&port->port, tty);
215 223
216 if (port->port.count == 1) { 224 /* If the console is attached, the device is already open */
225 if (port->port.count == 1 && !port->console) {
217 226
218 /* lock this module before we call it 227 /* lock this module before we call it
219 * this may fail, which means we must bail out, 228 * this may fail, which means we must bail out,
@@ -238,9 +247,11 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
238 goto bailout_interface_put; 247 goto bailout_interface_put;
239 mutex_unlock(&serial->disc_mutex); 248 mutex_unlock(&serial->disc_mutex);
240 } 249 }
241
242 mutex_unlock(&port->mutex); 250 mutex_unlock(&port->mutex);
243 return 0; 251 /* Now do the correct tty layer semantics */
252 retval = tty_port_block_til_ready(&port->port, tty, filp);
253 if (retval == 0)
254 return 0;
244 255
245bailout_interface_put: 256bailout_interface_put:
246 usb_autopm_put_interface(serial->interface); 257 usb_autopm_put_interface(serial->interface);
@@ -259,64 +270,92 @@ bailout_serial_put:
259 return retval; 270 return retval;
260} 271}
261 272
262static void serial_close(struct tty_struct *tty, struct file *filp) 273/**
274 * serial_do_down - shut down hardware
275 * @port: port to shut down
276 *
277 * Shut down a USB port unless it is the console. We never shut down the
278 * console hardware as it will always be in use.
279 *
280 * Don't free any resources at this point
281 */
282static void serial_do_down(struct usb_serial_port *port)
263{ 283{
264 struct usb_serial_port *port = tty->driver_data; 284 struct usb_serial_driver *drv = port->serial->type;
265 struct usb_serial *serial; 285 struct usb_serial *serial;
266 struct module *owner; 286 struct module *owner;
267 int count;
268 287
269 if (!port) 288 /* The console is magical, do not hang up the console hardware
289 or there will be tears */
290 if (port->console)
270 return; 291 return;
271 292
272 dbg("%s - port %d", __func__, port->number);
273
274 mutex_lock(&port->mutex); 293 mutex_lock(&port->mutex);
275 serial = port->serial; 294 serial = port->serial;
276 owner = serial->type->driver.owner; 295 owner = serial->type->driver.owner;
277 296
278 if (port->port.count == 0) { 297 if (drv->close)
279 mutex_unlock(&port->mutex); 298 drv->close(port);
280 return;
281 }
282 299
283 if (port->port.count == 1)
284 /* only call the device specific close if this
285 * port is being closed by the last owner. Ensure we do
286 * this before we drop the port count. The call is protected
287 * by the port mutex
288 */
289 serial->type->close(tty, port, filp);
290
291 if (port->port.count == (port->console ? 2 : 1)) {
292 struct tty_struct *tty = tty_port_tty_get(&port->port);
293 if (tty) {
294 /* We must do this before we drop the port count to
295 zero. */
296 if (tty->driver_data)
297 tty->driver_data = NULL;
298 tty_port_tty_set(&port->port, NULL);
299 tty_kref_put(tty);
300 }
301 }
302
303 --port->port.count;
304 count = port->port.count;
305 mutex_unlock(&port->mutex); 300 mutex_unlock(&port->mutex);
306 put_device(&port->dev); 301}
302
303/**
304 * serial_do_free - free resources post close/hangup
305 * @port: port to free up
306 *
307 * Do the resource freeing and refcount dropping for the port. We must
308 * be careful about ordering and we must avoid freeing up the console.
309 */
310
311static void serial_do_free(struct usb_serial_port *port)
312{
313 struct usb_serial *serial;
314 struct module *owner;
307 315
316 /* The console is magical, do not hang up the console hardware
317 or there will be tears */
318 if (port->console)
319 return;
320
321 serial = port->serial;
322 owner = serial->type->driver.owner;
323 put_device(&port->dev);
308 /* Mustn't dereference port any more */ 324 /* Mustn't dereference port any more */
309 if (count == 0) { 325 mutex_lock(&serial->disc_mutex);
310 mutex_lock(&serial->disc_mutex); 326 if (!serial->disconnected)
311 if (!serial->disconnected) 327 usb_autopm_put_interface(serial->interface);
312 usb_autopm_put_interface(serial->interface); 328 mutex_unlock(&serial->disc_mutex);
313 mutex_unlock(&serial->disc_mutex);
314 }
315 usb_serial_put(serial); 329 usb_serial_put(serial);
316
317 /* Mustn't dereference serial any more */ 330 /* Mustn't dereference serial any more */
318 if (count == 0) 331 module_put(owner);
319 module_put(owner); 332}
333
334static void serial_close(struct tty_struct *tty, struct file *filp)
335{
336 struct usb_serial_port *port = tty->driver_data;
337
338 if (!port)
339 return;
340
341 dbg("%s - port %d", __func__, port->number);
342
343
344 if (tty_port_close_start(&port->port, tty, filp) == 0)
345 return;
346
347 serial_do_down(port);
348 tty_port_close_end(&port->port, tty);
349 tty_port_tty_set(&port->port, NULL);
350 serial_do_free(port);
351}
352
353static void serial_hangup(struct tty_struct *tty)
354{
355 struct usb_serial_port *port = tty->driver_data;
356 serial_do_down(port);
357 tty_port_hangup(&port->port);
358 serial_do_free(port);
320} 359}
321 360
322static int serial_write(struct tty_struct *tty, const unsigned char *buf, 361static int serial_write(struct tty_struct *tty, const unsigned char *buf,
@@ -648,6 +687,29 @@ static struct usb_serial_driver *search_serial_device(
648 return NULL; 687 return NULL;
649} 688}
650 689
690static int serial_carrier_raised(struct tty_port *port)
691{
692 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
693 struct usb_serial_driver *drv = p->serial->type;
694 if (drv->carrier_raised)
695 return drv->carrier_raised(p);
696 /* No carrier control - don't block */
697 return 1;
698}
699
700static void serial_dtr_rts(struct tty_port *port, int on)
701{
702 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
703 struct usb_serial_driver *drv = p->serial->type;
704 if (drv->dtr_rts)
705 drv->dtr_rts(p, on);
706}
707
708static const struct tty_port_operations serial_port_ops = {
709 .carrier_raised = serial_carrier_raised,
710 .dtr_rts = serial_dtr_rts,
711};
712
651int usb_serial_probe(struct usb_interface *interface, 713int usb_serial_probe(struct usb_interface *interface,
652 const struct usb_device_id *id) 714 const struct usb_device_id *id)
653{ 715{
@@ -841,6 +903,7 @@ int usb_serial_probe(struct usb_interface *interface,
841 if (!port) 903 if (!port)
842 goto probe_error; 904 goto probe_error;
843 tty_port_init(&port->port); 905 tty_port_init(&port->port);
906 port->port.ops = &serial_port_ops;
844 port->serial = serial; 907 port->serial = serial;
845 spin_lock_init(&port->lock); 908 spin_lock_init(&port->lock);
846 mutex_init(&port->mutex); 909 mutex_init(&port->mutex);
@@ -995,10 +1058,15 @@ int usb_serial_probe(struct usb_interface *interface,
995 1058
996 dev_set_name(&port->dev, "ttyUSB%d", port->number); 1059 dev_set_name(&port->dev, "ttyUSB%d", port->number);
997 dbg ("%s - registering %s", __func__, dev_name(&port->dev)); 1060 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1061 port->dev_state = PORT_REGISTERING;
998 retval = device_register(&port->dev); 1062 retval = device_register(&port->dev);
999 if (retval) 1063 if (retval) {
1000 dev_err(&port->dev, "Error registering port device, " 1064 dev_err(&port->dev, "Error registering port device, "
1001 "continuing\n"); 1065 "continuing\n");
1066 port->dev_state = PORT_UNREGISTERED;
1067 } else {
1068 port->dev_state = PORT_REGISTERED;
1069 }
1002 } 1070 }
1003 1071
1004 usb_serial_console_init(debug, minor); 1072 usb_serial_console_init(debug, minor);
@@ -1062,31 +1130,38 @@ void usb_serial_disconnect(struct usb_interface *interface)
1062 serial->disconnected = 1; 1130 serial->disconnected = 1;
1063 mutex_unlock(&serial->disc_mutex); 1131 mutex_unlock(&serial->disc_mutex);
1064 1132
1065 /* Unfortunately, many of the sub-drivers expect the port structures
1066 * to exist when their shutdown method is called, so we have to go
1067 * through this awkward two-step unregistration procedure.
1068 */
1069 for (i = 0; i < serial->num_ports; ++i) { 1133 for (i = 0; i < serial->num_ports; ++i) {
1070 port = serial->port[i]; 1134 port = serial->port[i];
1071 if (port) { 1135 if (port) {
1072 struct tty_struct *tty = tty_port_tty_get(&port->port); 1136 struct tty_struct *tty = tty_port_tty_get(&port->port);
1073 if (tty) { 1137 if (tty) {
1138 /* The hangup will occur asynchronously but
1139 the object refcounts will sort out all the
1140 cleanup */
1074 tty_hangup(tty); 1141 tty_hangup(tty);
1075 tty_kref_put(tty); 1142 tty_kref_put(tty);
1076 } 1143 }
1077 kill_traffic(port); 1144 kill_traffic(port);
1078 cancel_work_sync(&port->work); 1145 cancel_work_sync(&port->work);
1079 device_del(&port->dev); 1146 if (port->dev_state == PORT_REGISTERED) {
1080 } 1147
1081 } 1148 /* Make sure the port is bound so that the
1082 serial->type->shutdown(serial); 1149 * driver's port_remove method is called.
1083 for (i = 0; i < serial->num_ports; ++i) { 1150 */
1084 port = serial->port[i]; 1151 if (!port->dev.driver) {
1085 if (port) { 1152 int rc;
1086 put_device(&port->dev); 1153
1087 serial->port[i] = NULL; 1154 port->dev.driver =
1155 &serial->type->driver;
1156 rc = device_bind_driver(&port->dev);
1157 }
1158 port->dev_state = PORT_UNREGISTERING;
1159 device_del(&port->dev);
1160 port->dev_state = PORT_UNREGISTERED;
1161 }
1088 } 1162 }
1089 } 1163 }
1164 serial->type->disconnect(serial);
1090 1165
1091 /* let the last holder of this object 1166 /* let the last holder of this object
1092 * cause it to be cleaned up */ 1167 * cause it to be cleaned up */
@@ -1135,6 +1210,7 @@ static const struct tty_operations serial_ops = {
1135 .open = serial_open, 1210 .open = serial_open,
1136 .close = serial_close, 1211 .close = serial_close,
1137 .write = serial_write, 1212 .write = serial_write,
1213 .hangup = serial_hangup,
1138 .write_room = serial_write_room, 1214 .write_room = serial_write_room,
1139 .ioctl = serial_ioctl, 1215 .ioctl = serial_ioctl,
1140 .set_termios = serial_set_termios, 1216 .set_termios = serial_set_termios,
@@ -1147,6 +1223,7 @@ static const struct tty_operations serial_ops = {
1147 .proc_fops = &serial_proc_fops, 1223 .proc_fops = &serial_proc_fops,
1148}; 1224};
1149 1225
1226
1150struct tty_driver *usb_serial_tty_driver; 1227struct tty_driver *usb_serial_tty_driver;
1151 1228
1152static int __init usb_serial_init(void) 1229static int __init usb_serial_init(void)
@@ -1262,7 +1339,8 @@ static void fixup_generic(struct usb_serial_driver *device)
1262 set_to_generic_if_null(device, chars_in_buffer); 1339 set_to_generic_if_null(device, chars_in_buffer);
1263 set_to_generic_if_null(device, read_bulk_callback); 1340 set_to_generic_if_null(device, read_bulk_callback);
1264 set_to_generic_if_null(device, write_bulk_callback); 1341 set_to_generic_if_null(device, write_bulk_callback);
1265 set_to_generic_if_null(device, shutdown); 1342 set_to_generic_if_null(device, disconnect);
1343 set_to_generic_if_null(device, release);
1266} 1344}
1267 1345
1268int usb_serial_register(struct usb_serial_driver *driver) 1346int usb_serial_register(struct usb_serial_driver *driver)
diff --git a/drivers/usb/serial/usb_debug.c b/drivers/usb/serial/usb_debug.c
index 6c9cbb59552a..614800972dc3 100644
--- a/drivers/usb/serial/usb_debug.c
+++ b/drivers/usb/serial/usb_debug.c
@@ -15,7 +15,19 @@
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include <linux/usb/serial.h> 16#include <linux/usb/serial.h>
17 17
18#define URB_DEBUG_MAX_IN_FLIGHT_URBS 4000
18#define USB_DEBUG_MAX_PACKET_SIZE 8 19#define USB_DEBUG_MAX_PACKET_SIZE 8
20#define USB_DEBUG_BRK_SIZE 8
21static char USB_DEBUG_BRK[USB_DEBUG_BRK_SIZE] = {
22 0x00,
23 0xff,
24 0x01,
25 0xfe,
26 0x00,
27 0xfe,
28 0x01,
29 0xff,
30};
19 31
20static struct usb_device_id id_table [] = { 32static struct usb_device_id id_table [] = {
21 { USB_DEVICE(0x0525, 0x127a) }, 33 { USB_DEVICE(0x0525, 0x127a) },
@@ -38,6 +50,32 @@ static int usb_debug_open(struct tty_struct *tty, struct usb_serial_port *port,
38 return usb_serial_generic_open(tty, port, filp); 50 return usb_serial_generic_open(tty, port, filp);
39} 51}
40 52
53/* This HW really does not support a serial break, so one will be
54 * emulated when ever the break state is set to true.
55 */
56static void usb_debug_break_ctl(struct tty_struct *tty, int break_state)
57{
58 struct usb_serial_port *port = tty->driver_data;
59 if (!break_state)
60 return;
61 usb_serial_generic_write(tty, port, USB_DEBUG_BRK, USB_DEBUG_BRK_SIZE);
62}
63
64static void usb_debug_read_bulk_callback(struct urb *urb)
65{
66 struct usb_serial_port *port = urb->context;
67
68 if (urb->actual_length == USB_DEBUG_BRK_SIZE &&
69 memcmp(urb->transfer_buffer, USB_DEBUG_BRK,
70 USB_DEBUG_BRK_SIZE) == 0) {
71 usb_serial_handle_break(port);
72 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC);
73 return;
74 }
75
76 usb_serial_generic_read_bulk_callback(urb);
77}
78
41static struct usb_serial_driver debug_device = { 79static struct usb_serial_driver debug_device = {
42 .driver = { 80 .driver = {
43 .owner = THIS_MODULE, 81 .owner = THIS_MODULE,
@@ -46,6 +84,9 @@ static struct usb_serial_driver debug_device = {
46 .id_table = id_table, 84 .id_table = id_table,
47 .num_ports = 1, 85 .num_ports = 1,
48 .open = usb_debug_open, 86 .open = usb_debug_open,
87 .max_in_flight_urbs = URB_DEBUG_MAX_IN_FLIGHT_URBS,
88 .break_ctl = usb_debug_break_ctl,
89 .read_bulk_callback = usb_debug_read_bulk_callback,
49}; 90};
50 91
51static int __init debug_init(void) 92static int __init debug_init(void)
diff --git a/drivers/usb/serial/visor.c b/drivers/usb/serial/visor.c
index 5ac414bda718..f5d0f64dcc52 100644
--- a/drivers/usb/serial/visor.c
+++ b/drivers/usb/serial/visor.c
@@ -38,8 +38,7 @@
38/* function prototypes for a handspring visor */ 38/* function prototypes for a handspring visor */
39static int visor_open(struct tty_struct *tty, struct usb_serial_port *port, 39static int visor_open(struct tty_struct *tty, struct usb_serial_port *port,
40 struct file *filp); 40 struct file *filp);
41static void visor_close(struct tty_struct *tty, struct usb_serial_port *port, 41static void visor_close(struct usb_serial_port *port);
42 struct file *filp);
43static int visor_write(struct tty_struct *tty, struct usb_serial_port *port, 42static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
44 const unsigned char *buf, int count); 43 const unsigned char *buf, int count);
45static int visor_write_room(struct tty_struct *tty); 44static int visor_write_room(struct tty_struct *tty);
@@ -48,7 +47,7 @@ static void visor_unthrottle(struct tty_struct *tty);
48static int visor_probe(struct usb_serial *serial, 47static int visor_probe(struct usb_serial *serial,
49 const struct usb_device_id *id); 48 const struct usb_device_id *id);
50static int visor_calc_num_ports(struct usb_serial *serial); 49static int visor_calc_num_ports(struct usb_serial *serial);
51static void visor_shutdown(struct usb_serial *serial); 50static void visor_release(struct usb_serial *serial);
52static void visor_write_bulk_callback(struct urb *urb); 51static void visor_write_bulk_callback(struct urb *urb);
53static void visor_read_bulk_callback(struct urb *urb); 52static void visor_read_bulk_callback(struct urb *urb);
54static void visor_read_int_callback(struct urb *urb); 53static void visor_read_int_callback(struct urb *urb);
@@ -203,7 +202,7 @@ static struct usb_serial_driver handspring_device = {
203 .attach = treo_attach, 202 .attach = treo_attach,
204 .probe = visor_probe, 203 .probe = visor_probe,
205 .calc_num_ports = visor_calc_num_ports, 204 .calc_num_ports = visor_calc_num_ports,
206 .shutdown = visor_shutdown, 205 .release = visor_release,
207 .write = visor_write, 206 .write = visor_write,
208 .write_room = visor_write_room, 207 .write_room = visor_write_room,
209 .write_bulk_callback = visor_write_bulk_callback, 208 .write_bulk_callback = visor_write_bulk_callback,
@@ -228,7 +227,7 @@ static struct usb_serial_driver clie_5_device = {
228 .attach = clie_5_attach, 227 .attach = clie_5_attach,
229 .probe = visor_probe, 228 .probe = visor_probe,
230 .calc_num_ports = visor_calc_num_ports, 229 .calc_num_ports = visor_calc_num_ports,
231 .shutdown = visor_shutdown, 230 .release = visor_release,
232 .write = visor_write, 231 .write = visor_write,
233 .write_room = visor_write_room, 232 .write_room = visor_write_room,
234 .write_bulk_callback = visor_write_bulk_callback, 233 .write_bulk_callback = visor_write_bulk_callback,
@@ -324,8 +323,7 @@ exit:
324} 323}
325 324
326 325
327static void visor_close(struct tty_struct *tty, 326static void visor_close(struct usb_serial_port *port)
328 struct usb_serial_port *port, struct file *filp)
329{ 327{
330 struct visor_private *priv = usb_get_serial_port_data(port); 328 struct visor_private *priv = usb_get_serial_port_data(port);
331 unsigned char *transfer_buffer; 329 unsigned char *transfer_buffer;
@@ -920,7 +918,7 @@ static int clie_5_attach(struct usb_serial *serial)
920 return generic_startup(serial); 918 return generic_startup(serial);
921} 919}
922 920
923static void visor_shutdown(struct usb_serial *serial) 921static void visor_release(struct usb_serial *serial)
924{ 922{
925 struct visor_private *priv; 923 struct visor_private *priv;
926 int i; 924 int i;
@@ -929,10 +927,7 @@ static void visor_shutdown(struct usb_serial *serial)
929 927
930 for (i = 0; i < serial->num_ports; i++) { 928 for (i = 0; i < serial->num_ports; i++) {
931 priv = usb_get_serial_port_data(serial->port[i]); 929 priv = usb_get_serial_port_data(serial->port[i]);
932 if (priv) { 930 kfree(priv);
933 usb_set_serial_port_data(serial->port[i], NULL);
934 kfree(priv);
935 }
936 } 931 }
937} 932}
938 933
diff --git a/drivers/usb/serial/whiteheat.c b/drivers/usb/serial/whiteheat.c
index 5335d3211c07..8d126dd7a02e 100644
--- a/drivers/usb/serial/whiteheat.c
+++ b/drivers/usb/serial/whiteheat.c
@@ -144,11 +144,10 @@ static int whiteheat_firmware_attach(struct usb_serial *serial);
144 144
145/* function prototypes for the Connect Tech WhiteHEAT serial converter */ 145/* function prototypes for the Connect Tech WhiteHEAT serial converter */
146static int whiteheat_attach(struct usb_serial *serial); 146static int whiteheat_attach(struct usb_serial *serial);
147static void whiteheat_shutdown(struct usb_serial *serial); 147static void whiteheat_release(struct usb_serial *serial);
148static int whiteheat_open(struct tty_struct *tty, 148static int whiteheat_open(struct tty_struct *tty,
149 struct usb_serial_port *port, struct file *filp); 149 struct usb_serial_port *port, struct file *filp);
150static void whiteheat_close(struct tty_struct *tty, 150static void whiteheat_close(struct usb_serial_port *port);
151 struct usb_serial_port *port, struct file *filp);
152static int whiteheat_write(struct tty_struct *tty, 151static int whiteheat_write(struct tty_struct *tty,
153 struct usb_serial_port *port, 152 struct usb_serial_port *port,
154 const unsigned char *buf, int count); 153 const unsigned char *buf, int count);
@@ -190,7 +189,7 @@ static struct usb_serial_driver whiteheat_device = {
190 .id_table = id_table_std, 189 .id_table = id_table_std,
191 .num_ports = 4, 190 .num_ports = 4,
192 .attach = whiteheat_attach, 191 .attach = whiteheat_attach,
193 .shutdown = whiteheat_shutdown, 192 .release = whiteheat_release,
194 .open = whiteheat_open, 193 .open = whiteheat_open,
195 .close = whiteheat_close, 194 .close = whiteheat_close,
196 .write = whiteheat_write, 195 .write = whiteheat_write,
@@ -618,7 +617,7 @@ no_command_buffer:
618} 617}
619 618
620 619
621static void whiteheat_shutdown(struct usb_serial *serial) 620static void whiteheat_release(struct usb_serial *serial)
622{ 621{
623 struct usb_serial_port *command_port; 622 struct usb_serial_port *command_port;
624 struct usb_serial_port *port; 623 struct usb_serial_port *port;
@@ -712,8 +711,7 @@ exit:
712} 711}
713 712
714 713
715static void whiteheat_close(struct tty_struct *tty, 714static void whiteheat_close(struct usb_serial_port *port)
716 struct usb_serial_port *port, struct file *filp)
717{ 715{
718 struct whiteheat_private *info = usb_get_serial_port_data(port); 716 struct whiteheat_private *info = usb_get_serial_port_data(port);
719 struct whiteheat_urb_wrap *wrap; 717 struct whiteheat_urb_wrap *wrap;
@@ -723,31 +721,7 @@ static void whiteheat_close(struct tty_struct *tty,
723 721
724 dbg("%s - port %d", __func__, port->number); 722 dbg("%s - port %d", __func__, port->number);
725 723
726 mutex_lock(&port->serial->disc_mutex);
727 /* filp is NULL when called from usb_serial_disconnect */
728 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
729 mutex_unlock(&port->serial->disc_mutex);
730 return;
731 }
732 mutex_unlock(&port->serial->disc_mutex);
733
734 tty->closing = 1;
735
736/*
737 * Not currently in use; tty_wait_until_sent() calls
738 * serial_chars_in_buffer() which deadlocks on the second semaphore
739 * acquisition. This should be fixed at some point. Greg's been
740 * notified.
741 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
742 tty_wait_until_sent(tty, CLOSING_DELAY);
743 }
744*/
745
746 tty_driver_flush_buffer(tty);
747 tty_ldisc_flush(tty);
748
749 firm_report_tx_done(port); 724 firm_report_tx_done(port);
750
751 firm_close(port); 725 firm_close(port);
752 726
753 /* shutdown our bulk reads and writes */ 727 /* shutdown our bulk reads and writes */
@@ -775,10 +749,7 @@ static void whiteheat_close(struct tty_struct *tty,
775 } 749 }
776 spin_unlock_irq(&info->lock); 750 spin_unlock_irq(&info->lock);
777 mutex_unlock(&info->deathwarrant); 751 mutex_unlock(&info->deathwarrant);
778
779 stop_command_port(port->serial); 752 stop_command_port(port->serial);
780
781 tty->closing = 0;
782} 753}
783 754
784 755
diff --git a/drivers/usb/storage/initializers.c b/drivers/usb/storage/initializers.c
index 2dd9bd4bff56..ec17c96371af 100644
--- a/drivers/usb/storage/initializers.c
+++ b/drivers/usb/storage/initializers.c
@@ -52,7 +52,7 @@ int usb_stor_euscsi_init(struct us_data *us)
52 us->iobuf[0] = 0x1; 52 us->iobuf[0] = 0x1;
53 result = usb_stor_control_msg(us, us->send_ctrl_pipe, 53 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
54 0x0C, USB_RECIP_INTERFACE | USB_TYPE_VENDOR, 54 0x0C, USB_RECIP_INTERFACE | USB_TYPE_VENDOR,
55 0x01, 0x0, us->iobuf, 0x1, 5*HZ); 55 0x01, 0x0, us->iobuf, 0x1, 5000);
56 US_DEBUGP("-- result is %d\n", result); 56 US_DEBUGP("-- result is %d\n", result);
57 57
58 return 0; 58 return 0;
@@ -80,14 +80,16 @@ int usb_stor_ucr61s2b_init(struct us_data *us)
80 80
81 res = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb, 81 res = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
82 US_BULK_CB_WRAP_LEN, &partial); 82 US_BULK_CB_WRAP_LEN, &partial);
83 if(res) 83 if (res)
84 return res; 84 return -EIO;
85 85
86 US_DEBUGP("Getting status packet...\n"); 86 US_DEBUGP("Getting status packet...\n");
87 res = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs, 87 res = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
88 US_BULK_CS_WRAP_LEN, &partial); 88 US_BULK_CS_WRAP_LEN, &partial);
89 if (res)
90 return -EIO;
89 91
90 return (res ? -1 : 0); 92 return 0;
91} 93}
92 94
93/* This places the HUAWEI E220 devices in multi-port mode */ 95/* This places the HUAWEI E220 devices in multi-port mode */
@@ -99,6 +101,6 @@ int usb_stor_huawei_e220_init(struct us_data *us)
99 USB_REQ_SET_FEATURE, 101 USB_REQ_SET_FEATURE,
100 USB_TYPE_STANDARD | USB_RECIP_DEVICE, 102 USB_TYPE_STANDARD | USB_RECIP_DEVICE,
101 0x01, 0x0, NULL, 0x0, 1000); 103 0x01, 0x0, NULL, 0x0, 1000);
102 US_DEBUGP("usb_control_msg performing result is %d\n", result); 104 US_DEBUGP("Huawei mode set result is %d\n", result);
103 return (result ? 0 : -1); 105 return (result ? 0 : -ENODEV);
104} 106}
diff --git a/drivers/usb/storage/option_ms.c b/drivers/usb/storage/option_ms.c
index 353f922939a4..773a5cd38c5a 100644
--- a/drivers/usb/storage/option_ms.c
+++ b/drivers/usb/storage/option_ms.c
@@ -37,7 +37,7 @@ MODULE_PARM_DESC(option_zero_cd, "ZeroCD mode (1=Force Modem (default),"
37 37
38#define RESPONSE_LEN 1024 38#define RESPONSE_LEN 1024
39 39
40static int option_rezero(struct us_data *us, int ep_in, int ep_out) 40static int option_rezero(struct us_data *us)
41{ 41{
42 const unsigned char rezero_msg[] = { 42 const unsigned char rezero_msg[] = {
43 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12, 43 0x55, 0x53, 0x42, 0x43, 0x78, 0x56, 0x34, 0x12,
@@ -54,10 +54,10 @@ static int option_rezero(struct us_data *us, int ep_in, int ep_out)
54 if (buffer == NULL) 54 if (buffer == NULL)
55 return USB_STOR_TRANSPORT_ERROR; 55 return USB_STOR_TRANSPORT_ERROR;
56 56
57 memcpy(buffer, rezero_msg, sizeof (rezero_msg)); 57 memcpy(buffer, rezero_msg, sizeof(rezero_msg));
58 result = usb_stor_bulk_transfer_buf(us, 58 result = usb_stor_bulk_transfer_buf(us,
59 usb_sndbulkpipe(us->pusb_dev, ep_out), 59 us->send_bulk_pipe,
60 buffer, sizeof (rezero_msg), NULL); 60 buffer, sizeof(rezero_msg), NULL);
61 if (result != USB_STOR_XFER_GOOD) { 61 if (result != USB_STOR_XFER_GOOD) {
62 result = USB_STOR_XFER_ERROR; 62 result = USB_STOR_XFER_ERROR;
63 goto out; 63 goto out;
@@ -66,9 +66,15 @@ static int option_rezero(struct us_data *us, int ep_in, int ep_out)
66 /* Some of the devices need to be asked for a response, but we don't 66 /* Some of the devices need to be asked for a response, but we don't
67 * care what that response is. 67 * care what that response is.
68 */ 68 */
69 result = usb_stor_bulk_transfer_buf(us, 69 usb_stor_bulk_transfer_buf(us,
70 usb_sndbulkpipe(us->pusb_dev, ep_out), 70 us->recv_bulk_pipe,
71 buffer, RESPONSE_LEN, NULL); 71 buffer, RESPONSE_LEN, NULL);
72
73 /* Read the CSW */
74 usb_stor_bulk_transfer_buf(us,
75 us->recv_bulk_pipe,
76 buffer, 13, NULL);
77
72 result = USB_STOR_XFER_GOOD; 78 result = USB_STOR_XFER_GOOD;
73 79
74out: 80out:
@@ -76,63 +82,78 @@ out:
76 return result; 82 return result;
77} 83}
78 84
79int option_ms_init(struct us_data *us) 85static int option_inquiry(struct us_data *us)
80{ 86{
81 struct usb_device *udev; 87 const unsigned char inquiry_msg[] = {
82 struct usb_interface *intf; 88 0x55, 0x53, 0x42, 0x43, 0x12, 0x34, 0x56, 0x78,
83 struct usb_host_interface *iface_desc; 89 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12,
84 struct usb_endpoint_descriptor *endpoint = NULL; 90 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
85 u8 ep_in = 0, ep_out = 0; 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
86 int ep_in_size = 0, ep_out_size = 0; 92 };
87 int i, result; 93 char *buffer;
88 94 int result;
89 udev = us->pusb_dev;
90 intf = us->pusb_intf;
91
92 /* Ensure it's really a ZeroCD device; devices that are already
93 * in modem mode return 0xFF for class, subclass, and protocol.
94 */
95 if (udev->descriptor.bDeviceClass != 0 ||
96 udev->descriptor.bDeviceSubClass != 0 ||
97 udev->descriptor.bDeviceProtocol != 0)
98 return USB_STOR_TRANSPORT_GOOD;
99 95
100 US_DEBUGP("Option MS: option_ms_init called\n"); 96 US_DEBUGP("Option MS: %s", "device inquiry for vendor name\n");
101 97
102 /* Find the right mass storage interface */ 98 buffer = kzalloc(0x24, GFP_KERNEL);
103 iface_desc = intf->cur_altsetting; 99 if (buffer == NULL)
104 if (iface_desc->desc.bInterfaceClass != 0x8 || 100 return USB_STOR_TRANSPORT_ERROR;
105 iface_desc->desc.bInterfaceSubClass != 0x6 ||
106 iface_desc->desc.bInterfaceProtocol != 0x50) {
107 US_DEBUGP("Option MS: mass storage interface not found, no action "
108 "required\n");
109 return USB_STOR_TRANSPORT_GOOD;
110 }
111 101
112 /* Find the mass storage bulk endpoints */ 102 memcpy(buffer, inquiry_msg, sizeof(inquiry_msg));
113 for (i = 0; i < iface_desc->desc.bNumEndpoints && (!ep_in_size || !ep_out_size); ++i) { 103 result = usb_stor_bulk_transfer_buf(us,
114 endpoint = &iface_desc->endpoint[i].desc; 104 us->send_bulk_pipe,
115 105 buffer, sizeof(inquiry_msg), NULL);
116 if (usb_endpoint_is_bulk_in(endpoint)) { 106 if (result != USB_STOR_XFER_GOOD) {
117 ep_in = usb_endpoint_num(endpoint); 107 result = USB_STOR_XFER_ERROR;
118 ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize); 108 goto out;
119 } else if (usb_endpoint_is_bulk_out(endpoint)) {
120 ep_out = usb_endpoint_num(endpoint);
121 ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
122 }
123 } 109 }
124 110
125 /* Can't find the mass storage endpoints */ 111 result = usb_stor_bulk_transfer_buf(us,
126 if (!ep_in_size || !ep_out_size) { 112 us->recv_bulk_pipe,
127 US_DEBUGP("Option MS: mass storage endpoints not found, no action " 113 buffer, 0x24, NULL);
128 "required\n"); 114 if (result != USB_STOR_XFER_GOOD) {
129 return USB_STOR_TRANSPORT_GOOD; 115 result = USB_STOR_XFER_ERROR;
116 goto out;
130 } 117 }
131 118
119 result = memcmp(buffer+8, "Option", 6);
120
121 if (result != 0)
122 result = memcmp(buffer+8, "ZCOPTION", 8);
123
124 /* Read the CSW */
125 usb_stor_bulk_transfer_buf(us,
126 us->recv_bulk_pipe,
127 buffer, 13, NULL);
128
129out:
130 kfree(buffer);
131 return result;
132}
133
134
135int option_ms_init(struct us_data *us)
136{
137 int result;
138
139 US_DEBUGP("Option MS: option_ms_init called\n");
140
141 /* Additional test for vendor information via INQUIRY,
142 * because some vendor/product IDs are ambiguous
143 */
144 result = option_inquiry(us);
145 if (result != 0) {
146 US_DEBUGP("Option MS: vendor is not Option or not determinable,"
147 " no action taken\n");
148 return 0;
149 } else
150 US_DEBUGP("Option MS: this is a genuine Option device,"
151 " proceeding\n");
152
132 /* Force Modem mode */ 153 /* Force Modem mode */
133 if (option_zero_cd == ZCD_FORCE_MODEM) { 154 if (option_zero_cd == ZCD_FORCE_MODEM) {
134 US_DEBUGP("Option MS: %s", "Forcing Modem Mode\n"); 155 US_DEBUGP("Option MS: %s", "Forcing Modem Mode\n");
135 result = option_rezero(us, ep_in, ep_out); 156 result = option_rezero(us);
136 if (result != USB_STOR_XFER_GOOD) 157 if (result != USB_STOR_XFER_GOOD)
137 US_DEBUGP("Option MS: Failed to switch to modem mode.\n"); 158 US_DEBUGP("Option MS: Failed to switch to modem mode.\n");
138 return -EIO; 159 return -EIO;
@@ -142,6 +163,6 @@ int option_ms_init(struct us_data *us)
142 " requests it\n"); 163 " requests it\n");
143 } 164 }
144 165
145 return USB_STOR_TRANSPORT_GOOD; 166 return 0;
146} 167}
147 168
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 4ca3b5860643..cfa26d56ce60 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -132,7 +132,7 @@ static int slave_configure(struct scsi_device *sdev)
132 132
133 if (us->fflags & US_FL_MAX_SECTORS_MIN) 133 if (us->fflags & US_FL_MAX_SECTORS_MIN)
134 max_sectors = PAGE_CACHE_SIZE >> 9; 134 max_sectors = PAGE_CACHE_SIZE >> 9;
135 if (sdev->request_queue->max_sectors > max_sectors) 135 if (queue_max_sectors(sdev->request_queue) > max_sectors)
136 blk_queue_max_sectors(sdev->request_queue, 136 blk_queue_max_sectors(sdev->request_queue,
137 max_sectors); 137 max_sectors);
138 } else if (sdev->type == TYPE_TAPE) { 138 } else if (sdev->type == TYPE_TAPE) {
@@ -483,7 +483,7 @@ static ssize_t show_max_sectors(struct device *dev, struct device_attribute *att
483{ 483{
484 struct scsi_device *sdev = to_scsi_device(dev); 484 struct scsi_device *sdev = to_scsi_device(dev);
485 485
486 return sprintf(buf, "%u\n", sdev->request_queue->max_sectors); 486 return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue));
487} 487}
488 488
489/* Input routine for the sysfs max_sectors file */ 489/* Input routine for the sysfs max_sectors file */
diff --git a/drivers/usb/storage/sierra_ms.c b/drivers/usb/storage/sierra_ms.c
index 4359a2cb42df..4395c4100ec2 100644
--- a/drivers/usb/storage/sierra_ms.c
+++ b/drivers/usb/storage/sierra_ms.c
@@ -202,6 +202,6 @@ int sierra_ms_init(struct us_data *us)
202complete: 202complete:
203 result = device_create_file(&us->pusb_intf->dev, &dev_attr_truinst); 203 result = device_create_file(&us->pusb_intf->dev, &dev_attr_truinst);
204 204
205 return USB_STOR_TRANSPORT_GOOD; 205 return 0;
206} 206}
207 207
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index 4b8b69045fe6..1b9c5dd0fb27 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -1385,7 +1385,7 @@ UNUSUAL_DEV( 0x10d6, 0x2200, 0x0100, 0x0100,
1385UNUSUAL_DEV( 0x1186, 0x3e04, 0x0000, 0x0000, 1385UNUSUAL_DEV( 0x1186, 0x3e04, 0x0000, 0x0000,
1386 "D-Link", 1386 "D-Link",
1387 "USB Mass Storage", 1387 "USB Mass Storage",
1388 US_SC_DEVICE, US_PR_DEVICE, option_ms_init, 0), 1388 US_SC_DEVICE, US_PR_DEVICE, option_ms_init, US_FL_IGNORE_DEVICE),
1389 1389
1390/* Reported by Kevin Lloyd <linux@sierrawireless.com> 1390/* Reported by Kevin Lloyd <linux@sierrawireless.com>
1391 * Entry is needed for the initializer function override, 1391 * Entry is needed for the initializer function override,