aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@g5.osdl.org>2006-03-21 12:25:47 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-21 12:25:47 -0500
commit2bf2154c6bb5599e3ec3f73c34861a0b12aa839e (patch)
tree62691bd915e2e3c2e6648306d3fb893f7a1dc57e /drivers/usb
parent08a4ecee986dd98e86090ff5faac4782b6765aed (diff)
parent71a8924bee63d891f6256d560e32416a458440b3 (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6: (81 commits) [PATCH] USB: omninet: fix up debugging comments [PATCH] USB serial: add navman driver [PATCH] USB: Fix irda-usb use after use [PATCH] USB: rtl8150 small fix [PATCH] USB: ftdi_sio: add Icom ID1 USB product and vendor ids [PATCH] USB: cp2101: add new device IDs [PATCH] USB: fix check_ctrlrecip to allow control transfers in state ADDRESS [PATCH] USB: vicam.c: fix a NULL pointer dereference [PATCH] USB: ZC0301 driver bugfix [PATCH] USB: add support for Creativelabs Silvercrest USB keyboard [PATCH] USB: storage: new unusual_devs.h entry: Mitsumi 7in1 Card Reader [PATCH] USB: storage: unusual_devs.h entry 0420:0001 [PATCH] USB: storage: another unusual_devs.h entry [PATCH] USB: storage: sandisk unusual_devices entry [PATCH] USB: fix initdata issue in isp116x-hcd [PATCH] USB: usbcore: usb_set_configuration oops (NULL ptr dereference) [PATCH] USB: usbcore: Don't assume a USB configuration includes any interfaces [PATCH] USB: ub 03 drop stall clearing [PATCH] USB: ub 02 remove diag [PATCH] USB: ub 01 remove first_open ...
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/Kconfig9
-rw-r--r--drivers/usb/Makefile4
-rw-r--r--drivers/usb/class/Kconfig47
-rw-r--r--drivers/usb/class/Makefile2
-rw-r--r--drivers/usb/class/audio.c3869
-rw-r--r--drivers/usb/class/audio.h110
-rw-r--r--drivers/usb/class/cdc-acm.c23
-rw-r--r--drivers/usb/class/usb-midi.c2153
-rw-r--r--drivers/usb/class/usb-midi.h164
-rw-r--r--drivers/usb/class/usblp.c15
-rw-r--r--drivers/usb/core/devices.c7
-rw-r--r--drivers/usb/core/devio.c24
-rw-r--r--drivers/usb/core/hcd-pci.c11
-rw-r--r--drivers/usb/core/hcd.c153
-rw-r--r--drivers/usb/core/hcd.h4
-rw-r--r--drivers/usb/core/hub.c45
-rw-r--r--drivers/usb/core/message.c17
-rw-r--r--drivers/usb/core/notify.c15
-rw-r--r--drivers/usb/core/usb.c5
-rw-r--r--drivers/usb/gadget/Kconfig17
-rw-r--r--drivers/usb/gadget/Makefile1
-rw-r--r--drivers/usb/gadget/at91_udc.c1773
-rw-r--r--drivers/usb/gadget/at91_udc.h181
-rw-r--r--drivers/usb/gadget/dummy_hcd.c3
-rw-r--r--drivers/usb/gadget/ether.c53
-rw-r--r--drivers/usb/gadget/file_storage.c4
-rw-r--r--drivers/usb/gadget/gadget_chips.h30
-rw-r--r--drivers/usb/gadget/goku_udc.c3
-rw-r--r--drivers/usb/gadget/inode.c6
-rw-r--r--drivers/usb/gadget/lh7a40x_udc.c3
-rw-r--r--drivers/usb/gadget/net2280.c3
-rw-r--r--drivers/usb/gadget/omap_udc.c6
-rw-r--r--drivers/usb/gadget/pxa2xx_udc.c3
-rw-r--r--drivers/usb/gadget/serial.c9
-rw-r--r--drivers/usb/gadget/zero.c15
-rw-r--r--drivers/usb/host/Kconfig2
-rw-r--r--drivers/usb/host/ehci-au1xxx.c297
-rw-r--r--drivers/usb/host/ehci-fsl.c366
-rw-r--r--drivers/usb/host/ehci-fsl.h37
-rw-r--r--drivers/usb/host/ehci-hcd.c13
-rw-r--r--drivers/usb/host/ehci-hub.c4
-rw-r--r--drivers/usb/host/ehci-mem.c11
-rw-r--r--drivers/usb/host/ehci-pci.c25
-rw-r--r--drivers/usb/host/ehci-q.c17
-rw-r--r--drivers/usb/host/ehci-sched.c20
-rw-r--r--drivers/usb/host/ehci.h18
-rw-r--r--drivers/usb/host/hc_crisv10.c12
-rw-r--r--drivers/usb/host/isp116x-hcd.c5
-rw-r--r--drivers/usb/host/ohci-at91.c306
-rw-r--r--drivers/usb/host/ohci-au1xxx.c102
-rw-r--r--drivers/usb/host/ohci-hcd.c54
-rw-r--r--drivers/usb/host/ohci-hub.c12
-rw-r--r--drivers/usb/host/ohci-pci.c15
-rw-r--r--drivers/usb/host/sl811-hcd.c3
-rw-r--r--drivers/usb/host/uhci-debug.c356
-rw-r--r--drivers/usb/host/uhci-hcd.c127
-rw-r--r--drivers/usb/host/uhci-hcd.h188
-rw-r--r--drivers/usb/host/uhci-hub.c21
-rw-r--r--drivers/usb/host/uhci-q.c1294
-rw-r--r--drivers/usb/image/mdc800.c67
-rw-r--r--drivers/usb/input/ati_remote.c2
-rw-r--r--drivers/usb/input/hid-core.c175
-rw-r--r--drivers/usb/input/hid-lgff.c6
-rw-r--r--drivers/usb/input/hid-tmff.c3
-rw-r--r--drivers/usb/input/hid.h10
-rw-r--r--drivers/usb/input/hiddev.c6
-rw-r--r--drivers/usb/media/Kconfig15
-rw-r--r--drivers/usb/media/Makefile7
-rw-r--r--drivers/usb/media/dabusb.c36
-rw-r--r--drivers/usb/media/dabusb.h2
-rw-r--r--drivers/usb/media/et61x251.h28
-rw-r--r--drivers/usb/media/et61x251_core.c321
-rw-r--r--drivers/usb/media/et61x251_sensor.h5
-rw-r--r--drivers/usb/media/et61x251_tas5130d1b.c10
-rw-r--r--drivers/usb/media/ov511.c97
-rw-r--r--drivers/usb/media/ov511.h11
-rw-r--r--drivers/usb/media/pwc/pwc-ctrl.c1
-rw-r--r--drivers/usb/media/pwc/pwc-if.c9
-rw-r--r--drivers/usb/media/se401.c16
-rw-r--r--drivers/usb/media/se401.h3
-rw-r--r--drivers/usb/media/sn9c102.h28
-rw-r--r--drivers/usb/media/sn9c102_core.c326
-rw-r--r--drivers/usb/media/sn9c102_ov7630.c33
-rw-r--r--drivers/usb/media/sn9c102_pas202bca.c238
-rw-r--r--drivers/usb/media/sn9c102_pas202bcb.c2
-rw-r--r--drivers/usb/media/sn9c102_sensor.h15
-rw-r--r--drivers/usb/media/sn9c102_tas5110c1b.c14
-rw-r--r--drivers/usb/media/sn9c102_tas5130d1b.c12
-rw-r--r--drivers/usb/media/stv680.c20
-rw-r--r--drivers/usb/media/stv680.h2
-rw-r--r--drivers/usb/media/usbvideo.c31
-rw-r--r--drivers/usb/media/usbvideo.h5
-rw-r--r--drivers/usb/media/vicam.c22
-rw-r--r--drivers/usb/media/w9968cf.c88
-rw-r--r--drivers/usb/media/w9968cf.h14
-rw-r--r--drivers/usb/media/zc0301.h192
-rw-r--r--drivers/usb/media/zc0301_core.c2055
-rw-r--r--drivers/usb/media/zc0301_pas202bcb.c361
-rw-r--r--drivers/usb/media/zc0301_sensor.h103
-rw-r--r--drivers/usb/misc/auerswald.c6
-rw-r--r--drivers/usb/misc/cytherm.c3
-rw-r--r--drivers/usb/misc/idmouse.c28
-rw-r--r--drivers/usb/misc/ldusb.c14
-rw-r--r--drivers/usb/misc/legousbtower.c11
-rw-r--r--drivers/usb/misc/phidgetkit.c9
-rw-r--r--drivers/usb/misc/phidgetservo.c3
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.c5
-rw-r--r--drivers/usb/misc/sisusbvga/sisusb.h8
-rw-r--r--drivers/usb/misc/usblcd.c3
-rw-r--r--drivers/usb/misc/usbled.c3
-rw-r--r--drivers/usb/misc/usbtest.c9
-rw-r--r--drivers/usb/mon/mon_main.c22
-rw-r--r--drivers/usb/mon/mon_text.c24
-rw-r--r--drivers/usb/mon/usb_mon.h2
-rw-r--r--drivers/usb/net/pegasus.c1
-rw-r--r--drivers/usb/net/pegasus.h26
-rw-r--r--drivers/usb/net/rtl8150.c4
-rw-r--r--drivers/usb/net/zd1201.c9
-rw-r--r--drivers/usb/serial/Kconfig7
-rw-r--r--drivers/usb/serial/Makefile1
-rw-r--r--drivers/usb/serial/cp2101.c7
-rw-r--r--drivers/usb/serial/cypress_m8.c73
-rw-r--r--drivers/usb/serial/cypress_m8.h5
-rw-r--r--drivers/usb/serial/ftdi_sio.c4
-rw-r--r--drivers/usb/serial/ftdi_sio.h7
-rw-r--r--drivers/usb/serial/garmin_gps.c3
-rw-r--r--drivers/usb/serial/io_edgeport.c3
-rw-r--r--drivers/usb/serial/io_ti.c6
-rw-r--r--drivers/usb/serial/ir-usb.c3
-rw-r--r--drivers/usb/serial/keyspan.c6
-rw-r--r--drivers/usb/serial/kobil_sct.c16
-rw-r--r--drivers/usb/serial/mct_u232.c3
-rw-r--r--drivers/usb/serial/navman.c157
-rw-r--r--drivers/usb/serial/omninet.c10
-rw-r--r--drivers/usb/serial/option.c3
-rw-r--r--drivers/usb/serial/pl2303.c8
-rw-r--r--drivers/usb/serial/pl2303.h4
-rw-r--r--drivers/usb/serial/ti_usb_3410_5052.c3
-rw-r--r--drivers/usb/serial/usb-serial.c6
-rw-r--r--drivers/usb/serial/visor.c3
-rw-r--r--drivers/usb/storage/datafab.c3
-rw-r--r--drivers/usb/storage/isd200.c10
-rw-r--r--drivers/usb/storage/jumpshot.c3
-rw-r--r--drivers/usb/storage/scsiglue.c9
-rw-r--r--drivers/usb/storage/sddr55.c3
-rw-r--r--drivers/usb/storage/shuttle_usbat.c3
-rw-r--r--drivers/usb/storage/unusual_devs.h32
-rw-r--r--drivers/usb/storage/usb.c25
-rw-r--r--drivers/usb/storage/usb.h5
149 files changed, 8580 insertions, 8524 deletions
diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
index 85dacc92545a..b1222cd4aa46 100644
--- a/drivers/usb/Kconfig
+++ b/drivers/usb/Kconfig
@@ -10,6 +10,7 @@ menu "USB support"
10config USB_ARCH_HAS_HCD 10config USB_ARCH_HAS_HCD
11 boolean 11 boolean
12 default y if USB_ARCH_HAS_OHCI 12 default y if USB_ARCH_HAS_OHCI
13 default y if USB_ARCH_HAS_EHCI
13 default y if ARM # SL-811 14 default y if ARM # SL-811
14 default PCI 15 default PCI
15 16
@@ -22,6 +23,7 @@ config USB_ARCH_HAS_OHCI
22 default y if ARCH_LH7A404 23 default y if ARCH_LH7A404
23 default y if ARCH_S3C2410 24 default y if ARCH_S3C2410
24 default y if PXA27x 25 default y if PXA27x
26 default y if ARCH_AT91RM9200
25 # PPC: 27 # PPC:
26 default y if STB03xxx 28 default y if STB03xxx
27 default y if PPC_MPC52xx 29 default y if PPC_MPC52xx
@@ -30,6 +32,13 @@ config USB_ARCH_HAS_OHCI
30 # more: 32 # more:
31 default PCI 33 default PCI
32 34
35# some non-PCI hcds implement EHCI
36config USB_ARCH_HAS_EHCI
37 boolean
38 default y if PPC_83xx
39 default y if SOC_AU1200
40 default PCI
41
33# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface. 42# ARM SA1111 chips have a non-PCI based "OHCI-compatible" USB host interface.
34config USB 43config USB
35 tristate "Support for Host-side USB" 44 tristate "Support for Host-side USB"
diff --git a/drivers/usb/Makefile b/drivers/usb/Makefile
index 36e476dd9123..bb36a1c1dbb3 100644
--- a/drivers/usb/Makefile
+++ b/drivers/usb/Makefile
@@ -15,10 +15,9 @@ obj-$(CONFIG_USB_OHCI_HCD) += host/
15obj-$(CONFIG_USB_UHCI_HCD) += host/ 15obj-$(CONFIG_USB_UHCI_HCD) += host/
16obj-$(CONFIG_USB_SL811_HCD) += host/ 16obj-$(CONFIG_USB_SL811_HCD) += host/
17obj-$(CONFIG_ETRAX_USB_HOST) += host/ 17obj-$(CONFIG_ETRAX_USB_HOST) += host/
18obj-$(CONFIG_USB_OHCI_AT91) += host/
18 19
19obj-$(CONFIG_USB_ACM) += class/ 20obj-$(CONFIG_USB_ACM) += class/
20obj-$(CONFIG_USB_AUDIO) += class/
21obj-$(CONFIG_USB_MIDI) += class/
22obj-$(CONFIG_USB_PRINTER) += class/ 21obj-$(CONFIG_USB_PRINTER) += class/
23 22
24obj-$(CONFIG_USB_STORAGE) += storage/ 23obj-$(CONFIG_USB_STORAGE) += storage/
@@ -48,6 +47,7 @@ obj-$(CONFIG_USB_SN9C102) += media/
48obj-$(CONFIG_USB_STV680) += media/ 47obj-$(CONFIG_USB_STV680) += media/
49obj-$(CONFIG_USB_VICAM) += media/ 48obj-$(CONFIG_USB_VICAM) += media/
50obj-$(CONFIG_USB_W9968CF) += media/ 49obj-$(CONFIG_USB_W9968CF) += media/
50obj-$(CONFIG_USB_ZC0301) += media/
51 51
52obj-$(CONFIG_USB_CATC) += net/ 52obj-$(CONFIG_USB_CATC) += net/
53obj-$(CONFIG_USB_KAWETH) += net/ 53obj-$(CONFIG_USB_KAWETH) += net/
diff --git a/drivers/usb/class/Kconfig b/drivers/usb/class/Kconfig
index ef105a92a7bd..3a9102d2591b 100644
--- a/drivers/usb/class/Kconfig
+++ b/drivers/usb/class/Kconfig
@@ -4,53 +4,6 @@
4comment "USB Device Class drivers" 4comment "USB Device Class drivers"
5 depends on USB 5 depends on USB
6 6
7config OBSOLETE_OSS_USB_DRIVER
8 bool "Obsolete OSS USB drivers"
9 depends on USB && SOUND
10 help
11 This option enables support for the obsolete USB Audio and Midi
12 drivers that are scheduled for removal in the near future since
13 there are ALSA drivers for the same hardware.
14
15 Please contact Adrian Bunk <bunk@stusta.de> if you had to
16 say Y here because of missing support in the ALSA drivers.
17
18 If unsure, say N.
19
20config USB_AUDIO
21 tristate "USB Audio support"
22 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
23 help
24 Say Y here if you want to connect USB audio equipment such as
25 speakers to your computer's USB port. You only need this if you use
26 the OSS sound driver; ALSA has its own option for usb audio support.
27
28 To compile this driver as a module, choose M here: the
29 module will be called audio.
30
31config USB_MIDI
32 tristate "USB MIDI support"
33 depends on USB && SOUND && OBSOLETE_OSS_USB_DRIVER
34 ---help---
35 Say Y here if you want to connect a USB MIDI device to your
36 computer's USB port. You only need this if you use the OSS
37 sound system; USB MIDI devices are supported by ALSA's USB
38 audio driver. This driver is for devices that comply with
39 'Universal Serial Bus Device Class Definition for MIDI Device'.
40
41 The following devices are known to work:
42 * Steinberg USB2MIDI
43 * Roland MPU64
44 * Roland PC-300
45 * Roland SC8850
46 * Roland UM-1
47 * Roland UM-2
48 * Roland UA-100
49 * Yamaha MU1000
50
51 To compile this driver as a module, choose M here: the
52 module will be called usb-midi.
53
54config USB_ACM 7config USB_ACM
55 tristate "USB Modem (CDC ACM) support" 8 tristate "USB Modem (CDC ACM) support"
56 depends on USB 9 depends on USB
diff --git a/drivers/usb/class/Makefile b/drivers/usb/class/Makefile
index 229471247751..cc391e6c2af8 100644
--- a/drivers/usb/class/Makefile
+++ b/drivers/usb/class/Makefile
@@ -4,6 +4,4 @@
4# 4#
5 5
6obj-$(CONFIG_USB_ACM) += cdc-acm.o 6obj-$(CONFIG_USB_ACM) += cdc-acm.o
7obj-$(CONFIG_USB_AUDIO) += audio.o
8obj-$(CONFIG_USB_MIDI) += usb-midi.o
9obj-$(CONFIG_USB_PRINTER) += usblp.o 7obj-$(CONFIG_USB_PRINTER) += usblp.o
diff --git a/drivers/usb/class/audio.c b/drivers/usb/class/audio.c
deleted file mode 100644
index 3ad9ee8b84a9..000000000000
--- a/drivers/usb/class/audio.c
+++ /dev/null
@@ -1,3869 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * audio.c -- USB Audio Class driver
5 *
6 * Copyright (C) 1999, 2000, 2001, 2003, 2004
7 * Alan Cox (alan@lxorguk.ukuu.org.uk)
8 * Thomas Sailer (sailer@ife.ee.ethz.ch)
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 as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Debugging:
16 * Use the 'lsusb' utility to dump the descriptors.
17 *
18 * 1999-09-07: Alan Cox
19 * Parsing Audio descriptor patch
20 * 1999-09-08: Thomas Sailer
21 * Added OSS compatible data io functions; both parts of the
22 * driver remain to be glued together
23 * 1999-09-10: Thomas Sailer
24 * Beautified the driver. Added sample format conversions.
25 * Still not properly glued with the parsing code.
26 * The parsing code seems to have its problems btw,
27 * Since it parses all available configs but doesn't
28 * store which iface/altsetting belongs to which config.
29 * 1999-09-20: Thomas Sailer
30 * Threw out Alan's parsing code and implemented my own one.
31 * You cannot reasonnably linearly parse audio descriptors,
32 * especially the AudioClass descriptors have to be considered
33 * pointer lists. Mixer parsing untested, due to lack of device.
34 * First stab at synch pipe implementation, the Dallas USB DAC
35 * wants to use an Asynch out pipe. usb_audio_state now basically
36 * only contains lists of mixer and wave devices. We can therefore
37 * now have multiple mixer/wave devices per USB device.
38 * 1999-10-28: Thomas Sailer
39 * Converted to URB API. Fixed a taskstate/wakeup semantics mistake
40 * that made the driver consume all available CPU cycles.
41 * Now runs stable on UHCI-Acher/Fliegl/Sailer.
42 * 1999-10-31: Thomas Sailer
43 * Audio can now be unloaded if it is not in use by any mixer
44 * or dsp client (formerly you had to disconnect the audio devices
45 * from the USB port)
46 * Finally, about three months after ordering, my "Maxxtro SPK222"
47 * speakers arrived, isn't disdata a great mail order company 8-)
48 * Parse class specific endpoint descriptor of the audiostreaming
49 * interfaces and take the endpoint attributes from there.
50 * Unbelievably, the Philips USB DAC has a sampling rate range
51 * of over a decade, yet does not support the sampling rate control!
52 * No wonder it sounds so bad, has very audible sampling rate
53 * conversion distortion. Don't try to listen to it using
54 * decent headphones!
55 * "Let's make things better" -> but please Philips start with your
56 * own stuff!!!!
57 * 1999-11-02: Thomas Sailer
58 * It takes the Philips boxes several seconds to acquire synchronisation
59 * that means they won't play short sounds. Should probably maintain
60 * the ISO datastream even if there's nothing to play.
61 * Fix counting the total_bytes counter, RealPlayer G2 depends on it.
62 * 1999-12-20: Thomas Sailer
63 * Fix bad bug in conversion to per interface probing.
64 * disconnect was called multiple times for the audio device,
65 * leading to a premature freeing of the audio structures
66 * 2000-05-13: Thomas Sailer
67 * I don't remember who changed the find_format routine,
68 * but the change was completely broken for the Dallas
69 * chip. Anyway taking sampling rate into account in find_format
70 * is bad and should not be done unless there are devices with
71 * completely broken audio descriptors. Unless someone shows
72 * me such a descriptor, I will not allow find_format to
73 * take the sampling rate into account.
74 * Also, the former find_format made:
75 * - mpg123 play mono instead of stereo
76 * - sox completely fail for wav's with sample rates < 44.1kHz
77 * for the Dallas chip.
78 * Also fix a rather long standing problem with applications that
79 * use "small" writes producing no sound at all.
80 * 2000-05-15: Thomas Sailer
81 * My fears came true, the Philips camera indeed has pretty stupid
82 * audio descriptors.
83 * 2000-05-17: Thomas Sailer
84 * Nemsoft spotted my stupid last minute change, thanks
85 * 2000-05-19: Thomas Sailer
86 * Fixed FEATURE_UNIT thinkos found thanks to the KC Technology
87 * Xtend device. Basically the driver treated FEATURE_UNIT's sourced
88 * by mono terminals as stereo.
89 * 2000-05-20: Thomas Sailer
90 * SELECTOR support (and thus selecting record channels from the mixer).
91 * Somewhat peculiar due to OSS interface limitations. Only works
92 * for channels where a "slider" is already in front of it (i.e.
93 * a MIXER unit or a FEATURE unit with volume capability).
94 * 2000-11-26: Thomas Sailer
95 * Workaround for Dallas DS4201. The DS4201 uses PCM8 as format tag for
96 * its 8 bit modes, but expects signed data (and should therefore have used PCM).
97 * 2001-03-10: Thomas Sailer
98 * provide abs function, prevent picking up a bogus kernel macro
99 * for abs. Bug report by Andrew Morton <andrewm@uow.edu.au>
100 * 2001-06-16: Bryce Nesbitt <bryce@obviously.com>
101 * Fix SNDCTL_DSP_STEREO API violation
102 * 2003-04-08: Oliver Neukum (oliver@neukum.name):
103 * Setting a configuration is done by usbcore and must not be overridden
104 * 2004-02-27: Workaround for broken synch descriptors
105 * 2004-03-07: Alan Stern <stern@rowland.harvard.edu>
106 * Add usb_ifnum_to_if() and usb_altnum_to_altsetting() support.
107 * Use the in-memory descriptors instead of reading them from the device.
108 *
109 */
110
111/*
112 * Strategy:
113 *
114 * Alan Cox and Thomas Sailer are starting to dig at opposite ends and
115 * are hoping to meet in the middle, just like tunnel diggers :)
116 * Alan tackles the descriptor parsing, Thomas the actual data IO and the
117 * OSS compatible interface.
118 *
119 * Data IO implementation issues
120 *
121 * A mmap'able ring buffer per direction is implemented, because
122 * almost every OSS app expects it. It is however impractical to
123 * transmit/receive USB data directly into and out of the ring buffer,
124 * due to alignment and synchronisation issues. Instead, the ring buffer
125 * feeds a constant time delay line that handles the USB issues.
126 *
127 * Now we first try to find an alternate setting that exactly matches
128 * the sample format requested by the user. If we find one, we do not
129 * need to perform any sample rate conversions. If there is no matching
130 * altsetting, we choose the closest one and perform sample format
131 * conversions. We never do sample rate conversion; these are too
132 * expensive to be performed in the kernel.
133 *
134 * Current status: no known HCD-specific issues.
135 *
136 * Generally: Due to the brokenness of the Audio Class spec
137 * it seems generally impossible to write a generic Audio Class driver,
138 * so a reasonable driver should implement the features that are actually
139 * used.
140 *
141 * Parsing implementation issues
142 *
143 * One cannot reasonably parse the AudioClass descriptors linearly.
144 * Therefore the current implementation features routines to look
145 * for a specific descriptor in the descriptor list.
146 *
147 * How does the parsing work? First, all interfaces are searched
148 * for an AudioControl class interface. If found, the config descriptor
149 * that belongs to the current configuration is searched and
150 * the HEADER descriptor is found. It contains a list of
151 * all AudioStreaming and MIDIStreaming devices. This list is then walked,
152 * and all AudioStreaming interfaces are classified into input and output
153 * interfaces (according to the endpoint0 direction in altsetting1) (MIDIStreaming
154 * is currently not supported). The input & output list is then used
155 * to group inputs and outputs together and issued pairwise to the
156 * AudioStreaming class parser. Finally, all OUTPUT_TERMINAL descriptors
157 * are walked and issued to the mixer construction routine.
158 *
159 * The AudioStreaming parser simply enumerates all altsettings belonging
160 * to the specified interface. It looks for AS_GENERAL and FORMAT_TYPE
161 * class specific descriptors to extract the sample format/sample rate
162 * data. Only sample format types PCM and PCM8 are supported right now, and
163 * only FORMAT_TYPE_I is handled. The isochronous data endpoint needs to
164 * be the first endpoint of the interface, and the optional synchronisation
165 * isochronous endpoint the second one.
166 *
167 * Mixer construction works as follows: The various TERMINAL and UNIT
168 * descriptors span a tree from the root (OUTPUT_TERMINAL) through the
169 * intermediate nodes (UNITs) to the leaves (INPUT_TERMINAL). We walk
170 * that tree in a depth first manner. FEATURE_UNITs may contribute volume,
171 * bass and treble sliders to the mixer, MIXER_UNITs volume sliders.
172 * The terminal type encoded in the INPUT_TERMINALs feeds a heuristic
173 * to determine "meaningful" OSS slider numbers, however we will see
174 * how well this works in practice. Other features are not used at the
175 * moment, they seem less often used. Also, it seems difficult at least
176 * to construct recording source switches from SELECTOR_UNITs, but
177 * since there are not many USB ADC's available, we leave that for later.
178 */
179
180/*****************************************************************************/
181
182#include <linux/kernel.h>
183#include <linux/slab.h>
184#include <linux/string.h>
185#include <linux/timer.h>
186#include <linux/sched.h>
187#include <linux/smp_lock.h>
188#include <linux/module.h>
189#include <linux/sound.h>
190#include <linux/soundcard.h>
191#include <linux/list.h>
192#include <linux/vmalloc.h>
193#include <linux/init.h>
194#include <linux/poll.h>
195#include <linux/bitops.h>
196#include <asm/uaccess.h>
197#include <asm/io.h>
198#include <linux/usb.h>
199
200#include "audio.h"
201
202/*
203 * Version Information
204 */
205#define DRIVER_VERSION "v1.0.0"
206#define DRIVER_AUTHOR "Alan Cox <alan@lxorguk.ukuu.org.uk>, Thomas Sailer (sailer@ife.ee.ethz.ch)"
207#define DRIVER_DESC "USB Audio Class driver"
208
209#define AUDIO_DEBUG 1
210
211#define SND_DEV_DSP16 5
212
213#define dprintk(x)
214
215/* --------------------------------------------------------------------- */
216
217/*
218 * Linked list of all audio devices...
219 */
220static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs);
221static DECLARE_MUTEX(open_sem);
222
223/*
224 * wait queue for processes wanting to open an USB audio device
225 */
226static DECLARE_WAIT_QUEUE_HEAD(open_wait);
227
228
229#define MAXFORMATS MAX_ALT
230#define DMABUFSHIFT 17 /* 128k worth of DMA buffer */
231#define NRSGBUF (1U<<(DMABUFSHIFT-PAGE_SHIFT))
232
233/*
234 * This influences:
235 * - Latency
236 * - Interrupt rate
237 * - Synchronisation behaviour
238 * Don't touch this if you don't understand all of the above.
239 */
240#define DESCFRAMES 5
241#define SYNCFRAMES DESCFRAMES
242
243#define MIXFLG_STEREOIN 1
244#define MIXFLG_STEREOOUT 2
245
246struct mixerchannel {
247 __u16 value;
248 __u16 osschannel; /* number of the OSS channel */
249 __s16 minval, maxval;
250 __u16 slctunitid;
251 __u8 unitid;
252 __u8 selector;
253 __u8 chnum;
254 __u8 flags;
255};
256
257struct audioformat {
258 unsigned int format;
259 unsigned int sratelo;
260 unsigned int sratehi;
261 unsigned char altsetting;
262 unsigned char attributes;
263};
264
265struct dmabuf {
266 /* buffer data format */
267 unsigned int format;
268 unsigned int srate;
269 /* physical buffer */
270 unsigned char *sgbuf[NRSGBUF];
271 unsigned bufsize;
272 unsigned numfrag;
273 unsigned fragshift;
274 unsigned wrptr, rdptr;
275 unsigned total_bytes;
276 int count;
277 unsigned error; /* over/underrun */
278 wait_queue_head_t wait;
279 /* redundant, but makes calculations easier */
280 unsigned fragsize;
281 unsigned dmasize;
282 /* OSS stuff */
283 unsigned mapped:1;
284 unsigned ready:1;
285 unsigned ossfragshift;
286 int ossmaxfrags;
287 unsigned subdivision;
288};
289
290struct usb_audio_state;
291
292#define FLG_URB0RUNNING 1
293#define FLG_URB1RUNNING 2
294#define FLG_SYNC0RUNNING 4
295#define FLG_SYNC1RUNNING 8
296#define FLG_RUNNING 16
297#define FLG_CONNECTED 32
298
299struct my_data_urb {
300 struct urb *urb;
301};
302
303struct my_sync_urb {
304 struct urb *urb;
305};
306
307
308struct usb_audiodev {
309 struct list_head list;
310 struct usb_audio_state *state;
311
312 /* soundcore stuff */
313 int dev_audio;
314
315 /* wave stuff */
316 mode_t open_mode;
317 spinlock_t lock; /* DMA buffer access spinlock */
318
319 struct usbin {
320 int interface; /* Interface number, -1 means not used */
321 unsigned int format; /* USB data format */
322 unsigned int datapipe; /* the data input pipe */
323 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but adaptive IN mode */
324 unsigned int syncinterval; /* P for adaptive IN mode, 0 otherwise */
325 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
326 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
327 unsigned int phase; /* phase accumulator */
328 unsigned int flags; /* see FLG_ defines */
329
330 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
331 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
332
333 struct dmabuf dma;
334 } usbin;
335
336 struct usbout {
337 int interface; /* Interface number, -1 means not used */
338 unsigned int format; /* USB data format */
339 unsigned int datapipe; /* the data input pipe */
340 unsigned int syncpipe; /* the synchronisation pipe - 0 for anything but asynchronous OUT mode */
341 unsigned int syncinterval; /* P for asynchronous OUT mode, 0 otherwise */
342 unsigned int freqn; /* nominal sampling rate in USB format, i.e. fs/1000 in Q10.14 */
343 unsigned int freqm; /* momentary sampling rate in USB format, i.e. fs/1000 in Q10.14 */
344 unsigned int freqmax; /* maximum sampling rate, used for buffer management */
345 unsigned int phase; /* phase accumulator */
346 unsigned int flags; /* see FLG_ defines */
347
348 struct my_data_urb durb[2]; /* ISO descriptors for the data endpoint */
349 struct my_sync_urb surb[2]; /* ISO sync pipe descriptor if needed */
350
351 struct dmabuf dma;
352 } usbout;
353
354
355 unsigned int numfmtin, numfmtout;
356 struct audioformat fmtin[MAXFORMATS];
357 struct audioformat fmtout[MAXFORMATS];
358};
359
360struct usb_mixerdev {
361 struct list_head list;
362 struct usb_audio_state *state;
363
364 /* soundcore stuff */
365 int dev_mixer;
366
367 unsigned char iface; /* interface number of the AudioControl interface */
368
369 /* USB format descriptions */
370 unsigned int numch, modcnt;
371
372 /* mixch is last and gets allocated dynamically */
373 struct mixerchannel ch[0];
374};
375
376struct usb_audio_state {
377 struct list_head audiodev;
378
379 /* USB device */
380 struct usb_device *usbdev;
381
382 struct list_head audiolist;
383 struct list_head mixerlist;
384
385 unsigned count; /* usage counter; NOTE: the usb stack is also considered a user */
386};
387
388/* private audio format extensions */
389#define AFMT_STEREO 0x80000000
390#define AFMT_ISSTEREO(x) ((x) & AFMT_STEREO)
391#define AFMT_IS16BIT(x) ((x) & (AFMT_S16_LE|AFMT_S16_BE|AFMT_U16_LE|AFMT_U16_BE))
392#define AFMT_ISUNSIGNED(x) ((x) & (AFMT_U8|AFMT_U16_LE|AFMT_U16_BE))
393#define AFMT_BYTESSHIFT(x) ((AFMT_ISSTEREO(x) ? 1 : 0) + (AFMT_IS16BIT(x) ? 1 : 0))
394#define AFMT_BYTES(x) (1<<AFMT_BYTESSHFIT(x))
395
396/* --------------------------------------------------------------------- */
397
398static inline unsigned ld2(unsigned int x)
399{
400 unsigned r = 0;
401
402 if (x >= 0x10000) {
403 x >>= 16;
404 r += 16;
405 }
406 if (x >= 0x100) {
407 x >>= 8;
408 r += 8;
409 }
410 if (x >= 0x10) {
411 x >>= 4;
412 r += 4;
413 }
414 if (x >= 4) {
415 x >>= 2;
416 r += 2;
417 }
418 if (x >= 2)
419 r++;
420 return r;
421}
422
423/* --------------------------------------------------------------------- */
424
425/*
426 * OSS compatible ring buffer management. The ring buffer may be mmap'ed into
427 * an application address space.
428 *
429 * I first used the rvmalloc stuff copied from bttv. Alan Cox did not like it, so
430 * we now use an array of pointers to a single page each. This saves us the
431 * kernel page table manipulations, but we have to do a page table alike mechanism
432 * (though only one indirection) in software.
433 */
434
435static void dmabuf_release(struct dmabuf *db)
436{
437 unsigned int nr;
438 void *p;
439
440 for(nr = 0; nr < NRSGBUF; nr++) {
441 if (!(p = db->sgbuf[nr]))
442 continue;
443 ClearPageReserved(virt_to_page(p));
444 free_page((unsigned long)p);
445 db->sgbuf[nr] = NULL;
446 }
447 db->mapped = db->ready = 0;
448}
449
450static int dmabuf_init(struct dmabuf *db)
451{
452 unsigned int nr, bytepersec, bufs;
453 void *p;
454
455 /* initialize some fields */
456 db->rdptr = db->wrptr = db->total_bytes = db->count = db->error = 0;
457 /* calculate required buffer size */
458 bytepersec = db->srate << AFMT_BYTESSHIFT(db->format);
459 bufs = 1U << DMABUFSHIFT;
460 if (db->ossfragshift) {
461 if ((1000 << db->ossfragshift) < bytepersec)
462 db->fragshift = ld2(bytepersec/1000);
463 else
464 db->fragshift = db->ossfragshift;
465 } else {
466 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
467 if (db->fragshift < 3)
468 db->fragshift = 3;
469 }
470 db->numfrag = bufs >> db->fragshift;
471 while (db->numfrag < 4 && db->fragshift > 3) {
472 db->fragshift--;
473 db->numfrag = bufs >> db->fragshift;
474 }
475 db->fragsize = 1 << db->fragshift;
476 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
477 db->numfrag = db->ossmaxfrags;
478 db->dmasize = db->numfrag << db->fragshift;
479 for(nr = 0; nr < NRSGBUF; nr++) {
480 if (!db->sgbuf[nr]) {
481 p = (void *)get_zeroed_page(GFP_KERNEL);
482 if (!p)
483 return -ENOMEM;
484 db->sgbuf[nr] = p;
485 SetPageReserved(virt_to_page(p));
486 }
487 memset(db->sgbuf[nr], AFMT_ISUNSIGNED(db->format) ? 0x80 : 0, PAGE_SIZE);
488 if ((nr << PAGE_SHIFT) >= db->dmasize)
489 break;
490 }
491 db->bufsize = nr << PAGE_SHIFT;
492 db->ready = 1;
493 dprintk((KERN_DEBUG "usbaudio: dmabuf_init bytepersec %d bufs %d ossfragshift %d ossmaxfrags %d "
494 "fragshift %d fragsize %d numfrag %d dmasize %d bufsize %d fmt 0x%x srate %d\n",
495 bytepersec, bufs, db->ossfragshift, db->ossmaxfrags, db->fragshift, db->fragsize,
496 db->numfrag, db->dmasize, db->bufsize, db->format, db->srate));
497 return 0;
498}
499
500static int dmabuf_mmap(struct vm_area_struct *vma, struct dmabuf *db, unsigned long start, unsigned long size, pgprot_t prot)
501{
502 unsigned int nr;
503
504 if (!db->ready || db->mapped || (start | size) & (PAGE_SIZE-1) || size > db->bufsize)
505 return -EINVAL;
506 size >>= PAGE_SHIFT;
507 for(nr = 0; nr < size; nr++)
508 if (!db->sgbuf[nr])
509 return -EINVAL;
510 db->mapped = 1;
511 for(nr = 0; nr < size; nr++) {
512 unsigned long pfn;
513
514 pfn = virt_to_phys(db->sgbuf[nr]) >> PAGE_SHIFT;
515 if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, prot))
516 return -EAGAIN;
517 start += PAGE_SIZE;
518 }
519 return 0;
520}
521
522static void dmabuf_copyin(struct dmabuf *db, const void *buffer, unsigned int size)
523{
524 unsigned int pgrem, rem;
525
526 db->total_bytes += size;
527 for (;;) {
528 if (size <= 0)
529 return;
530 pgrem = ((~db->wrptr) & (PAGE_SIZE-1)) + 1;
531 if (pgrem > size)
532 pgrem = size;
533 rem = db->dmasize - db->wrptr;
534 if (pgrem > rem)
535 pgrem = rem;
536 memcpy((db->sgbuf[db->wrptr >> PAGE_SHIFT]) + (db->wrptr & (PAGE_SIZE-1)), buffer, pgrem);
537 size -= pgrem;
538 buffer += pgrem;
539 db->wrptr += pgrem;
540 if (db->wrptr >= db->dmasize)
541 db->wrptr = 0;
542 }
543}
544
545static void dmabuf_copyout(struct dmabuf *db, void *buffer, unsigned int size)
546{
547 unsigned int pgrem, rem;
548
549 db->total_bytes += size;
550 for (;;) {
551 if (size <= 0)
552 return;
553 pgrem = ((~db->rdptr) & (PAGE_SIZE-1)) + 1;
554 if (pgrem > size)
555 pgrem = size;
556 rem = db->dmasize - db->rdptr;
557 if (pgrem > rem)
558 pgrem = rem;
559 memcpy(buffer, (db->sgbuf[db->rdptr >> PAGE_SHIFT]) + (db->rdptr & (PAGE_SIZE-1)), pgrem);
560 size -= pgrem;
561 buffer += pgrem;
562 db->rdptr += pgrem;
563 if (db->rdptr >= db->dmasize)
564 db->rdptr = 0;
565 }
566}
567
568static int dmabuf_copyin_user(struct dmabuf *db, unsigned int ptr, const void __user *buffer, unsigned int size)
569{
570 unsigned int pgrem, rem;
571
572 if (!db->ready || db->mapped)
573 return -EINVAL;
574 for (;;) {
575 if (size <= 0)
576 return 0;
577 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
578 if (pgrem > size)
579 pgrem = size;
580 rem = db->dmasize - ptr;
581 if (pgrem > rem)
582 pgrem = rem;
583 if (copy_from_user((db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), buffer, pgrem))
584 return -EFAULT;
585 size -= pgrem;
586 buffer += pgrem;
587 ptr += pgrem;
588 if (ptr >= db->dmasize)
589 ptr = 0;
590 }
591}
592
593static int dmabuf_copyout_user(struct dmabuf *db, unsigned int ptr, void __user *buffer, unsigned int size)
594{
595 unsigned int pgrem, rem;
596
597 if (!db->ready || db->mapped)
598 return -EINVAL;
599 for (;;) {
600 if (size <= 0)
601 return 0;
602 pgrem = ((~ptr) & (PAGE_SIZE-1)) + 1;
603 if (pgrem > size)
604 pgrem = size;
605 rem = db->dmasize - ptr;
606 if (pgrem > rem)
607 pgrem = rem;
608 if (copy_to_user(buffer, (db->sgbuf[ptr >> PAGE_SHIFT]) + (ptr & (PAGE_SIZE-1)), pgrem))
609 return -EFAULT;
610 size -= pgrem;
611 buffer += pgrem;
612 ptr += pgrem;
613 if (ptr >= db->dmasize)
614 ptr = 0;
615 }
616}
617
618/* --------------------------------------------------------------------- */
619/*
620 * USB I/O code. We do sample format conversion if necessary
621 */
622
623static void usbin_stop(struct usb_audiodev *as)
624{
625 struct usbin *u = &as->usbin;
626 unsigned long flags;
627 unsigned int i, notkilled = 1;
628
629 spin_lock_irqsave(&as->lock, flags);
630 u->flags &= ~FLG_RUNNING;
631 i = u->flags;
632 spin_unlock_irqrestore(&as->lock, flags);
633 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
634 if (notkilled)
635 schedule_timeout_interruptible(1);
636 else
637 schedule_timeout_uninterruptible(1);
638 spin_lock_irqsave(&as->lock, flags);
639 i = u->flags;
640 spin_unlock_irqrestore(&as->lock, flags);
641 if (notkilled && signal_pending(current)) {
642 if (i & FLG_URB0RUNNING)
643 usb_kill_urb(u->durb[0].urb);
644 if (i & FLG_URB1RUNNING)
645 usb_kill_urb(u->durb[1].urb);
646 if (i & FLG_SYNC0RUNNING)
647 usb_kill_urb(u->surb[0].urb);
648 if (i & FLG_SYNC1RUNNING)
649 usb_kill_urb(u->surb[1].urb);
650 notkilled = 0;
651 }
652 }
653 set_current_state(TASK_RUNNING);
654 kfree(u->durb[0].urb->transfer_buffer);
655 kfree(u->durb[1].urb->transfer_buffer);
656 kfree(u->surb[0].urb->transfer_buffer);
657 kfree(u->surb[1].urb->transfer_buffer);
658 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
659 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
660}
661
662static inline void usbin_release(struct usb_audiodev *as)
663{
664 usbin_stop(as);
665}
666
667static void usbin_disc(struct usb_audiodev *as)
668{
669 struct usbin *u = &as->usbin;
670
671 unsigned long flags;
672
673 spin_lock_irqsave(&as->lock, flags);
674 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
675 spin_unlock_irqrestore(&as->lock, flags);
676 usbin_stop(as);
677}
678
679static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt)
680{
681 unsigned int cnt, i;
682 __s16 *sp, *sp2, s;
683 unsigned char *bp;
684
685 cnt = scnt;
686 if (AFMT_ISSTEREO(ifmt))
687 cnt <<= 1;
688 sp = ((__s16 *)tmp) + cnt;
689 switch (ifmt & ~AFMT_STEREO) {
690 case AFMT_U8:
691 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
692 bp--;
693 sp--;
694 *sp = (*bp ^ 0x80) << 8;
695 }
696 break;
697
698 case AFMT_S8:
699 for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) {
700 bp--;
701 sp--;
702 *sp = *bp << 8;
703 }
704 break;
705
706 case AFMT_U16_LE:
707 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
708 bp -= 2;
709 sp--;
710 *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000;
711 }
712 break;
713
714 case AFMT_U16_BE:
715 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
716 bp -= 2;
717 sp--;
718 *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000;
719 }
720 break;
721
722 case AFMT_S16_LE:
723 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
724 bp -= 2;
725 sp--;
726 *sp = bp[0] | (bp[1] << 8);
727 }
728 break;
729
730 case AFMT_S16_BE:
731 for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) {
732 bp -= 2;
733 sp--;
734 *sp = bp[1] | (bp[0] << 8);
735 }
736 break;
737 }
738 if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) {
739 /* expand from mono to stereo */
740 for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) {
741 sp--;
742 sp2 -= 2;
743 sp2[0] = sp2[1] = sp[0];
744 }
745 }
746 if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) {
747 /* contract from stereo to mono */
748 for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2)
749 sp[0] = (sp2[0] + sp2[1]) >> 1;
750 }
751 cnt = scnt;
752 if (AFMT_ISSTEREO(ofmt))
753 cnt <<= 1;
754 sp = ((__s16 *)tmp);
755 bp = ((unsigned char *)obuf);
756 switch (ofmt & ~AFMT_STEREO) {
757 case AFMT_U8:
758 for (i = 0; i < cnt; i++, sp++, bp++)
759 *bp = (*sp >> 8) ^ 0x80;
760 break;
761
762 case AFMT_S8:
763 for (i = 0; i < cnt; i++, sp++, bp++)
764 *bp = *sp >> 8;
765 break;
766
767 case AFMT_U16_LE:
768 for (i = 0; i < cnt; i++, sp++, bp += 2) {
769 s = *sp;
770 bp[0] = s;
771 bp[1] = (s >> 8) ^ 0x80;
772 }
773 break;
774
775 case AFMT_U16_BE:
776 for (i = 0; i < cnt; i++, sp++, bp += 2) {
777 s = *sp;
778 bp[1] = s;
779 bp[0] = (s >> 8) ^ 0x80;
780 }
781 break;
782
783 case AFMT_S16_LE:
784 for (i = 0; i < cnt; i++, sp++, bp += 2) {
785 s = *sp;
786 bp[0] = s;
787 bp[1] = s >> 8;
788 }
789 break;
790
791 case AFMT_S16_BE:
792 for (i = 0; i < cnt; i++, sp++, bp += 2) {
793 s = *sp;
794 bp[1] = s;
795 bp[0] = s >> 8;
796 }
797 break;
798 }
799
800}
801
802static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples)
803{
804 union {
805 __s16 s[64];
806 unsigned char b[0];
807 } tmp;
808 unsigned int scnt, maxs, ufmtsh, dfmtsh;
809
810 ufmtsh = AFMT_BYTESSHIFT(u->format);
811 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
812 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
813 while (samples > 0) {
814 scnt = samples;
815 if (scnt > maxs)
816 scnt = maxs;
817 conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt);
818 dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh);
819 buffer += scnt << ufmtsh;
820 samples -= scnt;
821 }
822}
823
824static int usbin_prepare_desc(struct usbin *u, struct urb *urb)
825{
826 unsigned int i, maxsize, offs;
827
828 maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
829 //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format);
830 for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) {
831 urb->iso_frame_desc[i].length = maxsize;
832 urb->iso_frame_desc[i].offset = offs;
833 }
834 urb->interval = 1;
835 return 0;
836}
837
838/*
839 * return value: 0 if descriptor should be restarted, -1 otherwise
840 * convert sample format on the fly if necessary
841 */
842static int usbin_retire_desc(struct usbin *u, struct urb *urb)
843{
844 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree;
845 unsigned char *cp;
846
847 ufmtsh = AFMT_BYTESSHIFT(u->format);
848 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
849 for (i = 0; i < DESCFRAMES; i++) {
850 cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset;
851 if (urb->iso_frame_desc[i].status) {
852 dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
853 continue;
854 }
855 scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh;
856 if (!scnt)
857 continue;
858 cnt = scnt << dfmtsh;
859 if (!u->dma.mapped) {
860 dmafree = u->dma.dmasize - u->dma.count;
861 if (cnt > dmafree) {
862 scnt = dmafree >> dfmtsh;
863 cnt = scnt << dfmtsh;
864 err++;
865 }
866 }
867 u->dma.count += cnt;
868 if (u->format == u->dma.format) {
869 /* we do not need format conversion */
870 dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n"));
871 dmabuf_copyin(&u->dma, cp, cnt);
872 } else {
873 /* we need sampling format conversion */
874 dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format));
875 usbin_convert(u, cp, scnt);
876 }
877 }
878 if (err)
879 u->dma.error++;
880 if (u->dma.count >= (signed)u->dma.fragsize)
881 wake_up(&u->dma.wait);
882 return err ? -1 : 0;
883}
884
885static void usbin_completed(struct urb *urb, struct pt_regs *regs)
886{
887 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
888 struct usbin *u = &as->usbin;
889 unsigned long flags;
890 unsigned int mask;
891 int suret = 0;
892
893#if 0
894 printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
895#endif
896 if (urb == u->durb[0].urb)
897 mask = FLG_URB0RUNNING;
898 else if (urb == u->durb[1].urb)
899 mask = FLG_URB1RUNNING;
900 else {
901 mask = 0;
902 printk(KERN_ERR "usbin_completed: panic: unknown URB\n");
903 }
904 urb->dev = as->state->usbdev;
905 spin_lock_irqsave(&as->lock, flags);
906 if (!usbin_retire_desc(u, urb) &&
907 u->flags & FLG_RUNNING &&
908 !usbin_prepare_desc(u, urb) &&
909 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
910 u->flags |= mask;
911 } else {
912 u->flags &= ~(mask | FLG_RUNNING);
913 wake_up(&u->dma.wait);
914 printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret);
915 }
916 spin_unlock_irqrestore(&as->lock, flags);
917}
918
919/*
920 * we output sync data
921 */
922static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb)
923{
924 unsigned char *cp = urb->transfer_buffer;
925 unsigned int i, offs;
926
927 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) {
928 urb->iso_frame_desc[i].length = 3;
929 urb->iso_frame_desc[i].offset = offs;
930 cp[0] = u->freqn;
931 cp[1] = u->freqn >> 8;
932 cp[2] = u->freqn >> 16;
933 }
934 urb->interval = 1;
935 return 0;
936}
937
938/*
939 * return value: 0 if descriptor should be restarted, -1 otherwise
940 */
941static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb)
942{
943 unsigned int i;
944
945 for (i = 0; i < SYNCFRAMES; i++)
946 if (urb->iso_frame_desc[0].status)
947 dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
948 return 0;
949}
950
951static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs)
952{
953 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
954 struct usbin *u = &as->usbin;
955 unsigned long flags;
956 unsigned int mask;
957 int suret = 0;
958
959#if 0
960 printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
961#endif
962 if (urb == u->surb[0].urb)
963 mask = FLG_SYNC0RUNNING;
964 else if (urb == u->surb[1].urb)
965 mask = FLG_SYNC1RUNNING;
966 else {
967 mask = 0;
968 printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n");
969 }
970 urb->dev = as->state->usbdev;
971 spin_lock_irqsave(&as->lock, flags);
972 if (!usbin_sync_retire_desc(u, urb) &&
973 u->flags & FLG_RUNNING &&
974 !usbin_sync_prepare_desc(u, urb) &&
975 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
976 u->flags |= mask;
977 } else {
978 u->flags &= ~(mask | FLG_RUNNING);
979 wake_up(&u->dma.wait);
980 dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
981 }
982 spin_unlock_irqrestore(&as->lock, flags);
983}
984
985static int usbin_start(struct usb_audiodev *as)
986{
987 struct usb_device *dev = as->state->usbdev;
988 struct usbin *u = &as->usbin;
989 struct urb *urb;
990 unsigned long flags;
991 unsigned int maxsze, bufsz;
992
993#if 0
994 printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
995 dev->devnum, u->format, u->dma.format, u->dma.srate);
996#endif
997 /* allocate USB storage if not already done */
998 spin_lock_irqsave(&as->lock, flags);
999 if (!(u->flags & FLG_CONNECTED)) {
1000 spin_unlock_irqrestore(&as->lock, flags);
1001 return -EIO;
1002 }
1003 if (!(u->flags & FLG_RUNNING)) {
1004 spin_unlock_irqrestore(&as->lock, flags);
1005 u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1006 u->freqmax = u->freqn + (u->freqn >> 2);
1007 u->phase = 0;
1008 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1009 bufsz = DESCFRAMES * maxsze;
1010 kfree(u->durb[0].urb->transfer_buffer);
1011 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1012 u->durb[0].urb->transfer_buffer_length = bufsz;
1013 kfree(u->durb[1].urb->transfer_buffer);
1014 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1015 u->durb[1].urb->transfer_buffer_length = bufsz;
1016 if (u->syncpipe) {
1017 kfree(u->surb[0].urb->transfer_buffer);
1018 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1019 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1020 kfree(u->surb[1].urb->transfer_buffer);
1021 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1022 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1023 }
1024 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1025 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1026 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1027 return 0;
1028 }
1029 spin_lock_irqsave(&as->lock, flags);
1030 }
1031 if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) {
1032 spin_unlock_irqrestore(&as->lock, flags);
1033 return 0;
1034 }
1035 u->flags |= FLG_RUNNING;
1036 if (!(u->flags & FLG_URB0RUNNING)) {
1037 urb = u->durb[0].urb;
1038 urb->dev = dev;
1039 urb->pipe = u->datapipe;
1040 urb->transfer_flags = URB_ISO_ASAP;
1041 urb->number_of_packets = DESCFRAMES;
1042 urb->context = as;
1043 urb->complete = usbin_completed;
1044 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1045 u->flags |= FLG_URB0RUNNING;
1046 else
1047 u->flags &= ~FLG_RUNNING;
1048 }
1049 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1050 urb = u->durb[1].urb;
1051 urb->dev = dev;
1052 urb->pipe = u->datapipe;
1053 urb->transfer_flags = URB_ISO_ASAP;
1054 urb->number_of_packets = DESCFRAMES;
1055 urb->context = as;
1056 urb->complete = usbin_completed;
1057 if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1058 u->flags |= FLG_URB1RUNNING;
1059 else
1060 u->flags &= ~FLG_RUNNING;
1061 }
1062 if (u->syncpipe) {
1063 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1064 urb = u->surb[0].urb;
1065 urb->dev = dev;
1066 urb->pipe = u->syncpipe;
1067 urb->transfer_flags = URB_ISO_ASAP;
1068 urb->number_of_packets = SYNCFRAMES;
1069 urb->context = as;
1070 urb->complete = usbin_sync_completed;
1071 /* stride: u->syncinterval */
1072 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1073 u->flags |= FLG_SYNC0RUNNING;
1074 else
1075 u->flags &= ~FLG_RUNNING;
1076 }
1077 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1078 urb = u->surb[1].urb;
1079 urb->dev = dev;
1080 urb->pipe = u->syncpipe;
1081 urb->transfer_flags = URB_ISO_ASAP;
1082 urb->number_of_packets = SYNCFRAMES;
1083 urb->context = as;
1084 urb->complete = usbin_sync_completed;
1085 /* stride: u->syncinterval */
1086 if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL))
1087 u->flags |= FLG_SYNC1RUNNING;
1088 else
1089 u->flags &= ~FLG_RUNNING;
1090 }
1091 }
1092 spin_unlock_irqrestore(&as->lock, flags);
1093 return 0;
1094}
1095
1096static void usbout_stop(struct usb_audiodev *as)
1097{
1098 struct usbout *u = &as->usbout;
1099 unsigned long flags;
1100 unsigned int i, notkilled = 1;
1101
1102 spin_lock_irqsave(&as->lock, flags);
1103 u->flags &= ~FLG_RUNNING;
1104 i = u->flags;
1105 spin_unlock_irqrestore(&as->lock, flags);
1106 while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) {
1107 if (notkilled)
1108 schedule_timeout_interruptible(1);
1109 else
1110 schedule_timeout_uninterruptible(1);
1111 spin_lock_irqsave(&as->lock, flags);
1112 i = u->flags;
1113 spin_unlock_irqrestore(&as->lock, flags);
1114 if (notkilled && signal_pending(current)) {
1115 if (i & FLG_URB0RUNNING)
1116 usb_kill_urb(u->durb[0].urb);
1117 if (i & FLG_URB1RUNNING)
1118 usb_kill_urb(u->durb[1].urb);
1119 if (i & FLG_SYNC0RUNNING)
1120 usb_kill_urb(u->surb[0].urb);
1121 if (i & FLG_SYNC1RUNNING)
1122 usb_kill_urb(u->surb[1].urb);
1123 notkilled = 0;
1124 }
1125 }
1126 set_current_state(TASK_RUNNING);
1127 kfree(u->durb[0].urb->transfer_buffer);
1128 kfree(u->durb[1].urb->transfer_buffer);
1129 kfree(u->surb[0].urb->transfer_buffer);
1130 kfree(u->surb[1].urb->transfer_buffer);
1131 u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer =
1132 u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL;
1133}
1134
1135static inline void usbout_release(struct usb_audiodev *as)
1136{
1137 usbout_stop(as);
1138}
1139
1140static void usbout_disc(struct usb_audiodev *as)
1141{
1142 struct usbout *u = &as->usbout;
1143 unsigned long flags;
1144
1145 spin_lock_irqsave(&as->lock, flags);
1146 u->flags &= ~(FLG_RUNNING | FLG_CONNECTED);
1147 spin_unlock_irqrestore(&as->lock, flags);
1148 usbout_stop(as);
1149}
1150
1151static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples)
1152{
1153 union {
1154 __s16 s[64];
1155 unsigned char b[0];
1156 } tmp;
1157 unsigned int scnt, maxs, ufmtsh, dfmtsh;
1158
1159 ufmtsh = AFMT_BYTESSHIFT(u->format);
1160 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1161 maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64;
1162 while (samples > 0) {
1163 scnt = samples;
1164 if (scnt > maxs)
1165 scnt = maxs;
1166 dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh);
1167 conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt);
1168 buffer += scnt << ufmtsh;
1169 samples -= scnt;
1170 }
1171}
1172
1173static int usbout_prepare_desc(struct usbout *u, struct urb *urb)
1174{
1175 unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs;
1176 unsigned char *cp = urb->transfer_buffer;
1177
1178 ufmtsh = AFMT_BYTESSHIFT(u->format);
1179 dfmtsh = AFMT_BYTESSHIFT(u->dma.format);
1180 for (i = offs = 0; i < DESCFRAMES; i++) {
1181 urb->iso_frame_desc[i].offset = offs;
1182 u->phase = (u->phase & 0x3fff) + u->freqm;
1183 scnt = u->phase >> 14;
1184 if (!scnt) {
1185 urb->iso_frame_desc[i].length = 0;
1186 continue;
1187 }
1188 cnt = scnt << dfmtsh;
1189 if (!u->dma.mapped) {
1190 if (cnt > u->dma.count) {
1191 scnt = u->dma.count >> dfmtsh;
1192 cnt = scnt << dfmtsh;
1193 err++;
1194 }
1195 u->dma.count -= cnt;
1196 } else
1197 u->dma.count += cnt;
1198 if (u->format == u->dma.format) {
1199 /* we do not need format conversion */
1200 dmabuf_copyout(&u->dma, cp, cnt);
1201 } else {
1202 /* we need sampling format conversion */
1203 usbout_convert(u, cp, scnt);
1204 }
1205 cnt = scnt << ufmtsh;
1206 urb->iso_frame_desc[i].length = cnt;
1207 offs += cnt;
1208 cp += cnt;
1209 }
1210 urb->interval = 1;
1211 if (err)
1212 u->dma.error++;
1213 if (u->dma.mapped) {
1214 if (u->dma.count >= (signed)u->dma.fragsize)
1215 wake_up(&u->dma.wait);
1216 } else {
1217 if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize)
1218 wake_up(&u->dma.wait);
1219 }
1220 return err ? -1 : 0;
1221}
1222
1223/*
1224 * return value: 0 if descriptor should be restarted, -1 otherwise
1225 */
1226static int usbout_retire_desc(struct usbout *u, struct urb *urb)
1227{
1228 unsigned int i;
1229
1230 for (i = 0; i < DESCFRAMES; i++) {
1231 if (urb->iso_frame_desc[i].status) {
1232 dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1233 continue;
1234 }
1235 }
1236 return 0;
1237}
1238
1239static void usbout_completed(struct urb *urb, struct pt_regs *regs)
1240{
1241 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1242 struct usbout *u = &as->usbout;
1243 unsigned long flags;
1244 unsigned int mask;
1245 int suret = 0;
1246
1247#if 0
1248 printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1249#endif
1250 if (urb == u->durb[0].urb)
1251 mask = FLG_URB0RUNNING;
1252 else if (urb == u->durb[1].urb)
1253 mask = FLG_URB1RUNNING;
1254 else {
1255 mask = 0;
1256 printk(KERN_ERR "usbout_completed: panic: unknown URB\n");
1257 }
1258 urb->dev = as->state->usbdev;
1259 spin_lock_irqsave(&as->lock, flags);
1260 if (!usbout_retire_desc(u, urb) &&
1261 u->flags & FLG_RUNNING &&
1262 !usbout_prepare_desc(u, urb) &&
1263 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1264 u->flags |= mask;
1265 } else {
1266 u->flags &= ~(mask | FLG_RUNNING);
1267 wake_up(&u->dma.wait);
1268 dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1269 }
1270 spin_unlock_irqrestore(&as->lock, flags);
1271}
1272
1273static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb)
1274{
1275 unsigned int i, offs;
1276
1277 for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) {
1278 urb->iso_frame_desc[i].length = 3;
1279 urb->iso_frame_desc[i].offset = offs;
1280 }
1281 urb->interval = 1;
1282 return 0;
1283}
1284
1285/*
1286 * return value: 0 if descriptor should be restarted, -1 otherwise
1287 */
1288static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb)
1289{
1290 unsigned char *cp = urb->transfer_buffer;
1291 unsigned int f, i;
1292
1293 for (i = 0; i < SYNCFRAMES; i++, cp += 3) {
1294 if (urb->iso_frame_desc[i].status) {
1295 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status));
1296 continue;
1297 }
1298 if (urb->iso_frame_desc[i].actual_length < 3) {
1299 dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length));
1300 continue;
1301 }
1302 f = cp[0] | (cp[1] << 8) | (cp[2] << 16);
1303 if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) {
1304 printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn);
1305 continue;
1306 }
1307 u->freqm = f;
1308 }
1309 return 0;
1310}
1311
1312static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs)
1313{
1314 struct usb_audiodev *as = (struct usb_audiodev *)urb->context;
1315 struct usbout *u = &as->usbout;
1316 unsigned long flags;
1317 unsigned int mask;
1318 int suret = 0;
1319
1320#if 0
1321 printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags);
1322#endif
1323 if (urb == u->surb[0].urb)
1324 mask = FLG_SYNC0RUNNING;
1325 else if (urb == u->surb[1].urb)
1326 mask = FLG_SYNC1RUNNING;
1327 else {
1328 mask = 0;
1329 printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n");
1330 }
1331 urb->dev = as->state->usbdev;
1332 spin_lock_irqsave(&as->lock, flags);
1333 if (!usbout_sync_retire_desc(u, urb) &&
1334 u->flags & FLG_RUNNING &&
1335 !usbout_sync_prepare_desc(u, urb) &&
1336 (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) {
1337 u->flags |= mask;
1338 } else {
1339 u->flags &= ~(mask | FLG_RUNNING);
1340 wake_up(&u->dma.wait);
1341 dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret));
1342 }
1343 spin_unlock_irqrestore(&as->lock, flags);
1344}
1345
1346static int usbout_start(struct usb_audiodev *as)
1347{
1348 struct usb_device *dev = as->state->usbdev;
1349 struct usbout *u = &as->usbout;
1350 struct urb *urb;
1351 unsigned long flags;
1352 unsigned int maxsze, bufsz;
1353
1354#if 0
1355 printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n",
1356 dev->devnum, u->format, u->dma.format, u->dma.srate);
1357#endif
1358 /* allocate USB storage if not already done */
1359 spin_lock_irqsave(&as->lock, flags);
1360 if (!(u->flags & FLG_CONNECTED)) {
1361 spin_unlock_irqrestore(&as->lock, flags);
1362 return -EIO;
1363 }
1364 if (!(u->flags & FLG_RUNNING)) {
1365 spin_unlock_irqrestore(&as->lock, flags);
1366 u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */
1367 u->freqmax = u->freqn + (u->freqn >> 2);
1368 u->phase = 0;
1369 maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format));
1370 bufsz = DESCFRAMES * maxsze;
1371 kfree(u->durb[0].urb->transfer_buffer);
1372 u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1373 u->durb[0].urb->transfer_buffer_length = bufsz;
1374 kfree(u->durb[1].urb->transfer_buffer);
1375 u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL);
1376 u->durb[1].urb->transfer_buffer_length = bufsz;
1377 if (u->syncpipe) {
1378 kfree(u->surb[0].urb->transfer_buffer);
1379 u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1380 u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES;
1381 kfree(u->surb[1].urb->transfer_buffer);
1382 u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL);
1383 u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES;
1384 }
1385 if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer ||
1386 (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) {
1387 printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum);
1388 return 0;
1389 }
1390 spin_lock_irqsave(&as->lock, flags);
1391 }
1392 if (u->dma.count <= 0 && !u->dma.mapped) {
1393 spin_unlock_irqrestore(&as->lock, flags);
1394 return 0;
1395 }
1396 u->flags |= FLG_RUNNING;
1397 if (!(u->flags & FLG_URB0RUNNING)) {
1398 urb = u->durb[0].urb;
1399 urb->dev = dev;
1400 urb->pipe = u->datapipe;
1401 urb->transfer_flags = URB_ISO_ASAP;
1402 urb->number_of_packets = DESCFRAMES;
1403 urb->context = as;
1404 urb->complete = usbout_completed;
1405 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1406 u->flags |= FLG_URB0RUNNING;
1407 else
1408 u->flags &= ~FLG_RUNNING;
1409 }
1410 if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) {
1411 urb = u->durb[1].urb;
1412 urb->dev = dev;
1413 urb->pipe = u->datapipe;
1414 urb->transfer_flags = URB_ISO_ASAP;
1415 urb->number_of_packets = DESCFRAMES;
1416 urb->context = as;
1417 urb->complete = usbout_completed;
1418 if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1419 u->flags |= FLG_URB1RUNNING;
1420 else
1421 u->flags &= ~FLG_RUNNING;
1422 }
1423 if (u->syncpipe) {
1424 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) {
1425 urb = u->surb[0].urb;
1426 urb->dev = dev;
1427 urb->pipe = u->syncpipe;
1428 urb->transfer_flags = URB_ISO_ASAP;
1429 urb->number_of_packets = SYNCFRAMES;
1430 urb->context = as;
1431 urb->complete = usbout_sync_completed;
1432 /* stride: u->syncinterval */
1433 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1434 u->flags |= FLG_SYNC0RUNNING;
1435 else
1436 u->flags &= ~FLG_RUNNING;
1437 }
1438 if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) {
1439 urb = u->surb[1].urb;
1440 urb->dev = dev;
1441 urb->pipe = u->syncpipe;
1442 urb->transfer_flags = URB_ISO_ASAP;
1443 urb->number_of_packets = SYNCFRAMES;
1444 urb->context = as;
1445 urb->complete = usbout_sync_completed;
1446 /* stride: u->syncinterval */
1447 if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC))
1448 u->flags |= FLG_SYNC1RUNNING;
1449 else
1450 u->flags &= ~FLG_RUNNING;
1451 }
1452 }
1453 spin_unlock_irqrestore(&as->lock, flags);
1454 return 0;
1455}
1456
1457/* --------------------------------------------------------------------- */
1458
1459static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate)
1460{
1461 unsigned int g = 0;
1462
1463 if (srate < afp->sratelo)
1464 g += afp->sratelo - srate;
1465 if (srate > afp->sratehi)
1466 g += srate - afp->sratehi;
1467 if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt))
1468 g += 0x100000;
1469 if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt))
1470 g += 0x400000;
1471 if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt))
1472 g += 0x100000;
1473 if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt))
1474 g += 0x400000;
1475 return g;
1476}
1477
1478static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate)
1479{
1480 unsigned int i, g, gb = ~0;
1481 int j = -1; /* default to failure */
1482
1483 /* find "best" format (according to format_goodness) */
1484 for (i = 0; i < nr; i++) {
1485 g = format_goodness(&afp[i], fmt, srate);
1486 if (g >= gb)
1487 continue;
1488 j = i;
1489 gb = g;
1490 }
1491 return j;
1492}
1493
1494static int set_format_in(struct usb_audiodev *as)
1495{
1496 struct usb_device *dev = as->state->usbdev;
1497 struct usb_host_interface *alts;
1498 struct usb_interface *iface;
1499 struct usbin *u = &as->usbin;
1500 struct dmabuf *d = &u->dma;
1501 struct audioformat *fmt;
1502 unsigned int ep;
1503 unsigned char data[3];
1504 int fmtnr, ret;
1505
1506 iface = usb_ifnum_to_if(dev, u->interface);
1507 if (!iface)
1508 return 0;
1509
1510 fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate);
1511 if (fmtnr < 0) {
1512 printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n");
1513 return -1;
1514 }
1515
1516 fmt = as->fmtin + fmtnr;
1517 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1518 u->format = fmt->format;
1519 u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1520 u->syncpipe = u->syncinterval = 0;
1521 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) {
1522 if (alts->desc.bNumEndpoints < 2 ||
1523 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1524 alts->endpoint[1].desc.bSynchAddress != 0 ||
1525 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) {
1526 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in "
1527 "but has invalid synch pipe; treating as asynchronous in\n",
1528 dev->devnum, u->interface, fmt->altsetting);
1529 } else {
1530 u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1531 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1532 }
1533 }
1534 if (d->srate < fmt->sratelo)
1535 d->srate = fmt->sratelo;
1536 if (d->srate > fmt->sratehi)
1537 d->srate = fmt->sratehi;
1538 dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n",
1539 u->interface, fmt->altsetting));
1540 if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) {
1541 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1542 dev->devnum, u->interface, fmt->altsetting);
1543 return -1;
1544 }
1545 if (fmt->sratelo == fmt->sratehi)
1546 return 0;
1547 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1548 /* if endpoint has pitch control, enable it */
1549 if (fmt->attributes & 0x02) {
1550 data[0] = 1;
1551 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1552 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1553 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1554 ret, dev->devnum, u->interface, ep, d->srate);
1555 return -1;
1556 }
1557 }
1558 /* if endpoint has sampling rate control, set it */
1559 if (fmt->attributes & 0x01) {
1560 data[0] = d->srate;
1561 data[1] = d->srate >> 8;
1562 data[2] = d->srate >> 16;
1563 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1564 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1565 printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1566 ret, dev->devnum, u->interface, ep, d->srate);
1567 return -1;
1568 }
1569 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1570 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1571 printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n",
1572 ret, dev->devnum, u->interface, ep);
1573 return -1;
1574 }
1575 dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n",
1576 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1577 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1578 }
1579 dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1580 return 0;
1581}
1582
1583static int set_format_out(struct usb_audiodev *as)
1584{
1585 struct usb_device *dev = as->state->usbdev;
1586 struct usb_host_interface *alts;
1587 struct usb_interface *iface;
1588 struct usbout *u = &as->usbout;
1589 struct dmabuf *d = &u->dma;
1590 struct audioformat *fmt;
1591 unsigned int ep;
1592 unsigned char data[3];
1593 int fmtnr, ret;
1594
1595 iface = usb_ifnum_to_if(dev, u->interface);
1596 if (!iface)
1597 return 0;
1598
1599 fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate);
1600 if (fmtnr < 0) {
1601 printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n");
1602 return -1;
1603 }
1604
1605 fmt = as->fmtout + fmtnr;
1606 u->format = fmt->format;
1607 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
1608 u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf);
1609 u->syncpipe = u->syncinterval = 0;
1610 if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) {
1611#if 0
1612 printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n"
1613 KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n"
1614 KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints,
1615 alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress,
1616 alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress);
1617#endif
1618 if (alts->desc.bNumEndpoints < 2 ||
1619 alts->endpoint[1].desc.bmAttributes != 0x01 ||
1620 alts->endpoint[1].desc.bSynchAddress != 0 ||
1621 alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) {
1622 printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out "
1623 "but has invalid synch pipe; treating as adaptive out\n",
1624 dev->devnum, u->interface, fmt->altsetting);
1625 } else {
1626 u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf);
1627 u->syncinterval = alts->endpoint[1].desc.bRefresh;
1628 }
1629 }
1630 if (d->srate < fmt->sratelo)
1631 d->srate = fmt->sratelo;
1632 if (d->srate > fmt->sratehi)
1633 d->srate = fmt->sratehi;
1634 dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n",
1635 u->interface, fmt->altsetting));
1636 if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) {
1637 printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n",
1638 dev->devnum, u->interface, fmt->altsetting);
1639 return -1;
1640 }
1641 if (fmt->sratelo == fmt->sratehi)
1642 return 0;
1643 ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN);
1644 /* if endpoint has pitch control, enable it */
1645 if (fmt->attributes & 0x02) {
1646 data[0] = 1;
1647 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1648 PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
1649 printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n",
1650 ret, dev->devnum, u->interface, ep, d->srate);
1651 return -1;
1652 }
1653 }
1654 /* if endpoint has sampling rate control, set it */
1655 if (fmt->attributes & 0x01) {
1656 data[0] = d->srate;
1657 data[1] = d->srate >> 8;
1658 data[2] = d->srate >> 16;
1659 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1660 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1661 printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n",
1662 ret, dev->devnum, u->interface, ep, d->srate);
1663 return -1;
1664 }
1665 if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1666 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1667 printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n",
1668 ret, dev->devnum, u->interface, ep);
1669 return -1;
1670 }
1671 dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n",
1672 dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16)));
1673 d->srate = data[0] | (data[1] << 8) | (data[2] << 16);
1674 }
1675 dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate));
1676 return 0;
1677}
1678
1679static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate)
1680{
1681 int ret1 = 0, ret2 = 0;
1682
1683 if (!(fmode & (FMODE_READ|FMODE_WRITE)))
1684 return -EINVAL;
1685 if (fmode & FMODE_READ) {
1686 usbin_stop(s);
1687 s->usbin.dma.ready = 0;
1688 if (fmt == AFMT_QUERY)
1689 fmt = s->usbin.dma.format;
1690 else
1691 s->usbin.dma.format = fmt;
1692 if (!srate)
1693 srate = s->usbin.dma.srate;
1694 else
1695 s->usbin.dma.srate = srate;
1696 }
1697 if (fmode & FMODE_WRITE) {
1698 usbout_stop(s);
1699 s->usbout.dma.ready = 0;
1700 if (fmt == AFMT_QUERY)
1701 fmt = s->usbout.dma.format;
1702 else
1703 s->usbout.dma.format = fmt;
1704 if (!srate)
1705 srate = s->usbout.dma.srate;
1706 else
1707 s->usbout.dma.srate = srate;
1708 }
1709 if (fmode & FMODE_READ)
1710 ret1 = set_format_in(s);
1711 if (fmode & FMODE_WRITE)
1712 ret2 = set_format_out(s);
1713 return ret1 ? ret1 : ret2;
1714}
1715
1716/* --------------------------------------------------------------------- */
1717
1718static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value)
1719{
1720 struct usb_device *dev = ms->state->usbdev;
1721 unsigned char data[2];
1722 struct mixerchannel *ch;
1723 int v1, v2, v3;
1724
1725 if (mixch >= ms->numch)
1726 return -1;
1727 ch = &ms->ch[mixch];
1728 v3 = ch->maxval - ch->minval;
1729 v1 = value & 0xff;
1730 v2 = (value >> 8) & 0xff;
1731 if (v1 > 100)
1732 v1 = 100;
1733 if (v2 > 100)
1734 v2 = 100;
1735 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1736 v2 = v1;
1737 ch->value = v1 | (v2 << 8);
1738 v1 = (v1 * v3) / 100 + ch->minval;
1739 v2 = (v2 * v3) / 100 + ch->minval;
1740 switch (ch->selector) {
1741 case 0: /* mixer unit request */
1742 data[0] = v1;
1743 data[1] = v1 >> 8;
1744 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1745 (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1746 goto err;
1747 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1748 return 0;
1749 data[0] = v2;
1750 data[1] = v2 >> 8;
1751 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1752 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
1753 ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1754 goto err;
1755 return 0;
1756
1757 /* various feature unit controls */
1758 case VOLUME_CONTROL:
1759 data[0] = v1;
1760 data[1] = v1 >> 8;
1761 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1762 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1763 goto err;
1764 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1765 return 0;
1766 data[0] = v2;
1767 data[1] = v2 >> 8;
1768 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1769 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, 1000) < 0)
1770 goto err;
1771 return 0;
1772
1773 case BASS_CONTROL:
1774 case MID_CONTROL:
1775 case TREBLE_CONTROL:
1776 data[0] = v1 >> 8;
1777 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1778 (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1779 goto err;
1780 if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
1781 return 0;
1782 data[0] = v2 >> 8;
1783 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1784 (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, 1000) < 0)
1785 goto err;
1786 return 0;
1787
1788 default:
1789 return -1;
1790 }
1791 return 0;
1792
1793 err:
1794 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
1795 dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector);
1796 return -1;
1797}
1798
1799static int get_rec_src(struct usb_mixerdev *ms)
1800{
1801 struct usb_device *dev = ms->state->usbdev;
1802 unsigned int mask = 0, retmask = 0;
1803 unsigned int i, j;
1804 unsigned char buf;
1805 int err = 0;
1806
1807 for (i = 0; i < ms->numch; i++) {
1808 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1809 continue;
1810 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1811 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1812 err = -EIO;
1813 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1814 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1815 continue;
1816 }
1817 for (j = i; j < ms->numch; j++) {
1818 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1819 continue;
1820 mask |= 1 << j;
1821 if (buf == (ms->ch[j].slctunitid >> 8))
1822 retmask |= 1 << ms->ch[j].osschannel;
1823 }
1824 }
1825 if (err)
1826 return -EIO;
1827 return retmask;
1828}
1829
1830static int set_rec_src(struct usb_mixerdev *ms, int srcmask)
1831{
1832 struct usb_device *dev = ms->state->usbdev;
1833 unsigned int mask = 0, smask, bmask;
1834 unsigned int i, j;
1835 unsigned char buf;
1836 int err = 0;
1837
1838 for (i = 0; i < ms->numch; i++) {
1839 if (!ms->ch[i].slctunitid || (mask & (1 << i)))
1840 continue;
1841 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1842 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) {
1843 err = -EIO;
1844 printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n",
1845 dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff);
1846 continue;
1847 }
1848 /* first generate smask */
1849 smask = bmask = 0;
1850 for (j = i; j < ms->numch; j++) {
1851 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1852 continue;
1853 smask |= 1 << ms->ch[j].osschannel;
1854 if (buf == (ms->ch[j].slctunitid >> 8))
1855 bmask |= 1 << ms->ch[j].osschannel;
1856 mask |= 1 << j;
1857 }
1858 /* check for multiple set sources */
1859 j = hweight32(srcmask & smask);
1860 if (j == 0)
1861 continue;
1862 if (j > 1)
1863 srcmask &= ~bmask;
1864 for (j = i; j < ms->numch; j++) {
1865 if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff)
1866 continue;
1867 if (!(srcmask & (1 << ms->ch[j].osschannel)))
1868 continue;
1869 buf = ms->ch[j].slctunitid >> 8;
1870 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1871 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, 1000) < 0) {
1872 err = -EIO;
1873 printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n",
1874 dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff);
1875 continue;
1876 }
1877 }
1878 }
1879 return err ? -EIO : 0;
1880}
1881
1882/* --------------------------------------------------------------------- */
1883
1884/*
1885 * should be called with open_sem hold, so that no new processes
1886 * look at the audio device to be destroyed
1887 */
1888
1889static void release(struct usb_audio_state *s)
1890{
1891 struct usb_audiodev *as;
1892 struct usb_mixerdev *ms;
1893
1894 s->count--;
1895 if (s->count) {
1896 up(&open_sem);
1897 return;
1898 }
1899 up(&open_sem);
1900 wake_up(&open_wait);
1901 while (!list_empty(&s->audiolist)) {
1902 as = list_entry(s->audiolist.next, struct usb_audiodev, list);
1903 list_del(&as->list);
1904 usbin_release(as);
1905 usbout_release(as);
1906 dmabuf_release(&as->usbin.dma);
1907 dmabuf_release(&as->usbout.dma);
1908 usb_free_urb(as->usbin.durb[0].urb);
1909 usb_free_urb(as->usbin.durb[1].urb);
1910 usb_free_urb(as->usbin.surb[0].urb);
1911 usb_free_urb(as->usbin.surb[1].urb);
1912 usb_free_urb(as->usbout.durb[0].urb);
1913 usb_free_urb(as->usbout.durb[1].urb);
1914 usb_free_urb(as->usbout.surb[0].urb);
1915 usb_free_urb(as->usbout.surb[1].urb);
1916 kfree(as);
1917 }
1918 while (!list_empty(&s->mixerlist)) {
1919 ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list);
1920 list_del(&ms->list);
1921 kfree(ms);
1922 }
1923 kfree(s);
1924}
1925
1926static inline int prog_dmabuf_in(struct usb_audiodev *as)
1927{
1928 usbin_stop(as);
1929 return dmabuf_init(&as->usbin.dma);
1930}
1931
1932static inline int prog_dmabuf_out(struct usb_audiodev *as)
1933{
1934 usbout_stop(as);
1935 return dmabuf_init(&as->usbout.dma);
1936}
1937
1938/* --------------------------------------------------------------------- */
1939
1940static int usb_audio_open_mixdev(struct inode *inode, struct file *file)
1941{
1942 unsigned int minor = iminor(inode);
1943 struct usb_mixerdev *ms;
1944 struct usb_audio_state *s;
1945
1946 down(&open_sem);
1947 list_for_each_entry(s, &audiodevs, audiodev) {
1948 list_for_each_entry(ms, &s->mixerlist, list) {
1949 if (ms->dev_mixer == minor)
1950 goto mixer_found;
1951 }
1952 }
1953 up(&open_sem);
1954 return -ENODEV;
1955
1956 mixer_found:
1957 if (!s->usbdev) {
1958 up(&open_sem);
1959 return -EIO;
1960 }
1961 file->private_data = ms;
1962 s->count++;
1963
1964 up(&open_sem);
1965 return nonseekable_open(inode, file);
1966}
1967
1968static int usb_audio_release_mixdev(struct inode *inode, struct file *file)
1969{
1970 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1971 struct usb_audio_state *s;
1972
1973 lock_kernel();
1974 s = ms->state;
1975 down(&open_sem);
1976 release(s);
1977 unlock_kernel();
1978 return 0;
1979}
1980
1981static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1982{
1983 struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data;
1984 int i, j, val;
1985 int __user *user_arg = (int __user *)arg;
1986
1987 if (!ms->state->usbdev)
1988 return -ENODEV;
1989
1990 if (cmd == SOUND_MIXER_INFO) {
1991 mixer_info info;
1992
1993 memset(&info, 0, sizeof(info));
1994 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
1995 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
1996 info.modify_counter = ms->modcnt;
1997 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1998 return -EFAULT;
1999 return 0;
2000 }
2001 if (cmd == SOUND_OLD_MIXER_INFO) {
2002 _old_mixer_info info;
2003
2004 memset(&info, 0, sizeof(info));
2005 strncpy(info.id, "USB_AUDIO", sizeof(info.id));
2006 strncpy(info.name, "USB Audio Class Driver", sizeof(info.name));
2007 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
2008 return -EFAULT;
2009 return 0;
2010 }
2011 if (cmd == OSS_GETVERSION)
2012 return put_user(SOUND_VERSION, user_arg);
2013 if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
2014 return -EINVAL;
2015 if (_IOC_DIR(cmd) == _IOC_READ) {
2016 switch (_IOC_NR(cmd)) {
2017 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2018 val = get_rec_src(ms);
2019 if (val < 0)
2020 return val;
2021 return put_user(val, user_arg);
2022
2023 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
2024 for (val = i = 0; i < ms->numch; i++)
2025 val |= 1 << ms->ch[i].osschannel;
2026 return put_user(val, user_arg);
2027
2028 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
2029 for (val = i = 0; i < ms->numch; i++)
2030 if (ms->ch[i].slctunitid)
2031 val |= 1 << ms->ch[i].osschannel;
2032 return put_user(val, user_arg);
2033
2034 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
2035 for (val = i = 0; i < ms->numch; i++)
2036 if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))
2037 val |= 1 << ms->ch[i].osschannel;
2038 return put_user(val, user_arg);
2039
2040 case SOUND_MIXER_CAPS:
2041 return put_user(SOUND_CAP_EXCL_INPUT, user_arg);
2042
2043 default:
2044 i = _IOC_NR(cmd);
2045 if (i >= SOUND_MIXER_NRDEVICES)
2046 return -EINVAL;
2047 for (j = 0; j < ms->numch; j++) {
2048 if (ms->ch[j].osschannel == i) {
2049 return put_user(ms->ch[j].value, user_arg);
2050 }
2051 }
2052 return -EINVAL;
2053 }
2054 }
2055 if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE))
2056 return -EINVAL;
2057 ms->modcnt++;
2058 switch (_IOC_NR(cmd)) {
2059 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
2060 if (get_user(val, user_arg))
2061 return -EFAULT;
2062 return set_rec_src(ms, val);
2063
2064 default:
2065 i = _IOC_NR(cmd);
2066 if (i >= SOUND_MIXER_NRDEVICES)
2067 return -EINVAL;
2068 for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++);
2069 if (j >= ms->numch)
2070 return -EINVAL;
2071 if (get_user(val, user_arg))
2072 return -EFAULT;
2073 if (wrmixer(ms, j, val))
2074 return -EIO;
2075 return put_user(ms->ch[j].value, user_arg);
2076 }
2077}
2078
2079static /*const*/ struct file_operations usb_mixer_fops = {
2080 .owner = THIS_MODULE,
2081 .llseek = no_llseek,
2082 .ioctl = usb_audio_ioctl_mixdev,
2083 .open = usb_audio_open_mixdev,
2084 .release = usb_audio_release_mixdev,
2085};
2086
2087/* --------------------------------------------------------------------- */
2088
2089static int drain_out(struct usb_audiodev *as, int nonblock)
2090{
2091 DECLARE_WAITQUEUE(wait, current);
2092 unsigned long flags;
2093 int count, tmo;
2094
2095 if (as->usbout.dma.mapped || !as->usbout.dma.ready)
2096 return 0;
2097 usbout_start(as);
2098 add_wait_queue(&as->usbout.dma.wait, &wait);
2099 for (;;) {
2100 __set_current_state(TASK_INTERRUPTIBLE);
2101 spin_lock_irqsave(&as->lock, flags);
2102 count = as->usbout.dma.count;
2103 spin_unlock_irqrestore(&as->lock, flags);
2104 if (count <= 0)
2105 break;
2106 if (signal_pending(current))
2107 break;
2108 if (nonblock) {
2109 remove_wait_queue(&as->usbout.dma.wait, &wait);
2110 set_current_state(TASK_RUNNING);
2111 return -EBUSY;
2112 }
2113 tmo = 3 * HZ * count / as->usbout.dma.srate;
2114 tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format);
2115 if (!schedule_timeout(tmo + 1)) {
2116 printk(KERN_DEBUG "usbaudio: dma timed out??\n");
2117 break;
2118 }
2119 }
2120 remove_wait_queue(&as->usbout.dma.wait, &wait);
2121 set_current_state(TASK_RUNNING);
2122 if (signal_pending(current))
2123 return -ERESTARTSYS;
2124 return 0;
2125}
2126
2127/* --------------------------------------------------------------------- */
2128
2129static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2130{
2131 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2132 DECLARE_WAITQUEUE(wait, current);
2133 ssize_t ret = 0;
2134 unsigned long flags;
2135 unsigned int ptr;
2136 int cnt, err;
2137
2138 if (as->usbin.dma.mapped)
2139 return -ENXIO;
2140 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2141 return ret;
2142 if (!access_ok(VERIFY_WRITE, buffer, count))
2143 return -EFAULT;
2144 add_wait_queue(&as->usbin.dma.wait, &wait);
2145 while (count > 0) {
2146 spin_lock_irqsave(&as->lock, flags);
2147 ptr = as->usbin.dma.rdptr;
2148 cnt = as->usbin.dma.count;
2149 /* set task state early to avoid wakeup races */
2150 if (cnt <= 0)
2151 __set_current_state(TASK_INTERRUPTIBLE);
2152 spin_unlock_irqrestore(&as->lock, flags);
2153 if (cnt > count)
2154 cnt = count;
2155 if (cnt <= 0) {
2156 if (usbin_start(as)) {
2157 if (!ret)
2158 ret = -ENODEV;
2159 break;
2160 }
2161 if (file->f_flags & O_NONBLOCK) {
2162 if (!ret)
2163 ret = -EAGAIN;
2164 break;
2165 }
2166 schedule();
2167 if (signal_pending(current)) {
2168 if (!ret)
2169 ret = -ERESTARTSYS;
2170 break;
2171 }
2172 continue;
2173 }
2174 if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) {
2175 if (!ret)
2176 ret = err;
2177 break;
2178 }
2179 ptr += cnt;
2180 if (ptr >= as->usbin.dma.dmasize)
2181 ptr -= as->usbin.dma.dmasize;
2182 spin_lock_irqsave(&as->lock, flags);
2183 as->usbin.dma.rdptr = ptr;
2184 as->usbin.dma.count -= cnt;
2185 spin_unlock_irqrestore(&as->lock, flags);
2186 count -= cnt;
2187 buffer += cnt;
2188 ret += cnt;
2189 }
2190 __set_current_state(TASK_RUNNING);
2191 remove_wait_queue(&as->usbin.dma.wait, &wait);
2192 return ret;
2193}
2194
2195static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2196{
2197 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2198 DECLARE_WAITQUEUE(wait, current);
2199 ssize_t ret = 0;
2200 unsigned long flags;
2201 unsigned int ptr;
2202 unsigned int start_thr;
2203 int cnt, err;
2204
2205 if (as->usbout.dma.mapped)
2206 return -ENXIO;
2207 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2208 return ret;
2209 if (!access_ok(VERIFY_READ, buffer, count))
2210 return -EFAULT;
2211 start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES));
2212 add_wait_queue(&as->usbout.dma.wait, &wait);
2213 while (count > 0) {
2214#if 0
2215 printk(KERN_DEBUG "usb_audio_write: count %u dma: count %u rdptr %u wrptr %u dmasize %u fragsize %u flags 0x%02x taskst 0x%lx\n",
2216 count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize,
2217 as->usbout.flags, current->state);
2218#endif
2219 spin_lock_irqsave(&as->lock, flags);
2220 if (as->usbout.dma.count < 0) {
2221 as->usbout.dma.count = 0;
2222 as->usbout.dma.rdptr = as->usbout.dma.wrptr;
2223 }
2224 ptr = as->usbout.dma.wrptr;
2225 cnt = as->usbout.dma.dmasize - as->usbout.dma.count;
2226 /* set task state early to avoid wakeup races */
2227 if (cnt <= 0)
2228 __set_current_state(TASK_INTERRUPTIBLE);
2229 spin_unlock_irqrestore(&as->lock, flags);
2230 if (cnt > count)
2231 cnt = count;
2232 if (cnt <= 0) {
2233 if (usbout_start(as)) {
2234 if (!ret)
2235 ret = -ENODEV;
2236 break;
2237 }
2238 if (file->f_flags & O_NONBLOCK) {
2239 if (!ret)
2240 ret = -EAGAIN;
2241 break;
2242 }
2243 schedule();
2244 if (signal_pending(current)) {
2245 if (!ret)
2246 ret = -ERESTARTSYS;
2247 break;
2248 }
2249 continue;
2250 }
2251 if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) {
2252 if (!ret)
2253 ret = err;
2254 break;
2255 }
2256 ptr += cnt;
2257 if (ptr >= as->usbout.dma.dmasize)
2258 ptr -= as->usbout.dma.dmasize;
2259 spin_lock_irqsave(&as->lock, flags);
2260 as->usbout.dma.wrptr = ptr;
2261 as->usbout.dma.count += cnt;
2262 spin_unlock_irqrestore(&as->lock, flags);
2263 count -= cnt;
2264 buffer += cnt;
2265 ret += cnt;
2266 if (as->usbout.dma.count >= start_thr && usbout_start(as)) {
2267 if (!ret)
2268 ret = -ENODEV;
2269 break;
2270 }
2271 }
2272 __set_current_state(TASK_RUNNING);
2273 remove_wait_queue(&as->usbout.dma.wait, &wait);
2274 return ret;
2275}
2276
2277/* Called without the kernel lock - fine */
2278static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait)
2279{
2280 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2281 unsigned long flags;
2282 unsigned int mask = 0;
2283
2284 if (file->f_mode & FMODE_WRITE) {
2285 if (!as->usbout.dma.ready)
2286 prog_dmabuf_out(as);
2287 poll_wait(file, &as->usbout.dma.wait, wait);
2288 }
2289 if (file->f_mode & FMODE_READ) {
2290 if (!as->usbin.dma.ready)
2291 prog_dmabuf_in(as);
2292 poll_wait(file, &as->usbin.dma.wait, wait);
2293 }
2294 spin_lock_irqsave(&as->lock, flags);
2295 if (file->f_mode & FMODE_READ) {
2296 if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize)
2297 mask |= POLLIN | POLLRDNORM;
2298 }
2299 if (file->f_mode & FMODE_WRITE) {
2300 if (as->usbout.dma.mapped) {
2301 if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize)
2302 mask |= POLLOUT | POLLWRNORM;
2303 } else {
2304 if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize)
2305 mask |= POLLOUT | POLLWRNORM;
2306 }
2307 }
2308 spin_unlock_irqrestore(&as->lock, flags);
2309 return mask;
2310}
2311
2312static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma)
2313{
2314 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2315 struct dmabuf *db;
2316 int ret = -EINVAL;
2317
2318 lock_kernel();
2319 if (vma->vm_flags & VM_WRITE) {
2320 if ((ret = prog_dmabuf_out(as)) != 0)
2321 goto out;
2322 db = &as->usbout.dma;
2323 } else if (vma->vm_flags & VM_READ) {
2324 if ((ret = prog_dmabuf_in(as)) != 0)
2325 goto out;
2326 db = &as->usbin.dma;
2327 } else
2328 goto out;
2329
2330 ret = -EINVAL;
2331 if (vma->vm_pgoff != 0)
2332 goto out;
2333
2334 ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot);
2335out:
2336 unlock_kernel();
2337 return ret;
2338}
2339
2340static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2341{
2342 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2343 struct usb_audio_state *s = as->state;
2344 int __user *user_arg = (int __user *)arg;
2345 unsigned long flags;
2346 audio_buf_info abinfo;
2347 count_info cinfo;
2348 int val = 0;
2349 int val2, mapped, ret;
2350
2351 if (!s->usbdev)
2352 return -EIO;
2353 mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) ||
2354 ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped);
2355#if 0
2356 if (arg)
2357 get_user(val, (int *)arg);
2358 printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val)
2359#endif
2360 switch (cmd) {
2361 case OSS_GETVERSION:
2362 return put_user(SOUND_VERSION, user_arg);
2363
2364 case SNDCTL_DSP_SYNC:
2365 if (file->f_mode & FMODE_WRITE)
2366 return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/);
2367 return 0;
2368
2369 case SNDCTL_DSP_SETDUPLEX:
2370 return 0;
2371
2372 case SNDCTL_DSP_GETCAPS:
2373 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2374 DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg);
2375
2376 case SNDCTL_DSP_RESET:
2377 if (file->f_mode & FMODE_WRITE) {
2378 usbout_stop(as);
2379 as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0;
2380 }
2381 if (file->f_mode & FMODE_READ) {
2382 usbin_stop(as);
2383 as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0;
2384 }
2385 return 0;
2386
2387 case SNDCTL_DSP_SPEED:
2388 if (get_user(val, user_arg))
2389 return -EFAULT;
2390 if (val >= 0) {
2391 if (val < 4000)
2392 val = 4000;
2393 if (val > 100000)
2394 val = 100000;
2395 if (set_format(as, file->f_mode, AFMT_QUERY, val))
2396 return -EIO;
2397 }
2398 return put_user((file->f_mode & FMODE_READ) ?
2399 as->usbin.dma.srate : as->usbout.dma.srate,
2400 user_arg);
2401
2402 case SNDCTL_DSP_STEREO:
2403 if (get_user(val, user_arg))
2404 return -EFAULT;
2405 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2406 if (val)
2407 val2 |= AFMT_STEREO;
2408 else
2409 val2 &= ~AFMT_STEREO;
2410 if (set_format(as, file->f_mode, val2, 0))
2411 return -EIO;
2412 return 0;
2413
2414 case SNDCTL_DSP_CHANNELS:
2415 if (get_user(val, user_arg))
2416 return -EFAULT;
2417 if (val != 0) {
2418 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2419 if (val == 1)
2420 val2 &= ~AFMT_STEREO;
2421 else
2422 val2 |= AFMT_STEREO;
2423 if (set_format(as, file->f_mode, val2, 0))
2424 return -EIO;
2425 }
2426 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2427 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2428
2429 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2430 return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2431 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg);
2432
2433 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2434 if (get_user(val, user_arg))
2435 return -EFAULT;
2436 if (val != AFMT_QUERY) {
2437 if (hweight32(val) != 1)
2438 return -EINVAL;
2439 if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE |
2440 AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE)))
2441 return -EINVAL;
2442 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2443 val |= val2 & AFMT_STEREO;
2444 if (set_format(as, file->f_mode, val, 0))
2445 return -EIO;
2446 }
2447 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2448 return put_user(val2 & ~AFMT_STEREO, user_arg);
2449
2450 case SNDCTL_DSP_POST:
2451 return 0;
2452
2453 case SNDCTL_DSP_GETTRIGGER:
2454 val = 0;
2455 if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING)
2456 val |= PCM_ENABLE_INPUT;
2457 if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING)
2458 val |= PCM_ENABLE_OUTPUT;
2459 return put_user(val, user_arg);
2460
2461 case SNDCTL_DSP_SETTRIGGER:
2462 if (get_user(val, user_arg))
2463 return -EFAULT;
2464 if (file->f_mode & FMODE_READ) {
2465 if (val & PCM_ENABLE_INPUT) {
2466 if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as)))
2467 return ret;
2468 if (usbin_start(as))
2469 return -ENODEV;
2470 } else
2471 usbin_stop(as);
2472 }
2473 if (file->f_mode & FMODE_WRITE) {
2474 if (val & PCM_ENABLE_OUTPUT) {
2475 if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as)))
2476 return ret;
2477 if (usbout_start(as))
2478 return -ENODEV;
2479 } else
2480 usbout_stop(as);
2481 }
2482 return 0;
2483
2484 case SNDCTL_DSP_GETOSPACE:
2485 if (!(file->f_mode & FMODE_WRITE))
2486 return -EINVAL;
2487 if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0)
2488 return val;
2489 spin_lock_irqsave(&as->lock, flags);
2490 abinfo.fragsize = as->usbout.dma.fragsize;
2491 abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count;
2492 abinfo.fragstotal = as->usbout.dma.numfrag;
2493 abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift;
2494 spin_unlock_irqrestore(&as->lock, flags);
2495 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2496
2497 case SNDCTL_DSP_GETISPACE:
2498 if (!(file->f_mode & FMODE_READ))
2499 return -EINVAL;
2500 if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0)
2501 return val;
2502 spin_lock_irqsave(&as->lock, flags);
2503 abinfo.fragsize = as->usbin.dma.fragsize;
2504 abinfo.bytes = as->usbin.dma.count;
2505 abinfo.fragstotal = as->usbin.dma.numfrag;
2506 abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift;
2507 spin_unlock_irqrestore(&as->lock, flags);
2508 return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2509
2510 case SNDCTL_DSP_NONBLOCK:
2511 file->f_flags |= O_NONBLOCK;
2512 return 0;
2513
2514 case SNDCTL_DSP_GETODELAY:
2515 if (!(file->f_mode & FMODE_WRITE))
2516 return -EINVAL;
2517 spin_lock_irqsave(&as->lock, flags);
2518 val = as->usbout.dma.count;
2519 spin_unlock_irqrestore(&as->lock, flags);
2520 return put_user(val, user_arg);
2521
2522 case SNDCTL_DSP_GETIPTR:
2523 if (!(file->f_mode & FMODE_READ))
2524 return -EINVAL;
2525 spin_lock_irqsave(&as->lock, flags);
2526 cinfo.bytes = as->usbin.dma.total_bytes;
2527 cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift;
2528 cinfo.ptr = as->usbin.dma.wrptr;
2529 if (as->usbin.dma.mapped)
2530 as->usbin.dma.count &= as->usbin.dma.fragsize-1;
2531 spin_unlock_irqrestore(&as->lock, flags);
2532 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2533 return -EFAULT;
2534 return 0;
2535
2536 case SNDCTL_DSP_GETOPTR:
2537 if (!(file->f_mode & FMODE_WRITE))
2538 return -EINVAL;
2539 spin_lock_irqsave(&as->lock, flags);
2540 cinfo.bytes = as->usbout.dma.total_bytes;
2541 cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift;
2542 cinfo.ptr = as->usbout.dma.rdptr;
2543 if (as->usbout.dma.mapped)
2544 as->usbout.dma.count &= as->usbout.dma.fragsize-1;
2545 spin_unlock_irqrestore(&as->lock, flags);
2546 if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo)))
2547 return -EFAULT;
2548 return 0;
2549
2550 case SNDCTL_DSP_GETBLKSIZE:
2551 if (file->f_mode & FMODE_WRITE) {
2552 if ((val = prog_dmabuf_out(as)))
2553 return val;
2554 return put_user(as->usbout.dma.fragsize, user_arg);
2555 }
2556 if ((val = prog_dmabuf_in(as)))
2557 return val;
2558 return put_user(as->usbin.dma.fragsize, user_arg);
2559
2560 case SNDCTL_DSP_SETFRAGMENT:
2561 if (get_user(val, user_arg))
2562 return -EFAULT;
2563 if (file->f_mode & FMODE_READ) {
2564 as->usbin.dma.ossfragshift = val & 0xffff;
2565 as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff;
2566 if (as->usbin.dma.ossfragshift < 4)
2567 as->usbin.dma.ossfragshift = 4;
2568 if (as->usbin.dma.ossfragshift > 15)
2569 as->usbin.dma.ossfragshift = 15;
2570 if (as->usbin.dma.ossmaxfrags < 4)
2571 as->usbin.dma.ossmaxfrags = 4;
2572 }
2573 if (file->f_mode & FMODE_WRITE) {
2574 as->usbout.dma.ossfragshift = val & 0xffff;
2575 as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff;
2576 if (as->usbout.dma.ossfragshift < 4)
2577 as->usbout.dma.ossfragshift = 4;
2578 if (as->usbout.dma.ossfragshift > 15)
2579 as->usbout.dma.ossfragshift = 15;
2580 if (as->usbout.dma.ossmaxfrags < 4)
2581 as->usbout.dma.ossmaxfrags = 4;
2582 }
2583 return 0;
2584
2585 case SNDCTL_DSP_SUBDIVIDE:
2586 if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) ||
2587 (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision))
2588 return -EINVAL;
2589 if (get_user(val, user_arg))
2590 return -EFAULT;
2591 if (val != 1 && val != 2 && val != 4)
2592 return -EINVAL;
2593 if (file->f_mode & FMODE_READ)
2594 as->usbin.dma.subdivision = val;
2595 if (file->f_mode & FMODE_WRITE)
2596 as->usbout.dma.subdivision = val;
2597 return 0;
2598
2599 case SOUND_PCM_READ_RATE:
2600 return put_user((file->f_mode & FMODE_READ) ?
2601 as->usbin.dma.srate : as->usbout.dma.srate,
2602 user_arg);
2603
2604 case SOUND_PCM_READ_CHANNELS:
2605 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2606 return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg);
2607
2608 case SOUND_PCM_READ_BITS:
2609 val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format;
2610 return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg);
2611
2612 case SOUND_PCM_WRITE_FILTER:
2613 case SNDCTL_DSP_SETSYNCRO:
2614 case SOUND_PCM_READ_FILTER:
2615 return -EINVAL;
2616 }
2617 dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n"));
2618 return -ENOIOCTLCMD;
2619}
2620
2621static int usb_audio_open(struct inode *inode, struct file *file)
2622{
2623 unsigned int minor = iminor(inode);
2624 DECLARE_WAITQUEUE(wait, current);
2625 struct usb_audiodev *as;
2626 struct usb_audio_state *s;
2627
2628 for (;;) {
2629 down(&open_sem);
2630 list_for_each_entry(s, &audiodevs, audiodev) {
2631 list_for_each_entry(as, &s->audiolist, list) {
2632 if (!((as->dev_audio ^ minor) & ~0xf))
2633 goto device_found;
2634 }
2635 }
2636 up(&open_sem);
2637 return -ENODEV;
2638
2639 device_found:
2640 if (!s->usbdev) {
2641 up(&open_sem);
2642 return -EIO;
2643 }
2644 /* wait for device to become free */
2645 if (!(as->open_mode & file->f_mode))
2646 break;
2647 if (file->f_flags & O_NONBLOCK) {
2648 up(&open_sem);
2649 return -EBUSY;
2650 }
2651 __set_current_state(TASK_INTERRUPTIBLE);
2652 add_wait_queue(&open_wait, &wait);
2653 up(&open_sem);
2654 schedule();
2655 __set_current_state(TASK_RUNNING);
2656 remove_wait_queue(&open_wait, &wait);
2657 if (signal_pending(current))
2658 return -ERESTARTSYS;
2659 }
2660 if (file->f_mode & FMODE_READ)
2661 as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0;
2662 if (file->f_mode & FMODE_WRITE)
2663 as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0;
2664 if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) {
2665 up(&open_sem);
2666 return -EIO;
2667 }
2668 file->private_data = as;
2669 as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2670 s->count++;
2671 up(&open_sem);
2672 return nonseekable_open(inode, file);
2673}
2674
2675static int usb_audio_release(struct inode *inode, struct file *file)
2676{
2677 struct usb_audiodev *as = (struct usb_audiodev *)file->private_data;
2678 struct usb_audio_state *s;
2679 struct usb_device *dev;
2680
2681 lock_kernel();
2682 s = as->state;
2683 dev = s->usbdev;
2684 if (file->f_mode & FMODE_WRITE)
2685 drain_out(as, file->f_flags & O_NONBLOCK);
2686 down(&open_sem);
2687 if (file->f_mode & FMODE_WRITE) {
2688 usbout_stop(as);
2689 if (dev && as->usbout.interface >= 0)
2690 usb_set_interface(dev, as->usbout.interface, 0);
2691 dmabuf_release(&as->usbout.dma);
2692 usbout_release(as);
2693 }
2694 if (file->f_mode & FMODE_READ) {
2695 usbin_stop(as);
2696 if (dev && as->usbin.interface >= 0)
2697 usb_set_interface(dev, as->usbin.interface, 0);
2698 dmabuf_release(&as->usbin.dma);
2699 usbin_release(as);
2700 }
2701 as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
2702 release(s);
2703 wake_up(&open_wait);
2704 unlock_kernel();
2705 return 0;
2706}
2707
2708static /*const*/ struct file_operations usb_audio_fops = {
2709 .owner = THIS_MODULE,
2710 .llseek = no_llseek,
2711 .read = usb_audio_read,
2712 .write = usb_audio_write,
2713 .poll = usb_audio_poll,
2714 .ioctl = usb_audio_ioctl,
2715 .mmap = usb_audio_mmap,
2716 .open = usb_audio_open,
2717 .release = usb_audio_release,
2718};
2719
2720/* --------------------------------------------------------------------- */
2721
2722static int usb_audio_probe(struct usb_interface *iface,
2723 const struct usb_device_id *id);
2724static void usb_audio_disconnect(struct usb_interface *iface);
2725
2726static struct usb_device_id usb_audio_ids [] = {
2727 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
2728 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1},
2729 { } /* Terminating entry */
2730};
2731
2732MODULE_DEVICE_TABLE (usb, usb_audio_ids);
2733
2734static struct usb_driver usb_audio_driver = {
2735 .name = "audio",
2736 .probe = usb_audio_probe,
2737 .disconnect = usb_audio_disconnect,
2738 .id_table = usb_audio_ids,
2739};
2740
2741static void *find_descriptor(void *descstart, unsigned int desclen, void *after,
2742 u8 dtype, int iface, int altsetting)
2743{
2744 u8 *p, *end, *next;
2745 int ifc = -1, as = -1;
2746
2747 p = descstart;
2748 end = p + desclen;
2749 for (; p < end;) {
2750 if (p[0] < 2)
2751 return NULL;
2752 next = p + p[0];
2753 if (next > end)
2754 return NULL;
2755 if (p[1] == USB_DT_INTERFACE) {
2756 /* minimum length of interface descriptor */
2757 if (p[0] < 9)
2758 return NULL;
2759 ifc = p[2];
2760 as = p[3];
2761 }
2762 if (p[1] == dtype && (!after || (void *)p > after) &&
2763 (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) {
2764 return p;
2765 }
2766 p = next;
2767 }
2768 return NULL;
2769}
2770
2771static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting)
2772{
2773 unsigned char *p;
2774
2775 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting);
2776 while (p) {
2777 if (p[0] >= 3 && p[2] == dsubtype)
2778 return p;
2779 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting);
2780 }
2781 return NULL;
2782}
2783
2784static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface)
2785{
2786 unsigned char *p;
2787
2788 p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1);
2789 while (p) {
2790 if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit)
2791 return p;
2792 p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1);
2793 }
2794 return NULL;
2795}
2796
2797static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout)
2798{
2799 struct usb_device *dev = s->usbdev;
2800 struct usb_audiodev *as;
2801 struct usb_host_interface *alts;
2802 struct usb_interface *iface;
2803 struct audioformat *fp;
2804 unsigned char *fmt, *csep;
2805 unsigned int i, j, k, format, idx;
2806
2807 if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL)))
2808 return;
2809 memset(as, 0, sizeof(struct usb_audiodev));
2810 init_waitqueue_head(&as->usbin.dma.wait);
2811 init_waitqueue_head(&as->usbout.dma.wait);
2812 spin_lock_init(&as->lock);
2813 as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2814 as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2815 as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2816 as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2817 as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2818 as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL);
2819 as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2820 as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL);
2821 if ((!as->usbin.durb[0].urb) ||
2822 (!as->usbin.durb[1].urb) ||
2823 (!as->usbin.surb[0].urb) ||
2824 (!as->usbin.surb[1].urb) ||
2825 (!as->usbout.durb[0].urb) ||
2826 (!as->usbout.durb[1].urb) ||
2827 (!as->usbout.surb[0].urb) ||
2828 (!as->usbout.surb[1].urb)) {
2829 usb_free_urb(as->usbin.durb[0].urb);
2830 usb_free_urb(as->usbin.durb[1].urb);
2831 usb_free_urb(as->usbin.surb[0].urb);
2832 usb_free_urb(as->usbin.surb[1].urb);
2833 usb_free_urb(as->usbout.durb[0].urb);
2834 usb_free_urb(as->usbout.durb[1].urb);
2835 usb_free_urb(as->usbout.surb[0].urb);
2836 usb_free_urb(as->usbout.surb[1].urb);
2837 kfree(as);
2838 return;
2839 }
2840 as->state = s;
2841 as->usbin.interface = asifin;
2842 as->usbout.interface = asifout;
2843 /* search for input formats */
2844 if (asifin >= 0) {
2845 as->usbin.flags = FLG_CONNECTED;
2846 iface = usb_ifnum_to_if(dev, asifin);
2847 for (idx = 0; idx < iface->num_altsetting; idx++) {
2848 alts = &iface->altsetting[idx];
2849 i = alts->desc.bAlternateSetting;
2850 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2851 continue;
2852 if (alts->desc.bNumEndpoints < 1) {
2853 if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */
2854 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2855 dev->devnum, asifin, i);
2856 }
2857 continue;
2858 }
2859 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2860 !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2861 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n",
2862 dev->devnum, asifin, i);
2863 continue;
2864 }
2865 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i);
2866 if (!fmt) {
2867 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2868 dev->devnum, asifin, i);
2869 continue;
2870 }
2871 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2872 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2873 dev->devnum, asifin, i);
2874 continue;
2875 }
2876 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2877 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i);
2878 if (!fmt) {
2879 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2880 dev->devnum, asifin, i);
2881 continue;
2882 }
2883 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2884 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2885 dev->devnum, asifin, i);
2886 continue;
2887 }
2888 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2889 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2890 dev->devnum, asifin, i, fmt[4], fmt[5]);
2891 continue;
2892 }
2893 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i);
2894 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2895 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2896 dev->devnum, asifin, i);
2897 continue;
2898 }
2899 if (as->numfmtin >= MAXFORMATS)
2900 continue;
2901 fp = &as->fmtin[as->numfmtin++];
2902 if (fmt[5] == 2)
2903 format &= (AFMT_U16_LE | AFMT_S16_LE);
2904 else
2905 format &= (AFMT_U8 | AFMT_S8);
2906 if (fmt[4] == 2)
2907 format |= AFMT_STEREO;
2908 fp->format = format;
2909 fp->altsetting = i;
2910 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2911 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo);
2912 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
2913 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
2914 printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k);
2915 if (k > fp->sratehi)
2916 fp->sratehi = k;
2917 if (k < fp->sratelo)
2918 fp->sratelo = k;
2919 }
2920 fp->attributes = csep[3];
2921 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
2922 dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
2923 }
2924 }
2925 /* search for output formats */
2926 if (asifout >= 0) {
2927 as->usbout.flags = FLG_CONNECTED;
2928 iface = usb_ifnum_to_if(dev, asifout);
2929 for (idx = 0; idx < iface->num_altsetting; idx++) {
2930 alts = &iface->altsetting[idx];
2931 i = alts->desc.bAlternateSetting;
2932 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2)
2933 continue;
2934 if (alts->desc.bNumEndpoints < 1) {
2935 /* altsetting 0 should never have iso EPs */
2936 if (i != 0)
2937 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n",
2938 dev->devnum, asifout, i);
2939 continue;
2940 }
2941 if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 ||
2942 (alts->endpoint[0].desc.bEndpointAddress & 0x80)) {
2943 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n",
2944 dev->devnum, asifout, i);
2945 continue;
2946 }
2947 /* See USB audio formats manual, section 2 */
2948 fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i);
2949 if (!fmt) {
2950 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2951 dev->devnum, asifout, i);
2952 continue;
2953 }
2954 if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) {
2955 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n",
2956 dev->devnum, asifout, i);
2957 continue;
2958 }
2959 format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8);
2960 /* Dallas DS4201 workaround */
2961 if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa &&
2962 le16_to_cpu(dev->descriptor.idProduct) == 0x4201)
2963 format = (AFMT_S16_LE | AFMT_S8);
2964 fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i);
2965 if (!fmt) {
2966 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n",
2967 dev->devnum, asifout, i);
2968 continue;
2969 }
2970 if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) {
2971 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n",
2972 dev->devnum, asifout, i);
2973 continue;
2974 }
2975 if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) {
2976 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n",
2977 dev->devnum, asifout, i, fmt[4], fmt[5]);
2978 continue;
2979 }
2980 csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i);
2981 if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
2982 printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n",
2983 dev->devnum, asifout, i);
2984 continue;
2985 }
2986 if (as->numfmtout >= MAXFORMATS)
2987 continue;
2988 fp = &as->fmtout[as->numfmtout++];
2989 if (fmt[5] == 2)
2990 format &= (AFMT_U16_LE | AFMT_S16_LE);
2991 else
2992 format &= (AFMT_U8 | AFMT_S8);
2993 if (fmt[4] == 2)
2994 format |= AFMT_STEREO;
2995 fp->format = format;
2996 fp->altsetting = i;
2997 fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16);
2998 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo);
2999 for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) {
3000 k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16);
3001 printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k);
3002 if (k > fp->sratehi)
3003 fp->sratehi = k;
3004 if (k < fp->sratelo)
3005 fp->sratelo = k;
3006 }
3007 fp->attributes = csep[3];
3008 printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n",
3009 dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes);
3010 }
3011 }
3012 if (as->numfmtin == 0 && as->numfmtout == 0) {
3013 usb_free_urb(as->usbin.durb[0].urb);
3014 usb_free_urb(as->usbin.durb[1].urb);
3015 usb_free_urb(as->usbin.surb[0].urb);
3016 usb_free_urb(as->usbin.surb[1].urb);
3017 usb_free_urb(as->usbout.durb[0].urb);
3018 usb_free_urb(as->usbout.durb[1].urb);
3019 usb_free_urb(as->usbout.surb[0].urb);
3020 usb_free_urb(as->usbout.surb[1].urb);
3021 kfree(as);
3022 return;
3023 }
3024 if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) {
3025 printk(KERN_ERR "usbaudio: cannot register dsp\n");
3026 usb_free_urb(as->usbin.durb[0].urb);
3027 usb_free_urb(as->usbin.durb[1].urb);
3028 usb_free_urb(as->usbin.surb[0].urb);
3029 usb_free_urb(as->usbin.surb[1].urb);
3030 usb_free_urb(as->usbout.durb[0].urb);
3031 usb_free_urb(as->usbout.durb[1].urb);
3032 usb_free_urb(as->usbout.surb[0].urb);
3033 usb_free_urb(as->usbout.surb[1].urb);
3034 kfree(as);
3035 return;
3036 }
3037 printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio);
3038 /* everything successful */
3039 list_add_tail(&as->list, &s->audiolist);
3040}
3041
3042struct consmixstate {
3043 struct usb_audio_state *s;
3044 unsigned char *buffer;
3045 unsigned int buflen;
3046 unsigned int ctrlif;
3047 struct mixerchannel mixch[SOUND_MIXER_NRDEVICES];
3048 unsigned int nrmixch;
3049 unsigned int mixchmask;
3050 unsigned long unitbitmap[32/sizeof(unsigned long)];
3051 /* return values */
3052 unsigned int nrchannels;
3053 unsigned int termtype;
3054 unsigned int chconfig;
3055};
3056
3057static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr)
3058{
3059 struct mixerchannel *c;
3060
3061 if (nr >= SOUND_MIXER_NRDEVICES) {
3062 printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr);
3063 return NULL;
3064 }
3065 if (!(state->mixchmask & (1 << nr))) {
3066 printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr);
3067 return NULL;
3068 }
3069 c = &state->mixch[state->nrmixch++];
3070 c->osschannel = nr;
3071 state->mixchmask &= ~(1 << nr);
3072 return c;
3073}
3074
3075static unsigned int getvolchannel(struct consmixstate *state)
3076{
3077 unsigned int u;
3078
3079 if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME))
3080 return SOUND_MIXER_VOLUME;
3081 if ((state->termtype & 0xff00) == 0x0100) {
3082 if (state->mixchmask & SOUND_MASK_PCM)
3083 return SOUND_MIXER_PCM;
3084 if (state->mixchmask & SOUND_MASK_ALTPCM)
3085 return SOUND_MIXER_ALTPCM;
3086 }
3087 if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC))
3088 return SOUND_MIXER_MIC;
3089 if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER))
3090 return SOUND_MIXER_SPEAKER;
3091 if ((state->termtype & 0xff00) == 0x0500) {
3092 if (state->mixchmask & SOUND_MASK_PHONEIN)
3093 return SOUND_MIXER_PHONEIN;
3094 if (state->mixchmask & SOUND_MASK_PHONEOUT)
3095 return SOUND_MIXER_PHONEOUT;
3096 }
3097 if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO))
3098 return SOUND_MIXER_RADIO;
3099 if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO))
3100 return SOUND_MIXER_VIDEO;
3101 u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 |
3102 SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3));
3103 return u-1;
3104}
3105
3106static void prepmixch(struct consmixstate *state)
3107{
3108 struct usb_device *dev = state->s->usbdev;
3109 struct mixerchannel *ch;
3110 unsigned char *buf;
3111 __s16 v1;
3112 unsigned int v2, v3;
3113
3114 if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES)
3115 return;
3116 buf = kmalloc(sizeof(*buf) * 2, GFP_KERNEL);
3117 if (!buf) {
3118 printk(KERN_ERR "prepmixch: out of memory\n") ;
3119 return;
3120 }
3121
3122 ch = &state->mixch[state->nrmixch-1];
3123 switch (ch->selector) {
3124 case 0: /* mixer unit request */
3125 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3126 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3127 goto err;
3128 ch->minval = buf[0] | (buf[1] << 8);
3129 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3130 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3131 goto err;
3132 ch->maxval = buf[0] | (buf[1] << 8);
3133 v2 = ch->maxval - ch->minval;
3134 if (!v2)
3135 v2 = 1;
3136 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3137 (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3138 goto err;
3139 v1 = buf[0] | (buf[1] << 8);
3140 v3 = v1 - ch->minval;
3141 v3 = 100 * v3 / v2;
3142 if (v3 > 100)
3143 v3 = 100;
3144 ch->value = v3;
3145 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3146 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3147 ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)),
3148 state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3149 goto err;
3150 v1 = buf[0] | (buf[1] << 8);
3151 v3 = v1 - ch->minval;
3152 v3 = 100 * v3 / v2;
3153 if (v3 > 100)
3154 v3 = 100;
3155 }
3156 ch->value |= v3 << 8;
3157 break;
3158
3159 /* various feature unit controls */
3160 case VOLUME_CONTROL:
3161 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3162 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3163 goto err;
3164 ch->minval = buf[0] | (buf[1] << 8);
3165 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3166 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3167 goto err;
3168 ch->maxval = buf[0] | (buf[1] << 8);
3169 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3170 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3171 goto err;
3172 v1 = buf[0] | (buf[1] << 8);
3173 v2 = ch->maxval - ch->minval;
3174 v3 = v1 - ch->minval;
3175 if (!v2)
3176 v2 = 1;
3177 v3 = 100 * v3 / v2;
3178 if (v3 > 100)
3179 v3 = 100;
3180 ch->value = v3;
3181 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3182 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3183 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0)
3184 goto err;
3185 v1 = buf[0] | (buf[1] << 8);
3186 v3 = v1 - ch->minval;
3187 v3 = 100 * v3 / v2;
3188 if (v3 > 100)
3189 v3 = 100;
3190 }
3191 ch->value |= v3 << 8;
3192 break;
3193
3194 case BASS_CONTROL:
3195 case MID_CONTROL:
3196 case TREBLE_CONTROL:
3197 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3198 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3199 goto err;
3200 ch->minval = buf[0] << 8;
3201 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3202 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3203 goto err;
3204 ch->maxval = buf[0] << 8;
3205 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3206 (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3207 goto err;
3208 v1 = buf[0] << 8;
3209 v2 = ch->maxval - ch->minval;
3210 v3 = v1 - ch->minval;
3211 if (!v2)
3212 v2 = 1;
3213 v3 = 100 * v3 / v2;
3214 if (v3 > 100)
3215 v3 = 100;
3216 ch->value = v3;
3217 if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) {
3218 if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
3219 (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0)
3220 goto err;
3221 v1 = buf[0] << 8;
3222 v3 = v1 - ch->minval;
3223 v3 = 100 * v3 / v2;
3224 if (v3 > 100)
3225 v3 = 100;
3226 }
3227 ch->value |= v3 << 8;
3228 break;
3229
3230 default:
3231 goto err;
3232 }
3233
3234 freebuf:
3235 kfree(buf);
3236 return;
3237 err:
3238 printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n",
3239 dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector);
3240 if (state->nrmixch)
3241 state->nrmixch--;
3242 goto freebuf;
3243}
3244
3245
3246static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid);
3247
3248static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch)
3249{
3250 unsigned int idx;
3251
3252 idx = inidx*numoch;
3253 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3254 return 0;
3255 if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)))
3256 return 1;
3257 idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT);
3258 if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7))))
3259 return 0;
3260 return 1;
3261}
3262
3263static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer)
3264{
3265 unsigned int nroutch = mixer[5+mixer[4]];
3266 unsigned int chidx[SOUND_MIXER_NRDEVICES+1];
3267 unsigned int termt[SOUND_MIXER_NRDEVICES];
3268 unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0;
3269 unsigned char *bmap = &mixer[9+mixer[4]];
3270 unsigned int bmapsize;
3271 struct mixerchannel *ch;
3272 unsigned int i;
3273
3274 if (!mixer[4]) {
3275 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]);
3276 return;
3277 }
3278 if (mixer[4] > SOUND_MIXER_NRDEVICES) {
3279 printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]);
3280 return;
3281 }
3282 chidx[0] = 0;
3283 for (i = 0; i < mixer[4]; i++) {
3284 usb_audio_recurseunit(state, mixer[5+i]);
3285 chidx[i+1] = chidx[i] + state->nrchannels;
3286 termt[i] = state->termtype;
3287 }
3288 state->termtype = 0;
3289 state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8);
3290 bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3;
3291 bmap += bmapsize - 1;
3292 if (mixer[0] < 10+mixer[4]+bmapsize) {
3293 printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]);
3294 return;
3295 }
3296 for (i = 0; i < mixer[4]; i++) {
3297 state->termtype = termt[i];
3298 if (chidx[i+1]-chidx[i] >= 2) {
3299 flg |= MIXFLG_STEREOIN;
3300 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3301 ch = getmixchannel(state, getvolchannel(state));
3302 if (ch) {
3303 ch->unitid = mixer[3];
3304 ch->selector = 0;
3305 ch->chnum = chidx[i]+1;
3306 ch->flags = flg;
3307 prepmixch(state);
3308 }
3309 continue;
3310 }
3311 }
3312 flg &= ~MIXFLG_STEREOIN;
3313 if (checkmixbmap(bmap, flg, chidx[i], nroutch)) {
3314 ch = getmixchannel(state, getvolchannel(state));
3315 if (ch) {
3316 ch->unitid = mixer[3];
3317 ch->selector = 0;
3318 ch->chnum = chidx[i]+1;
3319 ch->flags = flg;
3320 prepmixch(state);
3321 }
3322 }
3323 }
3324 state->termtype = 0;
3325}
3326
3327static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid)
3328{
3329 unsigned int i;
3330
3331 for (i = 0; i < state->nrmixch; i++)
3332 if (state->mixch[i].unitid == unitid)
3333 return &state->mixch[i];
3334 return NULL;
3335}
3336
3337static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector)
3338{
3339 unsigned int chnum, i, mixch;
3340 struct mixerchannel *mch;
3341
3342 if (!selector[4]) {
3343 printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]);
3344 return;
3345 }
3346 mixch = state->nrmixch;
3347 usb_audio_recurseunit(state, selector[5]);
3348 if (state->nrmixch != mixch) {
3349 mch = &state->mixch[state->nrmixch-1];
3350 mch->slctunitid = selector[3] | (1 << 8);
3351 } else if ((mch = slctsrc_findunit(state, selector[5]))) {
3352 mch->slctunitid = selector[3] | (1 << 8);
3353 } else {
3354 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]);
3355 }
3356 chnum = state->nrchannels;
3357 for (i = 1; i < selector[4]; i++) {
3358 mixch = state->nrmixch;
3359 usb_audio_recurseunit(state, selector[5+i]);
3360 if (chnum != state->nrchannels) {
3361 printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]);
3362 state->termtype = 0;
3363 state->chconfig = 0;
3364 state->nrchannels = 0;
3365 return;
3366 }
3367 if (state->nrmixch != mixch) {
3368 mch = &state->mixch[state->nrmixch-1];
3369 mch->slctunitid = selector[3] | ((i + 1) << 8);
3370 } else if ((mch = slctsrc_findunit(state, selector[5+i]))) {
3371 mch->slctunitid = selector[3] | ((i + 1) << 8);
3372 } else {
3373 printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1);
3374 }
3375 }
3376 state->termtype = 0;
3377 state->chconfig = 0;
3378}
3379
3380/* in the future we might try to handle 3D etc. effect units */
3381
3382static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc)
3383{
3384 unsigned int i;
3385
3386 for (i = 0; i < proc[6]; i++)
3387 usb_audio_recurseunit(state, proc[7+i]);
3388 state->nrchannels = proc[7+proc[6]];
3389 state->termtype = 0;
3390 state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8);
3391}
3392
3393
3394/* See Audio Class Spec, section 4.3.2.5 */
3395static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr)
3396{
3397 struct mixerchannel *ch;
3398 unsigned short chftr, mchftr;
3399#if 0
3400 struct usb_device *dev = state->s->usbdev;
3401 unsigned char data[1];
3402#endif
3403 unsigned char nr_logical_channels, i;
3404
3405 usb_audio_recurseunit(state, ftr[4]);
3406
3407 if (ftr[5] == 0 ) {
3408 printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]);
3409 return;
3410 }
3411
3412 if (state->nrchannels == 0) {
3413 printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]);
3414 return;
3415 }
3416 if (state->nrchannels > 2)
3417 printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]);
3418
3419 nr_logical_channels=(ftr[0]-7)/ftr[5]-1;
3420
3421 if (nr_logical_channels != state->nrchannels) {
3422 printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels);
3423
3424 if (state->nrchannels == 1 && nr_logical_channels==0) {
3425 printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n");
3426 } else if (state->nrchannels == 1 && nr_logical_channels==2) {
3427 printk(KERN_INFO "usbaudio: assuming that a stereo channel connected directly to a mixer is missing in search (got Labtec headset?). Should be fine.\n");
3428 state->nrchannels=nr_logical_channels;
3429 } else {
3430 printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n");
3431 }
3432 }
3433
3434 /* There is always a master channel */
3435 mchftr = ftr[6];
3436 /* Binary AND over logical channels if they exist */
3437 if (nr_logical_channels) {
3438 chftr = ftr[6+ftr[5]];
3439 for (i = 2; i <= nr_logical_channels; i++)
3440 chftr &= ftr[6+i*ftr[5]];
3441 } else {
3442 chftr = 0;
3443 }
3444
3445 /* volume control */
3446 if (chftr & 2) {
3447 ch = getmixchannel(state, getvolchannel(state));
3448 if (ch) {
3449 ch->unitid = ftr[3];
3450 ch->selector = VOLUME_CONTROL;
3451 ch->chnum = 1;
3452 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3453 prepmixch(state);
3454 }
3455 } else if (mchftr & 2) {
3456 ch = getmixchannel(state, getvolchannel(state));
3457 if (ch) {
3458 ch->unitid = ftr[3];
3459 ch->selector = VOLUME_CONTROL;
3460 ch->chnum = 0;
3461 ch->flags = 0;
3462 prepmixch(state);
3463 }
3464 }
3465 /* bass control */
3466 if (chftr & 4) {
3467 ch = getmixchannel(state, SOUND_MIXER_BASS);
3468 if (ch) {
3469 ch->unitid = ftr[3];
3470 ch->selector = BASS_CONTROL;
3471 ch->chnum = 1;
3472 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3473 prepmixch(state);
3474 }
3475 } else if (mchftr & 4) {
3476 ch = getmixchannel(state, SOUND_MIXER_BASS);
3477 if (ch) {
3478 ch->unitid = ftr[3];
3479 ch->selector = BASS_CONTROL;
3480 ch->chnum = 0;
3481 ch->flags = 0;
3482 prepmixch(state);
3483 }
3484 }
3485 /* treble control */
3486 if (chftr & 16) {
3487 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3488 if (ch) {
3489 ch->unitid = ftr[3];
3490 ch->selector = TREBLE_CONTROL;
3491 ch->chnum = 1;
3492 ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0;
3493 prepmixch(state);
3494 }
3495 } else if (mchftr & 16) {
3496 ch = getmixchannel(state, SOUND_MIXER_TREBLE);
3497 if (ch) {
3498 ch->unitid = ftr[3];
3499 ch->selector = TREBLE_CONTROL;
3500 ch->chnum = 0;
3501 ch->flags = 0;
3502 prepmixch(state);
3503 }
3504 }
3505#if 0
3506 /* if there are mute controls, unmute them */
3507 /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */
3508 if ((chftr & 1) || (mchftr & 1)) {
3509 printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif);
3510 data[0] = 0;
3511 if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
3512 (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, 1000) < 0)
3513 printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif);
3514 }
3515#endif
3516}
3517
3518static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid)
3519{
3520 unsigned char *p1;
3521 unsigned int i, j;
3522
3523 if (test_and_set_bit(unitid, state->unitbitmap)) {
3524 printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid);
3525 return;
3526 }
3527 p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif);
3528 if (!p1) {
3529 printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid);
3530 return;
3531 }
3532 state->nrchannels = 0;
3533 state->termtype = 0;
3534 state->chconfig = 0;
3535 switch (p1[2]) {
3536 case INPUT_TERMINAL:
3537 if (p1[0] < 12) {
3538 printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid);
3539 return;
3540 }
3541 state->nrchannels = p1[7];
3542 state->termtype = p1[4] | (p1[5] << 8);
3543 state->chconfig = p1[8] | (p1[9] << 8);
3544 return;
3545
3546 case MIXER_UNIT:
3547 if (p1[0] < 10 || p1[0] < 10+p1[4]) {
3548 printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid);
3549 return;
3550 }
3551 usb_audio_mixerunit(state, p1);
3552 return;
3553
3554 case SELECTOR_UNIT:
3555 if (p1[0] < 6 || p1[0] < 6+p1[4]) {
3556 printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid);
3557 return;
3558 }
3559 usb_audio_selectorunit(state, p1);
3560 return;
3561
3562 case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */
3563 if (p1[0] < 7 || p1[0] < 7+p1[5]) {
3564 printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid);
3565 return;
3566 }
3567 usb_audio_featureunit(state, p1);
3568 return;
3569
3570 case PROCESSING_UNIT:
3571 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3572 printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid);
3573 return;
3574 }
3575 usb_audio_processingunit(state, p1);
3576 return;
3577
3578 case EXTENSION_UNIT:
3579 if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) {
3580 printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid);
3581 return;
3582 }
3583 for (j = i = 0; i < p1[6]; i++) {
3584 usb_audio_recurseunit(state, p1[7+i]);
3585 if (!i)
3586 j = state->termtype;
3587 else if (j != state->termtype)
3588 j = 0;
3589 }
3590 state->nrchannels = p1[7+p1[6]];
3591 state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8);
3592 state->termtype = j;
3593 return;
3594
3595 default:
3596 printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]);
3597 return;
3598 }
3599}
3600
3601static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm)
3602{
3603 struct usb_mixerdev *ms;
3604 struct consmixstate state;
3605
3606 memset(&state, 0, sizeof(state));
3607 state.s = s;
3608 state.nrmixch = 0;
3609 state.mixchmask = ~0;
3610 state.buffer = buffer;
3611 state.buflen = buflen;
3612 state.ctrlif = ctrlif;
3613 set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */
3614 printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n",
3615 oterm[3], oterm[4] | (oterm[5] << 8));
3616 usb_audio_recurseunit(&state, oterm[7]);
3617 if (!state.nrmixch) {
3618 printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]);
3619 return;
3620 }
3621 if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL)))
3622 return;
3623 memset(ms, 0, sizeof(struct usb_mixerdev));
3624 memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel));
3625 ms->state = s;
3626 ms->iface = ctrlif;
3627 ms->numch = state.nrmixch;
3628 if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) {
3629 printk(KERN_ERR "usbaudio: cannot register mixer\n");
3630 kfree(ms);
3631 return;
3632 }
3633 printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer);
3634 list_add_tail(&ms->list, &s->mixerlist);
3635}
3636
3637/* arbitrary limit, we won't check more interfaces than this */
3638#define USB_MAXINTERFACES 32
3639
3640static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif)
3641{
3642 struct usb_audio_state *s;
3643 struct usb_interface *iface;
3644 struct usb_host_interface *alt;
3645 unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES];
3646 unsigned char *p1;
3647 unsigned int i, j, k, numifin = 0, numifout = 0;
3648
3649 if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL)))
3650 return NULL;
3651 memset(s, 0, sizeof(struct usb_audio_state));
3652 INIT_LIST_HEAD(&s->audiolist);
3653 INIT_LIST_HEAD(&s->mixerlist);
3654 s->usbdev = dev;
3655 s->count = 1;
3656
3657 /* find audiocontrol interface */
3658 if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) {
3659 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n",
3660 dev->devnum, ctrlif);
3661 goto ret;
3662 }
3663 if (p1[0] < 8 + p1[7]) {
3664 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n",
3665 dev->devnum, ctrlif);
3666 goto ret;
3667 }
3668 if (!p1[7])
3669 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n",
3670 dev->devnum, ctrlif);
3671 for (i = 0; i < p1[7]; i++) {
3672 j = p1[8+i];
3673 iface = usb_ifnum_to_if(dev, j);
3674 if (!iface) {
3675 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n",
3676 dev->devnum, ctrlif, j);
3677 continue;
3678 }
3679 if (iface->num_altsetting == 1) {
3680 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif);
3681 continue;
3682 }
3683 alt = usb_altnum_to_altsetting(iface, 0);
3684 if (!alt) {
3685 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n",
3686 dev->devnum, ctrlif, j);
3687 continue;
3688 }
3689 if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) {
3690 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n",
3691 dev->devnum, ctrlif, j);
3692 continue;
3693 }
3694 if (alt->desc.bInterfaceSubClass == 3) {
3695 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n",
3696 dev->devnum, ctrlif, j);
3697 continue;
3698 }
3699 if (alt->desc.bInterfaceSubClass != 2) {
3700 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n",
3701 dev->devnum, ctrlif, j);
3702 continue;
3703 }
3704 if (alt->desc.bNumEndpoints > 0) {
3705 /* Check all endpoints; should they all have a bandwidth of 0 ? */
3706 for (k = 0; k < alt->desc.bNumEndpoints; k++) {
3707 if (le16_to_cpu(alt->endpoint[k].desc.wMaxPacketSize) > 0) {
3708 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k);
3709 break;
3710 }
3711 }
3712 if (k < alt->desc.bNumEndpoints)
3713 continue;
3714 }
3715
3716 alt = usb_altnum_to_altsetting(iface, 1);
3717 if (!alt) {
3718 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n",
3719 dev->devnum, ctrlif, j);
3720 continue;
3721 }
3722 if (alt->desc.bNumEndpoints < 1) {
3723 printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n",
3724 dev->devnum, ctrlif, j);
3725 continue;
3726 }
3727 /* note: this requires the data endpoint to be ep0 and the optional sync
3728 ep to be ep1, which seems to be the case */
3729 if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) {
3730 if (numifin < USB_MAXINTERFACES) {
3731 ifin[numifin++] = j;
3732 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3733 }
3734 } else {
3735 if (numifout < USB_MAXINTERFACES) {
3736 ifout[numifout++] = j;
3737 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1);
3738 }
3739 }
3740 }
3741 printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n",
3742 dev->devnum, ctrlif, numifin, numifout);
3743 for (i = 0; i < numifin && i < numifout; i++)
3744 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]);
3745 for (j = i; j < numifin; j++)
3746 usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1);
3747 for (j = i; j < numifout; j++)
3748 usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]);
3749 /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */
3750 p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1);
3751 while (p1) {
3752 if (p1[0] >= 9)
3753 usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1);
3754 p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1);
3755 }
3756
3757ret:
3758 if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) {
3759 kfree(s);
3760 return NULL;
3761 }
3762 /* everything successful */
3763 down(&open_sem);
3764 list_add_tail(&s->audiodev, &audiodevs);
3765 up(&open_sem);
3766 printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s);
3767 return s;
3768}
3769
3770/* we only care for the currently active configuration */
3771
3772static int usb_audio_probe(struct usb_interface *intf,
3773 const struct usb_device_id *id)
3774{
3775 struct usb_device *dev = interface_to_usbdev (intf);
3776 struct usb_audio_state *s;
3777 unsigned char *buffer;
3778 unsigned int buflen;
3779
3780#if 0
3781 printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum,
3782 config->interface[ifnum].altsetting[0].desc.bInterfaceClass,
3783 config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass);
3784#endif
3785
3786 /*
3787 * audiocontrol interface found
3788 * find which configuration number is active
3789 */
3790 buffer = dev->rawdescriptors[dev->actconfig - dev->config];
3791 buflen = le16_to_cpu(dev->actconfig->desc.wTotalLength);
3792 s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber);
3793 if (s) {
3794 usb_set_intfdata (intf, s);
3795 return 0;
3796 }
3797 return -ENODEV;
3798}
3799
3800
3801/* a revoke facility would make things simpler */
3802
3803static void usb_audio_disconnect(struct usb_interface *intf)
3804{
3805 struct usb_audio_state *s = usb_get_intfdata (intf);
3806 struct usb_audiodev *as;
3807 struct usb_mixerdev *ms;
3808
3809 if (!s)
3810 return;
3811
3812 /* we get called with -1 for every audiostreaming interface registered */
3813 if (s == (struct usb_audio_state *)-1) {
3814 dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n"));
3815 return;
3816 }
3817 if (!s->usbdev) {
3818 dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s));
3819 return;
3820 }
3821 down(&open_sem);
3822 list_del_init(&s->audiodev);
3823 s->usbdev = NULL;
3824 usb_set_intfdata (intf, NULL);
3825
3826 /* deregister all audio and mixer devices, so no new processes can open this device */
3827 list_for_each_entry(as, &s->audiolist, list) {
3828 usbin_disc(as);
3829 usbout_disc(as);
3830 wake_up(&as->usbin.dma.wait);
3831 wake_up(&as->usbout.dma.wait);
3832 if (as->dev_audio >= 0) {
3833 unregister_sound_dsp(as->dev_audio);
3834 printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio);
3835 }
3836 as->dev_audio = -1;
3837 }
3838 list_for_each_entry(ms, &s->mixerlist, list) {
3839 if (ms->dev_mixer >= 0) {
3840 unregister_sound_mixer(ms->dev_mixer);
3841 printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer);
3842 }
3843 ms->dev_mixer = -1;
3844 }
3845 release(s);
3846 wake_up(&open_wait);
3847}
3848
3849static int __init usb_audio_init(void)
3850{
3851 int result = usb_register(&usb_audio_driver);
3852 if (result == 0)
3853 info(DRIVER_VERSION ":" DRIVER_DESC);
3854 return result;
3855}
3856
3857
3858static void __exit usb_audio_cleanup(void)
3859{
3860 usb_deregister(&usb_audio_driver);
3861}
3862
3863module_init(usb_audio_init);
3864module_exit(usb_audio_cleanup);
3865
3866MODULE_AUTHOR( DRIVER_AUTHOR );
3867MODULE_DESCRIPTION( DRIVER_DESC );
3868MODULE_LICENSE("GPL");
3869
diff --git a/drivers/usb/class/audio.h b/drivers/usb/class/audio.h
deleted file mode 100644
index 45916eb12103..000000000000
--- a/drivers/usb/class/audio.h
+++ /dev/null
@@ -1,110 +0,0 @@
1#define CS_AUDIO_UNDEFINED 0x20
2#define CS_AUDIO_DEVICE 0x21
3#define CS_AUDIO_CONFIGURATION 0x22
4#define CS_AUDIO_STRING 0x23
5#define CS_AUDIO_INTERFACE 0x24
6#define CS_AUDIO_ENDPOINT 0x25
7
8#define HEADER 0x01
9#define INPUT_TERMINAL 0x02
10#define OUTPUT_TERMINAL 0x03
11#define MIXER_UNIT 0x04
12#define SELECTOR_UNIT 0x05
13#define FEATURE_UNIT 0x06
14#define PROCESSING_UNIT 0x07
15#define EXTENSION_UNIT 0x08
16
17#define AS_GENERAL 0x01
18#define FORMAT_TYPE 0x02
19#define FORMAT_SPECIFIC 0x03
20
21#define EP_GENERAL 0x01
22
23#define MAX_CHAN 9
24#define MAX_FREQ 16
25#define MAX_IFACE 8
26#define MAX_FORMAT 8
27#define MAX_ALT 32 /* Sorry, we need quite a few for the Philips webcams */
28
29struct usb_audio_terminal
30{
31 u8 flags;
32 u8 assoc;
33 u16 type; /* Mic etc */
34 u8 channels;
35 u8 source;
36 u16 chancfg;
37};
38
39struct usb_audio_format
40{
41 u8 type;
42 u8 channels;
43 u8 num_freq;
44 u8 sfz;
45 u8 bits;
46 u16 freq[MAX_FREQ];
47};
48
49struct usb_audio_interface
50{
51 u8 terminal;
52 u8 delay;
53 u16 num_formats;
54 u16 format_type;
55 u8 flags;
56 u8 idleconf; /* Idle config */
57#define AU_IFACE_FOUND 1
58 struct usb_audio_format format[MAX_FORMAT];
59};
60
61struct usb_audio_device
62{
63 struct list_head list;
64 u8 mixer;
65 u8 selector;
66 void *irq_handle;
67 u8 num_channels;
68 u8 num_dsp_iface;
69 u8 channel_map[MAX_CHAN];
70 struct usb_audio_terminal terminal[MAX_CHAN];
71 struct usb_audio_interface interface[MAX_IFACE][MAX_ALT];
72};
73
74
75
76/* Audio Class specific Request Codes */
77
78#define SET_CUR 0x01
79#define GET_CUR 0x81
80#define SET_MIN 0x02
81#define GET_MIN 0x82
82#define SET_MAX 0x03
83#define GET_MAX 0x83
84#define SET_RES 0x04
85#define GET_RES 0x84
86#define SET_MEM 0x05
87#define GET_MEM 0x85
88#define GET_STAT 0xff
89
90/* Terminal Control Selectors */
91
92#define COPY_PROTECT_CONTROL 0x01
93
94/* Feature Unit Control Selectors */
95
96#define MUTE_CONTROL 0x01
97#define VOLUME_CONTROL 0x02
98#define BASS_CONTROL 0x03
99#define MID_CONTROL 0x04
100#define TREBLE_CONTROL 0x05
101#define GRAPHIC_EQUALIZER_CONTROL 0x06
102#define AUTOMATIC_GAIN_CONTROL 0x07
103#define DELAY_CONTROL 0x08
104#define BASS_BOOST_CONTROL 0x09
105#define LOUDNESS_CONTROL 0x0a
106
107/* Endpoint Control Selectors */
108
109#define SAMPLING_FREQ_CONTROL 0x01
110#define PITCH_CONTROL 0x02
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
index 97bdeb1c2181..6dd339f4c0fc 100644
--- a/drivers/usb/class/cdc-acm.c
+++ b/drivers/usb/class/cdc-acm.c
@@ -60,6 +60,7 @@
60#include <linux/tty_flip.h> 60#include <linux/tty_flip.h>
61#include <linux/module.h> 61#include <linux/module.h>
62#include <linux/smp_lock.h> 62#include <linux/smp_lock.h>
63#include <linux/mutex.h>
63#include <asm/uaccess.h> 64#include <asm/uaccess.h>
64#include <linux/usb.h> 65#include <linux/usb.h>
65#include <linux/usb_cdc.h> 66#include <linux/usb_cdc.h>
@@ -80,7 +81,7 @@ static struct usb_driver acm_driver;
80static struct tty_driver *acm_tty_driver; 81static struct tty_driver *acm_tty_driver;
81static struct acm *acm_table[ACM_TTY_MINORS]; 82static struct acm *acm_table[ACM_TTY_MINORS];
82 83
83static DECLARE_MUTEX(open_sem); 84static DEFINE_MUTEX(open_mutex);
84 85
85#define ACM_READY(acm) (acm && acm->dev && acm->used) 86#define ACM_READY(acm) (acm && acm->dev && acm->used)
86 87
@@ -431,8 +432,8 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
431 int rv = -EINVAL; 432 int rv = -EINVAL;
432 int i; 433 int i;
433 dbg("Entering acm_tty_open.\n"); 434 dbg("Entering acm_tty_open.\n");
434 435
435 down(&open_sem); 436 mutex_lock(&open_mutex);
436 437
437 acm = acm_table[tty->index]; 438 acm = acm_table[tty->index];
438 if (!acm || !acm->dev) 439 if (!acm || !acm->dev)
@@ -474,14 +475,14 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
474 475
475done: 476done:
476err_out: 477err_out:
477 up(&open_sem); 478 mutex_unlock(&open_mutex);
478 return rv; 479 return rv;
479 480
480full_bailout: 481full_bailout:
481 usb_kill_urb(acm->ctrlurb); 482 usb_kill_urb(acm->ctrlurb);
482bail_out: 483bail_out:
483 acm->used--; 484 acm->used--;
484 up(&open_sem); 485 mutex_unlock(&open_mutex);
485 return -EIO; 486 return -EIO;
486} 487}
487 488
@@ -507,7 +508,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
507 if (!acm || !acm->used) 508 if (!acm || !acm->used)
508 return; 509 return;
509 510
510 down(&open_sem); 511 mutex_lock(&open_mutex);
511 if (!--acm->used) { 512 if (!--acm->used) {
512 if (acm->dev) { 513 if (acm->dev) {
513 acm_set_control(acm, acm->ctrlout = 0); 514 acm_set_control(acm, acm->ctrlout = 0);
@@ -518,7 +519,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
518 } else 519 } else
519 acm_tty_unregister(acm); 520 acm_tty_unregister(acm);
520 } 521 }
521 up(&open_sem); 522 mutex_unlock(&open_mutex);
522} 523}
523 524
524static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 525static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
@@ -1013,9 +1014,9 @@ static void acm_disconnect(struct usb_interface *intf)
1013 return; 1014 return;
1014 } 1015 }
1015 1016
1016 down(&open_sem); 1017 mutex_lock(&open_mutex);
1017 if (!usb_get_intfdata(intf)) { 1018 if (!usb_get_intfdata(intf)) {
1018 up(&open_sem); 1019 mutex_unlock(&open_mutex);
1019 return; 1020 return;
1020 } 1021 }
1021 acm->dev = NULL; 1022 acm->dev = NULL;
@@ -1045,11 +1046,11 @@ static void acm_disconnect(struct usb_interface *intf)
1045 1046
1046 if (!acm->used) { 1047 if (!acm->used) {
1047 acm_tty_unregister(acm); 1048 acm_tty_unregister(acm);
1048 up(&open_sem); 1049 mutex_unlock(&open_mutex);
1049 return; 1050 return;
1050 } 1051 }
1051 1052
1052 up(&open_sem); 1053 mutex_unlock(&open_mutex);
1053 1054
1054 if (acm->tty) 1055 if (acm->tty)
1055 tty_hangup(acm->tty); 1056 tty_hangup(acm->tty);
diff --git a/drivers/usb/class/usb-midi.c b/drivers/usb/class/usb-midi.c
deleted file mode 100644
index f13f004d311f..000000000000
--- a/drivers/usb/class/usb-midi.c
+++ /dev/null
@@ -1,2153 +0,0 @@
1/*
2 usb-midi.c -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 This driver is based on:
22 - 'Universal Serial Bus Device Class Definition for MIDI Device'
23 - linux/drivers/sound/es1371.c, linux/drivers/usb/audio.c
24 - alsa/lowlevel/pci/cs64xx.c
25 - umidi.c for NetBSD
26 */
27
28/* ------------------------------------------------------------------------- */
29
30
31#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/list.h>
35#include <linux/slab.h>
36#include <linux/usb.h>
37#include <linux/poll.h>
38#include <linux/sound.h>
39#include <linux/init.h>
40#include <asm/semaphore.h>
41
42#include "usb-midi.h"
43
44/* ------------------------------------------------------------------------- */
45
46/* More verbose on syslog */
47#undef MIDI_DEBUG
48
49#define MIDI_IN_BUFSIZ 1024
50
51#define HAVE_SUPPORT_USB_MIDI_CLASS
52
53#undef HAVE_SUPPORT_ALSA
54
55/* ------------------------------------------------------------------------- */
56
57static int singlebyte = 0;
58module_param(singlebyte, int, 0);
59MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet");
60
61static int maxdevices = 4;
62module_param(maxdevices, int, 0);
63MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device");
64
65static int uvendor = -1;
66module_param(uvendor, int, 0);
67MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface");
68
69static int uproduct = -1;
70module_param(uproduct, int, 0);
71MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface");
72
73static int uinterface = -1;
74module_param(uinterface, int, 0);
75MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface");
76
77static int ualt = -1;
78module_param(ualt, int, 0);
79MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface");
80
81static int umin = -1;
82module_param(umin, int, 0);
83MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface");
84
85static int umout = -1;
86module_param(umout, int, 0);
87MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface");
88
89static int ucable = -1;
90module_param(ucable, int, 0);
91MODULE_PARM_DESC(ucable, "The cable number used for a semi-compliant interface");
92
93/** Note -- the usb_string() returns only Latin-1 characters.
94 * (unicode chars <= 255). To support Japanese, a unicode16LE-to-EUC or
95 * unicode16LE-to-JIS routine is needed to wrap around usb_get_string().
96 **/
97static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/
98module_param(ulangid, ushort, 0);
99MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices");
100
101MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>");
102MODULE_DESCRIPTION("USB-MIDI driver");
103MODULE_LICENSE("GPL");
104
105/* ------------------------------------------------------------------------- */
106
107/** MIDIStreaming Class-Specific Interface Descriptor Subtypes **/
108
109#define MS_DESCRIPTOR_UNDEFINED 0
110#define MS_HEADER 1
111#define MIDI_IN_JACK 2
112#define MIDI_OUT_JACK 3
113/* Spec reads: ELEMENT */
114#define ELEMENT_DESCRIPTOR 4
115
116#define MS_HEADER_LENGTH 7
117
118/** MIDIStreaming Class-Specific Endpoint Descriptor Subtypes **/
119
120#define DESCRIPTOR_UNDEFINED 0
121/* Spec reads: MS_GENERAL */
122#define MS_GENERAL_ENDPOINT 1
123
124/** MIDIStreaming MIDI IN and OUT Jack Types **/
125
126#define JACK_TYPE_UNDEFINED 0
127/* Spec reads: EMBEDDED */
128#define EMBEDDED_JACK 1
129/* Spec reads: EXTERNAL */
130#define EXTERNAL_JACK 2
131
132
133/* structure summary
134
135 usb_midi_state usb_device
136 | |
137 *| *| per ep
138 in_ep out_ep
139 | |
140 *| *| per cable
141 min mout
142 | | (cable to device pairing magic)
143 | |
144 usb_midi_dev dev_id (major,minor) == file->private_data
145
146*/
147
148/* usb_midi_state: corresponds to a USB-MIDI module */
149struct usb_midi_state {
150 struct list_head mididev;
151
152 struct usb_device *usbdev;
153
154 struct list_head midiDevList;
155 struct list_head inEndpointList;
156 struct list_head outEndpointList;
157
158 spinlock_t lock;
159
160 unsigned int count; /* usage counter */
161};
162
163/* midi_out_endpoint: corresponds to an output endpoint */
164struct midi_out_endpoint {
165 struct list_head list;
166
167 struct usb_device *usbdev;
168 int endpoint;
169 spinlock_t lock;
170 wait_queue_head_t wait;
171
172 unsigned char *buf;
173 int bufWrPtr;
174 int bufSize;
175
176 struct urb *urb;
177};
178
179/* midi_in_endpoint: corresponds to an input endpoint */
180struct midi_in_endpoint {
181 struct list_head list;
182
183 struct usb_device *usbdev;
184 int endpoint;
185 spinlock_t lock;
186 wait_queue_head_t wait;
187
188 struct usb_mididev *cables[16]; // cables open for read
189 int readers; // number of cables open for read
190
191 struct urb *urb;
192 unsigned char *recvBuf;
193 int recvBufSize;
194 int urbSubmitted; //FIXME: == readers > 0
195};
196
197/* usb_mididev: corresponds to a logical device */
198struct usb_mididev {
199 struct list_head list;
200
201 struct usb_midi_state *midi;
202 int dev_midi;
203 mode_t open_mode;
204
205 struct {
206 struct midi_in_endpoint *ep;
207 int cableId;
208
209// as we are pushing data from usb_bulk_read to usb_midi_read,
210// we need a larger, cyclic buffer here.
211 unsigned char buf[MIDI_IN_BUFSIZ];
212 int bufRdPtr;
213 int bufWrPtr;
214 int bufRemains;
215 } min;
216
217 struct {
218 struct midi_out_endpoint *ep;
219 int cableId;
220
221 unsigned char buf[3];
222 int bufPtr;
223 int bufRemains;
224
225 int isInExclusive;
226 unsigned char lastEvent;
227 } mout;
228
229 int singlebyte;
230};
231
232/** Map the high nybble of MIDI voice messages to number of Message bytes.
233 * High nyble ranges from 0x8 to 0xe
234 */
235
236static int remains_80e0[] = {
237 3, /** 0x8X Note Off **/
238 3, /** 0x9X Note On **/
239 3, /** 0xAX Poly-key pressure **/
240 3, /** 0xBX Control Change **/
241 2, /** 0xCX Program Change **/
242 2, /** 0xDX Channel pressure **/
243 3 /** 0xEX PitchBend Change **/
244};
245
246/** Map the messages to a number of Message bytes.
247 *
248 **/
249static int remains_f0f6[] = {
250 0, /** 0xF0 **/
251 2, /** 0XF1 **/
252 3, /** 0XF2 **/
253 2, /** 0XF3 **/
254 2, /** 0XF4 (Undefined by MIDI Spec, and subject to change) **/
255 2, /** 0XF5 (Undefined by MIDI Spec, and subject to change) **/
256 1 /** 0XF6 **/
257};
258
259/** Map the messages to a CIN (Code Index Number).
260 *
261 **/
262static int cin_f0ff[] = {
263 4, /** 0xF0 System Exclusive Message Start (special cases may be 6 or 7) */
264 2, /** 0xF1 **/
265 3, /** 0xF2 **/
266 2, /** 0xF3 **/
267 2, /** 0xF4 **/
268 2, /** 0xF5 **/
269 5, /** 0xF6 **/
270 5, /** 0xF7 End of System Exclusive Message (May be 6 or 7) **/
271 5, /** 0xF8 **/
272 5, /** 0xF9 **/
273 5, /** 0xFA **/
274 5, /** 0xFB **/
275 5, /** 0xFC **/
276 5, /** 0xFD **/
277 5, /** 0xFE **/
278 5 /** 0xFF **/
279};
280
281/** Map MIDIStreaming Event packet Code Index Number (low nybble of byte 0)
282 * to the number of bytes of valid MIDI data.
283 *
284 * CIN of 0 and 1 are NOT USED in MIDIStreaming 1.0.
285 *
286 **/
287static int cin_to_len[] = {
288 0, 0, 2, 3,
289 3, 1, 2, 3,
290 3, 3, 3, 3,
291 2, 2, 3, 1
292};
293
294
295/* ------------------------------------------------------------------------- */
296
297static struct list_head mididevs = LIST_HEAD_INIT(mididevs);
298
299static DECLARE_MUTEX(open_sem);
300static DECLARE_WAIT_QUEUE_HEAD(open_wait);
301
302
303/* ------------------------------------------------------------------------- */
304
305static void usb_write_callback(struct urb *urb, struct pt_regs *regs)
306{
307 struct midi_out_endpoint *ep = (struct midi_out_endpoint *)urb->context;
308
309 if ( waitqueue_active( &ep->wait ) )
310 wake_up_interruptible( &ep->wait );
311}
312
313
314static int usb_write( struct midi_out_endpoint *ep, unsigned char *buf, int len )
315{
316 struct usb_device *d;
317 int pipe;
318 int ret = 0;
319 int status;
320 int maxretry = 50;
321
322 DECLARE_WAITQUEUE(wait,current);
323 init_waitqueue_head(&ep->wait);
324
325 d = ep->usbdev;
326 pipe = usb_sndbulkpipe(d, ep->endpoint);
327 usb_fill_bulk_urb( ep->urb, d, pipe, (unsigned char*)buf, len,
328 usb_write_callback, ep );
329
330 status = usb_submit_urb(ep->urb, GFP_KERNEL);
331
332 if (status) {
333 printk(KERN_ERR "usbmidi: Cannot submit urb (%d)\n",status);
334 ret = -EIO;
335 goto error;
336 }
337
338 add_wait_queue( &ep->wait, &wait );
339 set_current_state( TASK_INTERRUPTIBLE );
340
341 while( ep->urb->status == -EINPROGRESS ) {
342 if ( maxretry-- < 0 ) {
343 printk(KERN_ERR "usbmidi: usb_bulk_msg timed out\n");
344 ret = -ETIME;
345 break;
346 }
347 interruptible_sleep_on_timeout( &ep->wait, 10 );
348 }
349 set_current_state( TASK_RUNNING );
350 remove_wait_queue( &ep->wait, &wait );
351
352error:
353 return ret;
354}
355
356
357/** Copy data from URB to In endpoint buf.
358 * Discard if CIN == 0 or CIN = 1.
359 *
360 *
361 **/
362
363static void usb_bulk_read(struct urb *urb, struct pt_regs *regs)
364{
365 struct midi_in_endpoint *ep = (struct midi_in_endpoint *)(urb->context);
366 unsigned char *data = urb->transfer_buffer;
367 int i, j, wake;
368
369 if ( !ep->urbSubmitted ) {
370 return;
371 }
372
373 if ( (urb->status == 0) && (urb->actual_length > 0) ) {
374 wake = 0;
375 spin_lock( &ep->lock );
376
377 for(j = 0; j < urb->actual_length; j += 4) {
378 int cin = (data[j]>>0)&0xf;
379 int cab = (data[j]>>4)&0xf;
380 struct usb_mididev *cable = ep->cables[cab];
381 if ( cable ) {
382 int len = cin_to_len[cin]; /** length of MIDI data **/
383 for (i = 0; i < len; i++) {
384 cable->min.buf[cable->min.bufWrPtr] = data[1+i+j];
385 cable->min.bufWrPtr = (cable->min.bufWrPtr+1)%MIDI_IN_BUFSIZ;
386 if (cable->min.bufRemains < MIDI_IN_BUFSIZ)
387 cable->min.bufRemains += 1;
388 else /** need to drop data **/
389 cable->min.bufRdPtr += (cable->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
390 wake = 1;
391 }
392 }
393 }
394
395 spin_unlock ( &ep->lock );
396 if ( wake ) {
397 wake_up( &ep->wait );
398 }
399 }
400
401 /* urb->dev must be reinitialized on 2.4.x kernels */
402 urb->dev = ep->usbdev;
403
404 urb->actual_length = 0;
405 usb_submit_urb(urb, GFP_ATOMIC);
406}
407
408
409
410/* ------------------------------------------------------------------------- */
411
412/* This routine must be called with spin_lock */
413
414/** Wrapper around usb_write().
415 * This routine must be called with spin_lock held on ep.
416 * Called by midiWrite(), putOneMidiEvent(), and usb_midi_write();
417 **/
418static int flush_midi_buffer( struct midi_out_endpoint *ep )
419{
420 int ret=0;
421
422 if ( ep->bufWrPtr > 0 ) {
423 ret = usb_write( ep, ep->buf, ep->bufWrPtr );
424 ep->bufWrPtr = 0;
425 }
426
427 return ret;
428}
429
430
431/* ------------------------------------------------------------------------- */
432
433
434/** Given a MIDI Event, determine size of data to be attached to
435 * USB-MIDI packet.
436 * Returns 1, 2 or 3.
437 * Called by midiWrite();
438 * Uses remains_80e0 and remains_f0f6;
439 **/
440static int get_remains(int event)
441{
442 int ret;
443
444 if ( event < 0x80 ) {
445 ret = 1;
446 } else if ( event < 0xf0 ) {
447 ret = remains_80e0[((event-0x80)>>4)&0x0f];
448 } else if ( event < 0xf7 ) {
449 ret = remains_f0f6[event-0xf0];
450 } else {
451 ret = 1;
452 }
453
454 return ret;
455}
456
457/** Given the output MIDI data in the output buffer, computes a reasonable
458 * CIN.
459 * Called by putOneMidiEvent().
460 **/
461static int get_CIN( struct usb_mididev *m )
462{
463 int cin;
464
465 if ( m->mout.buf[0] == 0xf7 ) {
466 cin = 5;
467 }
468 else if ( m->mout.buf[1] == 0xf7 ) {
469 cin = 6;
470 }
471 else if ( m->mout.buf[2] == 0xf7 ) {
472 cin = 7;
473 }
474 else {
475 if ( m->mout.isInExclusive == 1 ) {
476 cin = 4;
477 } else if ( m->mout.buf[0] < 0x80 ) {
478 /** One byte that we know nothing about. **/
479 cin = 0xF;
480 } else if ( m->mout.buf[0] < 0xf0 ) {
481 /** MIDI Voice messages 0x8X to 0xEX map to cin 0x8 to 0xE. **/
482 cin = (m->mout.buf[0]>>4)&0x0f;
483 }
484 else {
485 /** Special lookup table exists for real-time events. **/
486 cin = cin_f0ff[m->mout.buf[0]-0xf0];
487 }
488 }
489
490 return cin;
491}
492
493
494/* ------------------------------------------------------------------------- */
495
496
497
498/** Move data to USB endpoint buffer.
499 *
500 **/
501static int put_one_midi_event(struct usb_mididev *m)
502{
503 int cin;
504 unsigned long flags;
505 struct midi_out_endpoint *ep = m->mout.ep;
506 int ret=0;
507
508 cin = get_CIN( m );
509 if ( cin > 0x0f || cin < 0 ) {
510 return -EINVAL;
511 }
512
513 spin_lock_irqsave( &ep->lock, flags );
514 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | cin;
515 ep->buf[ep->bufWrPtr++] = m->mout.buf[0];
516 ep->buf[ep->bufWrPtr++] = m->mout.buf[1];
517 ep->buf[ep->bufWrPtr++] = m->mout.buf[2];
518 if ( ep->bufWrPtr >= ep->bufSize ) {
519 ret = flush_midi_buffer( ep );
520 }
521 spin_unlock_irqrestore( &ep->lock, flags);
522
523 m->mout.buf[0] = m->mout.buf[1] = m->mout.buf[2] = 0;
524 m->mout.bufPtr = 0;
525
526 return ret;
527}
528
529/** Write the MIDI message v on the midi device.
530 * Called by usb_midi_write();
531 * Responsible for packaging a MIDI data stream into USB-MIDI packets.
532 **/
533
534static int midi_write( struct usb_mididev *m, int v )
535{
536 unsigned long flags;
537 struct midi_out_endpoint *ep = m->mout.ep;
538 int ret=0;
539 unsigned char c = (unsigned char)v;
540 unsigned char sysrt_buf[4];
541
542 if ( m->singlebyte != 0 ) {
543 /** Simple code to handle the single-byte USB-MIDI protocol. */
544 spin_lock_irqsave( &ep->lock, flags );
545 if ( ep->bufWrPtr+4 > ep->bufSize ) {
546 ret = flush_midi_buffer( ep );
547 if ( !ret ) {
548 spin_unlock_irqrestore( &ep->lock, flags );
549 return ret;
550 }
551 }
552 ep->buf[ep->bufWrPtr++] = (m->mout.cableId<<4) | 0x0f; /* single byte */
553 ep->buf[ep->bufWrPtr++] = c;
554 ep->buf[ep->bufWrPtr++] = 0;
555 ep->buf[ep->bufWrPtr++] = 0;
556 if ( ep->bufWrPtr >= ep->bufSize ) {
557 ret = flush_midi_buffer( ep );
558 }
559 spin_unlock_irqrestore( &ep->lock, flags );
560
561 return ret;
562 }
563 /** Normal USB-MIDI protocol begins here. */
564
565 if ( c > 0xf7 ) { /* system: Realtime messages */
566 /** Realtime messages are written IMMEDIATELY. */
567 sysrt_buf[0] = (m->mout.cableId<<4) | 0x0f;
568 sysrt_buf[1] = c;
569 sysrt_buf[2] = 0;
570 sysrt_buf[3] = 0;
571 spin_lock_irqsave( &ep->lock, flags );
572 ret = usb_write( ep, sysrt_buf, 4 );
573 spin_unlock_irqrestore( &ep->lock, flags );
574 /* m->mout.lastEvent = 0; */
575
576 return ret;
577 }
578
579 if ( c >= 0x80 ) {
580 if ( c < 0xf0 ) {
581 m->mout.lastEvent = c;
582 m->mout.isInExclusive = 0;
583 m->mout.bufRemains = get_remains(c);
584 } else if ( c == 0xf0 ) {
585 /* m->mout.lastEvent = 0; */
586 m->mout.isInExclusive = 1;
587 m->mout.bufRemains = get_remains(c);
588 } else if ( c == 0xf7 && m->mout.isInExclusive == 1 ) {
589 /* m->mout.lastEvent = 0; */
590 m->mout.isInExclusive = 0;
591 m->mout.bufRemains = 1;
592 } else if ( c > 0xf0 ) {
593 /* m->mout.lastEvent = 0; */
594 m->mout.isInExclusive = 0;
595 m->mout.bufRemains = get_remains(c);
596 }
597
598 } else if ( m->mout.bufRemains == 0 && m->mout.isInExclusive == 0 ) {
599 if ( m->mout.lastEvent == 0 ) {
600 return 0; /* discard, waiting for the first event */
601 }
602 /** track status **/
603 m->mout.buf[0] = m->mout.lastEvent;
604 m->mout.bufPtr = 1;
605 m->mout.bufRemains = get_remains(m->mout.lastEvent)-1;
606 }
607
608 m->mout.buf[m->mout.bufPtr++] = c;
609 m->mout.bufRemains--;
610 if ( m->mout.bufRemains == 0 || m->mout.bufPtr >= 3) {
611 ret = put_one_midi_event(m);
612 }
613
614 return ret;
615}
616
617
618/* ------------------------------------------------------------------------- */
619
620/** Basic operation on /dev/midiXX as registered through struct file_operations.
621 *
622 * Basic contract: Used to change the current read/write position in a file.
623 * On success, the non-negative position is reported.
624 * On failure, the negative of an error code is reported.
625 *
626 * Because a MIDIStream is not a file, all seek operations are doomed to fail.
627 *
628 **/
629static loff_t usb_midi_llseek(struct file *file, loff_t offset, int origin)
630{
631 /** Tell user you cannot seek on a PIPE-like device. **/
632 return -ESPIPE;
633}
634
635
636/** Basic operation on /dev/midiXX as registered through struct file_operations.
637 *
638 * Basic contract: Block until count bytes have been read or an error occurs.
639 *
640 **/
641
642static ssize_t usb_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
643{
644 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
645 struct midi_in_endpoint *ep = m->min.ep;
646 ssize_t ret;
647 DECLARE_WAITQUEUE(wait, current);
648
649 if ( !access_ok(VERIFY_READ, buffer, count) ) {
650 return -EFAULT;
651 }
652 if ( count == 0 ) {
653 return 0;
654 }
655
656 add_wait_queue( &ep->wait, &wait );
657 ret = 0;
658 while( count > 0 ) {
659 int cnt;
660 int d = (int)count;
661
662 cnt = m->min.bufRemains;
663 if ( cnt > d ) {
664 cnt = d;
665 }
666
667 if ( cnt <= 0 ) {
668 if ( file->f_flags & O_NONBLOCK ) {
669 if (!ret)
670 ret = -EAGAIN;
671 break;
672 }
673 __set_current_state(TASK_INTERRUPTIBLE);
674 schedule();
675 if (signal_pending(current)) {
676 if(!ret)
677 ret=-ERESTARTSYS;
678 break;
679 }
680 continue;
681 }
682
683 {
684 int i;
685 unsigned long flags; /* used to synchronize access to the endpoint */
686 spin_lock_irqsave( &ep->lock, flags );
687 for (i = 0; i < cnt; i++) {
688 if ( copy_to_user( buffer+i, m->min.buf+m->min.bufRdPtr, 1 ) ) {
689 if ( !ret )
690 ret = -EFAULT;
691 break;
692 }
693 m->min.bufRdPtr = (m->min.bufRdPtr+1)%MIDI_IN_BUFSIZ;
694 m->min.bufRemains -= 1;
695 }
696 spin_unlock_irqrestore( &ep->lock, flags );
697 }
698
699 count-=cnt;
700 buffer+=cnt;
701 ret+=cnt;
702
703 break;
704 }
705
706 remove_wait_queue( &ep->wait, &wait );
707 set_current_state(TASK_RUNNING);
708
709 return ret;
710}
711
712
713/** Basic operation on /dev/midiXX as registered through struct file_operations.
714 *
715 * Basic Contract: Take MIDI data byte-by-byte and pass it to
716 * writeMidi() which packages MIDI data into USB-MIDI stream.
717 * Then flushMidiData() is called to ensure all bytes have been written
718 * in a timely fashion.
719 *
720 **/
721
722static ssize_t usb_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
723{
724 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
725 ssize_t ret;
726 unsigned long int flags;
727
728 if ( !access_ok(VERIFY_READ, buffer, count) ) {
729 return -EFAULT;
730 }
731 if ( count == 0 ) {
732 return 0;
733 }
734
735 ret = 0;
736 while( count > 0 ) {
737 unsigned char c;
738
739 if (copy_from_user((unsigned char *)&c, buffer, 1)) {
740 if ( ret == 0 )
741 ret = -EFAULT;
742 break;
743 }
744 if( midi_write(m, (int)c) ) {
745 if ( ret == 0 )
746 ret = -EFAULT;
747 break;
748 }
749 count--;
750 buffer++;
751 ret++;
752 }
753
754 spin_lock_irqsave( &m->mout.ep->lock, flags );
755 if ( flush_midi_buffer(m->mout.ep) < 0 ) {
756 ret = -EFAULT;
757 }
758 spin_unlock_irqrestore( &m->mout.ep->lock, flags );
759
760 return ret;
761}
762
763/** Basic operation on /dev/midiXX as registered through struct file_operations.
764 *
765 * Basic contract: Wait (spin) until ready to read or write on the file.
766 *
767 **/
768static unsigned int usb_midi_poll(struct file *file, struct poll_table_struct *wait)
769{
770 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
771 struct midi_in_endpoint *iep = m->min.ep;
772 struct midi_out_endpoint *oep = m->mout.ep;
773 unsigned long flags;
774 unsigned int mask = 0;
775
776 if ( file->f_mode & FMODE_READ ) {
777 poll_wait( file, &iep->wait, wait );
778 spin_lock_irqsave( &iep->lock, flags );
779 if ( m->min.bufRemains > 0 )
780 mask |= POLLIN | POLLRDNORM;
781 spin_unlock_irqrestore( &iep->lock, flags );
782 }
783
784 if ( file->f_mode & FMODE_WRITE ) {
785 poll_wait( file, &oep->wait, wait );
786 spin_lock_irqsave( &oep->lock, flags );
787 if ( oep->bufWrPtr < oep->bufSize )
788 mask |= POLLOUT | POLLWRNORM;
789 spin_unlock_irqrestore( &oep->lock, flags );
790 }
791
792 return mask;
793}
794
795
796/** Basic operation on /dev/midiXX as registered through struct file_operations.
797 *
798 * Basic contract: This is always the first operation performed on the
799 * device node. If no method is defined, the open succeeds without any
800 * notification given to the module.
801 *
802 **/
803
804static int usb_midi_open(struct inode *inode, struct file *file)
805{
806 int minor = iminor(inode);
807 DECLARE_WAITQUEUE(wait, current);
808 struct usb_midi_state *s;
809 struct usb_mididev *m;
810 unsigned long flags;
811 int succeed = 0;
812
813#if 0
814 printk(KERN_INFO "usb-midi: Open minor= %d.\n", minor);
815#endif
816
817 for(;;) {
818 down(&open_sem);
819 list_for_each_entry(s, &mididevs, mididev) {
820 list_for_each_entry(m, &s->midiDevList, list) {
821 if ( !((m->dev_midi ^ minor) & ~0xf) )
822 goto device_found;
823 }
824 }
825 up(&open_sem);
826 return -ENODEV;
827
828 device_found:
829 if ( !s->usbdev ) {
830 up(&open_sem);
831 return -EIO;
832 }
833 if ( !(m->open_mode & file->f_mode) ) {
834 break;
835 }
836 if ( file->f_flags & O_NONBLOCK ) {
837 up(&open_sem);
838 return -EBUSY;
839 }
840 __set_current_state(TASK_INTERRUPTIBLE);
841 add_wait_queue( &open_wait, &wait );
842 up(&open_sem);
843 schedule();
844 remove_wait_queue( &open_wait, &wait );
845 if ( signal_pending(current) ) {
846 return -ERESTARTSYS;
847 }
848 }
849
850 file->private_data = m;
851 spin_lock_irqsave( &s->lock, flags );
852
853 if ( !(m->open_mode & (FMODE_READ | FMODE_WRITE)) ) {
854 //FIXME: intented semantics unclear here
855 m->min.bufRdPtr = 0;
856 m->min.bufWrPtr = 0;
857 m->min.bufRemains = 0;
858 spin_lock_init(&m->min.ep->lock);
859
860 m->mout.bufPtr = 0;
861 m->mout.bufRemains = 0;
862 m->mout.isInExclusive = 0;
863 m->mout.lastEvent = 0;
864 spin_lock_init(&m->mout.ep->lock);
865 }
866
867 if ( (file->f_mode & FMODE_READ) && m->min.ep != NULL ) {
868 unsigned long int flagsep;
869 spin_lock_irqsave( &m->min.ep->lock, flagsep );
870 m->min.ep->cables[m->min.cableId] = m;
871 m->min.ep->readers += 1;
872 m->min.bufRdPtr = 0;
873 m->min.bufWrPtr = 0;
874 m->min.bufRemains = 0;
875 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
876
877 if ( !(m->min.ep->urbSubmitted)) {
878
879 /* urb->dev must be reinitialized on 2.4.x kernels */
880 m->min.ep->urb->dev = m->min.ep->usbdev;
881
882 if ( usb_submit_urb(m->min.ep->urb, GFP_ATOMIC) ) {
883 printk(KERN_ERR "usbmidi: Cannot submit urb for MIDI-IN\n");
884 }
885 m->min.ep->urbSubmitted = 1;
886 }
887 m->open_mode |= FMODE_READ;
888 succeed = 1;
889 }
890
891 if ( (file->f_mode & FMODE_WRITE) && m->mout.ep != NULL ) {
892 m->mout.bufPtr = 0;
893 m->mout.bufRemains = 0;
894 m->mout.isInExclusive = 0;
895 m->mout.lastEvent = 0;
896 m->open_mode |= FMODE_WRITE;
897 succeed = 1;
898 }
899
900 spin_unlock_irqrestore( &s->lock, flags );
901
902 s->count++;
903 up(&open_sem);
904
905 /** Changed to prevent extra increments to USE_COUNT. **/
906 if (!succeed) {
907 return -EBUSY;
908 }
909
910#if 0
911 printk(KERN_INFO "usb-midi: Open Succeeded. minor= %d.\n", minor);
912#endif
913
914 return nonseekable_open(inode, file); /** Success. **/
915}
916
917
918/** Basic operation on /dev/midiXX as registered through struct file_operations.
919 *
920 * Basic contract: Close an opened file and deallocate anything we allocated.
921 * Like open(), this can be missing. If open set file->private_data,
922 * release() must clear it.
923 *
924 **/
925
926static int usb_midi_release(struct inode *inode, struct file *file)
927{
928 struct usb_mididev *m = (struct usb_mididev *)file->private_data;
929 struct usb_midi_state *s = (struct usb_midi_state *)m->midi;
930
931#if 0
932 printk(KERN_INFO "usb-midi: Close.\n");
933#endif
934
935 down(&open_sem);
936
937 if ( m->open_mode & FMODE_WRITE ) {
938 m->open_mode &= ~FMODE_WRITE;
939 usb_kill_urb( m->mout.ep->urb );
940 }
941
942 if ( m->open_mode & FMODE_READ ) {
943 unsigned long int flagsep;
944 spin_lock_irqsave( &m->min.ep->lock, flagsep );
945 m->min.ep->cables[m->min.cableId] = NULL; // discard cable
946 m->min.ep->readers -= 1;
947 m->open_mode &= ~FMODE_READ;
948 if ( m->min.ep->readers == 0 &&
949 m->min.ep->urbSubmitted ) {
950 m->min.ep->urbSubmitted = 0;
951 usb_kill_urb(m->min.ep->urb);
952 }
953 spin_unlock_irqrestore( &m->min.ep->lock, flagsep );
954 }
955
956 s->count--;
957
958 up(&open_sem);
959 wake_up(&open_wait);
960
961 file->private_data = NULL;
962 return 0;
963}
964
965static struct file_operations usb_midi_fops = {
966 .owner = THIS_MODULE,
967 .llseek = usb_midi_llseek,
968 .read = usb_midi_read,
969 .write = usb_midi_write,
970 .poll = usb_midi_poll,
971 .open = usb_midi_open,
972 .release = usb_midi_release,
973};
974
975/* ------------------------------------------------------------------------- */
976
977/** Returns filled midi_in_endpoint structure or null on failure.
978 *
979 * Parameters:
980 * d - a usb_device
981 * endPoint - An usb endpoint in the range 0 to 15.
982 * Called by allocUsbMidiDev();
983 *
984 **/
985
986static struct midi_in_endpoint *alloc_midi_in_endpoint( struct usb_device *d, int endPoint )
987{
988 struct midi_in_endpoint *ep;
989 int bufSize;
990 int pipe;
991
992 endPoint &= 0x0f; /* Silently force endPoint to lie in range 0 to 15. */
993
994 pipe = usb_rcvbulkpipe( d, endPoint );
995 bufSize = usb_maxpacket( d, pipe, 0 );
996 /* usb_pipein() = ! usb_pipeout() = true for an in Endpoint */
997
998 ep = (struct midi_in_endpoint *)kmalloc(sizeof(struct midi_in_endpoint), GFP_KERNEL);
999 if ( !ep ) {
1000 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint\n");
1001 return NULL;
1002 }
1003 memset( ep, 0, sizeof(struct midi_in_endpoint) );
1004// this sets cables[] and readers to 0, too.
1005// for (i=0; i<16; i++) ep->cables[i] = 0; // discard cable
1006// ep->readers = 0;
1007
1008 ep->endpoint = endPoint;
1009
1010 ep->recvBuf = (unsigned char *)kmalloc(sizeof(unsigned char)*(bufSize), GFP_KERNEL);
1011 if ( !ep->recvBuf ) {
1012 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint buffer\n");
1013 kfree(ep);
1014 return NULL;
1015 }
1016
1017 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1018 if ( !ep->urb ) {
1019 printk(KERN_ERR "usbmidi: no memory for midi in-endpoint urb\n");
1020 kfree(ep->recvBuf);
1021 kfree(ep);
1022 return NULL;
1023 }
1024 usb_fill_bulk_urb( ep->urb, d,
1025 usb_rcvbulkpipe(d, endPoint),
1026 (unsigned char *)ep->recvBuf, bufSize,
1027 usb_bulk_read, ep );
1028
1029 /* ep->bufRdPtr = 0; */
1030 /* ep->bufWrPtr = 0; */
1031 /* ep->bufRemains = 0; */
1032 /* ep->urbSubmitted = 0; */
1033 ep->recvBufSize = bufSize;
1034
1035 init_waitqueue_head(&ep->wait);
1036
1037 return ep;
1038}
1039
1040static int remove_midi_in_endpoint( struct midi_in_endpoint *min )
1041{
1042 usb_kill_urb( min->urb );
1043 usb_free_urb( min->urb );
1044 kfree( min->recvBuf );
1045 kfree( min );
1046
1047 return 0;
1048}
1049
1050/** Returns filled midi_out_endpoint structure or null on failure.
1051 *
1052 * Parameters:
1053 * d - a usb_device
1054 * endPoint - An usb endpoint in the range 0 to 15.
1055 * Called by allocUsbMidiDev();
1056 *
1057 **/
1058static struct midi_out_endpoint *alloc_midi_out_endpoint( struct usb_device *d, int endPoint )
1059{
1060 struct midi_out_endpoint *ep = NULL;
1061 int pipe;
1062 int bufSize;
1063
1064 endPoint &= 0x0f;
1065 pipe = usb_sndbulkpipe( d, endPoint );
1066 bufSize = usb_maxpacket( d, pipe, 1 );
1067
1068 ep = (struct midi_out_endpoint *)kmalloc(sizeof(struct midi_out_endpoint), GFP_KERNEL);
1069 if ( !ep ) {
1070 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint\n");
1071 return NULL;
1072 }
1073 memset( ep, 0, sizeof(struct midi_out_endpoint) );
1074
1075 ep->endpoint = endPoint;
1076 ep->buf = (unsigned char *)kmalloc(sizeof(unsigned char)*bufSize, GFP_KERNEL);
1077 if ( !ep->buf ) {
1078 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint buffer\n");
1079 kfree(ep);
1080 return NULL;
1081 }
1082
1083 ep->urb = usb_alloc_urb(0, GFP_KERNEL); /* no ISO */
1084 if ( !ep->urb ) {
1085 printk(KERN_ERR "usbmidi: no memory for midi out-endpoint urb\n");
1086 kfree(ep->buf);
1087 kfree(ep);
1088 return NULL;
1089 }
1090
1091 ep->bufSize = bufSize;
1092 /* ep->bufWrPtr = 0; */
1093
1094 init_waitqueue_head(&ep->wait);
1095
1096 return ep;
1097}
1098
1099
1100static int remove_midi_out_endpoint( struct midi_out_endpoint *mout )
1101{
1102 usb_kill_urb( mout->urb );
1103 usb_free_urb( mout->urb );
1104 kfree( mout->buf );
1105 kfree( mout );
1106
1107 return 0;
1108}
1109
1110
1111/** Returns a filled usb_mididev structure, registered as a Linux MIDI device.
1112 *
1113 * Returns null if memory is not available or the device cannot be registered.
1114 * Called by allocUsbMidiDev();
1115 *
1116 **/
1117static struct usb_mididev *allocMidiDev(
1118 struct usb_midi_state *s,
1119 struct midi_in_endpoint *min,
1120 struct midi_out_endpoint *mout,
1121 int inCableId,
1122 int outCableId )
1123{
1124 struct usb_mididev *m;
1125
1126 m = (struct usb_mididev *)kmalloc(sizeof(struct usb_mididev), GFP_KERNEL);
1127 if (!m) {
1128 printk(KERN_ERR "usbmidi: no memory for midi device\n");
1129 return NULL;
1130 }
1131
1132 memset(m, 0, sizeof(struct usb_mididev));
1133
1134 if ((m->dev_midi = register_sound_midi(&usb_midi_fops, -1)) < 0) {
1135 printk(KERN_ERR "usbmidi: cannot register midi device\n");
1136 kfree(m);
1137 return NULL;
1138 }
1139
1140 m->midi = s;
1141 /* m->open_mode = 0; */
1142
1143 if ( min ) {
1144 m->min.ep = min;
1145 m->min.ep->usbdev = s->usbdev;
1146 m->min.cableId = inCableId;
1147 }
1148 /* m->min.bufPtr = 0; */
1149 /* m->min.bufRemains = 0; */
1150
1151 if ( mout ) {
1152 m->mout.ep = mout;
1153 m->mout.ep->usbdev = s->usbdev;
1154 m->mout.cableId = outCableId;
1155 }
1156 /* m->mout.bufPtr = 0; */
1157 /* m->mout.bufRemains = 0; */
1158 /* m->mout.isInExclusive = 0; */
1159 /* m->mout.lastEvent = 0; */
1160
1161 m->singlebyte = singlebyte;
1162
1163 return m;
1164}
1165
1166
1167static void release_midi_device( struct usb_midi_state *s )
1168{
1169 struct usb_mididev *m;
1170 struct midi_in_endpoint *min;
1171 struct midi_out_endpoint *mout;
1172
1173 if ( s->count > 0 ) {
1174 up(&open_sem);
1175 return;
1176 }
1177 up( &open_sem );
1178 wake_up( &open_wait );
1179
1180 while(!list_empty(&s->inEndpointList)) {
1181 min = list_entry(s->inEndpointList.next, struct midi_in_endpoint, list);
1182 list_del(&min->list);
1183 remove_midi_in_endpoint(min);
1184 }
1185
1186 while(!list_empty(&s->outEndpointList)) {
1187 mout = list_entry(s->outEndpointList.next, struct midi_out_endpoint, list);
1188 list_del(&mout->list);
1189 remove_midi_out_endpoint(mout);
1190 }
1191
1192 while(!list_empty(&s->midiDevList)) {
1193 m = list_entry(s->midiDevList.next, struct usb_mididev, list);
1194 list_del(&m->list);
1195 kfree(m);
1196 }
1197
1198 kfree(s);
1199
1200 return;
1201}
1202
1203
1204/* ------------------------------------------------------------------------- */
1205
1206/** Utility routine to find a descriptor in a dump of many descriptors.
1207 * Returns start of descriptor or NULL if not found.
1208 * descStart pointer to list of interfaces.
1209 * descLength length (in bytes) of dump
1210 * after (ignored if NULL) this routine returns only descriptors after "after"
1211 * dtype (mandatory) The descriptor type.
1212 * iface (ignored if -1) returns descriptor at/following given interface
1213 * altSetting (ignored if -1) returns descriptor at/following given altSetting
1214 *
1215 *
1216 * Called by parseDescriptor(), find_csinterface_descriptor();
1217 *
1218 */
1219static void *find_descriptor( void *descStart, unsigned int descLength, void *after, unsigned char dtype, int iface, int altSetting )
1220{
1221 unsigned char *p, *end, *next;
1222 int interfaceNumber = -1, altSet = -1;
1223
1224 p = descStart;
1225 end = p + descLength;
1226 for( ; p < end; ) {
1227 if ( p[0] < 2 )
1228 return NULL;
1229 next = p + p[0];
1230 if ( next > end )
1231 return NULL;
1232 if ( p[1] == USB_DT_INTERFACE ) {
1233 if ( p[0] < USB_DT_INTERFACE_SIZE )
1234 return NULL;
1235 interfaceNumber = p[2];
1236 altSet = p[3];
1237 }
1238 if ( p[1] == dtype &&
1239 ( !after || ( p > (unsigned char *)after) ) &&
1240 ( ( iface == -1) || (iface == interfaceNumber) ) &&
1241 ( (altSetting == -1) || (altSetting == altSet) )) {
1242 return p;
1243 }
1244 p = next;
1245 }
1246 return NULL;
1247}
1248
1249/** Utility to find a class-specific interface descriptor.
1250 * dsubtype is a descriptor subtype
1251 * Called by parseDescriptor();
1252 **/
1253static void *find_csinterface_descriptor(void *descStart, unsigned int descLength, void *after, u8 dsubtype, int iface, int altSetting)
1254{
1255 unsigned char *p;
1256
1257 p = find_descriptor( descStart, descLength, after, USB_DT_CS_INTERFACE, iface, altSetting );
1258 while ( p ) {
1259 if ( p[0] >= 3 && p[2] == dsubtype )
1260 return p;
1261 p = find_descriptor( descStart, descLength, p, USB_DT_CS_INTERFACE,
1262 iface, altSetting );
1263 }
1264 return NULL;
1265}
1266
1267
1268/** The magic of making a new usb_midi_device from config happens here.
1269 *
1270 * The caller is responsible for free-ing this return value (if not NULL).
1271 *
1272 **/
1273static struct usb_midi_device *parse_descriptor( struct usb_device *d, unsigned char *buffer, int bufSize, unsigned int ifnum , unsigned int altSetting, int quirks)
1274{
1275 struct usb_midi_device *u;
1276 unsigned char *p1;
1277 unsigned char *p2;
1278 unsigned char *next;
1279 int iep, oep;
1280 int length;
1281 unsigned long longBits;
1282 int pins, nbytes, offset, shift, jack;
1283#ifdef HAVE_JACK_STRINGS
1284 /** Jacks can have associated names. **/
1285 unsigned char jack2string[256];
1286#endif
1287
1288 u = NULL;
1289 /* find audiocontrol interface */
1290 p1 = find_csinterface_descriptor( buffer, bufSize, NULL,
1291 MS_HEADER, ifnum, altSetting);
1292
1293 if ( !p1 ) {
1294 goto error_end;
1295 }
1296
1297 if ( p1[0] < MS_HEADER_LENGTH ) {
1298 goto error_end;
1299 }
1300
1301 /* Assume success. Since the device corresponds to USB-MIDI spec, we assume
1302 that the rest of the USB 2.0 spec is obeyed. */
1303
1304 u = (struct usb_midi_device *)kmalloc( sizeof(struct usb_midi_device), GFP_KERNEL );
1305 if ( !u ) {
1306 return NULL;
1307 }
1308 u->deviceName = NULL;
1309 u->idVendor = le16_to_cpu(d->descriptor.idVendor);
1310 u->idProduct = le16_to_cpu(d->descriptor.idProduct);
1311 u->interface = ifnum;
1312 u->altSetting = altSetting;
1313 u->in[0].endpoint = -1;
1314 u->in[0].cableId = -1;
1315 u->out[0].endpoint = -1;
1316 u->out[0].cableId = -1;
1317
1318
1319 printk(KERN_INFO "usb-midi: Found MIDIStreaming device corresponding to Release %d.%02d of spec.\n",
1320 (p1[4] >> 4) * 10 + (p1[4] & 0x0f ),
1321 (p1[3] >> 4) * 10 + (p1[3] & 0x0f )
1322 );
1323
1324 length = p1[5] | (p1[6] << 8);
1325
1326#ifdef HAVE_JACK_STRINGS
1327 memset(jack2string, 0, sizeof(unsigned char) * 256);
1328#endif
1329
1330 length -= p1[0];
1331 for (p2 = p1 + p1[0]; length > 0; p2 = next) {
1332 next = p2 + p2[0];
1333 length -= p2[0];
1334
1335 if (p2[0] < 2 )
1336 break;
1337 if (p2[1] != USB_DT_CS_INTERFACE)
1338 break;
1339 if (p2[2] == MIDI_IN_JACK && p2[0] >= 6 ) {
1340 jack = p2[4];
1341#ifdef HAVE_JACK_STRINGS
1342 jack2string[jack] = p2[5];
1343#endif
1344 printk(KERN_INFO "usb-midi: Found IN Jack 0x%02x %s\n",
1345 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL" );
1346 } else if ( p2[2] == MIDI_OUT_JACK && p2[0] >= 6) {
1347 pins = p2[5];
1348 if ( p2[0] < (6 + 2 * pins) )
1349 continue;
1350 jack = p2[4];
1351#ifdef HAVE_JACK_STRINGS
1352 jack2string[jack] = p2[5 + 2 * pins];
1353#endif
1354 printk(KERN_INFO "usb-midi: Found OUT Jack 0x%02x %s, %d pins\n",
1355 jack, (p2[3] == EMBEDDED_JACK)?"EMBEDDED":"EXTERNAL", pins );
1356 } else if ( p2[2] == ELEMENT_DESCRIPTOR && p2[0] >= 10) {
1357 pins = p2[4];
1358 if ( p2[0] < (9 + 2 * pins ) )
1359 continue;
1360 nbytes = p2[8 + 2 * pins ];
1361 if ( p2[0] < (10 + 2 * pins + nbytes) )
1362 continue;
1363 longBits = 0L;
1364 for ( offset = 0, shift = 0; offset < nbytes && offset < 8; offset ++, shift += 8) {
1365 longBits |= ((long)(p2[9 + 2 * pins + offset])) << shift;
1366 }
1367 jack = p2[3];
1368#ifdef HAVE_JACK_STRINGS
1369 jack2string[jack] = p2[9 + 2 * pins + nbytes];
1370#endif
1371 printk(KERN_INFO "usb-midi: Found ELEMENT 0x%02x, %d/%d pins in/out, bits: 0x%016lx\n",
1372 jack, pins, (int)(p2[5 + 2 * pins]), (long)longBits );
1373 } else {
1374 }
1375 }
1376
1377 iep=0;
1378 oep=0;
1379
1380 if (quirks==0) {
1381 /* MIDISTREAM */
1382 p2 = NULL;
1383 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1384 ifnum, altSetting ); p1; p1 = next ) {
1385 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1386 ifnum, altSetting );
1387 p2 = find_descriptor(buffer, bufSize, p1, USB_DT_CS_ENDPOINT,
1388 ifnum, altSetting );
1389
1390 if ( p2 && next && ( p2 > next ) )
1391 p2 = NULL;
1392
1393 if ( p1[0] < 9 || !p2 || p2[0] < 4 )
1394 continue;
1395
1396 if ( (p1[2] & 0x80) == 0x80 ) {
1397 if ( iep < 15 ) {
1398 pins = p2[3]; /* not pins -- actually "cables" */
1399 if ( pins > 16 )
1400 pins = 16;
1401 u->in[iep].endpoint = p1[2];
1402 u->in[iep].cableId = ( 1 << pins ) - 1;
1403 if ( u->in[iep].cableId )
1404 iep ++;
1405 if ( iep < 15 ) {
1406 u->in[iep].endpoint = -1;
1407 u->in[iep].cableId = -1;
1408 }
1409 }
1410 } else {
1411 if ( oep < 15 ) {
1412 pins = p2[3]; /* not pins -- actually "cables" */
1413 if ( pins > 16 )
1414 pins = 16;
1415 u->out[oep].endpoint = p1[2];
1416 u->out[oep].cableId = ( 1 << pins ) - 1;
1417 if ( u->out[oep].cableId )
1418 oep ++;
1419 if ( oep < 15 ) {
1420 u->out[oep].endpoint = -1;
1421 u->out[oep].cableId = -1;
1422 }
1423 }
1424 }
1425
1426 }
1427 } else if (quirks==1) {
1428 /* YAMAHA quirks */
1429 for (p1 = find_descriptor(buffer, bufSize, NULL, USB_DT_ENDPOINT,
1430 ifnum, altSetting ); p1; p1 = next ) {
1431 next = find_descriptor(buffer, bufSize, p1, USB_DT_ENDPOINT,
1432 ifnum, altSetting );
1433
1434 if ( p1[0] < 7 )
1435 continue;
1436
1437 if ( (p1[2] & 0x80) == 0x80 ) {
1438 if ( iep < 15 ) {
1439 pins = iep+1;
1440 if ( pins > 16 )
1441 pins = 16;
1442 u->in[iep].endpoint = p1[2];
1443 u->in[iep].cableId = ( 1 << pins ) - 1;
1444 if ( u->in[iep].cableId )
1445 iep ++;
1446 if ( iep < 15 ) {
1447 u->in[iep].endpoint = -1;
1448 u->in[iep].cableId = -1;
1449 }
1450 }
1451 } else {
1452 if ( oep < 15 ) {
1453 pins = oep+1;
1454 u->out[oep].endpoint = p1[2];
1455 u->out[oep].cableId = ( 1 << pins ) - 1;
1456 if ( u->out[oep].cableId )
1457 oep ++;
1458 if ( oep < 15 ) {
1459 u->out[oep].endpoint = -1;
1460 u->out[oep].cableId = -1;
1461 }
1462 }
1463 }
1464
1465 }
1466 }
1467
1468 if ( !iep && ! oep ) {
1469 goto error_end;
1470 }
1471
1472 return u;
1473
1474error_end:
1475 kfree(u);
1476 return NULL;
1477}
1478
1479/* ------------------------------------------------------------------------- */
1480
1481/** Returns number between 0 and 16.
1482 *
1483 **/
1484static int on_bits( unsigned short v )
1485{
1486 int i;
1487 int ret=0;
1488
1489 for ( i=0 ; i<16 ; i++ ) {
1490 if ( v & (1<<i) )
1491 ret++;
1492 }
1493
1494 return ret;
1495}
1496
1497
1498/** USB-device will be interrogated for altSetting.
1499 *
1500 * Returns negative on error.
1501 * Called by allocUsbMidiDev();
1502 *
1503 **/
1504
1505static int get_alt_setting( struct usb_device *d, int ifnum )
1506{
1507 int alts, alt=0;
1508 struct usb_interface *iface;
1509 struct usb_host_interface *interface;
1510 struct usb_endpoint_descriptor *ep;
1511 int epin, epout;
1512 int i;
1513
1514 iface = usb_ifnum_to_if( d, ifnum );
1515 alts = iface->num_altsetting;
1516
1517 for ( alt=0 ; alt<alts ; alt++ ) {
1518 interface = &iface->altsetting[alt];
1519 epin = -1;
1520 epout = -1;
1521
1522 for ( i=0 ; i<interface->desc.bNumEndpoints ; i++ ) {
1523 ep = &interface->endpoint[i].desc;
1524 if ( (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ) {
1525 continue;
1526 }
1527 if ( (ep->bEndpointAddress & USB_DIR_IN) && epin < 0 ) {
1528 epin = i;
1529 } else if ( epout < 0 ) {
1530 epout = i;
1531 }
1532 if ( epin >= 0 && epout >= 0 ) {
1533 return interface->desc.bAlternateSetting;
1534 }
1535 }
1536 }
1537
1538 return -ENODEV;
1539}
1540
1541
1542/* ------------------------------------------------------------------------- */
1543
1544
1545/** Returns 0 if successful in allocating and registering internal structures.
1546 * Returns negative on failure.
1547 * Calls allocMidiDev which additionally registers /dev/midiXX devices.
1548 * Writes messages on success to indicate which /dev/midiXX is which physical
1549 * endpoint.
1550 *
1551 **/
1552static int alloc_usb_midi_device( struct usb_device *d, struct usb_midi_state *s, struct usb_midi_device *u )
1553{
1554 struct usb_mididev **mdevs=NULL;
1555 struct midi_in_endpoint *mins[15], *min;
1556 struct midi_out_endpoint *mouts[15], *mout;
1557 int inDevs=0, outDevs=0;
1558 int inEndpoints=0, outEndpoints=0;
1559 int inEndpoint, outEndpoint;
1560 int inCableId, outCableId;
1561 int i;
1562 int devices = 0;
1563 int alt = 0;
1564
1565 /* Obtain altSetting or die.. */
1566 alt = u->altSetting;
1567 if ( alt < 0 ) {
1568 alt = get_alt_setting( d, u->interface );
1569 }
1570 if ( alt < 0 )
1571 return -ENXIO;
1572
1573 /* Configure interface */
1574 if ( usb_set_interface( d, u->interface, alt ) < 0 ) {
1575 return -ENXIO;
1576 }
1577
1578 for ( i = 0 ; i < 15 ; i++ ) {
1579 mins[i] = NULL;
1580 mouts[i] = NULL;
1581 }
1582
1583 /* Begin Allocation */
1584 while( inEndpoints < 15
1585 && inDevs < maxdevices
1586 && u->in[inEndpoints].cableId >= 0 ) {
1587 inDevs += on_bits((unsigned short)u->in[inEndpoints].cableId);
1588 mins[inEndpoints] = alloc_midi_in_endpoint( d, u->in[inEndpoints].endpoint );
1589 if ( mins[inEndpoints] == NULL )
1590 goto error_end;
1591 inEndpoints++;
1592 }
1593
1594 while( outEndpoints < 15
1595 && outDevs < maxdevices
1596 && u->out[outEndpoints].cableId >= 0 ) {
1597 outDevs += on_bits((unsigned short)u->out[outEndpoints].cableId);
1598 mouts[outEndpoints] = alloc_midi_out_endpoint( d, u->out[outEndpoints].endpoint );
1599 if ( mouts[outEndpoints] == NULL )
1600 goto error_end;
1601 outEndpoints++;
1602 }
1603
1604 devices = inDevs > outDevs ? inDevs : outDevs;
1605 devices = maxdevices > devices ? devices : maxdevices;
1606
1607 /* obtain space for device name (iProduct) if not known. */
1608 if ( ! u->deviceName ) {
1609 mdevs = (struct usb_mididev **)
1610 kmalloc(sizeof(struct usb_mididevs *)*devices
1611 + sizeof(char) * 256, GFP_KERNEL);
1612 } else {
1613 mdevs = (struct usb_mididev **)
1614 kmalloc(sizeof(struct usb_mididevs *)*devices, GFP_KERNEL);
1615 }
1616
1617 if ( !mdevs ) {
1618 /* devices = 0; */
1619 /* mdevs = NULL; */
1620 goto error_end;
1621 }
1622 for ( i=0 ; i<devices ; i++ ) {
1623 mdevs[i] = NULL;
1624 }
1625
1626 /* obtain device name (iProduct) if not known. */
1627 if ( ! u->deviceName ) {
1628 u->deviceName = (char *) (mdevs + devices);
1629 if ( ! d->have_langid && d->descriptor.iProduct) {
1630 alt = usb_get_string(d, 0, 0, u->deviceName, 250);
1631 if (alt < 0) {
1632 printk(KERN_INFO "error getting string descriptor 0 (error=%d)\n", alt);
1633 } else if (u->deviceName[0] < 4) {
1634 printk(KERN_INFO "string descriptor 0 too short (length = %d)\n", alt);
1635 } else {
1636 printk(KERN_INFO "string descriptor 0 found (length = %d)\n", alt);
1637 for(; alt >= 4; alt -= 2) {
1638 i = u->deviceName[alt-2] | (u->deviceName[alt-1]<< 8);
1639 printk(KERN_INFO "usb-midi: langid(%d) 0x%04x\n",
1640 (alt-4) >> 1, i);
1641 if ( ( ( i ^ ulangid ) & 0xff ) == 0 ) {
1642 d->have_langid = 1;
1643 d->string_langid = i;
1644 printk(KERN_INFO "usb-midi: langid(match) 0x%04x\n", i);
1645 if ( i == ulangid )
1646 break;
1647 }
1648 }
1649 }
1650 }
1651 u->deviceName[0] = (char) 0;
1652 if (d->descriptor.iProduct) {
1653 printk(KERN_INFO "usb-midi: fetchString(%d)\n", d->descriptor.iProduct);
1654 alt = usb_string(d, d->descriptor.iProduct, u->deviceName, 255);
1655 if( alt < 0 ) {
1656 u->deviceName[0] = (char) 0;
1657 }
1658 printk(KERN_INFO "usb-midi: fetchString = %d\n", alt);
1659 }
1660 /* Failsafe */
1661 if ( !u->deviceName[0] ) {
1662 if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_ROLAND ) {
1663 strcpy(u->deviceName, "Unknown Roland");
1664 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_STEINBERG ) {
1665 strcpy(u->deviceName, "Unknown Steinberg");
1666 } else if (le16_to_cpu(d->descriptor.idVendor) == USB_VENDOR_ID_YAMAHA ) {
1667 strcpy(u->deviceName, "Unknown Yamaha");
1668 } else {
1669 strcpy(u->deviceName, "Unknown");
1670 }
1671 }
1672 }
1673
1674 inEndpoint = 0; inCableId = -1;
1675 outEndpoint = 0; outCableId = -1;
1676
1677 for ( i=0 ; i<devices ; i++ ) {
1678 for ( inCableId ++ ;
1679 inEndpoint <15
1680 && mins[inEndpoint]
1681 && !(u->in[inEndpoint].cableId & (1<<inCableId)) ;
1682 inCableId++ ) {
1683 if ( inCableId >= 16 ) {
1684 inEndpoint ++;
1685 inCableId = 0;
1686 }
1687 }
1688 min = mins[inEndpoint];
1689 for ( outCableId ++ ;
1690 outEndpoint <15
1691 && mouts[outEndpoint]
1692 && !(u->out[outEndpoint].cableId & (1<<outCableId)) ;
1693 outCableId++ ) {
1694 if ( outCableId >= 16 ) {
1695 outEndpoint ++;
1696 outCableId = 0;
1697 }
1698 }
1699 mout = mouts[outEndpoint];
1700
1701 mdevs[i] = allocMidiDev( s, min, mout, inCableId, outCableId );
1702 if ( mdevs[i] == NULL )
1703 goto error_end;
1704
1705 }
1706
1707 /* Success! */
1708 for ( i=0 ; i<devices ; i++ ) {
1709 list_add_tail( &mdevs[i]->list, &s->midiDevList );
1710 }
1711 for ( i=0 ; i<inEndpoints ; i++ ) {
1712 list_add_tail( &mins[i]->list, &s->inEndpointList );
1713 }
1714 for ( i=0 ; i<outEndpoints ; i++ ) {
1715 list_add_tail( &mouts[i]->list, &s->outEndpointList );
1716 }
1717
1718 printk(KERN_INFO "usbmidi: found [ %s ] (0x%04x:0x%04x), attached:\n", u->deviceName, u->idVendor, u->idProduct );
1719 for ( i=0 ; i<devices ; i++ ) {
1720 int dm = (mdevs[i]->dev_midi-2)>>4;
1721 if ( mdevs[i]->mout.ep != NULL && mdevs[i]->min.ep != NULL ) {
1722 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%2d) out (ep:%02x cid:%2d bufsiz:%2d)\n",
1723 dm,
1724 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize,
1725 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1726 } else if ( mdevs[i]->min.ep != NULL ) {
1727 printk(KERN_INFO "usbmidi: /dev/midi%02d: in (ep:%02x cid:%2d bufsiz:%02d)\n",
1728 dm,
1729 mdevs[i]->min.ep->endpoint|USB_DIR_IN, mdevs[i]->min.cableId, mdevs[i]->min.ep->recvBufSize);
1730 } else if ( mdevs[i]->mout.ep != NULL ) {
1731 printk(KERN_INFO "usbmidi: /dev/midi%02d: out (ep:%02x cid:%2d bufsiz:%02d)\n",
1732 dm,
1733 mdevs[i]->mout.ep->endpoint, mdevs[i]->mout.cableId, mdevs[i]->mout.ep->bufSize);
1734 }
1735 }
1736
1737 kfree(mdevs);
1738 return 0;
1739
1740 error_end:
1741 if ( mdevs != NULL ) {
1742 for ( i=0 ; i<devices ; i++ ) {
1743 if ( mdevs[i] != NULL ) {
1744 unregister_sound_midi( mdevs[i]->dev_midi );
1745 kfree(mdevs[i]);
1746 }
1747 }
1748 kfree(mdevs);
1749 }
1750
1751 for ( i=0 ; i<15 ; i++ ) {
1752 if ( mins[i] != NULL ) {
1753 remove_midi_in_endpoint( mins[i] );
1754 }
1755 if ( mouts[i] != NULL ) {
1756 remove_midi_out_endpoint( mouts[i] );
1757 }
1758 }
1759
1760 return -ENOMEM;
1761}
1762
1763/* ------------------------------------------------------------------------- */
1764
1765/** Attempt to scan YAMAHA's device descriptor and detect correct values of
1766 * them.
1767 * Return 0 on succes, negative on failure.
1768 * Called by usb_midi_probe();
1769 **/
1770
1771static int detect_yamaha_device( struct usb_device *d,
1772 struct usb_interface *iface, unsigned int ifnum,
1773 struct usb_midi_state *s)
1774{
1775 struct usb_host_interface *interface;
1776 struct usb_midi_device *u;
1777 unsigned char *buffer;
1778 int bufSize;
1779 int i;
1780 int alts=-1;
1781 int ret;
1782
1783 if (le16_to_cpu(d->descriptor.idVendor) != USB_VENDOR_ID_YAMAHA) {
1784 return -EINVAL;
1785 }
1786
1787 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1788 interface = iface->altsetting + i;
1789
1790 if ( interface->desc.bInterfaceClass != 255 ||
1791 interface->desc.bInterfaceSubClass != 0 )
1792 continue;
1793 alts = interface->desc.bAlternateSetting;
1794 }
1795 if ( alts == -1 ) {
1796 return -EINVAL;
1797 }
1798
1799 printk(KERN_INFO "usb-midi: Found YAMAHA USB-MIDI device on dev %04x:%04x, iface %d\n",
1800 le16_to_cpu(d->descriptor.idVendor),
1801 le16_to_cpu(d->descriptor.idProduct), ifnum);
1802
1803 i = d->actconfig - d->config;
1804 buffer = d->rawdescriptors[i];
1805 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1806
1807 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 1);
1808 if ( u == NULL ) {
1809 return -EINVAL;
1810 }
1811
1812 ret = alloc_usb_midi_device( d, s, u );
1813
1814 kfree(u);
1815
1816 return ret;
1817}
1818
1819
1820/** Scan table of known devices which are only partially compliant with
1821 * the MIDIStreaming specification.
1822 * Called by usb_midi_probe();
1823 *
1824 **/
1825
1826static int detect_vendor_specific_device( struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s )
1827{
1828 struct usb_midi_device *u;
1829 int i;
1830 int ret = -ENXIO;
1831
1832 for ( i=0; i<VENDOR_SPECIFIC_USB_MIDI_DEVICES ; i++ ) {
1833 u=&(usb_midi_devices[i]);
1834
1835 if ( le16_to_cpu(d->descriptor.idVendor) != u->idVendor ||
1836 le16_to_cpu(d->descriptor.idProduct) != u->idProduct ||
1837 ifnum != u->interface )
1838 continue;
1839
1840 ret = alloc_usb_midi_device( d, s, u );
1841 break;
1842 }
1843
1844 return ret;
1845}
1846
1847
1848/** Attempt to match any config of an interface to a MIDISTREAMING interface.
1849 * Returns 0 on success, negative on failure.
1850 * Called by usb_midi_probe();
1851 **/
1852static int detect_midi_subclass(struct usb_device *d,
1853 struct usb_interface *iface, unsigned int ifnum,
1854 struct usb_midi_state *s)
1855{
1856 struct usb_host_interface *interface;
1857 struct usb_midi_device *u;
1858 unsigned char *buffer;
1859 int bufSize;
1860 int i;
1861 int alts=-1;
1862 int ret;
1863
1864 for ( i=0 ; i < iface->num_altsetting; i++ ) {
1865 interface = iface->altsetting + i;
1866
1867 if ( interface->desc.bInterfaceClass != USB_CLASS_AUDIO ||
1868 interface->desc.bInterfaceSubClass != USB_SUBCLASS_MIDISTREAMING )
1869 continue;
1870 alts = interface->desc.bAlternateSetting;
1871 }
1872 if ( alts == -1 ) {
1873 return -EINVAL;
1874 }
1875
1876 printk(KERN_INFO "usb-midi: Found MIDISTREAMING on dev %04x:%04x, iface %d\n",
1877 le16_to_cpu(d->descriptor.idVendor),
1878 le16_to_cpu(d->descriptor.idProduct), ifnum);
1879
1880
1881 /* From USB Spec v2.0, Section 9.5.
1882 If the class or vendor specific descriptors use the same format
1883 as standard descriptors (e.g., start with a length byte and
1884 followed by a type byte), they must be returned interleaved with
1885 standard descriptors in the configuration information returned by
1886 a GetDescriptor(Configuration) request. In this case, the class
1887 or vendor-specific descriptors must follow a related standard
1888 descriptor they modify or extend.
1889 */
1890
1891 i = d->actconfig - d->config;
1892 buffer = d->rawdescriptors[i];
1893 bufSize = le16_to_cpu(d->actconfig->desc.wTotalLength);
1894
1895 u = parse_descriptor( d, buffer, bufSize, ifnum, alts, 0);
1896 if ( u == NULL ) {
1897 return -EINVAL;
1898 }
1899
1900 ret = alloc_usb_midi_device( d, s, u );
1901
1902 kfree(u);
1903
1904 return ret;
1905}
1906
1907
1908/** When user has requested a specific device, match it exactly.
1909 *
1910 * Uses uvendor, uproduct, uinterface, ualt, umin, umout and ucable.
1911 * Called by usb_midi_probe();
1912 *
1913 **/
1914static int detect_by_hand(struct usb_device *d, unsigned int ifnum, struct usb_midi_state *s)
1915{
1916 struct usb_midi_device u;
1917
1918 if ( le16_to_cpu(d->descriptor.idVendor) != uvendor ||
1919 le16_to_cpu(d->descriptor.idProduct) != uproduct ||
1920 ifnum != uinterface ) {
1921 return -EINVAL;
1922 }
1923
1924 if ( ualt < 0 )
1925 ualt = -1;
1926
1927 if ( umin < 0 || umin > 15 )
1928 umin = 0x01 | USB_DIR_IN;
1929 if ( umout < 0 || umout > 15 )
1930 umout = 0x01;
1931 if ( ucable < 0 || ucable > 15 )
1932 ucable = 0;
1933
1934 u.deviceName = NULL; /* A flag for alloc_usb_midi_device to get device
1935 name from device. */
1936 u.idVendor = uvendor;
1937 u.idProduct = uproduct;
1938 u.interface = uinterface;
1939 u.altSetting = ualt;
1940
1941 u.in[0].endpoint = umin;
1942 u.in[0].cableId = (1<<ucable);
1943
1944 u.out[0].endpoint = umout;
1945 u.out[0].cableId = (1<<ucable);
1946
1947 return alloc_usb_midi_device( d, s, &u );
1948}
1949
1950
1951
1952/* ------------------------------------------------------------------------- */
1953
1954static int usb_midi_probe(struct usb_interface *intf,
1955 const struct usb_device_id *id)
1956{
1957 struct usb_midi_state *s;
1958 struct usb_device *dev = interface_to_usbdev(intf);
1959 int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1960
1961 s = (struct usb_midi_state *)kmalloc(sizeof(struct usb_midi_state), GFP_KERNEL);
1962 if ( !s )
1963 return -ENOMEM;
1964
1965 memset( s, 0, sizeof(struct usb_midi_state) );
1966 INIT_LIST_HEAD(&s->midiDevList);
1967 INIT_LIST_HEAD(&s->inEndpointList);
1968 INIT_LIST_HEAD(&s->outEndpointList);
1969 s->usbdev = dev;
1970 s->count = 0;
1971 spin_lock_init(&s->lock);
1972
1973 if (
1974 detect_by_hand( dev, ifnum, s ) &&
1975 detect_midi_subclass( dev, intf, ifnum, s ) &&
1976 detect_vendor_specific_device( dev, ifnum, s ) &&
1977 detect_yamaha_device( dev, intf, ifnum, s) ) {
1978 kfree(s);
1979 return -EIO;
1980 }
1981
1982 down(&open_sem);
1983 list_add_tail(&s->mididev, &mididevs);
1984 up(&open_sem);
1985
1986 usb_set_intfdata (intf, s);
1987 return 0;
1988}
1989
1990
1991static void usb_midi_disconnect(struct usb_interface *intf)
1992{
1993 struct usb_midi_state *s = usb_get_intfdata (intf);
1994 struct usb_mididev *m;
1995
1996 if ( !s )
1997 return;
1998
1999 if ( s == (struct usb_midi_state *)-1 ) {
2000 return;
2001 }
2002 if ( !s->usbdev ) {
2003 return;
2004 }
2005 down(&open_sem);
2006 list_del(&s->mididev);
2007 INIT_LIST_HEAD(&s->mididev);
2008 s->usbdev = NULL;
2009 usb_set_intfdata (intf, NULL);
2010
2011 list_for_each_entry(m, &s->midiDevList, list) {
2012 wake_up(&(m->min.ep->wait));
2013 wake_up(&(m->mout.ep->wait));
2014 if ( m->dev_midi >= 0 ) {
2015 unregister_sound_midi(m->dev_midi);
2016 }
2017 m->dev_midi = -1;
2018 }
2019 release_midi_device(s);
2020 wake_up(&open_wait);
2021}
2022
2023/* we want to look at all devices by hand */
2024static struct usb_device_id id_table[] = {
2025 {.driver_info = 42},
2026 {}
2027};
2028
2029static struct usb_driver usb_midi_driver = {
2030 .name = "midi",
2031 .probe = usb_midi_probe,
2032 .disconnect = usb_midi_disconnect,
2033 .id_table = id_table,
2034};
2035
2036/* ------------------------------------------------------------------------- */
2037
2038static int __init usb_midi_init(void)
2039{
2040 return usb_register(&usb_midi_driver);
2041}
2042
2043static void __exit usb_midi_exit(void)
2044{
2045 usb_deregister(&usb_midi_driver);
2046}
2047
2048module_init(usb_midi_init) ;
2049module_exit(usb_midi_exit) ;
2050
2051#ifdef HAVE_ALSA_SUPPORT
2052#define SNDRV_MAIN_OBJECT_FILE
2053#include "../../include/driver.h"
2054#include "../../include/control.h"
2055#include "../../include/info.h"
2056#include "../../include/cs46xx.h"
2057
2058/* ------------------------------------------------------------------------- */
2059
2060static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream)
2061{
2062 return 0;
2063}
2064
2065static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream )
2066{
2067 return 0;
2068}
2069
2070static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
2071{
2072 return 0;
2073}
2074
2075
2076/* ------------------------------------------------------------------------- */
2077
2078static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream)
2079{
2080 return 0;
2081}
2082
2083static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream)
2084{
2085 return 0;
2086}
2087
2088static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream,
2089 int up)
2090{
2091 return 0;
2092}
2093
2094/* ------------------------------------------------------------------------- */
2095
2096static snd_rawmidi_ops_t snd_usbmidi_output =
2097{
2098 .open = snd_usbmidi_output_open,
2099 .close = snd_usbmidi_output_close,
2100 .trigger = snd_usbmidi_output_trigger,
2101};
2102static snd_rawmidi_ops_t snd_usbmidi_input =
2103{
2104 .open = snd_usbmidi_input_open,
2105 .close = snd_usbmidi_input_close,
2106 .trigger = snd_usbmidi_input_trigger,
2107};
2108
2109int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
2110{
2111 snd_rawmidi_t *rmidi;
2112 int err;
2113
2114 if (rrawmidi)
2115 *rrawmidi = NULL;
2116 if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0)
2117 return err;
2118 strcpy(rmidi->name, "USB-MIDI");
2119
2120 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output );
2121 snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input );
2122
2123 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
2124
2125 rmidi->private_data = chip;
2126 chip->rmidi = rmidi;
2127 if (rrawmidi)
2128 *rrawmidi = NULL;
2129
2130 return 0;
2131}
2132
2133int snd_usbmidi_create( snd_card_t * card,
2134 struct pci_dev * pci,
2135 usbmidi_t ** rchip )
2136{
2137 usbmidi_t *chip;
2138 int err, idx;
2139 snd_region_t *region;
2140 static snd_device_opt_t ops = {
2141 .dev_free = snd_usbmidi_dev_free,
2142 };
2143
2144 *rchip = NULL;
2145 chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL );
2146 if ( chip == NULL )
2147 return -ENOMEM;
2148}
2149
2150EXPORT_SYMBOL(snd_usbmidi_create);
2151EXPORT_SYMBOL(snd_usbmidi_midi);
2152#endif /* HAVE_ALSA_SUPPORT */
2153
diff --git a/drivers/usb/class/usb-midi.h b/drivers/usb/class/usb-midi.h
deleted file mode 100644
index 358cdef8492e..000000000000
--- a/drivers/usb/class/usb-midi.h
+++ /dev/null
@@ -1,164 +0,0 @@
1/*
2 usb-midi.h -- USB-MIDI driver
3
4 Copyright (C) 2001
5 NAGANO Daisuke <breeze.nagano@nifty.ne.jp>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/* ------------------------------------------------------------------------- */
23
24#ifndef _USB_MIDI_H_
25#define _USB_MIDI_H_
26
27#ifndef USB_SUBCLASS_MIDISTREAMING
28#define USB_SUBCLASS_MIDISTREAMING 3
29#endif
30
31/* ------------------------------------------------------------------------- */
32/* Roland MIDI Devices */
33
34#define USB_VENDOR_ID_ROLAND 0x0582
35#define USBMIDI_ROLAND_UA100G 0x0000
36#define USBMIDI_ROLAND_MPU64 0x0002
37#define USBMIDI_ROLAND_SC8850 0x0003
38#define USBMIDI_ROLAND_SC8820 0x0007
39#define USBMIDI_ROLAND_UM2 0x0005
40#define USBMIDI_ROLAND_UM1 0x0009
41#define USBMIDI_ROLAND_PC300 0x0008
42
43/* YAMAHA MIDI Devices */
44#define USB_VENDOR_ID_YAMAHA 0x0499
45#define USBMIDI_YAMAHA_MU1000 0x1001
46
47/* Steinberg MIDI Devices */
48#define USB_VENDOR_ID_STEINBERG 0x0763
49#define USBMIDI_STEINBERG_USB2MIDI 0x1001
50
51/* Mark of the Unicorn MIDI Devices */
52#define USB_VENDOR_ID_MOTU 0x07fd
53#define USBMIDI_MOTU_FASTLANE 0x0001
54
55/* ------------------------------------------------------------------------- */
56/* Supported devices */
57
58struct usb_midi_endpoint {
59 int endpoint;
60 int cableId; /* if bit-n == 1 then cableId-n is enabled (n: 0 - 15) */
61};
62
63struct usb_midi_device {
64 char *deviceName;
65
66 u16 idVendor;
67 u16 idProduct;
68 int interface;
69 int altSetting; /* -1: auto detect */
70
71 struct usb_midi_endpoint in[15];
72 struct usb_midi_endpoint out[15];
73};
74
75static struct usb_midi_device usb_midi_devices[] = {
76 { /* Roland UM-1 */
77 "Roland UM-1",
78 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1, 2, -1,
79 { { 0x81, 1 }, {-1, -1} },
80 { { 0x01, 1,}, {-1, -1} },
81 },
82
83 { /* Roland UM-2 */
84 "Roland UM-2" ,
85 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2, 2, -1,
86 { { 0x81, 3 }, {-1, -1} },
87 { { 0x01, 3,}, {-1, -1} },
88 },
89
90/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
91 { /* Roland UA-100 */
92 "Roland UA-100",
93 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G, 2, -1,
94 { { 0x82, 7 }, {-1, -1} }, /** cables 0,1 and 2 for SYSEX **/
95 { { 0x02, 7 }, {-1, -1} },
96 },
97
98/** Next entry courtesy research by Michael Minn <michael@michaelminn.com> **/
99 { /* Roland SC8850 */
100 "Roland SC8850",
101 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850, 2, -1,
102 { { 0x81, 0x3f }, {-1, -1} },
103 { { 0x01, 0x3f }, {-1, -1} },
104 },
105
106 { /* Roland SC8820 */
107 "Roland SC8820",
108 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
109 { { 0x81, 0x13 }, {-1, -1} },
110 { { 0x01, 0x13 }, {-1, -1} },
111 },
112
113 { /* Roland SC8820 */
114 "Roland SC8820",
115 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820, 2, -1,
116 { { 0x81, 17 }, {-1, -1} },
117 { { 0x01, 17 }, {-1, -1} },
118 },
119
120 { /* YAMAHA MU1000 */
121 "YAMAHA MU1000",
122 USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000, 0, -1,
123 { { 0x81, 1 }, {-1, -1} },
124 { { 0x01, 15 }, {-1, -1} },
125 },
126 { /* Roland PC-300 */
127 "Roland PC-300",
128 USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300, 2, -1,
129 { { 0x81, 1 }, {-1, -1} },
130 { { 0x01, 1 }, {-1, -1} },
131 },
132 { /* MOTU Fastlane USB */
133 "MOTU Fastlane USB",
134 USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE, 1, 0,
135 { { 0x82, 3 }, {-1, -1} },
136 { { 0x02, 3 }, {-1, -1} },
137 }
138};
139
140#define VENDOR_SPECIFIC_USB_MIDI_DEVICES (sizeof(usb_midi_devices)/sizeof(struct usb_midi_device))
141
142/* for Hot-Plugging */
143
144static struct usb_device_id usb_midi_ids [] = {
145 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
146 .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING},
147 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM1 ) },
148 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UM2 ) },
149 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_UA100G ) },
150 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_PC300 ) },
151 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8850 ) },
152 { USB_DEVICE( USB_VENDOR_ID_ROLAND, USBMIDI_ROLAND_SC8820 ) },
153 { USB_DEVICE( USB_VENDOR_ID_YAMAHA, USBMIDI_YAMAHA_MU1000 ) },
154 { USB_DEVICE( USB_VENDOR_ID_MOTU, USBMIDI_MOTU_FASTLANE ) },
155/* { USB_DEVICE( USB_VENDOR_ID_STEINBERG, USBMIDI_STEINBERG_USB2MIDI ) },*/
156 { } /* Terminating entry */
157};
158
159MODULE_DEVICE_TABLE (usb, usb_midi_ids);
160
161/* ------------------------------------------------------------------------- */
162#endif /* _USB_MIDI_H_ */
163
164
diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c
index d34848ac30b0..48dee4b8d8e5 100644
--- a/drivers/usb/class/usblp.c
+++ b/drivers/usb/class/usblp.c
@@ -55,6 +55,7 @@
55#include <linux/init.h> 55#include <linux/init.h>
56#include <linux/slab.h> 56#include <linux/slab.h>
57#include <linux/lp.h> 57#include <linux/lp.h>
58#include <linux/mutex.h>
58#undef DEBUG 59#undef DEBUG
59#include <linux/usb.h> 60#include <linux/usb.h>
60 61
@@ -223,7 +224,7 @@ static int usblp_cache_device_id_string(struct usblp *usblp);
223 224
224/* forward reference to make our lives easier */ 225/* forward reference to make our lives easier */
225static struct usb_driver usblp_driver; 226static struct usb_driver usblp_driver;
226static DECLARE_MUTEX(usblp_sem); /* locks the existence of usblp's */ 227static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
227 228
228/* 229/*
229 * Functions for usblp control messages. 230 * Functions for usblp control messages.
@@ -351,7 +352,7 @@ static int usblp_open(struct inode *inode, struct file *file)
351 if (minor < 0) 352 if (minor < 0)
352 return -ENODEV; 353 return -ENODEV;
353 354
354 down (&usblp_sem); 355 mutex_lock (&usblp_mutex);
355 356
356 retval = -ENODEV; 357 retval = -ENODEV;
357 intf = usb_find_interface(&usblp_driver, minor); 358 intf = usb_find_interface(&usblp_driver, minor);
@@ -399,7 +400,7 @@ static int usblp_open(struct inode *inode, struct file *file)
399 } 400 }
400 } 401 }
401out: 402out:
402 up (&usblp_sem); 403 mutex_unlock (&usblp_mutex);
403 return retval; 404 return retval;
404} 405}
405 406
@@ -425,13 +426,13 @@ static int usblp_release(struct inode *inode, struct file *file)
425{ 426{
426 struct usblp *usblp = file->private_data; 427 struct usblp *usblp = file->private_data;
427 428
428 down (&usblp_sem); 429 mutex_lock (&usblp_mutex);
429 usblp->used = 0; 430 usblp->used = 0;
430 if (usblp->present) { 431 if (usblp->present) {
431 usblp_unlink_urbs(usblp); 432 usblp_unlink_urbs(usblp);
432 } else /* finish cleanup from disconnect */ 433 } else /* finish cleanup from disconnect */
433 usblp_cleanup (usblp); 434 usblp_cleanup (usblp);
434 up (&usblp_sem); 435 mutex_unlock (&usblp_mutex);
435 return 0; 436 return 0;
436} 437}
437 438
@@ -1152,7 +1153,7 @@ static void usblp_disconnect(struct usb_interface *intf)
1152 1153
1153 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1154 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1154 1155
1155 down (&usblp_sem); 1156 mutex_lock (&usblp_mutex);
1156 down (&usblp->sem); 1157 down (&usblp->sem);
1157 usblp->present = 0; 1158 usblp->present = 0;
1158 usb_set_intfdata (intf, NULL); 1159 usb_set_intfdata (intf, NULL);
@@ -1166,7 +1167,7 @@ static void usblp_disconnect(struct usb_interface *intf)
1166 1167
1167 if (!usblp->used) 1168 if (!usblp->used)
1168 usblp_cleanup (usblp); 1169 usblp_cleanup (usblp);
1169 up (&usblp_sem); 1170 mutex_unlock (&usblp_mutex);
1170} 1171}
1171 1172
1172static struct usb_device_id usblp_ids [] = { 1173static struct usb_device_id usblp_ids [] = {
diff --git a/drivers/usb/core/devices.c b/drivers/usb/core/devices.c
index 2684e15b813b..c0f37343a276 100644
--- a/drivers/usb/core/devices.c
+++ b/drivers/usb/core/devices.c
@@ -57,6 +57,7 @@
57#include <linux/usb.h> 57#include <linux/usb.h>
58#include <linux/smp_lock.h> 58#include <linux/smp_lock.h>
59#include <linux/usbdevice_fs.h> 59#include <linux/usbdevice_fs.h>
60#include <linux/mutex.h>
60#include <asm/uaccess.h> 61#include <asm/uaccess.h>
61 62
62#include "usb.h" 63#include "usb.h"
@@ -570,7 +571,7 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
570 if (!access_ok(VERIFY_WRITE, buf, nbytes)) 571 if (!access_ok(VERIFY_WRITE, buf, nbytes))
571 return -EFAULT; 572 return -EFAULT;
572 573
573 down (&usb_bus_list_lock); 574 mutex_lock(&usb_bus_list_lock);
574 /* print devices for all busses */ 575 /* print devices for all busses */
575 list_for_each_entry(bus, &usb_bus_list, bus_list) { 576 list_for_each_entry(bus, &usb_bus_list, bus_list) {
576 /* recurse through all children of the root hub */ 577 /* recurse through all children of the root hub */
@@ -580,12 +581,12 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
580 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0); 581 ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0);
581 usb_unlock_device(bus->root_hub); 582 usb_unlock_device(bus->root_hub);
582 if (ret < 0) { 583 if (ret < 0) {
583 up(&usb_bus_list_lock); 584 mutex_unlock(&usb_bus_list_lock);
584 return ret; 585 return ret;
585 } 586 }
586 total_written += ret; 587 total_written += ret;
587 } 588 }
588 up (&usb_bus_list_lock); 589 mutex_unlock(&usb_bus_list_lock);
589 return total_written; 590 return total_written;
590} 591}
591 592
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index 2b68998fe4b3..545da37afca7 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -134,26 +134,21 @@ static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, l
134 } 134 }
135 135
136 if (pos < sizeof(struct usb_device_descriptor)) { 136 if (pos < sizeof(struct usb_device_descriptor)) {
137 struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL); 137 struct usb_device_descriptor temp_desc ; /* 18 bytes - fits on the stack */
138 if (!desc) { 138
139 ret = -ENOMEM; 139 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
140 goto err; 140 le16_to_cpus(&temp_desc.bcdUSB);
141 } 141 le16_to_cpus(&temp_desc.idVendor);
142 memcpy(desc, &dev->descriptor, sizeof(dev->descriptor)); 142 le16_to_cpus(&temp_desc.idProduct);
143 le16_to_cpus(&desc->bcdUSB); 143 le16_to_cpus(&temp_desc.bcdDevice);
144 le16_to_cpus(&desc->idVendor);
145 le16_to_cpus(&desc->idProduct);
146 le16_to_cpus(&desc->bcdDevice);
147 144
148 len = sizeof(struct usb_device_descriptor) - pos; 145 len = sizeof(struct usb_device_descriptor) - pos;
149 if (len > nbytes) 146 if (len > nbytes)
150 len = nbytes; 147 len = nbytes;
151 if (copy_to_user(buf, ((char *)desc) + pos, len)) { 148 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
152 kfree(desc);
153 ret = -EFAULT; 149 ret = -EFAULT;
154 goto err; 150 goto err;
155 } 151 }
156 kfree(desc);
157 152
158 *ppos += len; 153 *ppos += len;
159 buf += len; 154 buf += len;
@@ -498,7 +493,8 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsig
498{ 493{
499 int ret = 0; 494 int ret = 0;
500 495
501 if (ps->dev->state != USB_STATE_CONFIGURED) 496 if (ps->dev->state != USB_STATE_ADDRESS
497 && ps->dev->state != USB_STATE_CONFIGURED)
502 return -EHOSTUNREACH; 498 return -EHOSTUNREACH;
503 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype)) 499 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
504 return 0; 500 return 0;
diff --git a/drivers/usb/core/hcd-pci.c b/drivers/usb/core/hcd-pci.c
index 29b5b2a6e183..e0afb5ad29e5 100644
--- a/drivers/usb/core/hcd-pci.c
+++ b/drivers/usb/core/hcd-pci.c
@@ -264,14 +264,19 @@ int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message)
264 */ 264 */
265 retval = pci_set_power_state (dev, PCI_D3hot); 265 retval = pci_set_power_state (dev, PCI_D3hot);
266 if (retval == 0) { 266 if (retval == 0) {
267 dev_dbg (hcd->self.controller, "--> PCI D3\n"); 267 int wake = device_can_wakeup(&hcd->self.root_hub->dev);
268
269 wake = wake && device_may_wakeup(hcd->self.controller);
270
271 dev_dbg (hcd->self.controller, "--> PCI D3%s\n",
272 wake ? "/wakeup" : "");
268 273
269 /* Ignore these return values. We rely on pci code to 274 /* Ignore these return values. We rely on pci code to
270 * reject requests the hardware can't implement, rather 275 * reject requests the hardware can't implement, rather
271 * than coding the same thing. 276 * than coding the same thing.
272 */ 277 */
273 (void) pci_enable_wake (dev, PCI_D3hot, hcd->remote_wakeup); 278 (void) pci_enable_wake (dev, PCI_D3hot, wake);
274 (void) pci_enable_wake (dev, PCI_D3cold, hcd->remote_wakeup); 279 (void) pci_enable_wake (dev, PCI_D3cold, wake);
275 } else { 280 } else {
276 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n", 281 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n",
277 retval); 282 retval);
diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 0018bbc4de34..fbd938d4ea58 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -34,6 +34,7 @@
34#include <asm/scatterlist.h> 34#include <asm/scatterlist.h>
35#include <linux/device.h> 35#include <linux/device.h>
36#include <linux/dma-mapping.h> 36#include <linux/dma-mapping.h>
37#include <linux/mutex.h>
37#include <asm/irq.h> 38#include <asm/irq.h>
38#include <asm/byteorder.h> 39#include <asm/byteorder.h>
39 40
@@ -93,7 +94,7 @@ struct usb_busmap {
93static struct usb_busmap busmap; 94static struct usb_busmap busmap;
94 95
95/* used when updating list of hcds */ 96/* used when updating list of hcds */
96DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */ 97DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
97EXPORT_SYMBOL_GPL (usb_bus_list_lock); 98EXPORT_SYMBOL_GPL (usb_bus_list_lock);
98 99
99/* used for controlling access to virtual root hubs */ 100/* used for controlling access to virtual root hubs */
@@ -366,21 +367,39 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
366 367
367 /* DEVICE REQUESTS */ 368 /* DEVICE REQUESTS */
368 369
370 /* The root hub's remote wakeup enable bit is implemented using
371 * driver model wakeup flags. If this system supports wakeup
372 * through USB, userspace may change the default "allow wakeup"
373 * policy through sysfs or these calls.
374 *
375 * Most root hubs support wakeup from downstream devices, for
376 * runtime power management (disabling USB clocks and reducing
377 * VBUS power usage). However, not all of them do so; silicon,
378 * board, and BIOS bugs here are not uncommon, so these can't
379 * be treated quite like external hubs.
380 *
381 * Likewise, not all root hubs will pass wakeup events upstream,
382 * to wake up the whole system. So don't assume root hub and
383 * controller capabilities are identical.
384 */
385
369 case DeviceRequest | USB_REQ_GET_STATUS: 386 case DeviceRequest | USB_REQ_GET_STATUS:
370 tbuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP) 387 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
388 << USB_DEVICE_REMOTE_WAKEUP)
371 | (1 << USB_DEVICE_SELF_POWERED); 389 | (1 << USB_DEVICE_SELF_POWERED);
372 tbuf [1] = 0; 390 tbuf [1] = 0;
373 len = 2; 391 len = 2;
374 break; 392 break;
375 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 393 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
376 if (wValue == USB_DEVICE_REMOTE_WAKEUP) 394 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
377 hcd->remote_wakeup = 0; 395 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
378 else 396 else
379 goto error; 397 goto error;
380 break; 398 break;
381 case DeviceOutRequest | USB_REQ_SET_FEATURE: 399 case DeviceOutRequest | USB_REQ_SET_FEATURE:
382 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP) 400 if (device_can_wakeup(&hcd->self.root_hub->dev)
383 hcd->remote_wakeup = 1; 401 && wValue == USB_DEVICE_REMOTE_WAKEUP)
402 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
384 else 403 else
385 goto error; 404 goto error;
386 break; 405 break;
@@ -409,7 +428,7 @@ static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
409 bufp = fs_rh_config_descriptor; 428 bufp = fs_rh_config_descriptor;
410 len = sizeof fs_rh_config_descriptor; 429 len = sizeof fs_rh_config_descriptor;
411 } 430 }
412 if (hcd->can_wakeup) 431 if (device_can_wakeup(&hcd->self.root_hub->dev))
413 patch_wakeup = 1; 432 patch_wakeup = 1;
414 break; 433 break;
415 case USB_DT_STRING << 8: 434 case USB_DT_STRING << 8:
@@ -761,14 +780,14 @@ static int usb_register_bus(struct usb_bus *bus)
761{ 780{
762 int busnum; 781 int busnum;
763 782
764 down (&usb_bus_list_lock); 783 mutex_lock(&usb_bus_list_lock);
765 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); 784 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
766 if (busnum < USB_MAXBUS) { 785 if (busnum < USB_MAXBUS) {
767 set_bit (busnum, busmap.busmap); 786 set_bit (busnum, busmap.busmap);
768 bus->busnum = busnum; 787 bus->busnum = busnum;
769 } else { 788 } else {
770 printk (KERN_ERR "%s: too many buses\n", usbcore_name); 789 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
771 up(&usb_bus_list_lock); 790 mutex_unlock(&usb_bus_list_lock);
772 return -E2BIG; 791 return -E2BIG;
773 } 792 }
774 793
@@ -776,7 +795,7 @@ static int usb_register_bus(struct usb_bus *bus)
776 bus->controller, "usb_host%d", busnum); 795 bus->controller, "usb_host%d", busnum);
777 if (IS_ERR(bus->class_dev)) { 796 if (IS_ERR(bus->class_dev)) {
778 clear_bit(busnum, busmap.busmap); 797 clear_bit(busnum, busmap.busmap);
779 up(&usb_bus_list_lock); 798 mutex_unlock(&usb_bus_list_lock);
780 return PTR_ERR(bus->class_dev); 799 return PTR_ERR(bus->class_dev);
781 } 800 }
782 801
@@ -784,7 +803,7 @@ static int usb_register_bus(struct usb_bus *bus)
784 803
785 /* Add it to the local list of buses */ 804 /* Add it to the local list of buses */
786 list_add (&bus->bus_list, &usb_bus_list); 805 list_add (&bus->bus_list, &usb_bus_list);
787 up (&usb_bus_list_lock); 806 mutex_unlock(&usb_bus_list_lock);
788 807
789 usb_notify_add_bus(bus); 808 usb_notify_add_bus(bus);
790 809
@@ -809,9 +828,9 @@ static void usb_deregister_bus (struct usb_bus *bus)
809 * controller code, as well as having it call this when cleaning 828 * controller code, as well as having it call this when cleaning
810 * itself up 829 * itself up
811 */ 830 */
812 down (&usb_bus_list_lock); 831 mutex_lock(&usb_bus_list_lock);
813 list_del (&bus->bus_list); 832 list_del (&bus->bus_list);
814 up (&usb_bus_list_lock); 833 mutex_unlock(&usb_bus_list_lock);
815 834
816 usb_notify_remove_bus(bus); 835 usb_notify_remove_bus(bus);
817 836
@@ -822,18 +841,17 @@ static void usb_deregister_bus (struct usb_bus *bus)
822 841
823/** 842/**
824 * register_root_hub - called by usb_add_hcd() to register a root hub 843 * register_root_hub - called by usb_add_hcd() to register a root hub
825 * @usb_dev: the usb root hub device to be registered.
826 * @hcd: host controller for this root hub 844 * @hcd: host controller for this root hub
827 * 845 *
828 * This function registers the root hub with the USB subsystem. It sets up 846 * This function registers the root hub with the USB subsystem. It sets up
829 * the device properly in the device tree and stores the root_hub pointer 847 * the device properly in the device tree and then calls usb_new_device()
830 * in the bus structure, then calls usb_new_device() to register the usb 848 * to register the usb device. It also assigns the root hub's USB address
831 * device. It also assigns the root hub's USB address (always 1). 849 * (always 1).
832 */ 850 */
833static int register_root_hub (struct usb_device *usb_dev, 851static int register_root_hub(struct usb_hcd *hcd)
834 struct usb_hcd *hcd)
835{ 852{
836 struct device *parent_dev = hcd->self.controller; 853 struct device *parent_dev = hcd->self.controller;
854 struct usb_device *usb_dev = hcd->self.root_hub;
837 const int devnum = 1; 855 const int devnum = 1;
838 int retval; 856 int retval;
839 857
@@ -844,14 +862,12 @@ static int register_root_hub (struct usb_device *usb_dev,
844 set_bit (devnum, usb_dev->bus->devmap.devicemap); 862 set_bit (devnum, usb_dev->bus->devmap.devicemap);
845 usb_set_device_state(usb_dev, USB_STATE_ADDRESS); 863 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
846 864
847 down (&usb_bus_list_lock); 865 mutex_lock(&usb_bus_list_lock);
848 usb_dev->bus->root_hub = usb_dev;
849 866
850 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 867 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
851 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); 868 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
852 if (retval != sizeof usb_dev->descriptor) { 869 if (retval != sizeof usb_dev->descriptor) {
853 usb_dev->bus->root_hub = NULL; 870 mutex_unlock(&usb_bus_list_lock);
854 up (&usb_bus_list_lock);
855 dev_dbg (parent_dev, "can't read %s device descriptor %d\n", 871 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
856 usb_dev->dev.bus_id, retval); 872 usb_dev->dev.bus_id, retval);
857 return (retval < 0) ? retval : -EMSGSIZE; 873 return (retval < 0) ? retval : -EMSGSIZE;
@@ -859,11 +875,10 @@ static int register_root_hub (struct usb_device *usb_dev,
859 875
860 retval = usb_new_device (usb_dev); 876 retval = usb_new_device (usb_dev);
861 if (retval) { 877 if (retval) {
862 usb_dev->bus->root_hub = NULL;
863 dev_err (parent_dev, "can't register root hub for %s, %d\n", 878 dev_err (parent_dev, "can't register root hub for %s, %d\n",
864 usb_dev->dev.bus_id, retval); 879 usb_dev->dev.bus_id, retval);
865 } 880 }
866 up (&usb_bus_list_lock); 881 mutex_unlock(&usb_bus_list_lock);
867 882
868 if (retval == 0) { 883 if (retval == 0) {
869 spin_lock_irq (&hcd_root_hub_lock); 884 spin_lock_irq (&hcd_root_hub_lock);
@@ -1090,7 +1105,6 @@ static void urb_unlink (struct urb *urb)
1090 spin_lock_irqsave (&hcd_data_lock, flags); 1105 spin_lock_irqsave (&hcd_data_lock, flags);
1091 list_del_init (&urb->urb_list); 1106 list_del_init (&urb->urb_list);
1092 spin_unlock_irqrestore (&hcd_data_lock, flags); 1107 spin_unlock_irqrestore (&hcd_data_lock, flags);
1093 usb_put_dev (urb->dev);
1094} 1108}
1095 1109
1096 1110
@@ -1130,7 +1144,6 @@ static int hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1130 case HC_STATE_RUNNING: 1144 case HC_STATE_RUNNING:
1131 case HC_STATE_RESUMING: 1145 case HC_STATE_RESUMING:
1132doit: 1146doit:
1133 usb_get_dev (urb->dev);
1134 list_add_tail (&urb->urb_list, &ep->urb_list); 1147 list_add_tail (&urb->urb_list, &ep->urb_list);
1135 status = 0; 1148 status = 0;
1136 break; 1149 break;
@@ -1771,12 +1784,10 @@ int usb_add_hcd(struct usb_hcd *hcd,
1771 1784
1772 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1785 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1773 1786
1774 /* till now HC has been in an indeterminate state ... */ 1787 /* HC is in reset state, but accessible. Now do the one-time init,
1775 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { 1788 * bottom up so that hcds can customize the root hubs before khubd
1776 dev_err(hcd->self.controller, "can't reset\n"); 1789 * starts talking to them. (Note, bus id is assigned early too.)
1777 return retval; 1790 */
1778 }
1779
1780 if ((retval = hcd_buffer_create(hcd)) != 0) { 1791 if ((retval = hcd_buffer_create(hcd)) != 0) {
1781 dev_dbg(hcd->self.controller, "pool alloc failed\n"); 1792 dev_dbg(hcd->self.controller, "pool alloc failed\n");
1782 return retval; 1793 return retval;
@@ -1785,6 +1796,36 @@ int usb_add_hcd(struct usb_hcd *hcd,
1785 if ((retval = usb_register_bus(&hcd->self)) < 0) 1796 if ((retval = usb_register_bus(&hcd->self)) < 0)
1786 goto err_register_bus; 1797 goto err_register_bus;
1787 1798
1799 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1800 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1801 retval = -ENOMEM;
1802 goto err_allocate_root_hub;
1803 }
1804 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1805 USB_SPEED_FULL;
1806 hcd->self.root_hub = rhdev;
1807
1808 /* "reset" is misnamed; its role is now one-time init. the controller
1809 * should already have been reset (and boot firmware kicked off etc).
1810 */
1811 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
1812 dev_err(hcd->self.controller, "can't setup\n");
1813 goto err_hcd_driver_setup;
1814 }
1815
1816 /* wakeup flag init is in transition; for now we can't rely on PCI to
1817 * initialize these bits properly, so we let reset() override it.
1818 * This init should _precede_ the reset() once PCI behaves.
1819 */
1820 device_init_wakeup(&rhdev->dev,
1821 device_can_wakeup(hcd->self.controller));
1822
1823 /* NOTE: root hub and controller capabilities may not be the same */
1824 if (device_can_wakeup(hcd->self.controller)
1825 && device_can_wakeup(&hcd->self.root_hub->dev))
1826 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
1827
1828 /* enable irqs just before we start the controller */
1788 if (hcd->driver->irq) { 1829 if (hcd->driver->irq) {
1789 char buf[8], *bufp = buf; 1830 char buf[8], *bufp = buf;
1790 1831
@@ -1816,56 +1857,32 @@ int usb_add_hcd(struct usb_hcd *hcd,
1816 (unsigned long long)hcd->rsrc_start); 1857 (unsigned long long)hcd->rsrc_start);
1817 } 1858 }
1818 1859
1819 /* Allocate the root hub before calling hcd->driver->start(),
1820 * but don't register it until afterward so that the hardware
1821 * is running.
1822 */
1823 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1824 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1825 retval = -ENOMEM;
1826 goto err_allocate_root_hub;
1827 }
1828
1829 /* Although in principle hcd->driver->start() might need to use rhdev,
1830 * none of the current drivers do.
1831 */
1832 if ((retval = hcd->driver->start(hcd)) < 0) { 1860 if ((retval = hcd->driver->start(hcd)) < 0) {
1833 dev_err(hcd->self.controller, "startup error %d\n", retval); 1861 dev_err(hcd->self.controller, "startup error %d\n", retval);
1834 goto err_hcd_driver_start; 1862 goto err_hcd_driver_start;
1835 } 1863 }
1836 1864
1837 /* hcd->driver->start() reported can_wakeup, probably with 1865 /* starting here, usbcore will pay attention to this root hub */
1838 * assistance from board's boot firmware.
1839 * NOTE: normal devices won't enable wakeup by default.
1840 */
1841 if (hcd->can_wakeup)
1842 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
1843 hcd->remote_wakeup = hcd->can_wakeup;
1844
1845 rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
1846 USB_SPEED_FULL;
1847 rhdev->bus_mA = min(500u, hcd->power_budget); 1866 rhdev->bus_mA = min(500u, hcd->power_budget);
1848 if ((retval = register_root_hub(rhdev, hcd)) != 0) 1867 if ((retval = register_root_hub(hcd)) != 0)
1849 goto err_register_root_hub; 1868 goto err_register_root_hub;
1850 1869
1851 if (hcd->uses_new_polling && hcd->poll_rh) 1870 if (hcd->uses_new_polling && hcd->poll_rh)
1852 usb_hcd_poll_rh_status(hcd); 1871 usb_hcd_poll_rh_status(hcd);
1853 return retval; 1872 return retval;
1854 1873
1855 err_register_root_hub: 1874err_register_root_hub:
1856 hcd->driver->stop(hcd); 1875 hcd->driver->stop(hcd);
1857 1876err_hcd_driver_start:
1858 err_hcd_driver_start:
1859 usb_put_dev(rhdev);
1860
1861 err_allocate_root_hub:
1862 if (hcd->irq >= 0) 1877 if (hcd->irq >= 0)
1863 free_irq(irqnum, hcd); 1878 free_irq(irqnum, hcd);
1864 1879err_request_irq:
1865 err_request_irq: 1880err_hcd_driver_setup:
1881 hcd->self.root_hub = NULL;
1882 usb_put_dev(rhdev);
1883err_allocate_root_hub:
1866 usb_deregister_bus(&hcd->self); 1884 usb_deregister_bus(&hcd->self);
1867 1885err_register_bus:
1868 err_register_bus:
1869 hcd_buffer_destroy(hcd); 1886 hcd_buffer_destroy(hcd);
1870 return retval; 1887 return retval;
1871} 1888}
@@ -1891,9 +1908,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
1891 hcd->rh_registered = 0; 1908 hcd->rh_registered = 0;
1892 spin_unlock_irq (&hcd_root_hub_lock); 1909 spin_unlock_irq (&hcd_root_hub_lock);
1893 1910
1894 down(&usb_bus_list_lock); 1911 mutex_lock(&usb_bus_list_lock);
1895 usb_disconnect(&hcd->self.root_hub); 1912 usb_disconnect(&hcd->self.root_hub);
1896 up(&usb_bus_list_lock); 1913 mutex_unlock(&usb_bus_list_lock);
1897 1914
1898 hcd->poll_rh = 0; 1915 hcd->poll_rh = 0;
1899 del_timer_sync(&hcd->rh_timer); 1916 del_timer_sync(&hcd->rh_timer);
diff --git a/drivers/usb/core/hcd.h b/drivers/usb/core/hcd.h
index 591b5aad1a18..7022aafb2ae8 100644
--- a/drivers/usb/core/hcd.h
+++ b/drivers/usb/core/hcd.h
@@ -78,8 +78,6 @@ struct usb_hcd { /* usb_bus.hcpriv points to this */
78#define HCD_FLAG_HW_ACCESSIBLE 0x00000001 78#define HCD_FLAG_HW_ACCESSIBLE 0x00000001
79#define HCD_FLAG_SAW_IRQ 0x00000002 79#define HCD_FLAG_SAW_IRQ 0x00000002
80 80
81 unsigned can_wakeup:1; /* hw supports wakeup? */
82 unsigned remote_wakeup:1;/* sw should use wakeup? */
83 unsigned rh_registered:1;/* is root hub registered? */ 81 unsigned rh_registered:1;/* is root hub registered? */
84 82
85 /* The next flag is a stopgap, to be removed when all the HCDs 83 /* The next flag is a stopgap, to be removed when all the HCDs
@@ -364,7 +362,7 @@ extern void usb_set_device_state(struct usb_device *udev,
364/* exported only within usbcore */ 362/* exported only within usbcore */
365 363
366extern struct list_head usb_bus_list; 364extern struct list_head usb_bus_list;
367extern struct semaphore usb_bus_list_lock; 365extern struct mutex usb_bus_list_lock;
368extern wait_queue_head_t usb_kill_urb_queue; 366extern wait_queue_head_t usb_kill_urb_queue;
369 367
370extern struct usb_bus *usb_bus_get (struct usb_bus *bus); 368extern struct usb_bus *usb_bus_get (struct usb_bus *bus);
diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c
index 650d5ee5871b..8e65f7a237e4 100644
--- a/drivers/usb/core/hub.c
+++ b/drivers/usb/core/hub.c
@@ -22,6 +22,7 @@
22#include <linux/usb.h> 22#include <linux/usb.h>
23#include <linux/usbdevice_fs.h> 23#include <linux/usbdevice_fs.h>
24#include <linux/kthread.h> 24#include <linux/kthread.h>
25#include <linux/mutex.h>
25 26
26#include <asm/semaphore.h> 27#include <asm/semaphore.h>
27#include <asm/uaccess.h> 28#include <asm/uaccess.h>
@@ -1005,12 +1006,18 @@ void usb_set_device_state(struct usb_device *udev,
1005 ; /* do nothing */ 1006 ; /* do nothing */
1006 else if (new_state != USB_STATE_NOTATTACHED) { 1007 else if (new_state != USB_STATE_NOTATTACHED) {
1007 udev->state = new_state; 1008 udev->state = new_state;
1008 if (new_state == USB_STATE_CONFIGURED) 1009
1009 device_init_wakeup(&udev->dev, 1010 /* root hub wakeup capabilities are managed out-of-band
1010 (udev->actconfig->desc.bmAttributes 1011 * and may involve silicon errata ... ignore them here.
1011 & USB_CONFIG_ATT_WAKEUP)); 1012 */
1012 else if (new_state != USB_STATE_SUSPENDED) 1013 if (udev->parent) {
1013 device_init_wakeup(&udev->dev, 0); 1014 if (new_state == USB_STATE_CONFIGURED)
1015 device_init_wakeup(&udev->dev,
1016 (udev->actconfig->desc.bmAttributes
1017 & USB_CONFIG_ATT_WAKEUP));
1018 else if (new_state != USB_STATE_SUSPENDED)
1019 device_init_wakeup(&udev->dev, 0);
1020 }
1014 } else 1021 } else
1015 recursively_mark_NOTATTACHED(udev); 1022 recursively_mark_NOTATTACHED(udev);
1016 spin_unlock_irqrestore(&device_state_lock, flags); 1023 spin_unlock_irqrestore(&device_state_lock, flags);
@@ -1172,8 +1179,11 @@ static int choose_configuration(struct usb_device *udev)
1172 c = udev->config; 1179 c = udev->config;
1173 num_configs = udev->descriptor.bNumConfigurations; 1180 num_configs = udev->descriptor.bNumConfigurations;
1174 for (i = 0; i < num_configs; (i++, c++)) { 1181 for (i = 0; i < num_configs; (i++, c++)) {
1175 struct usb_interface_descriptor *desc = 1182 struct usb_interface_descriptor *desc = NULL;
1176 &c->intf_cache[0]->altsetting->desc; 1183
1184 /* It's possible that a config has no interfaces! */
1185 if (c->desc.bNumInterfaces > 0)
1186 desc = &c->intf_cache[0]->altsetting->desc;
1177 1187
1178 /* 1188 /*
1179 * HP's USB bus-powered keyboard has only one configuration 1189 * HP's USB bus-powered keyboard has only one configuration
@@ -1208,7 +1218,8 @@ static int choose_configuration(struct usb_device *udev)
1208 /* If the first config's first interface is COMM/2/0xff 1218 /* If the first config's first interface is COMM/2/0xff
1209 * (MSFT RNDIS), rule it out unless Linux has host-side 1219 * (MSFT RNDIS), rule it out unless Linux has host-side
1210 * RNDIS support. */ 1220 * RNDIS support. */
1211 if (i == 0 && desc->bInterfaceClass == USB_CLASS_COMM 1221 if (i == 0 && desc
1222 && desc->bInterfaceClass == USB_CLASS_COMM
1212 && desc->bInterfaceSubClass == 2 1223 && desc->bInterfaceSubClass == 2
1213 && desc->bInterfaceProtocol == 0xff) { 1224 && desc->bInterfaceProtocol == 0xff) {
1214#ifndef CONFIG_USB_NET_RNDIS 1225#ifndef CONFIG_USB_NET_RNDIS
@@ -1224,8 +1235,8 @@ static int choose_configuration(struct usb_device *udev)
1224 * than a vendor-specific driver. */ 1235 * than a vendor-specific driver. */
1225 else if (udev->descriptor.bDeviceClass != 1236 else if (udev->descriptor.bDeviceClass !=
1226 USB_CLASS_VENDOR_SPEC && 1237 USB_CLASS_VENDOR_SPEC &&
1227 desc->bInterfaceClass != 1238 (!desc || desc->bInterfaceClass !=
1228 USB_CLASS_VENDOR_SPEC) { 1239 USB_CLASS_VENDOR_SPEC)) {
1229 best = c; 1240 best = c;
1230 break; 1241 break;
1231 } 1242 }
@@ -1876,18 +1887,18 @@ int usb_resume_device(struct usb_device *udev)
1876 if (udev->state == USB_STATE_NOTATTACHED) 1887 if (udev->state == USB_STATE_NOTATTACHED)
1877 return -ENODEV; 1888 return -ENODEV;
1878 1889
1879#ifdef CONFIG_USB_SUSPEND
1880 /* selective resume of one downstream hub-to-device port */ 1890 /* selective resume of one downstream hub-to-device port */
1881 if (udev->parent) { 1891 if (udev->parent) {
1892#ifdef CONFIG_USB_SUSPEND
1882 if (udev->state == USB_STATE_SUSPENDED) { 1893 if (udev->state == USB_STATE_SUSPENDED) {
1883 // NOTE swsusp may bork us, device state being wrong... 1894 // NOTE swsusp may bork us, device state being wrong...
1884 // NOTE this fails if parent is also suspended... 1895 // NOTE this fails if parent is also suspended...
1885 status = hub_port_resume(hdev_to_hub(udev->parent), 1896 status = hub_port_resume(hdev_to_hub(udev->parent),
1886 udev->portnum, udev); 1897 udev->portnum, udev);
1887 } else 1898 } else
1899#endif
1888 status = 0; 1900 status = 0;
1889 } else 1901 } else
1890#endif
1891 status = finish_device_resume(udev); 1902 status = finish_device_resume(udev);
1892 if (status < 0) 1903 if (status < 0)
1893 dev_dbg(&udev->dev, "can't resume, status %d\n", 1904 dev_dbg(&udev->dev, "can't resume, status %d\n",
@@ -2162,7 +2173,7 @@ static int
2162hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 2173hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2163 int retry_counter) 2174 int retry_counter)
2164{ 2175{
2165 static DECLARE_MUTEX(usb_address0_sem); 2176 static DEFINE_MUTEX(usb_address0_mutex);
2166 2177
2167 struct usb_device *hdev = hub->hdev; 2178 struct usb_device *hdev = hub->hdev;
2168 int i, j, retval; 2179 int i, j, retval;
@@ -2183,7 +2194,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2183 if (oldspeed == USB_SPEED_LOW) 2194 if (oldspeed == USB_SPEED_LOW)
2184 delay = HUB_LONG_RESET_TIME; 2195 delay = HUB_LONG_RESET_TIME;
2185 2196
2186 down(&usb_address0_sem); 2197 mutex_lock(&usb_address0_mutex);
2187 2198
2188 /* Reset the device; full speed may morph to high speed */ 2199 /* Reset the device; full speed may morph to high speed */
2189 retval = hub_port_reset(hub, port1, udev, delay); 2200 retval = hub_port_reset(hub, port1, udev, delay);
@@ -2381,7 +2392,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2381fail: 2392fail:
2382 if (retval) 2393 if (retval)
2383 hub_port_disable(hub, port1, 0); 2394 hub_port_disable(hub, port1, 0);
2384 up(&usb_address0_sem); 2395 mutex_unlock(&usb_address0_mutex);
2385 return retval; 2396 return retval;
2386} 2397}
2387 2398
@@ -3017,7 +3028,7 @@ int usb_reset_device(struct usb_device *udev)
3017 parent_hub = hdev_to_hub(parent_hdev); 3028 parent_hub = hdev_to_hub(parent_hdev);
3018 3029
3019 /* If we're resetting an active hub, take some special actions */ 3030 /* If we're resetting an active hub, take some special actions */
3020 if (udev->actconfig && 3031 if (udev->actconfig && udev->actconfig->desc.bNumInterfaces > 0 &&
3021 udev->actconfig->interface[0]->dev.driver == 3032 udev->actconfig->interface[0]->dev.driver ==
3022 &hub_driver.driver && 3033 &hub_driver.driver &&
3023 (hub = hdev_to_hub(udev)) != NULL) { 3034 (hub = hdev_to_hub(udev)) != NULL) {
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 7135e542679d..08fb20f06f3e 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -631,8 +631,8 @@ int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char
631 * Returns the number of bytes received on success, or else the status code 631 * Returns the number of bytes received on success, or else the status code
632 * returned by the underlying usb_control_msg() call. 632 * returned by the underlying usb_control_msg() call.
633 */ 633 */
634int usb_get_string(struct usb_device *dev, unsigned short langid, 634static int usb_get_string(struct usb_device *dev, unsigned short langid,
635 unsigned char index, void *buf, int size) 635 unsigned char index, void *buf, int size)
636{ 636{
637 int i; 637 int i;
638 int result; 638 int result;
@@ -1388,11 +1388,13 @@ free_interfaces:
1388 if (dev->state != USB_STATE_ADDRESS) 1388 if (dev->state != USB_STATE_ADDRESS)
1389 usb_disable_device (dev, 1); // Skip ep0 1389 usb_disable_device (dev, 1); // Skip ep0
1390 1390
1391 i = dev->bus_mA - cp->desc.bMaxPower * 2; 1391 if (cp) {
1392 if (i < 0) 1392 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1393 dev_warn(&dev->dev, "new config #%d exceeds power " 1393 if (i < 0)
1394 "limit by %dmA\n", 1394 dev_warn(&dev->dev, "new config #%d exceeds power "
1395 configuration, -i); 1395 "limit by %dmA\n",
1396 configuration, -i);
1397 }
1396 1398
1397 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 1399 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1398 USB_REQ_SET_CONFIGURATION, 0, configuration, 0, 1400 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
@@ -1488,7 +1490,6 @@ EXPORT_SYMBOL(usb_sg_wait);
1488// synchronous control message convenience routines 1490// synchronous control message convenience routines
1489EXPORT_SYMBOL(usb_get_descriptor); 1491EXPORT_SYMBOL(usb_get_descriptor);
1490EXPORT_SYMBOL(usb_get_status); 1492EXPORT_SYMBOL(usb_get_status);
1491EXPORT_SYMBOL(usb_get_string);
1492EXPORT_SYMBOL(usb_string); 1493EXPORT_SYMBOL(usb_string);
1493 1494
1494// synchronous calls that also maintain usbcore state 1495// synchronous calls that also maintain usbcore state
diff --git a/drivers/usb/core/notify.c b/drivers/usb/core/notify.c
index fbbebab52fbd..4b55285de9a0 100644
--- a/drivers/usb/core/notify.c
+++ b/drivers/usb/core/notify.c
@@ -13,16 +13,17 @@
13#include <linux/kernel.h> 13#include <linux/kernel.h>
14#include <linux/notifier.h> 14#include <linux/notifier.h>
15#include <linux/usb.h> 15#include <linux/usb.h>
16#include <linux/mutex.h>
16#include "usb.h" 17#include "usb.h"
17 18
18 19
19static struct notifier_block *usb_notifier_list; 20static struct notifier_block *usb_notifier_list;
20static DECLARE_MUTEX(usb_notifier_lock); 21static DEFINE_MUTEX(usb_notifier_lock);
21 22
22static void usb_notifier_chain_register(struct notifier_block **list, 23static void usb_notifier_chain_register(struct notifier_block **list,
23 struct notifier_block *n) 24 struct notifier_block *n)
24{ 25{
25 down(&usb_notifier_lock); 26 mutex_lock(&usb_notifier_lock);
26 while (*list) { 27 while (*list) {
27 if (n->priority > (*list)->priority) 28 if (n->priority > (*list)->priority)
28 break; 29 break;
@@ -30,13 +31,13 @@ static void usb_notifier_chain_register(struct notifier_block **list,
30 } 31 }
31 n->next = *list; 32 n->next = *list;
32 *list = n; 33 *list = n;
33 up(&usb_notifier_lock); 34 mutex_unlock(&usb_notifier_lock);
34} 35}
35 36
36static void usb_notifier_chain_unregister(struct notifier_block **nl, 37static void usb_notifier_chain_unregister(struct notifier_block **nl,
37 struct notifier_block *n) 38 struct notifier_block *n)
38{ 39{
39 down(&usb_notifier_lock); 40 mutex_lock(&usb_notifier_lock);
40 while ((*nl)!=NULL) { 41 while ((*nl)!=NULL) {
41 if ((*nl)==n) { 42 if ((*nl)==n) {
42 *nl = n->next; 43 *nl = n->next;
@@ -45,7 +46,7 @@ static void usb_notifier_chain_unregister(struct notifier_block **nl,
45 nl=&((*nl)->next); 46 nl=&((*nl)->next);
46 } 47 }
47exit: 48exit:
48 up(&usb_notifier_lock); 49 mutex_unlock(&usb_notifier_lock);
49} 50}
50 51
51static int usb_notifier_call_chain(struct notifier_block **n, 52static int usb_notifier_call_chain(struct notifier_block **n,
@@ -54,7 +55,7 @@ static int usb_notifier_call_chain(struct notifier_block **n,
54 int ret=NOTIFY_DONE; 55 int ret=NOTIFY_DONE;
55 struct notifier_block *nb = *n; 56 struct notifier_block *nb = *n;
56 57
57 down(&usb_notifier_lock); 58 mutex_lock(&usb_notifier_lock);
58 while (nb) { 59 while (nb) {
59 ret = nb->notifier_call(nb,val,v); 60 ret = nb->notifier_call(nb,val,v);
60 if (ret&NOTIFY_STOP_MASK) { 61 if (ret&NOTIFY_STOP_MASK) {
@@ -63,7 +64,7 @@ static int usb_notifier_call_chain(struct notifier_block **n,
63 nb = nb->next; 64 nb = nb->next;
64 } 65 }
65exit: 66exit:
66 up(&usb_notifier_lock); 67 mutex_unlock(&usb_notifier_lock);
67 return ret; 68 return ret;
68} 69}
69 70
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
index 13d1d367f7f1..d7352aa73b5e 100644
--- a/drivers/usb/core/usb.c
+++ b/drivers/usb/core/usb.c
@@ -33,6 +33,7 @@
33#include <linux/errno.h> 33#include <linux/errno.h>
34#include <linux/smp_lock.h> 34#include <linux/smp_lock.h>
35#include <linux/usb.h> 35#include <linux/usb.h>
36#include <linux/mutex.h>
36 37
37#include <asm/io.h> 38#include <asm/io.h>
38#include <asm/scatterlist.h> 39#include <asm/scatterlist.h>
@@ -639,7 +640,7 @@ struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
639 struct usb_bus *bus; 640 struct usb_bus *bus;
640 struct usb_device *dev = NULL; 641 struct usb_device *dev = NULL;
641 642
642 down(&usb_bus_list_lock); 643 mutex_lock(&usb_bus_list_lock);
643 for (buslist = usb_bus_list.next; 644 for (buslist = usb_bus_list.next;
644 buslist != &usb_bus_list; 645 buslist != &usb_bus_list;
645 buslist = buslist->next) { 646 buslist = buslist->next) {
@@ -653,7 +654,7 @@ struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
653 goto exit; 654 goto exit;
654 } 655 }
655exit: 656exit:
656 up(&usb_bus_list_lock); 657 mutex_unlock(&usb_bus_list_lock);
657 return dev; 658 return dev;
658} 659}
659 660
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index ff075a53c8d6..d80f71877d68 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -187,6 +187,23 @@ config USB_OTG
187 187
188 Select this only if your OMAP board has a Mini-AB connector. 188 Select this only if your OMAP board has a Mini-AB connector.
189 189
190config USB_GADGET_AT91
191 boolean "AT91 USB Device Port"
192 depends on ARCH_AT91RM9200
193 select USB_GADGET_SELECTED
194 help
195 Many Atmel AT91 processors (such as the AT91RM2000) have a
196 full speed USB Device Port with support for five configurable
197 endpoints (plus endpoint zero).
198
199 Say "y" to link the driver statically, or "m" to build a
200 dynamically linked module called "at91_udc" and force all
201 gadget drivers to also be dynamically linked.
202
203config USB_AT91
204 tristate
205 depends on USB_GADGET_AT91
206 default USB_GADGET
190 207
191config USB_GADGET_DUMMY_HCD 208config USB_GADGET_DUMMY_HCD
192 boolean "Dummy HCD (DEVELOPMENT)" 209 boolean "Dummy HCD (DEVELOPMENT)"
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index d5fd04d886e6..5a28e61392ec 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -7,6 +7,7 @@ obj-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
7obj-$(CONFIG_USB_GOKU) += goku_udc.o 7obj-$(CONFIG_USB_GOKU) += goku_udc.o
8obj-$(CONFIG_USB_OMAP) += omap_udc.o 8obj-$(CONFIG_USB_OMAP) += omap_udc.o
9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o 9obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
10obj-$(CONFIG_USB_AT91) += at91_udc.o
10 11
11# 12#
12# USB gadget drivers 13# USB gadget drivers
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
new file mode 100644
index 000000000000..865858cfd1c2
--- /dev/null
+++ b/drivers/usb/gadget/at91_udc.c
@@ -0,0 +1,1773 @@
1/*
2 * at91_udc -- driver for at91-series USB peripheral controller
3 *
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License 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
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24#undef DEBUG
25#undef VERBOSE
26#undef PACKET_TRACE
27
28#include <linux/config.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/platform_device.h>
32#include <linux/delay.h>
33#include <linux/ioport.h>
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/smp_lock.h>
37#include <linux/errno.h>
38#include <linux/init.h>
39#include <linux/list.h>
40#include <linux/interrupt.h>
41#include <linux/proc_fs.h>
42#include <linux/clk.h>
43#include <linux/usb_ch9.h>
44#include <linux/usb_gadget.h>
45
46#include <asm/byteorder.h>
47#include <asm/io.h>
48#include <asm/irq.h>
49#include <asm/system.h>
50#include <asm/mach-types.h>
51
52#include <asm/arch/hardware.h>
53#include <asm/arch/gpio.h>
54#include <asm/arch/board.h>
55
56#include "at91_udc.h"
57
58
59/*
60 * This controller is simple and PIO-only. It's used in many AT91-series
61 * ARMv4T controllers, including the at91rm9200 (arm920T, with MMU),
62 * at91sam9261 (arm926ejs, with MMU), and several no-mmu versions.
63 *
64 * This driver expects the board has been wired with two GPIOs suppporting
65 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
66 * testing hasn't covered such cases.) The pullup is most important; it
67 * provides software control over whether the host enumerates the device.
68 * The VBUS sensing helps during enumeration, and allows both USB clocks
69 * (and the transceiver) to stay gated off until they're necessary, saving
70 * power. During USB suspend, the 48 MHz clock is gated off.
71 */
72
73#define DRIVER_VERSION "8 March 2005"
74
75static const char driver_name [] = "at91_udc";
76static const char ep0name[] = "ep0";
77
78/*-------------------------------------------------------------------------*/
79
80/*
81 * Read from a UDP register.
82 */
83static inline unsigned long at91_udp_read(unsigned int reg)
84{
85 void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP;
86
87 return __raw_readl(udp_base + reg);
88}
89
90/*
91 * Write to a UDP register.
92 */
93static inline void at91_udp_write(unsigned int reg, unsigned long value)
94{
95 void __iomem *udp_base = (void __iomem *)AT91_VA_BASE_UDP;
96
97 __raw_writel(value, udp_base + reg);
98}
99
100/*-------------------------------------------------------------------------*/
101
102#ifdef CONFIG_USB_GADGET_DEBUG_FILES
103
104#include <linux/seq_file.h>
105
106static const char debug_filename[] = "driver/udc";
107
108#define FOURBITS "%s%s%s%s"
109#define EIGHTBITS FOURBITS FOURBITS
110
111static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
112{
113 static char *types[] = {
114 "control", "out-iso", "out-bulk", "out-int",
115 "BOGUS", "in-iso", "in-bulk", "in-int"};
116
117 u32 csr;
118 struct at91_request *req;
119 unsigned long flags;
120
121 local_irq_save(flags);
122
123 csr = __raw_readl(ep->creg);
124
125 /* NOTE: not collecting per-endpoint irq statistics... */
126
127 seq_printf(s, "\n");
128 seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
129 ep->ep.name, ep->ep.maxpacket,
130 ep->is_in ? "in" : "out",
131 ep->is_iso ? " iso" : "",
132 ep->is_pingpong
133 ? (ep->fifo_bank ? "pong" : "ping")
134 : "",
135 ep->stopped ? " stopped" : "");
136 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
137 csr,
138 (csr & 0x07ff0000) >> 16,
139 (csr & (1 << 15)) ? "enabled" : "disabled",
140 (csr & (1 << 11)) ? "DATA1" : "DATA0",
141 types[(csr & 0x700) >> 8],
142
143 /* iff type is control then print current direction */
144 (!(csr & 0x700))
145 ? ((csr & (1 << 7)) ? " IN" : " OUT")
146 : "",
147 (csr & (1 << 6)) ? " rxdatabk1" : "",
148 (csr & (1 << 5)) ? " forcestall" : "",
149 (csr & (1 << 4)) ? " txpktrdy" : "",
150
151 (csr & (1 << 3)) ? " stallsent" : "",
152 (csr & (1 << 2)) ? " rxsetup" : "",
153 (csr & (1 << 1)) ? " rxdatabk0" : "",
154 (csr & (1 << 0)) ? " txcomp" : "");
155 if (list_empty (&ep->queue))
156 seq_printf(s, "\t(queue empty)\n");
157
158 else list_for_each_entry (req, &ep->queue, queue) {
159 unsigned length = req->req.actual;
160
161 seq_printf(s, "\treq %p len %d/%d buf %p\n",
162 &req->req, length,
163 req->req.length, req->req.buf);
164 }
165 local_irq_restore(flags);
166}
167
168static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
169{
170 int i;
171
172 seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
173 (mask & (1 << 13)) ? " wakeup" : "",
174 (mask & (1 << 12)) ? " endbusres" : "",
175
176 (mask & (1 << 11)) ? " sofint" : "",
177 (mask & (1 << 10)) ? " extrsm" : "",
178 (mask & (1 << 9)) ? " rxrsm" : "",
179 (mask & (1 << 8)) ? " rxsusp" : "");
180 for (i = 0; i < 8; i++) {
181 if (mask & (1 << i))
182 seq_printf(s, " ep%d", i);
183 }
184 seq_printf(s, "\n");
185}
186
187static int proc_udc_show(struct seq_file *s, void *unused)
188{
189 struct at91_udc *udc = s->private;
190 struct at91_ep *ep;
191 u32 tmp;
192
193 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
194
195 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
196 udc->vbus ? "present" : "off",
197 udc->enabled
198 ? (udc->vbus ? "active" : "enabled")
199 : "disabled",
200 udc->selfpowered ? "self" : "VBUS",
201 udc->suspended ? ", suspended" : "",
202 udc->driver ? udc->driver->driver.name : "(none)");
203
204 /* don't access registers when interface isn't clocked */
205 if (!udc->clocked) {
206 seq_printf(s, "(not clocked)\n");
207 return 0;
208 }
209
210 tmp = at91_udp_read(AT91_UDP_FRM_NUM);
211 seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
212 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
213 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
214 (tmp & AT91_UDP_NUM));
215
216 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
217 seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
218 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
219 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
220 (tmp & AT91_UDP_ESR) ? " esr" : "",
221 (tmp & AT91_UDP_CONFG) ? " confg" : "",
222 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
223
224 tmp = at91_udp_read(AT91_UDP_FADDR);
225 seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
226 (tmp & AT91_UDP_FEN) ? " fen" : "",
227 (tmp & AT91_UDP_FADD));
228
229 proc_irq_show(s, "imr ", at91_udp_read(AT91_UDP_IMR));
230 proc_irq_show(s, "isr ", at91_udp_read(AT91_UDP_ISR));
231
232 if (udc->enabled && udc->vbus) {
233 proc_ep_show(s, &udc->ep[0]);
234 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
235 if (ep->desc)
236 proc_ep_show(s, ep);
237 }
238 }
239 return 0;
240}
241
242static int proc_udc_open(struct inode *inode, struct file *file)
243{
244 return single_open(file, proc_udc_show, PDE(inode)->data);
245}
246
247static struct file_operations proc_ops = {
248 .open = proc_udc_open,
249 .read = seq_read,
250 .llseek = seq_lseek,
251 .release = single_release,
252};
253
254static void create_debug_file(struct at91_udc *udc)
255{
256 struct proc_dir_entry *pde;
257
258 pde = create_proc_entry (debug_filename, 0, NULL);
259 udc->pde = pde;
260 if (pde == NULL)
261 return;
262
263 pde->proc_fops = &proc_ops;
264 pde->data = udc;
265}
266
267static void remove_debug_file(struct at91_udc *udc)
268{
269 if (udc->pde)
270 remove_proc_entry(debug_filename, NULL);
271}
272
273#else
274
275static inline void create_debug_file(struct at91_udc *udc) {}
276static inline void remove_debug_file(struct at91_udc *udc) {}
277
278#endif
279
280
281/*-------------------------------------------------------------------------*/
282
283static void done(struct at91_ep *ep, struct at91_request *req, int status)
284{
285 unsigned stopped = ep->stopped;
286
287 list_del_init(&req->queue);
288 if (req->req.status == -EINPROGRESS)
289 req->req.status = status;
290 else
291 status = req->req.status;
292 if (status && status != -ESHUTDOWN)
293 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
294
295 ep->stopped = 1;
296 req->req.complete(&ep->ep, &req->req);
297 ep->stopped = stopped;
298
299 /* ep0 is always ready; other endpoints need a non-empty queue */
300 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
301 at91_udp_write(AT91_UDP_IDR, ep->int_mask);
302}
303
304/*-------------------------------------------------------------------------*/
305
306/* bits indicating OUT fifo has data ready */
307#define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
308
309/*
310 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
311 * back most of the value you just read (because of side effects, including
312 * bits that may change after reading and before writing).
313 *
314 * Except when changing a specific bit, always write values which:
315 * - clear SET_FX bits (setting them could change something)
316 * - set CLR_FX bits (clearing them could change something)
317 *
318 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
319 * that shouldn't normally be changed.
320 */
321#define SET_FX (AT91_UDP_TXPKTRDY)
322#define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
323
324/* pull OUT packet data from the endpoint's fifo */
325static int read_fifo (struct at91_ep *ep, struct at91_request *req)
326{
327 u32 __iomem *creg = ep->creg;
328 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
329 u32 csr;
330 u8 *buf;
331 unsigned int count, bufferspace, is_done;
332
333 buf = req->req.buf + req->req.actual;
334 bufferspace = req->req.length - req->req.actual;
335
336 /*
337 * there might be nothing to read if ep_queue() calls us,
338 * or if we already emptied both pingpong buffers
339 */
340rescan:
341 csr = __raw_readl(creg);
342 if ((csr & RX_DATA_READY) == 0)
343 return 0;
344
345 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
346 if (count > ep->ep.maxpacket)
347 count = ep->ep.maxpacket;
348 if (count > bufferspace) {
349 DBG("%s buffer overflow\n", ep->ep.name);
350 req->req.status = -EOVERFLOW;
351 count = bufferspace;
352 }
353 __raw_readsb(dreg, buf, count);
354
355 /* release and swap pingpong mem bank */
356 csr |= CLR_FX;
357 if (ep->is_pingpong) {
358 if (ep->fifo_bank == 0) {
359 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
360 ep->fifo_bank = 1;
361 } else {
362 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
363 ep->fifo_bank = 0;
364 }
365 } else
366 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
367 __raw_writel(csr, creg);
368
369 req->req.actual += count;
370 is_done = (count < ep->ep.maxpacket);
371 if (count == bufferspace)
372 is_done = 1;
373
374 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
375 is_done ? " (done)" : "");
376
377 /*
378 * avoid extra trips through IRQ logic for packets already in
379 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
380 */
381 if (is_done)
382 done(ep, req, 0);
383 else if (ep->is_pingpong) {
384 bufferspace -= count;
385 buf += count;
386 goto rescan;
387 }
388
389 return is_done;
390}
391
392/* load fifo for an IN packet */
393static int write_fifo(struct at91_ep *ep, struct at91_request *req)
394{
395 u32 __iomem *creg = ep->creg;
396 u32 csr = __raw_readl(creg);
397 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
398 unsigned total, count, is_last;
399
400 /*
401 * TODO: allow for writing two packets to the fifo ... that'll
402 * reduce the amount of IN-NAKing, but probably won't affect
403 * throughput much. (Unlike preventing OUT-NAKing!)
404 */
405
406 /*
407 * If ep_queue() calls us, the queue is empty and possibly in
408 * odd states like TXCOMP not yet cleared (we do it, saving at
409 * least one IRQ) or the fifo not yet being free. Those aren't
410 * issues normally (IRQ handler fast path).
411 */
412 if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
413 if (csr & AT91_UDP_TXCOMP) {
414 csr |= CLR_FX;
415 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
416 __raw_writel(csr, creg);
417 csr = __raw_readl(creg);
418 }
419 if (csr & AT91_UDP_TXPKTRDY)
420 return 0;
421 }
422
423 total = req->req.length - req->req.actual;
424 if (ep->ep.maxpacket < total) {
425 count = ep->ep.maxpacket;
426 is_last = 0;
427 } else {
428 count = total;
429 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
430 }
431
432 /*
433 * Write the packet, maybe it's a ZLP.
434 *
435 * NOTE: incrementing req->actual before we receive the ACK means
436 * gadget driver IN bytecounts can be wrong in fault cases. That's
437 * fixable with PIO drivers like this one (save "count" here, and
438 * do the increment later on TX irq), but not for most DMA hardware.
439 *
440 * So all gadget drivers must accept that potential error. Some
441 * hardware supports precise fifo status reporting, letting them
442 * recover when the actual bytecount matters (e.g. for USB Test
443 * and Measurement Class devices).
444 */
445 __raw_writesb(dreg, req->req.buf + req->req.actual, count);
446 csr &= ~SET_FX;
447 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
448 __raw_writel(csr, creg);
449 req->req.actual += count;
450
451 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
452 is_last ? " (done)" : "");
453 if (is_last)
454 done(ep, req, 0);
455 return is_last;
456}
457
458static void nuke(struct at91_ep *ep, int status)
459{
460 struct at91_request *req;
461
462 // terminer chaque requete dans la queue
463 ep->stopped = 1;
464 if (list_empty(&ep->queue))
465 return;
466
467 VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
468 while (!list_empty(&ep->queue)) {
469 req = list_entry(ep->queue.next, struct at91_request, queue);
470 done(ep, req, status);
471 }
472}
473
474/*-------------------------------------------------------------------------*/
475
476static int at91_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
477{
478 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
479 struct at91_udc *dev = ep->udc;
480 u16 maxpacket;
481 u32 tmp;
482 unsigned long flags;
483
484 if (!_ep || !ep
485 || !desc || ep->desc
486 || _ep->name == ep0name
487 || desc->bDescriptorType != USB_DT_ENDPOINT
488 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
489 || maxpacket > ep->maxpacket) {
490 DBG("bad ep or descriptor\n");
491 return -EINVAL;
492 }
493
494 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
495 DBG("bogus device state\n");
496 return -ESHUTDOWN;
497 }
498
499 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
500 switch (tmp) {
501 case USB_ENDPOINT_XFER_CONTROL:
502 DBG("only one control endpoint\n");
503 return -EINVAL;
504 case USB_ENDPOINT_XFER_INT:
505 if (maxpacket > 64)
506 goto bogus_max;
507 break;
508 case USB_ENDPOINT_XFER_BULK:
509 switch (maxpacket) {
510 case 8:
511 case 16:
512 case 32:
513 case 64:
514 goto ok;
515 }
516bogus_max:
517 DBG("bogus maxpacket %d\n", maxpacket);
518 return -EINVAL;
519 case USB_ENDPOINT_XFER_ISOC:
520 if (!ep->is_pingpong) {
521 DBG("iso requires double buffering\n");
522 return -EINVAL;
523 }
524 break;
525 }
526
527ok:
528 local_irq_save(flags);
529
530 /* initialize endpoint to match this descriptor */
531 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
532 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
533 ep->stopped = 0;
534 if (ep->is_in)
535 tmp |= 0x04;
536 tmp <<= 8;
537 tmp |= AT91_UDP_EPEDS;
538 __raw_writel(tmp, ep->creg);
539
540 ep->desc = desc;
541 ep->ep.maxpacket = maxpacket;
542
543 /*
544 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
545 * since endpoint resets don't reset hw pingpong state.
546 */
547 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
548 at91_udp_write(AT91_UDP_RST_EP, 0);
549
550 local_irq_restore(flags);
551 return 0;
552}
553
554static int at91_ep_disable (struct usb_ep * _ep)
555{
556 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
557 unsigned long flags;
558
559 if (ep == &ep->udc->ep[0])
560 return -EINVAL;
561
562 local_irq_save(flags);
563
564 nuke(ep, -ESHUTDOWN);
565
566 /* restore the endpoint's pristine config */
567 ep->desc = NULL;
568 ep->ep.maxpacket = ep->maxpacket;
569
570 /* reset fifos and endpoint */
571 if (ep->udc->clocked) {
572 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
573 at91_udp_write(AT91_UDP_RST_EP, 0);
574 __raw_writel(0, ep->creg);
575 }
576
577 local_irq_restore(flags);
578 return 0;
579}
580
581/*
582 * this is a PIO-only driver, so there's nothing
583 * interesting for request or buffer allocation.
584 */
585
586static struct usb_request *at91_ep_alloc_request (struct usb_ep *_ep, unsigned int gfp_flags)
587{
588 struct at91_request *req;
589
590 req = kcalloc(1, sizeof (struct at91_request), SLAB_KERNEL);
591 if (!req)
592 return NULL;
593
594 INIT_LIST_HEAD(&req->queue);
595 return &req->req;
596}
597
598static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
599{
600 struct at91_request *req;
601
602 req = container_of(_req, struct at91_request, req);
603 BUG_ON(!list_empty(&req->queue));
604 kfree(req);
605}
606
607static void *at91_ep_alloc_buffer(
608 struct usb_ep *_ep,
609 unsigned bytes,
610 dma_addr_t *dma,
611 gfp_t gfp_flags)
612{
613 *dma = ~0;
614 return kmalloc(bytes, gfp_flags);
615}
616
617static void at91_ep_free_buffer(
618 struct usb_ep *ep,
619 void *buf,
620 dma_addr_t dma,
621 unsigned bytes)
622{
623 kfree(buf);
624}
625
626static int at91_ep_queue(struct usb_ep *_ep,
627 struct usb_request *_req, gfp_t gfp_flags)
628{
629 struct at91_request *req;
630 struct at91_ep *ep;
631 struct at91_udc *dev;
632 int status;
633 unsigned long flags;
634
635 req = container_of(_req, struct at91_request, req);
636 ep = container_of(_ep, struct at91_ep, ep);
637
638 if (!_req || !_req->complete
639 || !_req->buf || !list_empty(&req->queue)) {
640 DBG("invalid request\n");
641 return -EINVAL;
642 }
643
644 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
645 DBG("invalid ep\n");
646 return -EINVAL;
647 }
648
649 dev = ep->udc;
650
651 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
652 DBG("invalid device\n");
653 return -EINVAL;
654 }
655
656 _req->status = -EINPROGRESS;
657 _req->actual = 0;
658
659 local_irq_save(flags);
660
661 /* try to kickstart any empty and idle queue */
662 if (list_empty(&ep->queue) && !ep->stopped) {
663 int is_ep0;
664
665 /*
666 * If this control request has a non-empty DATA stage, this
667 * will start that stage. It works just like a non-control
668 * request (until the status stage starts, maybe early).
669 *
670 * If the data stage is empty, then this starts a successful
671 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
672 */
673 is_ep0 = (ep->ep.name == ep0name);
674 if (is_ep0) {
675 u32 tmp;
676
677 if (!dev->req_pending) {
678 status = -EINVAL;
679 goto done;
680 }
681
682 /*
683 * defer changing CONFG until after the gadget driver
684 * reconfigures the endpoints.
685 */
686 if (dev->wait_for_config_ack) {
687 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
688 tmp ^= AT91_UDP_CONFG;
689 VDBG("toggle config\n");
690 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
691 }
692 if (req->req.length == 0) {
693ep0_in_status:
694 PACKET("ep0 in/status\n");
695 status = 0;
696 tmp = __raw_readl(ep->creg);
697 tmp &= ~SET_FX;
698 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
699 __raw_writel(tmp, ep->creg);
700 dev->req_pending = 0;
701 goto done;
702 }
703 }
704
705 if (ep->is_in)
706 status = write_fifo(ep, req);
707 else {
708 status = read_fifo(ep, req);
709
710 /* IN/STATUS stage is otherwise triggered by irq */
711 if (status && is_ep0)
712 goto ep0_in_status;
713 }
714 } else
715 status = 0;
716
717 if (req && !status) {
718 list_add_tail (&req->queue, &ep->queue);
719 at91_udp_write(AT91_UDP_IER, ep->int_mask);
720 }
721done:
722 local_irq_restore(flags);
723 return (status < 0) ? status : 0;
724}
725
726static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
727{
728 struct at91_ep *ep;
729 struct at91_request *req;
730
731 ep = container_of(_ep, struct at91_ep, ep);
732 if (!_ep || ep->ep.name == ep0name)
733 return -EINVAL;
734
735 /* make sure it's actually queued on this endpoint */
736 list_for_each_entry (req, &ep->queue, queue) {
737 if (&req->req == _req)
738 break;
739 }
740 if (&req->req != _req)
741 return -EINVAL;
742
743 done(ep, req, -ECONNRESET);
744 return 0;
745}
746
747static int at91_ep_set_halt(struct usb_ep *_ep, int value)
748{
749 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
750 u32 __iomem *creg;
751 u32 csr;
752 unsigned long flags;
753 int status = 0;
754
755 if (!_ep || ep->is_iso || !ep->udc->clocked)
756 return -EINVAL;
757
758 creg = ep->creg;
759 local_irq_save(flags);
760
761 csr = __raw_readl(creg);
762
763 /*
764 * fail with still-busy IN endpoints, ensuring correct sequencing
765 * of data tx then stall. note that the fifo rx bytecount isn't
766 * completely accurate as a tx bytecount.
767 */
768 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
769 status = -EAGAIN;
770 else {
771 csr |= CLR_FX;
772 csr &= ~SET_FX;
773 if (value) {
774 csr |= AT91_UDP_FORCESTALL;
775 VDBG("halt %s\n", ep->ep.name);
776 } else {
777 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
778 at91_udp_write(AT91_UDP_RST_EP, 0);
779 csr &= ~AT91_UDP_FORCESTALL;
780 }
781 __raw_writel(csr, creg);
782 }
783
784 local_irq_restore(flags);
785 return status;
786}
787
788static struct usb_ep_ops at91_ep_ops = {
789 .enable = at91_ep_enable,
790 .disable = at91_ep_disable,
791 .alloc_request = at91_ep_alloc_request,
792 .free_request = at91_ep_free_request,
793 .alloc_buffer = at91_ep_alloc_buffer,
794 .free_buffer = at91_ep_free_buffer,
795 .queue = at91_ep_queue,
796 .dequeue = at91_ep_dequeue,
797 .set_halt = at91_ep_set_halt,
798 // there's only imprecise fifo status reporting
799};
800
801/*-------------------------------------------------------------------------*/
802
803static int at91_get_frame(struct usb_gadget *gadget)
804{
805 if (!to_udc(gadget)->clocked)
806 return -EINVAL;
807 return at91_udp_read(AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
808}
809
810static int at91_wakeup(struct usb_gadget *gadget)
811{
812 struct at91_udc *udc = to_udc(gadget);
813 u32 glbstate;
814 int status = -EINVAL;
815 unsigned long flags;
816
817 DBG("%s\n", __FUNCTION__ );
818 local_irq_save(flags);
819
820 if (!udc->clocked || !udc->suspended)
821 goto done;
822
823 /* NOTE: some "early versions" handle ESR differently ... */
824
825 glbstate = at91_udp_read(AT91_UDP_GLB_STAT);
826 if (!(glbstate & AT91_UDP_ESR))
827 goto done;
828 glbstate |= AT91_UDP_ESR;
829 at91_udp_write(AT91_UDP_GLB_STAT, glbstate);
830
831done:
832 local_irq_restore(flags);
833 return status;
834}
835
836/* reinit == restore inital software state */
837static void udc_reinit(struct at91_udc *udc)
838{
839 u32 i;
840
841 INIT_LIST_HEAD(&udc->gadget.ep_list);
842 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
843
844 for (i = 0; i < NUM_ENDPOINTS; i++) {
845 struct at91_ep *ep = &udc->ep[i];
846
847 if (i != 0)
848 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
849 ep->desc = NULL;
850 ep->stopped = 0;
851 ep->fifo_bank = 0;
852 ep->ep.maxpacket = ep->maxpacket;
853 // initialiser une queue par endpoint
854 INIT_LIST_HEAD(&ep->queue);
855 }
856}
857
858static void stop_activity(struct at91_udc *udc)
859{
860 struct usb_gadget_driver *driver = udc->driver;
861 int i;
862
863 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
864 driver = NULL;
865 udc->gadget.speed = USB_SPEED_UNKNOWN;
866
867 for (i = 0; i < NUM_ENDPOINTS; i++) {
868 struct at91_ep *ep = &udc->ep[i];
869 ep->stopped = 1;
870 nuke(ep, -ESHUTDOWN);
871 }
872 if (driver)
873 driver->disconnect(&udc->gadget);
874
875 udc_reinit(udc);
876}
877
878static void clk_on(struct at91_udc *udc)
879{
880 if (udc->clocked)
881 return;
882 udc->clocked = 1;
883 clk_enable(udc->iclk);
884 clk_enable(udc->fclk);
885}
886
887static void clk_off(struct at91_udc *udc)
888{
889 if (!udc->clocked)
890 return;
891 udc->clocked = 0;
892 udc->gadget.speed = USB_SPEED_UNKNOWN;
893 clk_disable(udc->iclk);
894 clk_disable(udc->fclk);
895}
896
897/*
898 * activate/deactivate link with host; minimize power usage for
899 * inactive links by cutting clocks and transceiver power.
900 */
901static void pullup(struct at91_udc *udc, int is_on)
902{
903 if (!udc->enabled || !udc->vbus)
904 is_on = 0;
905 DBG("%sactive\n", is_on ? "" : "in");
906 if (is_on) {
907 clk_on(udc);
908 at91_udp_write(AT91_UDP_TXVC, 0);
909 at91_set_gpio_value(udc->board.pullup_pin, 1);
910 } else {
911 stop_activity(udc);
912 at91_udp_write(AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
913 at91_set_gpio_value(udc->board.pullup_pin, 0);
914 clk_off(udc);
915
916 // REVISIT: with transceiver disabled, will D- float
917 // so that a host would falsely detect a device?
918 }
919}
920
921/* vbus is here! turn everything on that's ready */
922static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
923{
924 struct at91_udc *udc = to_udc(gadget);
925 unsigned long flags;
926
927 // VDBG("vbus %s\n", is_active ? "on" : "off");
928 local_irq_save(flags);
929 udc->vbus = (is_active != 0);
930 pullup(udc, is_active);
931 local_irq_restore(flags);
932 return 0;
933}
934
935static int at91_pullup(struct usb_gadget *gadget, int is_on)
936{
937 struct at91_udc *udc = to_udc(gadget);
938 unsigned long flags;
939
940 local_irq_save(flags);
941 udc->enabled = is_on = !!is_on;
942 pullup(udc, is_on);
943 local_irq_restore(flags);
944 return 0;
945}
946
947static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
948{
949 struct at91_udc *udc = to_udc(gadget);
950 unsigned long flags;
951
952 local_irq_save(flags);
953 udc->selfpowered = (is_on != 0);
954 local_irq_restore(flags);
955 return 0;
956}
957
958static const struct usb_gadget_ops at91_udc_ops = {
959 .get_frame = at91_get_frame,
960 .wakeup = at91_wakeup,
961 .set_selfpowered = at91_set_selfpowered,
962 .vbus_session = at91_vbus_session,
963 .pullup = at91_pullup,
964
965 /*
966 * VBUS-powered devices may also also want to support bigger
967 * power budgets after an appropriate SET_CONFIGURATION.
968 */
969 // .vbus_power = at91_vbus_power,
970};
971
972/*-------------------------------------------------------------------------*/
973
974static int handle_ep(struct at91_ep *ep)
975{
976 struct at91_request *req;
977 u32 __iomem *creg = ep->creg;
978 u32 csr = __raw_readl(creg);
979
980 if (!list_empty(&ep->queue))
981 req = list_entry(ep->queue.next,
982 struct at91_request, queue);
983 else
984 req = NULL;
985
986 if (ep->is_in) {
987 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
988 csr |= CLR_FX;
989 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
990 __raw_writel(csr, creg);
991 }
992 if (req)
993 return write_fifo(ep, req);
994
995 } else {
996 if (csr & AT91_UDP_STALLSENT) {
997 /* STALLSENT bit == ISOERR */
998 if (ep->is_iso && req)
999 req->req.status = -EILSEQ;
1000 csr |= CLR_FX;
1001 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1002 __raw_writel(csr, creg);
1003 csr = __raw_readl(creg);
1004 }
1005 if (req && (csr & RX_DATA_READY))
1006 return read_fifo(ep, req);
1007 }
1008 return 0;
1009}
1010
1011union setup {
1012 u8 raw[8];
1013 struct usb_ctrlrequest r;
1014};
1015
1016static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1017{
1018 u32 __iomem *creg = ep->creg;
1019 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1020 unsigned rxcount, i = 0;
1021 u32 tmp;
1022 union setup pkt;
1023 int status = 0;
1024
1025 /* read and ack SETUP; hard-fail for bogus packets */
1026 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1027 if (likely(rxcount == 8)) {
1028 while (rxcount--)
1029 pkt.raw[i++] = __raw_readb(dreg);
1030 if (pkt.r.bRequestType & USB_DIR_IN) {
1031 csr |= AT91_UDP_DIR;
1032 ep->is_in = 1;
1033 } else {
1034 csr &= ~AT91_UDP_DIR;
1035 ep->is_in = 0;
1036 }
1037 } else {
1038 // REVISIT this happens sometimes under load; why??
1039 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1040 status = -EINVAL;
1041 }
1042 csr |= CLR_FX;
1043 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1044 __raw_writel(csr, creg);
1045 udc->wait_for_addr_ack = 0;
1046 udc->wait_for_config_ack = 0;
1047 ep->stopped = 0;
1048 if (unlikely(status != 0))
1049 goto stall;
1050
1051#define w_index le16_to_cpu(pkt.r.wIndex)
1052#define w_value le16_to_cpu(pkt.r.wValue)
1053#define w_length le16_to_cpu(pkt.r.wLength)
1054
1055 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1056 pkt.r.bRequestType, pkt.r.bRequest,
1057 w_value, w_index, w_length);
1058
1059 /*
1060 * A few standard requests get handled here, ones that touch
1061 * hardware ... notably for device and endpoint features.
1062 */
1063 udc->req_pending = 1;
1064 csr = __raw_readl(creg);
1065 csr |= CLR_FX;
1066 csr &= ~SET_FX;
1067 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1068
1069 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1070 | USB_REQ_SET_ADDRESS:
1071 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1072 udc->addr = w_value;
1073 udc->wait_for_addr_ack = 1;
1074 udc->req_pending = 0;
1075 /* FADDR is set later, when we ack host STATUS */
1076 return;
1077
1078 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1079 | USB_REQ_SET_CONFIGURATION:
1080 tmp = at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1081 if (pkt.r.wValue)
1082 udc->wait_for_config_ack = (tmp == 0);
1083 else
1084 udc->wait_for_config_ack = (tmp != 0);
1085 if (udc->wait_for_config_ack)
1086 VDBG("wait for config\n");
1087 /* CONFG is toggled later, if gadget driver succeeds */
1088 break;
1089
1090 /*
1091 * Hosts may set or clear remote wakeup status, and
1092 * devices may report they're VBUS powered.
1093 */
1094 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1095 | USB_REQ_GET_STATUS:
1096 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1097 if (at91_udp_read(AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1098 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1099 PACKET("get device status\n");
1100 __raw_writeb(tmp, dreg);
1101 __raw_writeb(0, dreg);
1102 goto write_in;
1103 /* then STATUS starts later, automatically */
1104 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1105 | USB_REQ_SET_FEATURE:
1106 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1107 goto stall;
1108 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1109 tmp |= AT91_UDP_ESR;
1110 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1111 goto succeed;
1112 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1113 | USB_REQ_CLEAR_FEATURE:
1114 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1115 goto stall;
1116 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1117 tmp &= ~AT91_UDP_ESR;
1118 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1119 goto succeed;
1120
1121 /*
1122 * Interfaces have no feature settings; this is pretty useless.
1123 * we won't even insist the interface exists...
1124 */
1125 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1126 | USB_REQ_GET_STATUS:
1127 PACKET("get interface status\n");
1128 __raw_writeb(0, dreg);
1129 __raw_writeb(0, dreg);
1130 goto write_in;
1131 /* then STATUS starts later, automatically */
1132 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1133 | USB_REQ_SET_FEATURE:
1134 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1135 | USB_REQ_CLEAR_FEATURE:
1136 goto stall;
1137
1138 /*
1139 * Hosts may clear bulk/intr endpoint halt after the gadget
1140 * driver sets it (not widely used); or set it (for testing)
1141 */
1142 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1143 | USB_REQ_GET_STATUS:
1144 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1145 ep = &udc->ep[tmp];
1146 if (tmp > NUM_ENDPOINTS || (tmp && !ep->desc))
1147 goto stall;
1148
1149 if (tmp) {
1150 if ((w_index & USB_DIR_IN)) {
1151 if (!ep->is_in)
1152 goto stall;
1153 } else if (ep->is_in)
1154 goto stall;
1155 }
1156 PACKET("get %s status\n", ep->ep.name);
1157 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1158 tmp = (1 << USB_ENDPOINT_HALT);
1159 else
1160 tmp = 0;
1161 __raw_writeb(tmp, dreg);
1162 __raw_writeb(0, dreg);
1163 goto write_in;
1164 /* then STATUS starts later, automatically */
1165 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1166 | USB_REQ_SET_FEATURE:
1167 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1168 ep = &udc->ep[tmp];
1169 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1170 goto stall;
1171 if (!ep->desc || ep->is_iso)
1172 goto stall;
1173 if ((w_index & USB_DIR_IN)) {
1174 if (!ep->is_in)
1175 goto stall;
1176 } else if (ep->is_in)
1177 goto stall;
1178
1179 tmp = __raw_readl(ep->creg);
1180 tmp &= ~SET_FX;
1181 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1182 __raw_writel(tmp, ep->creg);
1183 goto succeed;
1184 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1185 | USB_REQ_CLEAR_FEATURE:
1186 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1187 ep = &udc->ep[tmp];
1188 if (w_value != USB_ENDPOINT_HALT || tmp > NUM_ENDPOINTS)
1189 goto stall;
1190 if (tmp == 0)
1191 goto succeed;
1192 if (!ep->desc || ep->is_iso)
1193 goto stall;
1194 if ((w_index & USB_DIR_IN)) {
1195 if (!ep->is_in)
1196 goto stall;
1197 } else if (ep->is_in)
1198 goto stall;
1199
1200 at91_udp_write(AT91_UDP_RST_EP, ep->int_mask);
1201 at91_udp_write(AT91_UDP_RST_EP, 0);
1202 tmp = __raw_readl(ep->creg);
1203 tmp |= CLR_FX;
1204 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1205 __raw_writel(tmp, ep->creg);
1206 if (!list_empty(&ep->queue))
1207 handle_ep(ep);
1208 goto succeed;
1209 }
1210
1211#undef w_value
1212#undef w_index
1213#undef w_length
1214
1215 /* pass request up to the gadget driver */
1216 status = udc->driver->setup(&udc->gadget, &pkt.r);
1217 if (status < 0) {
1218stall:
1219 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1220 pkt.r.bRequestType, pkt.r.bRequest, status);
1221 csr |= AT91_UDP_FORCESTALL;
1222 __raw_writel(csr, creg);
1223 udc->req_pending = 0;
1224 }
1225 return;
1226
1227succeed:
1228 /* immediate successful (IN) STATUS after zero length DATA */
1229 PACKET("ep0 in/status\n");
1230write_in:
1231 csr |= AT91_UDP_TXPKTRDY;
1232 __raw_writel(csr, creg);
1233 udc->req_pending = 0;
1234 return;
1235}
1236
1237static void handle_ep0(struct at91_udc *udc)
1238{
1239 struct at91_ep *ep0 = &udc->ep[0];
1240 u32 __iomem *creg = ep0->creg;
1241 u32 csr = __raw_readl(creg);
1242 struct at91_request *req;
1243
1244 if (unlikely(csr & AT91_UDP_STALLSENT)) {
1245 nuke(ep0, -EPROTO);
1246 udc->req_pending = 0;
1247 csr |= CLR_FX;
1248 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1249 __raw_writel(csr, creg);
1250 VDBG("ep0 stalled\n");
1251 csr = __raw_readl(creg);
1252 }
1253 if (csr & AT91_UDP_RXSETUP) {
1254 nuke(ep0, 0);
1255 udc->req_pending = 0;
1256 handle_setup(udc, ep0, csr);
1257 return;
1258 }
1259
1260 if (list_empty(&ep0->queue))
1261 req = NULL;
1262 else
1263 req = list_entry(ep0->queue.next, struct at91_request, queue);
1264
1265 /* host ACKed an IN packet that we sent */
1266 if (csr & AT91_UDP_TXCOMP) {
1267 csr |= CLR_FX;
1268 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1269
1270 /* write more IN DATA? */
1271 if (req && ep0->is_in) {
1272 if (handle_ep(ep0))
1273 udc->req_pending = 0;
1274
1275 /*
1276 * Ack after:
1277 * - last IN DATA packet (including GET_STATUS)
1278 * - IN/STATUS for OUT DATA
1279 * - IN/STATUS for any zero-length DATA stage
1280 * except for the IN DATA case, the host should send
1281 * an OUT status later, which we'll ack.
1282 */
1283 } else {
1284 udc->req_pending = 0;
1285 __raw_writel(csr, creg);
1286
1287 /*
1288 * SET_ADDRESS takes effect only after the STATUS
1289 * (to the original address) gets acked.
1290 */
1291 if (udc->wait_for_addr_ack) {
1292 u32 tmp;
1293
1294 at91_udp_write(AT91_UDP_FADDR, AT91_UDP_FEN | udc->addr);
1295 tmp = at91_udp_read(AT91_UDP_GLB_STAT);
1296 tmp &= ~AT91_UDP_FADDEN;
1297 if (udc->addr)
1298 tmp |= AT91_UDP_FADDEN;
1299 at91_udp_write(AT91_UDP_GLB_STAT, tmp);
1300
1301 udc->wait_for_addr_ack = 0;
1302 VDBG("address %d\n", udc->addr);
1303 }
1304 }
1305 }
1306
1307 /* OUT packet arrived ... */
1308 else if (csr & AT91_UDP_RX_DATA_BK0) {
1309 csr |= CLR_FX;
1310 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1311
1312 /* OUT DATA stage */
1313 if (!ep0->is_in) {
1314 if (req) {
1315 if (handle_ep(ep0)) {
1316 /* send IN/STATUS */
1317 PACKET("ep0 in/status\n");
1318 csr = __raw_readl(creg);
1319 csr &= ~SET_FX;
1320 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1321 __raw_writel(csr, creg);
1322 udc->req_pending = 0;
1323 }
1324 } else if (udc->req_pending) {
1325 /*
1326 * AT91 hardware has a hard time with this
1327 * "deferred response" mode for control-OUT
1328 * transfers. (For control-IN it's fine.)
1329 *
1330 * The normal solution leaves OUT data in the
1331 * fifo until the gadget driver is ready.
1332 * We couldn't do that here without disabling
1333 * the IRQ that tells about SETUP packets,
1334 * e.g. when the host gets impatient...
1335 *
1336 * Working around it by copying into a buffer
1337 * would almost be a non-deferred response,
1338 * except that it wouldn't permit reliable
1339 * stalling of the request. Instead, demand
1340 * that gadget drivers not use this mode.
1341 */
1342 DBG("no control-OUT deferred responses!\n");
1343 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1344 udc->req_pending = 0;
1345 }
1346
1347 /* STATUS stage for control-IN; ack. */
1348 } else {
1349 PACKET("ep0 out/status ACK\n");
1350 __raw_writel(csr, creg);
1351
1352 /* "early" status stage */
1353 if (req)
1354 done(ep0, req, 0);
1355 }
1356 }
1357}
1358
1359static irqreturn_t at91_udc_irq (int irq, void *_udc, struct pt_regs *r)
1360{
1361 struct at91_udc *udc = _udc;
1362 u32 rescans = 5;
1363
1364 while (rescans--) {
1365 u32 status = at91_udp_read(AT91_UDP_ISR);
1366
1367 status &= at91_udp_read(AT91_UDP_IMR);
1368 if (!status)
1369 break;
1370
1371 /* USB reset irq: not maskable */
1372 if (status & AT91_UDP_ENDBUSRES) {
1373 at91_udp_write(AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1374 at91_udp_write(AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1375 /* Atmel code clears this irq twice */
1376 at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1377 at91_udp_write(AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1378 VDBG("end bus reset\n");
1379 udc->addr = 0;
1380 stop_activity(udc);
1381
1382 /* enable ep0 */
1383 at91_udp_write(AT91_UDP_CSR(0), AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1384 udc->gadget.speed = USB_SPEED_FULL;
1385 udc->suspended = 0;
1386 at91_udp_write(AT91_UDP_IER, AT91_UDP_EP(0));
1387
1388 /*
1389 * NOTE: this driver keeps clocks off unless the
1390 * USB host is present. That saves power, and also
1391 * eliminates IRQs (reset, resume, suspend) that can
1392 * otherwise flood from the controller. If your
1393 * board doesn't support VBUS detection, suspend and
1394 * resume irq logic may need more attention...
1395 */
1396
1397 /* host initiated suspend (3+ms bus idle) */
1398 } else if (status & AT91_UDP_RXSUSP) {
1399 at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXSUSP);
1400 at91_udp_write(AT91_UDP_IER, AT91_UDP_RXRSM);
1401 at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXSUSP);
1402 // VDBG("bus suspend\n");
1403 if (udc->suspended)
1404 continue;
1405 udc->suspended = 1;
1406
1407 /*
1408 * NOTE: when suspending a VBUS-powered device, the
1409 * gadget driver should switch into slow clock mode
1410 * and then into standby to avoid drawing more than
1411 * 500uA power (2500uA for some high-power configs).
1412 */
1413 if (udc->driver && udc->driver->suspend)
1414 udc->driver->suspend(&udc->gadget);
1415
1416 /* host initiated resume */
1417 } else if (status & AT91_UDP_RXRSM) {
1418 at91_udp_write(AT91_UDP_IDR, AT91_UDP_RXRSM);
1419 at91_udp_write(AT91_UDP_IER, AT91_UDP_RXSUSP);
1420 at91_udp_write(AT91_UDP_ICR, AT91_UDP_RXRSM);
1421 // VDBG("bus resume\n");
1422 if (!udc->suspended)
1423 continue;
1424 udc->suspended = 0;
1425
1426 /*
1427 * NOTE: for a VBUS-powered device, the gadget driver
1428 * would normally want to switch out of slow clock
1429 * mode into normal mode.
1430 */
1431 if (udc->driver && udc->driver->resume)
1432 udc->driver->resume(&udc->gadget);
1433
1434 /* endpoint IRQs are cleared by handling them */
1435 } else {
1436 int i;
1437 unsigned mask = 1;
1438 struct at91_ep *ep = &udc->ep[1];
1439
1440 if (status & mask)
1441 handle_ep0(udc);
1442 for (i = 1; i < NUM_ENDPOINTS; i++) {
1443 mask <<= 1;
1444 if (status & mask)
1445 handle_ep(ep);
1446 ep++;
1447 }
1448 }
1449 }
1450
1451 return IRQ_HANDLED;
1452}
1453
1454/*-------------------------------------------------------------------------*/
1455
1456static struct at91_udc controller = {
1457 .gadget = {
1458 .ops = &at91_udc_ops,
1459 .ep0 = &controller.ep[0].ep,
1460 .name = driver_name,
1461 .dev = {
1462 .bus_id = "gadget"
1463 }
1464 },
1465 .ep[0] = {
1466 .ep = {
1467 .name = ep0name,
1468 .ops = &at91_ep_ops,
1469 },
1470 .udc = &controller,
1471 .maxpacket = 8,
1472 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(0)),
1473 .int_mask = 1 << 0,
1474 },
1475 .ep[1] = {
1476 .ep = {
1477 .name = "ep1",
1478 .ops = &at91_ep_ops,
1479 },
1480 .udc = &controller,
1481 .is_pingpong = 1,
1482 .maxpacket = 64,
1483 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(1)),
1484 .int_mask = 1 << 1,
1485 },
1486 .ep[2] = {
1487 .ep = {
1488 .name = "ep2",
1489 .ops = &at91_ep_ops,
1490 },
1491 .udc = &controller,
1492 .is_pingpong = 1,
1493 .maxpacket = 64,
1494 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(2)),
1495 .int_mask = 1 << 2,
1496 },
1497 .ep[3] = {
1498 .ep = {
1499 /* could actually do bulk too */
1500 .name = "ep3-int",
1501 .ops = &at91_ep_ops,
1502 },
1503 .udc = &controller,
1504 .maxpacket = 8,
1505 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(3)),
1506 .int_mask = 1 << 3,
1507 },
1508 .ep[4] = {
1509 .ep = {
1510 .name = "ep4",
1511 .ops = &at91_ep_ops,
1512 },
1513 .udc = &controller,
1514 .is_pingpong = 1,
1515 .maxpacket = 256,
1516 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(4)),
1517 .int_mask = 1 << 4,
1518 },
1519 .ep[5] = {
1520 .ep = {
1521 .name = "ep5",
1522 .ops = &at91_ep_ops,
1523 },
1524 .udc = &controller,
1525 .is_pingpong = 1,
1526 .maxpacket = 256,
1527 .creg = (void __iomem *)(AT91_VA_BASE_UDP + AT91_UDP_CSR(5)),
1528 .int_mask = 1 << 5,
1529 },
1530 /* ep6 and ep7 are also reserved */
1531};
1532
1533static irqreturn_t at91_vbus_irq(int irq, void *_udc, struct pt_regs *r)
1534{
1535 struct at91_udc *udc = _udc;
1536 unsigned value;
1537
1538 /* vbus needs at least brief debouncing */
1539 udelay(10);
1540 value = at91_get_gpio_value(udc->board.vbus_pin);
1541 if (value != udc->vbus)
1542 at91_vbus_session(&udc->gadget, value);
1543
1544 return IRQ_HANDLED;
1545}
1546
1547int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1548{
1549 struct at91_udc *udc = &controller;
1550 int retval;
1551
1552 if (!driver
1553 || driver->speed != USB_SPEED_FULL
1554 || !driver->bind
1555 || !driver->unbind
1556 || !driver->setup) {
1557 DBG("bad parameter.\n");
1558 return -EINVAL;
1559 }
1560
1561 if (udc->driver) {
1562 DBG("UDC already has a gadget driver\n");
1563 return -EBUSY;
1564 }
1565
1566 udc->driver = driver;
1567 udc->gadget.dev.driver = &driver->driver;
1568 udc->gadget.dev.driver_data = &driver->driver;
1569 udc->enabled = 1;
1570 udc->selfpowered = 1;
1571
1572 retval = driver->bind(&udc->gadget);
1573 if (retval) {
1574 DBG("driver->bind() returned %d\n", retval);
1575 udc->driver = NULL;
1576 return retval;
1577 }
1578
1579 local_irq_disable();
1580 pullup(udc, 1);
1581 local_irq_enable();
1582
1583 DBG("bound to %s\n", driver->driver.name);
1584 return 0;
1585}
1586EXPORT_SYMBOL (usb_gadget_register_driver);
1587
1588int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1589{
1590 struct at91_udc *udc = &controller;
1591
1592 if (!driver || driver != udc->driver)
1593 return -EINVAL;
1594
1595 local_irq_disable();
1596 udc->enabled = 0;
1597 pullup(udc, 0);
1598 local_irq_enable();
1599
1600 driver->unbind(&udc->gadget);
1601 udc->driver = NULL;
1602
1603 DBG("unbound from %s\n", driver->driver.name);
1604 return 0;
1605}
1606EXPORT_SYMBOL (usb_gadget_unregister_driver);
1607
1608/*-------------------------------------------------------------------------*/
1609
1610static void at91udc_shutdown(struct platform_device *dev)
1611{
1612 /* force disconnect on reboot */
1613 pullup(platform_get_drvdata(dev), 0);
1614}
1615
1616static int __devinit at91udc_probe(struct platform_device *pdev)
1617{
1618 struct device *dev = &pdev->dev;
1619 struct at91_udc *udc;
1620 int retval;
1621
1622 if (!dev->platform_data) {
1623 /* small (so we copy it) but critical! */
1624 DBG("missing platform_data\n");
1625 return -ENODEV;
1626 }
1627
1628 if (!request_mem_region(AT91_BASE_UDP, SZ_16K, driver_name)) {
1629 DBG("someone's using UDC memory\n");
1630 return -EBUSY;
1631 }
1632
1633 /* init software state */
1634 udc = &controller;
1635 udc->gadget.dev.parent = dev;
1636 udc->board = *(struct at91_udc_data *) dev->platform_data;
1637 udc->pdev = pdev;
1638 udc_reinit(udc);
1639 udc->enabled = 0;
1640
1641 /* get interface and function clocks */
1642 udc->iclk = clk_get(dev, "udc_clk");
1643 udc->fclk = clk_get(dev, "udpck");
1644 if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1645 DBG("clocks missing\n");
1646 return -ENODEV;
1647 }
1648
1649 retval = device_register(&udc->gadget.dev);
1650 if (retval < 0)
1651 goto fail0;
1652
1653 /* disable everything until there's a gadget driver and vbus */
1654 pullup(udc, 0);
1655
1656 /* request UDC and maybe VBUS irqs */
1657 if (request_irq(AT91_ID_UDP, at91_udc_irq, SA_INTERRUPT, driver_name, udc)) {
1658 DBG("request irq %d failed\n", AT91_ID_UDP);
1659 retval = -EBUSY;
1660 goto fail1;
1661 }
1662 if (udc->board.vbus_pin > 0) {
1663 if (request_irq(udc->board.vbus_pin, at91_vbus_irq, SA_INTERRUPT, driver_name, udc)) {
1664 DBG("request vbus irq %d failed\n", udc->board.vbus_pin);
1665 free_irq(AT91_ID_UDP, udc);
1666 retval = -EBUSY;
1667 goto fail1;
1668 }
1669 } else {
1670 DBG("no VBUS detection, assuming always-on\n");
1671 udc->vbus = 1;
1672 }
1673 dev_set_drvdata(dev, udc);
1674 create_debug_file(udc);
1675
1676 INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1677 return 0;
1678
1679fail1:
1680 device_unregister(&udc->gadget.dev);
1681fail0:
1682 release_mem_region(AT91_VA_BASE_UDP, SZ_16K);
1683 DBG("%s probe failed, %d\n", driver_name, retval);
1684 return retval;
1685}
1686
1687static int __devexit at91udc_remove(struct platform_device *dev)
1688{
1689 struct at91_udc *udc = platform_get_drvdata(dev);
1690
1691 DBG("remove\n");
1692
1693 pullup(udc, 0);
1694
1695 if (udc->driver != 0)
1696 usb_gadget_unregister_driver(udc->driver);
1697
1698 remove_debug_file(udc);
1699 if (udc->board.vbus_pin > 0)
1700 free_irq(udc->board.vbus_pin, udc);
1701 free_irq(AT91_ID_UDP, udc);
1702 device_unregister(&udc->gadget.dev);
1703 release_mem_region(AT91_BASE_UDP, SZ_16K);
1704
1705 clk_put(udc->iclk);
1706 clk_put(udc->fclk);
1707
1708 return 0;
1709}
1710
1711#ifdef CONFIG_PM
1712static int at91udc_suspend(struct platform_device *dev, u32 state, u32 level)
1713{
1714 struct at91_udc *udc = platform_get_drvdata(dev);
1715
1716 /*
1717 * The "safe" suspend transitions are opportunistic ... e.g. when
1718 * the USB link is suspended (48MHz clock autogated off), or when
1719 * it's disconnected (programmatically gated off, elsewhere).
1720 * Then we can suspend, and the chip can enter slow clock mode.
1721 *
1722 * The problem case is some component (user mode?) suspending this
1723 * device while it's active, with the 48 MHz clock in use. There
1724 * are two basic approaches: (a) veto suspend levels involving slow
1725 * clock mode, (b) disconnect, so 48 MHz will no longer be in use
1726 * and we can enter slow clock mode. This uses (b) for now, since
1727 * it's simplest until AT91 PM exists and supports the other option.
1728 */
1729 if (udc->vbus && !udc->suspended)
1730 pullup(udc, 0);
1731 return 0;
1732}
1733
1734static int at91udc_resume(struct platform_device *dev, u32 level)
1735{
1736 struct at91_udc *udc = platform_get_drvdata(dev);
1737
1738 /* maybe reconnect to host; if so, clocks on */
1739 pullup(udc, 1);
1740 return 0;
1741}
1742#else
1743#define at91udc_suspend NULL
1744#define at91udc_resume NULL
1745#endif
1746
1747static struct platform_driver at91_udc = {
1748 .probe = at91udc_probe,
1749 .remove = __devexit_p(at91udc_remove),
1750 .shutdown = at91udc_shutdown,
1751 .suspend = at91udc_suspend,
1752 .resume = at91udc_resume,
1753 .driver = {
1754 .name = (char *) driver_name,
1755 .owner = THIS_MODULE,
1756 },
1757};
1758
1759static int __devinit udc_init_module(void)
1760{
1761 return platform_driver_register(&at91_udc);
1762}
1763module_init(udc_init_module);
1764
1765static void __devexit udc_exit_module(void)
1766{
1767 platform_driver_unregister(&at91_udc);
1768}
1769module_exit(udc_exit_module);
1770
1771MODULE_DESCRIPTION("AT91RM9200 udc driver");
1772MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1773MODULE_LICENSE("GPL");
diff --git a/drivers/usb/gadget/at91_udc.h b/drivers/usb/gadget/at91_udc.h
new file mode 100644
index 000000000000..5a4799cedd19
--- /dev/null
+++ b/drivers/usb/gadget/at91_udc.h
@@ -0,0 +1,181 @@
1/*
2 * Copyright (C) 2004 by Thomas Rathbone, HP Labs
3 * Copyright (C) 2005 by Ivan Kokshaysky
4 * Copyright (C) 2006 by SAN People
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#ifndef AT91_UDC_H
23#define AT91_UDC_H
24
25/*
26 * USB Device Port (UDP) registers.
27 * Based on AT91RM9200 datasheet revision E.
28 */
29
30#define AT91_UDP_FRM_NUM 0x00 /* Frame Number Register */
31#define AT91_UDP_NUM (0x7ff << 0) /* Frame Number */
32#define AT91_UDP_FRM_ERR (1 << 16) /* Frame Error */
33#define AT91_UDP_FRM_OK (1 << 17) /* Frame OK */
34
35#define AT91_UDP_GLB_STAT 0x04 /* Global State Register */
36#define AT91_UDP_FADDEN (1 << 0) /* Function Address Enable */
37#define AT91_UDP_CONFG (1 << 1) /* Configured */
38#define AT91_UDP_ESR (1 << 2) /* Enable Send Resume */
39#define AT91_UDP_RSMINPR (1 << 3) /* Resume has been sent */
40#define AT91_UDP_RMWUPE (1 << 4) /* Remote Wake Up Enable */
41
42#define AT91_UDP_FADDR 0x08 /* Function Address Register */
43#define AT91_UDP_FADD (0x7f << 0) /* Function Address Value */
44#define AT91_UDP_FEN (1 << 8) /* Function Enable */
45
46#define AT91_UDP_IER 0x10 /* Interrupt Enable Register */
47#define AT91_UDP_IDR 0x14 /* Interrupt Disable Register */
48#define AT91_UDP_IMR 0x18 /* Interrupt Mask Register */
49
50#define AT91_UDP_ISR 0x1c /* Interrupt Status Register */
51#define AT91_UDP_EP(n) (1 << (n)) /* Endpoint Interrupt Status */
52#define AT91_UDP_RXSUSP (1 << 8) /* USB Suspend Interrupt Status */
53#define AT91_UDP_RXRSM (1 << 9) /* USB Resume Interrupt Status */
54#define AT91_UDP_EXTRSM (1 << 10) /* External Resume Interrupt Status */
55#define AT91_UDP_SOFINT (1 << 11) /* Start of Frame Interrupt Status */
56#define AT91_UDP_ENDBUSRES (1 << 12) /* End of Bus Reset Interrpt Status */
57#define AT91_UDP_WAKEUP (1 << 13) /* USB Wakeup Interrupt Status */
58
59#define AT91_UDP_ICR 0x20 /* Interrupt Clear Register */
60#define AT91_UDP_RST_EP 0x28 /* Reset Endpoint Register */
61
62#define AT91_UDP_CSR(n) (0x30+((n)*4)) /* Endpoint Control/Status Registers 0-7 */
63#define AT91_UDP_TXCOMP (1 << 0) /* Generates IN packet with data previously written in DPR */
64#define AT91_UDP_RX_DATA_BK0 (1 << 1) /* Receive Data Bank 0 */
65#define AT91_UDP_RXSETUP (1 << 2) /* Send STALL to the host */
66#define AT91_UDP_STALLSENT (1 << 3) /* Stall Sent / Isochronous error (Isochronous endpoints) */
67#define AT91_UDP_TXPKTRDY (1 << 4) /* Transmit Packet Ready */
68#define AT91_UDP_FORCESTALL (1 << 5) /* Force Stall */
69#define AT91_UDP_RX_DATA_BK1 (1 << 6) /* Receive Data Bank 1 */
70#define AT91_UDP_DIR (1 << 7) /* Transfer Direction */
71#define AT91_UDP_EPTYPE (7 << 8) /* Endpoint Type */
72#define AT91_UDP_EPTYPE_CTRL (0 << 8)
73#define AT91_UDP_EPTYPE_ISO_OUT (1 << 8)
74#define AT91_UDP_EPTYPE_BULK_OUT (2 << 8)
75#define AT91_UDP_EPTYPE_INT_OUT (3 << 8)
76#define AT91_UDP_EPTYPE_ISO_IN (5 << 8)
77#define AT91_UDP_EPTYPE_BULK_IN (6 << 8)
78#define AT91_UDP_EPTYPE_INT_IN (7 << 8)
79#define AT91_UDP_DTGLE (1 << 11) /* Data Toggle */
80#define AT91_UDP_EPEDS (1 << 15) /* Endpoint Enable/Disable */
81#define AT91_UDP_RXBYTECNT (0x7ff << 16) /* Number of bytes in FIFO */
82
83#define AT91_UDP_FDR(n) (0x50+((n)*4)) /* Endpoint FIFO Data Registers 0-7 */
84
85#define AT91_UDP_TXVC 0x74 /* Transceiver Control Register */
86#define AT91_UDP_TXVC_TXVDIS (1 << 8) /* Transceiver Disable */
87
88
89/*-------------------------------------------------------------------------*/
90
91/*
92 * controller driver data structures
93 */
94
95#define NUM_ENDPOINTS 6
96
97/*
98 * hardware won't disable bus reset, or resume while the controller
99 * is suspended ... watching suspend helps keep the logic symmetric.
100 */
101#define MINIMUS_INTERRUPTUS \
102 (AT91_UDP_ENDBUSRES | AT91_UDP_RXRSM | AT91_UDP_RXSUSP)
103
104struct at91_ep {
105 struct usb_ep ep;
106 struct list_head queue;
107 struct at91_udc *udc;
108 void __iomem *creg;
109
110 unsigned maxpacket:16;
111 u8 int_mask;
112 unsigned is_pingpong:1;
113
114 unsigned stopped:1;
115 unsigned is_in:1;
116 unsigned is_iso:1;
117 unsigned fifo_bank:1;
118
119 const struct usb_endpoint_descriptor
120 *desc;
121};
122
123/*
124 * driver is non-SMP, and just blocks IRQs whenever it needs
125 * access protection for chip registers or driver state
126 */
127struct at91_udc {
128 struct usb_gadget gadget;
129 struct at91_ep ep[NUM_ENDPOINTS];
130 struct usb_gadget_driver *driver;
131 unsigned vbus:1;
132 unsigned enabled:1;
133 unsigned clocked:1;
134 unsigned suspended:1;
135 unsigned req_pending:1;
136 unsigned wait_for_addr_ack:1;
137 unsigned wait_for_config_ack:1;
138 unsigned selfpowered:1;
139 u8 addr;
140 struct at91_udc_data board;
141 struct clk *iclk, *fclk;
142 struct platform_device *pdev;
143 struct proc_dir_entry *pde;
144};
145
146static inline struct at91_udc *to_udc(struct usb_gadget *g)
147{
148 return container_of(g, struct at91_udc, gadget);
149}
150
151struct at91_request {
152 struct usb_request req;
153 struct list_head queue;
154};
155
156/*-------------------------------------------------------------------------*/
157
158#ifdef DEBUG
159#define DBG(stuff...) printk(KERN_DEBUG "udc: " stuff)
160#else
161#define DBG(stuff...) do{}while(0)
162#endif
163
164#ifdef VERBOSE
165# define VDBG DBG
166#else
167# define VDBG(stuff...) do{}while(0)
168#endif
169
170#ifdef PACKET_TRACE
171# define PACKET VDBG
172#else
173# define PACKET(stuff...) do{}while(0)
174#endif
175
176#define ERR(stuff...) printk(KERN_ERR "udc: " stuff)
177#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
178#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
179
180#endif
181
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 9734cb76dd6c..42ce41d71b7f 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -478,10 +478,9 @@ dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
478 return NULL; 478 return NULL;
479 ep = usb_ep_to_dummy_ep (_ep); 479 ep = usb_ep_to_dummy_ep (_ep);
480 480
481 req = kmalloc (sizeof *req, mem_flags); 481 req = kzalloc(sizeof(*req), mem_flags);
482 if (!req) 482 if (!req)
483 return NULL; 483 return NULL;
484 memset (req, 0, sizeof *req);
485 INIT_LIST_HEAD (&req->queue); 484 INIT_LIST_HEAD (&req->queue);
486 return &req->req; 485 return &req->req;
487} 486}
diff --git a/drivers/usb/gadget/ether.c b/drivers/usb/gadget/ether.c
index afc84cfb61f9..c3d8e5c5bf28 100644
--- a/drivers/usb/gadget/ether.c
+++ b/drivers/usb/gadget/ether.c
@@ -182,33 +182,37 @@ struct eth_dev {
182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters). 182 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
183 */ 183 */
184 184
185static ushort __initdata idVendor; 185static ushort idVendor;
186module_param(idVendor, ushort, S_IRUGO); 186module_param(idVendor, ushort, S_IRUGO);
187MODULE_PARM_DESC(idVendor, "USB Vendor ID"); 187MODULE_PARM_DESC(idVendor, "USB Vendor ID");
188 188
189static ushort __initdata idProduct; 189static ushort idProduct;
190module_param(idProduct, ushort, S_IRUGO); 190module_param(idProduct, ushort, S_IRUGO);
191MODULE_PARM_DESC(idProduct, "USB Product ID"); 191MODULE_PARM_DESC(idProduct, "USB Product ID");
192 192
193static ushort __initdata bcdDevice; 193static ushort bcdDevice;
194module_param(bcdDevice, ushort, S_IRUGO); 194module_param(bcdDevice, ushort, S_IRUGO);
195MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); 195MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
196 196
197static char *__initdata iManufacturer; 197static char *iManufacturer;
198module_param(iManufacturer, charp, S_IRUGO); 198module_param(iManufacturer, charp, S_IRUGO);
199MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); 199MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
200 200
201static char *__initdata iProduct; 201static char *iProduct;
202module_param(iProduct, charp, S_IRUGO); 202module_param(iProduct, charp, S_IRUGO);
203MODULE_PARM_DESC(iProduct, "USB Product string"); 203MODULE_PARM_DESC(iProduct, "USB Product string");
204 204
205static char *iSerialNumber;
206module_param(iSerialNumber, charp, S_IRUGO);
207MODULE_PARM_DESC(iSerialNumber, "SerialNumber");
208
205/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */ 209/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
206static char *__initdata dev_addr; 210static char *dev_addr;
207module_param(dev_addr, charp, S_IRUGO); 211module_param(dev_addr, charp, S_IRUGO);
208MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); 212MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
209 213
210/* this address is invisible to ifconfig */ 214/* this address is invisible to ifconfig */
211static char *__initdata host_addr; 215static char *host_addr;
212module_param(host_addr, charp, S_IRUGO); 216module_param(host_addr, charp, S_IRUGO);
213MODULE_PARM_DESC(host_addr, "Host Ethernet Address"); 217MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
214 218
@@ -253,6 +257,14 @@ MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
253#define DEV_CONFIG_CDC 257#define DEV_CONFIG_CDC
254#endif 258#endif
255 259
260#ifdef CONFIG_USB_GADGET_MUSBHSFC
261#define DEV_CONFIG_CDC
262#endif
263
264#ifdef CONFIG_USB_GADGET_MUSBHDRC
265#define DEV_CONFIG_CDC
266#endif
267
256 268
257/* For CDC-incapable hardware, choose the simple cdc subset. 269/* For CDC-incapable hardware, choose the simple cdc subset.
258 * Anything that talks bulk (without notable bugs) can do this. 270 * Anything that talks bulk (without notable bugs) can do this.
@@ -395,6 +407,7 @@ static inline int BITRATE(struct usb_gadget *g)
395#define STRING_CDC 7 407#define STRING_CDC 7
396#define STRING_SUBSET 8 408#define STRING_SUBSET 8
397#define STRING_RNDIS 9 409#define STRING_RNDIS 9
410#define STRING_SERIALNUMBER 10
398 411
399/* holds our biggest descriptor (or RNDIS response) */ 412/* holds our biggest descriptor (or RNDIS response) */
400#define USB_BUFSIZ 256 413#define USB_BUFSIZ 256
@@ -862,6 +875,7 @@ static inline void __init hs_subset_descriptors(void)
862 875
863static char manufacturer [50]; 876static char manufacturer [50];
864static char product_desc [40] = DRIVER_DESC; 877static char product_desc [40] = DRIVER_DESC;
878static char serial_number [20];
865 879
866#ifdef DEV_CONFIG_CDC 880#ifdef DEV_CONFIG_CDC
867/* address that the host will use ... usually assigned at random */ 881/* address that the host will use ... usually assigned at random */
@@ -872,6 +886,7 @@ static char ethaddr [2 * ETH_ALEN + 1];
872static struct usb_string strings [] = { 886static struct usb_string strings [] = {
873 { STRING_MANUFACTURER, manufacturer, }, 887 { STRING_MANUFACTURER, manufacturer, },
874 { STRING_PRODUCT, product_desc, }, 888 { STRING_PRODUCT, product_desc, },
889 { STRING_SERIALNUMBER, serial_number, },
875 { STRING_DATA, "Ethernet Data", }, 890 { STRING_DATA, "Ethernet Data", },
876#ifdef DEV_CONFIG_CDC 891#ifdef DEV_CONFIG_CDC
877 { STRING_CDC, "CDC Ethernet", }, 892 { STRING_CDC, "CDC Ethernet", },
@@ -1549,7 +1564,8 @@ static int eth_change_mtu (struct net_device *net, int new_mtu)
1549{ 1564{
1550 struct eth_dev *dev = netdev_priv(net); 1565 struct eth_dev *dev = netdev_priv(net);
1551 1566
1552 // FIXME if rndis, don't change while link's live 1567 if (dev->rndis)
1568 return -EBUSY;
1553 1569
1554 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN) 1570 if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
1555 return -ERANGE; 1571 return -ERANGE;
@@ -2116,7 +2132,7 @@ eth_req_free (struct usb_ep *ep, struct usb_request *req)
2116} 2132}
2117 2133
2118 2134
2119static void 2135static void __exit
2120eth_unbind (struct usb_gadget *gadget) 2136eth_unbind (struct usb_gadget *gadget)
2121{ 2137{
2122 struct eth_dev *dev = get_gadget_data (gadget); 2138 struct eth_dev *dev = get_gadget_data (gadget);
@@ -2153,7 +2169,7 @@ static u8 __init nibble (unsigned char c)
2153 return 0; 2169 return 0;
2154} 2170}
2155 2171
2156static void __init get_ether_addr (const char *str, u8 *dev_addr) 2172static int __init get_ether_addr(const char *str, u8 *dev_addr)
2157{ 2173{
2158 if (str) { 2174 if (str) {
2159 unsigned i; 2175 unsigned i;
@@ -2168,9 +2184,10 @@ static void __init get_ether_addr (const char *str, u8 *dev_addr)
2168 dev_addr [i] = num; 2184 dev_addr [i] = num;
2169 } 2185 }
2170 if (is_valid_ether_addr (dev_addr)) 2186 if (is_valid_ether_addr (dev_addr))
2171 return; 2187 return 0;
2172 } 2188 }
2173 random_ether_addr(dev_addr); 2189 random_ether_addr(dev_addr);
2190 return 1;
2174} 2191}
2175 2192
2176static int __init 2193static int __init
@@ -2268,6 +2285,10 @@ eth_bind (struct usb_gadget *gadget)
2268 strlcpy (manufacturer, iManufacturer, sizeof manufacturer); 2285 strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
2269 if (iProduct) 2286 if (iProduct)
2270 strlcpy (product_desc, iProduct, sizeof product_desc); 2287 strlcpy (product_desc, iProduct, sizeof product_desc);
2288 if (iSerialNumber) {
2289 device_desc.iSerialNumber = STRING_SERIALNUMBER,
2290 strlcpy(serial_number, iSerialNumber, sizeof serial_number);
2291 }
2271 2292
2272 /* all we really need is bulk IN/OUT */ 2293 /* all we really need is bulk IN/OUT */
2273 usb_ep_autoconfig_reset (gadget); 2294 usb_ep_autoconfig_reset (gadget);
@@ -2377,9 +2398,13 @@ autoconf_fail:
2377 * The host side address is used with CDC and RNDIS, and commonly 2398 * The host side address is used with CDC and RNDIS, and commonly
2378 * ends up in a persistent config database. 2399 * ends up in a persistent config database.
2379 */ 2400 */
2380 get_ether_addr(dev_addr, net->dev_addr); 2401 if (get_ether_addr(dev_addr, net->dev_addr))
2402 dev_warn(&gadget->dev,
2403 "using random %s ethernet address\n", "self");
2381 if (cdc || rndis) { 2404 if (cdc || rndis) {
2382 get_ether_addr(host_addr, dev->host_mac); 2405 if (get_ether_addr(host_addr, dev->host_mac))
2406 dev_warn(&gadget->dev,
2407 "using random %s ethernet address\n", "host");
2383#ifdef DEV_CONFIG_CDC 2408#ifdef DEV_CONFIG_CDC
2384 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X", 2409 snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
2385 dev->host_mac [0], dev->host_mac [1], 2410 dev->host_mac [0], dev->host_mac [1],
@@ -2523,7 +2548,7 @@ static struct usb_gadget_driver eth_driver = {
2523 2548
2524 .function = (char *) driver_desc, 2549 .function = (char *) driver_desc,
2525 .bind = eth_bind, 2550 .bind = eth_bind,
2526 .unbind = eth_unbind, 2551 .unbind = __exit_p(eth_unbind),
2527 2552
2528 .setup = eth_setup, 2553 .setup = eth_setup,
2529 .disconnect = eth_disconnect, 2554 .disconnect = eth_disconnect,
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index de59c58896d6..cf3be299e353 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -3678,7 +3678,7 @@ static void lun_release(struct device *dev)
3678 kref_put(&fsg->ref, fsg_release); 3678 kref_put(&fsg->ref, fsg_release);
3679} 3679}
3680 3680
3681static void fsg_unbind(struct usb_gadget *gadget) 3681static void __exit fsg_unbind(struct usb_gadget *gadget)
3682{ 3682{
3683 struct fsg_dev *fsg = get_gadget_data(gadget); 3683 struct fsg_dev *fsg = get_gadget_data(gadget);
3684 int i; 3684 int i;
@@ -4064,7 +4064,7 @@ static struct usb_gadget_driver fsg_driver = {
4064#endif 4064#endif
4065 .function = (char *) longname, 4065 .function = (char *) longname,
4066 .bind = fsg_bind, 4066 .bind = fsg_bind,
4067 .unbind = fsg_unbind, 4067 .unbind = __exit_p(fsg_unbind),
4068 .disconnect = fsg_disconnect, 4068 .disconnect = fsg_disconnect,
4069 .setup = fsg_setup, 4069 .setup = fsg_setup,
4070 .suspend = fsg_suspend, 4070 .suspend = fsg_suspend,
diff --git a/drivers/usb/gadget/gadget_chips.h b/drivers/usb/gadget/gadget_chips.h
index 8cbae21d84b9..c4081407171f 100644
--- a/drivers/usb/gadget/gadget_chips.h
+++ b/drivers/usb/gadget/gadget_chips.h
@@ -3,9 +3,9 @@
3 * gadget drivers or other code that needs to deal with them, and which 3 * gadget drivers or other code that needs to deal with them, and which
4 * autoconfigures instead of using early binding to the hardware. 4 * autoconfigures instead of using early binding to the hardware.
5 * 5 *
6 * This could eventually work like the ARM mach_is_*() stuff, driven by 6 * This SHOULD eventually work like the ARM mach_is_*() stuff, driven by
7 * some config file that gets updated as new hardware is supported. 7 * some config file that gets updated as new hardware is supported.
8 * (And avoiding the runtime comparisons in typical one-choice cases.) 8 * (And avoiding all runtime comparisons in typical one-choice configs!)
9 * 9 *
10 * NOTE: some of these controller drivers may not be available yet. 10 * NOTE: some of these controller drivers may not be available yet.
11 */ 11 */
@@ -93,6 +93,26 @@
93#define gadget_is_imx(g) 0 93#define gadget_is_imx(g) 0
94#endif 94#endif
95 95
96/* Mentor high speed function controller */
97#ifdef CONFIG_USB_GADGET_MUSBHSFC
98#define gadget_is_musbhsfc(g) !strcmp("musbhsfc_udc", (g)->name)
99#else
100#define gadget_is_musbhsfc(g) 0
101#endif
102
103/* Mentor high speed "dual role" controller, peripheral mode */
104#ifdef CONFIG_USB_GADGET_MUSBHDRC
105#define gadget_is_musbhdrc(g) !strcmp("musbhdrc_udc", (g)->name)
106#else
107#define gadget_is_musbhdrc(g) 0
108#endif
109
110#ifdef CONFIG_USB_GADGET_MPC8272
111#define gadget_is_mpc8272(g) !strcmp("mpc8272_udc", (g)->name)
112#else
113#define gadget_is_mpc8272(g) 0
114#endif
115
96// CONFIG_USB_GADGET_SX2 116// CONFIG_USB_GADGET_SX2
97// CONFIG_USB_GADGET_AU1X00 117// CONFIG_USB_GADGET_AU1X00
98// ... 118// ...
@@ -143,5 +163,11 @@ static inline int usb_gadget_controller_number(struct usb_gadget *gadget)
143 return 0x13; 163 return 0x13;
144 else if (gadget_is_imx(gadget)) 164 else if (gadget_is_imx(gadget))
145 return 0x14; 165 return 0x14;
166 else if (gadget_is_musbhsfc(gadget))
167 return 0x15;
168 else if (gadget_is_musbhdrc(gadget))
169 return 0x16;
170 else if (gadget_is_mpc8272(gadget))
171 return 0x17;
146 return -ENOENT; 172 return -ENOENT;
147} 173}
diff --git a/drivers/usb/gadget/goku_udc.c b/drivers/usb/gadget/goku_udc.c
index b0f3cd63e3b9..66b81bbf6bee 100644
--- a/drivers/usb/gadget/goku_udc.c
+++ b/drivers/usb/gadget/goku_udc.c
@@ -275,11 +275,10 @@ goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
275 275
276 if (!_ep) 276 if (!_ep)
277 return NULL; 277 return NULL;
278 req = kmalloc(sizeof *req, gfp_flags); 278 req = kzalloc(sizeof *req, gfp_flags);
279 if (!req) 279 if (!req)
280 return NULL; 280 return NULL;
281 281
282 memset(req, 0, sizeof *req);
283 req->req.dma = DMA_ADDR_INVALID; 282 req->req.dma = DMA_ADDR_INVALID;
284 INIT_LIST_HEAD(&req->queue); 283 INIT_LIST_HEAD(&req->queue);
285 return &req->req; 284 return &req->req;
diff --git a/drivers/usb/gadget/inode.c b/drivers/usb/gadget/inode.c
index 0aab7d24c768..b44cfda76b61 100644
--- a/drivers/usb/gadget/inode.c
+++ b/drivers/usb/gadget/inode.c
@@ -170,10 +170,9 @@ static struct dev_data *dev_new (void)
170{ 170{
171 struct dev_data *dev; 171 struct dev_data *dev;
172 172
173 dev = kmalloc (sizeof *dev, GFP_KERNEL); 173 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
174 if (!dev) 174 if (!dev)
175 return NULL; 175 return NULL;
176 memset (dev, 0, sizeof *dev);
177 dev->state = STATE_DEV_DISABLED; 176 dev->state = STATE_DEV_DISABLED;
178 atomic_set (&dev->count, 1); 177 atomic_set (&dev->count, 1);
179 spin_lock_init (&dev->lock); 178 spin_lock_init (&dev->lock);
@@ -1592,10 +1591,9 @@ static int activate_ep_files (struct dev_data *dev)
1592 gadget_for_each_ep (ep, dev->gadget) { 1591 gadget_for_each_ep (ep, dev->gadget) {
1593 struct ep_data *data; 1592 struct ep_data *data;
1594 1593
1595 data = kmalloc (sizeof *data, GFP_KERNEL); 1594 data = kzalloc(sizeof(*data), GFP_KERNEL);
1596 if (!data) 1595 if (!data)
1597 goto enomem; 1596 goto enomem;
1598 memset (data, 0, sizeof data);
1599 data->state = STATE_EP_DISABLED; 1597 data->state = STATE_EP_DISABLED;
1600 init_MUTEX (&data->lock); 1598 init_MUTEX (&data->lock);
1601 init_waitqueue_head (&data->wait); 1599 init_waitqueue_head (&data->wait);
diff --git a/drivers/usb/gadget/lh7a40x_udc.c b/drivers/usb/gadget/lh7a40x_udc.c
index 1a362c5e7f3d..0d3424eda038 100644
--- a/drivers/usb/gadget/lh7a40x_udc.c
+++ b/drivers/usb/gadget/lh7a40x_udc.c
@@ -1114,11 +1114,10 @@ static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1114 1114
1115 DEBUG("%s, %p\n", __FUNCTION__, ep); 1115 DEBUG("%s, %p\n", __FUNCTION__, ep);
1116 1116
1117 req = kmalloc(sizeof *req, gfp_flags); 1117 req = kzalloc(sizeof(*req), gfp_flags);
1118 if (!req) 1118 if (!req)
1119 return 0; 1119 return 0;
1120 1120
1121 memset(req, 0, sizeof *req);
1122 INIT_LIST_HEAD(&req->queue); 1121 INIT_LIST_HEAD(&req->queue);
1123 1122
1124 return &req->req; 1123 return &req->req;
diff --git a/drivers/usb/gadget/net2280.c b/drivers/usb/gadget/net2280.c
index 67b13ab2f3f5..fb73dc100535 100644
--- a/drivers/usb/gadget/net2280.c
+++ b/drivers/usb/gadget/net2280.c
@@ -386,11 +386,10 @@ net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
386 return NULL; 386 return NULL;
387 ep = container_of (_ep, struct net2280_ep, ep); 387 ep = container_of (_ep, struct net2280_ep, ep);
388 388
389 req = kmalloc (sizeof *req, gfp_flags); 389 req = kzalloc(sizeof(*req), gfp_flags);
390 if (!req) 390 if (!req)
391 return NULL; 391 return NULL;
392 392
393 memset (req, 0, sizeof *req);
394 req->req.dma = DMA_ADDR_INVALID; 393 req->req.dma = DMA_ADDR_INVALID;
395 INIT_LIST_HEAD (&req->queue); 394 INIT_LIST_HEAD (&req->queue);
396 395
diff --git a/drivers/usb/gadget/omap_udc.c b/drivers/usb/gadget/omap_udc.c
index a8972d7c97be..fbea51448909 100644
--- a/drivers/usb/gadget/omap_udc.c
+++ b/drivers/usb/gadget/omap_udc.c
@@ -273,9 +273,8 @@ omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
273{ 273{
274 struct omap_req *req; 274 struct omap_req *req;
275 275
276 req = kmalloc(sizeof *req, gfp_flags); 276 req = kzalloc(sizeof(*req), gfp_flags);
277 if (req) { 277 if (req) {
278 memset (req, 0, sizeof *req);
279 req->req.dma = DMA_ADDR_INVALID; 278 req->req.dma = DMA_ADDR_INVALID;
280 INIT_LIST_HEAD (&req->queue); 279 INIT_LIST_HEAD (&req->queue);
281 } 280 }
@@ -2586,11 +2585,10 @@ omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2586 /* UDC_PULLUP_EN gates the chip clock */ 2585 /* UDC_PULLUP_EN gates the chip clock */
2587 // OTG_SYSCON_1_REG |= DEV_IDLE_EN; 2586 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2588 2587
2589 udc = kmalloc (sizeof *udc, SLAB_KERNEL); 2588 udc = kzalloc(sizeof(*udc), SLAB_KERNEL);
2590 if (!udc) 2589 if (!udc)
2591 return -ENOMEM; 2590 return -ENOMEM;
2592 2591
2593 memset(udc, 0, sizeof *udc);
2594 spin_lock_init (&udc->lock); 2592 spin_lock_init (&udc->lock);
2595 2593
2596 udc->gadget.ops = &omap_gadget_ops; 2594 udc->gadget.ops = &omap_gadget_ops;
diff --git a/drivers/usb/gadget/pxa2xx_udc.c b/drivers/usb/gadget/pxa2xx_udc.c
index bb028c5b8952..680f7fc5b171 100644
--- a/drivers/usb/gadget/pxa2xx_udc.c
+++ b/drivers/usb/gadget/pxa2xx_udc.c
@@ -335,11 +335,10 @@ pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
335{ 335{
336 struct pxa2xx_request *req; 336 struct pxa2xx_request *req;
337 337
338 req = kmalloc (sizeof *req, gfp_flags); 338 req = kzalloc(sizeof(*req), gfp_flags);
339 if (!req) 339 if (!req)
340 return NULL; 340 return NULL;
341 341
342 memset (req, 0, sizeof *req);
343 INIT_LIST_HEAD (&req->queue); 342 INIT_LIST_HEAD (&req->queue);
344 return &req->req; 343 return &req->req;
345} 344}
diff --git a/drivers/usb/gadget/serial.c b/drivers/usb/gadget/serial.c
index ba9acd531024..b992546c394d 100644
--- a/drivers/usb/gadget/serial.c
+++ b/drivers/usb/gadget/serial.c
@@ -369,7 +369,7 @@ static struct usb_gadget_driver gs_gadget_driver = {
369#endif /* CONFIG_USB_GADGET_DUALSPEED */ 369#endif /* CONFIG_USB_GADGET_DUALSPEED */
370 .function = GS_LONG_NAME, 370 .function = GS_LONG_NAME,
371 .bind = gs_bind, 371 .bind = gs_bind,
372 .unbind = gs_unbind, 372 .unbind = __exit_p(gs_unbind),
373 .setup = gs_setup, 373 .setup = gs_setup,
374 .disconnect = gs_disconnect, 374 .disconnect = gs_disconnect,
375 .driver = { 375 .driver = {
@@ -1413,7 +1413,7 @@ requeue:
1413 * Called on module load. Allocates and initializes the device 1413 * Called on module load. Allocates and initializes the device
1414 * structure and a control request. 1414 * structure and a control request.
1415 */ 1415 */
1416static int gs_bind(struct usb_gadget *gadget) 1416static int __init gs_bind(struct usb_gadget *gadget)
1417{ 1417{
1418 int ret; 1418 int ret;
1419 struct usb_ep *ep; 1419 struct usb_ep *ep;
@@ -1538,7 +1538,7 @@ autoconf_fail:
1538 * Called on module unload. Frees the control request and device 1538 * Called on module unload. Frees the control request and device
1539 * structure. 1539 * structure.
1540 */ 1540 */
1541static void gs_unbind(struct usb_gadget *gadget) 1541static void __exit gs_unbind(struct usb_gadget *gadget)
1542{ 1542{
1543 struct gs_dev *dev = get_gadget_data(gadget); 1543 struct gs_dev *dev = get_gadget_data(gadget);
1544 1544
@@ -2178,10 +2178,9 @@ static int gs_alloc_ports(struct gs_dev *dev, gfp_t kmalloc_flags)
2178 return -EIO; 2178 return -EIO;
2179 2179
2180 for (i=0; i<GS_NUM_PORTS; i++) { 2180 for (i=0; i<GS_NUM_PORTS; i++) {
2181 if ((port=(struct gs_port *)kmalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL) 2181 if ((port=kzalloc(sizeof(struct gs_port), kmalloc_flags)) == NULL)
2182 return -ENOMEM; 2182 return -ENOMEM;
2183 2183
2184 memset(port, 0, sizeof(struct gs_port));
2185 port->port_dev = dev; 2184 port->port_dev = dev;
2186 port->port_num = i; 2185 port->port_num = i;
2187 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE); 2186 port->port_line_coding.dwDTERate = cpu_to_le32(GS_DEFAULT_DTE_RATE);
diff --git a/drivers/usb/gadget/zero.c b/drivers/usb/gadget/zero.c
index ae7a1c0f5748..51424f66a765 100644
--- a/drivers/usb/gadget/zero.c
+++ b/drivers/usb/gadget/zero.c
@@ -1119,7 +1119,7 @@ zero_autoresume (unsigned long _dev)
1119 1119
1120/*-------------------------------------------------------------------------*/ 1120/*-------------------------------------------------------------------------*/
1121 1121
1122static void 1122static void __exit
1123zero_unbind (struct usb_gadget *gadget) 1123zero_unbind (struct usb_gadget *gadget)
1124{ 1124{
1125 struct zero_dev *dev = get_gadget_data (gadget); 1125 struct zero_dev *dev = get_gadget_data (gadget);
@@ -1136,7 +1136,7 @@ zero_unbind (struct usb_gadget *gadget)
1136 set_gadget_data (gadget, NULL); 1136 set_gadget_data (gadget, NULL);
1137} 1137}
1138 1138
1139static int 1139static int __init
1140zero_bind (struct usb_gadget *gadget) 1140zero_bind (struct usb_gadget *gadget)
1141{ 1141{
1142 struct zero_dev *dev; 1142 struct zero_dev *dev;
@@ -1188,10 +1188,9 @@ autoconf_fail:
1188 1188
1189 1189
1190 /* ok, we made sense of the hardware ... */ 1190 /* ok, we made sense of the hardware ... */
1191 dev = kmalloc (sizeof *dev, SLAB_KERNEL); 1191 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
1192 if (!dev) 1192 if (!dev)
1193 return -ENOMEM; 1193 return -ENOMEM;
1194 memset (dev, 0, sizeof *dev);
1195 spin_lock_init (&dev->lock); 1194 spin_lock_init (&dev->lock);
1196 dev->gadget = gadget; 1195 dev->gadget = gadget;
1197 set_gadget_data (gadget, dev); 1196 set_gadget_data (gadget, dev);
@@ -1224,12 +1223,6 @@ autoconf_fail:
1224 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 1223 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1225 } 1224 }
1226 1225
1227 if (gadget->is_otg) {
1228 otg_descriptor.bmAttributes |= USB_OTG_HNP,
1229 source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1230 loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1231 }
1232
1233 usb_gadget_set_selfpowered (gadget); 1226 usb_gadget_set_selfpowered (gadget);
1234 1227
1235 init_timer (&dev->resume); 1228 init_timer (&dev->resume);
@@ -1294,7 +1287,7 @@ static struct usb_gadget_driver zero_driver = {
1294#endif 1287#endif
1295 .function = (char *) longname, 1288 .function = (char *) longname,
1296 .bind = zero_bind, 1289 .bind = zero_bind,
1297 .unbind = zero_unbind, 1290 .unbind = __exit_p(zero_unbind),
1298 1291
1299 .setup = zero_setup, 1292 .setup = zero_setup,
1300 .disconnect = zero_disconnect, 1293 .disconnect = zero_disconnect,
diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index be3fd9bce573..e27b79a3c05f 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -6,7 +6,7 @@ comment "USB Host Controller Drivers"
6 6
7config USB_EHCI_HCD 7config USB_EHCI_HCD
8 tristate "EHCI HCD (USB 2.0) support" 8 tristate "EHCI HCD (USB 2.0) support"
9 depends on USB && PCI 9 depends on USB && USB_ARCH_HAS_EHCI
10 ---help--- 10 ---help---
11 The Enhanced Host Controller Interface (EHCI) is standard for USB 2.0 11 The Enhanced Host Controller Interface (EHCI) is standard for USB 2.0
12 "high speed" (480 Mbit/sec, 60 Mbyte/sec) host controller hardware. 12 "high speed" (480 Mbit/sec, 60 Mbyte/sec) host controller hardware.
diff --git a/drivers/usb/host/ehci-au1xxx.c b/drivers/usb/host/ehci-au1xxx.c
new file mode 100644
index 000000000000..63eadeec1324
--- /dev/null
+++ b/drivers/usb/host/ehci-au1xxx.c
@@ -0,0 +1,297 @@
1/*
2 * EHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
5 *
6 * Bus Glue for AMD Alchemy Au1xxx
7 *
8 * Based on "ohci-au1xxx.c" by Matt Porter <mporter@kernel.crashing.org>
9 *
10 * Modified for AMD Alchemy Au1200 EHC
11 * by K.Boge <karsten.boge@amd.com>
12 *
13 * This file is licenced under the GPL.
14 */
15
16#include <linux/platform_device.h>
17#include <asm/mach-au1x00/au1000.h>
18
19#ifndef CONFIG_SOC_AU1200
20#error "this Alchemy chip doesn't have EHCI"
21#else /* Au1200 */
22
23#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
24#define USB_MCFG_PFEN (1<<31)
25#define USB_MCFG_RDCOMB (1<<30)
26#define USB_MCFG_SSDEN (1<<23)
27#define USB_MCFG_PHYPLLEN (1<<19)
28#define USB_MCFG_EHCCLKEN (1<<17)
29#define USB_MCFG_UCAM (1<<7)
30#define USB_MCFG_EBMEN (1<<3)
31#define USB_MCFG_EMEMEN (1<<2)
32
33#define USBH_ENABLE_CE (USB_MCFG_PHYPLLEN | USB_MCFG_EHCCLKEN)
34
35#ifdef CONFIG_DMA_COHERENT
36#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
37 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
38 | USB_MCFG_SSDEN | USB_MCFG_UCAM \
39 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
40#else
41#define USBH_ENABLE_INIT (USBH_ENABLE_CE \
42 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
43 | USB_MCFG_SSDEN \
44 | USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
45#endif
46#define USBH_DISABLE (USB_MCFG_EBMEN | USB_MCFG_EMEMEN)
47
48#endif /* Au1200 */
49
50extern int usb_disabled(void);
51
52/*-------------------------------------------------------------------------*/
53
54static void au1xxx_start_ehc(struct platform_device *dev)
55{
56 pr_debug(__FILE__ ": starting Au1xxx EHCI USB Controller\n");
57
58 /* write HW defaults again in case Yamon cleared them */
59 if (au_readl(USB_HOST_CONFIG) == 0) {
60 au_writel(0x00d02000, USB_HOST_CONFIG);
61 au_readl(USB_HOST_CONFIG);
62 udelay(1000);
63 }
64 /* enable host controller */
65 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
66 au_readl(USB_HOST_CONFIG);
67 udelay(1000);
68 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG),
69 USB_HOST_CONFIG);
70 au_readl(USB_HOST_CONFIG);
71 udelay(1000);
72
73 pr_debug(__FILE__ ": Clock to USB host has been enabled\n");
74}
75
76static void au1xxx_stop_ehc(struct platform_device *dev)
77{
78 pr_debug(__FILE__ ": stopping Au1xxx EHCI USB Controller\n");
79
80 /* Disable mem */
81 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
82 udelay(1000);
83 /* Disable clock */
84 au_writel(~USB_MCFG_EHCCLKEN & au_readl(USB_HOST_CONFIG),
85 USB_HOST_CONFIG);
86 au_readl(USB_HOST_CONFIG);
87}
88
89/*-------------------------------------------------------------------------*/
90
91/* configure so an HC device and id are always provided */
92/* always called with process context; sleeping is OK */
93
94/**
95 * usb_ehci_au1xxx_probe - initialize Au1xxx-based HCDs
96 * Context: !in_interrupt()
97 *
98 * Allocates basic resources for this USB host controller, and
99 * then invokes the start() method for the HCD associated with it
100 * through the hotplug entry's driver_data.
101 *
102 */
103int usb_ehci_au1xxx_probe(const struct hc_driver *driver,
104 struct usb_hcd **hcd_out, struct platform_device *dev)
105{
106 int retval;
107 struct usb_hcd *hcd;
108 struct ehci_hcd *ehci;
109
110#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
111
112 /* Au1200 AB USB does not support coherent memory */
113 if (!(read_c0_prid() & 0xff)) {
114 pr_info("%s: this is chip revision AB!\n", dev->dev.name);
115 pr_info("%s: update your board or re-configure the kernel\n",
116 dev->dev.name);
117 return -ENODEV;
118 }
119#endif
120
121 au1xxx_start_ehc(dev);
122
123 if (dev->resource[1].flags != IORESOURCE_IRQ) {
124 pr_debug("resource[1] is not IORESOURCE_IRQ");
125 retval = -ENOMEM;
126 }
127 hcd = usb_create_hcd(driver, &dev->dev, "Au1xxx");
128 if (!hcd)
129 return -ENOMEM;
130 hcd->rsrc_start = dev->resource[0].start;
131 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
132
133 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
134 pr_debug("request_mem_region failed");
135 retval = -EBUSY;
136 goto err1;
137 }
138
139 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
140 if (!hcd->regs) {
141 pr_debug("ioremap failed");
142 retval = -ENOMEM;
143 goto err2;
144 }
145
146 ehci = hcd_to_ehci(hcd);
147 ehci->caps = hcd->regs;
148 ehci->regs = hcd->regs + HC_LENGTH(readl(&ehci->caps->hc_capbase));
149 /* cache this readonly data; minimize chip reads */
150 ehci->hcs_params = readl(&ehci->caps->hcs_params);
151
152 /* ehci_hcd_init(hcd_to_ehci(hcd)); */
153
154 retval =
155 usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT | SA_SHIRQ);
156 if (retval == 0)
157 return retval;
158
159 au1xxx_stop_ehc(dev);
160 iounmap(hcd->regs);
161err2:
162 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
163err1:
164 usb_put_hcd(hcd);
165 return retval;
166}
167
168/* may be called without controller electrically present */
169/* may be called with controller, bus, and devices active */
170
171/**
172 * usb_ehci_hcd_au1xxx_remove - shutdown processing for Au1xxx-based HCDs
173 * @dev: USB Host Controller being removed
174 * Context: !in_interrupt()
175 *
176 * Reverses the effect of usb_ehci_hcd_au1xxx_probe(), first invoking
177 * the HCD's stop() method. It is always called from a thread
178 * context, normally "rmmod", "apmd", or something similar.
179 *
180 */
181void usb_ehci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
182{
183 usb_remove_hcd(hcd);
184 iounmap(hcd->regs);
185 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
186 usb_put_hcd(hcd);
187 au1xxx_stop_ehc(dev);
188}
189
190/*-------------------------------------------------------------------------*/
191
192static const struct hc_driver ehci_au1xxx_hc_driver = {
193 .description = hcd_name,
194 .product_desc = "Au1xxx EHCI",
195 .hcd_priv_size = sizeof(struct ehci_hcd),
196
197 /*
198 * generic hardware linkage
199 */
200 .irq = ehci_irq,
201 .flags = HCD_MEMORY | HCD_USB2,
202
203 /*
204 * basic lifecycle operations
205 */
206 .reset = ehci_init,
207 .start = ehci_run,
208 .stop = ehci_stop,
209
210 /*
211 * managing i/o requests and associated device resources
212 */
213 .urb_enqueue = ehci_urb_enqueue,
214 .urb_dequeue = ehci_urb_dequeue,
215 .endpoint_disable = ehci_endpoint_disable,
216
217 /*
218 * scheduling support
219 */
220 .get_frame_number = ehci_get_frame,
221
222 /*
223 * root hub support
224 */
225 .hub_status_data = ehci_hub_status_data,
226 .hub_control = ehci_hub_control,
227#ifdef CONFIG_PM
228 .hub_suspend = ehci_hub_suspend,
229 .hub_resume = ehci_hub_resume,
230#endif
231};
232
233/*-------------------------------------------------------------------------*/
234
235static int ehci_hcd_au1xxx_drv_probe(struct device *dev)
236{
237 struct platform_device *pdev = to_platform_device(dev);
238 struct usb_hcd *hcd = NULL;
239 int ret;
240
241 pr_debug("In ehci_hcd_au1xxx_drv_probe\n");
242
243 if (usb_disabled())
244 return -ENODEV;
245
246 ret = usb_ehci_au1xxx_probe(&ehci_au1xxx_hc_driver, &hcd, pdev);
247 return ret;
248}
249
250static int ehci_hcd_au1xxx_drv_remove(struct device *dev)
251{
252 struct platform_device *pdev = to_platform_device(dev);
253 struct usb_hcd *hcd = dev_get_drvdata(dev);
254
255 usb_ehci_au1xxx_remove(hcd, pdev);
256 return 0;
257}
258
259 /*TBD*/
260/*static int ehci_hcd_au1xxx_drv_suspend(struct device *dev)
261{
262 struct platform_device *pdev = to_platform_device(dev);
263 struct usb_hcd *hcd = dev_get_drvdata(dev);
264
265 return 0;
266}
267static int ehci_hcd_au1xxx_drv_resume(struct device *dev)
268{
269 struct platform_device *pdev = to_platform_device(dev);
270 struct usb_hcd *hcd = dev_get_drvdata(dev);
271
272 return 0;
273}
274*/
275static struct device_driver ehci_hcd_au1xxx_driver = {
276 .name = "au1xxx-ehci",
277 .bus = &platform_bus_type,
278 .probe = ehci_hcd_au1xxx_drv_probe,
279 .remove = ehci_hcd_au1xxx_drv_remove,
280 /*.suspend = ehci_hcd_au1xxx_drv_suspend, */
281 /*.resume = ehci_hcd_au1xxx_drv_resume, */
282};
283
284static int __init ehci_hcd_au1xxx_init(void)
285{
286 pr_debug(DRIVER_INFO " (Au1xxx)\n");
287
288 return driver_register(&ehci_hcd_au1xxx_driver);
289}
290
291static void __exit ehci_hcd_au1xxx_cleanup(void)
292{
293 driver_unregister(&ehci_hcd_au1xxx_driver);
294}
295
296module_init(ehci_hcd_au1xxx_init);
297module_exit(ehci_hcd_au1xxx_cleanup);
diff --git a/drivers/usb/host/ehci-fsl.c b/drivers/usb/host/ehci-fsl.c
new file mode 100644
index 000000000000..f985f121a245
--- /dev/null
+++ b/drivers/usb/host/ehci-fsl.c
@@ -0,0 +1,366 @@
1/*
2 * (C) Copyright David Brownell 2000-2002
3 * Copyright (c) 2005 MontaVista Software
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Ported to 834x by Randy Vinson <rvinson@mvista.com> using code provided
20 * by Hunter Wu.
21 */
22
23#include <linux/platform_device.h>
24#include <linux/fsl_devices.h>
25
26#include "ehci-fsl.h"
27
28/* FIXME: Power Managment is un-ported so temporarily disable it */
29#undef CONFIG_PM
30
31/* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
32
33/* configure so an HC device and id are always provided */
34/* always called with process context; sleeping is OK */
35
36/**
37 * usb_hcd_fsl_probe - initialize FSL-based HCDs
38 * @drvier: Driver to be used for this HCD
39 * @pdev: USB Host Controller being probed
40 * Context: !in_interrupt()
41 *
42 * Allocates basic resources for this USB host controller.
43 *
44 */
45int usb_hcd_fsl_probe(const struct hc_driver *driver,
46 struct platform_device *pdev)
47{
48 struct fsl_usb2_platform_data *pdata;
49 struct usb_hcd *hcd;
50 struct resource *res;
51 int irq;
52 int retval;
53 unsigned int temp;
54
55 pr_debug("initializing FSL-SOC USB Controller\n");
56
57 /* Need platform data for setup */
58 pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
59 if (!pdata) {
60 dev_err(&pdev->dev,
61 "No platform data for %s.\n", pdev->dev.bus_id);
62 return -ENODEV;
63 }
64
65 /*
66 * This is a host mode driver, verify that we're supposed to be
67 * in host mode.
68 */
69 if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
70 (pdata->operating_mode == FSL_USB2_MPH_HOST))) {
71 dev_err(&pdev->dev,
72 "Non Host Mode configured for %s. Wrong driver linked.\n",
73 pdev->dev.bus_id);
74 return -ENODEV;
75 }
76
77 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
78 if (!res) {
79 dev_err(&pdev->dev,
80 "Found HC with no IRQ. Check %s setup!\n",
81 pdev->dev.bus_id);
82 return -ENODEV;
83 }
84 irq = res->start;
85
86 hcd = usb_create_hcd(driver, &pdev->dev, pdev->dev.bus_id);
87 if (!hcd) {
88 retval = -ENOMEM;
89 goto err1;
90 }
91
92 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93 if (!res) {
94 dev_err(&pdev->dev,
95 "Found HC with no register addr. Check %s setup!\n",
96 pdev->dev.bus_id);
97 retval = -ENODEV;
98 goto err2;
99 }
100 hcd->rsrc_start = res->start;
101 hcd->rsrc_len = res->end - res->start + 1;
102 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
103 driver->description)) {
104 dev_dbg(&pdev->dev, "controller already in use\n");
105 retval = -EBUSY;
106 goto err2;
107 }
108 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
109
110 if (hcd->regs == NULL) {
111 dev_dbg(&pdev->dev, "error mapping memory\n");
112 retval = -EFAULT;
113 goto err3;
114 }
115
116 /* Enable USB controller */
117 temp = in_be32(hcd->regs + 0x500);
118 out_be32(hcd->regs + 0x500, temp | 0x4);
119
120 /* Set to Host mode */
121 temp = in_le32(hcd->regs + 0x1a8);
122 out_le32(hcd->regs + 0x1a8, temp | 0x3);
123
124 retval = usb_add_hcd(hcd, irq, SA_SHIRQ);
125 if (retval != 0)
126 goto err4;
127 return retval;
128
129 err4:
130 iounmap(hcd->regs);
131 err3:
132 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
133 err2:
134 usb_put_hcd(hcd);
135 err1:
136 dev_err(&pdev->dev, "init %s fail, %d\n", pdev->dev.bus_id, retval);
137 return retval;
138}
139
140/* may be called without controller electrically present */
141/* may be called with controller, bus, and devices active */
142
143/**
144 * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
145 * @dev: USB Host Controller being removed
146 * Context: !in_interrupt()
147 *
148 * Reverses the effect of usb_hcd_fsl_probe().
149 *
150 */
151void usb_hcd_fsl_remove(struct usb_hcd *hcd, struct platform_device *pdev)
152{
153 usb_remove_hcd(hcd);
154 iounmap(hcd->regs);
155 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
156 usb_put_hcd(hcd);
157}
158
159static void mpc83xx_setup_phy(struct ehci_hcd *ehci,
160 enum fsl_usb2_phy_modes phy_mode,
161 unsigned int port_offset)
162{
163 u32 portsc = 0;
164 switch (phy_mode) {
165 case FSL_USB2_PHY_ULPI:
166 portsc |= PORT_PTS_ULPI;
167 break;
168 case FSL_USB2_PHY_SERIAL:
169 portsc |= PORT_PTS_SERIAL;
170 break;
171 case FSL_USB2_PHY_UTMI_WIDE:
172 portsc |= PORT_PTS_PTW;
173 /* fall through */
174 case FSL_USB2_PHY_UTMI:
175 portsc |= PORT_PTS_UTMI;
176 break;
177 case FSL_USB2_PHY_NONE:
178 break;
179 }
180 writel(portsc, &ehci->regs->port_status[port_offset]);
181}
182
183static void mpc83xx_usb_setup(struct usb_hcd *hcd)
184{
185 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
186 struct fsl_usb2_platform_data *pdata;
187 void __iomem *non_ehci = hcd->regs;
188
189 pdata =
190 (struct fsl_usb2_platform_data *)hcd->self.controller->
191 platform_data;
192 /* Enable PHY interface in the control reg. */
193 out_be32(non_ehci + FSL_SOC_USB_CTRL, 0x00000004);
194 out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0000001b);
195
196 if (pdata->operating_mode == FSL_USB2_DR_HOST)
197 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
198
199 if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
200 unsigned int chip, rev, svr;
201
202 svr = mfspr(SPRN_SVR);
203 chip = svr >> 16;
204 rev = (svr >> 4) & 0xf;
205
206 /* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
207 if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
208 ehci->has_fsl_port_bug = 1;
209
210 if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
211 mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
212 if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
213 mpc83xx_setup_phy(ehci, pdata->phy_mode, 1);
214 }
215
216 /* put controller in host mode. */
217 writel(0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
218 out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
219 out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
220 out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
221}
222
223/* called after powerup, by probe or system-pm "wakeup" */
224static int ehci_fsl_reinit(struct ehci_hcd *ehci)
225{
226 mpc83xx_usb_setup(ehci_to_hcd(ehci));
227 ehci_port_power(ehci, 0);
228
229 return 0;
230}
231
232/* called during probe() after chip reset completes */
233static int ehci_fsl_setup(struct usb_hcd *hcd)
234{
235 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
236 int retval;
237
238 /* EHCI registers start at offset 0x100 */
239 ehci->caps = hcd->regs + 0x100;
240 ehci->regs = hcd->regs + 0x100 +
241 HC_LENGTH(readl(&ehci->caps->hc_capbase));
242 dbg_hcs_params(ehci, "reset");
243 dbg_hcc_params(ehci, "reset");
244
245 /* cache this readonly data; minimize chip reads */
246 ehci->hcs_params = readl(&ehci->caps->hcs_params);
247
248 retval = ehci_halt(ehci);
249 if (retval)
250 return retval;
251
252 /* data structure init */
253 retval = ehci_init(hcd);
254 if (retval)
255 return retval;
256
257 ehci->is_tdi_rh_tt = 1;
258
259 ehci->sbrn = 0x20;
260
261 ehci_reset(ehci);
262
263 retval = ehci_fsl_reinit(ehci);
264 return retval;
265}
266
267static const struct hc_driver ehci_fsl_hc_driver = {
268 .description = hcd_name,
269 .product_desc = "Freescale On-Chip EHCI Host Controller",
270 .hcd_priv_size = sizeof(struct ehci_hcd),
271
272 /*
273 * generic hardware linkage
274 */
275 .irq = ehci_irq,
276 .flags = HCD_USB2,
277
278 /*
279 * basic lifecycle operations
280 */
281 .reset = ehci_fsl_setup,
282 .start = ehci_run,
283#ifdef CONFIG_PM
284 .suspend = ehci_bus_suspend,
285 .resume = ehci_bus_resume,
286#endif
287 .stop = ehci_stop,
288
289 /*
290 * managing i/o requests and associated device resources
291 */
292 .urb_enqueue = ehci_urb_enqueue,
293 .urb_dequeue = ehci_urb_dequeue,
294 .endpoint_disable = ehci_endpoint_disable,
295
296 /*
297 * scheduling support
298 */
299 .get_frame_number = ehci_get_frame,
300
301 /*
302 * root hub support
303 */
304 .hub_status_data = ehci_hub_status_data,
305 .hub_control = ehci_hub_control,
306 .bus_suspend = ehci_bus_suspend,
307 .bus_resume = ehci_bus_resume,
308};
309
310static int ehci_fsl_drv_probe(struct platform_device *pdev)
311{
312 if (usb_disabled())
313 return -ENODEV;
314
315 return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
316}
317
318static int ehci_fsl_drv_remove(struct platform_device *pdev)
319{
320 struct usb_hcd *hcd = platform_get_drvdata(pdev);
321
322 usb_hcd_fsl_remove(hcd, pdev);
323
324 return 0;
325}
326
327static struct platform_driver ehci_fsl_dr_driver = {
328 .probe = ehci_fsl_drv_probe,
329 .remove = ehci_fsl_drv_remove,
330 .driver = {
331 .name = "fsl-usb2-dr",
332 },
333};
334
335static struct platform_driver ehci_fsl_mph_driver = {
336 .probe = ehci_fsl_drv_probe,
337 .remove = ehci_fsl_drv_remove,
338 .driver = {
339 .name = "fsl-usb2-mph",
340 },
341};
342
343static int __init ehci_fsl_init(void)
344{
345 int retval;
346
347 pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
348 hcd_name,
349 sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
350 sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
351
352 retval = platform_driver_register(&ehci_fsl_dr_driver);
353 if (retval)
354 return retval;
355
356 return platform_driver_register(&ehci_fsl_mph_driver);
357}
358
359static void __exit ehci_fsl_cleanup(void)
360{
361 platform_driver_unregister(&ehci_fsl_mph_driver);
362 platform_driver_unregister(&ehci_fsl_dr_driver);
363}
364
365module_init(ehci_fsl_init);
366module_exit(ehci_fsl_cleanup);
diff --git a/drivers/usb/host/ehci-fsl.h b/drivers/usb/host/ehci-fsl.h
new file mode 100644
index 000000000000..caac0d1967d0
--- /dev/null
+++ b/drivers/usb/host/ehci-fsl.h
@@ -0,0 +1,37 @@
1/* Copyright (c) 2005 freescale semiconductor
2 * Copyright (c) 2005 MontaVista Software
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18#ifndef _EHCI_FSL_H
19#define _EHCI_FSL_H
20
21/* offsets for the non-ehci registers in the FSL SOC USB controller */
22#define FSL_SOC_USB_ULPIVP 0x170
23#define FSL_SOC_USB_PORTSC1 0x184
24#define PORT_PTS_MSK (3<<30)
25#define PORT_PTS_UTMI (0<<30)
26#define PORT_PTS_ULPI (2<<30)
27#define PORT_PTS_SERIAL (3<<30)
28#define PORT_PTS_PTW (1<<28)
29#define FSL_SOC_USB_PORTSC2 0x188
30#define FSL_SOC_USB_USBMODE 0x1a8
31#define FSL_SOC_USB_SNOOP1 0x400 /* NOTE: big-endian */
32#define FSL_SOC_USB_SNOOP2 0x404 /* NOTE: big-endian */
33#define FSL_SOC_USB_AGECNTTHRSH 0x408 /* NOTE: big-endian */
34#define FSL_SOC_USB_SICTRL 0x40c /* NOTE: big-endian */
35#define FSL_SOC_USB_PRICTRL 0x410 /* NOTE: big-endian */
36#define FSL_SOC_USB_CTRL 0x500 /* NOTE: big-endian */
37#endif /* _EHCI_FSL_H */
diff --git a/drivers/usb/host/ehci-hcd.c b/drivers/usb/host/ehci-hcd.c
index 9dd3d14c64f3..79f2d8b9bfb6 100644
--- a/drivers/usb/host/ehci-hcd.c
+++ b/drivers/usb/host/ehci-hcd.c
@@ -889,8 +889,19 @@ MODULE_LICENSE ("GPL");
889 889
890#ifdef CONFIG_PCI 890#ifdef CONFIG_PCI
891#include "ehci-pci.c" 891#include "ehci-pci.c"
892#define EHCI_BUS_GLUED
892#endif 893#endif
893 894
894#if !defined(CONFIG_PCI) 895#ifdef CONFIG_PPC_83xx
896#include "ehci-fsl.c"
897#define EHCI_BUS_GLUED
898#endif
899
900#ifdef CONFIG_SOC_AU1X00
901#include "ehci-au1xxx.c"
902#define EHCI_BUS_GLUED
903#endif
904
905#ifndef EHCI_BUS_GLUED
895#error "missing bus glue for ehci-hcd" 906#error "missing bus glue for ehci-hcd"
896#endif 907#endif
diff --git a/drivers/usb/host/ehci-hub.c b/drivers/usb/host/ehci-hub.c
index 69b0b9be7a64..d03e3cad5ca8 100644
--- a/drivers/usb/host/ehci-hub.c
+++ b/drivers/usb/host/ehci-hub.c
@@ -359,6 +359,8 @@ static int ehci_hub_control (
359 case USB_PORT_FEAT_SUSPEND: 359 case USB_PORT_FEAT_SUSPEND:
360 if (temp & PORT_RESET) 360 if (temp & PORT_RESET)
361 goto error; 361 goto error;
362 if (ehci->no_selective_suspend)
363 break;
362 if (temp & PORT_SUSPEND) { 364 if (temp & PORT_SUSPEND) {
363 if ((temp & PORT_PE) == 0) 365 if ((temp & PORT_PE) == 0)
364 goto error; 366 goto error;
@@ -514,6 +516,8 @@ static int ehci_hub_control (
514 temp &= ~PORT_RWC_BITS; 516 temp &= ~PORT_RWC_BITS;
515 switch (wValue) { 517 switch (wValue) {
516 case USB_PORT_FEAT_SUSPEND: 518 case USB_PORT_FEAT_SUSPEND:
519 if (ehci->no_selective_suspend)
520 break;
517 if ((temp & PORT_PE) == 0 521 if ((temp & PORT_PE) == 0
518 || (temp & PORT_RESET) != 0) 522 || (temp & PORT_RESET) != 0)
519 goto error; 523 goto error;
diff --git a/drivers/usb/host/ehci-mem.c b/drivers/usb/host/ehci-mem.c
index 91c2ab43cbcc..766061e0260a 100644
--- a/drivers/usb/host/ehci-mem.c
+++ b/drivers/usb/host/ehci-mem.c
@@ -75,7 +75,6 @@ static void qh_destroy (struct kref *kref)
75 } 75 }
76 if (qh->dummy) 76 if (qh->dummy)
77 ehci_qtd_free (ehci, qh->dummy); 77 ehci_qtd_free (ehci, qh->dummy);
78 usb_put_dev (qh->dev);
79 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma); 78 dma_pool_free (ehci->qh_pool, qh, qh->qh_dma);
80} 79}
81 80
@@ -221,13 +220,9 @@ static int ehci_mem_init (struct ehci_hcd *ehci, gfp_t flags)
221 ehci->periodic [i] = EHCI_LIST_END; 220 ehci->periodic [i] = EHCI_LIST_END;
222 221
223 /* software shadow of hardware table */ 222 /* software shadow of hardware table */
224 ehci->pshadow = kmalloc (ehci->periodic_size * sizeof (void *), flags); 223 ehci->pshadow = kcalloc(ehci->periodic_size, sizeof(void *), flags);
225 if (ehci->pshadow == NULL) { 224 if (ehci->pshadow != NULL)
226 goto fail; 225 return 0;
227 }
228 memset (ehci->pshadow, 0, ehci->periodic_size * sizeof (void *));
229
230 return 0;
231 226
232fail: 227fail:
233 ehci_dbg (ehci, "couldn't init memory\n"); 228 ehci_dbg (ehci, "couldn't init memory\n");
diff --git a/drivers/usb/host/ehci-pci.c b/drivers/usb/host/ehci-pci.c
index 3a6687df5594..1e03f1a5a5fd 100644
--- a/drivers/usb/host/ehci-pci.c
+++ b/drivers/usb/host/ehci-pci.c
@@ -106,11 +106,11 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
106 } 106 }
107 break; 107 break;
108 case PCI_VENDOR_ID_NVIDIA: 108 case PCI_VENDOR_ID_NVIDIA:
109 switch (pdev->device) {
109 /* NVidia reports that certain chips don't handle 110 /* NVidia reports that certain chips don't handle
110 * QH, ITD, or SITD addresses above 2GB. (But TD, 111 * QH, ITD, or SITD addresses above 2GB. (But TD,
111 * data buffer, and periodic schedule are normal.) 112 * data buffer, and periodic schedule are normal.)
112 */ 113 */
113 switch (pdev->device) {
114 case 0x003c: /* MCP04 */ 114 case 0x003c: /* MCP04 */
115 case 0x005b: /* CK804 */ 115 case 0x005b: /* CK804 */
116 case 0x00d8: /* CK8 */ 116 case 0x00d8: /* CK8 */
@@ -120,6 +120,14 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
120 ehci_warn(ehci, "can't enable NVidia " 120 ehci_warn(ehci, "can't enable NVidia "
121 "workaround for >2GB RAM\n"); 121 "workaround for >2GB RAM\n");
122 break; 122 break;
123 /* Some NForce2 chips have problems with selective suspend;
124 * fixed in newer silicon.
125 */
126 case 0x0068:
127 pci_read_config_dword(pdev, PCI_REVISION_ID, &temp);
128 if ((temp & 0xff) < 0xa4)
129 ehci->no_selective_suspend = 1;
130 break;
123 } 131 }
124 break; 132 break;
125 } 133 }
@@ -163,6 +171,21 @@ static int ehci_pci_setup(struct usb_hcd *hcd)
163 device_init_wakeup(&pdev->dev, 1); 171 device_init_wakeup(&pdev->dev, 1);
164 } 172 }
165 173
174#ifdef CONFIG_USB_SUSPEND
175 /* REVISIT: the controller works fine for wakeup iff the root hub
176 * itself is "globally" suspended, but usbcore currently doesn't
177 * understand such things.
178 *
179 * System suspend currently expects to be able to suspend the entire
180 * device tree, device-at-a-time. If we failed selective suspend
181 * reports, system suspend would fail; so the root hub code must claim
182 * success. That's lying to usbcore, and it matters for for runtime
183 * PM scenarios with selective suspend and remote wakeup...
184 */
185 if (ehci->no_selective_suspend && device_can_wakeup(&pdev->dev))
186 ehci_warn(ehci, "selective suspend/wakeup unavailable\n");
187#endif
188
166 retval = ehci_pci_reinit(ehci, pdev); 189 retval = ehci_pci_reinit(ehci, pdev);
167done: 190done:
168 return retval; 191 return retval;
diff --git a/drivers/usb/host/ehci-q.c b/drivers/usb/host/ehci-q.c
index 9b13bf2fa98d..e469221e7ec3 100644
--- a/drivers/usb/host/ehci-q.c
+++ b/drivers/usb/host/ehci-q.c
@@ -702,7 +702,7 @@ qh_make (
702 } 702 }
703 703
704 /* support for tt scheduling, and access to toggles */ 704 /* support for tt scheduling, and access to toggles */
705 qh->dev = usb_get_dev (urb->dev); 705 qh->dev = urb->dev;
706 706
707 /* using TT? */ 707 /* using TT? */
708 switch (urb->dev->speed) { 708 switch (urb->dev->speed) {
@@ -721,7 +721,14 @@ qh_make (
721 info1 |= maxp << 16; 721 info1 |= maxp << 16;
722 722
723 info2 |= (EHCI_TUNE_MULT_TT << 30); 723 info2 |= (EHCI_TUNE_MULT_TT << 30);
724 info2 |= urb->dev->ttport << 23; 724
725 /* Some Freescale processors have an erratum in which the
726 * port number in the queue head was 0..N-1 instead of 1..N.
727 */
728 if (ehci_has_fsl_portno_bug(ehci))
729 info2 |= (urb->dev->ttport-1) << 23;
730 else
731 info2 |= urb->dev->ttport << 23;
725 732
726 /* set the address of the TT; for TDI's integrated 733 /* set the address of the TT; for TDI's integrated
727 * root hub tt, leave it zeroed. 734 * root hub tt, leave it zeroed.
@@ -1015,12 +1022,14 @@ static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
1015 /* stop async schedule right now? */ 1022 /* stop async schedule right now? */
1016 if (unlikely (qh == ehci->async)) { 1023 if (unlikely (qh == ehci->async)) {
1017 /* can't get here without STS_ASS set */ 1024 /* can't get here without STS_ASS set */
1018 if (ehci_to_hcd(ehci)->state != HC_STATE_HALT) { 1025 if (ehci_to_hcd(ehci)->state != HC_STATE_HALT
1026 && !ehci->reclaim) {
1027 /* ... and CMD_IAAD clear */
1019 writel (cmd & ~CMD_ASE, &ehci->regs->command); 1028 writel (cmd & ~CMD_ASE, &ehci->regs->command);
1020 wmb (); 1029 wmb ();
1021 // handshake later, if we need to 1030 // handshake later, if we need to
1031 timer_action_done (ehci, TIMER_ASYNC_OFF);
1022 } 1032 }
1023 timer_action_done (ehci, TIMER_ASYNC_OFF);
1024 return; 1033 return;
1025 } 1034 }
1026 1035
diff --git a/drivers/usb/host/ehci-sched.c b/drivers/usb/host/ehci-sched.c
index ebcca9700671..5871944e6145 100644
--- a/drivers/usb/host/ehci-sched.c
+++ b/drivers/usb/host/ehci-sched.c
@@ -707,6 +707,7 @@ iso_stream_init (
707 } else { 707 } else {
708 u32 addr; 708 u32 addr;
709 int think_time; 709 int think_time;
710 int hs_transfers;
710 711
711 addr = dev->ttport << 24; 712 addr = dev->ttport << 24;
712 if (!ehci_is_TDI(ehci) 713 if (!ehci_is_TDI(ehci)
@@ -719,6 +720,7 @@ iso_stream_init (
719 think_time = dev->tt ? dev->tt->think_time : 0; 720 think_time = dev->tt ? dev->tt->think_time : 0;
720 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time ( 721 stream->tt_usecs = NS_TO_US (think_time + usb_calc_bus_time (
721 dev->speed, is_input, 1, maxp)); 722 dev->speed, is_input, 1, maxp));
723 hs_transfers = max (1u, (maxp + 187) / 188);
722 if (is_input) { 724 if (is_input) {
723 u32 tmp; 725 u32 tmp;
724 726
@@ -727,12 +729,11 @@ iso_stream_init (
727 stream->usecs = HS_USECS_ISO (1); 729 stream->usecs = HS_USECS_ISO (1);
728 stream->raw_mask = 1; 730 stream->raw_mask = 1;
729 731
730 /* pessimistic c-mask */ 732 /* c-mask as specified in USB 2.0 11.18.4 3.c */
731 tmp = usb_calc_bus_time (USB_SPEED_FULL, 1, 0, maxp) 733 tmp = (1 << (hs_transfers + 2)) - 1;
732 / (125 * 1000); 734 stream->raw_mask |= tmp << (8 + 2);
733 stream->raw_mask |= 3 << (tmp + 9);
734 } else 735 } else
735 stream->raw_mask = smask_out [maxp / 188]; 736 stream->raw_mask = smask_out [hs_transfers - 1];
736 bandwidth = stream->usecs + stream->c_usecs; 737 bandwidth = stream->usecs + stream->c_usecs;
737 bandwidth /= 1 << (interval + 2); 738 bandwidth /= 1 << (interval + 2);
738 739
@@ -863,9 +864,8 @@ iso_sched_alloc (unsigned packets, gfp_t mem_flags)
863 int size = sizeof *iso_sched; 864 int size = sizeof *iso_sched;
864 865
865 size += packets * sizeof (struct ehci_iso_packet); 866 size += packets * sizeof (struct ehci_iso_packet);
866 iso_sched = kmalloc (size, mem_flags); 867 iso_sched = kzalloc(size, mem_flags);
867 if (likely (iso_sched != NULL)) { 868 if (likely (iso_sched != NULL)) {
868 memset(iso_sched, 0, size);
869 INIT_LIST_HEAD (&iso_sched->td_list); 869 INIT_LIST_HEAD (&iso_sched->td_list);
870 } 870 }
871 return iso_sched; 871 return iso_sched;
@@ -1398,7 +1398,7 @@ itd_complete (
1398 */ 1398 */
1399 1399
1400 /* give urb back to the driver ... can be out-of-order */ 1400 /* give urb back to the driver ... can be out-of-order */
1401 dev = usb_get_dev (urb->dev); 1401 dev = urb->dev;
1402 ehci_urb_done (ehci, urb, regs); 1402 ehci_urb_done (ehci, urb, regs);
1403 urb = NULL; 1403 urb = NULL;
1404 1404
@@ -1417,7 +1417,6 @@ itd_complete (
1417 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); 1417 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1418 } 1418 }
1419 iso_stream_put (ehci, stream); 1419 iso_stream_put (ehci, stream);
1420 usb_put_dev (dev);
1421 1420
1422 return 1; 1421 return 1;
1423} 1422}
@@ -1764,7 +1763,7 @@ sitd_complete (
1764 */ 1763 */
1765 1764
1766 /* give urb back to the driver */ 1765 /* give urb back to the driver */
1767 dev = usb_get_dev (urb->dev); 1766 dev = urb->dev;
1768 ehci_urb_done (ehci, urb, regs); 1767 ehci_urb_done (ehci, urb, regs);
1769 urb = NULL; 1768 urb = NULL;
1770 1769
@@ -1783,7 +1782,6 @@ sitd_complete (
1783 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); 1782 (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out");
1784 } 1783 }
1785 iso_stream_put (ehci, stream); 1784 iso_stream_put (ehci, stream);
1786 usb_put_dev (dev);
1787 1785
1788 return 1; 1786 return 1;
1789} 1787}
diff --git a/drivers/usb/host/ehci.h b/drivers/usb/host/ehci.h
index 18e257c2bdb5..679c1cdcc915 100644
--- a/drivers/usb/host/ehci.h
+++ b/drivers/usb/host/ehci.h
@@ -88,7 +88,12 @@ struct ehci_hcd { /* one per controller */
88 unsigned long next_statechange; 88 unsigned long next_statechange;
89 u32 command; 89 u32 command;
90 90
91 /* SILICON QUIRKS */
91 unsigned is_tdi_rh_tt:1; /* TDI roothub with TT */ 92 unsigned is_tdi_rh_tt:1; /* TDI roothub with TT */
93 unsigned no_selective_suspend:1;
94 unsigned has_fsl_port_bug:1; /* FreeScale */
95
96 u8 sbrn; /* packed release number */
92 97
93 /* irq statistics */ 98 /* irq statistics */
94#ifdef EHCI_STATS 99#ifdef EHCI_STATS
@@ -97,7 +102,6 @@ struct ehci_hcd { /* one per controller */
97#else 102#else
98# define COUNT(x) do {} while (0) 103# define COUNT(x) do {} while (0)
99#endif 104#endif
100 u8 sbrn; /* packed release number */
101}; 105};
102 106
103/* convert between an HCD pointer and the corresponding EHCI_HCD */ 107/* convert between an HCD pointer and the corresponding EHCI_HCD */
@@ -638,6 +642,18 @@ ehci_port_speed(struct ehci_hcd *ehci, unsigned int portsc)
638 642
639/*-------------------------------------------------------------------------*/ 643/*-------------------------------------------------------------------------*/
640 644
645#ifdef CONFIG_PPC_83xx
646/* Some Freescale processors have an erratum in which the TT
647 * port number in the queue head was 0..N-1 instead of 1..N.
648 */
649#define ehci_has_fsl_portno_bug(e) ((e)->has_fsl_port_bug)
650#else
651#define ehci_has_fsl_portno_bug(e) (0)
652#endif
653
654
655/*-------------------------------------------------------------------------*/
656
641#ifndef DEBUG 657#ifndef DEBUG
642#define STUB_DEBUG_FILES 658#define STUB_DEBUG_FILES
643#endif /* DEBUG */ 659#endif /* DEBUG */
diff --git a/drivers/usb/host/hc_crisv10.c b/drivers/usb/host/hc_crisv10.c
index 641268d7e6f3..2fe7fd19437b 100644
--- a/drivers/usb/host/hc_crisv10.c
+++ b/drivers/usb/host/hc_crisv10.c
@@ -2137,10 +2137,9 @@ static int etrax_usb_submit_bulk_urb(struct urb *urb)
2137 urb->status = -EINPROGRESS; 2137 urb->status = -EINPROGRESS;
2138 2138
2139 /* Setup the hcpriv data. */ 2139 /* Setup the hcpriv data. */
2140 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2140 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2141 assert(urb_priv != NULL); 2141 assert(urb_priv != NULL);
2142 /* This sets rx_offset to 0. */ 2142 /* This sets rx_offset to 0. */
2143 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2144 urb_priv->urb_state = NOT_STARTED; 2143 urb_priv->urb_state = NOT_STARTED;
2145 urb->hcpriv = urb_priv; 2144 urb->hcpriv = urb_priv;
2146 2145
@@ -2475,10 +2474,9 @@ static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2475 urb->status = -EINPROGRESS; 2474 urb->status = -EINPROGRESS;
2476 2475
2477 /* Setup the hcpriv data. */ 2476 /* Setup the hcpriv data. */
2478 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2477 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2479 assert(urb_priv != NULL); 2478 assert(urb_priv != NULL);
2480 /* This sets rx_offset to 0. */ 2479 /* This sets rx_offset to 0. */
2481 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2482 urb_priv->urb_state = NOT_STARTED; 2480 urb_priv->urb_state = NOT_STARTED;
2483 urb->hcpriv = urb_priv; 2481 urb->hcpriv = urb_priv;
2484 2482
@@ -2767,9 +2765,8 @@ static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2767 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 2765 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2768 interval = urb->interval; 2766 interval = urb->interval;
2769 2767
2770 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG); 2768 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2771 assert(urb_priv != NULL); 2769 assert(urb_priv != NULL);
2772 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2773 urb->hcpriv = urb_priv; 2770 urb->hcpriv = urb_priv;
2774 2771
2775 first_ep = &TxIntrEPList[0]; 2772 first_ep = &TxIntrEPList[0];
@@ -2997,9 +2994,8 @@ static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
2997 2994
2998 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL; 2995 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
2999 2996
3000 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC); 2997 urb_priv = kzalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
3001 assert(urb_priv != NULL); 2998 assert(urb_priv != NULL);
3002 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
3003 2999
3004 urb->hcpriv = urb_priv; 3000 urb->hcpriv = urb_priv;
3005 urb_priv->epid = epid; 3001 urb_priv->epid = epid;
diff --git a/drivers/usb/host/isp116x-hcd.c b/drivers/usb/host/isp116x-hcd.c
index 972ce04889f8..e99210b7909b 100644
--- a/drivers/usb/host/isp116x-hcd.c
+++ b/drivers/usb/host/isp116x-hcd.c
@@ -724,7 +724,7 @@ static int isp116x_urb_enqueue(struct usb_hcd *hcd,
724 ep = hep->hcpriv; 724 ep = hep->hcpriv;
725 else { 725 else {
726 INIT_LIST_HEAD(&ep->schedule); 726 INIT_LIST_HEAD(&ep->schedule);
727 ep->udev = usb_get_dev(udev); 727 ep->udev = udev;
728 ep->epnum = epnum; 728 ep->epnum = epnum;
729 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out); 729 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
730 usb_settoggle(udev, epnum, is_out, 0); 730 usb_settoggle(udev, epnum, is_out, 0);
@@ -891,7 +891,6 @@ static void isp116x_endpoint_disable(struct usb_hcd *hcd,
891 if (!list_empty(&hep->urb_list)) 891 if (!list_empty(&hep->urb_list))
892 WARN("ep %p not empty?\n", ep); 892 WARN("ep %p not empty?\n", ep);
893 893
894 usb_put_dev(ep->udev);
895 kfree(ep); 894 kfree(ep);
896 hep->hcpriv = NULL; 895 hep->hcpriv = NULL;
897} 896}
@@ -1553,7 +1552,7 @@ static struct hc_driver isp116x_hc_driver = {
1553 1552
1554/*----------------------------------------------------------------*/ 1553/*----------------------------------------------------------------*/
1555 1554
1556static int __init_or_module isp116x_remove(struct platform_device *pdev) 1555static int isp116x_remove(struct platform_device *pdev)
1557{ 1556{
1558 struct usb_hcd *hcd = platform_get_drvdata(pdev); 1557 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1559 struct isp116x *isp116x; 1558 struct isp116x *isp116x;
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
new file mode 100644
index 000000000000..980030d684d5
--- /dev/null
+++ b/drivers/usb/host/ohci-at91.c
@@ -0,0 +1,306 @@
1/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * Copyright (C) 2004 SAN People (Pty) Ltd.
5 * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
6 *
7 * AT91RM9200 Bus Glue
8 *
9 * Based on fragments of 2.4 driver by Rick Bronson.
10 * Based on ohci-omap.c
11 *
12 * This file is licenced under the GPL.
13 */
14
15#include <linux/clk.h>
16#include <linux/platform_device.h>
17
18#include <asm/mach-types.h>
19#include <asm/hardware.h>
20#include <asm/arch/board.h>
21
22#ifndef CONFIG_ARCH_AT91RM9200
23#error "This file is AT91RM9200 bus glue. CONFIG_ARCH_AT91RM9200 must be defined."
24#endif
25
26/* interface and function clocks */
27static struct clk *iclk, *fclk;
28
29extern int usb_disabled(void);
30
31/*-------------------------------------------------------------------------*/
32
33static void at91_start_hc(struct platform_device *pdev)
34{
35 struct usb_hcd *hcd = platform_get_drvdata(pdev);
36 struct ohci_regs __iomem *regs = hcd->regs;
37
38 dev_dbg(&pdev->dev, "starting AT91RM9200 OHCI USB Controller\n");
39
40 /*
41 * Start the USB clocks.
42 */
43 clk_enable(iclk);
44 clk_enable(fclk);
45
46 /*
47 * The USB host controller must remain in reset.
48 */
49 writel(0, &regs->control);
50}
51
52static void at91_stop_hc(struct platform_device *pdev)
53{
54 struct usb_hcd *hcd = platform_get_drvdata(pdev);
55 struct ohci_regs __iomem *regs = hcd->regs;
56
57 dev_dbg(&pdev->dev, "stopping AT91RM9200 OHCI USB Controller\n");
58
59 /*
60 * Put the USB host controller into reset.
61 */
62 writel(0, &regs->control);
63
64 /*
65 * Stop the USB clocks.
66 */
67 clk_disable(fclk);
68 clk_disable(iclk);
69}
70
71
72/*-------------------------------------------------------------------------*/
73
74static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
75
76/* configure so an HC device and id are always provided */
77/* always called with process context; sleeping is OK */
78
79
80/**
81 * usb_hcd_at91_probe - initialize AT91RM9200-based HCDs
82 * Context: !in_interrupt()
83 *
84 * Allocates basic resources for this USB host controller, and
85 * then invokes the start() method for the HCD associated with it
86 * through the hotplug entry's driver_data.
87 *
88 * Store this function in the HCD's struct pci_driver as probe().
89 */
90int usb_hcd_at91_probe (const struct hc_driver *driver, struct platform_device *pdev)
91{
92 int retval;
93 struct usb_hcd *hcd = NULL;
94
95 if (pdev->num_resources != 2) {
96 pr_debug("hcd probe: invalid num_resources");
97 return -ENODEV;
98 }
99
100 if ((pdev->resource[0].flags != IORESOURCE_MEM) || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
101 pr_debug("hcd probe: invalid resource type\n");
102 return -ENODEV;
103 }
104
105 hcd = usb_create_hcd(driver, &pdev->dev, "at91rm9200");
106 if (!hcd)
107 return -ENOMEM;
108 hcd->rsrc_start = pdev->resource[0].start;
109 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
110
111 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
112 pr_debug("request_mem_region failed\n");
113 retval = -EBUSY;
114 goto err1;
115 }
116
117 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
118 if (!hcd->regs) {
119 pr_debug("ioremap failed\n");
120 retval = -EIO;
121 goto err2;
122 }
123
124 iclk = clk_get(&pdev->dev, "ohci_clk");
125 fclk = clk_get(&pdev->dev, "uhpck");
126
127 at91_start_hc(pdev);
128 ohci_hcd_init(hcd_to_ohci(hcd));
129
130 retval = usb_add_hcd(hcd, pdev->resource[1].start, SA_INTERRUPT);
131 if (retval == 0)
132 return retval;
133
134 /* Error handling */
135 at91_stop_hc(pdev);
136
137 clk_put(fclk);
138 clk_put(iclk);
139
140 iounmap(hcd->regs);
141
142 err2:
143 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
144
145 err1:
146 usb_put_hcd(hcd);
147 return retval;
148}
149
150
151/* may be called without controller electrically present */
152/* may be called with controller, bus, and devices active */
153
154/**
155 * usb_hcd_at91_remove - shutdown processing for AT91RM9200-based HCDs
156 * @dev: USB Host Controller being removed
157 * Context: !in_interrupt()
158 *
159 * Reverses the effect of usb_hcd_at91_probe(), first invoking
160 * the HCD's stop() method. It is always called from a thread
161 * context, normally "rmmod", "apmd", or something similar.
162 *
163 */
164static int usb_hcd_at91_remove (struct usb_hcd *hcd, struct platform_device *pdev)
165{
166 usb_remove_hcd(hcd);
167 at91_stop_hc(pdev);
168 iounmap(hcd->regs);
169 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
170
171 clk_put(fclk);
172 clk_put(iclk);
173 fclk = iclk = NULL;
174
175 dev_set_drvdata(&pdev->dev, NULL);
176 return 0;
177}
178
179/*-------------------------------------------------------------------------*/
180
181static int __devinit
182ohci_at91_start (struct usb_hcd *hcd)
183{
184// struct at91_ohci_data *board = hcd->self.controller->platform_data;
185 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
186 int ret;
187
188 if ((ret = ohci_init(ohci)) < 0)
189 return ret;
190
191 if ((ret = ohci_run(ohci)) < 0) {
192 err("can't start %s", hcd->self.bus_name);
193 ohci_stop(hcd);
194 return ret;
195 }
196// hcd->self.root_hub->maxchild = board->ports;
197 return 0;
198}
199
200/*-------------------------------------------------------------------------*/
201
202static const struct hc_driver ohci_at91_hc_driver = {
203 .description = hcd_name,
204 .product_desc = "AT91RM9200 OHCI",
205 .hcd_priv_size = sizeof(struct ohci_hcd),
206
207 /*
208 * generic hardware linkage
209 */
210 .irq = ohci_irq,
211 .flags = HCD_USB11 | HCD_MEMORY,
212
213 /*
214 * basic lifecycle operations
215 */
216 .start = ohci_at91_start,
217 .stop = ohci_stop,
218
219 /*
220 * managing i/o requests and associated device resources
221 */
222 .urb_enqueue = ohci_urb_enqueue,
223 .urb_dequeue = ohci_urb_dequeue,
224 .endpoint_disable = ohci_endpoint_disable,
225
226 /*
227 * scheduling support
228 */
229 .get_frame_number = ohci_get_frame,
230
231 /*
232 * root hub support
233 */
234 .hub_status_data = ohci_hub_status_data,
235 .hub_control = ohci_hub_control,
236
237#ifdef CONFIG_PM
238 .hub_suspend = ohci_hub_suspend,
239 .hub_resume = ohci_hub_resume,
240#endif
241 .start_port_reset = ohci_start_port_reset,
242};
243
244/*-------------------------------------------------------------------------*/
245
246static int ohci_hcd_at91_drv_probe(struct platform_device *dev)
247{
248 return usb_hcd_at91_probe(&ohci_at91_hc_driver, dev);
249}
250
251static int ohci_hcd_at91_drv_remove(struct platform_device *dev)
252{
253 return usb_hcd_at91_remove(platform_get_drvdata(dev), dev);
254}
255
256#ifdef CONFIG_PM
257static int ohci_hcd_at91_drv_suspend(struct platform_device *dev, u32 state, u32 level)
258{
259 printk("%s(%s:%d): not implemented yet\n",
260 __func__, __FILE__, __LINE__);
261
262 clk_disable(fclk);
263
264 return 0;
265}
266
267static int ohci_hcd_at91_drv_resume(struct platform_device *dev, u32 state)
268{
269 printk("%s(%s:%d): not implemented yet\n",
270 __func__, __FILE__, __LINE__);
271
272 clk_enable(fclk);
273
274 return 0;
275}
276#else
277#define ohci_hcd_at91_drv_suspend NULL
278#define ohci_hcd_at91_drv_resume NULL
279#endif
280
281static struct platform_driver ohci_hcd_at91_driver = {
282 .probe = ohci_hcd_at91_drv_probe,
283 .remove = ohci_hcd_at91_drv_remove,
284 .suspend = ohci_hcd_at91_drv_suspend,
285 .resume = ohci_hcd_at91_drv_resume,
286 .driver = {
287 .name = "at91rm9200-ohci",
288 .owner = THIS_MODULE,
289 },
290};
291
292static int __init ohci_hcd_at91_init (void)
293{
294 if (usb_disabled())
295 return -ENODEV;
296
297 return platform_driver_register(&ohci_hcd_at91_driver);
298}
299
300static void __exit ohci_hcd_at91_cleanup (void)
301{
302 platform_driver_unregister(&ohci_hcd_at91_driver);
303}
304
305module_init (ohci_hcd_at91_init);
306module_exit (ohci_hcd_at91_cleanup);
diff --git a/drivers/usb/host/ohci-au1xxx.c b/drivers/usb/host/ohci-au1xxx.c
index db280ca7b7a0..a1c8b3b2fcc7 100644
--- a/drivers/usb/host/ohci-au1xxx.c
+++ b/drivers/usb/host/ohci-au1xxx.c
@@ -23,6 +23,8 @@
23 23
24#include <asm/mach-au1x00/au1000.h> 24#include <asm/mach-au1x00/au1000.h>
25 25
26#ifndef CONFIG_SOC_AU1200
27
26#define USBH_ENABLE_BE (1<<0) 28#define USBH_ENABLE_BE (1<<0)
27#define USBH_ENABLE_C (1<<1) 29#define USBH_ENABLE_C (1<<1)
28#define USBH_ENABLE_E (1<<2) 30#define USBH_ENABLE_E (1<<2)
@@ -37,21 +39,68 @@
37#error not byte order defined 39#error not byte order defined
38#endif 40#endif
39 41
42#else /* Au1200 */
43
44#define USB_HOST_CONFIG (USB_MSR_BASE + USB_MSR_MCFG)
45#define USB_MCFG_PFEN (1<<31)
46#define USB_MCFG_RDCOMB (1<<30)
47#define USB_MCFG_SSDEN (1<<23)
48#define USB_MCFG_OHCCLKEN (1<<16)
49#define USB_MCFG_UCAM (1<<7)
50#define USB_MCFG_OBMEN (1<<1)
51#define USB_MCFG_OMEMEN (1<<0)
52
53#define USBH_ENABLE_CE USB_MCFG_OHCCLKEN
54#ifdef CONFIG_DMA_COHERENT
55#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \
56 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
57 | USB_MCFG_SSDEN | USB_MCFG_UCAM \
58 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
59#else
60#define USBH_ENABLE_INIT (USB_MCFG_OHCCLKEN \
61 | USB_MCFG_PFEN | USB_MCFG_RDCOMB \
62 | USB_MCFG_SSDEN \
63 | USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
64#endif
65#define USBH_DISABLE (USB_MCFG_OBMEN | USB_MCFG_OMEMEN)
66
67#endif /* Au1200 */
68
40extern int usb_disabled(void); 69extern int usb_disabled(void);
41 70
42/*-------------------------------------------------------------------------*/ 71/*-------------------------------------------------------------------------*/
43 72
44static void au1xxx_start_hc(struct platform_device *dev) 73static void au1xxx_start_ohc(struct platform_device *dev)
45{ 74{
46 printk(KERN_DEBUG __FILE__ 75 printk(KERN_DEBUG __FILE__
47 ": starting Au1xxx OHCI USB Controller\n"); 76 ": starting Au1xxx OHCI USB Controller\n");
48 77
49 /* enable host controller */ 78 /* enable host controller */
79
80#ifndef CONFIG_SOC_AU1200
81
50 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG); 82 au_writel(USBH_ENABLE_CE, USB_HOST_CONFIG);
51 udelay(1000); 83 udelay(1000);
52 au_writel(USBH_ENABLE_INIT, USB_HOST_CONFIG); 84 au_writel(USBH_ENABLE_INIT, USB_HOST_CONFIG);
53 udelay(1000); 85 udelay(1000);
54 86
87#else /* Au1200 */
88
89 /* write HW defaults again in case Yamon cleared them */
90 if (au_readl(USB_HOST_CONFIG) == 0) {
91 au_writel(0x00d02000, USB_HOST_CONFIG);
92 au_readl(USB_HOST_CONFIG);
93 udelay(1000);
94 }
95 au_writel(USBH_ENABLE_CE | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
96 au_readl(USB_HOST_CONFIG);
97 udelay(1000);
98 au_writel(USBH_ENABLE_INIT | au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
99 au_readl(USB_HOST_CONFIG);
100 udelay(1000);
101
102#endif /* Au1200 */
103
55 /* wait for reset complete (read register twice; see au1500 errata) */ 104 /* wait for reset complete (read register twice; see au1500 errata) */
56 while (au_readl(USB_HOST_CONFIG), 105 while (au_readl(USB_HOST_CONFIG),
57 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD)) 106 !(au_readl(USB_HOST_CONFIG) & USBH_ENABLE_RD))
@@ -61,13 +110,25 @@ static void au1xxx_start_hc(struct platform_device *dev)
61 ": Clock to USB host has been enabled \n"); 110 ": Clock to USB host has been enabled \n");
62} 111}
63 112
64static void au1xxx_stop_hc(struct platform_device *dev) 113static void au1xxx_stop_ohc(struct platform_device *dev)
65{ 114{
66 printk(KERN_DEBUG __FILE__ 115 printk(KERN_DEBUG __FILE__
67 ": stopping Au1xxx OHCI USB Controller\n"); 116 ": stopping Au1xxx OHCI USB Controller\n");
68 117
118#ifndef CONFIG_SOC_AU1200
119
69 /* Disable clock */ 120 /* Disable clock */
70 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG); 121 au_writel(au_readl(USB_HOST_CONFIG) & ~USBH_ENABLE_CE, USB_HOST_CONFIG);
122
123#else /* Au1200 */
124
125 /* Disable mem */
126 au_writel(~USBH_DISABLE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
127 udelay(1000);
128 /* Disable clock */
129 au_writel(~USBH_ENABLE_CE & au_readl(USB_HOST_CONFIG), USB_HOST_CONFIG);
130 au_readl(USB_HOST_CONFIG);
131#endif /* Au1200 */
71} 132}
72 133
73 134
@@ -78,7 +139,7 @@ static void au1xxx_stop_hc(struct platform_device *dev)
78 139
79 140
80/** 141/**
81 * usb_hcd_au1xxx_probe - initialize Au1xxx-based HCDs 142 * usb_ohci_au1xxx_probe - initialize Au1xxx-based HCDs
82 * Context: !in_interrupt() 143 * Context: !in_interrupt()
83 * 144 *
84 * Allocates basic resources for this USB host controller, and 145 * Allocates basic resources for this USB host controller, and
@@ -86,14 +147,25 @@ static void au1xxx_stop_hc(struct platform_device *dev)
86 * through the hotplug entry's driver_data. 147 * through the hotplug entry's driver_data.
87 * 148 *
88 */ 149 */
89int usb_hcd_au1xxx_probe (const struct hc_driver *driver, 150static int usb_ohci_au1xxx_probe(const struct hc_driver *driver,
90 struct platform_device *dev) 151 struct platform_device *dev)
91{ 152{
92 int retval; 153 int retval;
93 struct usb_hcd *hcd; 154 struct usb_hcd *hcd;
94 155
95 if(dev->resource[1].flags != IORESOURCE_IRQ) { 156#if defined(CONFIG_SOC_AU1200) && defined(CONFIG_DMA_COHERENT)
96 pr_debug ("resource[1] is not IORESOURCE_IRQ"); 157 /* Au1200 AB USB does not support coherent memory */
158 if (!(read_c0_prid() & 0xff)) {
159 pr_info("%s: this is chip revision AB !!\n",
160 dev->dev.name);
161 pr_info("%s: update your board or re-configure the kernel\n",
162 dev->dev.name);
163 return -ENODEV;
164 }
165#endif
166
167 if (dev->resource[1].flags != IORESOURCE_IRQ) {
168 pr_debug("resource[1] is not IORESOURCE_IRQ\n");
97 return -ENOMEM; 169 return -ENOMEM;
98 } 170 }
99 171
@@ -104,26 +176,26 @@ int usb_hcd_au1xxx_probe (const struct hc_driver *driver,
104 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1; 176 hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
105 177
106 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { 178 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
107 pr_debug("request_mem_region failed"); 179 pr_debug("request_mem_region failed\n");
108 retval = -EBUSY; 180 retval = -EBUSY;
109 goto err1; 181 goto err1;
110 } 182 }
111 183
112 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); 184 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
113 if (!hcd->regs) { 185 if (!hcd->regs) {
114 pr_debug("ioremap failed"); 186 pr_debug("ioremap failed\n");
115 retval = -ENOMEM; 187 retval = -ENOMEM;
116 goto err2; 188 goto err2;
117 } 189 }
118 190
119 au1xxx_start_hc(dev); 191 au1xxx_start_ohc(dev);
120 ohci_hcd_init(hcd_to_ohci(hcd)); 192 ohci_hcd_init(hcd_to_ohci(hcd));
121 193
122 retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT); 194 retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT | SA_SHIRQ);
123 if (retval == 0) 195 if (retval == 0)
124 return retval; 196 return retval;
125 197
126 au1xxx_stop_hc(dev); 198 au1xxx_stop_ohc(dev);
127 iounmap(hcd->regs); 199 iounmap(hcd->regs);
128 err2: 200 err2:
129 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 201 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
@@ -146,10 +218,10 @@ int usb_hcd_au1xxx_probe (const struct hc_driver *driver,
146 * context, normally "rmmod", "apmd", or something similar. 218 * context, normally "rmmod", "apmd", or something similar.
147 * 219 *
148 */ 220 */
149void usb_hcd_au1xxx_remove (struct usb_hcd *hcd, struct platform_device *dev) 221static void usb_ohci_au1xxx_remove(struct usb_hcd *hcd, struct platform_device *dev)
150{ 222{
151 usb_remove_hcd(hcd); 223 usb_remove_hcd(hcd);
152 au1xxx_stop_hc(dev); 224 au1xxx_stop_ohc(dev);
153 iounmap(hcd->regs); 225 iounmap(hcd->regs);
154 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 226 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
155 usb_put_hcd(hcd); 227 usb_put_hcd(hcd);
@@ -235,7 +307,7 @@ static int ohci_hcd_au1xxx_drv_probe(struct platform_device *pdev)
235 if (usb_disabled()) 307 if (usb_disabled())
236 return -ENODEV; 308 return -ENODEV;
237 309
238 ret = usb_hcd_au1xxx_probe(&ohci_au1xxx_hc_driver, pdev); 310 ret = usb_ohci_au1xxx_probe(&ohci_au1xxx_hc_driver, pdev);
239 return ret; 311 return ret;
240} 312}
241 313
@@ -243,7 +315,7 @@ static int ohci_hcd_au1xxx_drv_remove(struct platform_device *pdev)
243{ 315{
244 struct usb_hcd *hcd = platform_get_drvdata(pdev); 316 struct usb_hcd *hcd = platform_get_drvdata(pdev);
245 317
246 usb_hcd_au1xxx_remove(hcd, pdev); 318 usb_ohci_au1xxx_remove(hcd, pdev);
247 return 0; 319 return 0;
248} 320}
249 /*TBD*/ 321 /*TBD*/
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index a4b12404ae08..544f7589912f 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -443,11 +443,16 @@ ohci_reboot (struct notifier_block *block, unsigned long code, void *null)
443static int ohci_init (struct ohci_hcd *ohci) 443static int ohci_init (struct ohci_hcd *ohci)
444{ 444{
445 int ret; 445 int ret;
446 struct usb_hcd *hcd = ohci_to_hcd(ohci);
446 447
447 disable (ohci); 448 disable (ohci);
448 ohci->regs = ohci_to_hcd(ohci)->regs; 449 ohci->regs = hcd->regs;
449 ohci->next_statechange = jiffies; 450 ohci->next_statechange = jiffies;
450 451
452 /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
453 * was never needed for most non-PCI systems ... remove the code?
454 */
455
451#ifndef IR_DISABLE 456#ifndef IR_DISABLE
452 /* SMM owns the HC? not for long! */ 457 /* SMM owns the HC? not for long! */
453 if (!no_handshake && ohci_readl (ohci, 458 if (!no_handshake && ohci_readl (ohci,
@@ -478,8 +483,10 @@ static int ohci_init (struct ohci_hcd *ohci)
478 483
479 /* Disable HC interrupts */ 484 /* Disable HC interrupts */
480 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable); 485 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
481 // flush the writes 486
482 (void) ohci_readl (ohci, &ohci->regs->control); 487 /* flush the writes, and save key bits like RWC */
488 if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
489 ohci->hc_control |= OHCI_CTRL_RWC;
483 490
484 /* Read the number of ports unless overridden */ 491 /* Read the number of ports unless overridden */
485 if (ohci->num_ports == 0) 492 if (ohci->num_ports == 0)
@@ -488,16 +495,19 @@ static int ohci_init (struct ohci_hcd *ohci)
488 if (ohci->hcca) 495 if (ohci->hcca)
489 return 0; 496 return 0;
490 497
491 ohci->hcca = dma_alloc_coherent (ohci_to_hcd(ohci)->self.controller, 498 ohci->hcca = dma_alloc_coherent (hcd->self.controller,
492 sizeof *ohci->hcca, &ohci->hcca_dma, 0); 499 sizeof *ohci->hcca, &ohci->hcca_dma, 0);
493 if (!ohci->hcca) 500 if (!ohci->hcca)
494 return -ENOMEM; 501 return -ENOMEM;
495 502
496 if ((ret = ohci_mem_init (ohci)) < 0) 503 if ((ret = ohci_mem_init (ohci)) < 0)
497 ohci_stop (ohci_to_hcd(ohci)); 504 ohci_stop (hcd);
505 else {
506 register_reboot_notifier (&ohci->reboot_notifier);
507 create_debug_files (ohci);
508 }
498 509
499 return ret; 510 return ret;
500
501} 511}
502 512
503/*-------------------------------------------------------------------------*/ 513/*-------------------------------------------------------------------------*/
@@ -510,6 +520,7 @@ static int ohci_run (struct ohci_hcd *ohci)
510{ 520{
511 u32 mask, temp; 521 u32 mask, temp;
512 int first = ohci->fminterval == 0; 522 int first = ohci->fminterval == 0;
523 struct usb_hcd *hcd = ohci_to_hcd(ohci);
513 524
514 disable (ohci); 525 disable (ohci);
515 526
@@ -525,18 +536,17 @@ static int ohci_run (struct ohci_hcd *ohci)
525 /* also: power/overcurrent flags in roothub.a */ 536 /* also: power/overcurrent flags in roothub.a */
526 } 537 }
527 538
528 /* Reset USB nearly "by the book". RemoteWakeupConnected 539 /* Reset USB nearly "by the book". RemoteWakeupConnected was
529 * saved if boot firmware (BIOS/SMM/...) told us it's connected 540 * saved if boot firmware (BIOS/SMM/...) told us it's connected,
530 * (for OHCI integrated on mainboard, it normally is) 541 * or if bus glue did the same (e.g. for PCI add-in cards with
542 * PCI PM support).
531 */ 543 */
532 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
533 ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n", 544 ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n",
534 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS), 545 hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
535 ohci->hc_control); 546 ohci_readl (ohci, &ohci->regs->control));
536 547 if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
537 if (ohci->hc_control & OHCI_CTRL_RWC 548 && !device_may_wakeup(hcd->self.controller))
538 && !(ohci->flags & OHCI_QUIRK_AMD756)) 549 device_init_wakeup(hcd->self.controller, 1);
539 ohci_to_hcd(ohci)->can_wakeup = 1;
540 550
541 switch (ohci->hc_control & OHCI_CTRL_HCFS) { 551 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
542 case OHCI_USB_OPER: 552 case OHCI_USB_OPER:
@@ -632,7 +642,7 @@ retry:
632 ohci->hc_control &= OHCI_CTRL_RWC; 642 ohci->hc_control &= OHCI_CTRL_RWC;
633 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER; 643 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
634 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control); 644 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
635 ohci_to_hcd(ohci)->state = HC_STATE_RUNNING; 645 hcd->state = HC_STATE_RUNNING;
636 646
637 /* wake on ConnectStatusChange, matching external hubs */ 647 /* wake on ConnectStatusChange, matching external hubs */
638 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status); 648 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
@@ -667,15 +677,10 @@ retry:
667 677
668 // POTPGT delay is bits 24-31, in 2 ms units. 678 // POTPGT delay is bits 24-31, in 2 ms units.
669 mdelay ((temp >> 23) & 0x1fe); 679 mdelay ((temp >> 23) & 0x1fe);
670 ohci_to_hcd(ohci)->state = HC_STATE_RUNNING; 680 hcd->state = HC_STATE_RUNNING;
671 681
672 ohci_dump (ohci, 1); 682 ohci_dump (ohci, 1);
673 683
674 if (ohci_to_hcd(ohci)->self.root_hub == NULL) {
675 register_reboot_notifier (&ohci->reboot_notifier);
676 create_debug_files (ohci);
677 }
678
679 return 0; 684 return 0;
680} 685}
681 686
@@ -905,6 +910,10 @@ MODULE_LICENSE ("GPL");
905#include "ohci-ppc-soc.c" 910#include "ohci-ppc-soc.c"
906#endif 911#endif
907 912
913#ifdef CONFIG_ARCH_AT91RM9200
914#include "ohci-at91.c"
915#endif
916
908#if !(defined(CONFIG_PCI) \ 917#if !(defined(CONFIG_PCI) \
909 || defined(CONFIG_SA1111) \ 918 || defined(CONFIG_SA1111) \
910 || defined(CONFIG_ARCH_S3C2410) \ 919 || defined(CONFIG_ARCH_S3C2410) \
@@ -913,6 +922,7 @@ MODULE_LICENSE ("GPL");
913 || defined (CONFIG_PXA27x) \ 922 || defined (CONFIG_PXA27x) \
914 || defined (CONFIG_SOC_AU1X00) \ 923 || defined (CONFIG_SOC_AU1X00) \
915 || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \ 924 || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \
925 || defined (CONFIG_ARCH_AT91RM9200) \
916 ) 926 )
917#error "missing bus glue for ohci-hcd" 927#error "missing bus glue for ohci-hcd"
918#endif 928#endif
diff --git a/drivers/usb/host/ohci-hub.c b/drivers/usb/host/ohci-hub.c
index 4b2226d77b34..0bb972b58336 100644
--- a/drivers/usb/host/ohci-hub.c
+++ b/drivers/usb/host/ohci-hub.c
@@ -107,7 +107,7 @@ static int ohci_bus_suspend (struct usb_hcd *hcd)
107 &ohci->regs->intrstatus); 107 &ohci->regs->intrstatus);
108 108
109 /* maybe resume can wake root hub */ 109 /* maybe resume can wake root hub */
110 if (hcd->remote_wakeup) 110 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
111 ohci->hc_control |= OHCI_CTRL_RWE; 111 ohci->hc_control |= OHCI_CTRL_RWE;
112 else 112 else
113 ohci->hc_control &= ~OHCI_CTRL_RWE; 113 ohci->hc_control &= ~OHCI_CTRL_RWE;
@@ -246,9 +246,9 @@ static int ohci_bus_resume (struct usb_hcd *hcd)
246 (void) ohci_readl (ohci, &ohci->regs->control); 246 (void) ohci_readl (ohci, &ohci->regs->control);
247 msleep (3); 247 msleep (3);
248 248
249 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER; 249 temp = ohci->hc_control;
250 if (hcd->can_wakeup) 250 temp &= OHCI_CTRL_RWC;
251 temp |= OHCI_CTRL_RWC; 251 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
252 ohci->hc_control = temp; 252 ohci->hc_control = temp;
253 ohci_writel (ohci, temp, &ohci->regs->control); 253 ohci_writel (ohci, temp, &ohci->regs->control);
254 (void) ohci_readl (ohci, &ohci->regs->control); 254 (void) ohci_readl (ohci, &ohci->regs->control);
@@ -302,7 +302,7 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
302{ 302{
303 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 303 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
304 int i, changed = 0, length = 1; 304 int i, changed = 0, length = 1;
305 int can_suspend = hcd->can_wakeup; 305 int can_suspend = device_may_wakeup(&hcd->self.root_hub->dev);
306 unsigned long flags; 306 unsigned long flags;
307 307
308 spin_lock_irqsave (&ohci->lock, flags); 308 spin_lock_irqsave (&ohci->lock, flags);
@@ -354,7 +354,7 @@ ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
354 */ 354 */
355 if (!(status & RH_PS_CCS)) 355 if (!(status & RH_PS_CCS))
356 continue; 356 continue;
357 if ((status & RH_PS_PSS) && hcd->remote_wakeup) 357 if ((status & RH_PS_PSS) && can_suspend)
358 continue; 358 continue;
359 can_suspend = 0; 359 can_suspend = 0;
360 } 360 }
diff --git a/drivers/usb/host/ohci-pci.c b/drivers/usb/host/ohci-pci.c
index 1b09dde068e1..1bfe96f4d045 100644
--- a/drivers/usb/host/ohci-pci.c
+++ b/drivers/usb/host/ohci-pci.c
@@ -35,7 +35,10 @@ ohci_pci_start (struct usb_hcd *hcd)
35 struct ohci_hcd *ohci = hcd_to_ohci (hcd); 35 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
36 int ret; 36 int ret;
37 37
38 if(hcd->self.controller && hcd->self.controller->bus == &pci_bus_type) { 38 /* REVISIT this whole block should move to reset(), which handles
39 * all the other one-time init.
40 */
41 if (hcd->self.controller) {
39 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 42 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
40 43
41 /* AMD 756, for most chips (early revs), corrupts register 44 /* AMD 756, for most chips (early revs), corrupts register
@@ -45,7 +48,8 @@ ohci_pci_start (struct usb_hcd *hcd)
45 && pdev->device == 0x740c) { 48 && pdev->device == 0x740c) {
46 ohci->flags = OHCI_QUIRK_AMD756; 49 ohci->flags = OHCI_QUIRK_AMD756;
47 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n"); 50 ohci_dbg (ohci, "AMD756 erratum 4 workaround\n");
48 // also somewhat erratum 10 (suspend/resume issues) 51 /* also erratum 10 (suspend/resume issues) */
52 device_init_wakeup(&hcd->self.root_hub->dev, 0);
49 } 53 }
50 54
51 /* FIXME for some of the early AMD 760 southbridges, OHCI 55 /* FIXME for some of the early AMD 760 southbridges, OHCI
@@ -88,6 +92,13 @@ ohci_pci_start (struct usb_hcd *hcd)
88 ohci_dbg (ohci, 92 ohci_dbg (ohci,
89 "enabled Compaq ZFMicro chipset quirk\n"); 93 "enabled Compaq ZFMicro chipset quirk\n");
90 } 94 }
95
96 /* RWC may not be set for add-in PCI cards, since boot
97 * firmware probably ignored them. This transfers PCI
98 * PM wakeup capabilities (once the PCI layer is fixed).
99 */
100 if (device_may_wakeup(&pdev->dev))
101 ohci->hc_control |= OHCI_CTRL_RWC;
91 } 102 }
92 103
93 /* NOTE: there may have already been a first reset, to 104 /* NOTE: there may have already been a first reset, to
diff --git a/drivers/usb/host/sl811-hcd.c b/drivers/usb/host/sl811-hcd.c
index 517360b77d8e..a92343052751 100644
--- a/drivers/usb/host/sl811-hcd.c
+++ b/drivers/usb/host/sl811-hcd.c
@@ -853,7 +853,7 @@ static int sl811h_urb_enqueue(
853 853
854 } else { 854 } else {
855 INIT_LIST_HEAD(&ep->schedule); 855 INIT_LIST_HEAD(&ep->schedule);
856 ep->udev = usb_get_dev(udev); 856 ep->udev = udev;
857 ep->epnum = epnum; 857 ep->epnum = epnum;
858 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out); 858 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
859 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE; 859 ep->defctrl = SL11H_HCTLMASK_ARM | SL11H_HCTLMASK_ENABLE;
@@ -1052,7 +1052,6 @@ sl811h_endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
1052 if (!list_empty(&hep->urb_list)) 1052 if (!list_empty(&hep->urb_list))
1053 WARN("ep %p not empty?\n", ep); 1053 WARN("ep %p not empty?\n", ep);
1054 1054
1055 usb_put_dev(ep->udev);
1056 kfree(ep); 1055 kfree(ep);
1057 hep->hcpriv = NULL; 1056 hep->hcpriv = NULL;
1058} 1057}
diff --git a/drivers/usb/host/uhci-debug.c b/drivers/usb/host/uhci-debug.c
index 5832953086f8..e1239319655c 100644
--- a/drivers/usb/host/uhci-debug.c
+++ b/drivers/usb/host/uhci-debug.c
@@ -17,10 +17,13 @@
17 17
18#include "uhci-hcd.h" 18#include "uhci-hcd.h"
19 19
20static struct dentry *uhci_debugfs_root = NULL; 20#define uhci_debug_operations (* (struct file_operations *) NULL)
21static struct dentry *uhci_debugfs_root;
22
23#ifdef DEBUG
21 24
22/* Handle REALLY large printks so we don't overflow buffers */ 25/* Handle REALLY large printks so we don't overflow buffers */
23static inline void lprintk(char *buf) 26static void lprintk(char *buf)
24{ 27{
25 char *p; 28 char *p;
26 29
@@ -90,13 +93,59 @@ static int uhci_show_td(struct uhci_td *td, char *buf, int len, int space)
90 return out - buf; 93 return out - buf;
91} 94}
92 95
93static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space) 96static int uhci_show_urbp(struct urb_priv *urbp, char *buf, int len, int space)
94{ 97{
95 char *out = buf; 98 char *out = buf;
96 struct urb_priv *urbp;
97 struct list_head *head, *tmp;
98 struct uhci_td *td; 99 struct uhci_td *td;
99 int i = 0, checked = 0, prevactive = 0; 100 int i, nactive, ninactive;
101
102 if (len < 200)
103 return 0;
104
105 out += sprintf(out, "urb_priv [%p] ", urbp);
106 out += sprintf(out, "urb [%p] ", urbp->urb);
107 out += sprintf(out, "qh [%p] ", urbp->qh);
108 out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
109 out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe),
110 (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
111
112 switch (usb_pipetype(urbp->urb->pipe)) {
113 case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO"); break;
114 case PIPE_INTERRUPT: out += sprintf(out, "INT"); break;
115 case PIPE_BULK: out += sprintf(out, "BLK"); break;
116 case PIPE_CONTROL: out += sprintf(out, "CTL"); break;
117 }
118
119 out += sprintf(out, "%s", (urbp->fsbr ? " FSBR" : ""));
120
121 if (urbp->urb->status != -EINPROGRESS)
122 out += sprintf(out, " Status=%d", urbp->urb->status);
123 out += sprintf(out, "\n");
124
125 i = nactive = ninactive = 0;
126 list_for_each_entry(td, &urbp->td_list, list) {
127 if (++i <= 10 || debug > 2) {
128 out += sprintf(out, "%*s%d: ", space + 2, "", i);
129 out += uhci_show_td(td, out, len - (out - buf), 0);
130 } else {
131 if (td_status(td) & TD_CTRL_ACTIVE)
132 ++nactive;
133 else
134 ++ninactive;
135 }
136 }
137 if (nactive + ninactive > 0)
138 out += sprintf(out, "%*s[skipped %d inactive and %d active "
139 "TDs]\n",
140 space, "", ninactive, nactive);
141
142 return out - buf;
143}
144
145static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
146{
147 char *out = buf;
148 int i, nurbs;
100 __le32 element = qh_element(qh); 149 __le32 element = qh_element(qh);
101 150
102 /* Try to make sure there's enough memory */ 151 /* Try to make sure there's enough memory */
@@ -118,86 +167,40 @@ static int uhci_show_qh(struct uhci_qh *qh, char *buf, int len, int space)
118 if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH))) 167 if (!(element & ~(UHCI_PTR_QH | UHCI_PTR_DEPTH)))
119 out += sprintf(out, "%*s Element is NULL (bug?)\n", space, ""); 168 out += sprintf(out, "%*s Element is NULL (bug?)\n", space, "");
120 169
121 if (!qh->urbp) { 170 if (list_empty(&qh->queue)) {
122 out += sprintf(out, "%*s urbp == NULL\n", space, ""); 171 out += sprintf(out, "%*s queue is empty\n", space, "");
123 goto out; 172 } else {
124 } 173 struct urb_priv *urbp = list_entry(qh->queue.next,
125 174 struct urb_priv, node);
126 urbp = qh->urbp; 175 struct uhci_td *td = list_entry(urbp->td_list.next,
127 176 struct uhci_td, list);
128 head = &urbp->td_list; 177
129 tmp = head->next; 178 if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS))
130 179 out += sprintf(out, "%*s Element != First TD\n",
131 td = list_entry(tmp, struct uhci_td, list); 180 space, "");
132 181 i = nurbs = 0;
133 if (cpu_to_le32(td->dma_handle) != (element & ~UHCI_PTR_BITS)) 182 list_for_each_entry(urbp, &qh->queue, node) {
134 out += sprintf(out, "%*s Element != First TD\n", space, ""); 183 if (++i <= 10)
135 184 out += uhci_show_urbp(urbp, out,
136 while (tmp != head) { 185 len - (out - buf), space + 2);
137 struct uhci_td *td = list_entry(tmp, struct uhci_td, list); 186 else
138 187 ++nurbs;
139 tmp = tmp->next;
140
141 out += sprintf(out, "%*s%d: ", space + 2, "", i++);
142 out += uhci_show_td(td, out, len - (out - buf), 0);
143
144 if (i > 10 && !checked && prevactive && tmp != head &&
145 debug <= 2) {
146 struct list_head *ntmp = tmp;
147 struct uhci_td *ntd = td;
148 int active = 1, ni = i;
149
150 checked = 1;
151
152 while (ntmp != head && ntmp->next != head && active) {
153 ntd = list_entry(ntmp, struct uhci_td, list);
154
155 ntmp = ntmp->next;
156
157 active = td_status(ntd) & TD_CTRL_ACTIVE;
158
159 ni++;
160 }
161
162 if (active && ni > i) {
163 out += sprintf(out, "%*s[skipped %d active TDs]\n", space, "", ni - i);
164 tmp = ntmp;
165 td = ntd;
166 i = ni;
167 }
168 } 188 }
169 189 if (nurbs > 0)
170 prevactive = td_status(td) & TD_CTRL_ACTIVE; 190 out += sprintf(out, "%*s Skipped %d URBs\n",
191 space, "", nurbs);
171 } 192 }
172 193
173 if (list_empty(&urbp->queue_list) || urbp->queued) 194 if (qh->udev) {
174 goto out; 195 out += sprintf(out, "%*s Dummy TD\n", space, "");
175 196 out += uhci_show_td(qh->dummy_td, out, len - (out - buf), 0);
176 out += sprintf(out, "%*sQueued QHs:\n", -space, "--");
177
178 head = &urbp->queue_list;
179 tmp = head->next;
180
181 while (tmp != head) {
182 struct urb_priv *nurbp = list_entry(tmp, struct urb_priv,
183 queue_list);
184 tmp = tmp->next;
185
186 out += uhci_show_qh(nurbp->qh, out, len - (out - buf), space);
187 } 197 }
188 198
189out:
190 return out - buf; 199 return out - buf;
191} 200}
192 201
193#define show_frame_num() \
194 if (!shown) { \
195 shown = 1; \
196 out += sprintf(out, "- Frame %d\n", i); \
197 }
198
199#ifdef CONFIG_PROC_FS
200static const char * const qh_names[] = { 202static const char * const qh_names[] = {
203 "skel_unlink_qh", "skel_iso_qh",
201 "skel_int128_qh", "skel_int64_qh", 204 "skel_int128_qh", "skel_int64_qh",
202 "skel_int32_qh", "skel_int16_qh", 205 "skel_int32_qh", "skel_int16_qh",
203 "skel_int8_qh", "skel_int4_qh", 206 "skel_int8_qh", "skel_int4_qh",
@@ -206,12 +209,6 @@ static const char * const qh_names[] = {
206 "skel_bulk_qh", "skel_term_qh" 209 "skel_bulk_qh", "skel_term_qh"
207}; 210};
208 211
209#define show_qh_name() \
210 if (!shown) { \
211 shown = 1; \
212 out += sprintf(out, "- %s\n", qh_names[i]); \
213 }
214
215static int uhci_show_sc(int port, unsigned short status, char *buf, int len) 212static int uhci_show_sc(int port, unsigned short status, char *buf, int len)
216{ 213{
217 char *out = buf; 214 char *out = buf;
@@ -321,139 +318,29 @@ static int uhci_show_status(struct uhci_hcd *uhci, char *buf, int len)
321 return out - buf; 318 return out - buf;
322} 319}
323 320
324static int uhci_show_urbp(struct uhci_hcd *uhci, struct urb_priv *urbp, char *buf, int len)
325{
326 struct list_head *tmp;
327 char *out = buf;
328 int count = 0;
329
330 if (len < 200)
331 return 0;
332
333 out += sprintf(out, "urb_priv [%p] ", urbp);
334 out += sprintf(out, "urb [%p] ", urbp->urb);
335 out += sprintf(out, "qh [%p] ", urbp->qh);
336 out += sprintf(out, "Dev=%d ", usb_pipedevice(urbp->urb->pipe));
337 out += sprintf(out, "EP=%x(%s) ", usb_pipeendpoint(urbp->urb->pipe), (usb_pipein(urbp->urb->pipe) ? "IN" : "OUT"));
338
339 switch (usb_pipetype(urbp->urb->pipe)) {
340 case PIPE_ISOCHRONOUS: out += sprintf(out, "ISO "); break;
341 case PIPE_INTERRUPT: out += sprintf(out, "INT "); break;
342 case PIPE_BULK: out += sprintf(out, "BLK "); break;
343 case PIPE_CONTROL: out += sprintf(out, "CTL "); break;
344 }
345
346 out += sprintf(out, "%s", (urbp->fsbr ? "FSBR " : ""));
347 out += sprintf(out, "%s", (urbp->fsbr_timeout ? "FSBR_TO " : ""));
348
349 if (urbp->urb->status != -EINPROGRESS)
350 out += sprintf(out, "Status=%d ", urbp->urb->status);
351 //out += sprintf(out, "FSBRtime=%lx ",urbp->fsbrtime);
352
353 count = 0;
354 list_for_each(tmp, &urbp->td_list)
355 count++;
356 out += sprintf(out, "TDs=%d ",count);
357
358 if (urbp->queued)
359 out += sprintf(out, "queued\n");
360 else {
361 count = 0;
362 list_for_each(tmp, &urbp->queue_list)
363 count++;
364 out += sprintf(out, "queued URBs=%d\n", count);
365 }
366
367 return out - buf;
368}
369
370static int uhci_show_lists(struct uhci_hcd *uhci, char *buf, int len)
371{
372 char *out = buf;
373 struct list_head *head, *tmp;
374 int count;
375
376 out += sprintf(out, "Main list URBs:");
377 if (list_empty(&uhci->urb_list))
378 out += sprintf(out, " Empty\n");
379 else {
380 out += sprintf(out, "\n");
381 count = 0;
382 head = &uhci->urb_list;
383 tmp = head->next;
384 while (tmp != head) {
385 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
386
387 out += sprintf(out, " %d: ", ++count);
388 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
389 tmp = tmp->next;
390 }
391 }
392
393 out += sprintf(out, "Remove list URBs:");
394 if (list_empty(&uhci->urb_remove_list))
395 out += sprintf(out, " Empty\n");
396 else {
397 out += sprintf(out, "\n");
398 count = 0;
399 head = &uhci->urb_remove_list;
400 tmp = head->next;
401 while (tmp != head) {
402 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
403
404 out += sprintf(out, " %d: ", ++count);
405 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
406 tmp = tmp->next;
407 }
408 }
409
410 out += sprintf(out, "Complete list URBs:");
411 if (list_empty(&uhci->complete_list))
412 out += sprintf(out, " Empty\n");
413 else {
414 out += sprintf(out, "\n");
415 count = 0;
416 head = &uhci->complete_list;
417 tmp = head->next;
418 while (tmp != head) {
419 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, urb_list);
420
421 out += sprintf(out, " %d: ", ++count);
422 out += uhci_show_urbp(uhci, urbp, out, len - (out - buf));
423 tmp = tmp->next;
424 }
425 }
426
427 return out - buf;
428}
429
430static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len) 321static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
431{ 322{
432 unsigned long flags;
433 char *out = buf; 323 char *out = buf;
434 int i, j; 324 int i, j;
435 struct uhci_qh *qh; 325 struct uhci_qh *qh;
436 struct uhci_td *td; 326 struct uhci_td *td;
437 struct list_head *tmp, *head; 327 struct list_head *tmp, *head;
438 328
439 spin_lock_irqsave(&uhci->lock, flags);
440
441 out += uhci_show_root_hub_state(uhci, out, len - (out - buf)); 329 out += uhci_show_root_hub_state(uhci, out, len - (out - buf));
442 out += sprintf(out, "HC status\n"); 330 out += sprintf(out, "HC status\n");
443 out += uhci_show_status(uhci, out, len - (out - buf)); 331 out += uhci_show_status(uhci, out, len - (out - buf));
332 if (debug <= 1)
333 return out - buf;
444 334
445 out += sprintf(out, "Frame List\n"); 335 out += sprintf(out, "Frame List\n");
446 for (i = 0; i < UHCI_NUMFRAMES; ++i) { 336 for (i = 0; i < UHCI_NUMFRAMES; ++i) {
447 int shown = 0;
448 td = uhci->frame_cpu[i]; 337 td = uhci->frame_cpu[i];
449 if (!td) 338 if (!td)
450 continue; 339 continue;
451 340
452 if (td->dma_handle != (dma_addr_t)uhci->frame[i]) { 341 out += sprintf(out, "- Frame %d\n", i); \
453 show_frame_num(); 342 if (td->dma_handle != (dma_addr_t)uhci->frame[i])
454 out += sprintf(out, " frame list does not match td->dma_handle!\n"); 343 out += sprintf(out, " frame list does not match td->dma_handle!\n");
455 }
456 show_frame_num();
457 344
458 head = &td->fl_list; 345 head = &td->fl_list;
459 tmp = head; 346 tmp = head;
@@ -467,14 +354,11 @@ static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
467 out += sprintf(out, "Skeleton QHs\n"); 354 out += sprintf(out, "Skeleton QHs\n");
468 355
469 for (i = 0; i < UHCI_NUM_SKELQH; ++i) { 356 for (i = 0; i < UHCI_NUM_SKELQH; ++i) {
470 int shown = 0; 357 int cnt = 0;
471 358
472 qh = uhci->skelqh[i]; 359 qh = uhci->skelqh[i];
473 360 out += sprintf(out, "- %s\n", qh_names[i]); \
474 if (debug > 1) { 361 out += uhci_show_qh(qh, out, len - (out - buf), 4);
475 show_qh_name();
476 out += uhci_show_qh(qh, out, len - (out - buf), 4);
477 }
478 362
479 /* Last QH is the Terminating QH, it's different */ 363 /* Last QH is the Terminating QH, it's different */
480 if (i == UHCI_NUM_SKELQH - 1) { 364 if (i == UHCI_NUM_SKELQH - 1) {
@@ -487,53 +371,37 @@ static int uhci_sprint_schedule(struct uhci_hcd *uhci, char *buf, int len)
487 continue; 371 continue;
488 } 372 }
489 373
490 j = (i < 7) ? 7 : i+1; /* Next skeleton */ 374 j = (i < 9) ? 9 : i+1; /* Next skeleton */
491 if (list_empty(&qh->list)) { 375 head = &qh->node;
492 if (i < UHCI_NUM_SKELQH - 1) {
493 if (qh->link !=
494 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) {
495 show_qh_name();
496 out += sprintf(out, " skeleton QH not linked to next skeleton QH!\n");
497 }
498 }
499
500 continue;
501 }
502
503 show_qh_name();
504
505 head = &qh->list;
506 tmp = head->next; 376 tmp = head->next;
507 377
508 while (tmp != head) { 378 while (tmp != head) {
509 qh = list_entry(tmp, struct uhci_qh, list); 379 qh = list_entry(tmp, struct uhci_qh, node);
510
511 tmp = tmp->next; 380 tmp = tmp->next;
512 381 if (++cnt <= 10)
513 out += uhci_show_qh(qh, out, len - (out - buf), 4); 382 out += uhci_show_qh(qh, out,
383 len - (out - buf), 4);
514 } 384 }
385 if ((cnt -= 10) > 0)
386 out += sprintf(out, " Skipped %d QHs\n", cnt);
515 387
516 if (i < UHCI_NUM_SKELQH - 1) { 388 if (i > 1 && i < UHCI_NUM_SKELQH - 1) {
517 if (qh->link != 389 if (qh->link !=
518 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH)) 390 (cpu_to_le32(uhci->skelqh[j]->dma_handle) | UHCI_PTR_QH))
519 out += sprintf(out, " last QH not linked to next skeleton!\n"); 391 out += sprintf(out, " last QH not linked to next skeleton!\n");
520 } 392 }
521 } 393 }
522 394
523 if (debug > 2)
524 out += uhci_show_lists(uhci, out, len - (out - buf));
525
526 spin_unlock_irqrestore(&uhci->lock, flags);
527
528 return out - buf; 395 return out - buf;
529} 396}
530 397
398#ifdef CONFIG_DEBUG_FS
399
531#define MAX_OUTPUT (64 * 1024) 400#define MAX_OUTPUT (64 * 1024)
532 401
533struct uhci_debug { 402struct uhci_debug {
534 int size; 403 int size;
535 char *data; 404 char *data;
536 struct uhci_hcd *uhci;
537}; 405};
538 406
539static int uhci_debug_open(struct inode *inode, struct file *file) 407static int uhci_debug_open(struct inode *inode, struct file *file)
@@ -541,6 +409,7 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
541 struct uhci_hcd *uhci = inode->u.generic_ip; 409 struct uhci_hcd *uhci = inode->u.generic_ip;
542 struct uhci_debug *up; 410 struct uhci_debug *up;
543 int ret = -ENOMEM; 411 int ret = -ENOMEM;
412 unsigned long flags;
544 413
545 lock_kernel(); 414 lock_kernel();
546 up = kmalloc(sizeof(*up), GFP_KERNEL); 415 up = kmalloc(sizeof(*up), GFP_KERNEL);
@@ -553,7 +422,11 @@ static int uhci_debug_open(struct inode *inode, struct file *file)
553 goto out; 422 goto out;
554 } 423 }
555 424
556 up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT); 425 up->size = 0;
426 spin_lock_irqsave(&uhci->lock, flags);
427 if (uhci->is_initialized)
428 up->size = uhci_sprint_schedule(uhci, up->data, MAX_OUTPUT);
429 spin_unlock_irqrestore(&uhci->lock, flags);
557 430
558 file->private_data = up; 431 file->private_data = up;
559 432
@@ -604,15 +477,32 @@ static int uhci_debug_release(struct inode *inode, struct file *file)
604 return 0; 477 return 0;
605} 478}
606 479
480#undef uhci_debug_operations
607static struct file_operations uhci_debug_operations = { 481static struct file_operations uhci_debug_operations = {
482 .owner = THIS_MODULE,
608 .open = uhci_debug_open, 483 .open = uhci_debug_open,
609 .llseek = uhci_debug_lseek, 484 .llseek = uhci_debug_lseek,
610 .read = uhci_debug_read, 485 .read = uhci_debug_read,
611 .release = uhci_debug_release, 486 .release = uhci_debug_release,
612}; 487};
613 488
614#else /* CONFIG_DEBUG_FS */ 489#endif /* CONFIG_DEBUG_FS */
615 490
616#define uhci_debug_operations (* (struct file_operations *) NULL) 491#else /* DEBUG */
492
493static inline void lprintk(char *buf)
494{}
495
496static inline int uhci_show_qh(struct uhci_qh *qh, char *buf,
497 int len, int space)
498{
499 return 0;
500}
501
502static inline int uhci_sprint_schedule(struct uhci_hcd *uhci,
503 char *buf, int len)
504{
505 return 0;
506}
617 507
618#endif 508#endif
diff --git a/drivers/usb/host/uhci-hcd.c b/drivers/usb/host/uhci-hcd.c
index dfe121d35887..4edb8330c440 100644
--- a/drivers/usb/host/uhci-hcd.c
+++ b/drivers/usb/host/uhci-hcd.c
@@ -54,7 +54,7 @@
54/* 54/*
55 * Version Information 55 * Version Information
56 */ 56 */
57#define DRIVER_VERSION "v2.3" 57#define DRIVER_VERSION "v3.0"
58#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \ 58#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, \
59Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \ 59Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, \
60Alan Stern" 60Alan Stern"
@@ -68,12 +68,16 @@ Alan Stern"
68 * debug = 3, show all TDs in URBs when dumping 68 * debug = 3, show all TDs in URBs when dumping
69 */ 69 */
70#ifdef DEBUG 70#ifdef DEBUG
71#define DEBUG_CONFIGURED 1
71static int debug = 1; 72static int debug = 1;
72#else
73static int debug = 0;
74#endif
75module_param(debug, int, S_IRUGO | S_IWUSR); 73module_param(debug, int, S_IRUGO | S_IWUSR);
76MODULE_PARM_DESC(debug, "Debug level"); 74MODULE_PARM_DESC(debug, "Debug level");
75
76#else
77#define DEBUG_CONFIGURED 0
78#define debug 0
79#endif
80
77static char *errbuf; 81static char *errbuf;
78#define ERRBUF_LEN (32 * 1024) 82#define ERRBUF_LEN (32 * 1024)
79 83
@@ -338,6 +342,12 @@ static irqreturn_t uhci_irq(struct usb_hcd *hcd, struct pt_regs *regs)
338 dev_err(uhci_dev(uhci), 342 dev_err(uhci_dev(uhci),
339 "host controller halted, " 343 "host controller halted, "
340 "very bad!\n"); 344 "very bad!\n");
345 if (debug > 1 && errbuf) {
346 /* Print the schedule for debugging */
347 uhci_sprint_schedule(uhci,
348 errbuf, ERRBUF_LEN);
349 lprintk(errbuf);
350 }
341 hc_died(uhci); 351 hc_died(uhci);
342 352
343 /* Force a callback in case there are 353 /* Force a callback in case there are
@@ -376,6 +386,14 @@ static void release_uhci(struct uhci_hcd *uhci)
376{ 386{
377 int i; 387 int i;
378 388
389 if (DEBUG_CONFIGURED) {
390 spin_lock_irq(&uhci->lock);
391 uhci->is_initialized = 0;
392 spin_unlock_irq(&uhci->lock);
393
394 debugfs_remove(uhci->dentry);
395 }
396
379 for (i = 0; i < UHCI_NUM_SKELQH; i++) 397 for (i = 0; i < UHCI_NUM_SKELQH; i++)
380 uhci_free_qh(uhci, uhci->skelqh[i]); 398 uhci_free_qh(uhci, uhci->skelqh[i]);
381 399
@@ -390,8 +408,6 @@ static void release_uhci(struct uhci_hcd *uhci)
390 dma_free_coherent(uhci_dev(uhci), 408 dma_free_coherent(uhci_dev(uhci),
391 UHCI_NUMFRAMES * sizeof(*uhci->frame), 409 UHCI_NUMFRAMES * sizeof(*uhci->frame),
392 uhci->frame, uhci->frame_dma_handle); 410 uhci->frame, uhci->frame_dma_handle);
393
394 debugfs_remove(uhci->dentry);
395} 411}
396 412
397static int uhci_reset(struct usb_hcd *hcd) 413static int uhci_reset(struct usb_hcd *hcd)
@@ -474,33 +490,29 @@ static int uhci_start(struct usb_hcd *hcd)
474 490
475 hcd->uses_new_polling = 1; 491 hcd->uses_new_polling = 1;
476 492
477 dentry = debugfs_create_file(hcd->self.bus_name,
478 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root, uhci,
479 &uhci_debug_operations);
480 if (!dentry) {
481 dev_err(uhci_dev(uhci),
482 "couldn't create uhci debugfs entry\n");
483 retval = -ENOMEM;
484 goto err_create_debug_entry;
485 }
486 uhci->dentry = dentry;
487
488 uhci->fsbr = 0; 493 uhci->fsbr = 0;
489 uhci->fsbrtimeout = 0; 494 uhci->fsbrtimeout = 0;
490 495
491 spin_lock_init(&uhci->lock); 496 spin_lock_init(&uhci->lock);
492 INIT_LIST_HEAD(&uhci->qh_remove_list);
493 497
494 INIT_LIST_HEAD(&uhci->td_remove_list); 498 INIT_LIST_HEAD(&uhci->td_remove_list);
495 499 INIT_LIST_HEAD(&uhci->idle_qh_list);
496 INIT_LIST_HEAD(&uhci->urb_remove_list);
497
498 INIT_LIST_HEAD(&uhci->urb_list);
499
500 INIT_LIST_HEAD(&uhci->complete_list);
501 500
502 init_waitqueue_head(&uhci->waitqh); 501 init_waitqueue_head(&uhci->waitqh);
503 502
503 if (DEBUG_CONFIGURED) {
504 dentry = debugfs_create_file(hcd->self.bus_name,
505 S_IFREG|S_IRUGO|S_IWUSR, uhci_debugfs_root,
506 uhci, &uhci_debug_operations);
507 if (!dentry) {
508 dev_err(uhci_dev(uhci), "couldn't create uhci "
509 "debugfs entry\n");
510 retval = -ENOMEM;
511 goto err_create_debug_entry;
512 }
513 uhci->dentry = dentry;
514 }
515
504 uhci->frame = dma_alloc_coherent(uhci_dev(uhci), 516 uhci->frame = dma_alloc_coherent(uhci_dev(uhci),
505 UHCI_NUMFRAMES * sizeof(*uhci->frame), 517 UHCI_NUMFRAMES * sizeof(*uhci->frame),
506 &uhci->frame_dma_handle, 0); 518 &uhci->frame_dma_handle, 0);
@@ -540,7 +552,7 @@ static int uhci_start(struct usb_hcd *hcd)
540 } 552 }
541 553
542 for (i = 0; i < UHCI_NUM_SKELQH; i++) { 554 for (i = 0; i < UHCI_NUM_SKELQH; i++) {
543 uhci->skelqh[i] = uhci_alloc_qh(uhci); 555 uhci->skelqh[i] = uhci_alloc_qh(uhci, NULL, NULL);
544 if (!uhci->skelqh[i]) { 556 if (!uhci->skelqh[i]) {
545 dev_err(uhci_dev(uhci), "unable to allocate QH\n"); 557 dev_err(uhci_dev(uhci), "unable to allocate QH\n");
546 goto err_alloc_skelqh; 558 goto err_alloc_skelqh;
@@ -557,13 +569,17 @@ static int uhci_start(struct usb_hcd *hcd)
557 uhci->skel_int16_qh->link = 569 uhci->skel_int16_qh->link =
558 uhci->skel_int8_qh->link = 570 uhci->skel_int8_qh->link =
559 uhci->skel_int4_qh->link = 571 uhci->skel_int4_qh->link =
560 uhci->skel_int2_qh->link = 572 uhci->skel_int2_qh->link = UHCI_PTR_QH |
561 cpu_to_le32(uhci->skel_int1_qh->dma_handle) | UHCI_PTR_QH; 573 cpu_to_le32(uhci->skel_int1_qh->dma_handle);
562 uhci->skel_int1_qh->link = cpu_to_le32(uhci->skel_ls_control_qh->dma_handle) | UHCI_PTR_QH; 574
563 575 uhci->skel_int1_qh->link = UHCI_PTR_QH |
564 uhci->skel_ls_control_qh->link = cpu_to_le32(uhci->skel_fs_control_qh->dma_handle) | UHCI_PTR_QH; 576 cpu_to_le32(uhci->skel_ls_control_qh->dma_handle);
565 uhci->skel_fs_control_qh->link = cpu_to_le32(uhci->skel_bulk_qh->dma_handle) | UHCI_PTR_QH; 577 uhci->skel_ls_control_qh->link = UHCI_PTR_QH |
566 uhci->skel_bulk_qh->link = cpu_to_le32(uhci->skel_term_qh->dma_handle) | UHCI_PTR_QH; 578 cpu_to_le32(uhci->skel_fs_control_qh->dma_handle);
579 uhci->skel_fs_control_qh->link = UHCI_PTR_QH |
580 cpu_to_le32(uhci->skel_bulk_qh->dma_handle);
581 uhci->skel_bulk_qh->link = UHCI_PTR_QH |
582 cpu_to_le32(uhci->skel_term_qh->dma_handle);
567 583
568 /* This dummy TD is to work around a bug in Intel PIIX controllers */ 584 /* This dummy TD is to work around a bug in Intel PIIX controllers */
569 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) | 585 uhci_fill_td(uhci->term_td, 0, uhci_explen(0) |
@@ -589,15 +605,15 @@ static int uhci_start(struct usb_hcd *hcd)
589 605
590 /* 606 /*
591 * ffs (Find First bit Set) does exactly what we need: 607 * ffs (Find First bit Set) does exactly what we need:
592 * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[6], 608 * 1,3,5,... => ffs = 0 => use skel_int2_qh = skelqh[8],
593 * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[5], etc. 609 * 2,6,10,... => ffs = 1 => use skel_int4_qh = skelqh[7], etc.
594 * ffs > 6 => not on any high-period queue, so use 610 * ffs >= 7 => not on any high-period queue, so use
595 * skel_int1_qh = skelqh[7]. 611 * skel_int1_qh = skelqh[9].
596 * Add UHCI_NUMFRAMES to insure at least one bit is set. 612 * Add UHCI_NUMFRAMES to insure at least one bit is set.
597 */ 613 */
598 irq = 6 - (int) __ffs(i + UHCI_NUMFRAMES); 614 irq = 8 - (int) __ffs(i + UHCI_NUMFRAMES);
599 if (irq < 0) 615 if (irq <= 1)
600 irq = 7; 616 irq = 9;
601 617
602 /* Only place we don't use the frame list routines */ 618 /* Only place we don't use the frame list routines */
603 uhci->frame[i] = UHCI_PTR_QH | 619 uhci->frame[i] = UHCI_PTR_QH |
@@ -611,6 +627,7 @@ static int uhci_start(struct usb_hcd *hcd)
611 mb(); 627 mb();
612 628
613 configure_hc(uhci); 629 configure_hc(uhci);
630 uhci->is_initialized = 1;
614 start_rh(uhci); 631 start_rh(uhci);
615 return 0; 632 return 0;
616 633
@@ -767,13 +784,30 @@ static int uhci_resume(struct usb_hcd *hcd)
767} 784}
768#endif 785#endif
769 786
770/* Wait until all the URBs for a particular device/endpoint are gone */ 787/* Wait until a particular device/endpoint's QH is idle, and free it */
771static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd, 788static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
772 struct usb_host_endpoint *ep) 789 struct usb_host_endpoint *hep)
773{ 790{
774 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 791 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
792 struct uhci_qh *qh;
793
794 spin_lock_irq(&uhci->lock);
795 qh = (struct uhci_qh *) hep->hcpriv;
796 if (qh == NULL)
797 goto done;
798
799 while (qh->state != QH_STATE_IDLE) {
800 ++uhci->num_waiting;
801 spin_unlock_irq(&uhci->lock);
802 wait_event_interruptible(uhci->waitqh,
803 qh->state == QH_STATE_IDLE);
804 spin_lock_irq(&uhci->lock);
805 --uhci->num_waiting;
806 }
775 807
776 wait_event_interruptible(uhci->waitqh, list_empty(&ep->urb_list)); 808 uhci_free_qh(uhci, qh);
809done:
810 spin_unlock_irq(&uhci->lock);
777} 811}
778 812
779static int uhci_hcd_get_frame_number(struct usb_hcd *hcd) 813static int uhci_hcd_get_frame_number(struct usb_hcd *hcd)
@@ -857,16 +891,15 @@ static int __init uhci_hcd_init(void)
857 if (usb_disabled()) 891 if (usb_disabled())
858 return -ENODEV; 892 return -ENODEV;
859 893
860 if (debug) { 894 if (DEBUG_CONFIGURED) {
861 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL); 895 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
862 if (!errbuf) 896 if (!errbuf)
863 goto errbuf_failed; 897 goto errbuf_failed;
898 uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
899 if (!uhci_debugfs_root)
900 goto debug_failed;
864 } 901 }
865 902
866 uhci_debugfs_root = debugfs_create_dir("uhci", NULL);
867 if (!uhci_debugfs_root)
868 goto debug_failed;
869
870 uhci_up_cachep = kmem_cache_create("uhci_urb_priv", 903 uhci_up_cachep = kmem_cache_create("uhci_urb_priv",
871 sizeof(struct urb_priv), 0, 0, NULL, NULL); 904 sizeof(struct urb_priv), 0, 0, NULL, NULL);
872 if (!uhci_up_cachep) 905 if (!uhci_up_cachep)
diff --git a/drivers/usb/host/uhci-hcd.h b/drivers/usb/host/uhci-hcd.h
index 8b4b887a7d41..4a69c7eb09bd 100644
--- a/drivers/usb/host/uhci-hcd.h
+++ b/drivers/usb/host/uhci-hcd.h
@@ -28,8 +28,9 @@
28#define USBSTS_USBINT 0x0001 /* Interrupt due to IOC */ 28#define USBSTS_USBINT 0x0001 /* Interrupt due to IOC */
29#define USBSTS_ERROR 0x0002 /* Interrupt due to error */ 29#define USBSTS_ERROR 0x0002 /* Interrupt due to error */
30#define USBSTS_RD 0x0004 /* Resume Detect */ 30#define USBSTS_RD 0x0004 /* Resume Detect */
31#define USBSTS_HSE 0x0008 /* Host System Error - basically PCI problems */ 31#define USBSTS_HSE 0x0008 /* Host System Error: PCI problems */
32#define USBSTS_HCPE 0x0010 /* Host Controller Process Error - the scripts were buggy */ 32#define USBSTS_HCPE 0x0010 /* Host Controller Process Error:
33 * the schedule is buggy */
33#define USBSTS_HCH 0x0020 /* HC Halted */ 34#define USBSTS_HCH 0x0020 /* HC Halted */
34 35
35/* Interrupt enable register */ 36/* Interrupt enable register */
@@ -47,7 +48,8 @@
47/* USB port status and control registers */ 48/* USB port status and control registers */
48#define USBPORTSC1 16 49#define USBPORTSC1 16
49#define USBPORTSC2 18 50#define USBPORTSC2 18
50#define USBPORTSC_CCS 0x0001 /* Current Connect Status ("device present") */ 51#define USBPORTSC_CCS 0x0001 /* Current Connect Status
52 * ("device present") */
51#define USBPORTSC_CSC 0x0002 /* Connect Status Change */ 53#define USBPORTSC_CSC 0x0002 /* Connect Status Change */
52#define USBPORTSC_PE 0x0004 /* Port Enable */ 54#define USBPORTSC_PE 0x0004 /* Port Enable */
53#define USBPORTSC_PEC 0x0008 /* Port Enable Change */ 55#define USBPORTSC_PEC 0x0008 /* Port Enable Change */
@@ -71,15 +73,16 @@
71#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */ 73#define USBLEGSUP_RWC 0x8f00 /* the R/WC bits */
72#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */ 74#define USBLEGSUP_RO 0x5040 /* R/O and reserved bits */
73 75
74#define UHCI_PTR_BITS cpu_to_le32(0x000F) 76#define UHCI_PTR_BITS __constant_cpu_to_le32(0x000F)
75#define UHCI_PTR_TERM cpu_to_le32(0x0001) 77#define UHCI_PTR_TERM __constant_cpu_to_le32(0x0001)
76#define UHCI_PTR_QH cpu_to_le32(0x0002) 78#define UHCI_PTR_QH __constant_cpu_to_le32(0x0002)
77#define UHCI_PTR_DEPTH cpu_to_le32(0x0004) 79#define UHCI_PTR_DEPTH __constant_cpu_to_le32(0x0004)
78#define UHCI_PTR_BREADTH cpu_to_le32(0x0000) 80#define UHCI_PTR_BREADTH __constant_cpu_to_le32(0x0000)
79 81
80#define UHCI_NUMFRAMES 1024 /* in the frame list [array] */ 82#define UHCI_NUMFRAMES 1024 /* in the frame list [array] */
81#define UHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */ 83#define UHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */
82#define CAN_SCHEDULE_FRAMES 1000 /* how far future frames can be scheduled */ 84#define CAN_SCHEDULE_FRAMES 1000 /* how far in the future frames
85 * can be scheduled */
83 86
84 87
85/* 88/*
@@ -87,38 +90,59 @@
87 */ 90 */
88 91
89/* 92/*
90 * One role of a QH is to hold a queue of TDs for some endpoint. Each QH is 93 * One role of a QH is to hold a queue of TDs for some endpoint. One QH goes
91 * used with one URB, and qh->element (updated by the HC) is either: 94 * with each endpoint, and qh->element (updated by the HC) is either:
92 * - the next unprocessed TD for the URB, or 95 * - the next unprocessed TD in the endpoint's queue, or
93 * - UHCI_PTR_TERM (when there's no more traffic for this endpoint), or 96 * - UHCI_PTR_TERM (when there's no more traffic for this endpoint).
94 * - the QH for the next URB queued to the same endpoint.
95 * 97 *
96 * The other role of a QH is to serve as a "skeleton" framelist entry, so we 98 * The other role of a QH is to serve as a "skeleton" framelist entry, so we
97 * can easily splice a QH for some endpoint into the schedule at the right 99 * can easily splice a QH for some endpoint into the schedule at the right
98 * place. Then qh->element is UHCI_PTR_TERM. 100 * place. Then qh->element is UHCI_PTR_TERM.
99 * 101 *
100 * In the frame list, qh->link maintains a list of QHs seen by the HC: 102 * In the schedule, qh->link maintains a list of QHs seen by the HC:
101 * skel1 --> ep1-qh --> ep2-qh --> ... --> skel2 --> ... 103 * skel1 --> ep1-qh --> ep2-qh --> ... --> skel2 --> ...
104 *
105 * qh->node is the software equivalent of qh->link. The differences
106 * are that the software list is doubly-linked and QHs in the UNLINKING
107 * state are on the software list but not the hardware schedule.
108 *
109 * For bookkeeping purposes we maintain QHs even for Isochronous endpoints,
110 * but they never get added to the hardware schedule.
102 */ 111 */
112#define QH_STATE_IDLE 1 /* QH is not being used */
113#define QH_STATE_UNLINKING 2 /* QH has been removed from the
114 * schedule but the hardware may
115 * still be using it */
116#define QH_STATE_ACTIVE 3 /* QH is on the schedule */
117
103struct uhci_qh { 118struct uhci_qh {
104 /* Hardware fields */ 119 /* Hardware fields */
105 __le32 link; /* Next queue */ 120 __le32 link; /* Next QH in the schedule */
106 __le32 element; /* Queue element pointer */ 121 __le32 element; /* Queue element (TD) pointer */
107 122
108 /* Software fields */ 123 /* Software fields */
109 dma_addr_t dma_handle; 124 dma_addr_t dma_handle;
110 125
111 struct urb_priv *urbp; 126 struct list_head node; /* Node in the list of QHs */
127 struct usb_host_endpoint *hep; /* Endpoint information */
128 struct usb_device *udev;
129 struct list_head queue; /* Queue of urbps for this QH */
130 struct uhci_qh *skel; /* Skeleton for this QH */
131 struct uhci_td *dummy_td; /* Dummy TD to end the queue */
112 132
113 struct list_head list; 133 unsigned int unlink_frame; /* When the QH was unlinked */
114 struct list_head remove_list; 134 int state; /* QH_STATE_xxx; see above */
135
136 unsigned int initial_toggle:1; /* Endpoint's current toggle value */
137 unsigned int needs_fixup:1; /* Must fix the TD toggle values */
138 unsigned int is_stopped:1; /* Queue was stopped by an error */
115} __attribute__((aligned(16))); 139} __attribute__((aligned(16)));
116 140
117/* 141/*
118 * We need a special accessor for the element pointer because it is 142 * We need a special accessor for the element pointer because it is
119 * subject to asynchronous updates by the controller. 143 * subject to asynchronous updates by the controller.
120 */ 144 */
121static __le32 inline qh_element(struct uhci_qh *qh) { 145static inline __le32 qh_element(struct uhci_qh *qh) {
122 __le32 element = qh->element; 146 __le32 element = qh->element;
123 147
124 barrier(); 148 barrier();
@@ -149,11 +173,13 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
149#define TD_CTRL_ACTLEN_MASK 0x7FF /* actual length, encoded as n - 1 */ 173#define TD_CTRL_ACTLEN_MASK 0x7FF /* actual length, encoded as n - 1 */
150 174
151#define TD_CTRL_ANY_ERROR (TD_CTRL_STALLED | TD_CTRL_DBUFERR | \ 175#define TD_CTRL_ANY_ERROR (TD_CTRL_STALLED | TD_CTRL_DBUFERR | \
152 TD_CTRL_BABBLE | TD_CTRL_CRCTIME | TD_CTRL_BITSTUFF) 176 TD_CTRL_BABBLE | TD_CTRL_CRCTIME | \
177 TD_CTRL_BITSTUFF)
153 178
154#define uhci_maxerr(err) ((err) << TD_CTRL_C_ERR_SHIFT) 179#define uhci_maxerr(err) ((err) << TD_CTRL_C_ERR_SHIFT)
155#define uhci_status_bits(ctrl_sts) ((ctrl_sts) & 0xF60000) 180#define uhci_status_bits(ctrl_sts) ((ctrl_sts) & 0xF60000)
156#define uhci_actual_length(ctrl_sts) (((ctrl_sts) + 1) & TD_CTRL_ACTLEN_MASK) /* 1-based */ 181#define uhci_actual_length(ctrl_sts) (((ctrl_sts) + 1) & \
182 TD_CTRL_ACTLEN_MASK) /* 1-based */
157 183
158/* 184/*
159 * for TD <info>: (a.k.a. Token) 185 * for TD <info>: (a.k.a. Token)
@@ -163,7 +189,7 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
163#define TD_TOKEN_TOGGLE_SHIFT 19 189#define TD_TOKEN_TOGGLE_SHIFT 19
164#define TD_TOKEN_TOGGLE (1 << 19) 190#define TD_TOKEN_TOGGLE (1 << 19)
165#define TD_TOKEN_EXPLEN_SHIFT 21 191#define TD_TOKEN_EXPLEN_SHIFT 21
166#define TD_TOKEN_EXPLEN_MASK 0x7FF /* expected length, encoded as n - 1 */ 192#define TD_TOKEN_EXPLEN_MASK 0x7FF /* expected length, encoded as n-1 */
167#define TD_TOKEN_PID_MASK 0xFF 193#define TD_TOKEN_PID_MASK 0xFF
168 194
169#define uhci_explen(len) ((((len) - 1) & TD_TOKEN_EXPLEN_MASK) << \ 195#define uhci_explen(len) ((((len) - 1) & TD_TOKEN_EXPLEN_MASK) << \
@@ -187,7 +213,7 @@ static __le32 inline qh_element(struct uhci_qh *qh) {
187 * sw space after the TD entry. 213 * sw space after the TD entry.
188 * 214 *
189 * td->link points to either another TD (not necessarily for the same urb or 215 * td->link points to either another TD (not necessarily for the same urb or
190 * even the same endpoint), or nothing (PTR_TERM), or a QH (for queued urbs). 216 * even the same endpoint), or nothing (PTR_TERM), or a QH.
191 */ 217 */
192struct uhci_td { 218struct uhci_td {
193 /* Hardware fields */ 219 /* Hardware fields */
@@ -210,7 +236,7 @@ struct uhci_td {
210 * We need a special accessor for the control/status word because it is 236 * We need a special accessor for the control/status word because it is
211 * subject to asynchronous updates by the controller. 237 * subject to asynchronous updates by the controller.
212 */ 238 */
213static u32 inline td_status(struct uhci_td *td) { 239static inline u32 td_status(struct uhci_td *td) {
214 __le32 status = td->status; 240 __le32 status = td->status;
215 241
216 barrier(); 242 barrier();
@@ -223,17 +249,14 @@ static u32 inline td_status(struct uhci_td *td) {
223 */ 249 */
224 250
225/* 251/*
226 * The UHCI driver places Interrupt, Control and Bulk into QHs both 252 * The UHCI driver uses QHs with Interrupt, Control and Bulk URBs for
227 * to group together TDs for one transfer, and also to facilitate queuing 253 * automatic queuing. To make it easy to insert entries into the schedule,
228 * of URBs. To make it easy to insert entries into the schedule, we have 254 * we have a skeleton of QHs for each predefined Interrupt latency,
229 * a skeleton of QHs for each predefined Interrupt latency, low-speed 255 * low-speed control, full-speed control, bulk, and terminating QH
230 * control, full-speed control and terminating QH (see explanation for 256 * (see explanation for the terminating QH below).
231 * the terminating QH below).
232 * 257 *
233 * When we want to add a new QH, we add it to the end of the list for the 258 * When we want to add a new QH, we add it to the end of the list for the
234 * skeleton QH. 259 * skeleton QH. For instance, the schedule list can look like this:
235 *
236 * For instance, the queue can look like this:
237 * 260 *
238 * skel int128 QH 261 * skel int128 QH
239 * dev 1 interrupt QH 262 * dev 1 interrupt QH
@@ -256,26 +279,31 @@ static u32 inline td_status(struct uhci_td *td) {
256 * - To loop back to the full-speed control queue for full-speed bandwidth 279 * - To loop back to the full-speed control queue for full-speed bandwidth
257 * reclamation. 280 * reclamation.
258 * 281 *
259 * Isochronous transfers are stored before the start of the skeleton 282 * There's a special skeleton QH for Isochronous QHs. It never appears
260 * schedule and don't use QHs. While the UHCI spec doesn't forbid the 283 * on the schedule, and Isochronous TDs go on the schedule before the
261 * use of QHs for Isochronous, it doesn't use them either. And the spec 284 * the skeleton QHs. The hardware accesses them directly rather than
262 * says that queues never advance on an error completion status, which 285 * through their QH, which is used only for bookkeeping purposes.
263 * makes them totally unsuitable for Isochronous transfers. 286 * While the UHCI spec doesn't forbid the use of QHs for Isochronous,
287 * it doesn't use them either. And the spec says that queues never
288 * advance on an error completion status, which makes them totally
289 * unsuitable for Isochronous transfers.
264 */ 290 */
265 291
266#define UHCI_NUM_SKELQH 12 292#define UHCI_NUM_SKELQH 14
267#define skel_int128_qh skelqh[0] 293#define skel_unlink_qh skelqh[0]
268#define skel_int64_qh skelqh[1] 294#define skel_iso_qh skelqh[1]
269#define skel_int32_qh skelqh[2] 295#define skel_int128_qh skelqh[2]
270#define skel_int16_qh skelqh[3] 296#define skel_int64_qh skelqh[3]
271#define skel_int8_qh skelqh[4] 297#define skel_int32_qh skelqh[4]
272#define skel_int4_qh skelqh[5] 298#define skel_int16_qh skelqh[5]
273#define skel_int2_qh skelqh[6] 299#define skel_int8_qh skelqh[6]
274#define skel_int1_qh skelqh[7] 300#define skel_int4_qh skelqh[7]
275#define skel_ls_control_qh skelqh[8] 301#define skel_int2_qh skelqh[8]
276#define skel_fs_control_qh skelqh[9] 302#define skel_int1_qh skelqh[9]
277#define skel_bulk_qh skelqh[10] 303#define skel_ls_control_qh skelqh[10]
278#define skel_term_qh skelqh[11] 304#define skel_fs_control_qh skelqh[11]
305#define skel_bulk_qh skelqh[12]
306#define skel_term_qh skelqh[13]
279 307
280/* 308/*
281 * Search tree for determining where <interval> fits in the skelqh[] 309 * Search tree for determining where <interval> fits in the skelqh[]
@@ -293,21 +321,21 @@ static inline int __interval_to_skel(int interval)
293 if (interval < 16) { 321 if (interval < 16) {
294 if (interval < 4) { 322 if (interval < 4) {
295 if (interval < 2) 323 if (interval < 2)
296 return 7; /* int1 for 0-1 ms */ 324 return 9; /* int1 for 0-1 ms */
297 return 6; /* int2 for 2-3 ms */ 325 return 8; /* int2 for 2-3 ms */
298 } 326 }
299 if (interval < 8) 327 if (interval < 8)
300 return 5; /* int4 for 4-7 ms */ 328 return 7; /* int4 for 4-7 ms */
301 return 4; /* int8 for 8-15 ms */ 329 return 6; /* int8 for 8-15 ms */
302 } 330 }
303 if (interval < 64) { 331 if (interval < 64) {
304 if (interval < 32) 332 if (interval < 32)
305 return 3; /* int16 for 16-31 ms */ 333 return 5; /* int16 for 16-31 ms */
306 return 2; /* int32 for 32-63 ms */ 334 return 4; /* int32 for 32-63 ms */
307 } 335 }
308 if (interval < 128) 336 if (interval < 128)
309 return 1; /* int64 for 64-127 ms */ 337 return 3; /* int64 for 64-127 ms */
310 return 0; /* int128 for 128-255 ms (Max.) */ 338 return 2; /* int128 for 128-255 ms (Max.) */
311} 339}
312 340
313 341
@@ -360,15 +388,16 @@ struct uhci_hcd {
360 388
361 struct uhci_td *term_td; /* Terminating TD, see UHCI bug */ 389 struct uhci_td *term_td; /* Terminating TD, see UHCI bug */
362 struct uhci_qh *skelqh[UHCI_NUM_SKELQH]; /* Skeleton QHs */ 390 struct uhci_qh *skelqh[UHCI_NUM_SKELQH]; /* Skeleton QHs */
391 struct uhci_qh *next_qh; /* Next QH to scan */
363 392
364 spinlock_t lock; 393 spinlock_t lock;
365 394
366 dma_addr_t frame_dma_handle; /* Hardware frame list */ 395 dma_addr_t frame_dma_handle; /* Hardware frame list */
367 __le32 *frame; 396 __le32 *frame;
368 void **frame_cpu; /* CPU's frame list */ 397 void **frame_cpu; /* CPU's frame list */
369 398
370 int fsbr; /* Full-speed bandwidth reclamation */ 399 int fsbr; /* Full-speed bandwidth reclamation */
371 unsigned long fsbrtimeout; /* FSBR delay */ 400 unsigned long fsbrtimeout; /* FSBR delay */
372 401
373 enum uhci_rh_state rh_state; 402 enum uhci_rh_state rh_state;
374 unsigned long auto_stop_time; /* When to AUTO_STOP */ 403 unsigned long auto_stop_time; /* When to AUTO_STOP */
@@ -382,6 +411,7 @@ struct uhci_hcd {
382 unsigned int hc_inaccessible:1; /* HC is suspended or dead */ 411 unsigned int hc_inaccessible:1; /* HC is suspended or dead */
383 unsigned int working_RD:1; /* Suspended root hub doesn't 412 unsigned int working_RD:1; /* Suspended root hub doesn't
384 need to be polled */ 413 need to be polled */
414 unsigned int is_initialized:1; /* Data structure is usable */
385 415
386 /* Support for port suspend/resume/reset */ 416 /* Support for port suspend/resume/reset */
387 unsigned long port_c_suspend; /* Bit-arrays of ports */ 417 unsigned long port_c_suspend; /* Bit-arrays of ports */
@@ -389,27 +419,16 @@ struct uhci_hcd {
389 unsigned long resuming_ports; 419 unsigned long resuming_ports;
390 unsigned long ports_timeout; /* Time to stop signalling */ 420 unsigned long ports_timeout; /* Time to stop signalling */
391 421
392 /* Main list of URBs currently controlled by this HC */
393 struct list_head urb_list;
394
395 /* List of QHs that are done, but waiting to be unlinked (race) */
396 struct list_head qh_remove_list;
397 unsigned int qh_remove_age; /* Age in frames */
398
399 /* List of TDs that are done, but waiting to be freed (race) */ 422 /* List of TDs that are done, but waiting to be freed (race) */
400 struct list_head td_remove_list; 423 struct list_head td_remove_list;
401 unsigned int td_remove_age; /* Age in frames */ 424 unsigned int td_remove_age; /* Age in frames */
402 425
403 /* List of asynchronously unlinked URBs */ 426 struct list_head idle_qh_list; /* Where the idle QHs live */
404 struct list_head urb_remove_list;
405 unsigned int urb_remove_age; /* Age in frames */
406
407 /* List of URBs awaiting completion callback */
408 struct list_head complete_list;
409 427
410 int rh_numports; /* Number of root-hub ports */ 428 int rh_numports; /* Number of root-hub ports */
411 429
412 wait_queue_head_t waitqh; /* endpoint_disable waiters */ 430 wait_queue_head_t waitqh; /* endpoint_disable waiters */
431 int num_waiting; /* Number of waiters */
413}; 432};
414 433
415/* Convert between a usb_hcd pointer and the corresponding uhci_hcd */ 434/* Convert between a usb_hcd pointer and the corresponding uhci_hcd */
@@ -429,7 +448,7 @@ static inline struct usb_hcd *uhci_to_hcd(struct uhci_hcd *uhci)
429 * Private per-URB data 448 * Private per-URB data
430 */ 449 */
431struct urb_priv { 450struct urb_priv {
432 struct list_head urb_list; 451 struct list_head node; /* Node in the QH's urbp list */
433 452
434 struct urb *urb; 453 struct urb *urb;
435 454
@@ -437,15 +456,8 @@ struct urb_priv {
437 struct list_head td_list; 456 struct list_head td_list;
438 457
439 unsigned fsbr : 1; /* URB turned on FSBR */ 458 unsigned fsbr : 1; /* URB turned on FSBR */
440 unsigned fsbr_timeout : 1; /* URB timed out on FSBR */ 459 unsigned short_transfer : 1; /* URB got a short transfer, no
441 unsigned queued : 1; /* QH was queued (not linked in) */ 460 * need to rescan */
442 unsigned short_control_packet : 1; /* If we get a short packet during */
443 /* a control transfer, retrigger */
444 /* the status phase */
445
446 unsigned long fsbrtime; /* In jiffies */
447
448 struct list_head queue_list;
449}; 461};
450 462
451 463
diff --git a/drivers/usb/host/uhci-hub.c b/drivers/usb/host/uhci-hub.c
index a71e48a66805..152971d16769 100644
--- a/drivers/usb/host/uhci-hub.c
+++ b/drivers/usb/host/uhci-hub.c
@@ -99,6 +99,21 @@ static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
99 } 99 }
100} 100}
101 101
102/* Wait for the UHCI controller in HP's iLO2 server management chip.
103 * It can take up to 250 us to finish a reset and set the CSC bit.
104 */
105static void wait_for_HP(unsigned long port_addr)
106{
107 int i;
108
109 for (i = 10; i < 250; i += 10) {
110 if (inw(port_addr) & USBPORTSC_CSC)
111 return;
112 udelay(10);
113 }
114 /* Log a warning? */
115}
116
102static void uhci_check_ports(struct uhci_hcd *uhci) 117static void uhci_check_ports(struct uhci_hcd *uhci)
103{ 118{
104 unsigned int port; 119 unsigned int port;
@@ -113,6 +128,12 @@ static void uhci_check_ports(struct uhci_hcd *uhci)
113 CLR_RH_PORTSTAT(USBPORTSC_PR); 128 CLR_RH_PORTSTAT(USBPORTSC_PR);
114 udelay(10); 129 udelay(10);
115 130
131 /* HP's server management chip requires
132 * a longer delay. */
133 if (to_pci_dev(uhci_dev(uhci))->vendor ==
134 PCI_VENDOR_ID_HP)
135 wait_for_HP(port_addr);
136
116 /* If the port was enabled before, turning 137 /* If the port was enabled before, turning
117 * reset on caused a port enable change. 138 * reset on caused a port enable change.
118 * Turning reset off causes a port connect 139 * Turning reset off causes a port connect
diff --git a/drivers/usb/host/uhci-q.c b/drivers/usb/host/uhci-q.c
index 782398045f9f..a06d84c19e13 100644
--- a/drivers/usb/host/uhci-q.c
+++ b/drivers/usb/host/uhci-q.c
@@ -13,13 +13,9 @@
13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 13 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 14 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 15 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
16 * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu 16 * (C) Copyright 2004-2005 Alan Stern, stern@rowland.harvard.edu
17 */ 17 */
18 18
19static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
20static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb);
21static void uhci_remove_pending_urbps(struct uhci_hcd *uhci);
22static void uhci_free_pending_qhs(struct uhci_hcd *uhci);
23static void uhci_free_pending_tds(struct uhci_hcd *uhci); 19static void uhci_free_pending_tds(struct uhci_hcd *uhci);
24 20
25/* 21/*
@@ -30,7 +26,7 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci);
30 * games with the FSBR code to make sure we get the correct order in all 26 * games with the FSBR code to make sure we get the correct order in all
31 * the cases. I don't think it's worth the effort 27 * the cases. I don't think it's worth the effort
32 */ 28 */
33static inline void uhci_set_next_interrupt(struct uhci_hcd *uhci) 29static void uhci_set_next_interrupt(struct uhci_hcd *uhci)
34{ 30{
35 if (uhci->is_stopped) 31 if (uhci->is_stopped)
36 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies); 32 mod_timer(&uhci_to_hcd(uhci)->rh_timer, jiffies);
@@ -42,12 +38,6 @@ static inline void uhci_clear_next_interrupt(struct uhci_hcd *uhci)
42 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC); 38 uhci->term_td->status &= ~cpu_to_le32(TD_CTRL_IOC);
43} 39}
44 40
45static inline void uhci_moveto_complete(struct uhci_hcd *uhci,
46 struct urb_priv *urbp)
47{
48 list_move_tail(&urbp->urb_list, &uhci->complete_list);
49}
50
51static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci) 41static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
52{ 42{
53 dma_addr_t dma_handle; 43 dma_addr_t dma_handle;
@@ -58,10 +48,6 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
58 return NULL; 48 return NULL;
59 49
60 td->dma_handle = dma_handle; 50 td->dma_handle = dma_handle;
61
62 td->link = UHCI_PTR_TERM;
63 td->buffer = 0;
64
65 td->frame = -1; 51 td->frame = -1;
66 52
67 INIT_LIST_HEAD(&td->list); 53 INIT_LIST_HEAD(&td->list);
@@ -71,6 +57,18 @@ static struct uhci_td *uhci_alloc_td(struct uhci_hcd *uhci)
71 return td; 57 return td;
72} 58}
73 59
60static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
61{
62 if (!list_empty(&td->list))
63 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
64 if (!list_empty(&td->remove_list))
65 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
66 if (!list_empty(&td->fl_list))
67 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
68
69 dma_pool_free(uhci->td_pool, td, td->dma_handle);
70}
71
74static inline void uhci_fill_td(struct uhci_td *td, u32 status, 72static inline void uhci_fill_td(struct uhci_td *td, u32 status,
75 u32 token, u32 buffer) 73 u32 token, u32 buffer)
76{ 74{
@@ -82,7 +80,8 @@ static inline void uhci_fill_td(struct uhci_td *td, u32 status,
82/* 80/*
83 * We insert Isochronous URBs directly into the frame list at the beginning 81 * We insert Isochronous URBs directly into the frame list at the beginning
84 */ 82 */
85static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td, unsigned framenum) 83static inline void uhci_insert_td_in_frame_list(struct uhci_hcd *uhci,
84 struct uhci_td *td, unsigned framenum)
86{ 85{
87 framenum &= (UHCI_NUMFRAMES - 1); 86 framenum &= (UHCI_NUMFRAMES - 1);
88 87
@@ -108,7 +107,7 @@ static void uhci_insert_td_frame_list(struct uhci_hcd *uhci, struct uhci_td *td,
108 } 107 }
109} 108}
110 109
111static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci, 110static inline void uhci_remove_td_from_frame_list(struct uhci_hcd *uhci,
112 struct uhci_td *td) 111 struct uhci_td *td)
113{ 112{
114 /* If it's not inserted, don't remove it */ 113 /* If it's not inserted, don't remove it */
@@ -139,48 +138,21 @@ static inline void uhci_remove_td_frame_list(struct uhci_hcd *uhci,
139 td->frame = -1; 138 td->frame = -1;
140} 139}
141 140
142static void unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb) 141/*
142 * Remove all the TDs for an Isochronous URB from the frame list
143 */
144static void uhci_unlink_isochronous_tds(struct uhci_hcd *uhci, struct urb *urb)
143{ 145{
144 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv; 146 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
145 struct uhci_td *td; 147 struct uhci_td *td;
146 148
147 list_for_each_entry(td, &urbp->td_list, list) 149 list_for_each_entry(td, &urbp->td_list, list)
148 uhci_remove_td_frame_list(uhci, td); 150 uhci_remove_td_from_frame_list(uhci, td);
149 wmb(); 151 wmb();
150} 152}
151 153
152/* 154static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci,
153 * Inserts a td list into qh. 155 struct usb_device *udev, struct usb_host_endpoint *hep)
154 */
155static void uhci_insert_tds_in_qh(struct uhci_qh *qh, struct urb *urb, __le32 breadth)
156{
157 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
158 struct uhci_td *td;
159 __le32 *plink;
160
161 /* Ordering isn't important here yet since the QH hasn't been */
162 /* inserted into the schedule yet */
163 plink = &qh->element;
164 list_for_each_entry(td, &urbp->td_list, list) {
165 *plink = cpu_to_le32(td->dma_handle) | breadth;
166 plink = &td->link;
167 }
168 *plink = UHCI_PTR_TERM;
169}
170
171static void uhci_free_td(struct uhci_hcd *uhci, struct uhci_td *td)
172{
173 if (!list_empty(&td->list))
174 dev_warn(uhci_dev(uhci), "td %p still in list!\n", td);
175 if (!list_empty(&td->remove_list))
176 dev_warn(uhci_dev(uhci), "td %p still in remove_list!\n", td);
177 if (!list_empty(&td->fl_list))
178 dev_warn(uhci_dev(uhci), "td %p still in fl_list!\n", td);
179
180 dma_pool_free(uhci->td_pool, td, td->dma_handle);
181}
182
183static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
184{ 156{
185 dma_addr_t dma_handle; 157 dma_addr_t dma_handle;
186 struct uhci_qh *qh; 158 struct uhci_qh *qh;
@@ -194,256 +166,217 @@ static struct uhci_qh *uhci_alloc_qh(struct uhci_hcd *uhci)
194 qh->element = UHCI_PTR_TERM; 166 qh->element = UHCI_PTR_TERM;
195 qh->link = UHCI_PTR_TERM; 167 qh->link = UHCI_PTR_TERM;
196 168
197 qh->urbp = NULL; 169 INIT_LIST_HEAD(&qh->queue);
198 170 INIT_LIST_HEAD(&qh->node);
199 INIT_LIST_HEAD(&qh->list);
200 INIT_LIST_HEAD(&qh->remove_list);
201 171
172 if (udev) { /* Normal QH */
173 qh->dummy_td = uhci_alloc_td(uhci);
174 if (!qh->dummy_td) {
175 dma_pool_free(uhci->qh_pool, qh, dma_handle);
176 return NULL;
177 }
178 qh->state = QH_STATE_IDLE;
179 qh->hep = hep;
180 qh->udev = udev;
181 hep->hcpriv = qh;
182
183 } else { /* Skeleton QH */
184 qh->state = QH_STATE_ACTIVE;
185 qh->udev = NULL;
186 }
202 return qh; 187 return qh;
203} 188}
204 189
205static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 190static void uhci_free_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
206{ 191{
207 if (!list_empty(&qh->list)) 192 WARN_ON(qh->state != QH_STATE_IDLE && qh->udev);
193 if (!list_empty(&qh->queue))
208 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh); 194 dev_warn(uhci_dev(uhci), "qh %p list not empty!\n", qh);
209 if (!list_empty(&qh->remove_list))
210 dev_warn(uhci_dev(uhci), "qh %p still in remove_list!\n", qh);
211 195
196 list_del(&qh->node);
197 if (qh->udev) {
198 qh->hep->hcpriv = NULL;
199 uhci_free_td(uhci, qh->dummy_td);
200 }
212 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle); 201 dma_pool_free(uhci->qh_pool, qh, qh->dma_handle);
213} 202}
214 203
215/* 204/*
216 * Append this urb's qh after the last qh in skelqh->list 205 * When the currently executing URB is dequeued, save its current toggle value
217 *
218 * Note that urb_priv.queue_list doesn't have a separate queue head;
219 * it's a ring with every element "live".
220 */ 206 */
221static void uhci_insert_qh(struct uhci_hcd *uhci, struct uhci_qh *skelqh, struct urb *urb) 207static void uhci_save_toggle(struct uhci_qh *qh, struct urb *urb)
222{ 208{
223 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 209 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
224 struct urb_priv *turbp; 210 struct uhci_td *td;
225 struct uhci_qh *lqh;
226 211
227 /* Grab the last QH */ 212 /* If the QH element pointer is UHCI_PTR_TERM then then currently
228 lqh = list_entry(skelqh->list.prev, struct uhci_qh, list); 213 * executing URB has already been unlinked, so this one isn't it. */
214 if (qh_element(qh) == UHCI_PTR_TERM ||
215 qh->queue.next != &urbp->node)
216 return;
217 qh->element = UHCI_PTR_TERM;
229 218
230 /* Point to the next skelqh */ 219 /* Only bulk and interrupt pipes have to worry about toggles */
231 urbp->qh->link = lqh->link; 220 if (!(usb_pipetype(urb->pipe) == PIPE_BULK ||
232 wmb(); /* Ordering is important */ 221 usb_pipetype(urb->pipe) == PIPE_INTERRUPT))
222 return;
233 223
234 /* 224 /* Find the first active TD; that's the device's toggle state */
235 * Patch QHs for previous endpoint's queued URBs? HC goes 225 list_for_each_entry(td, &urbp->td_list, list) {
236 * here next, not to the next skelqh it now points to. 226 if (td_status(td) & TD_CTRL_ACTIVE) {
237 * 227 qh->needs_fixup = 1;
238 * lqh --> td ... --> qh ... --> td --> qh ... --> td 228 qh->initial_toggle = uhci_toggle(td_token(td));
239 * | | | 229 return;
240 * v v v 230 }
241 * +<----------------+-----------------+
242 * v
243 * newqh --> td ... --> td
244 * |
245 * v
246 * ...
247 *
248 * The HC could see (and use!) any of these as we write them.
249 */
250 lqh->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
251 if (lqh->urbp) {
252 list_for_each_entry(turbp, &lqh->urbp->queue_list, queue_list)
253 turbp->qh->link = lqh->link;
254 } 231 }
255 232
256 list_add_tail(&urbp->qh->list, &skelqh->list); 233 WARN_ON(1);
257} 234}
258 235
259/* 236/*
260 * Start removal of QH from schedule; it finishes next frame. 237 * Fix up the data toggles for URBs in a queue, when one of them
261 * TDs should be unlinked before this is called. 238 * terminates early (short transfer, error, or dequeued).
262 */ 239 */
263static void uhci_remove_qh(struct uhci_hcd *uhci, struct uhci_qh *qh) 240static void uhci_fixup_toggles(struct uhci_qh *qh, int skip_first)
264{ 241{
265 struct uhci_qh *pqh; 242 struct urb_priv *urbp = NULL;
266 __le32 newlink; 243 struct uhci_td *td;
267 244 unsigned int toggle = qh->initial_toggle;
268 if (!qh) 245 unsigned int pipe;
269 return; 246
270 247 /* Fixups for a short transfer start with the second URB in the
271 /* 248 * queue (the short URB is the first). */
272 * Only go through the hoops if it's actually linked in 249 if (skip_first)
273 */ 250 urbp = list_entry(qh->queue.next, struct urb_priv, node);
274 if (!list_empty(&qh->list)) { 251
275 252 /* When starting with the first URB, if the QH element pointer is
276 /* If our queue is nonempty, make the next URB the head */ 253 * still valid then we know the URB's toggles are okay. */
277 if (!list_empty(&qh->urbp->queue_list)) { 254 else if (qh_element(qh) != UHCI_PTR_TERM)
278 struct urb_priv *nurbp; 255 toggle = 2;
279 256
280 nurbp = list_entry(qh->urbp->queue_list.next, 257 /* Fix up the toggle for the URBs in the queue. Normally this
281 struct urb_priv, queue_list); 258 * loop won't run more than once: When an error or short transfer
282 nurbp->queued = 0; 259 * occurs, the queue usually gets emptied. */
283 list_add(&nurbp->qh->list, &qh->list); 260 urbp = list_prepare_entry(urbp, &qh->queue, node);
284 newlink = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 261 list_for_each_entry_continue(urbp, &qh->queue, node) {
285 } else 262
286 newlink = qh->link; 263 /* If the first TD has the right toggle value, we don't
287 264 * need to change any toggles in this URB */
288 /* Fix up the previous QH's queue to link to either 265 td = list_entry(urbp->td_list.next, struct uhci_td, list);
289 * the new head of this queue or the start of the 266 if (toggle > 1 || uhci_toggle(td_token(td)) == toggle) {
290 * next endpoint's queue. */ 267 td = list_entry(urbp->td_list.next, struct uhci_td,
291 pqh = list_entry(qh->list.prev, struct uhci_qh, list); 268 list);
292 pqh->link = newlink; 269 toggle = uhci_toggle(td_token(td)) ^ 1;
293 if (pqh->urbp) { 270
294 struct urb_priv *turbp; 271 /* Otherwise all the toggles in the URB have to be switched */
295 272 } else {
296 list_for_each_entry(turbp, &pqh->urbp->queue_list, 273 list_for_each_entry(td, &urbp->td_list, list) {
297 queue_list) 274 td->token ^= __constant_cpu_to_le32(
298 turbp->qh->link = newlink; 275 TD_TOKEN_TOGGLE);
276 toggle ^= 1;
277 }
299 } 278 }
300 wmb();
301
302 /* Leave qh->link in case the HC is on the QH now, it will */
303 /* continue the rest of the schedule */
304 qh->element = UHCI_PTR_TERM;
305
306 list_del_init(&qh->list);
307 }
308
309 list_del_init(&qh->urbp->queue_list);
310 qh->urbp = NULL;
311
312 uhci_get_current_frame_number(uhci);
313 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age) {
314 uhci_free_pending_qhs(uhci);
315 uhci->qh_remove_age = uhci->frame_number;
316 } 279 }
317 280
318 /* Check to see if the remove list is empty. Set the IOC bit */ 281 wmb();
319 /* to force an interrupt so we can remove the QH */ 282 pipe = list_entry(qh->queue.next, struct urb_priv, node)->urb->pipe;
320 if (list_empty(&uhci->qh_remove_list)) 283 usb_settoggle(qh->udev, usb_pipeendpoint(pipe),
321 uhci_set_next_interrupt(uhci); 284 usb_pipeout(pipe), toggle);
322 285 qh->needs_fixup = 0;
323 list_add(&qh->remove_list, &uhci->qh_remove_list);
324} 286}
325 287
326static int uhci_fixup_toggle(struct urb *urb, unsigned int toggle) 288/*
289 * Put a QH on the schedule in both hardware and software
290 */
291static void uhci_activate_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
327{ 292{
328 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 293 struct uhci_qh *pqh;
329 struct uhci_td *td;
330
331 list_for_each_entry(td, &urbp->td_list, list) {
332 if (toggle)
333 td->token |= cpu_to_le32(TD_TOKEN_TOGGLE);
334 else
335 td->token &= ~cpu_to_le32(TD_TOKEN_TOGGLE);
336
337 toggle ^= 1;
338 }
339
340 return toggle;
341}
342 294
343/* This function will append one URB's QH to another URB's QH. This is for */ 295 WARN_ON(list_empty(&qh->queue));
344/* queuing interrupt, control or bulk transfers */
345static void uhci_append_queued_urb(struct uhci_hcd *uhci, struct urb *eurb, struct urb *urb)
346{
347 struct urb_priv *eurbp, *urbp, *furbp, *lurbp;
348 struct uhci_td *lltd;
349 296
350 eurbp = eurb->hcpriv; 297 /* Set the element pointer if it isn't set already.
351 urbp = urb->hcpriv; 298 * This isn't needed for Isochronous queues, but it doesn't hurt. */
299 if (qh_element(qh) == UHCI_PTR_TERM) {
300 struct urb_priv *urbp = list_entry(qh->queue.next,
301 struct urb_priv, node);
302 struct uhci_td *td = list_entry(urbp->td_list.next,
303 struct uhci_td, list);
352 304
353 /* Find the first URB in the queue */ 305 qh->element = cpu_to_le32(td->dma_handle);
354 furbp = eurbp;
355 if (eurbp->queued) {
356 list_for_each_entry(furbp, &eurbp->queue_list, queue_list)
357 if (!furbp->queued)
358 break;
359 } 306 }
360 307
361 lurbp = list_entry(furbp->queue_list.prev, struct urb_priv, queue_list); 308 if (qh->state == QH_STATE_ACTIVE)
362 309 return;
363 lltd = list_entry(lurbp->td_list.prev, struct uhci_td, list); 310 qh->state = QH_STATE_ACTIVE;
364 311
365 /* Control transfers always start with toggle 0 */ 312 /* Move the QH from its old list to the end of the appropriate
366 if (!usb_pipecontrol(urb->pipe)) 313 * skeleton's list */
367 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 314 if (qh == uhci->next_qh)
368 usb_pipeout(urb->pipe), 315 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
369 uhci_fixup_toggle(urb, 316 node);
370 uhci_toggle(td_token(lltd)) ^ 1)); 317 list_move_tail(&qh->node, &qh->skel->node);
371 318
372 /* All qhs in the queue need to link to the next queue */ 319 /* Link it into the schedule */
373 urbp->qh->link = eurbp->qh->link; 320 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
374 321 qh->link = pqh->link;
375 wmb(); /* Make sure we flush everything */ 322 wmb();
376 323 pqh->link = UHCI_PTR_QH | cpu_to_le32(qh->dma_handle);
377 lltd->link = cpu_to_le32(urbp->qh->dma_handle) | UHCI_PTR_QH;
378
379 list_add_tail(&urbp->queue_list, &furbp->queue_list);
380
381 urbp->queued = 1;
382} 324}
383 325
384static void uhci_delete_queued_urb(struct uhci_hcd *uhci, struct urb *urb) 326/*
327 * Take a QH off the hardware schedule
328 */
329static void uhci_unlink_qh(struct uhci_hcd *uhci, struct uhci_qh *qh)
385{ 330{
386 struct urb_priv *urbp, *nurbp, *purbp, *turbp; 331 struct uhci_qh *pqh;
387 struct uhci_td *pltd;
388 unsigned int toggle;
389
390 urbp = urb->hcpriv;
391 332
392 if (list_empty(&urbp->queue_list)) 333 if (qh->state == QH_STATE_UNLINKING)
393 return; 334 return;
335 WARN_ON(qh->state != QH_STATE_ACTIVE || !qh->udev);
336 qh->state = QH_STATE_UNLINKING;
394 337
395 nurbp = list_entry(urbp->queue_list.next, struct urb_priv, queue_list); 338 /* Unlink the QH from the schedule and record when we did it */
339 pqh = list_entry(qh->node.prev, struct uhci_qh, node);
340 pqh->link = qh->link;
341 mb();
396 342
397 /* 343 uhci_get_current_frame_number(uhci);
398 * Fix up the toggle for the following URBs in the queue. 344 qh->unlink_frame = uhci->frame_number;
399 * Only needed for bulk and interrupt: control and isochronous
400 * endpoints don't propagate toggles between messages.
401 */
402 if (usb_pipebulk(urb->pipe) || usb_pipeint(urb->pipe)) {
403 if (!urbp->queued)
404 /* We just set the toggle in uhci_unlink_generic */
405 toggle = usb_gettoggle(urb->dev,
406 usb_pipeendpoint(urb->pipe),
407 usb_pipeout(urb->pipe));
408 else {
409 /* If we're in the middle of the queue, grab the */
410 /* toggle from the TD previous to us */
411 purbp = list_entry(urbp->queue_list.prev,
412 struct urb_priv, queue_list);
413 pltd = list_entry(purbp->td_list.prev,
414 struct uhci_td, list);
415 toggle = uhci_toggle(td_token(pltd)) ^ 1;
416 }
417 345
418 list_for_each_entry(turbp, &urbp->queue_list, queue_list) { 346 /* Force an interrupt so we know when the QH is fully unlinked */
419 if (!turbp->queued) 347 if (list_empty(&uhci->skel_unlink_qh->node))
420 break; 348 uhci_set_next_interrupt(uhci);
421 toggle = uhci_fixup_toggle(turbp->urb, toggle);
422 }
423 349
424 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), 350 /* Move the QH from its old list to the end of the unlinking list */
425 usb_pipeout(urb->pipe), toggle); 351 if (qh == uhci->next_qh)
426 } 352 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
353 node);
354 list_move_tail(&qh->node, &uhci->skel_unlink_qh->node);
355}
427 356
428 if (urbp->queued) { 357/*
429 /* We're somewhere in the middle (or end). The case where 358 * When we and the controller are through with a QH, it becomes IDLE.
430 * we're at the head is handled in uhci_remove_qh(). */ 359 * This happens when a QH has been off the schedule (on the unlinking
431 purbp = list_entry(urbp->queue_list.prev, struct urb_priv, 360 * list) for more than one frame, or when an error occurs while adding
432 queue_list); 361 * the first URB onto a new QH.
362 */
363static void uhci_make_qh_idle(struct uhci_hcd *uhci, struct uhci_qh *qh)
364{
365 WARN_ON(qh->state == QH_STATE_ACTIVE);
433 366
434 pltd = list_entry(purbp->td_list.prev, struct uhci_td, list); 367 if (qh == uhci->next_qh)
435 if (nurbp->queued) 368 uhci->next_qh = list_entry(qh->node.next, struct uhci_qh,
436 pltd->link = cpu_to_le32(nurbp->qh->dma_handle) | UHCI_PTR_QH; 369 node);
437 else 370 list_move(&qh->node, &uhci->idle_qh_list);
438 /* The next URB happens to be the beginning, so */ 371 qh->state = QH_STATE_IDLE;
439 /* we're the last, end the chain */
440 pltd->link = UHCI_PTR_TERM;
441 }
442 372
443 /* urbp->queue_list is handled in uhci_remove_qh() */ 373 /* If anyone is waiting for a QH to become idle, wake them up */
374 if (uhci->num_waiting)
375 wake_up_all(&uhci->waitqh);
444} 376}
445 377
446static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 378static inline struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci,
379 struct urb *urb)
447{ 380{
448 struct urb_priv *urbp; 381 struct urb_priv *urbp;
449 382
@@ -453,16 +386,11 @@ static struct urb_priv *uhci_alloc_urb_priv(struct uhci_hcd *uhci, struct urb *u
453 386
454 memset((void *)urbp, 0, sizeof(*urbp)); 387 memset((void *)urbp, 0, sizeof(*urbp));
455 388
456 urbp->fsbrtime = jiffies;
457 urbp->urb = urb; 389 urbp->urb = urb;
390 urb->hcpriv = urbp;
458 391
392 INIT_LIST_HEAD(&urbp->node);
459 INIT_LIST_HEAD(&urbp->td_list); 393 INIT_LIST_HEAD(&urbp->td_list);
460 INIT_LIST_HEAD(&urbp->queue_list);
461 INIT_LIST_HEAD(&urbp->urb_list);
462
463 list_add_tail(&urbp->urb_list, &uhci->urb_list);
464
465 urb->hcpriv = urbp;
466 394
467 return urbp; 395 return urbp;
468} 396}
@@ -482,18 +410,14 @@ static void uhci_remove_td_from_urb(struct uhci_td *td)
482 list_del_init(&td->list); 410 list_del_init(&td->list);
483} 411}
484 412
485static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb) 413static void uhci_free_urb_priv(struct uhci_hcd *uhci,
414 struct urb_priv *urbp)
486{ 415{
487 struct uhci_td *td, *tmp; 416 struct uhci_td *td, *tmp;
488 struct urb_priv *urbp;
489
490 urbp = (struct urb_priv *)urb->hcpriv;
491 if (!urbp)
492 return;
493 417
494 if (!list_empty(&urbp->urb_list)) 418 if (!list_empty(&urbp->node))
495 dev_warn(uhci_dev(uhci), "urb %p still on uhci->urb_list " 419 dev_warn(uhci_dev(uhci), "urb %p still on QH's list!\n",
496 "or uhci->remove_list!\n", urb); 420 urbp->urb);
497 421
498 uhci_get_current_frame_number(uhci); 422 uhci_get_current_frame_number(uhci);
499 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) { 423 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) {
@@ -502,7 +426,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
502 } 426 }
503 427
504 /* Check to see if the remove list is empty. Set the IOC bit */ 428 /* Check to see if the remove list is empty. Set the IOC bit */
505 /* to force an interrupt so we can remove the TDs*/ 429 /* to force an interrupt so we can remove the TDs. */
506 if (list_empty(&uhci->td_remove_list)) 430 if (list_empty(&uhci->td_remove_list))
507 uhci_set_next_interrupt(uhci); 431 uhci_set_next_interrupt(uhci);
508 432
@@ -511,7 +435,7 @@ static void uhci_destroy_urb_priv(struct uhci_hcd *uhci, struct urb *urb)
511 list_add(&td->remove_list, &uhci->td_remove_list); 435 list_add(&td->remove_list, &uhci->td_remove_list);
512 } 436 }
513 437
514 urb->hcpriv = NULL; 438 urbp->urb->hcpriv = NULL;
515 kmem_cache_free(uhci_up_cachep, urbp); 439 kmem_cache_free(uhci_up_cachep, urbp);
516} 440}
517 441
@@ -570,34 +494,33 @@ static int uhci_map_status(int status, int dir_out)
570/* 494/*
571 * Control transfers 495 * Control transfers
572 */ 496 */
573static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 497static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb,
498 struct uhci_qh *qh)
574{ 499{
575 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
576 struct uhci_td *td; 500 struct uhci_td *td;
577 struct uhci_qh *qh, *skelqh;
578 unsigned long destination, status; 501 unsigned long destination, status;
579 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 502 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
580 int len = urb->transfer_buffer_length; 503 int len = urb->transfer_buffer_length;
581 dma_addr_t data = urb->transfer_dma; 504 dma_addr_t data = urb->transfer_dma;
505 __le32 *plink;
582 506
583 /* The "pipe" thing contains the destination in bits 8--18 */ 507 /* The "pipe" thing contains the destination in bits 8--18 */
584 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; 508 destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
585 509
586 /* 3 errors */ 510 /* 3 errors, dummy TD remains inactive */
587 status = TD_CTRL_ACTIVE | uhci_maxerr(3); 511 status = uhci_maxerr(3);
588 if (urb->dev->speed == USB_SPEED_LOW) 512 if (urb->dev->speed == USB_SPEED_LOW)
589 status |= TD_CTRL_LS; 513 status |= TD_CTRL_LS;
590 514
591 /* 515 /*
592 * Build the TD for the control request setup packet 516 * Build the TD for the control request setup packet
593 */ 517 */
594 td = uhci_alloc_td(uhci); 518 td = qh->dummy_td;
595 if (!td)
596 return -ENOMEM;
597
598 uhci_add_td_to_urb(urb, td); 519 uhci_add_td_to_urb(urb, td);
599 uhci_fill_td(td, status, destination | uhci_explen(8), 520 uhci_fill_td(td, status, destination | uhci_explen(8),
600 urb->setup_dma); 521 urb->setup_dma);
522 plink = &td->link;
523 status |= TD_CTRL_ACTIVE;
601 524
602 /* 525 /*
603 * If direction is "send", change the packet ID from SETUP (0x2D) 526 * If direction is "send", change the packet ID from SETUP (0x2D)
@@ -615,21 +538,20 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
615 * Build the DATA TDs 538 * Build the DATA TDs
616 */ 539 */
617 while (len > 0) { 540 while (len > 0) {
618 int pktsze = len; 541 int pktsze = min(len, maxsze);
619
620 if (pktsze > maxsze)
621 pktsze = maxsze;
622 542
623 td = uhci_alloc_td(uhci); 543 td = uhci_alloc_td(uhci);
624 if (!td) 544 if (!td)
625 return -ENOMEM; 545 goto nomem;
546 *plink = cpu_to_le32(td->dma_handle);
626 547
627 /* Alternate Data0/1 (start with Data1) */ 548 /* Alternate Data0/1 (start with Data1) */
628 destination ^= TD_TOKEN_TOGGLE; 549 destination ^= TD_TOKEN_TOGGLE;
629 550
630 uhci_add_td_to_urb(urb, td); 551 uhci_add_td_to_urb(urb, td);
631 uhci_fill_td(td, status, destination | uhci_explen(pktsze), 552 uhci_fill_td(td, status, destination | uhci_explen(pktsze),
632 data); 553 data);
554 plink = &td->link;
633 555
634 data += pktsze; 556 data += pktsze;
635 len -= pktsze; 557 len -= pktsze;
@@ -640,7 +562,8 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
640 */ 562 */
641 td = uhci_alloc_td(uhci); 563 td = uhci_alloc_td(uhci);
642 if (!td) 564 if (!td)
643 return -ENOMEM; 565 goto nomem;
566 *plink = cpu_to_le32(td->dma_handle);
644 567
645 /* 568 /*
646 * It's IN if the pipe is an output pipe or we're not expecting 569 * It's IN if the pipe is an output pipe or we're not expecting
@@ -658,16 +581,21 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
658 581
659 uhci_add_td_to_urb(urb, td); 582 uhci_add_td_to_urb(urb, td);
660 uhci_fill_td(td, status | TD_CTRL_IOC, 583 uhci_fill_td(td, status | TD_CTRL_IOC,
661 destination | uhci_explen(0), 0); 584 destination | uhci_explen(0), 0);
662 585 plink = &td->link;
663 qh = uhci_alloc_qh(uhci);
664 if (!qh)
665 return -ENOMEM;
666 586
667 urbp->qh = qh; 587 /*
668 qh->urbp = urbp; 588 * Build the new dummy TD and activate the old one
589 */
590 td = uhci_alloc_td(uhci);
591 if (!td)
592 goto nomem;
593 *plink = cpu_to_le32(td->dma_handle);
669 594
670 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 595 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
596 wmb();
597 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
598 qh->dummy_td = td;
671 599
672 /* Low-speed transfers get a different queue, and won't hog the bus. 600 /* Low-speed transfers get a different queue, and won't hog the bus.
673 * Also, some devices enumerate better without FSBR; the easiest way 601 * Also, some devices enumerate better without FSBR; the easiest way
@@ -675,18 +603,17 @@ static int uhci_submit_control(struct uhci_hcd *uhci, struct urb *urb, struct ur
675 * isn't in the CONFIGURED state. */ 603 * isn't in the CONFIGURED state. */
676 if (urb->dev->speed == USB_SPEED_LOW || 604 if (urb->dev->speed == USB_SPEED_LOW ||
677 urb->dev->state != USB_STATE_CONFIGURED) 605 urb->dev->state != USB_STATE_CONFIGURED)
678 skelqh = uhci->skel_ls_control_qh; 606 qh->skel = uhci->skel_ls_control_qh;
679 else { 607 else {
680 skelqh = uhci->skel_fs_control_qh; 608 qh->skel = uhci->skel_fs_control_qh;
681 uhci_inc_fsbr(uhci, urb); 609 uhci_inc_fsbr(uhci, urb);
682 } 610 }
611 return 0;
683 612
684 if (eurb) 613nomem:
685 uhci_append_queued_urb(uhci, eurb, urb); 614 /* Remove the dummy TD from the td_list so it doesn't get freed */
686 else 615 uhci_remove_td_from_urb(qh->dummy_td);
687 uhci_insert_qh(uhci, skelqh, urb); 616 return -ENOMEM;
688
689 return -EINPROGRESS;
690} 617}
691 618
692/* 619/*
@@ -703,7 +630,7 @@ static int usb_control_retrigger_status(struct uhci_hcd *uhci, struct urb *urb)
703 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 630 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
704 struct uhci_td *td; 631 struct uhci_td *td;
705 632
706 urbp->short_control_packet = 1; 633 urbp->short_transfer = 1;
707 634
708 td = list_entry(urbp->td_list.prev, struct uhci_td, list); 635 td = list_entry(urbp->td_list.prev, struct uhci_td, list);
709 urbp->qh->element = cpu_to_le32(td->dma_handle); 636 urbp->qh->element = cpu_to_le32(td->dma_handle);
@@ -720,16 +647,14 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
720 unsigned int status; 647 unsigned int status;
721 int ret = 0; 648 int ret = 0;
722 649
723 if (list_empty(&urbp->td_list))
724 return -EINVAL;
725
726 head = &urbp->td_list; 650 head = &urbp->td_list;
727 651 if (urbp->short_transfer) {
728 if (urbp->short_control_packet) {
729 tmp = head->prev; 652 tmp = head->prev;
730 goto status_stage; 653 goto status_stage;
731 } 654 }
732 655
656 urb->actual_length = 0;
657
733 tmp = head->next; 658 tmp = head->next;
734 td = list_entry(tmp, struct uhci_td, list); 659 td = list_entry(tmp, struct uhci_td, list);
735 660
@@ -742,8 +667,6 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
742 if (status) 667 if (status)
743 goto td_error; 668 goto td_error;
744 669
745 urb->actual_length = 0;
746
747 /* The rest of the TDs (but the last) are data */ 670 /* The rest of the TDs (but the last) are data */
748 tmp = tmp->next; 671 tmp = tmp->next;
749 while (tmp != head && tmp->next != head) { 672 while (tmp != head && tmp->next != head) {
@@ -770,10 +693,7 @@ static int uhci_result_control(struct uhci_hcd *uhci, struct urb *urb)
770 goto err; 693 goto err;
771 } 694 }
772 695
773 if (uhci_packetid(td_token(td)) == USB_PID_IN) 696 return usb_control_retrigger_status(uhci, urb);
774 return usb_control_retrigger_status(uhci, urb);
775 else
776 return 0;
777 } 697 }
778 } 698 }
779 699
@@ -814,34 +734,40 @@ err:
814 if (errbuf) { 734 if (errbuf) {
815 /* Print the chain for debugging purposes */ 735 /* Print the chain for debugging purposes */
816 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 736 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
817
818 lprintk(errbuf); 737 lprintk(errbuf);
819 } 738 }
820 } 739 }
821 740
741 /* Note that the queue has stopped */
742 urbp->qh->element = UHCI_PTR_TERM;
743 urbp->qh->is_stopped = 1;
822 return ret; 744 return ret;
823} 745}
824 746
825/* 747/*
826 * Common submit for bulk and interrupt 748 * Common submit for bulk and interrupt
827 */ 749 */
828static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb, struct uhci_qh *skelqh) 750static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb,
751 struct uhci_qh *qh)
829{ 752{
830 struct uhci_td *td; 753 struct uhci_td *td;
831 struct uhci_qh *qh;
832 unsigned long destination, status; 754 unsigned long destination, status;
833 int maxsze = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)); 755 int maxsze = le16_to_cpu(qh->hep->desc.wMaxPacketSize);
834 int len = urb->transfer_buffer_length; 756 int len = urb->transfer_buffer_length;
835 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
836 dma_addr_t data = urb->transfer_dma; 757 dma_addr_t data = urb->transfer_dma;
758 __le32 *plink;
759 unsigned int toggle;
837 760
838 if (len < 0) 761 if (len < 0)
839 return -EINVAL; 762 return -EINVAL;
840 763
841 /* The "pipe" thing contains the destination in bits 8--18 */ 764 /* The "pipe" thing contains the destination in bits 8--18 */
842 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe); 765 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
766 toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
767 usb_pipeout(urb->pipe));
843 768
844 status = uhci_maxerr(3) | TD_CTRL_ACTIVE; 769 /* 3 errors, dummy TD remains inactive */
770 status = uhci_maxerr(3);
845 if (urb->dev->speed == USB_SPEED_LOW) 771 if (urb->dev->speed == USB_SPEED_LOW)
846 status |= TD_CTRL_LS; 772 status |= TD_CTRL_LS;
847 if (usb_pipein(urb->pipe)) 773 if (usb_pipein(urb->pipe))
@@ -850,30 +776,34 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
850 /* 776 /*
851 * Build the DATA TDs 777 * Build the DATA TDs
852 */ 778 */
779 plink = NULL;
780 td = qh->dummy_td;
853 do { /* Allow zero length packets */ 781 do { /* Allow zero length packets */
854 int pktsze = maxsze; 782 int pktsze = maxsze;
855 783
856 if (pktsze >= len) { 784 if (len <= pktsze) { /* The last packet */
857 pktsze = len; 785 pktsze = len;
858 if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) 786 if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
859 status &= ~TD_CTRL_SPD; 787 status &= ~TD_CTRL_SPD;
860 } 788 }
861 789
862 td = uhci_alloc_td(uhci); 790 if (plink) {
863 if (!td) 791 td = uhci_alloc_td(uhci);
864 return -ENOMEM; 792 if (!td)
865 793 goto nomem;
794 *plink = cpu_to_le32(td->dma_handle);
795 }
866 uhci_add_td_to_urb(urb, td); 796 uhci_add_td_to_urb(urb, td);
867 uhci_fill_td(td, status, destination | uhci_explen(pktsze) | 797 uhci_fill_td(td, status,
868 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 798 destination | uhci_explen(pktsze) |
869 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 799 (toggle << TD_TOKEN_TOGGLE_SHIFT),
870 data); 800 data);
801 plink = &td->link;
802 status |= TD_CTRL_ACTIVE;
871 803
872 data += pktsze; 804 data += pktsze;
873 len -= maxsze; 805 len -= maxsze;
874 806 toggle ^= 1;
875 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe),
876 usb_pipeout(urb->pipe));
877 } while (len > 0); 807 } while (len > 0);
878 808
879 /* 809 /*
@@ -883,20 +813,22 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
883 * however, if transfer_length == 0, the zero packet was already 813 * however, if transfer_length == 0, the zero packet was already
884 * prepared above. 814 * prepared above.
885 */ 815 */
886 if (usb_pipeout(urb->pipe) && (urb->transfer_flags & URB_ZERO_PACKET) && 816 if ((urb->transfer_flags & URB_ZERO_PACKET) &&
887 !len && urb->transfer_buffer_length) { 817 usb_pipeout(urb->pipe) && len == 0 &&
818 urb->transfer_buffer_length > 0) {
888 td = uhci_alloc_td(uhci); 819 td = uhci_alloc_td(uhci);
889 if (!td) 820 if (!td)
890 return -ENOMEM; 821 goto nomem;
822 *plink = cpu_to_le32(td->dma_handle);
891 823
892 uhci_add_td_to_urb(urb, td); 824 uhci_add_td_to_urb(urb, td);
893 uhci_fill_td(td, status, destination | uhci_explen(0) | 825 uhci_fill_td(td, status,
894 (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), 826 destination | uhci_explen(0) |
895 usb_pipeout(urb->pipe)) << TD_TOKEN_TOGGLE_SHIFT), 827 (toggle << TD_TOKEN_TOGGLE_SHIFT),
896 data); 828 data);
829 plink = &td->link;
897 830
898 usb_dotoggle(urb->dev, usb_pipeendpoint(urb->pipe), 831 toggle ^= 1;
899 usb_pipeout(urb->pipe));
900 } 832 }
901 833
902 /* Set the interrupt-on-completion flag on the last packet. 834 /* Set the interrupt-on-completion flag on the last packet.
@@ -905,24 +837,29 @@ static int uhci_submit_common(struct uhci_hcd *uhci, struct urb *urb, struct urb
905 * fast side but not enough to justify delaying an interrupt 837 * fast side but not enough to justify delaying an interrupt
906 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT 838 * more than 2 or 3 URBs, so we will ignore the URB_NO_INTERRUPT
907 * flag setting. */ 839 * flag setting. */
908 td->status |= cpu_to_le32(TD_CTRL_IOC); 840 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
909 841
910 qh = uhci_alloc_qh(uhci); 842 /*
911 if (!qh) 843 * Build the new dummy TD and activate the old one
912 return -ENOMEM; 844 */
913 845 td = uhci_alloc_td(uhci);
914 urbp->qh = qh; 846 if (!td)
915 qh->urbp = urbp; 847 goto nomem;
848 *plink = cpu_to_le32(td->dma_handle);
916 849
917 /* Always breadth first */ 850 uhci_fill_td(td, 0, USB_PID_OUT | uhci_explen(0), 0);
918 uhci_insert_tds_in_qh(qh, urb, UHCI_PTR_BREADTH); 851 wmb();
852 qh->dummy_td->status |= __constant_cpu_to_le32(TD_CTRL_ACTIVE);
853 qh->dummy_td = td;
919 854
920 if (eurb) 855 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
921 uhci_append_queued_urb(uhci, eurb, urb); 856 usb_pipeout(urb->pipe), toggle);
922 else 857 return 0;
923 uhci_insert_qh(uhci, skelqh, urb);
924 858
925 return -EINPROGRESS; 859nomem:
860 /* Remove the dummy TD from the td_list so it doesn't get freed */
861 uhci_remove_td_from_urb(qh->dummy_td);
862 return -ENOMEM;
926} 863}
927 864
928/* 865/*
@@ -954,8 +891,27 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
954 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 891 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
955 ret = -EREMOTEIO; 892 ret = -EREMOTEIO;
956 goto err; 893 goto err;
957 } else 894 }
958 return 0; 895
896 /*
897 * This URB stopped short of its end. We have to
898 * fix up the toggles of the following URBs on the
899 * queue and restart the queue.
900 *
901 * Do this only the first time we encounter the
902 * short URB.
903 */
904 if (!urbp->short_transfer) {
905 urbp->short_transfer = 1;
906 urbp->qh->initial_toggle =
907 uhci_toggle(td_token(td)) ^ 1;
908 uhci_fixup_toggles(urbp->qh, 1);
909
910 td = list_entry(urbp->td_list.prev,
911 struct uhci_td, list);
912 urbp->qh->element = td->link;
913 }
914 break;
959 } 915 }
960 } 916 }
961 917
@@ -964,31 +920,30 @@ static int uhci_result_common(struct uhci_hcd *uhci, struct urb *urb)
964td_error: 920td_error:
965 ret = uhci_map_status(status, uhci_packetout(td_token(td))); 921 ret = uhci_map_status(status, uhci_packetout(td_token(td)));
966 922
967err:
968 /*
969 * Enable this chunk of code if you want to see some more debugging.
970 * But be careful, it has the tendancy to starve out khubd and prevent
971 * disconnects from happening successfully if you have a slow debug
972 * log interface (like a serial console.
973 */
974#if 0
975 if ((debug == 1 && ret != -EPIPE) || debug > 1) { 923 if ((debug == 1 && ret != -EPIPE) || debug > 1) {
976 /* Some debugging code */ 924 /* Some debugging code */
977 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n", 925 dev_dbg(uhci_dev(uhci), "%s: failed with status %x\n",
978 __FUNCTION__, status); 926 __FUNCTION__, status);
979 927
980 if (errbuf) { 928 if (debug > 1 && errbuf) {
981 /* Print the chain for debugging purposes */ 929 /* Print the chain for debugging purposes */
982 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0); 930 uhci_show_qh(urbp->qh, errbuf, ERRBUF_LEN, 0);
983
984 lprintk(errbuf); 931 lprintk(errbuf);
985 } 932 }
986 } 933 }
987#endif 934err:
935
936 /* Note that the queue has stopped and save the next toggle value */
937 urbp->qh->element = UHCI_PTR_TERM;
938 urbp->qh->is_stopped = 1;
939 urbp->qh->needs_fixup = 1;
940 urbp->qh->initial_toggle = uhci_toggle(td_token(td)) ^
941 (ret == -EREMOTEIO);
988 return ret; 942 return ret;
989} 943}
990 944
991static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 945static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb,
946 struct uhci_qh *qh)
992{ 947{
993 int ret; 948 int ret;
994 949
@@ -996,95 +951,60 @@ static inline int uhci_submit_bulk(struct uhci_hcd *uhci, struct urb *urb, struc
996 if (urb->dev->speed == USB_SPEED_LOW) 951 if (urb->dev->speed == USB_SPEED_LOW)
997 return -EINVAL; 952 return -EINVAL;
998 953
999 ret = uhci_submit_common(uhci, urb, eurb, uhci->skel_bulk_qh); 954 qh->skel = uhci->skel_bulk_qh;
1000 if (ret == -EINPROGRESS) 955 ret = uhci_submit_common(uhci, urb, qh);
956 if (ret == 0)
1001 uhci_inc_fsbr(uhci, urb); 957 uhci_inc_fsbr(uhci, urb);
1002
1003 return ret; 958 return ret;
1004} 959}
1005 960
1006static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb, struct urb *eurb) 961static inline int uhci_submit_interrupt(struct uhci_hcd *uhci, struct urb *urb,
962 struct uhci_qh *qh)
1007{ 963{
1008 /* USB 1.1 interrupt transfers only involve one packet per interval; 964 /* USB 1.1 interrupt transfers only involve one packet per interval.
1009 * that's the uhci_submit_common() "breadth first" policy. Drivers 965 * Drivers can submit URBs of any length, but longer ones will need
1010 * can submit urbs of any length, but longer ones might need many 966 * multiple intervals to complete.
1011 * intervals to complete.
1012 */ 967 */
1013 return uhci_submit_common(uhci, urb, eurb, uhci->skelqh[__interval_to_skel(urb->interval)]); 968 qh->skel = uhci->skelqh[__interval_to_skel(urb->interval)];
969 return uhci_submit_common(uhci, urb, qh);
1014} 970}
1015 971
1016/* 972/*
1017 * Isochronous transfers 973 * Isochronous transfers
1018 */ 974 */
1019static int isochronous_find_limits(struct uhci_hcd *uhci, struct urb *urb, unsigned int *start, unsigned int *end) 975static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb,
1020{ 976 struct uhci_qh *qh)
1021 struct urb *last_urb = NULL;
1022 struct urb_priv *up;
1023 int ret = 0;
1024
1025 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1026 struct urb *u = up->urb;
1027
1028 /* look for pending URBs with identical pipe handle */
1029 if ((urb->pipe == u->pipe) && (urb->dev == u->dev) &&
1030 (u->status == -EINPROGRESS) && (u != urb)) {
1031 if (!last_urb)
1032 *start = u->start_frame;
1033 last_urb = u;
1034 }
1035 }
1036
1037 if (last_urb) {
1038 *end = (last_urb->start_frame + last_urb->number_of_packets *
1039 last_urb->interval) & (UHCI_NUMFRAMES-1);
1040 ret = 0;
1041 } else
1042 ret = -1; /* no previous urb found */
1043
1044 return ret;
1045}
1046
1047static int isochronous_find_start(struct uhci_hcd *uhci, struct urb *urb)
1048{ 977{
1049 int limits; 978 struct uhci_td *td = NULL; /* Since urb->number_of_packets > 0 */
1050 unsigned int start = 0, end = 0; 979 int i, frame;
980 unsigned long destination, status;
981 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1051 982
1052 if (urb->number_of_packets > 900) /* 900? Why? */ 983 if (urb->number_of_packets > 900) /* 900? Why? */
1053 return -EFBIG; 984 return -EFBIG;
1054 985
1055 limits = isochronous_find_limits(uhci, urb, &start, &end); 986 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
987 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1056 988
989 /* Figure out the starting frame number */
1057 if (urb->transfer_flags & URB_ISO_ASAP) { 990 if (urb->transfer_flags & URB_ISO_ASAP) {
1058 if (limits) { 991 if (list_empty(&qh->queue)) {
1059 uhci_get_current_frame_number(uhci); 992 uhci_get_current_frame_number(uhci);
1060 urb->start_frame = (uhci->frame_number + 10) 993 urb->start_frame = (uhci->frame_number + 10);
1061 & (UHCI_NUMFRAMES - 1); 994
1062 } else 995 } else { /* Go right after the last one */
1063 urb->start_frame = end; 996 struct urb *last_urb;
997
998 last_urb = list_entry(qh->queue.prev,
999 struct urb_priv, node)->urb;
1000 urb->start_frame = (last_urb->start_frame +
1001 last_urb->number_of_packets *
1002 last_urb->interval);
1003 }
1064 } else { 1004 } else {
1065 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1066 /* FIXME: Sanity check */ 1005 /* FIXME: Sanity check */
1067 } 1006 }
1068 1007 urb->start_frame &= (UHCI_NUMFRAMES - 1);
1069 return 0;
1070}
1071
1072/*
1073 * Isochronous transfers
1074 */
1075static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1076{
1077 struct uhci_td *td;
1078 int i, ret, frame;
1079 int status, destination;
1080 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1081
1082 status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
1083 destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid(urb->pipe);
1084
1085 ret = isochronous_find_start(uhci, urb);
1086 if (ret)
1087 return ret;
1088 1008
1089 for (i = 0; i < urb->number_of_packets; i++) { 1009 for (i = 0; i < urb->number_of_packets; i++) {
1090 td = uhci_alloc_td(uhci); 1010 td = uhci_alloc_td(uhci);
@@ -1092,20 +1012,25 @@ static int uhci_submit_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1092 return -ENOMEM; 1012 return -ENOMEM;
1093 1013
1094 uhci_add_td_to_urb(urb, td); 1014 uhci_add_td_to_urb(urb, td);
1095 uhci_fill_td(td, status, destination | uhci_explen(urb->iso_frame_desc[i].length), 1015 uhci_fill_td(td, status, destination |
1096 urb->transfer_dma + urb->iso_frame_desc[i].offset); 1016 uhci_explen(urb->iso_frame_desc[i].length),
1097 1017 urb->transfer_dma +
1098 if (i + 1 >= urb->number_of_packets) 1018 urb->iso_frame_desc[i].offset);
1099 td->status |= cpu_to_le32(TD_CTRL_IOC);
1100 } 1019 }
1101 1020
1021 /* Set the interrupt-on-completion flag on the last packet. */
1022 td->status |= __constant_cpu_to_le32(TD_CTRL_IOC);
1023
1024 qh->skel = uhci->skel_iso_qh;
1025
1026 /* Add the TDs to the frame list */
1102 frame = urb->start_frame; 1027 frame = urb->start_frame;
1103 list_for_each_entry(td, &urbp->td_list, list) { 1028 list_for_each_entry(td, &urbp->td_list, list) {
1104 uhci_insert_td_frame_list(uhci, td, frame); 1029 uhci_insert_td_in_frame_list(uhci, td, frame);
1105 frame += urb->interval; 1030 frame += urb->interval;
1106 } 1031 }
1107 1032
1108 return -EINPROGRESS; 1033 return 0;
1109} 1034}
1110 1035
1111static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb) 1036static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
@@ -1139,80 +1064,67 @@ static int uhci_result_isochronous(struct uhci_hcd *uhci, struct urb *urb)
1139 1064
1140 i++; 1065 i++;
1141 } 1066 }
1142 unlink_isochronous_tds(uhci, urb);
1143 1067
1144 return ret; 1068 return ret;
1145} 1069}
1146 1070
1147static struct urb *uhci_find_urb_ep(struct uhci_hcd *uhci, struct urb *urb)
1148{
1149 struct urb_priv *up;
1150
1151 /* We don't match Isoc transfers since they are special */
1152 if (usb_pipeisoc(urb->pipe))
1153 return NULL;
1154
1155 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1156 struct urb *u = up->urb;
1157
1158 if (u->dev == urb->dev && u->status == -EINPROGRESS) {
1159 /* For control, ignore the direction */
1160 if (usb_pipecontrol(urb->pipe) &&
1161 (u->pipe & ~USB_DIR_IN) == (urb->pipe & ~USB_DIR_IN))
1162 return u;
1163 else if (u->pipe == urb->pipe)
1164 return u;
1165 }
1166 }
1167
1168 return NULL;
1169}
1170
1171static int uhci_urb_enqueue(struct usb_hcd *hcd, 1071static int uhci_urb_enqueue(struct usb_hcd *hcd,
1172 struct usb_host_endpoint *ep, 1072 struct usb_host_endpoint *hep,
1173 struct urb *urb, gfp_t mem_flags) 1073 struct urb *urb, gfp_t mem_flags)
1174{ 1074{
1175 int ret; 1075 int ret;
1176 struct uhci_hcd *uhci = hcd_to_uhci(hcd); 1076 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1177 unsigned long flags; 1077 unsigned long flags;
1178 struct urb *eurb; 1078 struct urb_priv *urbp;
1079 struct uhci_qh *qh;
1179 int bustime; 1080 int bustime;
1180 1081
1181 spin_lock_irqsave(&uhci->lock, flags); 1082 spin_lock_irqsave(&uhci->lock, flags);
1182 1083
1183 ret = urb->status; 1084 ret = urb->status;
1184 if (ret != -EINPROGRESS) /* URB already unlinked! */ 1085 if (ret != -EINPROGRESS) /* URB already unlinked! */
1185 goto out; 1086 goto done;
1186 1087
1187 eurb = uhci_find_urb_ep(uhci, urb); 1088 ret = -ENOMEM;
1089 urbp = uhci_alloc_urb_priv(uhci, urb);
1090 if (!urbp)
1091 goto done;
1188 1092
1189 if (!uhci_alloc_urb_priv(uhci, urb)) { 1093 if (hep->hcpriv)
1190 ret = -ENOMEM; 1094 qh = (struct uhci_qh *) hep->hcpriv;
1191 goto out; 1095 else {
1096 qh = uhci_alloc_qh(uhci, urb->dev, hep);
1097 if (!qh)
1098 goto err_no_qh;
1192 } 1099 }
1100 urbp->qh = qh;
1193 1101
1194 switch (usb_pipetype(urb->pipe)) { 1102 switch (usb_pipetype(urb->pipe)) {
1195 case PIPE_CONTROL: 1103 case PIPE_CONTROL:
1196 ret = uhci_submit_control(uhci, urb, eurb); 1104 ret = uhci_submit_control(uhci, urb, qh);
1105 break;
1106 case PIPE_BULK:
1107 ret = uhci_submit_bulk(uhci, urb, qh);
1197 break; 1108 break;
1198 case PIPE_INTERRUPT: 1109 case PIPE_INTERRUPT:
1199 if (!eurb) { 1110 if (list_empty(&qh->queue)) {
1200 bustime = usb_check_bandwidth(urb->dev, urb); 1111 bustime = usb_check_bandwidth(urb->dev, urb);
1201 if (bustime < 0) 1112 if (bustime < 0)
1202 ret = bustime; 1113 ret = bustime;
1203 else { 1114 else {
1204 ret = uhci_submit_interrupt(uhci, urb, eurb); 1115 ret = uhci_submit_interrupt(uhci, urb, qh);
1205 if (ret == -EINPROGRESS) 1116 if (ret == 0)
1206 usb_claim_bandwidth(urb->dev, urb, bustime, 0); 1117 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1207 } 1118 }
1208 } else { /* inherit from parent */ 1119 } else { /* inherit from parent */
1209 urb->bandwidth = eurb->bandwidth; 1120 struct urb_priv *eurbp;
1210 ret = uhci_submit_interrupt(uhci, urb, eurb); 1121
1122 eurbp = list_entry(qh->queue.prev, struct urb_priv,
1123 node);
1124 urb->bandwidth = eurbp->urb->bandwidth;
1125 ret = uhci_submit_interrupt(uhci, urb, qh);
1211 } 1126 }
1212 break; 1127 break;
1213 case PIPE_BULK:
1214 ret = uhci_submit_bulk(uhci, urb, eurb);
1215 break;
1216 case PIPE_ISOCHRONOUS: 1128 case PIPE_ISOCHRONOUS:
1217 bustime = usb_check_bandwidth(urb->dev, urb); 1129 bustime = usb_check_bandwidth(urb->dev, urb);
1218 if (bustime < 0) { 1130 if (bustime < 0) {
@@ -1220,221 +1132,208 @@ static int uhci_urb_enqueue(struct usb_hcd *hcd,
1220 break; 1132 break;
1221 } 1133 }
1222 1134
1223 ret = uhci_submit_isochronous(uhci, urb); 1135 ret = uhci_submit_isochronous(uhci, urb, qh);
1224 if (ret == -EINPROGRESS) 1136 if (ret == 0)
1225 usb_claim_bandwidth(urb->dev, urb, bustime, 1); 1137 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
1226 break; 1138 break;
1227 } 1139 }
1140 if (ret != 0)
1141 goto err_submit_failed;
1142
1143 /* Add this URB to the QH */
1144 urbp->qh = qh;
1145 list_add_tail(&urbp->node, &qh->queue);
1228 1146
1229 if (ret != -EINPROGRESS) { 1147 /* If the new URB is the first and only one on this QH then either
1230 /* Submit failed, so delete it from the urb_list */ 1148 * the QH is new and idle or else it's unlinked and waiting to
1231 struct urb_priv *urbp = urb->hcpriv; 1149 * become idle, so we can activate it right away. */
1150 if (qh->queue.next == &urbp->node)
1151 uhci_activate_qh(uhci, qh);
1152 goto done;
1232 1153
1233 list_del_init(&urbp->urb_list); 1154err_submit_failed:
1234 uhci_destroy_urb_priv(uhci, urb); 1155 if (qh->state == QH_STATE_IDLE)
1235 } else 1156 uhci_make_qh_idle(uhci, qh); /* Reclaim unused QH */
1236 ret = 0;
1237 1157
1238out: 1158err_no_qh:
1159 uhci_free_urb_priv(uhci, urbp);
1160
1161done:
1239 spin_unlock_irqrestore(&uhci->lock, flags); 1162 spin_unlock_irqrestore(&uhci->lock, flags);
1240 return ret; 1163 return ret;
1241} 1164}
1242 1165
1243/* 1166static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1244 * Return the result of a transfer
1245 */
1246static void uhci_transfer_result(struct uhci_hcd *uhci, struct urb *urb)
1247{ 1167{
1248 int ret = -EINPROGRESS; 1168 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1169 unsigned long flags;
1249 struct urb_priv *urbp; 1170 struct urb_priv *urbp;
1250 1171
1251 spin_lock(&urb->lock); 1172 spin_lock_irqsave(&uhci->lock, flags);
1173 urbp = urb->hcpriv;
1174 if (!urbp) /* URB was never linked! */
1175 goto done;
1176
1177 /* Remove Isochronous TDs from the frame list ASAP */
1178 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1179 uhci_unlink_isochronous_tds(uhci, urb);
1180 uhci_unlink_qh(uhci, urbp->qh);
1181
1182done:
1183 spin_unlock_irqrestore(&uhci->lock, flags);
1184 return 0;
1185}
1252 1186
1253 urbp = (struct urb_priv *)urb->hcpriv; 1187/*
1188 * Finish unlinking an URB and give it back
1189 */
1190static void uhci_giveback_urb(struct uhci_hcd *uhci, struct uhci_qh *qh,
1191 struct urb *urb, struct pt_regs *regs)
1192__releases(uhci->lock)
1193__acquires(uhci->lock)
1194{
1195 struct urb_priv *urbp = (struct urb_priv *) urb->hcpriv;
1254 1196
1255 if (urb->status != -EINPROGRESS) /* URB already dequeued */ 1197 /* Isochronous TDs get unlinked directly from the frame list */
1256 goto out; 1198 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1199 uhci_unlink_isochronous_tds(uhci, urb);
1200
1201 /* If the URB isn't first on its queue, adjust the link pointer
1202 * of the last TD in the previous URB. */
1203 else if (qh->queue.next != &urbp->node) {
1204 struct urb_priv *purbp;
1205 struct uhci_td *ptd, *ltd;
1206
1207 purbp = list_entry(urbp->node.prev, struct urb_priv, node);
1208 ptd = list_entry(purbp->td_list.prev, struct uhci_td,
1209 list);
1210 ltd = list_entry(urbp->td_list.prev, struct uhci_td,
1211 list);
1212 ptd->link = ltd->link;
1213 }
1257 1214
1258 switch (usb_pipetype(urb->pipe)) { 1215 /* Take the URB off the QH's queue. If the queue is now empty,
1259 case PIPE_CONTROL: 1216 * this is a perfect time for a toggle fixup. */
1260 ret = uhci_result_control(uhci, urb); 1217 list_del_init(&urbp->node);
1261 break; 1218 if (list_empty(&qh->queue) && qh->needs_fixup) {
1262 case PIPE_BULK: 1219 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1263 case PIPE_INTERRUPT: 1220 usb_pipeout(urb->pipe), qh->initial_toggle);
1264 ret = uhci_result_common(uhci, urb); 1221 qh->needs_fixup = 0;
1265 break;
1266 case PIPE_ISOCHRONOUS:
1267 ret = uhci_result_isochronous(uhci, urb);
1268 break;
1269 } 1222 }
1270 1223
1271 if (ret == -EINPROGRESS) 1224 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1272 goto out; 1225 uhci_free_urb_priv(uhci, urbp);
1273 urb->status = ret;
1274 1226
1275 switch (usb_pipetype(urb->pipe)) { 1227 switch (usb_pipetype(urb->pipe)) {
1276 case PIPE_CONTROL:
1277 case PIPE_BULK:
1278 case PIPE_ISOCHRONOUS: 1228 case PIPE_ISOCHRONOUS:
1279 /* Release bandwidth for Interrupt or Isoc. transfers */ 1229 /* Release bandwidth for Interrupt or Isoc. transfers */
1280 if (urb->bandwidth) 1230 if (urb->bandwidth)
1281 usb_release_bandwidth(urb->dev, urb, 1); 1231 usb_release_bandwidth(urb->dev, urb, 1);
1282 uhci_unlink_generic(uhci, urb);
1283 break; 1232 break;
1284 case PIPE_INTERRUPT: 1233 case PIPE_INTERRUPT:
1285 /* Release bandwidth for Interrupt or Isoc. transfers */ 1234 /* Release bandwidth for Interrupt or Isoc. transfers */
1286 /* Make sure we don't release if we have a queued URB */ 1235 /* Make sure we don't release if we have a queued URB */
1287 if (list_empty(&urbp->queue_list) && urb->bandwidth) 1236 if (list_empty(&qh->queue) && urb->bandwidth)
1288 usb_release_bandwidth(urb->dev, urb, 0); 1237 usb_release_bandwidth(urb->dev, urb, 0);
1289 else 1238 else
1290 /* bandwidth was passed on to queued URB, */ 1239 /* bandwidth was passed on to queued URB, */
1291 /* so don't let usb_unlink_urb() release it */ 1240 /* so don't let usb_unlink_urb() release it */
1292 urb->bandwidth = 0; 1241 urb->bandwidth = 0;
1293 uhci_unlink_generic(uhci, urb);
1294 break; 1242 break;
1295 default:
1296 dev_info(uhci_dev(uhci), "%s: unknown pipe type %d "
1297 "for urb %p\n",
1298 __FUNCTION__, usb_pipetype(urb->pipe), urb);
1299 } 1243 }
1300 1244
1301 /* Move it from uhci->urb_list to uhci->complete_list */ 1245 spin_unlock(&uhci->lock);
1302 uhci_moveto_complete(uhci, urbp); 1246 usb_hcd_giveback_urb(uhci_to_hcd(uhci), urb, regs);
1303 1247 spin_lock(&uhci->lock);
1304out:
1305 spin_unlock(&urb->lock);
1306}
1307
1308static void uhci_unlink_generic(struct uhci_hcd *uhci, struct urb *urb)
1309{
1310 struct list_head *head;
1311 struct uhci_td *td;
1312 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1313 int prevactive = 0;
1314
1315 uhci_dec_fsbr(uhci, urb); /* Safe since it checks */
1316 1248
1317 /* 1249 /* If the queue is now empty, we can unlink the QH and give up its
1318 * Now we need to find out what the last successful toggle was 1250 * reserved bandwidth. */
1319 * so we can update the local data toggle for the next transfer 1251 if (list_empty(&qh->queue)) {
1320 * 1252 uhci_unlink_qh(uhci, qh);
1321 * There are 2 ways the last successful completed TD is found:
1322 *
1323 * 1) The TD is NOT active and the actual length < expected length
1324 * 2) The TD is NOT active and it's the last TD in the chain
1325 *
1326 * and a third way the first uncompleted TD is found:
1327 *
1328 * 3) The TD is active and the previous TD is NOT active
1329 *
1330 * Control and Isochronous ignore the toggle, so this is safe
1331 * for all types
1332 *
1333 * FIXME: The toggle fixups won't be 100% reliable until we
1334 * change over to using a single queue for each endpoint and
1335 * stop the queue before unlinking.
1336 */
1337 head = &urbp->td_list;
1338 list_for_each_entry(td, head, list) {
1339 unsigned int ctrlstat = td_status(td);
1340 1253
1341 if (!(ctrlstat & TD_CTRL_ACTIVE) && 1254 /* Bandwidth stuff not yet implemented */
1342 (uhci_actual_length(ctrlstat) <
1343 uhci_expected_length(td_token(td)) ||
1344 td->list.next == head))
1345 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1346 uhci_packetout(td_token(td)),
1347 uhci_toggle(td_token(td)) ^ 1);
1348 else if ((ctrlstat & TD_CTRL_ACTIVE) && !prevactive)
1349 usb_settoggle(urb->dev, uhci_endpoint(td_token(td)),
1350 uhci_packetout(td_token(td)),
1351 uhci_toggle(td_token(td)));
1352
1353 prevactive = ctrlstat & TD_CTRL_ACTIVE;
1354 } 1255 }
1355
1356 uhci_delete_queued_urb(uhci, urb);
1357
1358 /* The interrupt loop will reclaim the QHs */
1359 uhci_remove_qh(uhci, urbp->qh);
1360 urbp->qh = NULL;
1361} 1256}
1362 1257
1363static int uhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb) 1258/*
1259 * Scan the URBs in a QH's queue
1260 */
1261#define QH_FINISHED_UNLINKING(qh) \
1262 (qh->state == QH_STATE_UNLINKING && \
1263 uhci->frame_number + uhci->is_stopped != qh->unlink_frame)
1264
1265static void uhci_scan_qh(struct uhci_hcd *uhci, struct uhci_qh *qh,
1266 struct pt_regs *regs)
1364{ 1267{
1365 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1366 unsigned long flags;
1367 struct urb_priv *urbp; 1268 struct urb_priv *urbp;
1269 struct urb *urb;
1270 int status;
1368 1271
1369 spin_lock_irqsave(&uhci->lock, flags); 1272 while (!list_empty(&qh->queue)) {
1370 urbp = urb->hcpriv; 1273 urbp = list_entry(qh->queue.next, struct urb_priv, node);
1371 if (!urbp) /* URB was never linked! */ 1274 urb = urbp->urb;
1372 goto done;
1373 list_del_init(&urbp->urb_list);
1374
1375 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
1376 unlink_isochronous_tds(uhci, urb);
1377 uhci_unlink_generic(uhci, urb);
1378
1379 uhci_get_current_frame_number(uhci);
1380 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age) {
1381 uhci_remove_pending_urbps(uhci);
1382 uhci->urb_remove_age = uhci->frame_number;
1383 }
1384
1385 /* If we're the first, set the next interrupt bit */
1386 if (list_empty(&uhci->urb_remove_list))
1387 uhci_set_next_interrupt(uhci);
1388 list_add_tail(&urbp->urb_list, &uhci->urb_remove_list);
1389
1390done:
1391 spin_unlock_irqrestore(&uhci->lock, flags);
1392 return 0;
1393}
1394 1275
1395static int uhci_fsbr_timeout(struct uhci_hcd *uhci, struct urb *urb) 1276 switch (usb_pipetype(urb->pipe)) {
1396{ 1277 case PIPE_CONTROL:
1397 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 1278 status = uhci_result_control(uhci, urb);
1398 struct list_head *head; 1279 break;
1399 struct uhci_td *td; 1280 case PIPE_ISOCHRONOUS:
1400 int count = 0; 1281 status = uhci_result_isochronous(uhci, urb);
1401 1282 break;
1402 uhci_dec_fsbr(uhci, urb); 1283 default: /* PIPE_BULK or PIPE_INTERRUPT */
1284 status = uhci_result_common(uhci, urb);
1285 break;
1286 }
1287 if (status == -EINPROGRESS)
1288 break;
1403 1289
1404 urbp->fsbr_timeout = 1; 1290 spin_lock(&urb->lock);
1291 if (urb->status == -EINPROGRESS) /* Not dequeued */
1292 urb->status = status;
1293 else
1294 status = -ECONNRESET;
1295 spin_unlock(&urb->lock);
1405 1296
1406 /* 1297 /* Dequeued but completed URBs can't be given back unless
1407 * Ideally we would want to fix qh->element as well, but it's 1298 * the QH is stopped or has finished unlinking. */
1408 * read/write by the HC, so that can introduce a race. It's not 1299 if (status == -ECONNRESET &&
1409 * really worth the hassle 1300 !(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1410 */ 1301 return;
1411 1302
1412 head = &urbp->td_list; 1303 uhci_giveback_urb(uhci, qh, urb, regs);
1413 list_for_each_entry(td, head, list) { 1304 if (qh->is_stopped)
1414 /* 1305 break;
1415 * Make sure we don't do the last one (since it'll have the
1416 * TERM bit set) as well as we skip every so many TDs to
1417 * make sure it doesn't hog the bandwidth
1418 */
1419 if (td->list.next != head && (count % DEPTH_INTERVAL) ==
1420 (DEPTH_INTERVAL - 1))
1421 td->link |= UHCI_PTR_DEPTH;
1422
1423 count++;
1424 } 1306 }
1425 1307
1426 return 0; 1308 /* If the QH is neither stopped nor finished unlinking (normal case),
1427} 1309 * our work here is done. */
1428 1310 restart:
1429static void uhci_free_pending_qhs(struct uhci_hcd *uhci) 1311 if (!(qh->is_stopped || QH_FINISHED_UNLINKING(qh)))
1430{ 1312 return;
1431 struct uhci_qh *qh, *tmp;
1432
1433 list_for_each_entry_safe(qh, tmp, &uhci->qh_remove_list, remove_list) {
1434 list_del_init(&qh->remove_list);
1435 1313
1436 uhci_free_qh(uhci, qh); 1314 /* Otherwise give back each of the dequeued URBs */
1315 list_for_each_entry(urbp, &qh->queue, node) {
1316 urb = urbp->urb;
1317 if (urb->status != -EINPROGRESS) {
1318 uhci_save_toggle(qh, urb);
1319 uhci_giveback_urb(uhci, qh, urb, regs);
1320 goto restart;
1321 }
1322 }
1323 qh->is_stopped = 0;
1324
1325 /* There are no more dequeued URBs. If there are still URBs on the
1326 * queue, the QH can now be re-activated. */
1327 if (!list_empty(&qh->queue)) {
1328 if (qh->needs_fixup)
1329 uhci_fixup_toggles(qh, 0);
1330 uhci_activate_qh(uhci, qh);
1437 } 1331 }
1332
1333 /* The queue is empty. The QH can become idle if it is fully
1334 * unlinked. */
1335 else if (QH_FINISHED_UNLINKING(qh))
1336 uhci_make_qh_idle(uhci, qh);
1438} 1337}
1439 1338
1440static void uhci_free_pending_tds(struct uhci_hcd *uhci) 1339static void uhci_free_pending_tds(struct uhci_hcd *uhci)
@@ -1448,43 +1347,13 @@ static void uhci_free_pending_tds(struct uhci_hcd *uhci)
1448 } 1347 }
1449} 1348}
1450 1349
1451static void 1350/*
1452uhci_finish_urb(struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs) 1351 * Process events in the schedule, but only in one thread at a time
1453__releases(uhci->lock) 1352 */
1454__acquires(uhci->lock)
1455{
1456 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
1457
1458 uhci_destroy_urb_priv(uhci, urb);
1459
1460 spin_unlock(&uhci->lock);
1461 usb_hcd_giveback_urb(hcd, urb, regs);
1462 spin_lock(&uhci->lock);
1463}
1464
1465static void uhci_finish_completion(struct uhci_hcd *uhci, struct pt_regs *regs)
1466{
1467 struct urb_priv *urbp, *tmp;
1468
1469 list_for_each_entry_safe(urbp, tmp, &uhci->complete_list, urb_list) {
1470 struct urb *urb = urbp->urb;
1471
1472 list_del_init(&urbp->urb_list);
1473 uhci_finish_urb(uhci_to_hcd(uhci), urb, regs);
1474 }
1475}
1476
1477static void uhci_remove_pending_urbps(struct uhci_hcd *uhci)
1478{
1479
1480 /* Splice the urb_remove_list onto the end of the complete_list */
1481 list_splice_init(&uhci->urb_remove_list, uhci->complete_list.prev);
1482}
1483
1484/* Process events in the schedule, but only in one thread at a time */
1485static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs) 1353static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1486{ 1354{
1487 struct urb_priv *urbp, *tmp; 1355 int i;
1356 struct uhci_qh *qh;
1488 1357
1489 /* Don't allow re-entrant calls */ 1358 /* Don't allow re-entrant calls */
1490 if (uhci->scan_in_progress) { 1359 if (uhci->scan_in_progress) {
@@ -1498,60 +1367,39 @@ static void uhci_scan_schedule(struct uhci_hcd *uhci, struct pt_regs *regs)
1498 uhci_clear_next_interrupt(uhci); 1367 uhci_clear_next_interrupt(uhci);
1499 uhci_get_current_frame_number(uhci); 1368 uhci_get_current_frame_number(uhci);
1500 1369
1501 if (uhci->frame_number + uhci->is_stopped != uhci->qh_remove_age)
1502 uhci_free_pending_qhs(uhci);
1503 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age) 1370 if (uhci->frame_number + uhci->is_stopped != uhci->td_remove_age)
1504 uhci_free_pending_tds(uhci); 1371 uhci_free_pending_tds(uhci);
1505 if (uhci->frame_number + uhci->is_stopped != uhci->urb_remove_age)
1506 uhci_remove_pending_urbps(uhci);
1507
1508 /* Walk the list of pending URBs to see which ones completed
1509 * (must be _safe because uhci_transfer_result() dequeues URBs) */
1510 list_for_each_entry_safe(urbp, tmp, &uhci->urb_list, urb_list) {
1511 struct urb *urb = urbp->urb;
1512 1372
1513 /* Checks the status and does all of the magic necessary */ 1373 /* Go through all the QH queues and process the URBs in each one */
1514 uhci_transfer_result(uhci, urb); 1374 for (i = 0; i < UHCI_NUM_SKELQH - 1; ++i) {
1515 } 1375 uhci->next_qh = list_entry(uhci->skelqh[i]->node.next,
1516 uhci_finish_completion(uhci, regs); 1376 struct uhci_qh, node);
1517 1377 while ((qh = uhci->next_qh) != uhci->skelqh[i]) {
1518 /* If the controller is stopped, we can finish these off right now */ 1378 uhci->next_qh = list_entry(qh->node.next,
1519 if (uhci->is_stopped) { 1379 struct uhci_qh, node);
1520 uhci_free_pending_qhs(uhci); 1380 uhci_scan_qh(uhci, qh, regs);
1521 uhci_free_pending_tds(uhci); 1381 }
1522 uhci_remove_pending_urbps(uhci);
1523 } 1382 }
1524 1383
1525 if (uhci->need_rescan) 1384 if (uhci->need_rescan)
1526 goto rescan; 1385 goto rescan;
1527 uhci->scan_in_progress = 0; 1386 uhci->scan_in_progress = 0;
1528 1387
1529 if (list_empty(&uhci->urb_remove_list) && 1388 /* If the controller is stopped, we can finish these off right now */
1530 list_empty(&uhci->td_remove_list) && 1389 if (uhci->is_stopped)
1531 list_empty(&uhci->qh_remove_list)) 1390 uhci_free_pending_tds(uhci);
1391
1392 if (list_empty(&uhci->td_remove_list) &&
1393 list_empty(&uhci->skel_unlink_qh->node))
1532 uhci_clear_next_interrupt(uhci); 1394 uhci_clear_next_interrupt(uhci);
1533 else 1395 else
1534 uhci_set_next_interrupt(uhci); 1396 uhci_set_next_interrupt(uhci);
1535
1536 /* Wake up anyone waiting for an URB to complete */
1537 wake_up_all(&uhci->waitqh);
1538} 1397}
1539 1398
1540static void check_fsbr(struct uhci_hcd *uhci) 1399static void check_fsbr(struct uhci_hcd *uhci)
1541{ 1400{
1542 struct urb_priv *up; 1401 /* For now, don't scan URBs for FSBR timeouts.
1543 1402 * Add it back in later... */
1544 list_for_each_entry(up, &uhci->urb_list, urb_list) {
1545 struct urb *u = up->urb;
1546
1547 spin_lock(&u->lock);
1548
1549 /* Check if the FSBR timed out */
1550 if (up->fsbr && !up->fsbr_timeout && time_after_eq(jiffies, up->fsbrtime + IDLE_TIMEOUT))
1551 uhci_fsbr_timeout(uhci, u);
1552
1553 spin_unlock(&u->lock);
1554 }
1555 1403
1556 /* Really disable FSBR */ 1404 /* Really disable FSBR */
1557 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) { 1405 if (!uhci->fsbr && uhci->fsbrtimeout && time_after_eq(jiffies, uhci->fsbrtimeout)) {
diff --git a/drivers/usb/image/mdc800.c b/drivers/usb/image/mdc800.c
index 049871145d63..08daf400f985 100644
--- a/drivers/usb/image/mdc800.c
+++ b/drivers/usb/image/mdc800.c
@@ -96,6 +96,7 @@
96#include <linux/module.h> 96#include <linux/module.h>
97#include <linux/smp_lock.h> 97#include <linux/smp_lock.h>
98#include <linux/wait.h> 98#include <linux/wait.h>
99#include <linux/mutex.h>
99 100
100#include <linux/usb.h> 101#include <linux/usb.h>
101#include <linux/fs.h> 102#include <linux/fs.h>
@@ -169,7 +170,7 @@ struct mdc800_data
169 int out_count; // Bytes in the buffer 170 int out_count; // Bytes in the buffer
170 171
171 int open; // Camera device open ? 172 int open; // Camera device open ?
172 struct semaphore io_lock; // IO -lock 173 struct mutex io_lock; // IO -lock
173 174
174 char in [8]; // Command Input Buffer 175 char in [8]; // Command Input Buffer
175 int in_count; 176 int in_count;
@@ -497,7 +498,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
497 498
498 info ("Found Mustek MDC800 on USB."); 499 info ("Found Mustek MDC800 on USB.");
499 500
500 down (&mdc800->io_lock); 501 mutex_lock(&mdc800->io_lock);
501 502
502 retval = usb_register_dev(intf, &mdc800_class); 503 retval = usb_register_dev(intf, &mdc800_class);
503 if (retval) { 504 if (retval) {
@@ -542,7 +543,7 @@ static int mdc800_usb_probe (struct usb_interface *intf,
542 543
543 mdc800->state=READY; 544 mdc800->state=READY;
544 545
545 up (&mdc800->io_lock); 546 mutex_unlock(&mdc800->io_lock);
546 547
547 usb_set_intfdata(intf, mdc800); 548 usb_set_intfdata(intf, mdc800);
548 return 0; 549 return 0;
@@ -620,7 +621,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
620 int retval=0; 621 int retval=0;
621 int errn=0; 622 int errn=0;
622 623
623 down (&mdc800->io_lock); 624 mutex_lock(&mdc800->io_lock);
624 625
625 if (mdc800->state == NOT_CONNECTED) 626 if (mdc800->state == NOT_CONNECTED)
626 { 627 {
@@ -656,7 +657,7 @@ static int mdc800_device_open (struct inode* inode, struct file *file)
656 dbg ("Mustek MDC800 device opened."); 657 dbg ("Mustek MDC800 device opened.");
657 658
658error_out: 659error_out:
659 up (&mdc800->io_lock); 660 mutex_unlock(&mdc800->io_lock);
660 return errn; 661 return errn;
661} 662}
662 663
@@ -669,7 +670,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
669 int retval=0; 670 int retval=0;
670 dbg ("Mustek MDC800 device closed."); 671 dbg ("Mustek MDC800 device closed.");
671 672
672 down (&mdc800->io_lock); 673 mutex_lock(&mdc800->io_lock);
673 if (mdc800->open && (mdc800->state != NOT_CONNECTED)) 674 if (mdc800->open && (mdc800->state != NOT_CONNECTED))
674 { 675 {
675 usb_kill_urb(mdc800->irq_urb); 676 usb_kill_urb(mdc800->irq_urb);
@@ -682,7 +683,7 @@ static int mdc800_device_release (struct inode* inode, struct file *file)
682 retval=-EIO; 683 retval=-EIO;
683 } 684 }
684 685
685 up(&mdc800->io_lock); 686 mutex_unlock(&mdc800->io_lock);
686 return retval; 687 return retval;
687} 688}
688 689
@@ -695,21 +696,21 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
695 size_t left=len, sts=len; /* single transfer size */ 696 size_t left=len, sts=len; /* single transfer size */
696 char __user *ptr = buf; 697 char __user *ptr = buf;
697 698
698 down (&mdc800->io_lock); 699 mutex_lock(&mdc800->io_lock);
699 if (mdc800->state == NOT_CONNECTED) 700 if (mdc800->state == NOT_CONNECTED)
700 { 701 {
701 up (&mdc800->io_lock); 702 mutex_unlock(&mdc800->io_lock);
702 return -EBUSY; 703 return -EBUSY;
703 } 704 }
704 if (mdc800->state == WORKING) 705 if (mdc800->state == WORKING)
705 { 706 {
706 warn ("Illegal State \"working\" reached during read ?!"); 707 warn ("Illegal State \"working\" reached during read ?!");
707 up (&mdc800->io_lock); 708 mutex_unlock(&mdc800->io_lock);
708 return -EBUSY; 709 return -EBUSY;
709 } 710 }
710 if (!mdc800->open) 711 if (!mdc800->open)
711 { 712 {
712 up (&mdc800->io_lock); 713 mutex_unlock(&mdc800->io_lock);
713 return -EBUSY; 714 return -EBUSY;
714 } 715 }
715 716
@@ -717,7 +718,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
717 { 718 {
718 if (signal_pending (current)) 719 if (signal_pending (current))
719 { 720 {
720 up (&mdc800->io_lock); 721 mutex_unlock(&mdc800->io_lock);
721 return -EINTR; 722 return -EINTR;
722 } 723 }
723 724
@@ -736,7 +737,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
736 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL)) 737 if (usb_submit_urb (mdc800->download_urb, GFP_KERNEL))
737 { 738 {
738 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status); 739 err ("Can't submit download urb (status=%i)",mdc800->download_urb->status);
739 up (&mdc800->io_lock); 740 mutex_unlock(&mdc800->io_lock);
740 return len-left; 741 return len-left;
741 } 742 }
742 wait_event_timeout(mdc800->download_wait, mdc800->downloaded, 743 wait_event_timeout(mdc800->download_wait, mdc800->downloaded,
@@ -745,14 +746,14 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
745 if (mdc800->download_urb->status != 0) 746 if (mdc800->download_urb->status != 0)
746 { 747 {
747 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status); 748 err ("request download-bytes fails (status=%i)",mdc800->download_urb->status);
748 up (&mdc800->io_lock); 749 mutex_unlock(&mdc800->io_lock);
749 return len-left; 750 return len-left;
750 } 751 }
751 } 752 }
752 else 753 else
753 { 754 {
754 /* No more bytes -> that's an error*/ 755 /* No more bytes -> that's an error*/
755 up (&mdc800->io_lock); 756 mutex_unlock(&mdc800->io_lock);
756 return -EIO; 757 return -EIO;
757 } 758 }
758 } 759 }
@@ -761,7 +762,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
761 /* Copy Bytes */ 762 /* Copy Bytes */
762 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr], 763 if (copy_to_user(ptr, &mdc800->out [mdc800->out_ptr],
763 sts)) { 764 sts)) {
764 up(&mdc800->io_lock); 765 mutex_unlock(&mdc800->io_lock);
765 return -EFAULT; 766 return -EFAULT;
766 } 767 }
767 ptr+=sts; 768 ptr+=sts;
@@ -770,7 +771,7 @@ static ssize_t mdc800_device_read (struct file *file, char __user *buf, size_t l
770 } 771 }
771 } 772 }
772 773
773 up (&mdc800->io_lock); 774 mutex_unlock(&mdc800->io_lock);
774 return len-left; 775 return len-left;
775} 776}
776 777
@@ -785,15 +786,15 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
785{ 786{
786 size_t i=0; 787 size_t i=0;
787 788
788 down (&mdc800->io_lock); 789 mutex_lock(&mdc800->io_lock);
789 if (mdc800->state != READY) 790 if (mdc800->state != READY)
790 { 791 {
791 up (&mdc800->io_lock); 792 mutex_unlock(&mdc800->io_lock);
792 return -EBUSY; 793 return -EBUSY;
793 } 794 }
794 if (!mdc800->open ) 795 if (!mdc800->open )
795 { 796 {
796 up (&mdc800->io_lock); 797 mutex_unlock(&mdc800->io_lock);
797 return -EBUSY; 798 return -EBUSY;
798 } 799 }
799 800
@@ -802,13 +803,13 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
802 unsigned char c; 803 unsigned char c;
803 if (signal_pending (current)) 804 if (signal_pending (current))
804 { 805 {
805 up (&mdc800->io_lock); 806 mutex_unlock(&mdc800->io_lock);
806 return -EINTR; 807 return -EINTR;
807 } 808 }
808 809
809 if(get_user(c, buf+i)) 810 if(get_user(c, buf+i))
810 { 811 {
811 up(&mdc800->io_lock); 812 mutex_unlock(&mdc800->io_lock);
812 return -EFAULT; 813 return -EFAULT;
813 } 814 }
814 815
@@ -829,7 +830,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
829 } 830 }
830 else 831 else
831 { 832 {
832 up (&mdc800->io_lock); 833 mutex_unlock(&mdc800->io_lock);
833 return -EIO; 834 return -EIO;
834 } 835 }
835 836
@@ -841,7 +842,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
841 if (mdc800_usb_waitForIRQ (0,TO_GET_READY)) 842 if (mdc800_usb_waitForIRQ (0,TO_GET_READY))
842 { 843 {
843 err ("Camera didn't get ready.\n"); 844 err ("Camera didn't get ready.\n");
844 up (&mdc800->io_lock); 845 mutex_unlock(&mdc800->io_lock);
845 return -EIO; 846 return -EIO;
846 } 847 }
847 848
@@ -853,7 +854,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
853 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL)) 854 if (usb_submit_urb (mdc800->write_urb, GFP_KERNEL))
854 { 855 {
855 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status); 856 err ("submitting write urb fails (status=%i)", mdc800->write_urb->status);
856 up (&mdc800->io_lock); 857 mutex_unlock(&mdc800->io_lock);
857 return -EIO; 858 return -EIO;
858 } 859 }
859 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000); 860 wait_event_timeout(mdc800->write_wait, mdc800->written, TO_WRITE_GET_READY*HZ/1000);
@@ -861,7 +862,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
861 if (mdc800->state == WORKING) 862 if (mdc800->state == WORKING)
862 { 863 {
863 usb_kill_urb(mdc800->write_urb); 864 usb_kill_urb(mdc800->write_urb);
864 up (&mdc800->io_lock); 865 mutex_unlock(&mdc800->io_lock);
865 return -EIO; 866 return -EIO;
866 } 867 }
867 868
@@ -873,7 +874,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
873 { 874 {
874 err ("call 0x07 before 0x05,0x3e"); 875 err ("call 0x07 before 0x05,0x3e");
875 mdc800->state=READY; 876 mdc800->state=READY;
876 up (&mdc800->io_lock); 877 mutex_unlock(&mdc800->io_lock);
877 return -EIO; 878 return -EIO;
878 } 879 }
879 mdc800->pic_len=-1; 880 mdc800->pic_len=-1;
@@ -892,7 +893,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
892 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ)) 893 if (mdc800_usb_waitForIRQ (1,TO_READ_FROM_IRQ))
893 { 894 {
894 err ("requesting answer from irq fails"); 895 err ("requesting answer from irq fails");
895 up (&mdc800->io_lock); 896 mutex_unlock(&mdc800->io_lock);
896 return -EIO; 897 return -EIO;
897 } 898 }
898 899
@@ -920,7 +921,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
920 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND)) 921 if (mdc800_usb_waitForIRQ (0,TO_DEFAULT_COMMAND))
921 { 922 {
922 err ("Command Timeout."); 923 err ("Command Timeout.");
923 up (&mdc800->io_lock); 924 mutex_unlock(&mdc800->io_lock);
924 return -EIO; 925 return -EIO;
925 } 926 }
926 } 927 }
@@ -930,7 +931,7 @@ static ssize_t mdc800_device_write (struct file *file, const char __user *buf, s
930 } 931 }
931 i++; 932 i++;
932 } 933 }
933 up (&mdc800->io_lock); 934 mutex_unlock(&mdc800->io_lock);
934 return i; 935 return i;
935} 936}
936 937
@@ -978,15 +979,13 @@ static int __init usb_mdc800_init (void)
978{ 979{
979 int retval = -ENODEV; 980 int retval = -ENODEV;
980 /* Allocate Memory */ 981 /* Allocate Memory */
981 mdc800=kmalloc (sizeof (struct mdc800_data), GFP_KERNEL); 982 mdc800=kzalloc (sizeof (struct mdc800_data), GFP_KERNEL);
982 if (!mdc800) 983 if (!mdc800)
983 goto cleanup_on_fail; 984 goto cleanup_on_fail;
984 985
985 memset(mdc800, 0, sizeof(struct mdc800_data));
986 mdc800->dev = NULL; 986 mdc800->dev = NULL;
987 mdc800->open=0;
988 mdc800->state=NOT_CONNECTED; 987 mdc800->state=NOT_CONNECTED;
989 init_MUTEX (&mdc800->io_lock); 988 mutex_init (&mdc800->io_lock);
990 989
991 init_waitqueue_head (&mdc800->irq_wait); 990 init_waitqueue_head (&mdc800->irq_wait);
992 init_waitqueue_head (&mdc800->write_wait); 991 init_waitqueue_head (&mdc800->write_wait);
diff --git a/drivers/usb/input/ati_remote.c b/drivers/usb/input/ati_remote.c
index f7bdc506e613..99f986cb6e95 100644
--- a/drivers/usb/input/ati_remote.c
+++ b/drivers/usb/input/ati_remote.c
@@ -159,8 +159,6 @@ static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
159 */ 159 */
160#define FILTER_TIME (HZ / 20) 160#define FILTER_TIME (HZ / 20)
161 161
162static DECLARE_MUTEX(disconnect_sem);
163
164struct ati_remote { 162struct ati_remote {
165 struct input_dev *idev; 163 struct input_dev *idev;
166 struct usb_device *udev; 164 struct usb_device *udev;
diff --git a/drivers/usb/input/hid-core.c b/drivers/usb/input/hid-core.c
index 07a012f88772..58b59f6e9881 100644
--- a/drivers/usb/input/hid-core.c
+++ b/drivers/usb/input/hid-core.c
@@ -66,9 +66,8 @@ static struct hid_report *hid_register_report(struct hid_device *device, unsigne
66 if (report_enum->report_id_hash[id]) 66 if (report_enum->report_id_hash[id])
67 return report_enum->report_id_hash[id]; 67 return report_enum->report_id_hash[id];
68 68
69 if (!(report = kmalloc(sizeof(struct hid_report), GFP_KERNEL))) 69 if (!(report = kzalloc(sizeof(struct hid_report), GFP_KERNEL)))
70 return NULL; 70 return NULL;
71 memset(report, 0, sizeof(struct hid_report));
72 71
73 if (id != 0) 72 if (id != 0)
74 report_enum->numbered = 1; 73 report_enum->numbered = 1;
@@ -97,12 +96,9 @@ static struct hid_field *hid_register_field(struct hid_report *report, unsigned
97 return NULL; 96 return NULL;
98 } 97 }
99 98
100 if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage) 99 if (!(field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
101 + values * sizeof(unsigned), GFP_KERNEL))) return NULL; 100 + values * sizeof(unsigned), GFP_KERNEL))) return NULL;
102 101
103 memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
104 + values * sizeof(unsigned));
105
106 field->index = report->maxfield++; 102 field->index = report->maxfield++;
107 report->field[field->index] = field; 103 report->field[field->index] = field;
108 field->usage = (struct hid_usage *)(field + 1); 104 field->usage = (struct hid_usage *)(field + 1);
@@ -651,17 +647,14 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
651 hid_parser_reserved 647 hid_parser_reserved
652 }; 648 };
653 649
654 if (!(device = kmalloc(sizeof(struct hid_device), GFP_KERNEL))) 650 if (!(device = kzalloc(sizeof(struct hid_device), GFP_KERNEL)))
655 return NULL; 651 return NULL;
656 memset(device, 0, sizeof(struct hid_device));
657 652
658 if (!(device->collection = kmalloc(sizeof(struct hid_collection) * 653 if (!(device->collection = kzalloc(sizeof(struct hid_collection) *
659 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) { 654 HID_DEFAULT_NUM_COLLECTIONS, GFP_KERNEL))) {
660 kfree(device); 655 kfree(device);
661 return NULL; 656 return NULL;
662 } 657 }
663 memset(device->collection, 0, sizeof(struct hid_collection) *
664 HID_DEFAULT_NUM_COLLECTIONS);
665 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS; 658 device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
666 659
667 for (i = 0; i < HID_REPORT_TYPES; i++) 660 for (i = 0; i < HID_REPORT_TYPES; i++)
@@ -675,13 +668,12 @@ static struct hid_device *hid_parse_report(__u8 *start, unsigned size)
675 memcpy(device->rdesc, start, size); 668 memcpy(device->rdesc, start, size);
676 device->rsize = size; 669 device->rsize = size;
677 670
678 if (!(parser = kmalloc(sizeof(struct hid_parser), GFP_KERNEL))) { 671 if (!(parser = kzalloc(sizeof(struct hid_parser), GFP_KERNEL))) {
679 kfree(device->rdesc); 672 kfree(device->rdesc);
680 kfree(device->collection); 673 kfree(device->collection);
681 kfree(device); 674 kfree(device);
682 return NULL; 675 return NULL;
683 } 676 }
684 memset(parser, 0, sizeof(struct hid_parser));
685 parser->device = device; 677 parser->device = device;
686 678
687 end = start + size; 679 end = start + size;
@@ -911,6 +903,99 @@ static int hid_input_report(int type, struct urb *urb, int interrupt, struct pt_
911} 903}
912 904
913/* 905/*
906 * Input submission and I/O error handler.
907 */
908
909static void hid_io_error(struct hid_device *hid);
910
911/* Start up the input URB */
912static int hid_start_in(struct hid_device *hid)
913{
914 unsigned long flags;
915 int rc = 0;
916
917 spin_lock_irqsave(&hid->inlock, flags);
918 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &hid->iofl) &&
919 !test_and_set_bit(HID_IN_RUNNING, &hid->iofl)) {
920 rc = usb_submit_urb(hid->urbin, GFP_ATOMIC);
921 if (rc != 0)
922 clear_bit(HID_IN_RUNNING, &hid->iofl);
923 }
924 spin_unlock_irqrestore(&hid->inlock, flags);
925 return rc;
926}
927
928/* I/O retry timer routine */
929static void hid_retry_timeout(unsigned long _hid)
930{
931 struct hid_device *hid = (struct hid_device *) _hid;
932
933 dev_dbg(&hid->intf->dev, "retrying intr urb\n");
934 if (hid_start_in(hid))
935 hid_io_error(hid);
936}
937
938/* Workqueue routine to reset the device */
939static void hid_reset(void *_hid)
940{
941 struct hid_device *hid = (struct hid_device *) _hid;
942 int rc_lock, rc;
943
944 dev_dbg(&hid->intf->dev, "resetting device\n");
945 rc = rc_lock = usb_lock_device_for_reset(hid->dev, hid->intf);
946 if (rc_lock >= 0) {
947 rc = usb_reset_device(hid->dev);
948 if (rc_lock)
949 usb_unlock_device(hid->dev);
950 }
951 clear_bit(HID_RESET_PENDING, &hid->iofl);
952
953 if (rc == 0) {
954 hid->retry_delay = 0;
955 if (hid_start_in(hid))
956 hid_io_error(hid);
957 } else if (!(rc == -ENODEV || rc == -EHOSTUNREACH || rc == -EINTR))
958 err("can't reset device, %s-%s/input%d, status %d",
959 hid->dev->bus->bus_name,
960 hid->dev->devpath,
961 hid->ifnum, rc);
962}
963
964/* Main I/O error handler */
965static void hid_io_error(struct hid_device *hid)
966{
967 unsigned long flags;
968
969 spin_lock_irqsave(&hid->inlock, flags);
970
971 /* Stop when disconnected */
972 if (usb_get_intfdata(hid->intf) == NULL)
973 goto done;
974
975 /* When an error occurs, retry at increasing intervals */
976 if (hid->retry_delay == 0) {
977 hid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
978 hid->stop_retry = jiffies + msecs_to_jiffies(1000);
979 } else if (hid->retry_delay < 100)
980 hid->retry_delay *= 2;
981
982 if (time_after(jiffies, hid->stop_retry)) {
983
984 /* Retries failed, so do a port reset */
985 if (!test_and_set_bit(HID_RESET_PENDING, &hid->iofl)) {
986 if (schedule_work(&hid->reset_work))
987 goto done;
988 clear_bit(HID_RESET_PENDING, &hid->iofl);
989 }
990 }
991
992 mod_timer(&hid->io_retry,
993 jiffies + msecs_to_jiffies(hid->retry_delay));
994done:
995 spin_unlock_irqrestore(&hid->inlock, flags);
996}
997
998/*
914 * Input interrupt completion handler. 999 * Input interrupt completion handler.
915 */ 1000 */
916 1001
@@ -921,25 +1006,35 @@ static void hid_irq_in(struct urb *urb, struct pt_regs *regs)
921 1006
922 switch (urb->status) { 1007 switch (urb->status) {
923 case 0: /* success */ 1008 case 0: /* success */
1009 hid->retry_delay = 0;
924 hid_input_report(HID_INPUT_REPORT, urb, 1, regs); 1010 hid_input_report(HID_INPUT_REPORT, urb, 1, regs);
925 break; 1011 break;
926 case -ECONNRESET: /* unlink */ 1012 case -ECONNRESET: /* unlink */
927 case -ENOENT: 1013 case -ENOENT:
928 case -EPERM:
929 case -ESHUTDOWN: /* unplug */ 1014 case -ESHUTDOWN: /* unplug */
930 case -EILSEQ: /* unplug timeout on uhci */ 1015 clear_bit(HID_IN_RUNNING, &hid->iofl);
931 return; 1016 return;
1017 case -EILSEQ: /* protocol error or unplug */
1018 case -EPROTO: /* protocol error or unplug */
932 case -ETIMEDOUT: /* NAK */ 1019 case -ETIMEDOUT: /* NAK */
933 break; 1020 clear_bit(HID_IN_RUNNING, &hid->iofl);
1021 hid_io_error(hid);
1022 return;
934 default: /* error */ 1023 default: /* error */
935 warn("input irq status %d received", urb->status); 1024 warn("input irq status %d received", urb->status);
936 } 1025 }
937 1026
938 status = usb_submit_urb(urb, SLAB_ATOMIC); 1027 status = usb_submit_urb(urb, SLAB_ATOMIC);
939 if (status) 1028 if (status) {
940 err("can't resubmit intr, %s-%s/input%d, status %d", 1029 clear_bit(HID_IN_RUNNING, &hid->iofl);
941 hid->dev->bus->bus_name, hid->dev->devpath, 1030 if (status != -EPERM) {
942 hid->ifnum, status); 1031 err("can't resubmit intr, %s-%s/input%d, status %d",
1032 hid->dev->bus->bus_name,
1033 hid->dev->devpath,
1034 hid->ifnum, status);
1035 hid_io_error(hid);
1036 }
1037 }
943} 1038}
944 1039
945/* 1040/*
@@ -1101,8 +1196,9 @@ static void hid_irq_out(struct urb *urb, struct pt_regs *regs)
1101 case 0: /* success */ 1196 case 0: /* success */
1102 break; 1197 break;
1103 case -ESHUTDOWN: /* unplug */ 1198 case -ESHUTDOWN: /* unplug */
1104 case -EILSEQ: /* unplug timeout on uhci */
1105 unplug = 1; 1199 unplug = 1;
1200 case -EILSEQ: /* protocol error or unplug */
1201 case -EPROTO: /* protocol error or unplug */
1106 case -ECONNRESET: /* unlink */ 1202 case -ECONNRESET: /* unlink */
1107 case -ENOENT: 1203 case -ENOENT:
1108 break; 1204 break;
@@ -1149,8 +1245,9 @@ static void hid_ctrl(struct urb *urb, struct pt_regs *regs)
1149 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs); 1245 hid_input_report(hid->ctrl[hid->ctrltail].report->type, urb, 0, regs);
1150 break; 1246 break;
1151 case -ESHUTDOWN: /* unplug */ 1247 case -ESHUTDOWN: /* unplug */
1152 case -EILSEQ: /* unplug timectrl on uhci */
1153 unplug = 1; 1248 unplug = 1;
1249 case -EILSEQ: /* protocol error or unplug */
1250 case -EPROTO: /* protocol error or unplug */
1154 case -ECONNRESET: /* unlink */ 1251 case -ECONNRESET: /* unlink */
1155 case -ENOENT: 1252 case -ENOENT:
1156 case -EPIPE: /* report not available */ 1253 case -EPIPE: /* report not available */
@@ -1263,14 +1360,9 @@ static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
1263 1360
1264int hid_open(struct hid_device *hid) 1361int hid_open(struct hid_device *hid)
1265{ 1362{
1266 if (hid->open++) 1363 ++hid->open;
1267 return 0; 1364 if (hid_start_in(hid))
1268 1365 hid_io_error(hid);
1269 hid->urbin->dev = hid->dev;
1270
1271 if (usb_submit_urb(hid->urbin, GFP_KERNEL))
1272 return -EIO;
1273
1274 return 0; 1366 return 0;
1275} 1367}
1276 1368
@@ -1460,6 +1552,9 @@ void hid_init_reports(struct hid_device *hid)
1460#define USB_VENDOR_ID_HP 0x03f0 1552#define USB_VENDOR_ID_HP 0x03f0
1461#define USB_DEVICE_ID_HP_USBHUB_KB 0x020c 1553#define USB_DEVICE_ID_HP_USBHUB_KB 0x020c
1462 1554
1555#define USB_VENDOR_ID_CREATIVELABS 0x062a
1556#define USB_DEVICE_ID_CREATIVELABS_SILVERCREST 0x0201
1557
1463/* 1558/*
1464 * Alphabetically sorted blacklist by quirk type. 1559 * Alphabetically sorted blacklist by quirk type.
1465 */ 1560 */
@@ -1576,6 +1671,7 @@ static const struct hid_blacklist {
1576 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET }, 1671 { USB_VENDOR_ID_ATEN, USB_DEVICE_ID_ATEN_4PORTKVMC, HID_QUIRK_NOGET },
1577 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET}, 1672 { USB_VENDOR_ID_BTC, USB_DEVICE_ID_BTC_KEYBOARD, HID_QUIRK_NOGET},
1578 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET}, 1673 { USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_USBHUB_KB, HID_QUIRK_NOGET},
1674 { USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_CREATIVELABS_SILVERCREST, HID_QUIRK_NOGET },
1579 { USB_VENDOR_ID_HP, USB_DEVICE_ID_HP_USBHUB_KB, HID_QUIRK_NOGET }, 1675 { USB_VENDOR_ID_HP, USB_DEVICE_ID_HP_USBHUB_KB, HID_QUIRK_NOGET },
1580 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET }, 1676 { USB_VENDOR_ID_TANGTOP, USB_DEVICE_ID_TANGTOP_USBPS2, HID_QUIRK_NOGET },
1581 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT }, 1677 { USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_DUAL_USB_JOYPAD, HID_QUIRK_NOGET | HID_QUIRK_MULTI_INPUT },
@@ -1795,6 +1891,10 @@ static struct hid_device *usb_hid_configure(struct usb_interface *intf)
1795 1891
1796 init_waitqueue_head(&hid->wait); 1892 init_waitqueue_head(&hid->wait);
1797 1893
1894 INIT_WORK(&hid->reset_work, hid_reset, hid);
1895 setup_timer(&hid->io_retry, hid_retry_timeout, (unsigned long) hid);
1896
1897 spin_lock_init(&hid->inlock);
1798 spin_lock_init(&hid->outlock); 1898 spin_lock_init(&hid->outlock);
1799 spin_lock_init(&hid->ctrllock); 1899 spin_lock_init(&hid->ctrllock);
1800 1900
@@ -1863,11 +1963,16 @@ static void hid_disconnect(struct usb_interface *intf)
1863 if (!hid) 1963 if (!hid)
1864 return; 1964 return;
1865 1965
1966 spin_lock_irq(&hid->inlock); /* Sync with error handler */
1866 usb_set_intfdata(intf, NULL); 1967 usb_set_intfdata(intf, NULL);
1968 spin_unlock_irq(&hid->inlock);
1867 usb_kill_urb(hid->urbin); 1969 usb_kill_urb(hid->urbin);
1868 usb_kill_urb(hid->urbout); 1970 usb_kill_urb(hid->urbout);
1869 usb_kill_urb(hid->urbctrl); 1971 usb_kill_urb(hid->urbctrl);
1870 1972
1973 del_timer_sync(&hid->io_retry);
1974 flush_scheduled_work();
1975
1871 if (hid->claimed & HID_CLAIMED_INPUT) 1976 if (hid->claimed & HID_CLAIMED_INPUT)
1872 hidinput_disconnect(hid); 1977 hidinput_disconnect(hid);
1873 if (hid->claimed & HID_CLAIMED_HIDDEV) 1978 if (hid->claimed & HID_CLAIMED_HIDDEV)
@@ -1942,6 +2047,10 @@ static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1942{ 2047{
1943 struct hid_device *hid = usb_get_intfdata (intf); 2048 struct hid_device *hid = usb_get_intfdata (intf);
1944 2049
2050 spin_lock_irq(&hid->inlock); /* Sync with error handler */
2051 set_bit(HID_SUSPENDED, &hid->iofl);
2052 spin_unlock_irq(&hid->inlock);
2053 del_timer(&hid->io_retry);
1945 usb_kill_urb(hid->urbin); 2054 usb_kill_urb(hid->urbin);
1946 dev_dbg(&intf->dev, "suspend\n"); 2055 dev_dbg(&intf->dev, "suspend\n");
1947 return 0; 2056 return 0;
@@ -1952,10 +2061,8 @@ static int hid_resume(struct usb_interface *intf)
1952 struct hid_device *hid = usb_get_intfdata (intf); 2061 struct hid_device *hid = usb_get_intfdata (intf);
1953 int status; 2062 int status;
1954 2063
1955 if (hid->open) 2064 clear_bit(HID_SUSPENDED, &hid->iofl);
1956 status = usb_submit_urb(hid->urbin, GFP_NOIO); 2065 status = hid_start_in(hid);
1957 else
1958 status = 0;
1959 dev_dbg(&intf->dev, "resume status %d\n", status); 2066 dev_dbg(&intf->dev, "resume status %d\n", status);
1960 return status; 2067 return status;
1961} 2068}
diff --git a/drivers/usb/input/hid-lgff.c b/drivers/usb/input/hid-lgff.c
index f82c9c9e5d51..f07d44357ff1 100644
--- a/drivers/usb/input/hid-lgff.c
+++ b/drivers/usb/input/hid-lgff.c
@@ -154,10 +154,9 @@ int hid_lgff_init(struct hid_device* hid)
154 return -1; 154 return -1;
155 } 155 }
156 156
157 private = kmalloc(sizeof(struct lgff_device), GFP_KERNEL); 157 private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL);
158 if (!private) 158 if (!private)
159 return -1; 159 return -1;
160 memset(private, 0, sizeof(struct lgff_device));
161 hid->ff_private = private; 160 hid->ff_private = private;
162 161
163 /* Input init */ 162 /* Input init */
@@ -228,13 +227,12 @@ static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report)
228 } 227 }
229 *ret->field[0] = *report->field[0]; 228 *ret->field[0] = *report->field[0];
230 229
231 ret->field[0]->value = kmalloc(sizeof(s32[8]), GFP_KERNEL); 230 ret->field[0]->value = kzalloc(sizeof(s32[8]), GFP_KERNEL);
232 if (!ret->field[0]->value) { 231 if (!ret->field[0]->value) {
233 kfree(ret->field[0]); 232 kfree(ret->field[0]);
234 kfree(ret); 233 kfree(ret);
235 return NULL; 234 return NULL;
236 } 235 }
237 memset(ret->field[0]->value, 0, sizeof(s32[8]));
238 236
239 return ret; 237 return ret;
240} 238}
diff --git a/drivers/usb/input/hid-tmff.c b/drivers/usb/input/hid-tmff.c
index 023fd5ac31c8..534425c69c0a 100644
--- a/drivers/usb/input/hid-tmff.c
+++ b/drivers/usb/input/hid-tmff.c
@@ -113,11 +113,10 @@ int hid_tmff_init(struct hid_device *hid)
113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
114 struct input_dev *input_dev = hidinput->input; 114 struct input_dev *input_dev = hidinput->input;
115 115
116 private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL); 116 private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
117 if (!private) 117 if (!private)
118 return -ENOMEM; 118 return -ENOMEM;
119 119
120 memset(private, 0, sizeof(struct tmff_device));
121 hid->ff_private = private; 120 hid->ff_private = private;
122 121
123 /* Find the report to use */ 122 /* Find the report to use */
diff --git a/drivers/usb/input/hid.h b/drivers/usb/input/hid.h
index 8b0d4346ce9c..4e1b784fe527 100644
--- a/drivers/usb/input/hid.h
+++ b/drivers/usb/input/hid.h
@@ -31,6 +31,8 @@
31#include <linux/types.h> 31#include <linux/types.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/list.h> 33#include <linux/list.h>
34#include <linux/timer.h>
35#include <linux/workqueue.h>
34 36
35/* 37/*
36 * USB HID (Human Interface Device) interface class code 38 * USB HID (Human Interface Device) interface class code
@@ -370,6 +372,9 @@ struct hid_control_fifo {
370 372
371#define HID_CTRL_RUNNING 1 373#define HID_CTRL_RUNNING 1
372#define HID_OUT_RUNNING 2 374#define HID_OUT_RUNNING 2
375#define HID_IN_RUNNING 3
376#define HID_RESET_PENDING 4
377#define HID_SUSPENDED 5
373 378
374struct hid_input { 379struct hid_input {
375 struct list_head list; 380 struct list_head list;
@@ -393,12 +398,17 @@ struct hid_device { /* device report descriptor */
393 int ifnum; /* USB interface number */ 398 int ifnum; /* USB interface number */
394 399
395 unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */ 400 unsigned long iofl; /* I/O flags (CTRL_RUNNING, OUT_RUNNING) */
401 struct timer_list io_retry; /* Retry timer */
402 unsigned long stop_retry; /* Time to give up, in jiffies */
403 unsigned int retry_delay; /* Delay length in ms */
404 struct work_struct reset_work; /* Task context for resets */
396 405
397 unsigned int bufsize; /* URB buffer size */ 406 unsigned int bufsize; /* URB buffer size */
398 407
399 struct urb *urbin; /* Input URB */ 408 struct urb *urbin; /* Input URB */
400 char *inbuf; /* Input buffer */ 409 char *inbuf; /* Input buffer */
401 dma_addr_t inbuf_dma; /* Input buffer dma */ 410 dma_addr_t inbuf_dma; /* Input buffer dma */
411 spinlock_t inlock; /* Input fifo spinlock */
402 412
403 struct urb *urbctrl; /* Control URB */ 413 struct urb *urbctrl; /* Control URB */
404 struct usb_ctrlrequest *cr; /* Control request struct */ 414 struct usb_ctrlrequest *cr; /* Control request struct */
diff --git a/drivers/usb/input/hiddev.c b/drivers/usb/input/hiddev.c
index 925f5aba06f5..6dd666696178 100644
--- a/drivers/usb/input/hiddev.c
+++ b/drivers/usb/input/hiddev.c
@@ -257,9 +257,8 @@ static int hiddev_open(struct inode * inode, struct file * file) {
257 if (i >= HIDDEV_MINORS || !hiddev_table[i]) 257 if (i >= HIDDEV_MINORS || !hiddev_table[i])
258 return -ENODEV; 258 return -ENODEV;
259 259
260 if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL))) 260 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
261 return -ENOMEM; 261 return -ENOMEM;
262 memset(list, 0, sizeof(struct hiddev_list));
263 262
264 list->hiddev = hiddev_table[i]; 263 list->hiddev = hiddev_table[i];
265 list->next = hiddev_table[i]->list; 264 list->next = hiddev_table[i]->list;
@@ -754,9 +753,8 @@ int hiddev_connect(struct hid_device *hid)
754 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0) 753 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
755 return -1; 754 return -1;
756 755
757 if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL))) 756 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
758 return -1; 757 return -1;
759 memset(hiddev, 0, sizeof(struct hiddev));
760 758
761 retval = usb_register_dev(hid->intf, &hiddev_class); 759 retval = usb_register_dev(hid->intf, &hiddev_class);
762 if (retval) { 760 if (retval) {
diff --git a/drivers/usb/media/Kconfig b/drivers/usb/media/Kconfig
index 0d3d2cc5d7be..189d40f96be5 100644
--- a/drivers/usb/media/Kconfig
+++ b/drivers/usb/media/Kconfig
@@ -191,6 +191,21 @@ config USB_W9968CF
191 To compile this driver as a module, choose M here: the 191 To compile this driver as a module, choose M here: the
192 module will be called w9968cf. 192 module will be called w9968cf.
193 193
194config USB_ZC0301
195 tristate "USB ZC0301 Image Processor and Control Chip support"
196 depends on USB && VIDEO_DEV
197 ---help---
198 Say Y here if you want support for cameras based on the ZC0301
199 Image Processor and Control Chip.
200
201 See <file:Documentation/usb/zc0301.txt> for more informations.
202
203 This driver uses the Video For Linux API. You must say Y or M to
204 "Video For Linux" to use this driver.
205
206 To compile this driver as a module, choose M here: the
207 module will be called zc0301.
208
194config USB_PWC 209config USB_PWC
195 tristate "USB Philips Cameras" 210 tristate "USB Philips Cameras"
196 depends on USB && VIDEO_DEV 211 depends on USB && VIDEO_DEV
diff --git a/drivers/usb/media/Makefile b/drivers/usb/media/Makefile
index 3957aa1be0f2..50e89a33b85e 100644
--- a/drivers/usb/media/Makefile
+++ b/drivers/usb/media/Makefile
@@ -2,8 +2,12 @@
2# Makefile for USB Media drivers 2# Makefile for USB Media drivers
3# 3#
4 4
5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bcb.o sn9c102_tas5110c1b.o sn9c102_tas5130d1b.o 5sn9c102-objs := sn9c102_core.o sn9c102_hv7131d.o sn9c102_mi0343.o \
6 sn9c102_ov7630.o sn9c102_pas106b.o sn9c102_pas202bca.o \
7 sn9c102_pas202bcb.o sn9c102_tas5110c1b.o \
8 sn9c102_tas5130d1b.o
6et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o 9et61x251-objs := et61x251_core.o et61x251_tas5130d1b.o
10zc0301-objs := zc0301_core.o zc0301_pas202bcb.o
7 11
8obj-$(CONFIG_USB_DABUSB) += dabusb.o 12obj-$(CONFIG_USB_DABUSB) += dabusb.o
9obj-$(CONFIG_USB_DSBR) += dsbr100.o 13obj-$(CONFIG_USB_DSBR) += dsbr100.o
@@ -16,4 +20,5 @@ obj-$(CONFIG_USB_SN9C102) += sn9c102.o
16obj-$(CONFIG_USB_STV680) += stv680.o 20obj-$(CONFIG_USB_STV680) += stv680.o
17obj-$(CONFIG_USB_VICAM) += vicam.o usbvideo.o 21obj-$(CONFIG_USB_VICAM) += vicam.o usbvideo.o
18obj-$(CONFIG_USB_W9968CF) += w9968cf.o 22obj-$(CONFIG_USB_W9968CF) += w9968cf.o
23obj-$(CONFIG_USB_ZC0301) += zc0301.o
19obj-$(CONFIG_USB_PWC) += pwc/ 24obj-$(CONFIG_USB_PWC) += pwc/
diff --git a/drivers/usb/media/dabusb.c b/drivers/usb/media/dabusb.c
index 18d8eaf408d5..1774ab7a40d2 100644
--- a/drivers/usb/media/dabusb.c
+++ b/drivers/usb/media/dabusb.c
@@ -38,6 +38,7 @@
38#include <linux/delay.h> 38#include <linux/delay.h>
39#include <linux/usb.h> 39#include <linux/usb.h>
40#include <linux/smp_lock.h> 40#include <linux/smp_lock.h>
41#include <linux/mutex.h>
41 42
42#include "dabusb.h" 43#include "dabusb.h"
43#include "dabfirmware.h" 44#include "dabfirmware.h"
@@ -217,12 +218,11 @@ static int dabusb_alloc_buffers (pdabusb_t s)
217 pipesize, packets, transfer_buffer_length); 218 pipesize, packets, transfer_buffer_length);
218 219
219 while (buffers < (s->total_buffer_size << 10)) { 220 while (buffers < (s->total_buffer_size << 10)) {
220 b = (pbuff_t) kmalloc (sizeof (buff_t), GFP_KERNEL); 221 b = (pbuff_t) kzalloc (sizeof (buff_t), GFP_KERNEL);
221 if (!b) { 222 if (!b) {
222 err("kmalloc(sizeof(buff_t))==NULL"); 223 err("kzalloc(sizeof(buff_t))==NULL");
223 goto err; 224 goto err;
224 } 225 }
225 memset (b, 0, sizeof (buff_t));
226 b->s = s; 226 b->s = s;
227 b->purb = usb_alloc_urb(packets, GFP_KERNEL); 227 b->purb = usb_alloc_urb(packets, GFP_KERNEL);
228 if (!b->purb) { 228 if (!b->purb) {
@@ -571,7 +571,7 @@ static ssize_t dabusb_read (struct file *file, char __user *buf, size_t count, l
571 s->readptr = 0; 571 s->readptr = 0;
572 } 572 }
573 } 573 }
574 err: //up(&s->mutex); 574 err: //mutex_unlock(&s->mutex);
575 return ret; 575 return ret;
576} 576}
577 577
@@ -586,10 +586,10 @@ static int dabusb_open (struct inode *inode, struct file *file)
586 s = &dabusb[devnum - DABUSB_MINOR]; 586 s = &dabusb[devnum - DABUSB_MINOR];
587 587
588 dbg("dabusb_open"); 588 dbg("dabusb_open");
589 down (&s->mutex); 589 mutex_lock(&s->mutex);
590 590
591 while (!s->usbdev || s->opened) { 591 while (!s->usbdev || s->opened) {
592 up (&s->mutex); 592 mutex_unlock(&s->mutex);
593 593
594 if (file->f_flags & O_NONBLOCK) { 594 if (file->f_flags & O_NONBLOCK) {
595 return -EBUSY; 595 return -EBUSY;
@@ -599,15 +599,15 @@ static int dabusb_open (struct inode *inode, struct file *file)
599 if (signal_pending (current)) { 599 if (signal_pending (current)) {
600 return -EAGAIN; 600 return -EAGAIN;
601 } 601 }
602 down (&s->mutex); 602 mutex_lock(&s->mutex);
603 } 603 }
604 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) { 604 if (usb_set_interface (s->usbdev, _DABUSB_IF, 1) < 0) {
605 up(&s->mutex); 605 mutex_unlock(&s->mutex);
606 err("set_interface failed"); 606 err("set_interface failed");
607 return -EINVAL; 607 return -EINVAL;
608 } 608 }
609 s->opened = 1; 609 s->opened = 1;
610 up (&s->mutex); 610 mutex_unlock(&s->mutex);
611 611
612 file->f_pos = 0; 612 file->f_pos = 0;
613 file->private_data = s; 613 file->private_data = s;
@@ -621,10 +621,10 @@ static int dabusb_release (struct inode *inode, struct file *file)
621 621
622 dbg("dabusb_release"); 622 dbg("dabusb_release");
623 623
624 down (&s->mutex); 624 mutex_lock(&s->mutex);
625 dabusb_stop (s); 625 dabusb_stop (s);
626 dabusb_free_buffers (s); 626 dabusb_free_buffers (s);
627 up (&s->mutex); 627 mutex_unlock(&s->mutex);
628 628
629 if (!s->remove_pending) { 629 if (!s->remove_pending) {
630 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0) 630 if (usb_set_interface (s->usbdev, _DABUSB_IF, 0) < 0)
@@ -649,10 +649,10 @@ static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cm
649 if (s->remove_pending) 649 if (s->remove_pending)
650 return -EIO; 650 return -EIO;
651 651
652 down (&s->mutex); 652 mutex_lock(&s->mutex);
653 653
654 if (!s->usbdev) { 654 if (!s->usbdev) {
655 up (&s->mutex); 655 mutex_unlock(&s->mutex);
656 return -EIO; 656 return -EIO;
657 } 657 }
658 658
@@ -692,7 +692,7 @@ static int dabusb_ioctl (struct inode *inode, struct file *file, unsigned int cm
692 ret = -ENOIOCTLCMD; 692 ret = -ENOIOCTLCMD;
693 break; 693 break;
694 } 694 }
695 up (&s->mutex); 695 mutex_unlock(&s->mutex);
696 return ret; 696 return ret;
697} 697}
698 698
@@ -738,7 +738,7 @@ static int dabusb_probe (struct usb_interface *intf,
738 738
739 s = &dabusb[intf->minor]; 739 s = &dabusb[intf->minor];
740 740
741 down (&s->mutex); 741 mutex_lock(&s->mutex);
742 s->remove_pending = 0; 742 s->remove_pending = 0;
743 s->usbdev = usbdev; 743 s->usbdev = usbdev;
744 s->devnum = intf->minor; 744 s->devnum = intf->minor;
@@ -761,7 +761,7 @@ static int dabusb_probe (struct usb_interface *intf,
761 } 761 }
762 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber); 762 dbg("bound to interface: %d", intf->altsetting->desc.bInterfaceNumber);
763 usb_set_intfdata (intf, s); 763 usb_set_intfdata (intf, s);
764 up (&s->mutex); 764 mutex_unlock(&s->mutex);
765 765
766 retval = usb_register_dev(intf, &dabusb_class); 766 retval = usb_register_dev(intf, &dabusb_class);
767 if (retval) { 767 if (retval) {
@@ -772,7 +772,7 @@ static int dabusb_probe (struct usb_interface *intf,
772 return 0; 772 return 0;
773 773
774 reject: 774 reject:
775 up (&s->mutex); 775 mutex_unlock(&s->mutex);
776 s->usbdev = NULL; 776 s->usbdev = NULL;
777 return -ENODEV; 777 return -ENODEV;
778} 778}
@@ -829,7 +829,7 @@ static int __init dabusb_init (void)
829 for (u = 0; u < NRDABUSB; u++) { 829 for (u = 0; u < NRDABUSB; u++) {
830 pdabusb_t s = &dabusb[u]; 830 pdabusb_t s = &dabusb[u];
831 memset (s, 0, sizeof (dabusb_t)); 831 memset (s, 0, sizeof (dabusb_t));
832 init_MUTEX (&s->mutex); 832 mutex_init (&s->mutex);
833 s->usbdev = NULL; 833 s->usbdev = NULL;
834 s->total_buffer_size = buffers; 834 s->total_buffer_size = buffers;
835 init_waitqueue_head (&s->wait); 835 init_waitqueue_head (&s->wait);
diff --git a/drivers/usb/media/dabusb.h b/drivers/usb/media/dabusb.h
index 10b666e43abc..96b03e4af8b9 100644
--- a/drivers/usb/media/dabusb.h
+++ b/drivers/usb/media/dabusb.h
@@ -18,7 +18,7 @@ typedef enum { _stopped=0, _started } driver_state_t;
18 18
19typedef struct 19typedef struct
20{ 20{
21 struct semaphore mutex; 21 struct mutex mutex;
22 struct usb_device *usbdev; 22 struct usb_device *usbdev;
23 wait_queue_head_t wait; 23 wait_queue_head_t wait;
24 wait_queue_head_t remove_ok; 24 wait_queue_head_t remove_ok;
diff --git a/drivers/usb/media/et61x251.h b/drivers/usb/media/et61x251.h
index 652238f329f3..eee8afc9be72 100644
--- a/drivers/usb/media/et61x251.h
+++ b/drivers/usb/media/et61x251.h
@@ -33,7 +33,9 @@
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/param.h> 34#include <linux/param.h>
35#include <linux/rwsem.h> 35#include <linux/rwsem.h>
36#include <asm/semaphore.h> 36#include <linux/mutex.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
37 39
38#include "et61x251_sensor.h" 40#include "et61x251_sensor.h"
39 41
@@ -51,6 +53,7 @@
51#define ET61X251_ALTERNATE_SETTING 13 53#define ET61X251_ALTERNATE_SETTING 13
52#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS) 54#define ET61X251_URB_TIMEOUT msecs_to_jiffies(2 * ET61X251_ISO_PACKETS)
53#define ET61X251_CTRL_TIMEOUT 100 55#define ET61X251_CTRL_TIMEOUT 100
56#define ET61X251_FRAME_TIMEOUT 2
54 57
55/*****************************************************************************/ 58/*****************************************************************************/
56 59
@@ -127,15 +130,16 @@ struct et61x251_sysfs_attr {
127 130
128struct et61x251_module_param { 131struct et61x251_module_param {
129 u8 force_munmap; 132 u8 force_munmap;
133 u16 frame_timeout;
130}; 134};
131 135
132static DECLARE_MUTEX(et61x251_sysfs_lock); 136static DEFINE_MUTEX(et61x251_sysfs_lock);
133static DECLARE_RWSEM(et61x251_disconnect); 137static DECLARE_RWSEM(et61x251_disconnect);
134 138
135struct et61x251_device { 139struct et61x251_device {
136 struct video_device* v4ldev; 140 struct video_device* v4ldev;
137 141
138 struct et61x251_sensor* sensor; 142 struct et61x251_sensor sensor;
139 143
140 struct usb_device* usbdev; 144 struct usb_device* usbdev;
141 struct urb* urb[ET61X251_URBS]; 145 struct urb* urb[ET61X251_URBS];
@@ -157,19 +161,28 @@ struct et61x251_device {
157 enum et61x251_dev_state state; 161 enum et61x251_dev_state state;
158 u8 users; 162 u8 users;
159 163
160 struct semaphore dev_sem, fileop_sem; 164 struct mutex dev_mutex, fileop_mutex;
161 spinlock_t queue_lock; 165 spinlock_t queue_lock;
162 wait_queue_head_t open, wait_frame, wait_stream; 166 wait_queue_head_t open, wait_frame, wait_stream;
163}; 167};
164 168
165/*****************************************************************************/ 169/*****************************************************************************/
166 170
171struct et61x251_device*
172et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id)
173{
174 if (usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id))
175 return cam;
176
177 return NULL;
178}
179
180
167void 181void
168et61x251_attach_sensor(struct et61x251_device* cam, 182et61x251_attach_sensor(struct et61x251_device* cam,
169 struct et61x251_sensor* sensor) 183 struct et61x251_sensor* sensor)
170{ 184{
171 cam->sensor = sensor; 185 memcpy(&cam->sensor, sensor, sizeof(struct et61x251_sensor));
172 cam->sensor->usbdev = cam->usbdev;
173} 186}
174 187
175/*****************************************************************************/ 188/*****************************************************************************/
@@ -212,7 +225,8 @@ do { \
212 225
213#undef PDBG 226#undef PDBG
214#define PDBG(fmt, args...) \ 227#define PDBG(fmt, args...) \
215dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args) 228dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
229 __FUNCTION__, __LINE__ , ## args)
216 230
217#undef PDBGG 231#undef PDBGG
218#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ 232#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
diff --git a/drivers/usb/media/et61x251_core.c b/drivers/usb/media/et61x251_core.c
index 2c0171a5ad62..7cc01b828b3d 100644
--- a/drivers/usb/media/et61x251_core.c
+++ b/drivers/usb/media/et61x251_core.c
@@ -25,11 +25,9 @@
25#include <linux/moduleparam.h> 25#include <linux/moduleparam.h>
26#include <linux/errno.h> 26#include <linux/errno.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h> 28#include <linux/device.h>
30#include <linux/fs.h> 29#include <linux/fs.h>
31#include <linux/delay.h> 30#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h> 31#include <linux/compiler.h>
34#include <linux/ioctl.h> 32#include <linux/ioctl.h>
35#include <linux/poll.h> 33#include <linux/poll.h>
@@ -50,8 +48,8 @@
50#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia" 48#define ET61X251_MODULE_AUTHOR "(C) 2006 Luca Risolia"
51#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" 49#define ET61X251_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
52#define ET61X251_MODULE_LICENSE "GPL" 50#define ET61X251_MODULE_LICENSE "GPL"
53#define ET61X251_MODULE_VERSION "1:1.01" 51#define ET61X251_MODULE_VERSION "1:1.02"
54#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 1) 52#define ET61X251_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 2)
55 53
56/*****************************************************************************/ 54/*****************************************************************************/
57 55
@@ -90,6 +88,16 @@ MODULE_PARM_DESC(force_munmap,
90 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." 88 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
91 "\n"); 89 "\n");
92 90
91static unsigned int frame_timeout[] = {[0 ... ET61X251_MAX_DEVICES-1] =
92 ET61X251_FRAME_TIMEOUT};
93module_param_array(frame_timeout, uint, NULL, 0644);
94MODULE_PARM_DESC(frame_timeout,
95 "\n<n[,...]> Timeout for a video frame in seconds."
96 "\nThis parameter is specific for each detected camera."
97 "\nDefault value is "
98 __MODULE_STRING(ET61X251_FRAME_TIMEOUT)"."
99 "\n");
100
93#ifdef ET61X251_DEBUG 101#ifdef ET61X251_DEBUG
94static unsigned short debug = ET61X251_DEBUG_LEVEL; 102static unsigned short debug = ET61X251_DEBUG_LEVEL;
95module_param(debug, ushort, 0644); 103module_param(debug, ushort, 0644);
@@ -111,8 +119,8 @@ static u32
111et61x251_request_buffers(struct et61x251_device* cam, u32 count, 119et61x251_request_buffers(struct et61x251_device* cam, u32 count,
112 enum et61x251_io_method io) 120 enum et61x251_io_method io)
113{ 121{
114 struct v4l2_pix_format* p = &(cam->sensor->pix_format); 122 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
115 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds); 123 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
116 const size_t imagesize = cam->module_param.force_munmap || 124 const size_t imagesize = cam->module_param.force_munmap ||
117 io == IO_READ ? 125 io == IO_READ ?
118 (p->width * p->height * p->priv) / 8 : 126 (p->width * p->height * p->priv) / 8 :
@@ -268,8 +276,8 @@ et61x251_i2c_try_read(struct et61x251_device* cam,
268 int err = 0, res; 276 int err = 0, res;
269 277
270 data[0] = address; 278 data[0] = address;
271 data[1] = cam->sensor->i2c_slave_id; 279 data[1] = cam->sensor.i2c_slave_id;
272 data[2] = cam->sensor->rsta | 0x10; 280 data[2] = cam->sensor.rsta | 0x10;
273 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02); 281 data[3] = !(et61x251_read_reg(cam, 0x8b) & 0x02);
274 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 282 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
275 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT); 283 0, 0x88, data, 4, ET61X251_CTRL_TIMEOUT);
@@ -301,8 +309,8 @@ et61x251_i2c_try_write(struct et61x251_device* cam,
301 int err = 0, res; 309 int err = 0, res;
302 310
303 data[0] = address; 311 data[0] = address;
304 data[1] = cam->sensor->i2c_slave_id; 312 data[1] = cam->sensor.i2c_slave_id;
305 data[2] = cam->sensor->rsta | 0x12; 313 data[2] = cam->sensor.rsta | 0x12;
306 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 314 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
307 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT); 315 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
308 if (res < 0) 316 if (res < 0)
@@ -334,9 +342,6 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
334 u8* data = cam->control_buffer; 342 u8* data = cam->control_buffer;
335 int err = 0, res; 343 int err = 0, res;
336 344
337 if (!cam->sensor)
338 return -1;
339
340 data[0] = data2; 345 data[0] = data2;
341 data[1] = data3; 346 data[1] = data3;
342 data[2] = data4; 347 data[2] = data4;
@@ -350,8 +355,8 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
350 err += res; 355 err += res;
351 356
352 data[0] = address; 357 data[0] = address;
353 data[1] = cam->sensor->i2c_slave_id; 358 data[1] = cam->sensor.i2c_slave_id;
354 data[2] = cam->sensor->rsta | 0x02 | (n << 4); 359 data[2] = cam->sensor.rsta | 0x02 | (n << 4);
355 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41, 360 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x00, 0x41,
356 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT); 361 0, 0x88, data, 3, ET61X251_CTRL_TIMEOUT);
357 if (res < 0) 362 if (res < 0)
@@ -364,11 +369,11 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
364 if (res < 0) 369 if (res < 0)
365 err += res; 370 err += res;
366 371
367 err += et61x251_i2c_wait(cam, cam->sensor); 372 err += et61x251_i2c_wait(cam, &cam->sensor);
368 373
369 if (err) 374 if (err)
370 DBG(3, "I2C raw write failed for %s image sensor", 375 DBG(3, "I2C raw write failed for %s image sensor",
371 cam->sensor->name); 376 cam->sensor.name);
372 377
373 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, " 378 PDBGG("I2C raw write: %u bytes, address = 0x%02X, data1 = 0x%02X, "
374 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X," 379 "data2 = 0x%02X, data3 = 0x%02X, data4 = 0x%02X, data5 = 0x%02X,"
@@ -382,19 +387,13 @@ et61x251_i2c_raw_write(struct et61x251_device* cam, u8 n, u8 data1, u8 data2,
382 387
383int et61x251_i2c_read(struct et61x251_device* cam, u8 address) 388int et61x251_i2c_read(struct et61x251_device* cam, u8 address)
384{ 389{
385 if (!cam->sensor) 390 return et61x251_i2c_try_read(cam, &cam->sensor, address);
386 return -1;
387
388 return et61x251_i2c_try_read(cam, cam->sensor, address);
389} 391}
390 392
391 393
392int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value) 394int et61x251_i2c_write(struct et61x251_device* cam, u8 address, u8 value)
393{ 395{
394 if (!cam->sensor) 396 return et61x251_i2c_try_write(cam, &cam->sensor, address, value);
395 return -1;
396
397 return et61x251_i2c_try_write(cam, cam->sensor, address, value);
398} 397}
399 398
400/*****************************************************************************/ 399/*****************************************************************************/
@@ -417,7 +416,7 @@ static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
417 if ((*f)) 416 if ((*f))
418 (*f)->state = F_QUEUED; 417 (*f)->state = F_QUEUED;
419 DBG(3, "Stream interrupted"); 418 DBG(3, "Stream interrupted");
420 wake_up_interruptible(&cam->wait_stream); 419 wake_up(&cam->wait_stream);
421 } 420 }
422 421
423 if (cam->state & DEV_DISCONNECTED) 422 if (cam->state & DEV_DISCONNECTED)
@@ -435,9 +434,9 @@ static void et61x251_urb_complete(struct urb *urb, struct pt_regs* regs)
435 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t, 434 (*f) = list_entry(cam->inqueue.next, struct et61x251_frame_t,
436 frame); 435 frame);
437 436
438 imagesize = (cam->sensor->pix_format.width * 437 imagesize = (cam->sensor.pix_format.width *
439 cam->sensor->pix_format.height * 438 cam->sensor.pix_format.height *
440 cam->sensor->pix_format.priv) / 8; 439 cam->sensor.pix_format.priv) / 8;
441 440
442 for (i = 0; i < urb->number_of_packets; i++) { 441 for (i = 0; i < urb->number_of_packets; i++) {
443 unsigned int len, status; 442 unsigned int len, status;
@@ -476,7 +475,7 @@ start_of_frame:
476 475
477 if ((*f)->state == F_GRABBING) { 476 if ((*f)->state == F_GRABBING) {
478 if (sof && (*f)->buf.bytesused) { 477 if (sof && (*f)->buf.bytesused) {
479 if (cam->sensor->pix_format.pixelformat == 478 if (cam->sensor.pix_format.pixelformat ==
480 V4L2_PIX_FMT_ET61X251) 479 V4L2_PIX_FMT_ET61X251)
481 goto end_of_frame; 480 goto end_of_frame;
482 else { 481 else {
@@ -521,7 +520,7 @@ end_of_frame:
521 goto resubmit_urb; 520 goto resubmit_urb;
522 521
523 if (sof && 522 if (sof &&
524 cam->sensor->pix_format.pixelformat == 523 cam->sensor.pix_format.pixelformat ==
525 V4L2_PIX_FMT_ET61X251) 524 V4L2_PIX_FMT_ET61X251)
526 goto start_of_frame; 525 goto start_of_frame;
527 } 526 }
@@ -650,21 +649,21 @@ static int et61x251_stop_transfer(struct et61x251_device* cam)
650 649
651static int et61x251_stream_interrupt(struct et61x251_device* cam) 650static int et61x251_stream_interrupt(struct et61x251_device* cam)
652{ 651{
653 int err = 0; 652 long timeout;
654 653
655 cam->stream = STREAM_INTERRUPT; 654 cam->stream = STREAM_INTERRUPT;
656 err = wait_event_timeout(cam->wait_stream, 655 timeout = wait_event_timeout(cam->wait_stream,
657 (cam->stream == STREAM_OFF) || 656 (cam->stream == STREAM_OFF) ||
658 (cam->state & DEV_DISCONNECTED), 657 (cam->state & DEV_DISCONNECTED),
659 ET61X251_URB_TIMEOUT); 658 ET61X251_URB_TIMEOUT);
660 if (cam->state & DEV_DISCONNECTED) 659 if (cam->state & DEV_DISCONNECTED)
661 return -ENODEV; 660 return -ENODEV;
662 else if (err) { 661 else if (cam->stream != STREAM_OFF) {
663 cam->state |= DEV_MISCONFIGURED; 662 cam->state |= DEV_MISCONFIGURED;
664 DBG(1, "URB timeout reached. The camera is misconfigured. To " 663 DBG(1, "URB timeout reached. The camera is misconfigured. To "
665 "use it, close and open /dev/video%d again.", 664 "use it, close and open /dev/video%d again.",
666 cam->v4ldev->minor); 665 cam->v4ldev->minor);
667 return err; 666 return -EIO;
668 } 667 }
669 668
670 return 0; 669 return 0;
@@ -709,18 +708,18 @@ static ssize_t et61x251_show_reg(struct class_device* cd, char* buf)
709 struct et61x251_device* cam; 708 struct et61x251_device* cam;
710 ssize_t count; 709 ssize_t count;
711 710
712 if (down_interruptible(&et61x251_sysfs_lock)) 711 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
713 return -ERESTARTSYS; 712 return -ERESTARTSYS;
714 713
715 cam = video_get_drvdata(to_video_device(cd)); 714 cam = video_get_drvdata(to_video_device(cd));
716 if (!cam) { 715 if (!cam) {
717 up(&et61x251_sysfs_lock); 716 mutex_unlock(&et61x251_sysfs_lock);
718 return -ENODEV; 717 return -ENODEV;
719 } 718 }
720 719
721 count = sprintf(buf, "%u\n", cam->sysfs.reg); 720 count = sprintf(buf, "%u\n", cam->sysfs.reg);
722 721
723 up(&et61x251_sysfs_lock); 722 mutex_unlock(&et61x251_sysfs_lock);
724 723
725 return count; 724 return count;
726} 725}
@@ -733,18 +732,18 @@ et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
733 u8 index; 732 u8 index;
734 ssize_t count; 733 ssize_t count;
735 734
736 if (down_interruptible(&et61x251_sysfs_lock)) 735 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
737 return -ERESTARTSYS; 736 return -ERESTARTSYS;
738 737
739 cam = video_get_drvdata(to_video_device(cd)); 738 cam = video_get_drvdata(to_video_device(cd));
740 if (!cam) { 739 if (!cam) {
741 up(&et61x251_sysfs_lock); 740 mutex_unlock(&et61x251_sysfs_lock);
742 return -ENODEV; 741 return -ENODEV;
743 } 742 }
744 743
745 index = et61x251_strtou8(buf, len, &count); 744 index = et61x251_strtou8(buf, len, &count);
746 if (index > 0x8e || !count) { 745 if (index > 0x8e || !count) {
747 up(&et61x251_sysfs_lock); 746 mutex_unlock(&et61x251_sysfs_lock);
748 return -EINVAL; 747 return -EINVAL;
749 } 748 }
750 749
@@ -753,7 +752,7 @@ et61x251_store_reg(struct class_device* cd, const char* buf, size_t len)
753 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg); 752 DBG(2, "Moved ET61X[12]51 register index to 0x%02X", cam->sysfs.reg);
754 DBG(3, "Written bytes: %zd", count); 753 DBG(3, "Written bytes: %zd", count);
755 754
756 up(&et61x251_sysfs_lock); 755 mutex_unlock(&et61x251_sysfs_lock);
757 756
758 return count; 757 return count;
759} 758}
@@ -765,17 +764,17 @@ static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
765 ssize_t count; 764 ssize_t count;
766 int val; 765 int val;
767 766
768 if (down_interruptible(&et61x251_sysfs_lock)) 767 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
769 return -ERESTARTSYS; 768 return -ERESTARTSYS;
770 769
771 cam = video_get_drvdata(to_video_device(cd)); 770 cam = video_get_drvdata(to_video_device(cd));
772 if (!cam) { 771 if (!cam) {
773 up(&et61x251_sysfs_lock); 772 mutex_unlock(&et61x251_sysfs_lock);
774 return -ENODEV; 773 return -ENODEV;
775 } 774 }
776 775
777 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) { 776 if ((val = et61x251_read_reg(cam, cam->sysfs.reg)) < 0) {
778 up(&et61x251_sysfs_lock); 777 mutex_unlock(&et61x251_sysfs_lock);
779 return -EIO; 778 return -EIO;
780 } 779 }
781 780
@@ -783,7 +782,7 @@ static ssize_t et61x251_show_val(struct class_device* cd, char* buf)
783 782
784 DBG(3, "Read bytes: %zd", count); 783 DBG(3, "Read bytes: %zd", count);
785 784
786 up(&et61x251_sysfs_lock); 785 mutex_unlock(&et61x251_sysfs_lock);
787 786
788 return count; 787 return count;
789} 788}
@@ -797,24 +796,24 @@ et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
797 ssize_t count; 796 ssize_t count;
798 int err; 797 int err;
799 798
800 if (down_interruptible(&et61x251_sysfs_lock)) 799 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
801 return -ERESTARTSYS; 800 return -ERESTARTSYS;
802 801
803 cam = video_get_drvdata(to_video_device(cd)); 802 cam = video_get_drvdata(to_video_device(cd));
804 if (!cam) { 803 if (!cam) {
805 up(&et61x251_sysfs_lock); 804 mutex_unlock(&et61x251_sysfs_lock);
806 return -ENODEV; 805 return -ENODEV;
807 } 806 }
808 807
809 value = et61x251_strtou8(buf, len, &count); 808 value = et61x251_strtou8(buf, len, &count);
810 if (!count) { 809 if (!count) {
811 up(&et61x251_sysfs_lock); 810 mutex_unlock(&et61x251_sysfs_lock);
812 return -EINVAL; 811 return -EINVAL;
813 } 812 }
814 813
815 err = et61x251_write_reg(cam, value, cam->sysfs.reg); 814 err = et61x251_write_reg(cam, value, cam->sysfs.reg);
816 if (err) { 815 if (err) {
817 up(&et61x251_sysfs_lock); 816 mutex_unlock(&et61x251_sysfs_lock);
818 return -EIO; 817 return -EIO;
819 } 818 }
820 819
@@ -822,7 +821,7 @@ et61x251_store_val(struct class_device* cd, const char* buf, size_t len)
822 cam->sysfs.reg, value); 821 cam->sysfs.reg, value);
823 DBG(3, "Written bytes: %zd", count); 822 DBG(3, "Written bytes: %zd", count);
824 823
825 up(&et61x251_sysfs_lock); 824 mutex_unlock(&et61x251_sysfs_lock);
826 825
827 return count; 826 return count;
828} 827}
@@ -833,12 +832,12 @@ static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
833 struct et61x251_device* cam; 832 struct et61x251_device* cam;
834 ssize_t count; 833 ssize_t count;
835 834
836 if (down_interruptible(&et61x251_sysfs_lock)) 835 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
837 return -ERESTARTSYS; 836 return -ERESTARTSYS;
838 837
839 cam = video_get_drvdata(to_video_device(cd)); 838 cam = video_get_drvdata(to_video_device(cd));
840 if (!cam) { 839 if (!cam) {
841 up(&et61x251_sysfs_lock); 840 mutex_unlock(&et61x251_sysfs_lock);
842 return -ENODEV; 841 return -ENODEV;
843 } 842 }
844 843
@@ -846,7 +845,7 @@ static ssize_t et61x251_show_i2c_reg(struct class_device* cd, char* buf)
846 845
847 DBG(3, "Read bytes: %zd", count); 846 DBG(3, "Read bytes: %zd", count);
848 847
849 up(&et61x251_sysfs_lock); 848 mutex_unlock(&et61x251_sysfs_lock);
850 849
851 return count; 850 return count;
852} 851}
@@ -859,18 +858,18 @@ et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
859 u8 index; 858 u8 index;
860 ssize_t count; 859 ssize_t count;
861 860
862 if (down_interruptible(&et61x251_sysfs_lock)) 861 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
863 return -ERESTARTSYS; 862 return -ERESTARTSYS;
864 863
865 cam = video_get_drvdata(to_video_device(cd)); 864 cam = video_get_drvdata(to_video_device(cd));
866 if (!cam) { 865 if (!cam) {
867 up(&et61x251_sysfs_lock); 866 mutex_unlock(&et61x251_sysfs_lock);
868 return -ENODEV; 867 return -ENODEV;
869 } 868 }
870 869
871 index = et61x251_strtou8(buf, len, &count); 870 index = et61x251_strtou8(buf, len, &count);
872 if (!count) { 871 if (!count) {
873 up(&et61x251_sysfs_lock); 872 mutex_unlock(&et61x251_sysfs_lock);
874 return -EINVAL; 873 return -EINVAL;
875 } 874 }
876 875
@@ -879,7 +878,7 @@ et61x251_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
879 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); 878 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
880 DBG(3, "Written bytes: %zd", count); 879 DBG(3, "Written bytes: %zd", count);
881 880
882 up(&et61x251_sysfs_lock); 881 mutex_unlock(&et61x251_sysfs_lock);
883 882
884 return count; 883 return count;
885} 884}
@@ -891,22 +890,22 @@ static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
891 ssize_t count; 890 ssize_t count;
892 int val; 891 int val;
893 892
894 if (down_interruptible(&et61x251_sysfs_lock)) 893 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
895 return -ERESTARTSYS; 894 return -ERESTARTSYS;
896 895
897 cam = video_get_drvdata(to_video_device(cd)); 896 cam = video_get_drvdata(to_video_device(cd));
898 if (!cam) { 897 if (!cam) {
899 up(&et61x251_sysfs_lock); 898 mutex_unlock(&et61x251_sysfs_lock);
900 return -ENODEV; 899 return -ENODEV;
901 } 900 }
902 901
903 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) { 902 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
904 up(&et61x251_sysfs_lock); 903 mutex_unlock(&et61x251_sysfs_lock);
905 return -ENOSYS; 904 return -ENOSYS;
906 } 905 }
907 906
908 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { 907 if ((val = et61x251_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
909 up(&et61x251_sysfs_lock); 908 mutex_unlock(&et61x251_sysfs_lock);
910 return -EIO; 909 return -EIO;
911 } 910 }
912 911
@@ -914,7 +913,7 @@ static ssize_t et61x251_show_i2c_val(struct class_device* cd, char* buf)
914 913
915 DBG(3, "Read bytes: %zd", count); 914 DBG(3, "Read bytes: %zd", count);
916 915
917 up(&et61x251_sysfs_lock); 916 mutex_unlock(&et61x251_sysfs_lock);
918 917
919 return count; 918 return count;
920} 919}
@@ -928,29 +927,29 @@ et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
928 ssize_t count; 927 ssize_t count;
929 int err; 928 int err;
930 929
931 if (down_interruptible(&et61x251_sysfs_lock)) 930 if (mutex_lock_interruptible(&et61x251_sysfs_lock))
932 return -ERESTARTSYS; 931 return -ERESTARTSYS;
933 932
934 cam = video_get_drvdata(to_video_device(cd)); 933 cam = video_get_drvdata(to_video_device(cd));
935 if (!cam) { 934 if (!cam) {
936 up(&et61x251_sysfs_lock); 935 mutex_unlock(&et61x251_sysfs_lock);
937 return -ENODEV; 936 return -ENODEV;
938 } 937 }
939 938
940 if (!(cam->sensor->sysfs_ops & ET61X251_I2C_READ)) { 939 if (!(cam->sensor.sysfs_ops & ET61X251_I2C_READ)) {
941 up(&et61x251_sysfs_lock); 940 mutex_unlock(&et61x251_sysfs_lock);
942 return -ENOSYS; 941 return -ENOSYS;
943 } 942 }
944 943
945 value = et61x251_strtou8(buf, len, &count); 944 value = et61x251_strtou8(buf, len, &count);
946 if (!count) { 945 if (!count) {
947 up(&et61x251_sysfs_lock); 946 mutex_unlock(&et61x251_sysfs_lock);
948 return -EINVAL; 947 return -EINVAL;
949 } 948 }
950 949
951 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value); 950 err = et61x251_i2c_write(cam, cam->sysfs.i2c_reg, value);
952 if (err) { 951 if (err) {
953 up(&et61x251_sysfs_lock); 952 mutex_unlock(&et61x251_sysfs_lock);
954 return -EIO; 953 return -EIO;
955 } 954 }
956 955
@@ -958,7 +957,7 @@ et61x251_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
958 cam->sysfs.i2c_reg, value); 957 cam->sysfs.i2c_reg, value);
959 DBG(3, "Written bytes: %zd", count); 958 DBG(3, "Written bytes: %zd", count);
960 959
961 up(&et61x251_sysfs_lock); 960 mutex_unlock(&et61x251_sysfs_lock);
962 961
963 return count; 962 return count;
964} 963}
@@ -980,7 +979,7 @@ static void et61x251_create_sysfs(struct et61x251_device* cam)
980 979
981 video_device_create_file(v4ldev, &class_device_attr_reg); 980 video_device_create_file(v4ldev, &class_device_attr_reg);
982 video_device_create_file(v4ldev, &class_device_attr_val); 981 video_device_create_file(v4ldev, &class_device_attr_val);
983 if (cam->sensor && cam->sensor->sysfs_ops) { 982 if (cam->sensor.sysfs_ops) {
984 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 983 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
985 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 984 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
986 } 985 }
@@ -1048,7 +1047,7 @@ static int et61x251_set_scale(struct et61x251_device* cam, u8 scale)
1048static int 1047static int
1049et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect) 1048et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1050{ 1049{
1051 struct et61x251_sensor* s = cam->sensor; 1050 struct et61x251_sensor* s = &cam->sensor;
1052 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left + 1051 u16 fmw_sx = (u16)(rect->left - s->cropcap.bounds.left +
1053 s->active_pixel.left), 1052 s->active_pixel.left),
1054 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top + 1053 fmw_sy = (u16)(rect->top - s->cropcap.bounds.top +
@@ -1076,7 +1075,7 @@ et61x251_set_crop(struct et61x251_device* cam, struct v4l2_rect* rect)
1076 1075
1077static int et61x251_init(struct et61x251_device* cam) 1076static int et61x251_init(struct et61x251_device* cam)
1078{ 1077{
1079 struct et61x251_sensor* s = cam->sensor; 1078 struct et61x251_sensor* s = &cam->sensor;
1080 struct v4l2_control ctrl; 1079 struct v4l2_control ctrl;
1081 struct v4l2_queryctrl *qctrl; 1080 struct v4l2_queryctrl *qctrl;
1082 struct v4l2_rect* rect; 1081 struct v4l2_rect* rect;
@@ -1143,7 +1142,7 @@ static int et61x251_init(struct et61x251_device* cam)
1143 } 1142 }
1144 1143
1145 if (!(cam->state & DEV_INITIALIZED)) { 1144 if (!(cam->state & DEV_INITIALIZED)) {
1146 init_MUTEX(&cam->fileop_sem); 1145 mutex_init(&cam->fileop_mutex);
1147 spin_lock_init(&cam->queue_lock); 1146 spin_lock_init(&cam->queue_lock);
1148 init_waitqueue_head(&cam->wait_frame); 1147 init_waitqueue_head(&cam->wait_frame);
1149 init_waitqueue_head(&cam->wait_stream); 1148 init_waitqueue_head(&cam->wait_stream);
@@ -1161,13 +1160,15 @@ static int et61x251_init(struct et61x251_device* cam)
1161 1160
1162static void et61x251_release_resources(struct et61x251_device* cam) 1161static void et61x251_release_resources(struct et61x251_device* cam)
1163{ 1162{
1164 down(&et61x251_sysfs_lock); 1163 mutex_lock(&et61x251_sysfs_lock);
1165 1164
1166 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); 1165 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1167 video_set_drvdata(cam->v4ldev, NULL); 1166 video_set_drvdata(cam->v4ldev, NULL);
1168 video_unregister_device(cam->v4ldev); 1167 video_unregister_device(cam->v4ldev);
1169 1168
1170 up(&et61x251_sysfs_lock); 1169 usb_put_dev(cam->usbdev);
1170
1171 mutex_unlock(&et61x251_sysfs_lock);
1171 1172
1172 kfree(cam->control_buffer); 1173 kfree(cam->control_buffer);
1173} 1174}
@@ -1188,7 +1189,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1188 1189
1189 cam = video_get_drvdata(video_devdata(filp)); 1190 cam = video_get_drvdata(video_devdata(filp));
1190 1191
1191 if (down_interruptible(&cam->dev_sem)) { 1192 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1192 up_read(&et61x251_disconnect); 1193 up_read(&et61x251_disconnect);
1193 return -ERESTARTSYS; 1194 return -ERESTARTSYS;
1194 } 1195 }
@@ -1200,7 +1201,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1200 err = -EWOULDBLOCK; 1201 err = -EWOULDBLOCK;
1201 goto out; 1202 goto out;
1202 } 1203 }
1203 up(&cam->dev_sem); 1204 mutex_unlock(&cam->dev_mutex);
1204 err = wait_event_interruptible_exclusive(cam->open, 1205 err = wait_event_interruptible_exclusive(cam->open,
1205 cam->state & DEV_DISCONNECTED 1206 cam->state & DEV_DISCONNECTED
1206 || !cam->users); 1207 || !cam->users);
@@ -1212,7 +1213,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1212 up_read(&et61x251_disconnect); 1213 up_read(&et61x251_disconnect);
1213 return -ENODEV; 1214 return -ENODEV;
1214 } 1215 }
1215 down(&cam->dev_sem); 1216 mutex_lock(&cam->dev_mutex);
1216 } 1217 }
1217 1218
1218 1219
@@ -1240,7 +1241,7 @@ static int et61x251_open(struct inode* inode, struct file* filp)
1240 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); 1241 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1241 1242
1242out: 1243out:
1243 up(&cam->dev_sem); 1244 mutex_unlock(&cam->dev_mutex);
1244 up_read(&et61x251_disconnect); 1245 up_read(&et61x251_disconnect);
1245 return err; 1246 return err;
1246} 1247}
@@ -1250,7 +1251,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1250{ 1251{
1251 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 1252 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1252 1253
1253 down(&cam->dev_sem); /* prevent disconnect() to be called */ 1254 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1254 1255
1255 et61x251_stop_transfer(cam); 1256 et61x251_stop_transfer(cam);
1256 1257
@@ -1258,7 +1259,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1258 1259
1259 if (cam->state & DEV_DISCONNECTED) { 1260 if (cam->state & DEV_DISCONNECTED) {
1260 et61x251_release_resources(cam); 1261 et61x251_release_resources(cam);
1261 up(&cam->dev_sem); 1262 mutex_unlock(&cam->dev_mutex);
1262 kfree(cam); 1263 kfree(cam);
1263 return 0; 1264 return 0;
1264 } 1265 }
@@ -1268,7 +1269,7 @@ static int et61x251_release(struct inode* inode, struct file* filp)
1268 1269
1269 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); 1270 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1270 1271
1271 up(&cam->dev_sem); 1272 mutex_unlock(&cam->dev_mutex);
1272 1273
1273 return 0; 1274 return 0;
1274} 1275}
@@ -1281,28 +1282,29 @@ et61x251_read(struct file* filp, char __user * buf,
1281 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 1282 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
1282 struct et61x251_frame_t* f, * i; 1283 struct et61x251_frame_t* f, * i;
1283 unsigned long lock_flags; 1284 unsigned long lock_flags;
1285 long timeout;
1284 int err = 0; 1286 int err = 0;
1285 1287
1286 if (down_interruptible(&cam->fileop_sem)) 1288 if (mutex_lock_interruptible(&cam->fileop_mutex))
1287 return -ERESTARTSYS; 1289 return -ERESTARTSYS;
1288 1290
1289 if (cam->state & DEV_DISCONNECTED) { 1291 if (cam->state & DEV_DISCONNECTED) {
1290 DBG(1, "Device not present"); 1292 DBG(1, "Device not present");
1291 up(&cam->fileop_sem); 1293 mutex_unlock(&cam->fileop_mutex);
1292 return -ENODEV; 1294 return -ENODEV;
1293 } 1295 }
1294 1296
1295 if (cam->state & DEV_MISCONFIGURED) { 1297 if (cam->state & DEV_MISCONFIGURED) {
1296 DBG(1, "The camera is misconfigured. Close and open it " 1298 DBG(1, "The camera is misconfigured. Close and open it "
1297 "again."); 1299 "again.");
1298 up(&cam->fileop_sem); 1300 mutex_unlock(&cam->fileop_mutex);
1299 return -EIO; 1301 return -EIO;
1300 } 1302 }
1301 1303
1302 if (cam->io == IO_MMAP) { 1304 if (cam->io == IO_MMAP) {
1303 DBG(3, "Close and open the device again to choose the read " 1305 DBG(3, "Close and open the device again to choose the read "
1304 "method"); 1306 "method");
1305 up(&cam->fileop_sem); 1307 mutex_unlock(&cam->fileop_mutex);
1306 return -EINVAL; 1308 return -EINVAL;
1307 } 1309 }
1308 1310
@@ -1310,7 +1312,7 @@ et61x251_read(struct file* filp, char __user * buf,
1310 if (!et61x251_request_buffers(cam, cam->nreadbuffers, 1312 if (!et61x251_request_buffers(cam, cam->nreadbuffers,
1311 IO_READ)) { 1313 IO_READ)) {
1312 DBG(1, "read() failed, not enough memory"); 1314 DBG(1, "read() failed, not enough memory");
1313 up(&cam->fileop_sem); 1315 mutex_unlock(&cam->fileop_mutex);
1314 return -ENOMEM; 1316 return -ENOMEM;
1315 } 1317 }
1316 cam->io = IO_READ; 1318 cam->io = IO_READ;
@@ -1324,30 +1326,32 @@ et61x251_read(struct file* filp, char __user * buf,
1324 } 1326 }
1325 1327
1326 if (!count) { 1328 if (!count) {
1327 up(&cam->fileop_sem); 1329 mutex_unlock(&cam->fileop_mutex);
1328 return 0; 1330 return 0;
1329 } 1331 }
1330 1332
1331 if (list_empty(&cam->outqueue)) { 1333 if (list_empty(&cam->outqueue)) {
1332 if (filp->f_flags & O_NONBLOCK) { 1334 if (filp->f_flags & O_NONBLOCK) {
1333 up(&cam->fileop_sem); 1335 mutex_unlock(&cam->fileop_mutex);
1334 return -EAGAIN; 1336 return -EAGAIN;
1335 } 1337 }
1336 err = wait_event_interruptible 1338 timeout = wait_event_interruptible_timeout
1337 ( cam->wait_frame, 1339 ( cam->wait_frame,
1338 (!list_empty(&cam->outqueue)) || 1340 (!list_empty(&cam->outqueue)) ||
1339 (cam->state & DEV_DISCONNECTED) || 1341 (cam->state & DEV_DISCONNECTED) ||
1340 (cam->state & DEV_MISCONFIGURED) ); 1342 (cam->state & DEV_MISCONFIGURED),
1341 if (err) { 1343 cam->module_param.frame_timeout *
1342 up(&cam->fileop_sem); 1344 1000 * msecs_to_jiffies(1) );
1343 return err; 1345 if (timeout < 0) {
1346 mutex_unlock(&cam->fileop_mutex);
1347 return timeout;
1344 } 1348 }
1345 if (cam->state & DEV_DISCONNECTED) { 1349 if (cam->state & DEV_DISCONNECTED) {
1346 up(&cam->fileop_sem); 1350 mutex_unlock(&cam->fileop_mutex);
1347 return -ENODEV; 1351 return -ENODEV;
1348 } 1352 }
1349 if (cam->state & DEV_MISCONFIGURED) { 1353 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1350 up(&cam->fileop_sem); 1354 mutex_unlock(&cam->fileop_mutex);
1351 return -EIO; 1355 return -EIO;
1352 } 1356 }
1353 } 1357 }
@@ -1375,7 +1379,7 @@ exit:
1375 PDBGG("Frame #%lu, bytes read: %zu", 1379 PDBGG("Frame #%lu, bytes read: %zu",
1376 (unsigned long)f->buf.index, count); 1380 (unsigned long)f->buf.index, count);
1377 1381
1378 up(&cam->fileop_sem); 1382 mutex_unlock(&cam->fileop_mutex);
1379 1383
1380 return err ? err : count; 1384 return err ? err : count;
1381} 1385}
@@ -1388,7 +1392,7 @@ static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1388 unsigned long lock_flags; 1392 unsigned long lock_flags;
1389 unsigned int mask = 0; 1393 unsigned int mask = 0;
1390 1394
1391 if (down_interruptible(&cam->fileop_sem)) 1395 if (mutex_lock_interruptible(&cam->fileop_mutex))
1392 return POLLERR; 1396 return POLLERR;
1393 1397
1394 if (cam->state & DEV_DISCONNECTED) { 1398 if (cam->state & DEV_DISCONNECTED) {
@@ -1426,12 +1430,12 @@ static unsigned int et61x251_poll(struct file *filp, poll_table *wait)
1426 if (!list_empty(&cam->outqueue)) 1430 if (!list_empty(&cam->outqueue))
1427 mask |= POLLIN | POLLRDNORM; 1431 mask |= POLLIN | POLLRDNORM;
1428 1432
1429 up(&cam->fileop_sem); 1433 mutex_unlock(&cam->fileop_mutex);
1430 1434
1431 return mask; 1435 return mask;
1432 1436
1433error: 1437error:
1434 up(&cam->fileop_sem); 1438 mutex_unlock(&cam->fileop_mutex);
1435 return POLLERR; 1439 return POLLERR;
1436} 1440}
1437 1441
@@ -1465,25 +1469,25 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1465 void *pos; 1469 void *pos;
1466 u32 i; 1470 u32 i;
1467 1471
1468 if (down_interruptible(&cam->fileop_sem)) 1472 if (mutex_lock_interruptible(&cam->fileop_mutex))
1469 return -ERESTARTSYS; 1473 return -ERESTARTSYS;
1470 1474
1471 if (cam->state & DEV_DISCONNECTED) { 1475 if (cam->state & DEV_DISCONNECTED) {
1472 DBG(1, "Device not present"); 1476 DBG(1, "Device not present");
1473 up(&cam->fileop_sem); 1477 mutex_unlock(&cam->fileop_mutex);
1474 return -ENODEV; 1478 return -ENODEV;
1475 } 1479 }
1476 1480
1477 if (cam->state & DEV_MISCONFIGURED) { 1481 if (cam->state & DEV_MISCONFIGURED) {
1478 DBG(1, "The camera is misconfigured. Close and open it " 1482 DBG(1, "The camera is misconfigured. Close and open it "
1479 "again."); 1483 "again.");
1480 up(&cam->fileop_sem); 1484 mutex_unlock(&cam->fileop_mutex);
1481 return -EIO; 1485 return -EIO;
1482 } 1486 }
1483 1487
1484 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || 1488 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1485 size != PAGE_ALIGN(cam->frame[0].buf.length)) { 1489 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1486 up(&cam->fileop_sem); 1490 mutex_unlock(&cam->fileop_mutex);
1487 return -EINVAL; 1491 return -EINVAL;
1488 } 1492 }
1489 1493
@@ -1492,7 +1496,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1492 break; 1496 break;
1493 } 1497 }
1494 if (i == cam->nbuffers) { 1498 if (i == cam->nbuffers) {
1495 up(&cam->fileop_sem); 1499 mutex_unlock(&cam->fileop_mutex);
1496 return -EINVAL; 1500 return -EINVAL;
1497 } 1501 }
1498 1502
@@ -1502,7 +1506,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1502 pos = cam->frame[i].bufmem; 1506 pos = cam->frame[i].bufmem;
1503 while (size > 0) { /* size is page-aligned */ 1507 while (size > 0) { /* size is page-aligned */
1504 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1508 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1505 up(&cam->fileop_sem); 1509 mutex_unlock(&cam->fileop_mutex);
1506 return -EAGAIN; 1510 return -EAGAIN;
1507 } 1511 }
1508 start += PAGE_SIZE; 1512 start += PAGE_SIZE;
@@ -1515,7 +1519,7 @@ static int et61x251_mmap(struct file* filp, struct vm_area_struct *vma)
1515 1519
1516 et61x251_vm_open(vma); 1520 et61x251_vm_open(vma);
1517 1521
1518 up(&cam->fileop_sem); 1522 mutex_unlock(&cam->fileop_mutex);
1519 1523
1520 return 0; 1524 return 0;
1521} 1525}
@@ -1557,6 +1561,7 @@ et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1557 1561
1558 memset(&i, 0, sizeof(i)); 1562 memset(&i, 0, sizeof(i));
1559 strcpy(i.name, "Camera"); 1563 strcpy(i.name, "Camera");
1564 i.type = V4L2_INPUT_TYPE_CAMERA;
1560 1565
1561 if (copy_to_user(arg, &i, sizeof(i))) 1566 if (copy_to_user(arg, &i, sizeof(i)))
1562 return -EFAULT; 1567 return -EFAULT;
@@ -1566,7 +1571,19 @@ et61x251_vidioc_enuminput(struct et61x251_device* cam, void __user * arg)
1566 1571
1567 1572
1568static int 1573static int
1569et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg) 1574et61x251_vidioc_g_input(struct et61x251_device* cam, void __user * arg)
1575{
1576 int index = 0;
1577
1578 if (copy_to_user(arg, &index, sizeof(index)))
1579 return -EFAULT;
1580
1581 return 0;
1582}
1583
1584
1585static int
1586et61x251_vidioc_s_input(struct et61x251_device* cam, void __user * arg)
1570{ 1587{
1571 int index; 1588 int index;
1572 1589
@@ -1583,7 +1600,7 @@ et61x251_vidioc_gs_input(struct et61x251_device* cam, void __user * arg)
1583static int 1600static int
1584et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg) 1601et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1585{ 1602{
1586 struct et61x251_sensor* s = cam->sensor; 1603 struct et61x251_sensor* s = &cam->sensor;
1587 struct v4l2_queryctrl qc; 1604 struct v4l2_queryctrl qc;
1588 u8 i; 1605 u8 i;
1589 1606
@@ -1605,7 +1622,7 @@ et61x251_vidioc_query_ctrl(struct et61x251_device* cam, void __user * arg)
1605static int 1622static int
1606et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg) 1623et61x251_vidioc_g_ctrl(struct et61x251_device* cam, void __user * arg)
1607{ 1624{
1608 struct et61x251_sensor* s = cam->sensor; 1625 struct et61x251_sensor* s = &cam->sensor;
1609 struct v4l2_control ctrl; 1626 struct v4l2_control ctrl;
1610 int err = 0; 1627 int err = 0;
1611 u8 i; 1628 u8 i;
@@ -1637,7 +1654,7 @@ exit:
1637static int 1654static int
1638et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg) 1655et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1639{ 1656{
1640 struct et61x251_sensor* s = cam->sensor; 1657 struct et61x251_sensor* s = &cam->sensor;
1641 struct v4l2_control ctrl; 1658 struct v4l2_control ctrl;
1642 u8 i; 1659 u8 i;
1643 int err = 0; 1660 int err = 0;
@@ -1650,6 +1667,8 @@ et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1650 1667
1651 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1668 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1652 if (ctrl.id == s->qctrl[i].id) { 1669 if (ctrl.id == s->qctrl[i].id) {
1670 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1671 return -EINVAL;
1653 if (ctrl.value < s->qctrl[i].minimum || 1672 if (ctrl.value < s->qctrl[i].minimum ||
1654 ctrl.value > s->qctrl[i].maximum) 1673 ctrl.value > s->qctrl[i].maximum)
1655 return -ERANGE; 1674 return -ERANGE;
@@ -1669,7 +1688,7 @@ et61x251_vidioc_s_ctrl(struct et61x251_device* cam, void __user * arg)
1669static int 1688static int
1670et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg) 1689et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1671{ 1690{
1672 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 1691 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1673 1692
1674 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1693 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1675 cc->pixelaspect.numerator = 1; 1694 cc->pixelaspect.numerator = 1;
@@ -1685,7 +1704,7 @@ et61x251_vidioc_cropcap(struct et61x251_device* cam, void __user * arg)
1685static int 1704static int
1686et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg) 1705et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1687{ 1706{
1688 struct et61x251_sensor* s = cam->sensor; 1707 struct et61x251_sensor* s = &cam->sensor;
1689 struct v4l2_crop crop = { 1708 struct v4l2_crop crop = {
1690 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 1709 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1691 }; 1710 };
@@ -1702,7 +1721,7 @@ et61x251_vidioc_g_crop(struct et61x251_device* cam, void __user * arg)
1702static int 1721static int
1703et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg) 1722et61x251_vidioc_s_crop(struct et61x251_device* cam, void __user * arg)
1704{ 1723{
1705 struct et61x251_sensor* s = cam->sensor; 1724 struct et61x251_sensor* s = &cam->sensor;
1706 struct v4l2_crop crop; 1725 struct v4l2_crop crop;
1707 struct v4l2_rect* rect; 1726 struct v4l2_rect* rect;
1708 struct v4l2_rect* bounds = &(s->cropcap.bounds); 1727 struct v4l2_rect* bounds = &(s->cropcap.bounds);
@@ -1843,7 +1862,7 @@ static int
1843et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg) 1862et61x251_vidioc_g_fmt(struct et61x251_device* cam, void __user * arg)
1844{ 1863{
1845 struct v4l2_format format; 1864 struct v4l2_format format;
1846 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format); 1865 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1847 1866
1848 if (copy_from_user(&format, arg, sizeof(format))) 1867 if (copy_from_user(&format, arg, sizeof(format)))
1849 return -EFAULT; 1868 return -EFAULT;
@@ -1868,7 +1887,7 @@ static int
1868et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd, 1887et61x251_vidioc_try_s_fmt(struct et61x251_device* cam, unsigned int cmd,
1869 void __user * arg) 1888 void __user * arg)
1870{ 1889{
1871 struct et61x251_sensor* s = cam->sensor; 1890 struct et61x251_sensor* s = &cam->sensor;
1872 struct v4l2_format format; 1891 struct v4l2_format format;
1873 struct v4l2_pix_format* pix; 1892 struct v4l2_pix_format* pix;
1874 struct v4l2_pix_format* pfmt = &(s->pix_format); 1893 struct v4l2_pix_format* pfmt = &(s->pix_format);
@@ -2155,7 +2174,7 @@ et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2155 struct v4l2_buffer b; 2174 struct v4l2_buffer b;
2156 struct et61x251_frame_t *f; 2175 struct et61x251_frame_t *f;
2157 unsigned long lock_flags; 2176 unsigned long lock_flags;
2158 int err = 0; 2177 long timeout;
2159 2178
2160 if (copy_from_user(&b, arg, sizeof(b))) 2179 if (copy_from_user(&b, arg, sizeof(b)))
2161 return -EFAULT; 2180 return -EFAULT;
@@ -2168,16 +2187,18 @@ et61x251_vidioc_dqbuf(struct et61x251_device* cam, struct file* filp,
2168 return -EINVAL; 2187 return -EINVAL;
2169 if (filp->f_flags & O_NONBLOCK) 2188 if (filp->f_flags & O_NONBLOCK)
2170 return -EAGAIN; 2189 return -EAGAIN;
2171 err = wait_event_interruptible 2190 timeout = wait_event_interruptible_timeout
2172 ( cam->wait_frame, 2191 ( cam->wait_frame,
2173 (!list_empty(&cam->outqueue)) || 2192 (!list_empty(&cam->outqueue)) ||
2174 (cam->state & DEV_DISCONNECTED) || 2193 (cam->state & DEV_DISCONNECTED) ||
2175 (cam->state & DEV_MISCONFIGURED) ); 2194 (cam->state & DEV_MISCONFIGURED),
2176 if (err) 2195 cam->module_param.frame_timeout *
2177 return err; 2196 1000 * msecs_to_jiffies(1) );
2197 if (timeout < 0)
2198 return timeout;
2178 if (cam->state & DEV_DISCONNECTED) 2199 if (cam->state & DEV_DISCONNECTED)
2179 return -ENODEV; 2200 return -ENODEV;
2180 if (cam->state & DEV_MISCONFIGURED) 2201 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2181 return -EIO; 2202 return -EIO;
2182 } 2203 }
2183 2204
@@ -2309,8 +2330,10 @@ static int et61x251_ioctl_v4l2(struct inode* inode, struct file* filp,
2309 return et61x251_vidioc_enuminput(cam, arg); 2330 return et61x251_vidioc_enuminput(cam, arg);
2310 2331
2311 case VIDIOC_G_INPUT: 2332 case VIDIOC_G_INPUT:
2333 return et61x251_vidioc_g_input(cam, arg);
2334
2312 case VIDIOC_S_INPUT: 2335 case VIDIOC_S_INPUT:
2313 return et61x251_vidioc_gs_input(cam, arg); 2336 return et61x251_vidioc_s_input(cam, arg);
2314 2337
2315 case VIDIOC_QUERYCTRL: 2338 case VIDIOC_QUERYCTRL:
2316 return et61x251_vidioc_query_ctrl(cam, arg); 2339 return et61x251_vidioc_query_ctrl(cam, arg);
@@ -2393,19 +2416,19 @@ static int et61x251_ioctl(struct inode* inode, struct file* filp,
2393 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp)); 2416 struct et61x251_device* cam = video_get_drvdata(video_devdata(filp));
2394 int err = 0; 2417 int err = 0;
2395 2418
2396 if (down_interruptible(&cam->fileop_sem)) 2419 if (mutex_lock_interruptible(&cam->fileop_mutex))
2397 return -ERESTARTSYS; 2420 return -ERESTARTSYS;
2398 2421
2399 if (cam->state & DEV_DISCONNECTED) { 2422 if (cam->state & DEV_DISCONNECTED) {
2400 DBG(1, "Device not present"); 2423 DBG(1, "Device not present");
2401 up(&cam->fileop_sem); 2424 mutex_unlock(&cam->fileop_mutex);
2402 return -ENODEV; 2425 return -ENODEV;
2403 } 2426 }
2404 2427
2405 if (cam->state & DEV_MISCONFIGURED) { 2428 if (cam->state & DEV_MISCONFIGURED) {
2406 DBG(1, "The camera is misconfigured. Close and open it " 2429 DBG(1, "The camera is misconfigured. Close and open it "
2407 "again."); 2430 "again.");
2408 up(&cam->fileop_sem); 2431 mutex_unlock(&cam->fileop_mutex);
2409 return -EIO; 2432 return -EIO;
2410 } 2433 }
2411 2434
@@ -2413,7 +2436,7 @@ static int et61x251_ioctl(struct inode* inode, struct file* filp,
2413 2436
2414 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2437 err = et61x251_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2415 2438
2416 up(&cam->fileop_sem); 2439 mutex_unlock(&cam->fileop_mutex);
2417 2440
2418 return err; 2441 return err;
2419} 2442}
@@ -2459,7 +2482,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2459 goto fail; 2482 goto fail;
2460 } 2483 }
2461 2484
2462 init_MUTEX(&cam->dev_sem); 2485 mutex_init(&cam->dev_mutex);
2463 2486
2464 DBG(2, "ET61X[12]51 PC Camera Controller detected " 2487 DBG(2, "ET61X[12]51 PC Camera Controller detected "
2465 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct); 2488 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
@@ -2470,8 +2493,8 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2470 break; 2493 break;
2471 } 2494 }
2472 2495
2473 if (!err && cam->sensor) 2496 if (!err)
2474 DBG(2, "%s image sensor detected", cam->sensor->name); 2497 DBG(2, "%s image sensor detected", cam->sensor.name);
2475 else { 2498 else {
2476 DBG(1, "No supported image sensor detected"); 2499 DBG(1, "No supported image sensor detected");
2477 err = -ENODEV; 2500 err = -ENODEV;
@@ -2492,7 +2515,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2492 cam->v4ldev->release = video_device_release; 2515 cam->v4ldev->release = video_device_release;
2493 video_set_drvdata(cam->v4ldev, cam); 2516 video_set_drvdata(cam->v4ldev, cam);
2494 2517
2495 down(&cam->dev_sem); 2518 mutex_lock(&cam->dev_mutex);
2496 2519
2497 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2520 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2498 video_nr[dev_nr]); 2521 video_nr[dev_nr]);
@@ -2502,13 +2525,14 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2502 DBG(1, "Free /dev/videoX node not found"); 2525 DBG(1, "Free /dev/videoX node not found");
2503 video_nr[dev_nr] = -1; 2526 video_nr[dev_nr] = -1;
2504 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0; 2527 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2505 up(&cam->dev_sem); 2528 mutex_unlock(&cam->dev_mutex);
2506 goto fail; 2529 goto fail;
2507 } 2530 }
2508 2531
2509 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); 2532 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2510 2533
2511 cam->module_param.force_munmap = force_munmap[dev_nr]; 2534 cam->module_param.force_munmap = force_munmap[dev_nr];
2535 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2512 2536
2513 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0; 2537 dev_nr = (dev_nr < ET61X251_MAX_DEVICES-1) ? dev_nr+1 : 0;
2514 2538
@@ -2519,7 +2543,7 @@ et61x251_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2519 2543
2520 usb_set_intfdata(intf, cam); 2544 usb_set_intfdata(intf, cam);
2521 2545
2522 up(&cam->dev_sem); 2546 mutex_unlock(&cam->dev_mutex);
2523 2547
2524 return 0; 2548 return 0;
2525 2549
@@ -2543,7 +2567,7 @@ static void et61x251_usb_disconnect(struct usb_interface* intf)
2543 2567
2544 down_write(&et61x251_disconnect); 2568 down_write(&et61x251_disconnect);
2545 2569
2546 down(&cam->dev_sem); 2570 mutex_lock(&cam->dev_mutex);
2547 2571
2548 DBG(2, "Disconnecting %s...", cam->v4ldev->name); 2572 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2549 2573
@@ -2557,13 +2581,14 @@ static void et61x251_usb_disconnect(struct usb_interface* intf)
2557 et61x251_stop_transfer(cam); 2581 et61x251_stop_transfer(cam);
2558 cam->state |= DEV_DISCONNECTED; 2582 cam->state |= DEV_DISCONNECTED;
2559 wake_up_interruptible(&cam->wait_frame); 2583 wake_up_interruptible(&cam->wait_frame);
2560 wake_up_interruptible(&cam->wait_stream); 2584 wake_up(&cam->wait_stream);
2585 usb_get_dev(cam->usbdev);
2561 } else { 2586 } else {
2562 cam->state |= DEV_DISCONNECTED; 2587 cam->state |= DEV_DISCONNECTED;
2563 et61x251_release_resources(cam); 2588 et61x251_release_resources(cam);
2564 } 2589 }
2565 2590
2566 up(&cam->dev_sem); 2591 mutex_unlock(&cam->dev_mutex);
2567 2592
2568 if (!cam->users) 2593 if (!cam->users)
2569 kfree(cam); 2594 kfree(cam);
diff --git a/drivers/usb/media/et61x251_sensor.h b/drivers/usb/media/et61x251_sensor.h
index b9df91062fc0..56841ae8a207 100644
--- a/drivers/usb/media/et61x251_sensor.h
+++ b/drivers/usb/media/et61x251_sensor.h
@@ -42,6 +42,9 @@ static int (*et61x251_sensor_table[])(struct et61x251_device*) = { \
42 NULL, \ 42 NULL, \
43}; 43};
44 44
45extern struct et61x251_device*
46et61x251_match_id(struct et61x251_device* cam, const struct usb_device_id *id);
47
45extern void 48extern void
46et61x251_attach_sensor(struct et61x251_device* cam, 49et61x251_attach_sensor(struct et61x251_device* cam,
47 struct et61x251_sensor* sensor); 50 struct et61x251_sensor* sensor);
@@ -105,8 +108,6 @@ struct et61x251_sensor {
105 int (*set_pix_format)(struct et61x251_device* cam, 108 int (*set_pix_format)(struct et61x251_device* cam,
106 const struct v4l2_pix_format* pix); 109 const struct v4l2_pix_format* pix);
107 110
108 const struct usb_device* usbdev;
109
110 /* Private */ 111 /* Private */
111 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS]; 112 struct v4l2_queryctrl _qctrl[ET61X251_MAX_CTRLS];
112 struct v4l2_rect _rect; 113 struct v4l2_rect _rect;
diff --git a/drivers/usb/media/et61x251_tas5130d1b.c b/drivers/usb/media/et61x251_tas5130d1b.c
index 65f1ae9cf2b3..3998d76a307a 100644
--- a/drivers/usb/media/et61x251_tas5130d1b.c
+++ b/drivers/usb/media/et61x251_tas5130d1b.c
@@ -126,12 +126,16 @@ static struct et61x251_sensor tas5130d1b = {
126 126
127int et61x251_probe_tas5130d1b(struct et61x251_device* cam) 127int et61x251_probe_tas5130d1b(struct et61x251_device* cam)
128{ 128{
129 /* This sensor has no identifiers, so let's attach it anyway */ 129 const struct usb_device_id tas5130d1b_id_table[] = {
130 et61x251_attach_sensor(cam, &tas5130d1b); 130 { USB_DEVICE(0x102c, 0x6251), },
131 { }
132 };
131 133
132 /* Sensor detection is based on USB pid/vid */ 134 /* Sensor detection is based on USB pid/vid */
133 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6251) 135 if (!et61x251_match_id(cam, tas5130d1b_id_table))
134 return -ENODEV; 136 return -ENODEV;
135 137
138 et61x251_attach_sensor(cam, &tas5130d1b);
139
136 return 0; 140 return 0;
137} 141}
diff --git a/drivers/usb/media/ov511.c b/drivers/usb/media/ov511.c
index 51e9cc06f7e3..da44579d6f29 100644
--- a/drivers/usb/media/ov511.c
+++ b/drivers/usb/media/ov511.c
@@ -365,14 +365,14 @@ reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
365 365
366 PDEBUG(5, "0x%02X:0x%02X", reg, value); 366 PDEBUG(5, "0x%02X:0x%02X", reg, value);
367 367
368 down(&ov->cbuf_lock); 368 mutex_lock(&ov->cbuf_lock);
369 ov->cbuf[0] = value; 369 ov->cbuf[0] = value;
370 rc = usb_control_msg(ov->dev, 370 rc = usb_control_msg(ov->dev,
371 usb_sndctrlpipe(ov->dev, 0), 371 usb_sndctrlpipe(ov->dev, 0),
372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */, 372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE, 373 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
374 0, (__u16)reg, &ov->cbuf[0], 1, 1000); 374 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
375 up(&ov->cbuf_lock); 375 mutex_unlock(&ov->cbuf_lock);
376 376
377 if (rc < 0) 377 if (rc < 0)
378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc)); 378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
@@ -387,7 +387,7 @@ reg_r(struct usb_ov511 *ov, unsigned char reg)
387{ 387{
388 int rc; 388 int rc;
389 389
390 down(&ov->cbuf_lock); 390 mutex_lock(&ov->cbuf_lock);
391 rc = usb_control_msg(ov->dev, 391 rc = usb_control_msg(ov->dev,
392 usb_rcvctrlpipe(ov->dev, 0), 392 usb_rcvctrlpipe(ov->dev, 0),
393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */, 393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
@@ -401,7 +401,7 @@ reg_r(struct usb_ov511 *ov, unsigned char reg)
401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]); 401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
402 } 402 }
403 403
404 up(&ov->cbuf_lock); 404 mutex_unlock(&ov->cbuf_lock);
405 405
406 return rc; 406 return rc;
407} 407}
@@ -444,7 +444,7 @@ ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
444 444
445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n); 445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
446 446
447 down(&ov->cbuf_lock); 447 mutex_lock(&ov->cbuf_lock);
448 448
449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val); 449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
450 450
@@ -453,7 +453,7 @@ ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
453 1 /* REG_IO */, 453 1 /* REG_IO */,
454 USB_TYPE_VENDOR | USB_RECIP_DEVICE, 454 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
455 0, (__u16)reg, ov->cbuf, n, 1000); 455 0, (__u16)reg, ov->cbuf, n, 1000);
456 up(&ov->cbuf_lock); 456 mutex_unlock(&ov->cbuf_lock);
457 457
458 if (rc < 0) 458 if (rc < 0)
459 err("reg write multiple: error %d: %s", rc, 459 err("reg write multiple: error %d: %s", rc,
@@ -768,14 +768,14 @@ i2c_r(struct usb_ov511 *ov, unsigned char reg)
768{ 768{
769 int rc; 769 int rc;
770 770
771 down(&ov->i2c_lock); 771 mutex_lock(&ov->i2c_lock);
772 772
773 if (ov->bclass == BCL_OV518) 773 if (ov->bclass == BCL_OV518)
774 rc = ov518_i2c_read_internal(ov, reg); 774 rc = ov518_i2c_read_internal(ov, reg);
775 else 775 else
776 rc = ov511_i2c_read_internal(ov, reg); 776 rc = ov511_i2c_read_internal(ov, reg);
777 777
778 up(&ov->i2c_lock); 778 mutex_unlock(&ov->i2c_lock);
779 779
780 return rc; 780 return rc;
781} 781}
@@ -785,14 +785,14 @@ i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
785{ 785{
786 int rc; 786 int rc;
787 787
788 down(&ov->i2c_lock); 788 mutex_lock(&ov->i2c_lock);
789 789
790 if (ov->bclass == BCL_OV518) 790 if (ov->bclass == BCL_OV518)
791 rc = ov518_i2c_write_internal(ov, reg, value); 791 rc = ov518_i2c_write_internal(ov, reg, value);
792 else 792 else
793 rc = ov511_i2c_write_internal(ov, reg, value); 793 rc = ov511_i2c_write_internal(ov, reg, value);
794 794
795 up(&ov->i2c_lock); 795 mutex_unlock(&ov->i2c_lock);
796 796
797 return rc; 797 return rc;
798} 798}
@@ -842,9 +842,9 @@ i2c_w_mask(struct usb_ov511 *ov,
842{ 842{
843 int rc; 843 int rc;
844 844
845 down(&ov->i2c_lock); 845 mutex_lock(&ov->i2c_lock);
846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask); 846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
847 up(&ov->i2c_lock); 847 mutex_unlock(&ov->i2c_lock);
848 848
849 return rc; 849 return rc;
850} 850}
@@ -880,7 +880,7 @@ i2c_w_slave(struct usb_ov511 *ov,
880{ 880{
881 int rc = 0; 881 int rc = 0;
882 882
883 down(&ov->i2c_lock); 883 mutex_lock(&ov->i2c_lock);
884 884
885 /* Set new slave IDs */ 885 /* Set new slave IDs */
886 rc = i2c_set_slave_internal(ov, slave); 886 rc = i2c_set_slave_internal(ov, slave);
@@ -894,7 +894,7 @@ out:
894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0) 894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
895 err("Couldn't restore primary I2C slave"); 895 err("Couldn't restore primary I2C slave");
896 896
897 up(&ov->i2c_lock); 897 mutex_unlock(&ov->i2c_lock);
898 return rc; 898 return rc;
899} 899}
900 900
@@ -906,7 +906,7 @@ i2c_r_slave(struct usb_ov511 *ov,
906{ 906{
907 int rc; 907 int rc;
908 908
909 down(&ov->i2c_lock); 909 mutex_lock(&ov->i2c_lock);
910 910
911 /* Set new slave IDs */ 911 /* Set new slave IDs */
912 rc = i2c_set_slave_internal(ov, slave); 912 rc = i2c_set_slave_internal(ov, slave);
@@ -923,7 +923,7 @@ out:
923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0) 923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
924 err("Couldn't restore primary I2C slave"); 924 err("Couldn't restore primary I2C slave");
925 925
926 up(&ov->i2c_lock); 926 mutex_unlock(&ov->i2c_lock);
927 return rc; 927 return rc;
928} 928}
929 929
@@ -933,7 +933,7 @@ ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
933{ 933{
934 int rc; 934 int rc;
935 935
936 down(&ov->i2c_lock); 936 mutex_lock(&ov->i2c_lock);
937 937
938 rc = i2c_set_slave_internal(ov, sid); 938 rc = i2c_set_slave_internal(ov, sid);
939 if (rc < 0) 939 if (rc < 0)
@@ -942,7 +942,7 @@ ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
942 // FIXME: Is this actually necessary? 942 // FIXME: Is this actually necessary?
943 rc = ov51x_reset(ov, OV511_RESET_NOREGS); 943 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
944out: 944out:
945 up(&ov->i2c_lock); 945 mutex_unlock(&ov->i2c_lock);
946 return rc; 946 return rc;
947} 947}
948 948
@@ -3832,7 +3832,7 @@ ov51x_alloc(struct usb_ov511 *ov)
3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h); 3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3833 3833
3834 PDEBUG(4, "entered"); 3834 PDEBUG(4, "entered");
3835 down(&ov->buf_lock); 3835 mutex_lock(&ov->buf_lock);
3836 3836
3837 if (ov->buf_state == BUF_ALLOCATED) 3837 if (ov->buf_state == BUF_ALLOCATED)
3838 goto out; 3838 goto out;
@@ -3879,12 +3879,12 @@ ov51x_alloc(struct usb_ov511 *ov)
3879 3879
3880 ov->buf_state = BUF_ALLOCATED; 3880 ov->buf_state = BUF_ALLOCATED;
3881out: 3881out:
3882 up(&ov->buf_lock); 3882 mutex_unlock(&ov->buf_lock);
3883 PDEBUG(4, "leaving"); 3883 PDEBUG(4, "leaving");
3884 return 0; 3884 return 0;
3885error: 3885error:
3886 ov51x_do_dealloc(ov); 3886 ov51x_do_dealloc(ov);
3887 up(&ov->buf_lock); 3887 mutex_unlock(&ov->buf_lock);
3888 PDEBUG(4, "errored"); 3888 PDEBUG(4, "errored");
3889 return -ENOMEM; 3889 return -ENOMEM;
3890} 3890}
@@ -3893,9 +3893,9 @@ static void
3893ov51x_dealloc(struct usb_ov511 *ov) 3893ov51x_dealloc(struct usb_ov511 *ov)
3894{ 3894{
3895 PDEBUG(4, "entered"); 3895 PDEBUG(4, "entered");
3896 down(&ov->buf_lock); 3896 mutex_lock(&ov->buf_lock);
3897 ov51x_do_dealloc(ov); 3897 ov51x_do_dealloc(ov);
3898 up(&ov->buf_lock); 3898 mutex_unlock(&ov->buf_lock);
3899 PDEBUG(4, "leaving"); 3899 PDEBUG(4, "leaving");
3900} 3900}
3901 3901
@@ -3914,7 +3914,7 @@ ov51x_v4l1_open(struct inode *inode, struct file *file)
3914 3914
3915 PDEBUG(4, "opening"); 3915 PDEBUG(4, "opening");
3916 3916
3917 down(&ov->lock); 3917 mutex_lock(&ov->lock);
3918 3918
3919 err = -EBUSY; 3919 err = -EBUSY;
3920 if (ov->user) 3920 if (ov->user)
@@ -3958,7 +3958,7 @@ ov51x_v4l1_open(struct inode *inode, struct file *file)
3958 ov51x_led_control(ov, 1); 3958 ov51x_led_control(ov, 1);
3959 3959
3960out: 3960out:
3961 up(&ov->lock); 3961 mutex_unlock(&ov->lock);
3962 return err; 3962 return err;
3963} 3963}
3964 3964
@@ -3970,7 +3970,7 @@ ov51x_v4l1_close(struct inode *inode, struct file *file)
3970 3970
3971 PDEBUG(4, "ov511_close"); 3971 PDEBUG(4, "ov511_close");
3972 3972
3973 down(&ov->lock); 3973 mutex_lock(&ov->lock);
3974 3974
3975 ov->user--; 3975 ov->user--;
3976 ov51x_stop_isoc(ov); 3976 ov51x_stop_isoc(ov);
@@ -3981,15 +3981,15 @@ ov51x_v4l1_close(struct inode *inode, struct file *file)
3981 if (ov->dev) 3981 if (ov->dev)
3982 ov51x_dealloc(ov); 3982 ov51x_dealloc(ov);
3983 3983
3984 up(&ov->lock); 3984 mutex_unlock(&ov->lock);
3985 3985
3986 /* Device unplugged while open. Only a minimum of unregistration is done 3986 /* Device unplugged while open. Only a minimum of unregistration is done
3987 * here; the disconnect callback already did the rest. */ 3987 * here; the disconnect callback already did the rest. */
3988 if (!ov->dev) { 3988 if (!ov->dev) {
3989 down(&ov->cbuf_lock); 3989 mutex_lock(&ov->cbuf_lock);
3990 kfree(ov->cbuf); 3990 kfree(ov->cbuf);
3991 ov->cbuf = NULL; 3991 ov->cbuf = NULL;
3992 up(&ov->cbuf_lock); 3992 mutex_unlock(&ov->cbuf_lock);
3993 3993
3994 ov51x_dealloc(ov); 3994 ov51x_dealloc(ov);
3995 kfree(ov); 3995 kfree(ov);
@@ -4449,12 +4449,12 @@ ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
4449 struct usb_ov511 *ov = video_get_drvdata(vdev); 4449 struct usb_ov511 *ov = video_get_drvdata(vdev);
4450 int rc; 4450 int rc;
4451 4451
4452 if (down_interruptible(&ov->lock)) 4452 if (mutex_lock_interruptible(&ov->lock))
4453 return -EINTR; 4453 return -EINTR;
4454 4454
4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal); 4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
4456 4456
4457 up(&ov->lock); 4457 mutex_unlock(&ov->lock);
4458 return rc; 4458 return rc;
4459} 4459}
4460 4460
@@ -4468,7 +4468,7 @@ ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4468 int i, rc = 0, frmx = -1; 4468 int i, rc = 0, frmx = -1;
4469 struct ov511_frame *frame; 4469 struct ov511_frame *frame;
4470 4470
4471 if (down_interruptible(&ov->lock)) 4471 if (mutex_lock_interruptible(&ov->lock))
4472 return -EINTR; 4472 return -EINTR;
4473 4473
4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock); 4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
@@ -4604,11 +4604,11 @@ restart:
4604 4604
4605 PDEBUG(4, "read finished, returning %ld (sweet)", count); 4605 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4606 4606
4607 up(&ov->lock); 4607 mutex_unlock(&ov->lock);
4608 return count; 4608 return count;
4609 4609
4610error: 4610error:
4611 up(&ov->lock); 4611 mutex_unlock(&ov->lock);
4612 return rc; 4612 return rc;
4613} 4613}
4614 4614
@@ -4631,14 +4631,14 @@ ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))) 4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4632 return -EINVAL; 4632 return -EINVAL;
4633 4633
4634 if (down_interruptible(&ov->lock)) 4634 if (mutex_lock_interruptible(&ov->lock))
4635 return -EINTR; 4635 return -EINTR;
4636 4636
4637 pos = (unsigned long)ov->fbuf; 4637 pos = (unsigned long)ov->fbuf;
4638 while (size > 0) { 4638 while (size > 0) {
4639 page = vmalloc_to_pfn((void *)pos); 4639 page = vmalloc_to_pfn((void *)pos);
4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4641 up(&ov->lock); 4641 mutex_unlock(&ov->lock);
4642 return -EAGAIN; 4642 return -EAGAIN;
4643 } 4643 }
4644 start += PAGE_SIZE; 4644 start += PAGE_SIZE;
@@ -4649,7 +4649,7 @@ ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4649 size = 0; 4649 size = 0;
4650 } 4650 }
4651 4651
4652 up(&ov->lock); 4652 mutex_unlock(&ov->lock);
4653 return 0; 4653 return 0;
4654} 4654}
4655 4655
@@ -5639,7 +5639,7 @@ static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5639static ssize_t show_exposure(struct class_device *cd, char *buf) 5639static ssize_t show_exposure(struct class_device *cd, char *buf)
5640{ 5640{
5641 struct usb_ov511 *ov = cd_to_ov(cd); 5641 struct usb_ov511 *ov = cd_to_ov(cd);
5642 unsigned char exp; 5642 unsigned char exp = 0;
5643 5643
5644 if (!ov->dev) 5644 if (!ov->dev)
5645 return -ENODEV; 5645 return -ENODEV;
@@ -5686,13 +5686,11 @@ ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5686 if (idesc->bInterfaceSubClass != 0x00) 5686 if (idesc->bInterfaceSubClass != 0x00)
5687 return -ENODEV; 5687 return -ENODEV;
5688 5688
5689 if ((ov = kmalloc(sizeof(*ov), GFP_KERNEL)) == NULL) { 5689 if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5690 err("couldn't kmalloc ov struct"); 5690 err("couldn't kmalloc ov struct");
5691 goto error_out; 5691 goto error_out;
5692 } 5692 }
5693 5693
5694 memset(ov, 0, sizeof(*ov));
5695
5696 ov->dev = dev; 5694 ov->dev = dev;
5697 ov->iface = idesc->bInterfaceNumber; 5695 ov->iface = idesc->bInterfaceNumber;
5698 ov->led_policy = led; 5696 ov->led_policy = led;
@@ -5738,11 +5736,10 @@ ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5738 5736
5739 init_waitqueue_head(&ov->wq); 5737 init_waitqueue_head(&ov->wq);
5740 5738
5741 init_MUTEX(&ov->lock); /* to 1 == available */ 5739 mutex_init(&ov->lock); /* to 1 == available */
5742 init_MUTEX(&ov->buf_lock); 5740 mutex_init(&ov->buf_lock);
5743 init_MUTEX(&ov->param_lock); 5741 mutex_init(&ov->i2c_lock);
5744 init_MUTEX(&ov->i2c_lock); 5742 mutex_init(&ov->cbuf_lock);
5745 init_MUTEX(&ov->cbuf_lock);
5746 5743
5747 ov->buf_state = BUF_NOT_ALLOCATED; 5744 ov->buf_state = BUF_NOT_ALLOCATED;
5748 5745
@@ -5833,10 +5830,10 @@ error:
5833 } 5830 }
5834 5831
5835 if (ov->cbuf) { 5832 if (ov->cbuf) {
5836 down(&ov->cbuf_lock); 5833 mutex_lock(&ov->cbuf_lock);
5837 kfree(ov->cbuf); 5834 kfree(ov->cbuf);
5838 ov->cbuf = NULL; 5835 ov->cbuf = NULL;
5839 up(&ov->cbuf_lock); 5836 mutex_unlock(&ov->cbuf_lock);
5840 } 5837 }
5841 5838
5842 kfree(ov); 5839 kfree(ov);
@@ -5881,10 +5878,10 @@ ov51x_disconnect(struct usb_interface *intf)
5881 5878
5882 /* Free the memory */ 5879 /* Free the memory */
5883 if (ov && !ov->user) { 5880 if (ov && !ov->user) {
5884 down(&ov->cbuf_lock); 5881 mutex_lock(&ov->cbuf_lock);
5885 kfree(ov->cbuf); 5882 kfree(ov->cbuf);
5886 ov->cbuf = NULL; 5883 ov->cbuf = NULL;
5887 up(&ov->cbuf_lock); 5884 mutex_unlock(&ov->cbuf_lock);
5888 5885
5889 ov51x_dealloc(ov); 5886 ov51x_dealloc(ov);
5890 kfree(ov); 5887 kfree(ov);
diff --git a/drivers/usb/media/ov511.h b/drivers/usb/media/ov511.h
index 086509a137c6..bce9b3633889 100644
--- a/drivers/usb/media/ov511.h
+++ b/drivers/usb/media/ov511.h
@@ -5,6 +5,7 @@
5#include <linux/videodev.h> 5#include <linux/videodev.h>
6#include <linux/smp_lock.h> 6#include <linux/smp_lock.h>
7#include <linux/usb.h> 7#include <linux/usb.h>
8#include <linux/mutex.h>
8 9
9#define OV511_DEBUG /* Turn on debug messages */ 10#define OV511_DEBUG /* Turn on debug messages */
10 11
@@ -435,7 +436,7 @@ struct usb_ov511 {
435 436
436 int led_policy; /* LED: off|on|auto; OV511+ only */ 437 int led_policy; /* LED: off|on|auto; OV511+ only */
437 438
438 struct semaphore lock; /* Serializes user-accessible operations */ 439 struct mutex lock; /* Serializes user-accessible operations */
439 int user; /* user count for exclusive use */ 440 int user; /* user count for exclusive use */
440 441
441 int streaming; /* Are we streaming Isochronous? */ 442 int streaming; /* Are we streaming Isochronous? */
@@ -473,11 +474,9 @@ struct usb_ov511 {
473 int packet_size; /* Frame size per isoc desc */ 474 int packet_size; /* Frame size per isoc desc */
474 int packet_numbering; /* Is ISO frame numbering enabled? */ 475 int packet_numbering; /* Is ISO frame numbering enabled? */
475 476
476 struct semaphore param_lock; /* params lock for this camera */
477
478 /* Framebuffer/sbuf management */ 477 /* Framebuffer/sbuf management */
479 int buf_state; 478 int buf_state;
480 struct semaphore buf_lock; 479 struct mutex buf_lock;
481 480
482 struct ov51x_decomp_ops *decomp_ops; 481 struct ov51x_decomp_ops *decomp_ops;
483 482
@@ -494,12 +493,12 @@ struct usb_ov511 {
494 int pal; /* Device is designed for PAL resolution */ 493 int pal; /* Device is designed for PAL resolution */
495 494
496 /* I2C interface */ 495 /* I2C interface */
497 struct semaphore i2c_lock; /* Protect I2C controller regs */ 496 struct mutex i2c_lock; /* Protect I2C controller regs */
498 unsigned char primary_i2c_slave; /* I2C write id of sensor */ 497 unsigned char primary_i2c_slave; /* I2C write id of sensor */
499 498
500 /* Control transaction stuff */ 499 /* Control transaction stuff */
501 unsigned char *cbuf; /* Buffer for payload */ 500 unsigned char *cbuf; /* Buffer for payload */
502 struct semaphore cbuf_lock; 501 struct mutex cbuf_lock;
503}; 502};
504 503
505/* Used to represent a list of values and their respective symbolic names */ 504/* Used to represent a list of values and their respective symbolic names */
diff --git a/drivers/usb/media/pwc/pwc-ctrl.c b/drivers/usb/media/pwc/pwc-ctrl.c
index 3ebb6e9cdf92..0398b812e0ce 100644
--- a/drivers/usb/media/pwc/pwc-ctrl.c
+++ b/drivers/usb/media/pwc/pwc-ctrl.c
@@ -41,7 +41,6 @@
41#include <asm/uaccess.h> 41#include <asm/uaccess.h>
42#endif 42#endif
43#include <asm/errno.h> 43#include <asm/errno.h>
44#include <linux/version.h>
45 44
46#include "pwc.h" 45#include "pwc.h"
47#include "pwc-ioctl.h" 46#include "pwc-ioctl.h"
diff --git a/drivers/usb/media/pwc/pwc-if.c b/drivers/usb/media/pwc/pwc-if.c
index 4f9b0dc6fd7b..90eb26042817 100644
--- a/drivers/usb/media/pwc/pwc-if.c
+++ b/drivers/usb/media/pwc/pwc-if.c
@@ -62,7 +62,6 @@
62#include <linux/poll.h> 62#include <linux/poll.h>
63#include <linux/slab.h> 63#include <linux/slab.h>
64#include <linux/vmalloc.h> 64#include <linux/vmalloc.h>
65#include <linux/version.h>
66#include <asm/io.h> 65#include <asm/io.h>
67 66
68#include "pwc.h" 67#include "pwc.h"
@@ -827,13 +826,10 @@ static int pwc_isoc_init(struct pwc_device *pdev)
827 /* Get the current alternate interface, adjust packet size */ 826 /* Get the current alternate interface, adjust packet size */
828 if (!udev->actconfig) 827 if (!udev->actconfig)
829 return -EFAULT; 828 return -EFAULT;
830#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5) 829
831 idesc = &udev->actconfig->interface[0]->altsetting[pdev->valternate];
832#else
833 intf = usb_ifnum_to_if(udev, 0); 830 intf = usb_ifnum_to_if(udev, 0);
834 if (intf) 831 if (intf)
835 idesc = usb_altnum_to_altsetting(intf, pdev->valternate); 832 idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
836#endif
837 833
838 if (!idesc) 834 if (!idesc)
839 return -EFAULT; 835 return -EFAULT;
@@ -1871,12 +1867,11 @@ static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id
1871 Info("Warning: more than 1 configuration available.\n"); 1867 Info("Warning: more than 1 configuration available.\n");
1872 1868
1873 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */ 1869 /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
1874 pdev = kmalloc(sizeof(struct pwc_device), GFP_KERNEL); 1870 pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
1875 if (pdev == NULL) { 1871 if (pdev == NULL) {
1876 Err("Oops, could not allocate memory for pwc_device.\n"); 1872 Err("Oops, could not allocate memory for pwc_device.\n");
1877 return -ENOMEM; 1873 return -ENOMEM;
1878 } 1874 }
1879 memset(pdev, 0, sizeof(struct pwc_device));
1880 pdev->type = type_id; 1875 pdev->type = type_id;
1881 pdev->vsize = default_size; 1876 pdev->vsize = default_size;
1882 pdev->vframes = default_fps; 1877 pdev->vframes = default_fps;
diff --git a/drivers/usb/media/se401.c b/drivers/usb/media/se401.c
index 2ba562285fda..f03ea7f89596 100644
--- a/drivers/usb/media/se401.c
+++ b/drivers/usb/media/se401.c
@@ -1157,21 +1157,21 @@ static int se401_mmap(struct file *file, struct vm_area_struct *vma)
1157 unsigned long size = vma->vm_end-vma->vm_start; 1157 unsigned long size = vma->vm_end-vma->vm_start;
1158 unsigned long page, pos; 1158 unsigned long page, pos;
1159 1159
1160 down(&se401->lock); 1160 mutex_lock(&se401->lock);
1161 1161
1162 if (se401->dev == NULL) { 1162 if (se401->dev == NULL) {
1163 up(&se401->lock); 1163 mutex_unlock(&se401->lock);
1164 return -EIO; 1164 return -EIO;
1165 } 1165 }
1166 if (size > (((SE401_NUMFRAMES * se401->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) { 1166 if (size > (((SE401_NUMFRAMES * se401->maxframesize) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) {
1167 up(&se401->lock); 1167 mutex_unlock(&se401->lock);
1168 return -EINVAL; 1168 return -EINVAL;
1169 } 1169 }
1170 pos = (unsigned long)se401->fbuf; 1170 pos = (unsigned long)se401->fbuf;
1171 while (size > 0) { 1171 while (size > 0) {
1172 page = vmalloc_to_pfn((void *)pos); 1172 page = vmalloc_to_pfn((void *)pos);
1173 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 1173 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1174 up(&se401->lock); 1174 mutex_unlock(&se401->lock);
1175 return -EAGAIN; 1175 return -EAGAIN;
1176 } 1176 }
1177 start += PAGE_SIZE; 1177 start += PAGE_SIZE;
@@ -1181,7 +1181,7 @@ static int se401_mmap(struct file *file, struct vm_area_struct *vma)
1181 else 1181 else
1182 size = 0; 1182 size = 0;
1183 } 1183 }
1184 up(&se401->lock); 1184 mutex_unlock(&se401->lock);
1185 1185
1186 return 0; 1186 return 0;
1187} 1187}
@@ -1345,13 +1345,11 @@ static int se401_probe(struct usb_interface *intf,
1345 /* We found one */ 1345 /* We found one */
1346 info("SE401 camera found: %s", camera_name); 1346 info("SE401 camera found: %s", camera_name);
1347 1347
1348 if ((se401 = kmalloc(sizeof(*se401), GFP_KERNEL)) == NULL) { 1348 if ((se401 = kzalloc(sizeof(*se401), GFP_KERNEL)) == NULL) {
1349 err("couldn't kmalloc se401 struct"); 1349 err("couldn't kmalloc se401 struct");
1350 return -ENOMEM; 1350 return -ENOMEM;
1351 } 1351 }
1352 1352
1353 memset(se401, 0, sizeof(*se401));
1354
1355 se401->dev = dev; 1353 se401->dev = dev;
1356 se401->iface = interface->bInterfaceNumber; 1354 se401->iface = interface->bInterfaceNumber;
1357 se401->camera_name = camera_name; 1355 se401->camera_name = camera_name;
@@ -1366,7 +1364,7 @@ static int se401_probe(struct usb_interface *intf,
1366 memcpy(&se401->vdev, &se401_template, sizeof(se401_template)); 1364 memcpy(&se401->vdev, &se401_template, sizeof(se401_template));
1367 memcpy(se401->vdev.name, se401->camera_name, strlen(se401->camera_name)); 1365 memcpy(se401->vdev.name, se401->camera_name, strlen(se401->camera_name));
1368 init_waitqueue_head(&se401->wq); 1366 init_waitqueue_head(&se401->wq);
1369 init_MUTEX(&se401->lock); 1367 mutex_init(&se401->lock);
1370 wmb(); 1368 wmb();
1371 1369
1372 if (video_register_device(&se401->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { 1370 if (video_register_device(&se401->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
diff --git a/drivers/usb/media/se401.h b/drivers/usb/media/se401.h
index 2e5846f1eb20..e88a40d4c86a 100644
--- a/drivers/usb/media/se401.h
+++ b/drivers/usb/media/se401.h
@@ -5,6 +5,7 @@
5#include <asm/uaccess.h> 5#include <asm/uaccess.h>
6#include <linux/videodev.h> 6#include <linux/videodev.h>
7#include <linux/smp_lock.h> 7#include <linux/smp_lock.h>
8#include <linux/mutex.h>
8 9
9#define se401_DEBUG /* Turn on debug messages */ 10#define se401_DEBUG /* Turn on debug messages */
10 11
@@ -189,7 +190,7 @@ struct usb_se401 {
189 int maxframesize; 190 int maxframesize;
190 int cframesize; /* current framesize */ 191 int cframesize; /* current framesize */
191 192
192 struct semaphore lock; 193 struct mutex lock;
193 int user; /* user count for exclusive use */ 194 int user; /* user count for exclusive use */
194 int removed; /* device disconnected */ 195 int removed; /* device disconnected */
195 196
diff --git a/drivers/usb/media/sn9c102.h b/drivers/usb/media/sn9c102.h
index 17d60c1eea7e..1d70a62b9f23 100644
--- a/drivers/usb/media/sn9c102.h
+++ b/drivers/usb/media/sn9c102.h
@@ -33,7 +33,9 @@
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/param.h> 34#include <linux/param.h>
35#include <linux/rwsem.h> 35#include <linux/rwsem.h>
36#include <asm/semaphore.h> 36#include <linux/mutex.h>
37#include <linux/string.h>
38#include <linux/stddef.h>
37 39
38#include "sn9c102_sensor.h" 40#include "sn9c102_sensor.h"
39 41
@@ -50,6 +52,7 @@
50#define SN9C102_ALTERNATE_SETTING 8 52#define SN9C102_ALTERNATE_SETTING 8
51#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS) 53#define SN9C102_URB_TIMEOUT msecs_to_jiffies(2 * SN9C102_ISO_PACKETS)
52#define SN9C102_CTRL_TIMEOUT 300 54#define SN9C102_CTRL_TIMEOUT 300
55#define SN9C102_FRAME_TIMEOUT 2
53 56
54/*****************************************************************************/ 57/*****************************************************************************/
55 58
@@ -107,16 +110,17 @@ struct sn9c102_sysfs_attr {
107 110
108struct sn9c102_module_param { 111struct sn9c102_module_param {
109 u8 force_munmap; 112 u8 force_munmap;
113 u16 frame_timeout;
110}; 114};
111 115
112static DECLARE_MUTEX(sn9c102_sysfs_lock); 116static DEFINE_MUTEX(sn9c102_sysfs_lock);
113static DECLARE_RWSEM(sn9c102_disconnect); 117static DECLARE_RWSEM(sn9c102_disconnect);
114 118
115struct sn9c102_device { 119struct sn9c102_device {
116 struct video_device* v4ldev; 120 struct video_device* v4ldev;
117 121
118 enum sn9c102_bridge bridge; 122 enum sn9c102_bridge bridge;
119 struct sn9c102_sensor* sensor; 123 struct sn9c102_sensor sensor;
120 124
121 struct usb_device* usbdev; 125 struct usb_device* usbdev;
122 struct urb* urb[SN9C102_URBS]; 126 struct urb* urb[SN9C102_URBS];
@@ -141,19 +145,28 @@ struct sn9c102_device {
141 enum sn9c102_dev_state state; 145 enum sn9c102_dev_state state;
142 u8 users; 146 u8 users;
143 147
144 struct semaphore dev_sem, fileop_sem; 148 struct mutex dev_mutex, fileop_mutex;
145 spinlock_t queue_lock; 149 spinlock_t queue_lock;
146 wait_queue_head_t open, wait_frame, wait_stream; 150 wait_queue_head_t open, wait_frame, wait_stream;
147}; 151};
148 152
149/*****************************************************************************/ 153/*****************************************************************************/
150 154
155struct sn9c102_device*
156sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id)
157{
158 if (usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id))
159 return cam;
160
161 return NULL;
162}
163
164
151void 165void
152sn9c102_attach_sensor(struct sn9c102_device* cam, 166sn9c102_attach_sensor(struct sn9c102_device* cam,
153 struct sn9c102_sensor* sensor) 167 struct sn9c102_sensor* sensor)
154{ 168{
155 cam->sensor = sensor; 169 memcpy(&cam->sensor, sensor, sizeof(struct sn9c102_sensor));
156 cam->sensor->usbdev = cam->usbdev;
157} 170}
158 171
159/*****************************************************************************/ 172/*****************************************************************************/
@@ -196,7 +209,8 @@ do { \
196 209
197#undef PDBG 210#undef PDBG
198#define PDBG(fmt, args...) \ 211#define PDBG(fmt, args...) \
199dev_info(&cam->dev, "[%s:%d] " fmt "\n", __FUNCTION__, __LINE__ , ## args) 212dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
213 __FUNCTION__, __LINE__ , ## args)
200 214
201#undef PDBGG 215#undef PDBGG
202#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */ 216#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
diff --git a/drivers/usb/media/sn9c102_core.c b/drivers/usb/media/sn9c102_core.c
index c81397e4714b..4c6cc6395723 100644
--- a/drivers/usb/media/sn9c102_core.c
+++ b/drivers/usb/media/sn9c102_core.c
@@ -25,11 +25,9 @@
25#include <linux/moduleparam.h> 25#include <linux/moduleparam.h>
26#include <linux/errno.h> 26#include <linux/errno.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/device.h> 28#include <linux/device.h>
30#include <linux/fs.h> 29#include <linux/fs.h>
31#include <linux/delay.h> 30#include <linux/delay.h>
32#include <linux/stddef.h>
33#include <linux/compiler.h> 31#include <linux/compiler.h>
34#include <linux/ioctl.h> 32#include <linux/ioctl.h>
35#include <linux/poll.h> 33#include <linux/poll.h>
@@ -49,8 +47,8 @@
49#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia" 47#define SN9C102_MODULE_AUTHOR "(C) 2004-2006 Luca Risolia"
50#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>" 48#define SN9C102_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
51#define SN9C102_MODULE_LICENSE "GPL" 49#define SN9C102_MODULE_LICENSE "GPL"
52#define SN9C102_MODULE_VERSION "1:1.26" 50#define SN9C102_MODULE_VERSION "1:1.27"
53#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 26) 51#define SN9C102_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 27)
54 52
55/*****************************************************************************/ 53/*****************************************************************************/
56 54
@@ -89,6 +87,15 @@ MODULE_PARM_DESC(force_munmap,
89 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"." 87 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
90 "\n"); 88 "\n");
91 89
90static unsigned int frame_timeout[] = {[0 ... SN9C102_MAX_DEVICES-1] =
91 SN9C102_FRAME_TIMEOUT};
92module_param_array(frame_timeout, uint, NULL, 0644);
93MODULE_PARM_DESC(frame_timeout,
94 "\n<n[,...]> Timeout for a video frame in seconds."
95 "\nThis parameter is specific for each detected camera."
96 "\nDefault value is "__MODULE_STRING(SN9C102_FRAME_TIMEOUT)"."
97 "\n");
98
92#ifdef SN9C102_DEBUG 99#ifdef SN9C102_DEBUG
93static unsigned short debug = SN9C102_DEBUG_LEVEL; 100static unsigned short debug = SN9C102_DEBUG_LEVEL;
94module_param(debug, ushort, 0644); 101module_param(debug, ushort, 0644);
@@ -128,8 +135,8 @@ static u32
128sn9c102_request_buffers(struct sn9c102_device* cam, u32 count, 135sn9c102_request_buffers(struct sn9c102_device* cam, u32 count,
129 enum sn9c102_io_method io) 136 enum sn9c102_io_method io)
130{ 137{
131 struct v4l2_pix_format* p = &(cam->sensor->pix_format); 138 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
132 struct v4l2_rect* r = &(cam->sensor->cropcap.bounds); 139 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
133 const size_t imagesize = cam->module_param.force_munmap || 140 const size_t imagesize = cam->module_param.force_munmap ||
134 io == IO_READ ? 141 io == IO_READ ?
135 (p->width * p->height * p->priv) / 8 : 142 (p->width * p->height * p->priv) / 8 :
@@ -449,19 +456,13 @@ sn9c102_i2c_try_write(struct sn9c102_device* cam,
449 456
450int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address) 457int sn9c102_i2c_read(struct sn9c102_device* cam, u8 address)
451{ 458{
452 if (!cam->sensor) 459 return sn9c102_i2c_try_read(cam, &cam->sensor, address);
453 return -1;
454
455 return sn9c102_i2c_try_read(cam, cam->sensor, address);
456} 460}
457 461
458 462
459int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value) 463int sn9c102_i2c_write(struct sn9c102_device* cam, u8 address, u8 value)
460{ 464{
461 if (!cam->sensor) 465 return sn9c102_i2c_try_write(cam, &cam->sensor, address, value);
462 return -1;
463
464 return sn9c102_i2c_try_write(cam, cam->sensor, address, value);
465} 466}
466 467
467/*****************************************************************************/ 468/*****************************************************************************/
@@ -505,7 +506,7 @@ sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len)
505 size_t eoflen = sizeof(sn9c102_eof_header_t), i; 506 size_t eoflen = sizeof(sn9c102_eof_header_t), i;
506 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen; 507 unsigned j, n = sizeof(sn9c102_eof_header) / eoflen;
507 508
508 if (cam->sensor->pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X) 509 if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X)
509 return NULL; /* EOF header does not exist in compressed data */ 510 return NULL; /* EOF header does not exist in compressed data */
510 511
511 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++) 512 for (i = 0; (len >= eoflen) && (i <= len - eoflen); i++)
@@ -535,7 +536,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
535 if ((*f)) 536 if ((*f))
536 (*f)->state = F_QUEUED; 537 (*f)->state = F_QUEUED;
537 DBG(3, "Stream interrupted"); 538 DBG(3, "Stream interrupted");
538 wake_up_interruptible(&cam->wait_stream); 539 wake_up(&cam->wait_stream);
539 } 540 }
540 541
541 if (cam->state & DEV_DISCONNECTED) 542 if (cam->state & DEV_DISCONNECTED)
@@ -553,9 +554,9 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
553 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t, 554 (*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,
554 frame); 555 frame);
555 556
556 imagesize = (cam->sensor->pix_format.width * 557 imagesize = (cam->sensor.pix_format.width *
557 cam->sensor->pix_format.height * 558 cam->sensor.pix_format.height *
558 cam->sensor->pix_format.priv) / 8; 559 cam->sensor.pix_format.priv) / 8;
559 560
560 soflen = (cam->bridge) == BRIDGE_SN9C103 ? 561 soflen = (cam->bridge) == BRIDGE_SN9C103 ?
561 sizeof(sn9c103_sof_header_t) : 562 sizeof(sn9c103_sof_header_t) :
@@ -579,7 +580,7 @@ static void sn9c102_urb_complete(struct urb *urb, struct pt_regs* regs)
579 580
580redo: 581redo:
581 sof = sn9c102_find_sof_header(cam, pos, len); 582 sof = sn9c102_find_sof_header(cam, pos, len);
582 if (!sof) { 583 if (likely(!sof)) {
583 eof = sn9c102_find_eof_header(cam, pos, len); 584 eof = sn9c102_find_eof_header(cam, pos, len);
584 if ((*f)->state == F_GRABBING) { 585 if ((*f)->state == F_GRABBING) {
585end_of_frame: 586end_of_frame:
@@ -589,8 +590,9 @@ end_of_frame:
589 img = (eof > pos) ? eof - pos - 1 : 0; 590 img = (eof > pos) ? eof - pos - 1 : 0;
590 591
591 if ((*f)->buf.bytesused+img > imagesize) { 592 if ((*f)->buf.bytesused+img > imagesize) {
592 u32 b = (*f)->buf.bytesused + img - 593 u32 b;
593 imagesize; 594 b = (*f)->buf.bytesused + img -
595 imagesize;
594 img = imagesize - (*f)->buf.bytesused; 596 img = imagesize - (*f)->buf.bytesused;
595 DBG(3, "Expected EOF not found: " 597 DBG(3, "Expected EOF not found: "
596 "video frame cut"); 598 "video frame cut");
@@ -608,9 +610,10 @@ end_of_frame:
608 (*f)->buf.bytesused += img; 610 (*f)->buf.bytesused += img;
609 611
610 if ((*f)->buf.bytesused == imagesize || 612 if ((*f)->buf.bytesused == imagesize ||
611 (cam->sensor->pix_format.pixelformat == 613 (cam->sensor.pix_format.pixelformat ==
612 V4L2_PIX_FMT_SN9C10X && eof)) { 614 V4L2_PIX_FMT_SN9C10X && eof)) {
613 u32 b = (*f)->buf.bytesused; 615 u32 b;
616 b = (*f)->buf.bytesused;
614 (*f)->state = F_DONE; 617 (*f)->state = F_DONE;
615 (*f)->buf.sequence= ++cam->frame_count; 618 (*f)->buf.sequence= ++cam->frame_count;
616 spin_lock(&cam->queue_lock); 619 spin_lock(&cam->queue_lock);
@@ -667,7 +670,7 @@ start_of_frame:
667 if (eof && eof < sof) 670 if (eof && eof < sof)
668 goto end_of_frame; /* (1) */ 671 goto end_of_frame; /* (1) */
669 else { 672 else {
670 if (cam->sensor->pix_format.pixelformat == 673 if (cam->sensor.pix_format.pixelformat ==
671 V4L2_PIX_FMT_SN9C10X) { 674 V4L2_PIX_FMT_SN9C10X) {
672 eof = sof - soflen; 675 eof = sof - soflen;
673 goto end_of_frame; 676 goto end_of_frame;
@@ -808,20 +811,21 @@ static int sn9c102_stop_transfer(struct sn9c102_device* cam)
808 811
809static int sn9c102_stream_interrupt(struct sn9c102_device* cam) 812static int sn9c102_stream_interrupt(struct sn9c102_device* cam)
810{ 813{
811 int err = 0; 814 long timeout;
812 815
813 cam->stream = STREAM_INTERRUPT; 816 cam->stream = STREAM_INTERRUPT;
814 err = wait_event_timeout(cam->wait_stream, 817 timeout = wait_event_timeout(cam->wait_stream,
815 (cam->stream == STREAM_OFF) || 818 (cam->stream == STREAM_OFF) ||
816 (cam->state & DEV_DISCONNECTED), 819 (cam->state & DEV_DISCONNECTED),
817 SN9C102_URB_TIMEOUT); 820 SN9C102_URB_TIMEOUT);
818 if (cam->state & DEV_DISCONNECTED) 821 if (cam->state & DEV_DISCONNECTED)
819 return -ENODEV; 822 return -ENODEV;
820 else if (err) { 823 else if (cam->stream != STREAM_OFF) {
821 cam->state |= DEV_MISCONFIGURED; 824 cam->state |= DEV_MISCONFIGURED;
822 DBG(1, "The camera is misconfigured. To use it, close and " 825 DBG(1, "URB timeout reached. The camera is misconfigured. "
823 "open /dev/video%d again.", cam->v4ldev->minor); 826 "To use it, close and open /dev/video%d again.",
824 return err; 827 cam->v4ldev->minor);
828 return -EIO;
825 } 829 }
826 830
827 return 0; 831 return 0;
@@ -866,18 +870,18 @@ static ssize_t sn9c102_show_reg(struct class_device* cd, char* buf)
866 struct sn9c102_device* cam; 870 struct sn9c102_device* cam;
867 ssize_t count; 871 ssize_t count;
868 872
869 if (down_interruptible(&sn9c102_sysfs_lock)) 873 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
870 return -ERESTARTSYS; 874 return -ERESTARTSYS;
871 875
872 cam = video_get_drvdata(to_video_device(cd)); 876 cam = video_get_drvdata(to_video_device(cd));
873 if (!cam) { 877 if (!cam) {
874 up(&sn9c102_sysfs_lock); 878 mutex_unlock(&sn9c102_sysfs_lock);
875 return -ENODEV; 879 return -ENODEV;
876 } 880 }
877 881
878 count = sprintf(buf, "%u\n", cam->sysfs.reg); 882 count = sprintf(buf, "%u\n", cam->sysfs.reg);
879 883
880 up(&sn9c102_sysfs_lock); 884 mutex_unlock(&sn9c102_sysfs_lock);
881 885
882 return count; 886 return count;
883} 887}
@@ -890,18 +894,18 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
890 u8 index; 894 u8 index;
891 ssize_t count; 895 ssize_t count;
892 896
893 if (down_interruptible(&sn9c102_sysfs_lock)) 897 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
894 return -ERESTARTSYS; 898 return -ERESTARTSYS;
895 899
896 cam = video_get_drvdata(to_video_device(cd)); 900 cam = video_get_drvdata(to_video_device(cd));
897 if (!cam) { 901 if (!cam) {
898 up(&sn9c102_sysfs_lock); 902 mutex_unlock(&sn9c102_sysfs_lock);
899 return -ENODEV; 903 return -ENODEV;
900 } 904 }
901 905
902 index = sn9c102_strtou8(buf, len, &count); 906 index = sn9c102_strtou8(buf, len, &count);
903 if (index > 0x1f || !count) { 907 if (index > 0x1f || !count) {
904 up(&sn9c102_sysfs_lock); 908 mutex_unlock(&sn9c102_sysfs_lock);
905 return -EINVAL; 909 return -EINVAL;
906 } 910 }
907 911
@@ -910,7 +914,7 @@ sn9c102_store_reg(struct class_device* cd, const char* buf, size_t len)
910 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg); 914 DBG(2, "Moved SN9C10X register index to 0x%02X", cam->sysfs.reg);
911 DBG(3, "Written bytes: %zd", count); 915 DBG(3, "Written bytes: %zd", count);
912 916
913 up(&sn9c102_sysfs_lock); 917 mutex_unlock(&sn9c102_sysfs_lock);
914 918
915 return count; 919 return count;
916} 920}
@@ -922,17 +926,17 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
922 ssize_t count; 926 ssize_t count;
923 int val; 927 int val;
924 928
925 if (down_interruptible(&sn9c102_sysfs_lock)) 929 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
926 return -ERESTARTSYS; 930 return -ERESTARTSYS;
927 931
928 cam = video_get_drvdata(to_video_device(cd)); 932 cam = video_get_drvdata(to_video_device(cd));
929 if (!cam) { 933 if (!cam) {
930 up(&sn9c102_sysfs_lock); 934 mutex_unlock(&sn9c102_sysfs_lock);
931 return -ENODEV; 935 return -ENODEV;
932 } 936 }
933 937
934 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) { 938 if ((val = sn9c102_read_reg(cam, cam->sysfs.reg)) < 0) {
935 up(&sn9c102_sysfs_lock); 939 mutex_unlock(&sn9c102_sysfs_lock);
936 return -EIO; 940 return -EIO;
937 } 941 }
938 942
@@ -940,7 +944,7 @@ static ssize_t sn9c102_show_val(struct class_device* cd, char* buf)
940 944
941 DBG(3, "Read bytes: %zd", count); 945 DBG(3, "Read bytes: %zd", count);
942 946
943 up(&sn9c102_sysfs_lock); 947 mutex_unlock(&sn9c102_sysfs_lock);
944 948
945 return count; 949 return count;
946} 950}
@@ -954,24 +958,24 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
954 ssize_t count; 958 ssize_t count;
955 int err; 959 int err;
956 960
957 if (down_interruptible(&sn9c102_sysfs_lock)) 961 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
958 return -ERESTARTSYS; 962 return -ERESTARTSYS;
959 963
960 cam = video_get_drvdata(to_video_device(cd)); 964 cam = video_get_drvdata(to_video_device(cd));
961 if (!cam) { 965 if (!cam) {
962 up(&sn9c102_sysfs_lock); 966 mutex_unlock(&sn9c102_sysfs_lock);
963 return -ENODEV; 967 return -ENODEV;
964 } 968 }
965 969
966 value = sn9c102_strtou8(buf, len, &count); 970 value = sn9c102_strtou8(buf, len, &count);
967 if (!count) { 971 if (!count) {
968 up(&sn9c102_sysfs_lock); 972 mutex_unlock(&sn9c102_sysfs_lock);
969 return -EINVAL; 973 return -EINVAL;
970 } 974 }
971 975
972 err = sn9c102_write_reg(cam, value, cam->sysfs.reg); 976 err = sn9c102_write_reg(cam, value, cam->sysfs.reg);
973 if (err) { 977 if (err) {
974 up(&sn9c102_sysfs_lock); 978 mutex_unlock(&sn9c102_sysfs_lock);
975 return -EIO; 979 return -EIO;
976 } 980 }
977 981
@@ -979,7 +983,7 @@ sn9c102_store_val(struct class_device* cd, const char* buf, size_t len)
979 cam->sysfs.reg, value); 983 cam->sysfs.reg, value);
980 DBG(3, "Written bytes: %zd", count); 984 DBG(3, "Written bytes: %zd", count);
981 985
982 up(&sn9c102_sysfs_lock); 986 mutex_unlock(&sn9c102_sysfs_lock);
983 987
984 return count; 988 return count;
985} 989}
@@ -990,12 +994,12 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
990 struct sn9c102_device* cam; 994 struct sn9c102_device* cam;
991 ssize_t count; 995 ssize_t count;
992 996
993 if (down_interruptible(&sn9c102_sysfs_lock)) 997 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
994 return -ERESTARTSYS; 998 return -ERESTARTSYS;
995 999
996 cam = video_get_drvdata(to_video_device(cd)); 1000 cam = video_get_drvdata(to_video_device(cd));
997 if (!cam) { 1001 if (!cam) {
998 up(&sn9c102_sysfs_lock); 1002 mutex_unlock(&sn9c102_sysfs_lock);
999 return -ENODEV; 1003 return -ENODEV;
1000 } 1004 }
1001 1005
@@ -1003,7 +1007,7 @@ static ssize_t sn9c102_show_i2c_reg(struct class_device* cd, char* buf)
1003 1007
1004 DBG(3, "Read bytes: %zd", count); 1008 DBG(3, "Read bytes: %zd", count);
1005 1009
1006 up(&sn9c102_sysfs_lock); 1010 mutex_unlock(&sn9c102_sysfs_lock);
1007 1011
1008 return count; 1012 return count;
1009} 1013}
@@ -1016,18 +1020,18 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1016 u8 index; 1020 u8 index;
1017 ssize_t count; 1021 ssize_t count;
1018 1022
1019 if (down_interruptible(&sn9c102_sysfs_lock)) 1023 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1020 return -ERESTARTSYS; 1024 return -ERESTARTSYS;
1021 1025
1022 cam = video_get_drvdata(to_video_device(cd)); 1026 cam = video_get_drvdata(to_video_device(cd));
1023 if (!cam) { 1027 if (!cam) {
1024 up(&sn9c102_sysfs_lock); 1028 mutex_unlock(&sn9c102_sysfs_lock);
1025 return -ENODEV; 1029 return -ENODEV;
1026 } 1030 }
1027 1031
1028 index = sn9c102_strtou8(buf, len, &count); 1032 index = sn9c102_strtou8(buf, len, &count);
1029 if (!count) { 1033 if (!count) {
1030 up(&sn9c102_sysfs_lock); 1034 mutex_unlock(&sn9c102_sysfs_lock);
1031 return -EINVAL; 1035 return -EINVAL;
1032 } 1036 }
1033 1037
@@ -1036,7 +1040,7 @@ sn9c102_store_i2c_reg(struct class_device* cd, const char* buf, size_t len)
1036 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg); 1040 DBG(2, "Moved sensor register index to 0x%02X", cam->sysfs.i2c_reg);
1037 DBG(3, "Written bytes: %zd", count); 1041 DBG(3, "Written bytes: %zd", count);
1038 1042
1039 up(&sn9c102_sysfs_lock); 1043 mutex_unlock(&sn9c102_sysfs_lock);
1040 1044
1041 return count; 1045 return count;
1042} 1046}
@@ -1048,22 +1052,22 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1048 ssize_t count; 1052 ssize_t count;
1049 int val; 1053 int val;
1050 1054
1051 if (down_interruptible(&sn9c102_sysfs_lock)) 1055 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1052 return -ERESTARTSYS; 1056 return -ERESTARTSYS;
1053 1057
1054 cam = video_get_drvdata(to_video_device(cd)); 1058 cam = video_get_drvdata(to_video_device(cd));
1055 if (!cam) { 1059 if (!cam) {
1056 up(&sn9c102_sysfs_lock); 1060 mutex_unlock(&sn9c102_sysfs_lock);
1057 return -ENODEV; 1061 return -ENODEV;
1058 } 1062 }
1059 1063
1060 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_READ)) { 1064 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_READ)) {
1061 up(&sn9c102_sysfs_lock); 1065 mutex_unlock(&sn9c102_sysfs_lock);
1062 return -ENOSYS; 1066 return -ENOSYS;
1063 } 1067 }
1064 1068
1065 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) { 1069 if ((val = sn9c102_i2c_read(cam, cam->sysfs.i2c_reg)) < 0) {
1066 up(&sn9c102_sysfs_lock); 1070 mutex_unlock(&sn9c102_sysfs_lock);
1067 return -EIO; 1071 return -EIO;
1068 } 1072 }
1069 1073
@@ -1071,7 +1075,7 @@ static ssize_t sn9c102_show_i2c_val(struct class_device* cd, char* buf)
1071 1075
1072 DBG(3, "Read bytes: %zd", count); 1076 DBG(3, "Read bytes: %zd", count);
1073 1077
1074 up(&sn9c102_sysfs_lock); 1078 mutex_unlock(&sn9c102_sysfs_lock);
1075 1079
1076 return count; 1080 return count;
1077} 1081}
@@ -1085,29 +1089,29 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1085 ssize_t count; 1089 ssize_t count;
1086 int err; 1090 int err;
1087 1091
1088 if (down_interruptible(&sn9c102_sysfs_lock)) 1092 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1089 return -ERESTARTSYS; 1093 return -ERESTARTSYS;
1090 1094
1091 cam = video_get_drvdata(to_video_device(cd)); 1095 cam = video_get_drvdata(to_video_device(cd));
1092 if (!cam) { 1096 if (!cam) {
1093 up(&sn9c102_sysfs_lock); 1097 mutex_unlock(&sn9c102_sysfs_lock);
1094 return -ENODEV; 1098 return -ENODEV;
1095 } 1099 }
1096 1100
1097 if (!(cam->sensor->sysfs_ops & SN9C102_I2C_WRITE)) { 1101 if (!(cam->sensor.sysfs_ops & SN9C102_I2C_WRITE)) {
1098 up(&sn9c102_sysfs_lock); 1102 mutex_unlock(&sn9c102_sysfs_lock);
1099 return -ENOSYS; 1103 return -ENOSYS;
1100 } 1104 }
1101 1105
1102 value = sn9c102_strtou8(buf, len, &count); 1106 value = sn9c102_strtou8(buf, len, &count);
1103 if (!count) { 1107 if (!count) {
1104 up(&sn9c102_sysfs_lock); 1108 mutex_unlock(&sn9c102_sysfs_lock);
1105 return -EINVAL; 1109 return -EINVAL;
1106 } 1110 }
1107 1111
1108 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value); 1112 err = sn9c102_i2c_write(cam, cam->sysfs.i2c_reg, value);
1109 if (err) { 1113 if (err) {
1110 up(&sn9c102_sysfs_lock); 1114 mutex_unlock(&sn9c102_sysfs_lock);
1111 return -EIO; 1115 return -EIO;
1112 } 1116 }
1113 1117
@@ -1115,7 +1119,7 @@ sn9c102_store_i2c_val(struct class_device* cd, const char* buf, size_t len)
1115 cam->sysfs.i2c_reg, value); 1119 cam->sysfs.i2c_reg, value);
1116 DBG(3, "Written bytes: %zd", count); 1120 DBG(3, "Written bytes: %zd", count);
1117 1121
1118 up(&sn9c102_sysfs_lock); 1122 mutex_unlock(&sn9c102_sysfs_lock);
1119 1123
1120 return count; 1124 return count;
1121} 1125}
@@ -1130,18 +1134,18 @@ sn9c102_store_green(struct class_device* cd, const char* buf, size_t len)
1130 u8 value; 1134 u8 value;
1131 ssize_t count; 1135 ssize_t count;
1132 1136
1133 if (down_interruptible(&sn9c102_sysfs_lock)) 1137 if (mutex_lock_interruptible(&sn9c102_sysfs_lock))
1134 return -ERESTARTSYS; 1138 return -ERESTARTSYS;
1135 1139
1136 cam = video_get_drvdata(to_video_device(cd)); 1140 cam = video_get_drvdata(to_video_device(cd));
1137 if (!cam) { 1141 if (!cam) {
1138 up(&sn9c102_sysfs_lock); 1142 mutex_unlock(&sn9c102_sysfs_lock);
1139 return -ENODEV; 1143 return -ENODEV;
1140 } 1144 }
1141 1145
1142 bridge = cam->bridge; 1146 bridge = cam->bridge;
1143 1147
1144 up(&sn9c102_sysfs_lock); 1148 mutex_unlock(&sn9c102_sysfs_lock);
1145 1149
1146 value = sn9c102_strtou8(buf, len, &count); 1150 value = sn9c102_strtou8(buf, len, &count);
1147 if (!count) 1151 if (!count)
@@ -1249,7 +1253,7 @@ static void sn9c102_create_sysfs(struct sn9c102_device* cam)
1249 video_device_create_file(v4ldev, &class_device_attr_blue); 1253 video_device_create_file(v4ldev, &class_device_attr_blue);
1250 video_device_create_file(v4ldev, &class_device_attr_red); 1254 video_device_create_file(v4ldev, &class_device_attr_red);
1251 } 1255 }
1252 if (cam->sensor && cam->sensor->sysfs_ops) { 1256 if (cam->sensor.sysfs_ops) {
1253 video_device_create_file(v4ldev, &class_device_attr_i2c_reg); 1257 video_device_create_file(v4ldev, &class_device_attr_i2c_reg);
1254 video_device_create_file(v4ldev, &class_device_attr_i2c_val); 1258 video_device_create_file(v4ldev, &class_device_attr_i2c_val);
1255 } 1259 }
@@ -1312,7 +1316,7 @@ static int sn9c102_set_scale(struct sn9c102_device* cam, u8 scale)
1312 1316
1313static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect) 1317static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1314{ 1318{
1315 struct sn9c102_sensor* s = cam->sensor; 1319 struct sn9c102_sensor* s = &cam->sensor;
1316 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left), 1320 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left),
1317 v_start = (u8)(rect->top - s->cropcap.bounds.top), 1321 v_start = (u8)(rect->top - s->cropcap.bounds.top),
1318 h_size = (u8)(rect->width / 16), 1322 h_size = (u8)(rect->width / 16),
@@ -1335,7 +1339,7 @@ static int sn9c102_set_crop(struct sn9c102_device* cam, struct v4l2_rect* rect)
1335 1339
1336static int sn9c102_init(struct sn9c102_device* cam) 1340static int sn9c102_init(struct sn9c102_device* cam)
1337{ 1341{
1338 struct sn9c102_sensor* s = cam->sensor; 1342 struct sn9c102_sensor* s = &cam->sensor;
1339 struct v4l2_control ctrl; 1343 struct v4l2_control ctrl;
1340 struct v4l2_queryctrl *qctrl; 1344 struct v4l2_queryctrl *qctrl;
1341 struct v4l2_rect* rect; 1345 struct v4l2_rect* rect;
@@ -1404,7 +1408,7 @@ static int sn9c102_init(struct sn9c102_device* cam)
1404 } 1408 }
1405 1409
1406 if (!(cam->state & DEV_INITIALIZED)) { 1410 if (!(cam->state & DEV_INITIALIZED)) {
1407 init_MUTEX(&cam->fileop_sem); 1411 mutex_init(&cam->fileop_mutex);
1408 spin_lock_init(&cam->queue_lock); 1412 spin_lock_init(&cam->queue_lock);
1409 init_waitqueue_head(&cam->wait_frame); 1413 init_waitqueue_head(&cam->wait_frame);
1410 init_waitqueue_head(&cam->wait_stream); 1414 init_waitqueue_head(&cam->wait_stream);
@@ -1422,13 +1426,15 @@ static int sn9c102_init(struct sn9c102_device* cam)
1422 1426
1423static void sn9c102_release_resources(struct sn9c102_device* cam) 1427static void sn9c102_release_resources(struct sn9c102_device* cam)
1424{ 1428{
1425 down(&sn9c102_sysfs_lock); 1429 mutex_lock(&sn9c102_sysfs_lock);
1426 1430
1427 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor); 1431 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
1428 video_set_drvdata(cam->v4ldev, NULL); 1432 video_set_drvdata(cam->v4ldev, NULL);
1429 video_unregister_device(cam->v4ldev); 1433 video_unregister_device(cam->v4ldev);
1430 1434
1431 up(&sn9c102_sysfs_lock); 1435 usb_put_dev(cam->usbdev);
1436
1437 mutex_unlock(&sn9c102_sysfs_lock);
1432 1438
1433 kfree(cam->control_buffer); 1439 kfree(cam->control_buffer);
1434} 1440}
@@ -1449,7 +1455,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1449 1455
1450 cam = video_get_drvdata(video_devdata(filp)); 1456 cam = video_get_drvdata(video_devdata(filp));
1451 1457
1452 if (down_interruptible(&cam->dev_sem)) { 1458 if (mutex_lock_interruptible(&cam->dev_mutex)) {
1453 up_read(&sn9c102_disconnect); 1459 up_read(&sn9c102_disconnect);
1454 return -ERESTARTSYS; 1460 return -ERESTARTSYS;
1455 } 1461 }
@@ -1461,7 +1467,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1461 err = -EWOULDBLOCK; 1467 err = -EWOULDBLOCK;
1462 goto out; 1468 goto out;
1463 } 1469 }
1464 up(&cam->dev_sem); 1470 mutex_unlock(&cam->dev_mutex);
1465 err = wait_event_interruptible_exclusive(cam->open, 1471 err = wait_event_interruptible_exclusive(cam->open,
1466 cam->state & DEV_DISCONNECTED 1472 cam->state & DEV_DISCONNECTED
1467 || !cam->users); 1473 || !cam->users);
@@ -1473,7 +1479,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1473 up_read(&sn9c102_disconnect); 1479 up_read(&sn9c102_disconnect);
1474 return -ENODEV; 1480 return -ENODEV;
1475 } 1481 }
1476 down(&cam->dev_sem); 1482 mutex_lock(&cam->dev_mutex);
1477 } 1483 }
1478 1484
1479 1485
@@ -1501,7 +1507,7 @@ static int sn9c102_open(struct inode* inode, struct file* filp)
1501 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor); 1507 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
1502 1508
1503out: 1509out:
1504 up(&cam->dev_sem); 1510 mutex_unlock(&cam->dev_mutex);
1505 up_read(&sn9c102_disconnect); 1511 up_read(&sn9c102_disconnect);
1506 return err; 1512 return err;
1507} 1513}
@@ -1511,7 +1517,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1511{ 1517{
1512 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1518 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1513 1519
1514 down(&cam->dev_sem); /* prevent disconnect() to be called */ 1520 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
1515 1521
1516 sn9c102_stop_transfer(cam); 1522 sn9c102_stop_transfer(cam);
1517 1523
@@ -1519,7 +1525,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1519 1525
1520 if (cam->state & DEV_DISCONNECTED) { 1526 if (cam->state & DEV_DISCONNECTED) {
1521 sn9c102_release_resources(cam); 1527 sn9c102_release_resources(cam);
1522 up(&cam->dev_sem); 1528 mutex_unlock(&cam->dev_mutex);
1523 kfree(cam); 1529 kfree(cam);
1524 return 0; 1530 return 0;
1525 } 1531 }
@@ -1529,7 +1535,7 @@ static int sn9c102_release(struct inode* inode, struct file* filp)
1529 1535
1530 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor); 1536 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
1531 1537
1532 up(&cam->dev_sem); 1538 mutex_unlock(&cam->dev_mutex);
1533 1539
1534 return 0; 1540 return 0;
1535} 1541}
@@ -1541,35 +1547,36 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1541 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 1547 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
1542 struct sn9c102_frame_t* f, * i; 1548 struct sn9c102_frame_t* f, * i;
1543 unsigned long lock_flags; 1549 unsigned long lock_flags;
1550 long timeout;
1544 int err = 0; 1551 int err = 0;
1545 1552
1546 if (down_interruptible(&cam->fileop_sem)) 1553 if (mutex_lock_interruptible(&cam->fileop_mutex))
1547 return -ERESTARTSYS; 1554 return -ERESTARTSYS;
1548 1555
1549 if (cam->state & DEV_DISCONNECTED) { 1556 if (cam->state & DEV_DISCONNECTED) {
1550 DBG(1, "Device not present"); 1557 DBG(1, "Device not present");
1551 up(&cam->fileop_sem); 1558 mutex_unlock(&cam->fileop_mutex);
1552 return -ENODEV; 1559 return -ENODEV;
1553 } 1560 }
1554 1561
1555 if (cam->state & DEV_MISCONFIGURED) { 1562 if (cam->state & DEV_MISCONFIGURED) {
1556 DBG(1, "The camera is misconfigured. Close and open it " 1563 DBG(1, "The camera is misconfigured. Close and open it "
1557 "again."); 1564 "again.");
1558 up(&cam->fileop_sem); 1565 mutex_unlock(&cam->fileop_mutex);
1559 return -EIO; 1566 return -EIO;
1560 } 1567 }
1561 1568
1562 if (cam->io == IO_MMAP) { 1569 if (cam->io == IO_MMAP) {
1563 DBG(3, "Close and open the device again to choose " 1570 DBG(3, "Close and open the device again to choose "
1564 "the read method"); 1571 "the read method");
1565 up(&cam->fileop_sem); 1572 mutex_unlock(&cam->fileop_mutex);
1566 return -EINVAL; 1573 return -EINVAL;
1567 } 1574 }
1568 1575
1569 if (cam->io == IO_NONE) { 1576 if (cam->io == IO_NONE) {
1570 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) { 1577 if (!sn9c102_request_buffers(cam,cam->nreadbuffers, IO_READ)) {
1571 DBG(1, "read() failed, not enough memory"); 1578 DBG(1, "read() failed, not enough memory");
1572 up(&cam->fileop_sem); 1579 mutex_unlock(&cam->fileop_mutex);
1573 return -ENOMEM; 1580 return -ENOMEM;
1574 } 1581 }
1575 cam->io = IO_READ; 1582 cam->io = IO_READ;
@@ -1583,30 +1590,32 @@ sn9c102_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
1583 } 1590 }
1584 1591
1585 if (!count) { 1592 if (!count) {
1586 up(&cam->fileop_sem); 1593 mutex_unlock(&cam->fileop_mutex);
1587 return 0; 1594 return 0;
1588 } 1595 }
1589 1596
1590 if (list_empty(&cam->outqueue)) { 1597 if (list_empty(&cam->outqueue)) {
1591 if (filp->f_flags & O_NONBLOCK) { 1598 if (filp->f_flags & O_NONBLOCK) {
1592 up(&cam->fileop_sem); 1599 mutex_unlock(&cam->fileop_mutex);
1593 return -EAGAIN; 1600 return -EAGAIN;
1594 } 1601 }
1595 err = wait_event_interruptible 1602 timeout = wait_event_interruptible_timeout
1596 ( cam->wait_frame, 1603 ( cam->wait_frame,
1597 (!list_empty(&cam->outqueue)) || 1604 (!list_empty(&cam->outqueue)) ||
1598 (cam->state & DEV_DISCONNECTED) || 1605 (cam->state & DEV_DISCONNECTED) ||
1599 (cam->state & DEV_MISCONFIGURED) ); 1606 (cam->state & DEV_MISCONFIGURED),
1600 if (err) { 1607 cam->module_param.frame_timeout *
1601 up(&cam->fileop_sem); 1608 1000 * msecs_to_jiffies(1) );
1602 return err; 1609 if (timeout < 0) {
1610 mutex_unlock(&cam->fileop_mutex);
1611 return timeout;
1603 } 1612 }
1604 if (cam->state & DEV_DISCONNECTED) { 1613 if (cam->state & DEV_DISCONNECTED) {
1605 up(&cam->fileop_sem); 1614 mutex_unlock(&cam->fileop_mutex);
1606 return -ENODEV; 1615 return -ENODEV;
1607 } 1616 }
1608 if (cam->state & DEV_MISCONFIGURED) { 1617 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
1609 up(&cam->fileop_sem); 1618 mutex_unlock(&cam->fileop_mutex);
1610 return -EIO; 1619 return -EIO;
1611 } 1620 }
1612 } 1621 }
@@ -1634,7 +1643,7 @@ exit:
1634 PDBGG("Frame #%lu, bytes read: %zu", 1643 PDBGG("Frame #%lu, bytes read: %zu",
1635 (unsigned long)f->buf.index, count); 1644 (unsigned long)f->buf.index, count);
1636 1645
1637 up(&cam->fileop_sem); 1646 mutex_unlock(&cam->fileop_mutex);
1638 1647
1639 return count; 1648 return count;
1640} 1649}
@@ -1647,7 +1656,7 @@ static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1647 unsigned long lock_flags; 1656 unsigned long lock_flags;
1648 unsigned int mask = 0; 1657 unsigned int mask = 0;
1649 1658
1650 if (down_interruptible(&cam->fileop_sem)) 1659 if (mutex_lock_interruptible(&cam->fileop_mutex))
1651 return POLLERR; 1660 return POLLERR;
1652 1661
1653 if (cam->state & DEV_DISCONNECTED) { 1662 if (cam->state & DEV_DISCONNECTED) {
@@ -1685,12 +1694,12 @@ static unsigned int sn9c102_poll(struct file *filp, poll_table *wait)
1685 if (!list_empty(&cam->outqueue)) 1694 if (!list_empty(&cam->outqueue))
1686 mask |= POLLIN | POLLRDNORM; 1695 mask |= POLLIN | POLLRDNORM;
1687 1696
1688 up(&cam->fileop_sem); 1697 mutex_unlock(&cam->fileop_mutex);
1689 1698
1690 return mask; 1699 return mask;
1691 1700
1692error: 1701error:
1693 up(&cam->fileop_sem); 1702 mutex_unlock(&cam->fileop_mutex);
1694 return POLLERR; 1703 return POLLERR;
1695} 1704}
1696 1705
@@ -1724,25 +1733,25 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1724 void *pos; 1733 void *pos;
1725 u32 i; 1734 u32 i;
1726 1735
1727 if (down_interruptible(&cam->fileop_sem)) 1736 if (mutex_lock_interruptible(&cam->fileop_mutex))
1728 return -ERESTARTSYS; 1737 return -ERESTARTSYS;
1729 1738
1730 if (cam->state & DEV_DISCONNECTED) { 1739 if (cam->state & DEV_DISCONNECTED) {
1731 DBG(1, "Device not present"); 1740 DBG(1, "Device not present");
1732 up(&cam->fileop_sem); 1741 mutex_unlock(&cam->fileop_mutex);
1733 return -ENODEV; 1742 return -ENODEV;
1734 } 1743 }
1735 1744
1736 if (cam->state & DEV_MISCONFIGURED) { 1745 if (cam->state & DEV_MISCONFIGURED) {
1737 DBG(1, "The camera is misconfigured. Close and open it " 1746 DBG(1, "The camera is misconfigured. Close and open it "
1738 "again."); 1747 "again.");
1739 up(&cam->fileop_sem); 1748 mutex_unlock(&cam->fileop_mutex);
1740 return -EIO; 1749 return -EIO;
1741 } 1750 }
1742 1751
1743 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || 1752 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
1744 size != PAGE_ALIGN(cam->frame[0].buf.length)) { 1753 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
1745 up(&cam->fileop_sem); 1754 mutex_unlock(&cam->fileop_mutex);
1746 return -EINVAL; 1755 return -EINVAL;
1747 } 1756 }
1748 1757
@@ -1751,7 +1760,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1751 break; 1760 break;
1752 } 1761 }
1753 if (i == cam->nbuffers) { 1762 if (i == cam->nbuffers) {
1754 up(&cam->fileop_sem); 1763 mutex_unlock(&cam->fileop_mutex);
1755 return -EINVAL; 1764 return -EINVAL;
1756 } 1765 }
1757 1766
@@ -1761,7 +1770,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1761 pos = cam->frame[i].bufmem; 1770 pos = cam->frame[i].bufmem;
1762 while (size > 0) { /* size is page-aligned */ 1771 while (size > 0) { /* size is page-aligned */
1763 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) { 1772 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1764 up(&cam->fileop_sem); 1773 mutex_unlock(&cam->fileop_mutex);
1765 return -EAGAIN; 1774 return -EAGAIN;
1766 } 1775 }
1767 start += PAGE_SIZE; 1776 start += PAGE_SIZE;
@@ -1774,7 +1783,7 @@ static int sn9c102_mmap(struct file* filp, struct vm_area_struct *vma)
1774 1783
1775 sn9c102_vm_open(vma); 1784 sn9c102_vm_open(vma);
1776 1785
1777 up(&cam->fileop_sem); 1786 mutex_unlock(&cam->fileop_mutex);
1778 1787
1779 return 0; 1788 return 0;
1780} 1789}
@@ -1816,6 +1825,7 @@ sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1816 1825
1817 memset(&i, 0, sizeof(i)); 1826 memset(&i, 0, sizeof(i));
1818 strcpy(i.name, "Camera"); 1827 strcpy(i.name, "Camera");
1828 i.type = V4L2_INPUT_TYPE_CAMERA;
1819 1829
1820 if (copy_to_user(arg, &i, sizeof(i))) 1830 if (copy_to_user(arg, &i, sizeof(i)))
1821 return -EFAULT; 1831 return -EFAULT;
@@ -1825,7 +1835,19 @@ sn9c102_vidioc_enuminput(struct sn9c102_device* cam, void __user * arg)
1825 1835
1826 1836
1827static int 1837static int
1828sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg) 1838sn9c102_vidioc_g_input(struct sn9c102_device* cam, void __user * arg)
1839{
1840 int index = 0;
1841
1842 if (copy_to_user(arg, &index, sizeof(index)))
1843 return -EFAULT;
1844
1845 return 0;
1846}
1847
1848
1849static int
1850sn9c102_vidioc_s_input(struct sn9c102_device* cam, void __user * arg)
1829{ 1851{
1830 int index; 1852 int index;
1831 1853
@@ -1842,7 +1864,7 @@ sn9c102_vidioc_gs_input(struct sn9c102_device* cam, void __user * arg)
1842static int 1864static int
1843sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg) 1865sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1844{ 1866{
1845 struct sn9c102_sensor* s = cam->sensor; 1867 struct sn9c102_sensor* s = &cam->sensor;
1846 struct v4l2_queryctrl qc; 1868 struct v4l2_queryctrl qc;
1847 u8 i; 1869 u8 i;
1848 1870
@@ -1864,7 +1886,7 @@ sn9c102_vidioc_query_ctrl(struct sn9c102_device* cam, void __user * arg)
1864static int 1886static int
1865sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg) 1887sn9c102_vidioc_g_ctrl(struct sn9c102_device* cam, void __user * arg)
1866{ 1888{
1867 struct sn9c102_sensor* s = cam->sensor; 1889 struct sn9c102_sensor* s = &cam->sensor;
1868 struct v4l2_control ctrl; 1890 struct v4l2_control ctrl;
1869 int err = 0; 1891 int err = 0;
1870 u8 i; 1892 u8 i;
@@ -1896,7 +1918,7 @@ exit:
1896static int 1918static int
1897sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg) 1919sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1898{ 1920{
1899 struct sn9c102_sensor* s = cam->sensor; 1921 struct sn9c102_sensor* s = &cam->sensor;
1900 struct v4l2_control ctrl; 1922 struct v4l2_control ctrl;
1901 u8 i; 1923 u8 i;
1902 int err = 0; 1924 int err = 0;
@@ -1909,6 +1931,8 @@ sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1909 1931
1910 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++) 1932 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1911 if (ctrl.id == s->qctrl[i].id) { 1933 if (ctrl.id == s->qctrl[i].id) {
1934 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1935 return -EINVAL;
1912 if (ctrl.value < s->qctrl[i].minimum || 1936 if (ctrl.value < s->qctrl[i].minimum ||
1913 ctrl.value > s->qctrl[i].maximum) 1937 ctrl.value > s->qctrl[i].maximum)
1914 return -ERANGE; 1938 return -ERANGE;
@@ -1931,7 +1955,7 @@ sn9c102_vidioc_s_ctrl(struct sn9c102_device* cam, void __user * arg)
1931static int 1955static int
1932sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg) 1956sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1933{ 1957{
1934 struct v4l2_cropcap* cc = &(cam->sensor->cropcap); 1958 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1935 1959
1936 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1960 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1937 cc->pixelaspect.numerator = 1; 1961 cc->pixelaspect.numerator = 1;
@@ -1947,7 +1971,7 @@ sn9c102_vidioc_cropcap(struct sn9c102_device* cam, void __user * arg)
1947static int 1971static int
1948sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg) 1972sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1949{ 1973{
1950 struct sn9c102_sensor* s = cam->sensor; 1974 struct sn9c102_sensor* s = &cam->sensor;
1951 struct v4l2_crop crop = { 1975 struct v4l2_crop crop = {
1952 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 1976 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1953 }; 1977 };
@@ -1964,7 +1988,7 @@ sn9c102_vidioc_g_crop(struct sn9c102_device* cam, void __user * arg)
1964static int 1988static int
1965sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg) 1989sn9c102_vidioc_s_crop(struct sn9c102_device* cam, void __user * arg)
1966{ 1990{
1967 struct sn9c102_sensor* s = cam->sensor; 1991 struct sn9c102_sensor* s = &cam->sensor;
1968 struct v4l2_crop crop; 1992 struct v4l2_crop crop;
1969 struct v4l2_rect* rect; 1993 struct v4l2_rect* rect;
1970 struct v4l2_rect* bounds = &(s->cropcap.bounds); 1994 struct v4l2_rect* bounds = &(s->cropcap.bounds);
@@ -2105,7 +2129,7 @@ static int
2105sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg) 2129sn9c102_vidioc_g_fmt(struct sn9c102_device* cam, void __user * arg)
2106{ 2130{
2107 struct v4l2_format format; 2131 struct v4l2_format format;
2108 struct v4l2_pix_format* pfmt = &(cam->sensor->pix_format); 2132 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
2109 2133
2110 if (copy_from_user(&format, arg, sizeof(format))) 2134 if (copy_from_user(&format, arg, sizeof(format)))
2111 return -EFAULT; 2135 return -EFAULT;
@@ -2130,7 +2154,7 @@ static int
2130sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd, 2154sn9c102_vidioc_try_s_fmt(struct sn9c102_device* cam, unsigned int cmd,
2131 void __user * arg) 2155 void __user * arg)
2132{ 2156{
2133 struct sn9c102_sensor* s = cam->sensor; 2157 struct sn9c102_sensor* s = &cam->sensor;
2134 struct v4l2_format format; 2158 struct v4l2_format format;
2135 struct v4l2_pix_format* pix; 2159 struct v4l2_pix_format* pix;
2136 struct v4l2_pix_format* pfmt = &(s->pix_format); 2160 struct v4l2_pix_format* pfmt = &(s->pix_format);
@@ -2417,7 +2441,7 @@ sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2417 struct v4l2_buffer b; 2441 struct v4l2_buffer b;
2418 struct sn9c102_frame_t *f; 2442 struct sn9c102_frame_t *f;
2419 unsigned long lock_flags; 2443 unsigned long lock_flags;
2420 int err = 0; 2444 long timeout;
2421 2445
2422 if (copy_from_user(&b, arg, sizeof(b))) 2446 if (copy_from_user(&b, arg, sizeof(b)))
2423 return -EFAULT; 2447 return -EFAULT;
@@ -2430,16 +2454,18 @@ sn9c102_vidioc_dqbuf(struct sn9c102_device* cam, struct file* filp,
2430 return -EINVAL; 2454 return -EINVAL;
2431 if (filp->f_flags & O_NONBLOCK) 2455 if (filp->f_flags & O_NONBLOCK)
2432 return -EAGAIN; 2456 return -EAGAIN;
2433 err = wait_event_interruptible 2457 timeout = wait_event_interruptible_timeout
2434 ( cam->wait_frame, 2458 ( cam->wait_frame,
2435 (!list_empty(&cam->outqueue)) || 2459 (!list_empty(&cam->outqueue)) ||
2436 (cam->state & DEV_DISCONNECTED) || 2460 (cam->state & DEV_DISCONNECTED) ||
2437 (cam->state & DEV_MISCONFIGURED) ); 2461 (cam->state & DEV_MISCONFIGURED),
2438 if (err) 2462 cam->module_param.frame_timeout *
2439 return err; 2463 1000 * msecs_to_jiffies(1) );
2464 if (timeout < 0)
2465 return timeout;
2440 if (cam->state & DEV_DISCONNECTED) 2466 if (cam->state & DEV_DISCONNECTED)
2441 return -ENODEV; 2467 return -ENODEV;
2442 if (cam->state & DEV_MISCONFIGURED) 2468 if (!timeout || (cam->state & DEV_MISCONFIGURED))
2443 return -EIO; 2469 return -EIO;
2444 } 2470 }
2445 2471
@@ -2571,8 +2597,10 @@ static int sn9c102_ioctl_v4l2(struct inode* inode, struct file* filp,
2571 return sn9c102_vidioc_enuminput(cam, arg); 2597 return sn9c102_vidioc_enuminput(cam, arg);
2572 2598
2573 case VIDIOC_G_INPUT: 2599 case VIDIOC_G_INPUT:
2600 return sn9c102_vidioc_g_input(cam, arg);
2601
2574 case VIDIOC_S_INPUT: 2602 case VIDIOC_S_INPUT:
2575 return sn9c102_vidioc_gs_input(cam, arg); 2603 return sn9c102_vidioc_s_input(cam, arg);
2576 2604
2577 case VIDIOC_QUERYCTRL: 2605 case VIDIOC_QUERYCTRL:
2578 return sn9c102_vidioc_query_ctrl(cam, arg); 2606 return sn9c102_vidioc_query_ctrl(cam, arg);
@@ -2655,19 +2683,19 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2655 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp)); 2683 struct sn9c102_device* cam = video_get_drvdata(video_devdata(filp));
2656 int err = 0; 2684 int err = 0;
2657 2685
2658 if (down_interruptible(&cam->fileop_sem)) 2686 if (mutex_lock_interruptible(&cam->fileop_mutex))
2659 return -ERESTARTSYS; 2687 return -ERESTARTSYS;
2660 2688
2661 if (cam->state & DEV_DISCONNECTED) { 2689 if (cam->state & DEV_DISCONNECTED) {
2662 DBG(1, "Device not present"); 2690 DBG(1, "Device not present");
2663 up(&cam->fileop_sem); 2691 mutex_unlock(&cam->fileop_mutex);
2664 return -ENODEV; 2692 return -ENODEV;
2665 } 2693 }
2666 2694
2667 if (cam->state & DEV_MISCONFIGURED) { 2695 if (cam->state & DEV_MISCONFIGURED) {
2668 DBG(1, "The camera is misconfigured. Close and open it " 2696 DBG(1, "The camera is misconfigured. Close and open it "
2669 "again."); 2697 "again.");
2670 up(&cam->fileop_sem); 2698 mutex_unlock(&cam->fileop_mutex);
2671 return -EIO; 2699 return -EIO;
2672 } 2700 }
2673 2701
@@ -2675,7 +2703,7 @@ static int sn9c102_ioctl(struct inode* inode, struct file* filp,
2675 2703
2676 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg); 2704 err = sn9c102_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
2677 2705
2678 up(&cam->fileop_sem); 2706 mutex_unlock(&cam->fileop_mutex);
2679 2707
2680 return err; 2708 return err;
2681} 2709}
@@ -2722,7 +2750,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2722 goto fail; 2750 goto fail;
2723 } 2751 }
2724 2752
2725 init_MUTEX(&cam->dev_sem); 2753 mutex_init(&cam->dev_mutex);
2726 2754
2727 r = sn9c102_read_reg(cam, 0x00); 2755 r = sn9c102_read_reg(cam, 0x00);
2728 if (r < 0 || r != 0x10) { 2756 if (r < 0 || r != 0x10) {
@@ -2752,10 +2780,10 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2752 break; 2780 break;
2753 } 2781 }
2754 2782
2755 if (!err && cam->sensor) { 2783 if (!err) {
2756 DBG(2, "%s image sensor detected", cam->sensor->name); 2784 DBG(2, "%s image sensor detected", cam->sensor.name);
2757 DBG(3, "Support for %s maintained by %s", 2785 DBG(3, "Support for %s maintained by %s",
2758 cam->sensor->name, cam->sensor->maintainer); 2786 cam->sensor.name, cam->sensor.maintainer);
2759 } else { 2787 } else {
2760 DBG(1, "No supported image sensor detected"); 2788 DBG(1, "No supported image sensor detected");
2761 err = -ENODEV; 2789 err = -ENODEV;
@@ -2776,7 +2804,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2776 cam->v4ldev->release = video_device_release; 2804 cam->v4ldev->release = video_device_release;
2777 video_set_drvdata(cam->v4ldev, cam); 2805 video_set_drvdata(cam->v4ldev, cam);
2778 2806
2779 down(&cam->dev_sem); 2807 mutex_lock(&cam->dev_mutex);
2780 2808
2781 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER, 2809 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
2782 video_nr[dev_nr]); 2810 video_nr[dev_nr]);
@@ -2786,13 +2814,14 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2786 DBG(1, "Free /dev/videoX node not found"); 2814 DBG(1, "Free /dev/videoX node not found");
2787 video_nr[dev_nr] = -1; 2815 video_nr[dev_nr] = -1;
2788 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2816 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2789 up(&cam->dev_sem); 2817 mutex_unlock(&cam->dev_mutex);
2790 goto fail; 2818 goto fail;
2791 } 2819 }
2792 2820
2793 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor); 2821 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
2794 2822
2795 cam->module_param.force_munmap = force_munmap[dev_nr]; 2823 cam->module_param.force_munmap = force_munmap[dev_nr];
2824 cam->module_param.frame_timeout = frame_timeout[dev_nr];
2796 2825
2797 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0; 2826 dev_nr = (dev_nr < SN9C102_MAX_DEVICES-1) ? dev_nr+1 : 0;
2798 2827
@@ -2803,7 +2832,7 @@ sn9c102_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
2803 2832
2804 usb_set_intfdata(intf, cam); 2833 usb_set_intfdata(intf, cam);
2805 2834
2806 up(&cam->dev_sem); 2835 mutex_unlock(&cam->dev_mutex);
2807 2836
2808 return 0; 2837 return 0;
2809 2838
@@ -2827,7 +2856,7 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2827 2856
2828 down_write(&sn9c102_disconnect); 2857 down_write(&sn9c102_disconnect);
2829 2858
2830 down(&cam->dev_sem); 2859 mutex_lock(&cam->dev_mutex);
2831 2860
2832 DBG(2, "Disconnecting %s...", cam->v4ldev->name); 2861 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
2833 2862
@@ -2841,13 +2870,14 @@ static void sn9c102_usb_disconnect(struct usb_interface* intf)
2841 sn9c102_stop_transfer(cam); 2870 sn9c102_stop_transfer(cam);
2842 cam->state |= DEV_DISCONNECTED; 2871 cam->state |= DEV_DISCONNECTED;
2843 wake_up_interruptible(&cam->wait_frame); 2872 wake_up_interruptible(&cam->wait_frame);
2844 wake_up_interruptible(&cam->wait_stream); 2873 wake_up(&cam->wait_stream);
2874 usb_get_dev(cam->usbdev);
2845 } else { 2875 } else {
2846 cam->state |= DEV_DISCONNECTED; 2876 cam->state |= DEV_DISCONNECTED;
2847 sn9c102_release_resources(cam); 2877 sn9c102_release_resources(cam);
2848 } 2878 }
2849 2879
2850 up(&cam->dev_sem); 2880 mutex_unlock(&cam->dev_mutex);
2851 2881
2852 if (!cam->users) 2882 if (!cam->users)
2853 kfree(cam); 2883 kfree(cam);
diff --git a/drivers/usb/media/sn9c102_ov7630.c b/drivers/usb/media/sn9c102_ov7630.c
index 4a36519b5af4..42852b7cb042 100644
--- a/drivers/usb/media/sn9c102_ov7630.c
+++ b/drivers/usb/media/sn9c102_ov7630.c
@@ -34,8 +34,8 @@ static int ov7630_init(struct sn9c102_device* cam)
34 err += sn9c102_write_reg(cam, 0x0f, 0x18); 34 err += sn9c102_write_reg(cam, 0x0f, 0x18);
35 err += sn9c102_write_reg(cam, 0x50, 0x19); 35 err += sn9c102_write_reg(cam, 0x50, 0x19);
36 36
37 err += sn9c102_i2c_write(cam, 0x12, 0x8d); 37 err += sn9c102_i2c_write(cam, 0x12, 0x80);
38 err += sn9c102_i2c_write(cam, 0x11, 0x00); 38 err += sn9c102_i2c_write(cam, 0x11, 0x01);
39 err += sn9c102_i2c_write(cam, 0x15, 0x34); 39 err += sn9c102_i2c_write(cam, 0x15, 0x34);
40 err += sn9c102_i2c_write(cam, 0x16, 0x03); 40 err += sn9c102_i2c_write(cam, 0x16, 0x03);
41 err += sn9c102_i2c_write(cam, 0x17, 0x1c); 41 err += sn9c102_i2c_write(cam, 0x17, 0x1c);
@@ -43,12 +43,14 @@ static int ov7630_init(struct sn9c102_device* cam)
43 err += sn9c102_i2c_write(cam, 0x19, 0x06); 43 err += sn9c102_i2c_write(cam, 0x19, 0x06);
44 err += sn9c102_i2c_write(cam, 0x1a, 0xf6); 44 err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
45 err += sn9c102_i2c_write(cam, 0x1b, 0x04); 45 err += sn9c102_i2c_write(cam, 0x1b, 0x04);
46 err += sn9c102_i2c_write(cam, 0x20, 0x44); 46 err += sn9c102_i2c_write(cam, 0x20, 0xf6);
47 err += sn9c102_i2c_write(cam, 0x23, 0xee); 47 err += sn9c102_i2c_write(cam, 0x23, 0xee);
48 err += sn9c102_i2c_write(cam, 0x26, 0xa0); 48 err += sn9c102_i2c_write(cam, 0x26, 0xa0);
49 err += sn9c102_i2c_write(cam, 0x27, 0x9a); 49 err += sn9c102_i2c_write(cam, 0x27, 0x9a);
50 err += sn9c102_i2c_write(cam, 0x28, 0x20); 50 err += sn9c102_i2c_write(cam, 0x28, 0xa0);
51 err += sn9c102_i2c_write(cam, 0x29, 0x30); 51 err += sn9c102_i2c_write(cam, 0x29, 0x30);
52 err += sn9c102_i2c_write(cam, 0x2a, 0xa0);
53 err += sn9c102_i2c_write(cam, 0x2b, 0x1f);
52 err += sn9c102_i2c_write(cam, 0x2f, 0x3d); 54 err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
53 err += sn9c102_i2c_write(cam, 0x30, 0x24); 55 err += sn9c102_i2c_write(cam, 0x30, 0x24);
54 err += sn9c102_i2c_write(cam, 0x32, 0x86); 56 err += sn9c102_i2c_write(cam, 0x32, 0x86);
@@ -80,7 +82,7 @@ static int ov7630_set_ctrl(struct sn9c102_device* cam,
80 err += sn9c102_i2c_write(cam, 0x02, ctrl->value); 82 err += sn9c102_i2c_write(cam, 0x02, ctrl->value);
81 break; 83 break;
82 case V4L2_CID_BLUE_BALANCE: 84 case V4L2_CID_BLUE_BALANCE:
83 err += sn9c102_i2c_write(cam, 0x03, ctrl->value); 85 err += sn9c102_i2c_write(cam, 0x01, ctrl->value);
84 break; 86 break;
85 case V4L2_CID_GAIN: 87 case V4L2_CID_GAIN:
86 err += sn9c102_i2c_write(cam, 0x00, ctrl->value); 88 err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
@@ -108,7 +110,7 @@ static int ov7630_set_ctrl(struct sn9c102_device* cam,
108 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value); 110 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
109 break; 111 break;
110 case V4L2_CID_AUTO_WHITE_BALANCE: 112 case V4L2_CID_AUTO_WHITE_BALANCE:
111 err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x09); 113 err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x78);
112 break; 114 break;
113 case V4L2_CID_AUTOGAIN: 115 case V4L2_CID_AUTOGAIN:
114 err += sn9c102_i2c_write(cam, 0x13, ctrl->value); 116 err += sn9c102_i2c_write(cam, 0x13, ctrl->value);
@@ -371,26 +373,29 @@ static struct sn9c102_sensor ov7630 = {
371 373
372int sn9c102_probe_ov7630(struct sn9c102_device* cam) 374int sn9c102_probe_ov7630(struct sn9c102_device* cam)
373{ 375{
376 const struct usb_device_id ov7630_id_table[] = {
377 { USB_DEVICE(0x0c45, 0x602c), },
378 { USB_DEVICE(0x0c45, 0x602d), },
379 { USB_DEVICE(0x0c45, 0x608f), },
380 { USB_DEVICE(0x0c45, 0x60b0), },
381 { }
382 };
374 int err = 0; 383 int err = 0;
375 384
376 sn9c102_attach_sensor(cam, &ov7630); 385 if (!sn9c102_match_id(cam, ov7630_id_table))
377
378 if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c &&
379 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602d &&
380 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f &&
381 le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x60b0)
382 return -ENODEV; 386 return -ENODEV;
383 387
384 err += sn9c102_write_reg(cam, 0x01, 0x01); 388 err += sn9c102_write_reg(cam, 0x01, 0x01);
385 err += sn9c102_write_reg(cam, 0x00, 0x01); 389 err += sn9c102_write_reg(cam, 0x00, 0x01);
386 err += sn9c102_write_reg(cam, 0x28, 0x17); 390 err += sn9c102_write_reg(cam, 0x28, 0x17);
387
388 if (err) 391 if (err)
389 return -EIO; 392 return -EIO;
390 393
391 err += sn9c102_i2c_write(cam, 0x0b, 0); 394 err += sn9c102_i2c_try_write(cam, &ov7630, 0x0b, 0);
392 if (err) 395 if (err)
393 return -ENODEV; 396 return -ENODEV;
394 397
398 sn9c102_attach_sensor(cam, &ov7630);
399
395 return 0; 400 return 0;
396} 401}
diff --git a/drivers/usb/media/sn9c102_pas202bca.c b/drivers/usb/media/sn9c102_pas202bca.c
new file mode 100644
index 000000000000..3453237055bb
--- /dev/null
+++ b/drivers/usb/media/sn9c102_pas202bca.c
@@ -0,0 +1,238 @@
1/***************************************************************************
2 * Plug-in for PAS202BCA image sensor connected to the SN9C10x PC Camera *
3 * Controllers *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#include <linux/delay.h>
23#include "sn9c102_sensor.h"
24
25
26static struct sn9c102_sensor pas202bca;
27
28
29static int pas202bca_init(struct sn9c102_device* cam)
30{
31 int err = 0;
32
33 err += sn9c102_write_reg(cam, 0x00, 0x10);
34 err += sn9c102_write_reg(cam, 0x00, 0x11);
35 err += sn9c102_write_reg(cam, 0x00, 0x14);
36 err += sn9c102_write_reg(cam, 0x20, 0x17);
37 err += sn9c102_write_reg(cam, 0x30, 0x19);
38 err += sn9c102_write_reg(cam, 0x09, 0x18);
39
40 err += sn9c102_i2c_write(cam, 0x02, 0x14);
41 err += sn9c102_i2c_write(cam, 0x03, 0x40);
42 err += sn9c102_i2c_write(cam, 0x0d, 0x2c);
43 err += sn9c102_i2c_write(cam, 0x0e, 0x01);
44 err += sn9c102_i2c_write(cam, 0x0f, 0xa9);
45 err += sn9c102_i2c_write(cam, 0x10, 0x08);
46 err += sn9c102_i2c_write(cam, 0x13, 0x63);
47 err += sn9c102_i2c_write(cam, 0x15, 0x70);
48 err += sn9c102_i2c_write(cam, 0x11, 0x01);
49
50 msleep(400);
51
52 return err;
53}
54
55
56static int pas202bca_set_pix_format(struct sn9c102_device* cam,
57 const struct v4l2_pix_format* pix)
58{
59 int err = 0;
60
61 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
62 err += sn9c102_write_reg(cam, 0x24, 0x17);
63 else
64 err += sn9c102_write_reg(cam, 0x20, 0x17);
65
66 return err;
67}
68
69
70static int pas202bca_set_ctrl(struct sn9c102_device* cam,
71 const struct v4l2_control* ctrl)
72{
73 int err = 0;
74
75 switch (ctrl->id) {
76 case V4L2_CID_EXPOSURE:
77 err += sn9c102_i2c_write(cam, 0x04, ctrl->value >> 6);
78 err += sn9c102_i2c_write(cam, 0x05, ctrl->value & 0x3f);
79 break;
80 case V4L2_CID_RED_BALANCE:
81 err += sn9c102_i2c_write(cam, 0x09, ctrl->value);
82 break;
83 case V4L2_CID_BLUE_BALANCE:
84 err += sn9c102_i2c_write(cam, 0x07, ctrl->value);
85 break;
86 case V4L2_CID_GAIN:
87 err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
88 break;
89 case SN9C102_V4L2_CID_GREEN_BALANCE:
90 err += sn9c102_i2c_write(cam, 0x08, ctrl->value);
91 break;
92 case SN9C102_V4L2_CID_DAC_MAGNITUDE:
93 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
94 break;
95 default:
96 return -EINVAL;
97 }
98 err += sn9c102_i2c_write(cam, 0x11, 0x01);
99
100 return err ? -EIO : 0;
101}
102
103
104static int pas202bca_set_crop(struct sn9c102_device* cam,
105 const struct v4l2_rect* rect)
106{
107 struct sn9c102_sensor* s = &pas202bca;
108 int err = 0;
109 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 3,
110 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 3;
111
112 err += sn9c102_write_reg(cam, h_start, 0x12);
113 err += sn9c102_write_reg(cam, v_start, 0x13);
114
115 return err;
116}
117
118
119static struct sn9c102_sensor pas202bca = {
120 .name = "PAS202BCA",
121 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
122 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
123 .frequency = SN9C102_I2C_400KHZ | SN9C102_I2C_100KHZ,
124 .interface = SN9C102_I2C_2WIRES,
125 .i2c_slave_id = 0x40,
126 .init = &pas202bca_init,
127 .qctrl = {
128 {
129 .id = V4L2_CID_EXPOSURE,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "exposure",
132 .minimum = 0x01e5,
133 .maximum = 0x3fff,
134 .step = 0x0001,
135 .default_value = 0x01e5,
136 .flags = 0,
137 },
138 {
139 .id = V4L2_CID_GAIN,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "global gain",
142 .minimum = 0x00,
143 .maximum = 0x1f,
144 .step = 0x01,
145 .default_value = 0x0c,
146 .flags = 0,
147 },
148 {
149 .id = V4L2_CID_RED_BALANCE,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "red balance",
152 .minimum = 0x00,
153 .maximum = 0x0f,
154 .step = 0x01,
155 .default_value = 0x01,
156 .flags = 0,
157 },
158 {
159 .id = V4L2_CID_BLUE_BALANCE,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "blue balance",
162 .minimum = 0x00,
163 .maximum = 0x0f,
164 .step = 0x01,
165 .default_value = 0x05,
166 .flags = 0,
167 },
168 {
169 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
170 .type = V4L2_CTRL_TYPE_INTEGER,
171 .name = "green balance",
172 .minimum = 0x00,
173 .maximum = 0x0f,
174 .step = 0x01,
175 .default_value = 0x00,
176 .flags = 0,
177 },
178 {
179 .id = SN9C102_V4L2_CID_DAC_MAGNITUDE,
180 .type = V4L2_CTRL_TYPE_INTEGER,
181 .name = "DAC magnitude",
182 .minimum = 0x00,
183 .maximum = 0xff,
184 .step = 0x01,
185 .default_value = 0x04,
186 .flags = 0,
187 },
188 },
189 .set_ctrl = &pas202bca_set_ctrl,
190 .cropcap = {
191 .bounds = {
192 .left = 0,
193 .top = 0,
194 .width = 640,
195 .height = 480,
196 },
197 .defrect = {
198 .left = 0,
199 .top = 0,
200 .width = 640,
201 .height = 480,
202 },
203 },
204 .set_crop = &pas202bca_set_crop,
205 .pix_format = {
206 .width = 640,
207 .height = 480,
208 .pixelformat = V4L2_PIX_FMT_SBGGR8,
209 .priv = 8,
210 },
211 .set_pix_format = &pas202bca_set_pix_format
212};
213
214
215int sn9c102_probe_pas202bca(struct sn9c102_device* cam)
216{
217 const struct usb_device_id pas202bca_id_table[] = {
218 { USB_DEVICE(0x0c45, 0x60af), },
219 { }
220 };
221 int err = 0;
222
223 if (!sn9c102_match_id(cam,pas202bca_id_table))
224 return -ENODEV;
225
226 err += sn9c102_write_reg(cam, 0x01, 0x01);
227 err += sn9c102_write_reg(cam, 0x40, 0x01);
228 err += sn9c102_write_reg(cam, 0x28, 0x17);
229 if (err)
230 return -EIO;
231
232 if (sn9c102_i2c_try_write(cam, &pas202bca, 0x10, 0)) /* try to write */
233 return -ENODEV;
234
235 sn9c102_attach_sensor(cam, &pas202bca);
236
237 return 0;
238}
diff --git a/drivers/usb/media/sn9c102_pas202bcb.c b/drivers/usb/media/sn9c102_pas202bcb.c
index 5ca54c7daaf2..d068616ab337 100644
--- a/drivers/usb/media/sn9c102_pas202bcb.c
+++ b/drivers/usb/media/sn9c102_pas202bcb.c
@@ -263,7 +263,7 @@ static struct sn9c102_sensor pas202bcb = {
263 263
264 264
265int sn9c102_probe_pas202bcb(struct sn9c102_device* cam) 265int sn9c102_probe_pas202bcb(struct sn9c102_device* cam)
266{ 266{
267 int r0 = 0, r1 = 0, err = 0; 267 int r0 = 0, r1 = 0, err = 0;
268 unsigned int pid = 0; 268 unsigned int pid = 0;
269 269
diff --git a/drivers/usb/media/sn9c102_sensor.h b/drivers/usb/media/sn9c102_sensor.h
index 7d953b24f2f2..2afd9e9d09bb 100644
--- a/drivers/usb/media/sn9c102_sensor.h
+++ b/drivers/usb/media/sn9c102_sensor.h
@@ -66,6 +66,7 @@ extern int sn9c102_probe_hv7131d(struct sn9c102_device* cam);
66extern int sn9c102_probe_mi0343(struct sn9c102_device* cam); 66extern int sn9c102_probe_mi0343(struct sn9c102_device* cam);
67extern int sn9c102_probe_ov7630(struct sn9c102_device* cam); 67extern int sn9c102_probe_ov7630(struct sn9c102_device* cam);
68extern int sn9c102_probe_pas106b(struct sn9c102_device* cam); 68extern int sn9c102_probe_pas106b(struct sn9c102_device* cam);
69extern int sn9c102_probe_pas202bca(struct sn9c102_device* cam);
69extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam); 70extern int sn9c102_probe_pas202bcb(struct sn9c102_device* cam);
70extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam); 71extern int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam);
71extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam); 72extern int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam);
@@ -81,12 +82,17 @@ static int (*sn9c102_sensor_table[])(struct sn9c102_device*) = { \
81 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ \ 82 &sn9c102_probe_pas106b, /* strong detection based on SENSOR ids */ \
82 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ \ 83 &sn9c102_probe_pas202bcb, /* strong detection based on SENSOR ids */ \
83 &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ \ 84 &sn9c102_probe_hv7131d, /* strong detection based on SENSOR ids */ \
85 &sn9c102_probe_pas202bca, /* detection mostly based on USB pid/vid */ \
84 &sn9c102_probe_ov7630, /* detection mostly based on USB pid/vid */ \ 86 &sn9c102_probe_ov7630, /* detection mostly based on USB pid/vid */ \
85 &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ \ 87 &sn9c102_probe_tas5110c1b, /* detection based on USB pid/vid */ \
86 &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ \ 88 &sn9c102_probe_tas5130d1b, /* detection based on USB pid/vid */ \
87 NULL, \ 89 NULL, \
88}; 90};
89 91
92/* Device identification */
93extern struct sn9c102_device*
94sn9c102_match_id(struct sn9c102_device* cam, const struct usb_device_id *id);
95
90/* Attach a probed sensor to the camera. */ 96/* Attach a probed sensor to the camera. */
91extern void 97extern void
92sn9c102_attach_sensor(struct sn9c102_device* cam, 98sn9c102_attach_sensor(struct sn9c102_device* cam,
@@ -108,6 +114,7 @@ sn9c102_attach_sensor(struct sn9c102_device* cam,
108static const struct usb_device_id sn9c102_id_table[] = { \ 114static const struct usb_device_id sn9c102_id_table[] = { \
109 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \ 115 { USB_DEVICE(0x0c45, 0x6001), }, /* TAS5110C1B */ \
110 { USB_DEVICE(0x0c45, 0x6005), }, /* TAS5110C1B */ \ 116 { USB_DEVICE(0x0c45, 0x6005), }, /* TAS5110C1B */ \
117 { USB_DEVICE(0x0c45, 0x6007), }, \
111 { USB_DEVICE(0x0c45, 0x6009), }, /* PAS106B */ \ 118 { USB_DEVICE(0x0c45, 0x6009), }, /* PAS106B */ \
112 { USB_DEVICE(0x0c45, 0x600d), }, /* PAS106B */ \ 119 { USB_DEVICE(0x0c45, 0x600d), }, /* PAS106B */ \
113 { USB_DEVICE(0x0c45, 0x6024), }, \ 120 { USB_DEVICE(0x0c45, 0x6024), }, \
@@ -126,7 +133,7 @@ static const struct usb_device_id sn9c102_id_table[] = { \
126 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \ 133 { SN9C102_USB_DEVICE(0x0c45, 0x6088, 0xff), }, \
127 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \ 134 { SN9C102_USB_DEVICE(0x0c45, 0x608a, 0xff), }, \
128 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \ 135 { SN9C102_USB_DEVICE(0x0c45, 0x608b, 0xff), }, \
129 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131x */ \ 136 { SN9C102_USB_DEVICE(0x0c45, 0x608c, 0xff), }, /* HV7131/R */ \
130 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \ 137 { SN9C102_USB_DEVICE(0x0c45, 0x608e, 0xff), }, /* CIS-VF10 */ \
131 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \ 138 { SN9C102_USB_DEVICE(0x0c45, 0x608f, 0xff), }, /* OV7630 */ \
132 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \ 139 { SN9C102_USB_DEVICE(0x0c45, 0x60a0, 0xff), }, \
@@ -359,12 +366,6 @@ struct sn9c102_sensor {
359 error code without rolling back. 366 error code without rolling back.
360 */ 367 */
361 368
362 const struct usb_device* usbdev;
363 /*
364 Points to the usb_device struct after the sensor is attached.
365 Do not touch unless you know what you are doing.
366 */
367
368 /* 369 /*
369 Do NOT write to the data below, it's READ ONLY. It is used by the 370 Do NOT write to the data below, it's READ ONLY. It is used by the
370 core module to store successfully updated values of the above 371 core module to store successfully updated values of the above
diff --git a/drivers/usb/media/sn9c102_tas5110c1b.c b/drivers/usb/media/sn9c102_tas5110c1b.c
index 32ddf236cafe..2e08c552f40a 100644
--- a/drivers/usb/media/sn9c102_tas5110c1b.c
+++ b/drivers/usb/media/sn9c102_tas5110c1b.c
@@ -142,14 +142,18 @@ static struct sn9c102_sensor tas5110c1b = {
142 142
143int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam) 143int sn9c102_probe_tas5110c1b(struct sn9c102_device* cam)
144{ 144{
145 /* This sensor has no identifiers, so let's attach it anyway */ 145 const struct usb_device_id tas5110c1b_id_table[] = {
146 sn9c102_attach_sensor(cam, &tas5110c1b); 146 { USB_DEVICE(0x0c45, 0x6001), },
147 { USB_DEVICE(0x0c45, 0x6005), },
148 { USB_DEVICE(0x0c45, 0x60ab), },
149 { }
150 };
147 151
148 /* Sensor detection is based on USB pid/vid */ 152 /* Sensor detection is based on USB pid/vid */
149 if (le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6001 && 153 if (!sn9c102_match_id(cam, tas5110c1b_id_table))
150 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x6005 &&
151 le16_to_cpu(tas5110c1b.usbdev->descriptor.idProduct) != 0x60ab)
152 return -ENODEV; 154 return -ENODEV;
153 155
156 sn9c102_attach_sensor(cam, &tas5110c1b);
157
154 return 0; 158 return 0;
155} 159}
diff --git a/drivers/usb/media/sn9c102_tas5130d1b.c b/drivers/usb/media/sn9c102_tas5130d1b.c
index a0728f0ae00c..c7b339740bbf 100644
--- a/drivers/usb/media/sn9c102_tas5130d1b.c
+++ b/drivers/usb/media/sn9c102_tas5130d1b.c
@@ -153,13 +153,17 @@ static struct sn9c102_sensor tas5130d1b = {
153 153
154int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam) 154int sn9c102_probe_tas5130d1b(struct sn9c102_device* cam)
155{ 155{
156 /* This sensor has no identifiers, so let's attach it anyway */ 156 const struct usb_device_id tas5130d1b_id_table[] = {
157 sn9c102_attach_sensor(cam, &tas5130d1b); 157 { USB_DEVICE(0x0c45, 0x6025), },
158 { USB_DEVICE(0x0c45, 0x60aa), },
159 { }
160 };
158 161
159 /* Sensor detection is based on USB pid/vid */ 162 /* Sensor detection is based on USB pid/vid */
160 if (le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x6025 && 163 if (!sn9c102_match_id(cam, tas5130d1b_id_table))
161 le16_to_cpu(tas5130d1b.usbdev->descriptor.idProduct) != 0x60aa)
162 return -ENODEV; 164 return -ENODEV;
163 165
166 sn9c102_attach_sensor(cam, &tas5130d1b);
167
164 return 0; 168 return 0;
165} 169}
diff --git a/drivers/usb/media/stv680.c b/drivers/usb/media/stv680.c
index b497a6a0a206..9636da20748d 100644
--- a/drivers/usb/media/stv680.c
+++ b/drivers/usb/media/stv680.c
@@ -67,6 +67,7 @@
67#include <linux/errno.h> 67#include <linux/errno.h>
68#include <linux/videodev.h> 68#include <linux/videodev.h>
69#include <linux/usb.h> 69#include <linux/usb.h>
70#include <linux/mutex.h>
70 71
71#include "stv680.h" 72#include "stv680.h"
72 73
@@ -317,12 +318,11 @@ static int stv_init (struct usb_stv *stv680)
317 unsigned char *buffer; 318 unsigned char *buffer;
318 unsigned long int bufsize; 319 unsigned long int bufsize;
319 320
320 buffer = kmalloc (40, GFP_KERNEL); 321 buffer = kzalloc (40, GFP_KERNEL);
321 if (buffer == NULL) { 322 if (buffer == NULL) {
322 PDEBUG (0, "STV(e): Out of (small buf) memory"); 323 PDEBUG (0, "STV(e): Out of (small buf) memory");
323 return -1; 324 return -1;
324 } 325 }
325 memset (buffer, 0, 40);
326 udelay (100); 326 udelay (100);
327 327
328 /* set config 1, interface 0, alternate 0 */ 328 /* set config 1, interface 0, alternate 0 */
@@ -1258,22 +1258,22 @@ static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1258 unsigned long size = vma->vm_end-vma->vm_start; 1258 unsigned long size = vma->vm_end-vma->vm_start;
1259 unsigned long page, pos; 1259 unsigned long page, pos;
1260 1260
1261 down (&stv680->lock); 1261 mutex_lock(&stv680->lock);
1262 1262
1263 if (stv680->udev == NULL) { 1263 if (stv680->udev == NULL) {
1264 up (&stv680->lock); 1264 mutex_unlock(&stv680->lock);
1265 return -EIO; 1265 return -EIO;
1266 } 1266 }
1267 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1) 1267 if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
1268 & ~(PAGE_SIZE - 1))) { 1268 & ~(PAGE_SIZE - 1))) {
1269 up (&stv680->lock); 1269 mutex_unlock(&stv680->lock);
1270 return -EINVAL; 1270 return -EINVAL;
1271 } 1271 }
1272 pos = (unsigned long) stv680->fbuf; 1272 pos = (unsigned long) stv680->fbuf;
1273 while (size > 0) { 1273 while (size > 0) {
1274 page = vmalloc_to_pfn((void *)pos); 1274 page = vmalloc_to_pfn((void *)pos);
1275 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 1275 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
1276 up (&stv680->lock); 1276 mutex_unlock(&stv680->lock);
1277 return -EAGAIN; 1277 return -EAGAIN;
1278 } 1278 }
1279 start += PAGE_SIZE; 1279 start += PAGE_SIZE;
@@ -1283,7 +1283,7 @@ static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
1283 else 1283 else
1284 size = 0; 1284 size = 0;
1285 } 1285 }
1286 up (&stv680->lock); 1286 mutex_unlock(&stv680->lock);
1287 1287
1288 return 0; 1288 return 0;
1289} 1289}
@@ -1387,14 +1387,12 @@ static int stv680_probe (struct usb_interface *intf, const struct usb_device_id
1387 goto error; 1387 goto error;
1388 } 1388 }
1389 /* We found one */ 1389 /* We found one */
1390 if ((stv680 = kmalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) { 1390 if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
1391 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct."); 1391 PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
1392 retval = -ENOMEM; 1392 retval = -ENOMEM;
1393 goto error; 1393 goto error;
1394 } 1394 }
1395 1395
1396 memset (stv680, 0, sizeof (*stv680));
1397
1398 stv680->udev = dev; 1396 stv680->udev = dev;
1399 stv680->camera_name = camera_name; 1397 stv680->camera_name = camera_name;
1400 1398
@@ -1409,7 +1407,7 @@ static int stv680_probe (struct usb_interface *intf, const struct usb_device_id
1409 1407
1410 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name)); 1408 memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
1411 init_waitqueue_head (&stv680->wq); 1409 init_waitqueue_head (&stv680->wq);
1412 init_MUTEX (&stv680->lock); 1410 mutex_init (&stv680->lock);
1413 wmb (); 1411 wmb ();
1414 1412
1415 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) { 1413 if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
diff --git a/drivers/usb/media/stv680.h b/drivers/usb/media/stv680.h
index b0551cdb280b..ea46e0001e6d 100644
--- a/drivers/usb/media/stv680.h
+++ b/drivers/usb/media/stv680.h
@@ -118,7 +118,7 @@ struct usb_stv {
118 int origGain; 118 int origGain;
119 int origMode; /* original camera mode */ 119 int origMode; /* original camera mode */
120 120
121 struct semaphore lock; /* to lock the structure */ 121 struct mutex lock; /* to lock the structure */
122 int user; /* user count for exclusive use */ 122 int user; /* user count for exclusive use */
123 int removed; /* device disconnected */ 123 int removed; /* device disconnected */
124 int streaming; /* Are we streaming video? */ 124 int streaming; /* Are we streaming video? */
diff --git a/drivers/usb/media/usbvideo.c b/drivers/usb/media/usbvideo.c
index 63a72e550a1b..0b51fae720a9 100644
--- a/drivers/usb/media/usbvideo.c
+++ b/drivers/usb/media/usbvideo.c
@@ -690,14 +690,13 @@ int usbvideo_register(
690 } 690 }
691 691
692 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo); 692 base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
693 cams = (struct usbvideo *) kmalloc(base_size, GFP_KERNEL); 693 cams = (struct usbvideo *) kzalloc(base_size, GFP_KERNEL);
694 if (cams == NULL) { 694 if (cams == NULL) {
695 err("Failed to allocate %d. bytes for usbvideo struct", base_size); 695 err("Failed to allocate %d. bytes for usbvideo struct", base_size);
696 return -ENOMEM; 696 return -ENOMEM;
697 } 697 }
698 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras", 698 dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
699 __FUNCTION__, cams, base_size, num_cams); 699 __FUNCTION__, cams, base_size, num_cams);
700 memset(cams, 0, base_size);
701 700
702 /* Copy callbacks, apply defaults for those that are not set */ 701 /* Copy callbacks, apply defaults for those that are not set */
703 memmove(&cams->cb, cbTbl, sizeof(cams->cb)); 702 memmove(&cams->cb, cbTbl, sizeof(cams->cb));
@@ -715,7 +714,7 @@ int usbvideo_register(
715 cams->md_module = md; 714 cams->md_module = md;
716 if (cams->md_module == NULL) 715 if (cams->md_module == NULL)
717 warn("%s: module == NULL!", __FUNCTION__); 716 warn("%s: module == NULL!", __FUNCTION__);
718 init_MUTEX(&cams->lock); /* to 1 == available */ 717 mutex_init(&cams->lock); /* to 1 == available */
719 718
720 for (i = 0; i < num_cams; i++) { 719 for (i = 0; i < num_cams; i++) {
721 struct uvd *up = &cams->cam[i]; 720 struct uvd *up = &cams->cam[i];
@@ -863,7 +862,7 @@ static void usbvideo_Disconnect(struct usb_interface *intf)
863 if (uvd->debug > 0) 862 if (uvd->debug > 0)
864 info("%s(%p.)", __FUNCTION__, intf); 863 info("%s(%p.)", __FUNCTION__, intf);
865 864
866 down(&uvd->lock); 865 mutex_lock(&uvd->lock);
867 uvd->remove_pending = 1; /* Now all ISO data will be ignored */ 866 uvd->remove_pending = 1; /* Now all ISO data will be ignored */
868 867
869 /* At this time we ask to cancel outstanding URBs */ 868 /* At this time we ask to cancel outstanding URBs */
@@ -883,7 +882,7 @@ static void usbvideo_Disconnect(struct usb_interface *intf)
883 info("%s: In use, disconnect pending.", __FUNCTION__); 882 info("%s: In use, disconnect pending.", __FUNCTION__);
884 else 883 else
885 usbvideo_CameraRelease(uvd); 884 usbvideo_CameraRelease(uvd);
886 up(&uvd->lock); 885 mutex_unlock(&uvd->lock);
887 info("USB camera disconnected."); 886 info("USB camera disconnected.");
888 887
889 usbvideo_ClientDecModCount(uvd); 888 usbvideo_ClientDecModCount(uvd);
@@ -930,19 +929,19 @@ static int usbvideo_find_struct(struct usbvideo *cams)
930 err("No usbvideo handle?"); 929 err("No usbvideo handle?");
931 return -1; 930 return -1;
932 } 931 }
933 down(&cams->lock); 932 mutex_lock(&cams->lock);
934 for (u = 0; u < cams->num_cameras; u++) { 933 for (u = 0; u < cams->num_cameras; u++) {
935 struct uvd *uvd = &cams->cam[u]; 934 struct uvd *uvd = &cams->cam[u];
936 if (!uvd->uvd_used) /* This one is free */ 935 if (!uvd->uvd_used) /* This one is free */
937 { 936 {
938 uvd->uvd_used = 1; /* In use now */ 937 uvd->uvd_used = 1; /* In use now */
939 init_MUTEX(&uvd->lock); /* to 1 == available */ 938 mutex_init(&uvd->lock); /* to 1 == available */
940 uvd->dev = NULL; 939 uvd->dev = NULL;
941 rv = u; 940 rv = u;
942 break; 941 break;
943 } 942 }
944 } 943 }
945 up(&cams->lock); 944 mutex_unlock(&cams->lock);
946 return rv; 945 return rv;
947} 946}
948 947
@@ -984,7 +983,7 @@ struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
984 /* Not relying upon caller we increase module counter ourselves */ 983 /* Not relying upon caller we increase module counter ourselves */
985 usbvideo_ClientIncModCount(uvd); 984 usbvideo_ClientIncModCount(uvd);
986 985
987 down(&uvd->lock); 986 mutex_lock(&uvd->lock);
988 for (i=0; i < USBVIDEO_NUMSBUF; i++) { 987 for (i=0; i < USBVIDEO_NUMSBUF; i++) {
989 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL); 988 uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
990 if (uvd->sbuf[i].urb == NULL) { 989 if (uvd->sbuf[i].urb == NULL) {
@@ -1007,7 +1006,7 @@ struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
1007 * return control to the client's probe function right now. 1006 * return control to the client's probe function right now.
1008 */ 1007 */
1009allocate_done: 1008allocate_done:
1010 up (&uvd->lock); 1009 mutex_unlock(&uvd->lock);
1011 usbvideo_ClientDecModCount(uvd); 1010 usbvideo_ClientDecModCount(uvd);
1012 return uvd; 1011 return uvd;
1013} 1012}
@@ -1121,7 +1120,7 @@ static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1121 info("%s($%p)", __FUNCTION__, dev); 1120 info("%s($%p)", __FUNCTION__, dev);
1122 1121
1123 usbvideo_ClientIncModCount(uvd); 1122 usbvideo_ClientIncModCount(uvd);
1124 down(&uvd->lock); 1123 mutex_lock(&uvd->lock);
1125 1124
1126 if (uvd->user) { 1125 if (uvd->user) {
1127 err("%s: Someone tried to open an already opened device!", __FUNCTION__); 1126 err("%s: Someone tried to open an already opened device!", __FUNCTION__);
@@ -1202,7 +1201,7 @@ static int usbvideo_v4l_open(struct inode *inode, struct file *file)
1202 } 1201 }
1203 } 1202 }
1204 } 1203 }
1205 up(&uvd->lock); 1204 mutex_unlock(&uvd->lock);
1206 if (errCode != 0) 1205 if (errCode != 0)
1207 usbvideo_ClientDecModCount(uvd); 1206 usbvideo_ClientDecModCount(uvd);
1208 if (uvd->debug > 0) 1207 if (uvd->debug > 0)
@@ -1231,7 +1230,7 @@ static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1231 if (uvd->debug > 1) 1230 if (uvd->debug > 1)
1232 info("%s($%p)", __FUNCTION__, dev); 1231 info("%s($%p)", __FUNCTION__, dev);
1233 1232
1234 down(&uvd->lock); 1233 mutex_lock(&uvd->lock);
1235 GET_CALLBACK(uvd, stopDataPump)(uvd); 1234 GET_CALLBACK(uvd, stopDataPump)(uvd);
1236 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size); 1235 usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
1237 uvd->fbuf = NULL; 1236 uvd->fbuf = NULL;
@@ -1252,7 +1251,7 @@ static int usbvideo_v4l_close(struct inode *inode, struct file *file)
1252 info("usbvideo_v4l_close: Final disconnect."); 1251 info("usbvideo_v4l_close: Final disconnect.");
1253 usbvideo_CameraRelease(uvd); 1252 usbvideo_CameraRelease(uvd);
1254 } 1253 }
1255 up(&uvd->lock); 1254 mutex_unlock(&uvd->lock);
1256 usbvideo_ClientDecModCount(uvd); 1255 usbvideo_ClientDecModCount(uvd);
1257 1256
1258 if (uvd->debug > 1) 1257 if (uvd->debug > 1)
@@ -1512,7 +1511,7 @@ static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1512 if (uvd->debug >= 1) 1511 if (uvd->debug >= 1)
1513 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock); 1512 info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock);
1514 1513
1515 down(&uvd->lock); 1514 mutex_lock(&uvd->lock);
1516 1515
1517 /* See if a frame is completed, then use it. */ 1516 /* See if a frame is completed, then use it. */
1518 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) { 1517 for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
@@ -1644,7 +1643,7 @@ static ssize_t usbvideo_v4l_read(struct file *file, char __user *buf,
1644 } 1643 }
1645 } 1644 }
1646read_done: 1645read_done:
1647 up(&uvd->lock); 1646 mutex_unlock(&uvd->lock);
1648 return count; 1647 return count;
1649} 1648}
1650 1649
diff --git a/drivers/usb/media/usbvideo.h b/drivers/usb/media/usbvideo.h
index 6c390a1f981b..135433c2680a 100644
--- a/drivers/usb/media/usbvideo.h
+++ b/drivers/usb/media/usbvideo.h
@@ -19,6 +19,7 @@
19#include <linux/config.h> 19#include <linux/config.h>
20#include <linux/videodev.h> 20#include <linux/videodev.h>
21#include <linux/usb.h> 21#include <linux/usb.h>
22#include <linux/mutex.h>
22 23
23/* Most helpful debugging aid */ 24/* Most helpful debugging aid */
24#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__)))) 25#define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
@@ -213,7 +214,7 @@ struct uvd {
213 unsigned long flags; /* FLAGS_USBVIDEO_xxx */ 214 unsigned long flags; /* FLAGS_USBVIDEO_xxx */
214 unsigned long paletteBits; /* Which palettes we accept? */ 215 unsigned long paletteBits; /* Which palettes we accept? */
215 unsigned short defaultPalette; /* What palette to use for read() */ 216 unsigned short defaultPalette; /* What palette to use for read() */
216 struct semaphore lock; 217 struct mutex lock;
217 int user; /* user count for exclusive use */ 218 int user; /* user count for exclusive use */
218 219
219 videosize_t videosize; /* Current setting */ 220 videosize_t videosize; /* Current setting */
@@ -272,7 +273,7 @@ struct usbvideo {
272 int num_cameras; /* As allocated */ 273 int num_cameras; /* As allocated */
273 struct usb_driver usbdrv; /* Interface to the USB stack */ 274 struct usb_driver usbdrv; /* Interface to the USB stack */
274 char drvName[80]; /* Driver name */ 275 char drvName[80]; /* Driver name */
275 struct semaphore lock; /* Mutex protecting camera structures */ 276 struct mutex lock; /* Mutex protecting camera structures */
276 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */ 277 struct usbvideo_cb cb; /* Table of callbacks (virtual methods) */
277 struct video_device vdt; /* Video device template */ 278 struct video_device vdt; /* Video device template */
278 struct uvd *cam; /* Array of camera structures */ 279 struct uvd *cam; /* Array of camera structures */
diff --git a/drivers/usb/media/vicam.c b/drivers/usb/media/vicam.c
index 5df144073871..1d06e53ec7c5 100644
--- a/drivers/usb/media/vicam.c
+++ b/drivers/usb/media/vicam.c
@@ -42,6 +42,7 @@
42#include <linux/vmalloc.h> 42#include <linux/vmalloc.h>
43#include <linux/slab.h> 43#include <linux/slab.h>
44#include <linux/proc_fs.h> 44#include <linux/proc_fs.h>
45#include <linux/mutex.h>
45#include "usbvideo.h" 46#include "usbvideo.h"
46 47
47// #define VICAM_DEBUG 48// #define VICAM_DEBUG
@@ -407,7 +408,7 @@ struct vicam_camera {
407 struct usb_device *udev; // usb device 408 struct usb_device *udev; // usb device
408 409
409 /* guard against simultaneous accesses to the camera */ 410 /* guard against simultaneous accesses to the camera */
410 struct semaphore cam_lock; 411 struct mutex cam_lock;
411 412
412 int is_initialized; 413 int is_initialized;
413 u8 open_count; 414 u8 open_count;
@@ -461,12 +462,12 @@ static int send_control_msg(struct vicam_camera *cam,
461 u16 size) 462 u16 size)
462{ 463{
463 int status = -ENODEV; 464 int status = -ENODEV;
464 down(&cam->cam_lock); 465 mutex_lock(&cam->cam_lock);
465 if (cam->udev) { 466 if (cam->udev) {
466 status = __send_control_msg(cam, request, value, 467 status = __send_control_msg(cam, request, value,
467 index, cp, size); 468 index, cp, size);
468 } 469 }
469 up(&cam->cam_lock); 470 mutex_unlock(&cam->cam_lock);
470 return status; 471 return status;
471} 472}
472static int 473static int
@@ -763,6 +764,7 @@ vicam_open(struct inode *inode, struct file *file)
763 if (!cam) { 764 if (!cam) {
764 printk(KERN_ERR 765 printk(KERN_ERR
765 "vicam video_device improperly initialized"); 766 "vicam video_device improperly initialized");
767 return -EINVAL;
766 } 768 }
767 769
768 /* the videodev_lock held above us protects us from 770 /* the videodev_lock held above us protects us from
@@ -831,13 +833,13 @@ vicam_close(struct inode *inode, struct file *file)
831 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES); 833 rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
832 kfree(cam->cntrlbuf); 834 kfree(cam->cntrlbuf);
833 835
834 down(&cam->cam_lock); 836 mutex_lock(&cam->cam_lock);
835 837
836 cam->open_count--; 838 cam->open_count--;
837 open_count = cam->open_count; 839 open_count = cam->open_count;
838 udev = cam->udev; 840 udev = cam->udev;
839 841
840 up(&cam->cam_lock); 842 mutex_unlock(&cam->cam_lock);
841 843
842 if (!open_count && !udev) { 844 if (!open_count && !udev) {
843 kfree(cam); 845 kfree(cam);
@@ -960,7 +962,7 @@ read_frame(struct vicam_camera *cam, int framenum)
960 request[8] = 0; 962 request[8] = 0;
961 // bytes 9-15 do not seem to affect exposure or image quality 963 // bytes 9-15 do not seem to affect exposure or image quality
962 964
963 down(&cam->cam_lock); 965 mutex_lock(&cam->cam_lock);
964 966
965 if (!cam->udev) { 967 if (!cam->udev) {
966 goto done; 968 goto done;
@@ -985,7 +987,7 @@ read_frame(struct vicam_camera *cam, int framenum)
985 } 987 }
986 988
987 done: 989 done:
988 up(&cam->cam_lock); 990 mutex_unlock(&cam->cam_lock);
989} 991}
990 992
991static ssize_t 993static ssize_t
@@ -1309,7 +1311,7 @@ vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
1309 1311
1310 cam->shutter_speed = 15; 1312 cam->shutter_speed = 15;
1311 1313
1312 init_MUTEX(&cam->cam_lock); 1314 mutex_init(&cam->cam_lock);
1313 1315
1314 memcpy(&cam->vdev, &vicam_template, 1316 memcpy(&cam->vdev, &vicam_template,
1315 sizeof (vicam_template)); 1317 sizeof (vicam_template));
@@ -1351,7 +1353,7 @@ vicam_disconnect(struct usb_interface *intf)
1351 1353
1352 /* stop the camera from being used */ 1354 /* stop the camera from being used */
1353 1355
1354 down(&cam->cam_lock); 1356 mutex_lock(&cam->cam_lock);
1355 1357
1356 /* mark the camera as gone */ 1358 /* mark the camera as gone */
1357 1359
@@ -1368,7 +1370,7 @@ vicam_disconnect(struct usb_interface *intf)
1368 1370
1369 open_count = cam->open_count; 1371 open_count = cam->open_count;
1370 1372
1371 up(&cam->cam_lock); 1373 mutex_unlock(&cam->cam_lock);
1372 1374
1373 if (!open_count) { 1375 if (!open_count) {
1374 kfree(cam); 1376 kfree(cam);
diff --git a/drivers/usb/media/w9968cf.c b/drivers/usb/media/w9968cf.c
index 9937fc64c8bf..b57dec3782e0 100644
--- a/drivers/usb/media/w9968cf.c
+++ b/drivers/usb/media/w9968cf.c
@@ -47,6 +47,13 @@
47#include "w9968cf.h" 47#include "w9968cf.h"
48#include "w9968cf_decoder.h" 48#include "w9968cf_decoder.h"
49 49
50static struct w9968cf_vpp_t* w9968cf_vpp;
51static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
52
53static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
54static DEFINE_MUTEX(w9968cf_devlist_mutex); /* semaphore for list traversal */
55
56static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
50 57
51 58
52/**************************************************************************** 59/****************************************************************************
@@ -695,13 +702,12 @@ static int w9968cf_allocate_memory(struct w9968cf_device* cam)
695 /* Allocate memory for the isochronous transfer buffers */ 702 /* Allocate memory for the isochronous transfer buffers */
696 for (i = 0; i < W9968CF_URBS; i++) { 703 for (i = 0; i < W9968CF_URBS; i++) {
697 if (!(cam->transfer_buffer[i] = 704 if (!(cam->transfer_buffer[i] =
698 kmalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) { 705 kzalloc(W9968CF_ISO_PACKETS*p_size, GFP_KERNEL))) {
699 DBG(1, "Couldn't allocate memory for the isochronous " 706 DBG(1, "Couldn't allocate memory for the isochronous "
700 "transfer buffers (%u bytes)", 707 "transfer buffers (%u bytes)",
701 p_size * W9968CF_ISO_PACKETS) 708 p_size * W9968CF_ISO_PACKETS)
702 return -ENOMEM; 709 return -ENOMEM;
703 } 710 }
704 memset(cam->transfer_buffer[i], 0, W9968CF_ISO_PACKETS*p_size);
705 } 711 }
706 712
707 /* Allocate memory for the temporary frame buffer */ 713 /* Allocate memory for the temporary frame buffer */
@@ -2419,7 +2425,7 @@ w9968cf_configure_camera(struct w9968cf_device* cam,
2419 enum w9968cf_model_id mod_id, 2425 enum w9968cf_model_id mod_id,
2420 const unsigned short dev_nr) 2426 const unsigned short dev_nr)
2421{ 2427{
2422 init_MUTEX(&cam->fileop_sem); 2428 mutex_init(&cam->fileop_mutex);
2423 init_waitqueue_head(&cam->open); 2429 init_waitqueue_head(&cam->open);
2424 spin_lock_init(&cam->urb_lock); 2430 spin_lock_init(&cam->urb_lock);
2425 spin_lock_init(&cam->flist_lock); 2431 spin_lock_init(&cam->flist_lock);
@@ -2647,7 +2653,7 @@ static void w9968cf_adjust_configuration(struct w9968cf_device* cam)
2647 --------------------------------------------------------------------------*/ 2653 --------------------------------------------------------------------------*/
2648static void w9968cf_release_resources(struct w9968cf_device* cam) 2654static void w9968cf_release_resources(struct w9968cf_device* cam)
2649{ 2655{
2650 down(&w9968cf_devlist_sem); 2656 mutex_lock(&w9968cf_devlist_mutex);
2651 2657
2652 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor) 2658 DBG(2, "V4L device deregistered: /dev/video%d", cam->v4ldev->minor)
2653 2659
@@ -2658,7 +2664,7 @@ static void w9968cf_release_resources(struct w9968cf_device* cam)
2658 kfree(cam->control_buffer); 2664 kfree(cam->control_buffer);
2659 kfree(cam->data_buffer); 2665 kfree(cam->data_buffer);
2660 2666
2661 up(&w9968cf_devlist_sem); 2667 mutex_unlock(&w9968cf_devlist_mutex);
2662} 2668}
2663 2669
2664 2670
@@ -2678,14 +2684,14 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2678 2684
2679 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2685 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2680 2686
2681 down(&cam->dev_sem); 2687 mutex_lock(&cam->dev_mutex);
2682 2688
2683 if (cam->sensor == CC_UNKNOWN) { 2689 if (cam->sensor == CC_UNKNOWN) {
2684 DBG(2, "No supported image sensor has been detected by the " 2690 DBG(2, "No supported image sensor has been detected by the "
2685 "'ovcamchip' module for the %s (/dev/video%d). Make " 2691 "'ovcamchip' module for the %s (/dev/video%d). Make "
2686 "sure it is loaded *before* (re)connecting the camera.", 2692 "sure it is loaded *before* (re)connecting the camera.",
2687 symbolic(camlist, cam->id), cam->v4ldev->minor) 2693 symbolic(camlist, cam->id), cam->v4ldev->minor)
2688 up(&cam->dev_sem); 2694 mutex_unlock(&cam->dev_mutex);
2689 up_read(&w9968cf_disconnect); 2695 up_read(&w9968cf_disconnect);
2690 return -ENODEV; 2696 return -ENODEV;
2691 } 2697 }
@@ -2694,11 +2700,11 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2694 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'", 2700 DBG(2, "%s (/dev/video%d) has been already occupied by '%s'",
2695 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command) 2701 symbolic(camlist, cam->id),cam->v4ldev->minor,cam->command)
2696 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) { 2702 if ((filp->f_flags & O_NONBLOCK)||(filp->f_flags & O_NDELAY)) {
2697 up(&cam->dev_sem); 2703 mutex_unlock(&cam->dev_mutex);
2698 up_read(&w9968cf_disconnect); 2704 up_read(&w9968cf_disconnect);
2699 return -EWOULDBLOCK; 2705 return -EWOULDBLOCK;
2700 } 2706 }
2701 up(&cam->dev_sem); 2707 mutex_unlock(&cam->dev_mutex);
2702 err = wait_event_interruptible_exclusive(cam->open, 2708 err = wait_event_interruptible_exclusive(cam->open,
2703 cam->disconnected || 2709 cam->disconnected ||
2704 !cam->users); 2710 !cam->users);
@@ -2710,7 +2716,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2710 up_read(&w9968cf_disconnect); 2716 up_read(&w9968cf_disconnect);
2711 return -ENODEV; 2717 return -ENODEV;
2712 } 2718 }
2713 down(&cam->dev_sem); 2719 mutex_lock(&cam->dev_mutex);
2714 } 2720 }
2715 2721
2716 DBG(5, "Opening '%s', /dev/video%d ...", 2722 DBG(5, "Opening '%s', /dev/video%d ...",
@@ -2739,7 +2745,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2739 2745
2740 DBG(5, "Video device is open") 2746 DBG(5, "Video device is open")
2741 2747
2742 up(&cam->dev_sem); 2748 mutex_unlock(&cam->dev_mutex);
2743 up_read(&w9968cf_disconnect); 2749 up_read(&w9968cf_disconnect);
2744 2750
2745 return 0; 2751 return 0;
@@ -2747,7 +2753,7 @@ static int w9968cf_open(struct inode* inode, struct file* filp)
2747deallocate_memory: 2753deallocate_memory:
2748 w9968cf_deallocate_memory(cam); 2754 w9968cf_deallocate_memory(cam);
2749 DBG(2, "Failed to open the video device") 2755 DBG(2, "Failed to open the video device")
2750 up(&cam->dev_sem); 2756 mutex_unlock(&cam->dev_mutex);
2751 up_read(&w9968cf_disconnect); 2757 up_read(&w9968cf_disconnect);
2752 return err; 2758 return err;
2753} 2759}
@@ -2759,13 +2765,13 @@ static int w9968cf_release(struct inode* inode, struct file* filp)
2759 2765
2760 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2766 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2761 2767
2762 down(&cam->dev_sem); /* prevent disconnect() to be called */ 2768 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
2763 2769
2764 w9968cf_stop_transfer(cam); 2770 w9968cf_stop_transfer(cam);
2765 2771
2766 if (cam->disconnected) { 2772 if (cam->disconnected) {
2767 w9968cf_release_resources(cam); 2773 w9968cf_release_resources(cam);
2768 up(&cam->dev_sem); 2774 mutex_unlock(&cam->dev_mutex);
2769 kfree(cam); 2775 kfree(cam);
2770 return 0; 2776 return 0;
2771 } 2777 }
@@ -2775,7 +2781,7 @@ static int w9968cf_release(struct inode* inode, struct file* filp)
2775 wake_up_interruptible_nr(&cam->open, 1); 2781 wake_up_interruptible_nr(&cam->open, 1);
2776 2782
2777 DBG(5, "Video device closed") 2783 DBG(5, "Video device closed")
2778 up(&cam->dev_sem); 2784 mutex_unlock(&cam->dev_mutex);
2779 return 0; 2785 return 0;
2780} 2786}
2781 2787
@@ -2792,18 +2798,18 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2792 if (filp->f_flags & O_NONBLOCK) 2798 if (filp->f_flags & O_NONBLOCK)
2793 return -EWOULDBLOCK; 2799 return -EWOULDBLOCK;
2794 2800
2795 if (down_interruptible(&cam->fileop_sem)) 2801 if (mutex_lock_interruptible(&cam->fileop_mutex))
2796 return -ERESTARTSYS; 2802 return -ERESTARTSYS;
2797 2803
2798 if (cam->disconnected) { 2804 if (cam->disconnected) {
2799 DBG(2, "Device not present") 2805 DBG(2, "Device not present")
2800 up(&cam->fileop_sem); 2806 mutex_unlock(&cam->fileop_mutex);
2801 return -ENODEV; 2807 return -ENODEV;
2802 } 2808 }
2803 2809
2804 if (cam->misconfigured) { 2810 if (cam->misconfigured) {
2805 DBG(2, "The camera is misconfigured. Close and open it again.") 2811 DBG(2, "The camera is misconfigured. Close and open it again.")
2806 up(&cam->fileop_sem); 2812 mutex_unlock(&cam->fileop_mutex);
2807 return -EIO; 2813 return -EIO;
2808 } 2814 }
2809 2815
@@ -2818,11 +2824,11 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2818 cam->frame[1].status == F_READY || 2824 cam->frame[1].status == F_READY ||
2819 cam->disconnected); 2825 cam->disconnected);
2820 if (err) { 2826 if (err) {
2821 up(&cam->fileop_sem); 2827 mutex_unlock(&cam->fileop_mutex);
2822 return err; 2828 return err;
2823 } 2829 }
2824 if (cam->disconnected) { 2830 if (cam->disconnected) {
2825 up(&cam->fileop_sem); 2831 mutex_unlock(&cam->fileop_mutex);
2826 return -ENODEV; 2832 return -ENODEV;
2827 } 2833 }
2828 2834
@@ -2836,7 +2842,7 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2836 2842
2837 if (copy_to_user(buf, fr->buffer, count)) { 2843 if (copy_to_user(buf, fr->buffer, count)) {
2838 fr->status = F_UNUSED; 2844 fr->status = F_UNUSED;
2839 up(&cam->fileop_sem); 2845 mutex_unlock(&cam->fileop_mutex);
2840 return -EFAULT; 2846 return -EFAULT;
2841 } 2847 }
2842 *f_pos += count; 2848 *f_pos += count;
@@ -2845,7 +2851,7 @@ w9968cf_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
2845 2851
2846 DBG(5, "%zu bytes read", count) 2852 DBG(5, "%zu bytes read", count)
2847 2853
2848 up(&cam->fileop_sem); 2854 mutex_unlock(&cam->fileop_mutex);
2849 return count; 2855 return count;
2850} 2856}
2851 2857
@@ -2899,24 +2905,24 @@ w9968cf_ioctl(struct inode* inode, struct file* filp,
2899 2905
2900 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp)); 2906 cam = (struct w9968cf_device*)video_get_drvdata(video_devdata(filp));
2901 2907
2902 if (down_interruptible(&cam->fileop_sem)) 2908 if (mutex_lock_interruptible(&cam->fileop_mutex))
2903 return -ERESTARTSYS; 2909 return -ERESTARTSYS;
2904 2910
2905 if (cam->disconnected) { 2911 if (cam->disconnected) {
2906 DBG(2, "Device not present") 2912 DBG(2, "Device not present")
2907 up(&cam->fileop_sem); 2913 mutex_unlock(&cam->fileop_mutex);
2908 return -ENODEV; 2914 return -ENODEV;
2909 } 2915 }
2910 2916
2911 if (cam->misconfigured) { 2917 if (cam->misconfigured) {
2912 DBG(2, "The camera is misconfigured. Close and open it again.") 2918 DBG(2, "The camera is misconfigured. Close and open it again.")
2913 up(&cam->fileop_sem); 2919 mutex_unlock(&cam->fileop_mutex);
2914 return -EIO; 2920 return -EIO;
2915 } 2921 }
2916 2922
2917 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg); 2923 err = w9968cf_v4l_ioctl(inode, filp, cmd, (void __user *)arg);
2918 2924
2919 up(&cam->fileop_sem); 2925 mutex_unlock(&cam->fileop_mutex);
2920 return err; 2926 return err;
2921} 2927}
2922 2928
@@ -3499,14 +3505,12 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3499 return -ENODEV; 3505 return -ENODEV;
3500 3506
3501 cam = (struct w9968cf_device*) 3507 cam = (struct w9968cf_device*)
3502 kmalloc(sizeof(struct w9968cf_device), GFP_KERNEL); 3508 kzalloc(sizeof(struct w9968cf_device), GFP_KERNEL);
3503 if (!cam) 3509 if (!cam)
3504 return -ENOMEM; 3510 return -ENOMEM;
3505 3511
3506 memset(cam, 0, sizeof(*cam)); 3512 mutex_init(&cam->dev_mutex);
3507 3513 mutex_lock(&cam->dev_mutex);
3508 init_MUTEX(&cam->dev_sem);
3509 down(&cam->dev_sem);
3510 3514
3511 cam->usbdev = udev; 3515 cam->usbdev = udev;
3512 /* NOTE: a local copy is used to avoid possible race conditions */ 3516 /* NOTE: a local copy is used to avoid possible race conditions */
@@ -3518,10 +3522,10 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3518 simcams = W9968CF_SIMCAMS; 3522 simcams = W9968CF_SIMCAMS;
3519 3523
3520 /* How many cameras are connected ? */ 3524 /* How many cameras are connected ? */
3521 down(&w9968cf_devlist_sem); 3525 mutex_lock(&w9968cf_devlist_mutex);
3522 list_for_each(ptr, &w9968cf_dev_list) 3526 list_for_each(ptr, &w9968cf_dev_list)
3523 sc++; 3527 sc++;
3524 up(&w9968cf_devlist_sem); 3528 mutex_unlock(&w9968cf_devlist_mutex);
3525 3529
3526 if (sc >= simcams) { 3530 if (sc >= simcams) {
3527 DBG(2, "Device rejected: too many connected cameras " 3531 DBG(2, "Device rejected: too many connected cameras "
@@ -3532,21 +3536,19 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3532 3536
3533 3537
3534 /* Allocate 2 bytes of memory for camera control USB transfers */ 3538 /* Allocate 2 bytes of memory for camera control USB transfers */
3535 if (!(cam->control_buffer = kmalloc(2, GFP_KERNEL))) { 3539 if (!(cam->control_buffer = kzalloc(2, GFP_KERNEL))) {
3536 DBG(1,"Couldn't allocate memory for camera control transfers") 3540 DBG(1,"Couldn't allocate memory for camera control transfers")
3537 err = -ENOMEM; 3541 err = -ENOMEM;
3538 goto fail; 3542 goto fail;
3539 } 3543 }
3540 memset(cam->control_buffer, 0, 2);
3541 3544
3542 /* Allocate 8 bytes of memory for USB data transfers to the FSB */ 3545 /* Allocate 8 bytes of memory for USB data transfers to the FSB */
3543 if (!(cam->data_buffer = kmalloc(8, GFP_KERNEL))) { 3546 if (!(cam->data_buffer = kzalloc(8, GFP_KERNEL))) {
3544 DBG(1, "Couldn't allocate memory for data " 3547 DBG(1, "Couldn't allocate memory for data "
3545 "transfers to the FSB") 3548 "transfers to the FSB")
3546 err = -ENOMEM; 3549 err = -ENOMEM;
3547 goto fail; 3550 goto fail;
3548 } 3551 }
3549 memset(cam->data_buffer, 0, 8);
3550 3552
3551 /* Register the V4L device */ 3553 /* Register the V4L device */
3552 cam->v4ldev = video_device_alloc(); 3554 cam->v4ldev = video_device_alloc();
@@ -3583,9 +3585,9 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3583 w9968cf_configure_camera(cam, udev, mod_id, dev_nr); 3585 w9968cf_configure_camera(cam, udev, mod_id, dev_nr);
3584 3586
3585 /* Add a new entry into the list of V4L registered devices */ 3587 /* Add a new entry into the list of V4L registered devices */
3586 down(&w9968cf_devlist_sem); 3588 mutex_lock(&w9968cf_devlist_mutex);
3587 list_add(&cam->v4llist, &w9968cf_dev_list); 3589 list_add(&cam->v4llist, &w9968cf_dev_list);
3588 up(&w9968cf_devlist_sem); 3590 mutex_unlock(&w9968cf_devlist_mutex);
3589 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0; 3591 dev_nr = (dev_nr < W9968CF_MAX_DEVICES-1) ? dev_nr+1 : 0;
3590 3592
3591 w9968cf_turn_on_led(cam); 3593 w9968cf_turn_on_led(cam);
@@ -3593,7 +3595,7 @@ w9968cf_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
3593 w9968cf_i2c_init(cam); 3595 w9968cf_i2c_init(cam);
3594 3596
3595 usb_set_intfdata(intf, cam); 3597 usb_set_intfdata(intf, cam);
3596 up(&cam->dev_sem); 3598 mutex_unlock(&cam->dev_mutex);
3597 return 0; 3599 return 0;
3598 3600
3599fail: /* Free unused memory */ 3601fail: /* Free unused memory */
@@ -3601,7 +3603,7 @@ fail: /* Free unused memory */
3601 kfree(cam->data_buffer); 3603 kfree(cam->data_buffer);
3602 if (cam->v4ldev) 3604 if (cam->v4ldev)
3603 video_device_release(cam->v4ldev); 3605 video_device_release(cam->v4ldev);
3604 up(&cam->dev_sem); 3606 mutex_unlock(&cam->dev_mutex);
3605 kfree(cam); 3607 kfree(cam);
3606 return err; 3608 return err;
3607} 3609}
@@ -3616,7 +3618,7 @@ static void w9968cf_usb_disconnect(struct usb_interface* intf)
3616 3618
3617 if (cam) { 3619 if (cam) {
3618 /* Prevent concurrent accesses to data */ 3620 /* Prevent concurrent accesses to data */
3619 down(&cam->dev_sem); 3621 mutex_lock(&cam->dev_mutex);
3620 3622
3621 cam->disconnected = 1; 3623 cam->disconnected = 1;
3622 3624
@@ -3635,7 +3637,7 @@ static void w9968cf_usb_disconnect(struct usb_interface* intf)
3635 } else 3637 } else
3636 w9968cf_release_resources(cam); 3638 w9968cf_release_resources(cam);
3637 3639
3638 up(&cam->dev_sem); 3640 mutex_unlock(&cam->dev_mutex);
3639 3641
3640 if (!cam->users) 3642 if (!cam->users)
3641 kfree(cam); 3643 kfree(cam);
diff --git a/drivers/usb/media/w9968cf.h b/drivers/usb/media/w9968cf.h
index 47a6ff794171..a87be719a281 100644
--- a/drivers/usb/media/w9968cf.h
+++ b/drivers/usb/media/w9968cf.h
@@ -32,7 +32,7 @@
32#include <linux/param.h> 32#include <linux/param.h>
33#include <linux/types.h> 33#include <linux/types.h>
34#include <linux/rwsem.h> 34#include <linux/rwsem.h>
35#include <asm/semaphore.h> 35#include <linux/mutex.h>
36 36
37#include <media/ovcamchip.h> 37#include <media/ovcamchip.h>
38 38
@@ -194,14 +194,6 @@ enum w9968cf_vpp_flag {
194 VPP_UYVY_TO_RGBX = 0x08, 194 VPP_UYVY_TO_RGBX = 0x08,
195}; 195};
196 196
197static struct w9968cf_vpp_t* w9968cf_vpp;
198static DECLARE_WAIT_QUEUE_HEAD(w9968cf_vppmod_wait);
199
200static LIST_HEAD(w9968cf_dev_list); /* head of V4L registered cameras list */
201static DECLARE_MUTEX(w9968cf_devlist_sem); /* semaphore for list traversal */
202
203static DECLARE_RWSEM(w9968cf_disconnect); /* prevent races with open() */
204
205/* Main device driver structure */ 197/* Main device driver structure */
206struct w9968cf_device { 198struct w9968cf_device {
207 struct device dev; /* device structure */ 199 struct device dev; /* device structure */
@@ -277,8 +269,8 @@ struct w9968cf_device {
277 struct i2c_client* sensor_client; 269 struct i2c_client* sensor_client;
278 270
279 /* Locks */ 271 /* Locks */
280 struct semaphore dev_sem, /* for probe, disconnect,open and close */ 272 struct mutex dev_mutex, /* for probe, disconnect,open and close */
281 fileop_sem; /* for read and ioctl */ 273 fileop_mutex; /* for read and ioctl */
282 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */ 274 spinlock_t urb_lock, /* for submit_urb() and unlink_urb() */
283 flist_lock; /* for requested frame list accesses */ 275 flist_lock; /* for requested frame list accesses */
284 wait_queue_head_t open, wait_queue; 276 wait_queue_head_t open, wait_queue;
diff --git a/drivers/usb/media/zc0301.h b/drivers/usb/media/zc0301.h
new file mode 100644
index 000000000000..8e0655140e60
--- /dev/null
+++ b/drivers/usb/media/zc0301.h
@@ -0,0 +1,192 @@
1/***************************************************************************
2 * V4L2 driver for ZC0301 Image Processor and Control Chip *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
19 ***************************************************************************/
20
21#ifndef _ZC0301_H_
22#define _ZC0301_H_
23
24#include <linux/version.h>
25#include <linux/usb.h>
26#include <linux/videodev2.h>
27#include <media/v4l2-common.h>
28#include <linux/device.h>
29#include <linux/list.h>
30#include <linux/spinlock.h>
31#include <linux/time.h>
32#include <linux/wait.h>
33#include <linux/types.h>
34#include <linux/param.h>
35#include <linux/mutex.h>
36#include <linux/rwsem.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
39
40#include "zc0301_sensor.h"
41
42/*****************************************************************************/
43
44#define ZC0301_DEBUG
45#define ZC0301_DEBUG_LEVEL 2
46#define ZC0301_MAX_DEVICES 64
47#define ZC0301_FORCE_MUNMAP 0
48#define ZC0301_MAX_FRAMES 32
49#define ZC0301_COMPRESSION_QUALITY 0
50#define ZC0301_URBS 2
51#define ZC0301_ISO_PACKETS 7
52#define ZC0301_ALTERNATE_SETTING 7
53#define ZC0301_URB_TIMEOUT msecs_to_jiffies(2 * ZC0301_ISO_PACKETS)
54#define ZC0301_CTRL_TIMEOUT 100
55#define ZC0301_FRAME_TIMEOUT 2
56
57/*****************************************************************************/
58
59ZC0301_ID_TABLE
60ZC0301_SENSOR_TABLE
61
62enum zc0301_frame_state {
63 F_UNUSED,
64 F_QUEUED,
65 F_GRABBING,
66 F_DONE,
67 F_ERROR,
68};
69
70struct zc0301_frame_t {
71 void* bufmem;
72 struct v4l2_buffer buf;
73 enum zc0301_frame_state state;
74 struct list_head frame;
75 unsigned long vma_use_count;
76};
77
78enum zc0301_dev_state {
79 DEV_INITIALIZED = 0x01,
80 DEV_DISCONNECTED = 0x02,
81 DEV_MISCONFIGURED = 0x04,
82};
83
84enum zc0301_io_method {
85 IO_NONE,
86 IO_READ,
87 IO_MMAP,
88};
89
90enum zc0301_stream_state {
91 STREAM_OFF,
92 STREAM_INTERRUPT,
93 STREAM_ON,
94};
95
96struct zc0301_module_param {
97 u8 force_munmap;
98 u16 frame_timeout;
99};
100
101static DECLARE_RWSEM(zc0301_disconnect);
102
103struct zc0301_device {
104 struct video_device* v4ldev;
105
106 struct zc0301_sensor sensor;
107
108 struct usb_device* usbdev;
109 struct urb* urb[ZC0301_URBS];
110 void* transfer_buffer[ZC0301_URBS];
111 u8* control_buffer;
112
113 struct zc0301_frame_t *frame_current, frame[ZC0301_MAX_FRAMES];
114 struct list_head inqueue, outqueue;
115 u32 frame_count, nbuffers, nreadbuffers;
116
117 enum zc0301_io_method io;
118 enum zc0301_stream_state stream;
119
120 struct v4l2_jpegcompression compression;
121
122 struct zc0301_module_param module_param;
123
124 enum zc0301_dev_state state;
125 u8 users;
126
127 struct mutex dev_mutex, fileop_mutex;
128 spinlock_t queue_lock;
129 wait_queue_head_t open, wait_frame, wait_stream;
130};
131
132/*****************************************************************************/
133
134struct zc0301_device*
135zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id)
136{
137 return usb_match_id(usb_ifnum_to_if(cam->usbdev, 0), id) ? cam : NULL;
138}
139
140void
141zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor)
142{
143 memcpy(&cam->sensor, sensor, sizeof(struct zc0301_sensor));
144}
145
146/*****************************************************************************/
147
148#undef DBG
149#undef KDBG
150#ifdef ZC0301_DEBUG
151# define DBG(level, fmt, args...) \
152do { \
153 if (debug >= (level)) { \
154 if ((level) == 1) \
155 dev_err(&cam->usbdev->dev, fmt "\n", ## args); \
156 else if ((level) == 2) \
157 dev_info(&cam->usbdev->dev, fmt "\n", ## args); \
158 else if ((level) >= 3) \
159 dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
160 __FUNCTION__, __LINE__ , ## args); \
161 } \
162} while (0)
163# define KDBG(level, fmt, args...) \
164do { \
165 if (debug >= (level)) { \
166 if ((level) == 1 || (level) == 2) \
167 pr_info("zc0301: " fmt "\n", ## args); \
168 else if ((level) == 3) \
169 pr_debug("zc0301: [%s:%d] " fmt "\n", __FUNCTION__, \
170 __LINE__ , ## args); \
171 } \
172} while (0)
173# define V4LDBG(level, name, cmd) \
174do { \
175 if (debug >= (level)) \
176 v4l_print_ioctl(name, cmd); \
177} while (0)
178#else
179# define DBG(level, fmt, args...) do {;} while(0)
180# define KDBG(level, fmt, args...) do {;} while(0)
181# define V4LDBG(level, name, cmd) do {;} while(0)
182#endif
183
184#undef PDBG
185#define PDBG(fmt, args...) \
186dev_info(&cam->usbdev->dev, "[%s:%d] " fmt "\n", \
187 __FUNCTION__, __LINE__ , ## args)
188
189#undef PDBGG
190#define PDBGG(fmt, args...) do {;} while(0) /* placeholder */
191
192#endif /* _ZC0301_H_ */
diff --git a/drivers/usb/media/zc0301_core.c b/drivers/usb/media/zc0301_core.c
new file mode 100644
index 000000000000..4036c6268bff
--- /dev/null
+++ b/drivers/usb/media/zc0301_core.c
@@ -0,0 +1,2055 @@
1/***************************************************************************
2 * Video4Linux2 driver for ZC0301 Image Processor and Control Chip *
3 * *
4 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
5 * *
6 * Informations about the chip internals needed to enable the I2C protocol *
7 * have been taken from the documentation of the ZC030x Video4Linux1 *
8 * driver written by Andrew Birkett <andy@nobugs.org> *
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 as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the Free Software *
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 ***************************************************************************/
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/param.h>
29#include <linux/moduleparam.h>
30#include <linux/errno.h>
31#include <linux/slab.h>
32#include <linux/device.h>
33#include <linux/fs.h>
34#include <linux/delay.h>
35#include <linux/compiler.h>
36#include <linux/ioctl.h>
37#include <linux/poll.h>
38#include <linux/stat.h>
39#include <linux/mm.h>
40#include <linux/vmalloc.h>
41#include <linux/page-flags.h>
42#include <linux/byteorder/generic.h>
43#include <asm/page.h>
44#include <asm/uaccess.h>
45
46#include "zc0301.h"
47
48/*****************************************************************************/
49
50#define ZC0301_MODULE_NAME "V4L2 driver for ZC0301 " \
51 "Image Processor and Control Chip"
52#define ZC0301_MODULE_AUTHOR "(C) 2006 Luca Risolia"
53#define ZC0301_AUTHOR_EMAIL "<luca.risolia@studio.unibo.it>"
54#define ZC0301_MODULE_LICENSE "GPL"
55#define ZC0301_MODULE_VERSION "1:1.03"
56#define ZC0301_MODULE_VERSION_CODE KERNEL_VERSION(1, 0, 3)
57
58/*****************************************************************************/
59
60MODULE_DEVICE_TABLE(usb, zc0301_id_table);
61
62MODULE_AUTHOR(ZC0301_MODULE_AUTHOR " " ZC0301_AUTHOR_EMAIL);
63MODULE_DESCRIPTION(ZC0301_MODULE_NAME);
64MODULE_VERSION(ZC0301_MODULE_VERSION);
65MODULE_LICENSE(ZC0301_MODULE_LICENSE);
66
67static short video_nr[] = {[0 ... ZC0301_MAX_DEVICES-1] = -1};
68module_param_array(video_nr, short, NULL, 0444);
69MODULE_PARM_DESC(video_nr,
70 "\n<-1|n[,...]> Specify V4L2 minor mode number."
71 "\n -1 = use next available (default)"
72 "\n n = use minor number n (integer >= 0)"
73 "\nYou can specify up to "
74 __MODULE_STRING(ZC0301_MAX_DEVICES) " cameras this way."
75 "\nFor example:"
76 "\nvideo_nr=-1,2,-1 would assign minor number 2 to"
77 "\nthe second registered camera and use auto for the first"
78 "\none and for every other camera."
79 "\n");
80
81static short force_munmap[] = {[0 ... ZC0301_MAX_DEVICES-1] =
82 ZC0301_FORCE_MUNMAP};
83module_param_array(force_munmap, bool, NULL, 0444);
84MODULE_PARM_DESC(force_munmap,
85 "\n<0|1[,...]> Force the application to unmap previously"
86 "\nmapped buffer memory before calling any VIDIOC_S_CROP or"
87 "\nVIDIOC_S_FMT ioctl's. Not all the applications support"
88 "\nthis feature. This parameter is specific for each"
89 "\ndetected camera."
90 "\n 0 = do not force memory unmapping"
91 "\n 1 = force memory unmapping (save memory)"
92 "\nDefault value is "__MODULE_STRING(SN9C102_FORCE_MUNMAP)"."
93 "\n");
94
95static unsigned int frame_timeout[] = {[0 ... ZC0301_MAX_DEVICES-1] =
96 ZC0301_FRAME_TIMEOUT};
97module_param_array(frame_timeout, uint, NULL, 0644);
98MODULE_PARM_DESC(frame_timeout,
99 "\n<n[,...]> Timeout for a video frame in seconds."
100 "\nThis parameter is specific for each detected camera."
101 "\nDefault value is "__MODULE_STRING(ZC0301_FRAME_TIMEOUT)"."
102 "\n");
103
104#ifdef ZC0301_DEBUG
105static unsigned short debug = ZC0301_DEBUG_LEVEL;
106module_param(debug, ushort, 0644);
107MODULE_PARM_DESC(debug,
108 "\n<n> Debugging information level, from 0 to 3:"
109 "\n0 = none (use carefully)"
110 "\n1 = critical errors"
111 "\n2 = significant informations"
112 "\n3 = more verbose messages"
113 "\nLevel 3 is useful for testing only, when only "
114 "one device is used."
115 "\nDefault value is "__MODULE_STRING(ZC0301_DEBUG_LEVEL)"."
116 "\n");
117#endif
118
119/*****************************************************************************/
120
121static u32
122zc0301_request_buffers(struct zc0301_device* cam, u32 count,
123 enum zc0301_io_method io)
124{
125 struct v4l2_pix_format* p = &(cam->sensor.pix_format);
126 struct v4l2_rect* r = &(cam->sensor.cropcap.bounds);
127 const size_t imagesize = cam->module_param.force_munmap ||
128 io == IO_READ ?
129 (p->width * p->height * p->priv) / 8 :
130 (r->width * r->height * p->priv) / 8;
131 void* buff = NULL;
132 u32 i;
133
134 if (count > ZC0301_MAX_FRAMES)
135 count = ZC0301_MAX_FRAMES;
136
137 cam->nbuffers = count;
138 while (cam->nbuffers > 0) {
139 if ((buff = vmalloc_32(cam->nbuffers * PAGE_ALIGN(imagesize))))
140 break;
141 cam->nbuffers--;
142 }
143
144 for (i = 0; i < cam->nbuffers; i++) {
145 cam->frame[i].bufmem = buff + i*PAGE_ALIGN(imagesize);
146 cam->frame[i].buf.index = i;
147 cam->frame[i].buf.m.offset = i*PAGE_ALIGN(imagesize);
148 cam->frame[i].buf.length = imagesize;
149 cam->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 cam->frame[i].buf.sequence = 0;
151 cam->frame[i].buf.field = V4L2_FIELD_NONE;
152 cam->frame[i].buf.memory = V4L2_MEMORY_MMAP;
153 cam->frame[i].buf.flags = 0;
154 }
155
156 return cam->nbuffers;
157}
158
159
160static void zc0301_release_buffers(struct zc0301_device* cam)
161{
162 if (cam->nbuffers) {
163 vfree(cam->frame[0].bufmem);
164 cam->nbuffers = 0;
165 }
166 cam->frame_current = NULL;
167}
168
169
170static void zc0301_empty_framequeues(struct zc0301_device* cam)
171{
172 u32 i;
173
174 INIT_LIST_HEAD(&cam->inqueue);
175 INIT_LIST_HEAD(&cam->outqueue);
176
177 for (i = 0; i < ZC0301_MAX_FRAMES; i++) {
178 cam->frame[i].state = F_UNUSED;
179 cam->frame[i].buf.bytesused = 0;
180 }
181}
182
183
184static void zc0301_requeue_outqueue(struct zc0301_device* cam)
185{
186 struct zc0301_frame_t *i;
187
188 list_for_each_entry(i, &cam->outqueue, frame) {
189 i->state = F_QUEUED;
190 list_add(&i->frame, &cam->inqueue);
191 }
192
193 INIT_LIST_HEAD(&cam->outqueue);
194}
195
196
197static void zc0301_queue_unusedframes(struct zc0301_device* cam)
198{
199 unsigned long lock_flags;
200 u32 i;
201
202 for (i = 0; i < cam->nbuffers; i++)
203 if (cam->frame[i].state == F_UNUSED) {
204 cam->frame[i].state = F_QUEUED;
205 spin_lock_irqsave(&cam->queue_lock, lock_flags);
206 list_add_tail(&cam->frame[i].frame, &cam->inqueue);
207 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
208 }
209}
210
211/*****************************************************************************/
212
213int zc0301_write_reg(struct zc0301_device* cam, u16 index, u16 value)
214{
215 struct usb_device* udev = cam->usbdev;
216 int res;
217
218 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0xa0, 0x40,
219 value, index, NULL, 0, ZC0301_CTRL_TIMEOUT);
220 if (res < 0) {
221 DBG(3, "Failed to write a register (index 0x%04X, "
222 "value 0x%02X, error %d)",index, value, res);
223 return -1;
224 }
225
226 return 0;
227}
228
229
230int zc0301_read_reg(struct zc0301_device* cam, u16 index)
231{
232 struct usb_device* udev = cam->usbdev;
233 u8* buff = cam->control_buffer;
234 int res;
235
236 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0xa1, 0xc0,
237 0x0001, index, buff, 1, ZC0301_CTRL_TIMEOUT);
238 if (res < 0)
239 DBG(3, "Failed to read a register (index 0x%04X, error %d)",
240 index, res);
241
242 PDBGG("Read: index 0x%04X, value: 0x%04X", index, (int)(*buff));
243
244 return (res >= 0) ? (int)(*buff) : -1;
245}
246
247
248int zc0301_i2c_read(struct zc0301_device* cam, u16 address, u8 length)
249{
250 int err = 0, res, r0, r1;
251
252 err += zc0301_write_reg(cam, 0x0092, address);
253 err += zc0301_write_reg(cam, 0x0090, 0x02);
254
255 msleep(1);
256
257 res = zc0301_read_reg(cam, 0x0091);
258 if (res < 0)
259 err += res;
260 r0 = zc0301_read_reg(cam, 0x0095);
261 if (r0 < 0)
262 err += r0;
263 r1 = zc0301_read_reg(cam, 0x0096);
264 if (r1 < 0)
265 err += r1;
266
267 res = (length <= 1) ? r0 : r0 | (r1 << 8);
268
269 if (err)
270 DBG(3, "I2C read failed at address 0x%04X, value: 0x%04X",
271 address, res);
272
273
274 PDBGG("I2C read: address 0x%04X, value: 0x%04X", address, res);
275
276 return err ? -1 : res;
277}
278
279
280int zc0301_i2c_write(struct zc0301_device* cam, u16 address, u16 value)
281{
282 int err = 0, res;
283
284 err += zc0301_write_reg(cam, 0x0092, address);
285 err += zc0301_write_reg(cam, 0x0093, value & 0xff);
286 err += zc0301_write_reg(cam, 0x0094, value >> 8);
287 err += zc0301_write_reg(cam, 0x0090, 0x01);
288
289 msleep(1);
290
291 res = zc0301_read_reg(cam, 0x0091);
292 if (res < 0)
293 err += res;
294
295 if (err)
296 DBG(3, "I2C write failed at address 0x%04X, value: 0x%04X",
297 address, value);
298
299 PDBGG("I2C write: address 0x%04X, value: 0x%04X", address, value);
300
301 return err ? -1 : 0;
302}
303
304/*****************************************************************************/
305
306static void zc0301_urb_complete(struct urb *urb, struct pt_regs* regs)
307{
308 struct zc0301_device* cam = urb->context;
309 struct zc0301_frame_t** f;
310 size_t imagesize;
311 u8 i;
312 int err = 0;
313
314 if (urb->status == -ENOENT)
315 return;
316
317 f = &cam->frame_current;
318
319 if (cam->stream == STREAM_INTERRUPT) {
320 cam->stream = STREAM_OFF;
321 if ((*f))
322 (*f)->state = F_QUEUED;
323 DBG(3, "Stream interrupted");
324 wake_up(&cam->wait_stream);
325 }
326
327 if (cam->state & DEV_DISCONNECTED)
328 return;
329
330 if (cam->state & DEV_MISCONFIGURED) {
331 wake_up_interruptible(&cam->wait_frame);
332 return;
333 }
334
335 if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))
336 goto resubmit_urb;
337
338 if (!(*f))
339 (*f) = list_entry(cam->inqueue.next, struct zc0301_frame_t,
340 frame);
341
342 imagesize = (cam->sensor.pix_format.width *
343 cam->sensor.pix_format.height *
344 cam->sensor.pix_format.priv) / 8;
345
346 for (i = 0; i < urb->number_of_packets; i++) {
347 unsigned int len, status;
348 void *pos;
349 u16* soi;
350 u8 sof;
351
352 len = urb->iso_frame_desc[i].actual_length;
353 status = urb->iso_frame_desc[i].status;
354 pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;
355
356 if (status) {
357 DBG(3, "Error in isochronous frame");
358 (*f)->state = F_ERROR;
359 continue;
360 }
361
362 sof = (*(soi = pos) == 0xd8ff);
363
364 PDBGG("Isochrnous frame: length %u, #%u i,", len, i);
365
366 if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR)
367start_of_frame:
368 if (sof) {
369 (*f)->state = F_GRABBING;
370 (*f)->buf.bytesused = 0;
371 do_gettimeofday(&(*f)->buf.timestamp);
372 DBG(3, "SOF detected: new video frame");
373 }
374
375 if ((*f)->state == F_GRABBING) {
376 if (sof && (*f)->buf.bytesused)
377 goto end_of_frame;
378
379 if ((*f)->buf.bytesused + len > imagesize) {
380 DBG(3, "Video frame size exceeded");
381 (*f)->state = F_ERROR;
382 continue;
383 }
384
385 memcpy((*f)->bufmem+(*f)->buf.bytesused, pos, len);
386 (*f)->buf.bytesused += len;
387
388 if ((*f)->buf.bytesused == imagesize) {
389 u32 b;
390end_of_frame:
391 b = (*f)->buf.bytesused;
392 (*f)->state = F_DONE;
393 (*f)->buf.sequence= ++cam->frame_count;
394 spin_lock(&cam->queue_lock);
395 list_move_tail(&(*f)->frame, &cam->outqueue);
396 if (!list_empty(&cam->inqueue))
397 (*f) = list_entry(cam->inqueue.next,
398 struct zc0301_frame_t,
399 frame);
400 else
401 (*f) = NULL;
402 spin_unlock(&cam->queue_lock);
403 DBG(3, "Video frame captured: : %lu bytes",
404 (unsigned long)(b));
405
406 if (!(*f))
407 goto resubmit_urb;
408
409 if (sof)
410 goto start_of_frame;
411 }
412 }
413 }
414
415resubmit_urb:
416 urb->dev = cam->usbdev;
417 err = usb_submit_urb(urb, GFP_ATOMIC);
418 if (err < 0 && err != -EPERM) {
419 cam->state |= DEV_MISCONFIGURED;
420 DBG(1, "usb_submit_urb() failed");
421 }
422
423 wake_up_interruptible(&cam->wait_frame);
424}
425
426
427static int zc0301_start_transfer(struct zc0301_device* cam)
428{
429 struct usb_device *udev = cam->usbdev;
430 struct urb* urb;
431 const unsigned int wMaxPacketSize[] = {0, 128, 192, 256, 384,
432 512, 768, 1023};
433 const unsigned int psz = wMaxPacketSize[ZC0301_ALTERNATE_SETTING];
434 s8 i, j;
435 int err = 0;
436
437 for (i = 0; i < ZC0301_URBS; i++) {
438 cam->transfer_buffer[i] = kzalloc(ZC0301_ISO_PACKETS * psz,
439 GFP_KERNEL);
440 if (!cam->transfer_buffer[i]) {
441 err = -ENOMEM;
442 DBG(1, "Not enough memory");
443 goto free_buffers;
444 }
445 }
446
447 for (i = 0; i < ZC0301_URBS; i++) {
448 urb = usb_alloc_urb(ZC0301_ISO_PACKETS, GFP_KERNEL);
449 cam->urb[i] = urb;
450 if (!urb) {
451 err = -ENOMEM;
452 DBG(1, "usb_alloc_urb() failed");
453 goto free_urbs;
454 }
455 urb->dev = udev;
456 urb->context = cam;
457 urb->pipe = usb_rcvisocpipe(udev, 1);
458 urb->transfer_flags = URB_ISO_ASAP;
459 urb->number_of_packets = ZC0301_ISO_PACKETS;
460 urb->complete = zc0301_urb_complete;
461 urb->transfer_buffer = cam->transfer_buffer[i];
462 urb->transfer_buffer_length = psz * ZC0301_ISO_PACKETS;
463 urb->interval = 1;
464 for (j = 0; j < ZC0301_ISO_PACKETS; j++) {
465 urb->iso_frame_desc[j].offset = psz * j;
466 urb->iso_frame_desc[j].length = psz;
467 }
468 }
469
470 err = usb_set_interface(udev, 0, ZC0301_ALTERNATE_SETTING);
471 if (err) {
472 DBG(1, "usb_set_interface() failed");
473 goto free_urbs;
474 }
475
476 cam->frame_current = NULL;
477
478 for (i = 0; i < ZC0301_URBS; i++) {
479 err = usb_submit_urb(cam->urb[i], GFP_KERNEL);
480 if (err) {
481 for (j = i-1; j >= 0; j--)
482 usb_kill_urb(cam->urb[j]);
483 DBG(1, "usb_submit_urb() failed, error %d", err);
484 goto free_urbs;
485 }
486 }
487
488 return 0;
489
490free_urbs:
491 for (i = 0; (i < ZC0301_URBS) && cam->urb[i]; i++)
492 usb_free_urb(cam->urb[i]);
493
494free_buffers:
495 for (i = 0; (i < ZC0301_URBS) && cam->transfer_buffer[i]; i++)
496 kfree(cam->transfer_buffer[i]);
497
498 return err;
499}
500
501
502static int zc0301_stop_transfer(struct zc0301_device* cam)
503{
504 struct usb_device *udev = cam->usbdev;
505 s8 i;
506 int err = 0;
507
508 if (cam->state & DEV_DISCONNECTED)
509 return 0;
510
511 for (i = ZC0301_URBS-1; i >= 0; i--) {
512 usb_kill_urb(cam->urb[i]);
513 usb_free_urb(cam->urb[i]);
514 kfree(cam->transfer_buffer[i]);
515 }
516
517 err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */
518 if (err)
519 DBG(3, "usb_set_interface() failed");
520
521 return err;
522}
523
524
525static int zc0301_stream_interrupt(struct zc0301_device* cam)
526{
527 long timeout;
528
529 cam->stream = STREAM_INTERRUPT;
530 timeout = wait_event_timeout(cam->wait_stream,
531 (cam->stream == STREAM_OFF) ||
532 (cam->state & DEV_DISCONNECTED),
533 ZC0301_URB_TIMEOUT);
534 if (cam->state & DEV_DISCONNECTED)
535 return -ENODEV;
536 else if (cam->stream != STREAM_OFF) {
537 cam->state |= DEV_MISCONFIGURED;
538 DBG(1, "URB timeout reached. The camera is misconfigured. To "
539 "use it, close and open /dev/video%d again.",
540 cam->v4ldev->minor);
541 return -EIO;
542 }
543
544 return 0;
545}
546
547/*****************************************************************************/
548
549static int
550zc0301_set_compression(struct zc0301_device* cam,
551 struct v4l2_jpegcompression* compression)
552{
553 int r, err = 0;
554
555 if ((r = zc0301_read_reg(cam, 0x0008)) < 0)
556 err += r;
557 err += zc0301_write_reg(cam, 0x0008, r | 0x11 | compression->quality);
558
559 return err ? -EIO : 0;
560}
561
562
563static int zc0301_init(struct zc0301_device* cam)
564{
565 struct zc0301_sensor* s = &cam->sensor;
566 struct v4l2_control ctrl;
567 struct v4l2_queryctrl *qctrl;
568 struct v4l2_rect* rect;
569 u8 i = 0;
570 int err = 0;
571
572 if (!(cam->state & DEV_INITIALIZED)) {
573 init_waitqueue_head(&cam->open);
574 qctrl = s->qctrl;
575 rect = &(s->cropcap.defrect);
576 cam->compression.quality = ZC0301_COMPRESSION_QUALITY;
577 } else { /* use current values */
578 qctrl = s->_qctrl;
579 rect = &(s->_rect);
580 }
581
582 if (s->init) {
583 err = s->init(cam);
584 if (err) {
585 DBG(3, "Sensor initialization failed");
586 return err;
587 }
588 }
589
590 if ((err = zc0301_set_compression(cam, &cam->compression))) {
591 DBG(3, "set_compression() failed");
592 return err;
593 }
594
595 if (s->set_crop)
596 if ((err = s->set_crop(cam, rect))) {
597 DBG(3, "set_crop() failed");
598 return err;
599 }
600
601 if (s->set_ctrl) {
602 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
603 if (s->qctrl[i].id != 0 &&
604 !(s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)) {
605 ctrl.id = s->qctrl[i].id;
606 ctrl.value = qctrl[i].default_value;
607 err = s->set_ctrl(cam, &ctrl);
608 if (err) {
609 DBG(3, "Set %s control failed",
610 s->qctrl[i].name);
611 return err;
612 }
613 DBG(3, "Image sensor supports '%s' control",
614 s->qctrl[i].name);
615 }
616 }
617
618 if (!(cam->state & DEV_INITIALIZED)) {
619 mutex_init(&cam->fileop_mutex);
620 spin_lock_init(&cam->queue_lock);
621 init_waitqueue_head(&cam->wait_frame);
622 init_waitqueue_head(&cam->wait_stream);
623 cam->nreadbuffers = 2;
624 memcpy(s->_qctrl, s->qctrl, sizeof(s->qctrl));
625 memcpy(&(s->_rect), &(s->cropcap.defrect),
626 sizeof(struct v4l2_rect));
627 cam->state |= DEV_INITIALIZED;
628 }
629
630 DBG(2, "Initialization succeeded");
631 return 0;
632}
633
634
635static void zc0301_release_resources(struct zc0301_device* cam)
636{
637 DBG(2, "V4L2 device /dev/video%d deregistered", cam->v4ldev->minor);
638 video_set_drvdata(cam->v4ldev, NULL);
639 video_unregister_device(cam->v4ldev);
640 kfree(cam->control_buffer);
641}
642
643/*****************************************************************************/
644
645static int zc0301_open(struct inode* inode, struct file* filp)
646{
647 struct zc0301_device* cam;
648 int err = 0;
649
650 /*
651 This is the only safe way to prevent race conditions with
652 disconnect
653 */
654 if (!down_read_trylock(&zc0301_disconnect))
655 return -ERESTARTSYS;
656
657 cam = video_get_drvdata(video_devdata(filp));
658
659 if (mutex_lock_interruptible(&cam->dev_mutex)) {
660 up_read(&zc0301_disconnect);
661 return -ERESTARTSYS;
662 }
663
664 if (cam->users) {
665 DBG(2, "Device /dev/video%d is busy...", cam->v4ldev->minor);
666 if ((filp->f_flags & O_NONBLOCK) ||
667 (filp->f_flags & O_NDELAY)) {
668 err = -EWOULDBLOCK;
669 goto out;
670 }
671 mutex_unlock(&cam->dev_mutex);
672 err = wait_event_interruptible_exclusive(cam->open,
673 cam->state & DEV_DISCONNECTED
674 || !cam->users);
675 if (err) {
676 up_read(&zc0301_disconnect);
677 return err;
678 }
679 if (cam->state & DEV_DISCONNECTED) {
680 up_read(&zc0301_disconnect);
681 return -ENODEV;
682 }
683 mutex_lock(&cam->dev_mutex);
684 }
685
686
687 if (cam->state & DEV_MISCONFIGURED) {
688 err = zc0301_init(cam);
689 if (err) {
690 DBG(1, "Initialization failed again. "
691 "I will retry on next open().");
692 goto out;
693 }
694 cam->state &= ~DEV_MISCONFIGURED;
695 }
696
697 if ((err = zc0301_start_transfer(cam)))
698 goto out;
699
700 filp->private_data = cam;
701 cam->users++;
702 cam->io = IO_NONE;
703 cam->stream = STREAM_OFF;
704 cam->nbuffers = 0;
705 cam->frame_count = 0;
706 zc0301_empty_framequeues(cam);
707
708 DBG(3, "Video device /dev/video%d is open", cam->v4ldev->minor);
709
710out:
711 mutex_unlock(&cam->dev_mutex);
712 up_read(&zc0301_disconnect);
713 return err;
714}
715
716
717static int zc0301_release(struct inode* inode, struct file* filp)
718{
719 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
720
721 mutex_lock(&cam->dev_mutex); /* prevent disconnect() to be called */
722
723 zc0301_stop_transfer(cam);
724
725 zc0301_release_buffers(cam);
726
727 if (cam->state & DEV_DISCONNECTED) {
728 zc0301_release_resources(cam);
729 usb_put_dev(cam->usbdev);
730 mutex_unlock(&cam->dev_mutex);
731 kfree(cam);
732 return 0;
733 }
734
735 cam->users--;
736 wake_up_interruptible_nr(&cam->open, 1);
737
738 DBG(3, "Video device /dev/video%d closed", cam->v4ldev->minor);
739
740 mutex_unlock(&cam->dev_mutex);
741
742 return 0;
743}
744
745
746static ssize_t
747zc0301_read(struct file* filp, char __user * buf, size_t count, loff_t* f_pos)
748{
749 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
750 struct zc0301_frame_t* f, * i;
751 unsigned long lock_flags;
752 long timeout;
753 int err = 0;
754
755 if (mutex_lock_interruptible(&cam->fileop_mutex))
756 return -ERESTARTSYS;
757
758 if (cam->state & DEV_DISCONNECTED) {
759 DBG(1, "Device not present");
760 mutex_unlock(&cam->fileop_mutex);
761 return -ENODEV;
762 }
763
764 if (cam->state & DEV_MISCONFIGURED) {
765 DBG(1, "The camera is misconfigured. Close and open it "
766 "again.");
767 mutex_unlock(&cam->fileop_mutex);
768 return -EIO;
769 }
770
771 if (cam->io == IO_MMAP) {
772 DBG(3, "Close and open the device again to choose the read "
773 "method");
774 mutex_unlock(&cam->fileop_mutex);
775 return -EINVAL;
776 }
777
778 if (cam->io == IO_NONE) {
779 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
780 DBG(1, "read() failed, not enough memory");
781 mutex_unlock(&cam->fileop_mutex);
782 return -ENOMEM;
783 }
784 cam->io = IO_READ;
785 cam->stream = STREAM_ON;
786 }
787
788 if (list_empty(&cam->inqueue)) {
789 if (!list_empty(&cam->outqueue))
790 zc0301_empty_framequeues(cam);
791 zc0301_queue_unusedframes(cam);
792 }
793
794 if (!count) {
795 mutex_unlock(&cam->fileop_mutex);
796 return 0;
797 }
798
799 if (list_empty(&cam->outqueue)) {
800 if (filp->f_flags & O_NONBLOCK) {
801 mutex_unlock(&cam->fileop_mutex);
802 return -EAGAIN;
803 }
804 timeout = wait_event_interruptible_timeout
805 ( cam->wait_frame,
806 (!list_empty(&cam->outqueue)) ||
807 (cam->state & DEV_DISCONNECTED) ||
808 (cam->state & DEV_MISCONFIGURED),
809 cam->module_param.frame_timeout *
810 1000 * msecs_to_jiffies(1) );
811 if (timeout < 0) {
812 mutex_unlock(&cam->fileop_mutex);
813 return timeout;
814 }
815 if (cam->state & DEV_DISCONNECTED) {
816 mutex_unlock(&cam->fileop_mutex);
817 return -ENODEV;
818 }
819 if (!timeout || (cam->state & DEV_MISCONFIGURED)) {
820 mutex_unlock(&cam->fileop_mutex);
821 return -EIO;
822 }
823 }
824
825 f = list_entry(cam->outqueue.prev, struct zc0301_frame_t, frame);
826
827 if (count > f->buf.bytesused)
828 count = f->buf.bytesused;
829
830 if (copy_to_user(buf, f->bufmem, count)) {
831 err = -EFAULT;
832 goto exit;
833 }
834 *f_pos += count;
835
836exit:
837 spin_lock_irqsave(&cam->queue_lock, lock_flags);
838 list_for_each_entry(i, &cam->outqueue, frame)
839 i->state = F_UNUSED;
840 INIT_LIST_HEAD(&cam->outqueue);
841 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
842
843 zc0301_queue_unusedframes(cam);
844
845 PDBGG("Frame #%lu, bytes read: %zu",
846 (unsigned long)f->buf.index, count);
847
848 mutex_unlock(&cam->fileop_mutex);
849
850 return err ? err : count;
851}
852
853
854static unsigned int zc0301_poll(struct file *filp, poll_table *wait)
855{
856 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
857 struct zc0301_frame_t* f;
858 unsigned long lock_flags;
859 unsigned int mask = 0;
860
861 if (mutex_lock_interruptible(&cam->fileop_mutex))
862 return POLLERR;
863
864 if (cam->state & DEV_DISCONNECTED) {
865 DBG(1, "Device not present");
866 goto error;
867 }
868
869 if (cam->state & DEV_MISCONFIGURED) {
870 DBG(1, "The camera is misconfigured. Close and open it "
871 "again.");
872 goto error;
873 }
874
875 if (cam->io == IO_NONE) {
876 if (!zc0301_request_buffers(cam, cam->nreadbuffers, IO_READ)) {
877 DBG(1, "poll() failed, not enough memory");
878 goto error;
879 }
880 cam->io = IO_READ;
881 cam->stream = STREAM_ON;
882 }
883
884 if (cam->io == IO_READ) {
885 spin_lock_irqsave(&cam->queue_lock, lock_flags);
886 list_for_each_entry(f, &cam->outqueue, frame)
887 f->state = F_UNUSED;
888 INIT_LIST_HEAD(&cam->outqueue);
889 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
890 zc0301_queue_unusedframes(cam);
891 }
892
893 poll_wait(filp, &cam->wait_frame, wait);
894
895 if (!list_empty(&cam->outqueue))
896 mask |= POLLIN | POLLRDNORM;
897
898 mutex_unlock(&cam->fileop_mutex);
899
900 return mask;
901
902error:
903 mutex_unlock(&cam->fileop_mutex);
904 return POLLERR;
905}
906
907
908static void zc0301_vm_open(struct vm_area_struct* vma)
909{
910 struct zc0301_frame_t* f = vma->vm_private_data;
911 f->vma_use_count++;
912}
913
914
915static void zc0301_vm_close(struct vm_area_struct* vma)
916{
917 /* NOTE: buffers are not freed here */
918 struct zc0301_frame_t* f = vma->vm_private_data;
919 f->vma_use_count--;
920}
921
922
923static struct vm_operations_struct zc0301_vm_ops = {
924 .open = zc0301_vm_open,
925 .close = zc0301_vm_close,
926};
927
928
929static int zc0301_mmap(struct file* filp, struct vm_area_struct *vma)
930{
931 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
932 unsigned long size = vma->vm_end - vma->vm_start,
933 start = vma->vm_start;
934 void *pos;
935 u32 i;
936
937 if (mutex_lock_interruptible(&cam->fileop_mutex))
938 return -ERESTARTSYS;
939
940 if (cam->state & DEV_DISCONNECTED) {
941 DBG(1, "Device not present");
942 mutex_unlock(&cam->fileop_mutex);
943 return -ENODEV;
944 }
945
946 if (cam->state & DEV_MISCONFIGURED) {
947 DBG(1, "The camera is misconfigured. Close and open it "
948 "again.");
949 mutex_unlock(&cam->fileop_mutex);
950 return -EIO;
951 }
952
953 if (cam->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
954 size != PAGE_ALIGN(cam->frame[0].buf.length)) {
955 mutex_unlock(&cam->fileop_mutex);
956 return -EINVAL;
957 }
958
959 for (i = 0; i < cam->nbuffers; i++) {
960 if ((cam->frame[i].buf.m.offset>>PAGE_SHIFT) == vma->vm_pgoff)
961 break;
962 }
963 if (i == cam->nbuffers) {
964 mutex_unlock(&cam->fileop_mutex);
965 return -EINVAL;
966 }
967
968 vma->vm_flags |= VM_IO;
969 vma->vm_flags |= VM_RESERVED;
970
971 pos = cam->frame[i].bufmem;
972 while (size > 0) { /* size is page-aligned */
973 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
974 mutex_unlock(&cam->fileop_mutex);
975 return -EAGAIN;
976 }
977 start += PAGE_SIZE;
978 pos += PAGE_SIZE;
979 size -= PAGE_SIZE;
980 }
981
982 vma->vm_ops = &zc0301_vm_ops;
983 vma->vm_private_data = &cam->frame[i];
984
985 zc0301_vm_open(vma);
986
987 mutex_unlock(&cam->fileop_mutex);
988
989 return 0;
990}
991
992/*****************************************************************************/
993
994static int
995zc0301_vidioc_querycap(struct zc0301_device* cam, void __user * arg)
996{
997 struct v4l2_capability cap = {
998 .driver = "zc0301",
999 .version = ZC0301_MODULE_VERSION_CODE,
1000 .capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1001 V4L2_CAP_STREAMING,
1002 };
1003
1004 strlcpy(cap.card, cam->v4ldev->name, sizeof(cap.card));
1005 if (usb_make_path(cam->usbdev, cap.bus_info, sizeof(cap.bus_info)) < 0)
1006 strlcpy(cap.bus_info, cam->usbdev->dev.bus_id,
1007 sizeof(cap.bus_info));
1008
1009 if (copy_to_user(arg, &cap, sizeof(cap)))
1010 return -EFAULT;
1011
1012 return 0;
1013}
1014
1015
1016static int
1017zc0301_vidioc_enuminput(struct zc0301_device* cam, void __user * arg)
1018{
1019 struct v4l2_input i;
1020
1021 if (copy_from_user(&i, arg, sizeof(i)))
1022 return -EFAULT;
1023
1024 if (i.index)
1025 return -EINVAL;
1026
1027 memset(&i, 0, sizeof(i));
1028 strcpy(i.name, "Camera");
1029 i.type = V4L2_INPUT_TYPE_CAMERA;
1030
1031 if (copy_to_user(arg, &i, sizeof(i)))
1032 return -EFAULT;
1033
1034 return 0;
1035}
1036
1037
1038static int
1039zc0301_vidioc_g_input(struct zc0301_device* cam, void __user * arg)
1040{
1041 int index = 0;
1042
1043 if (copy_to_user(arg, &index, sizeof(index)))
1044 return -EFAULT;
1045
1046 return 0;
1047}
1048
1049
1050static int
1051zc0301_vidioc_s_input(struct zc0301_device* cam, void __user * arg)
1052{
1053 int index;
1054
1055 if (copy_from_user(&index, arg, sizeof(index)))
1056 return -EFAULT;
1057
1058 if (index != 0)
1059 return -EINVAL;
1060
1061 return 0;
1062}
1063
1064
1065static int
1066zc0301_vidioc_query_ctrl(struct zc0301_device* cam, void __user * arg)
1067{
1068 struct zc0301_sensor* s = &cam->sensor;
1069 struct v4l2_queryctrl qc;
1070 u8 i;
1071
1072 if (copy_from_user(&qc, arg, sizeof(qc)))
1073 return -EFAULT;
1074
1075 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1076 if (qc.id && qc.id == s->qctrl[i].id) {
1077 memcpy(&qc, &(s->qctrl[i]), sizeof(qc));
1078 if (copy_to_user(arg, &qc, sizeof(qc)))
1079 return -EFAULT;
1080 return 0;
1081 }
1082
1083 return -EINVAL;
1084}
1085
1086
1087static int
1088zc0301_vidioc_g_ctrl(struct zc0301_device* cam, void __user * arg)
1089{
1090 struct zc0301_sensor* s = &cam->sensor;
1091 struct v4l2_control ctrl;
1092 int err = 0;
1093 u8 i;
1094
1095 if (!s->get_ctrl && !s->set_ctrl)
1096 return -EINVAL;
1097
1098 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1099 return -EFAULT;
1100
1101 if (!s->get_ctrl) {
1102 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1103 if (ctrl.id == s->qctrl[i].id) {
1104 ctrl.value = s->_qctrl[i].default_value;
1105 goto exit;
1106 }
1107 return -EINVAL;
1108 } else
1109 err = s->get_ctrl(cam, &ctrl);
1110
1111exit:
1112 if (copy_to_user(arg, &ctrl, sizeof(ctrl)))
1113 return -EFAULT;
1114
1115 return err;
1116}
1117
1118
1119static int
1120zc0301_vidioc_s_ctrl(struct zc0301_device* cam, void __user * arg)
1121{
1122 struct zc0301_sensor* s = &cam->sensor;
1123 struct v4l2_control ctrl;
1124 u8 i;
1125 int err = 0;
1126
1127 if (!s->set_ctrl)
1128 return -EINVAL;
1129
1130 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1131 return -EFAULT;
1132
1133 for (i = 0; i < ARRAY_SIZE(s->qctrl); i++)
1134 if (ctrl.id == s->qctrl[i].id) {
1135 if (s->qctrl[i].flags & V4L2_CTRL_FLAG_DISABLED)
1136 return -EINVAL;
1137 if (ctrl.value < s->qctrl[i].minimum ||
1138 ctrl.value > s->qctrl[i].maximum)
1139 return -ERANGE;
1140 ctrl.value -= ctrl.value % s->qctrl[i].step;
1141 break;
1142 }
1143
1144 if ((err = s->set_ctrl(cam, &ctrl)))
1145 return err;
1146
1147 s->_qctrl[i].default_value = ctrl.value;
1148
1149 return 0;
1150}
1151
1152
1153static int
1154zc0301_vidioc_cropcap(struct zc0301_device* cam, void __user * arg)
1155{
1156 struct v4l2_cropcap* cc = &(cam->sensor.cropcap);
1157
1158 cc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1159 cc->pixelaspect.numerator = 1;
1160 cc->pixelaspect.denominator = 1;
1161
1162 if (copy_to_user(arg, cc, sizeof(*cc)))
1163 return -EFAULT;
1164
1165 return 0;
1166}
1167
1168
1169static int
1170zc0301_vidioc_g_crop(struct zc0301_device* cam, void __user * arg)
1171{
1172 struct zc0301_sensor* s = &cam->sensor;
1173 struct v4l2_crop crop = {
1174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1175 };
1176
1177 memcpy(&(crop.c), &(s->_rect), sizeof(struct v4l2_rect));
1178
1179 if (copy_to_user(arg, &crop, sizeof(crop)))
1180 return -EFAULT;
1181
1182 return 0;
1183}
1184
1185
1186static int
1187zc0301_vidioc_s_crop(struct zc0301_device* cam, void __user * arg)
1188{
1189 struct zc0301_sensor* s = &cam->sensor;
1190 struct v4l2_crop crop;
1191 struct v4l2_rect* rect;
1192 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1193 const enum zc0301_stream_state stream = cam->stream;
1194 const u32 nbuffers = cam->nbuffers;
1195 u32 i;
1196 int err = 0;
1197
1198 if (copy_from_user(&crop, arg, sizeof(crop)))
1199 return -EFAULT;
1200
1201 rect = &(crop.c);
1202
1203 if (crop.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1204 return -EINVAL;
1205
1206 if (cam->module_param.force_munmap)
1207 for (i = 0; i < cam->nbuffers; i++)
1208 if (cam->frame[i].vma_use_count) {
1209 DBG(3, "VIDIOC_S_CROP failed. "
1210 "Unmap the buffers first.");
1211 return -EINVAL;
1212 }
1213
1214 if (!s->set_crop) {
1215 memcpy(rect, &(s->_rect), sizeof(*rect));
1216 if (copy_to_user(arg, &crop, sizeof(crop)))
1217 return -EFAULT;
1218 return 0;
1219 }
1220
1221 rect->left &= ~7L;
1222 rect->top &= ~7L;
1223 if (rect->width < 8)
1224 rect->width = 8;
1225 if (rect->height < 8)
1226 rect->height = 8;
1227 if (rect->width > bounds->width)
1228 rect->width = bounds->width;
1229 if (rect->height > bounds->height)
1230 rect->height = bounds->height;
1231 if (rect->left < bounds->left)
1232 rect->left = bounds->left;
1233 if (rect->top < bounds->top)
1234 rect->top = bounds->top;
1235 if (rect->left + rect->width > bounds->left + bounds->width)
1236 rect->left = bounds->left+bounds->width - rect->width;
1237 if (rect->top + rect->height > bounds->top + bounds->height)
1238 rect->top = bounds->top+bounds->height - rect->height;
1239 rect->width &= ~7L;
1240 rect->height &= ~7L;
1241
1242 if (cam->stream == STREAM_ON)
1243 if ((err = zc0301_stream_interrupt(cam)))
1244 return err;
1245
1246 if (copy_to_user(arg, &crop, sizeof(crop))) {
1247 cam->stream = stream;
1248 return -EFAULT;
1249 }
1250
1251 if (cam->module_param.force_munmap || cam->io == IO_READ)
1252 zc0301_release_buffers(cam);
1253
1254 if (s->set_crop)
1255 err += s->set_crop(cam, rect);
1256
1257 if (err) { /* atomic, no rollback in ioctl() */
1258 cam->state |= DEV_MISCONFIGURED;
1259 DBG(1, "VIDIOC_S_CROP failed because of hardware problems. To "
1260 "use the camera, close and open /dev/video%d again.",
1261 cam->v4ldev->minor);
1262 return -EIO;
1263 }
1264
1265 s->pix_format.width = rect->width;
1266 s->pix_format.height = rect->height;
1267 memcpy(&(s->_rect), rect, sizeof(*rect));
1268
1269 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1270 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1271 cam->state |= DEV_MISCONFIGURED;
1272 DBG(1, "VIDIOC_S_CROP failed because of not enough memory. To "
1273 "use the camera, close and open /dev/video%d again.",
1274 cam->v4ldev->minor);
1275 return -ENOMEM;
1276 }
1277
1278 if (cam->io == IO_READ)
1279 zc0301_empty_framequeues(cam);
1280 else if (cam->module_param.force_munmap)
1281 zc0301_requeue_outqueue(cam);
1282
1283 cam->stream = stream;
1284
1285 return 0;
1286}
1287
1288
1289static int
1290zc0301_vidioc_enum_fmt(struct zc0301_device* cam, void __user * arg)
1291{
1292 struct v4l2_fmtdesc fmtd;
1293
1294 if (copy_from_user(&fmtd, arg, sizeof(fmtd)))
1295 return -EFAULT;
1296
1297 if (fmtd.index == 0) {
1298 strcpy(fmtd.description, "JPEG");
1299 fmtd.pixelformat = V4L2_PIX_FMT_JPEG;
1300 fmtd.flags = V4L2_FMT_FLAG_COMPRESSED;
1301 } else
1302 return -EINVAL;
1303
1304 fmtd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1305 memset(&fmtd.reserved, 0, sizeof(fmtd.reserved));
1306
1307 if (copy_to_user(arg, &fmtd, sizeof(fmtd)))
1308 return -EFAULT;
1309
1310 return 0;
1311}
1312
1313
1314static int
1315zc0301_vidioc_g_fmt(struct zc0301_device* cam, void __user * arg)
1316{
1317 struct v4l2_format format;
1318 struct v4l2_pix_format* pfmt = &(cam->sensor.pix_format);
1319
1320 if (copy_from_user(&format, arg, sizeof(format)))
1321 return -EFAULT;
1322
1323 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1324 return -EINVAL;
1325
1326 pfmt->bytesperline = 0;
1327 pfmt->sizeimage = pfmt->height * ((pfmt->width*pfmt->priv)/8);
1328 pfmt->field = V4L2_FIELD_NONE;
1329 memcpy(&(format.fmt.pix), pfmt, sizeof(*pfmt));
1330
1331 if (copy_to_user(arg, &format, sizeof(format)))
1332 return -EFAULT;
1333
1334 return 0;
1335}
1336
1337
1338static int
1339zc0301_vidioc_try_s_fmt(struct zc0301_device* cam, unsigned int cmd,
1340 void __user * arg)
1341{
1342 struct zc0301_sensor* s = &cam->sensor;
1343 struct v4l2_format format;
1344 struct v4l2_pix_format* pix;
1345 struct v4l2_pix_format* pfmt = &(s->pix_format);
1346 struct v4l2_rect* bounds = &(s->cropcap.bounds);
1347 struct v4l2_rect rect;
1348 const enum zc0301_stream_state stream = cam->stream;
1349 const u32 nbuffers = cam->nbuffers;
1350 u32 i;
1351 int err = 0;
1352
1353 if (copy_from_user(&format, arg, sizeof(format)))
1354 return -EFAULT;
1355
1356 pix = &(format.fmt.pix);
1357
1358 if (format.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1359 return -EINVAL;
1360
1361 memcpy(&rect, &(s->_rect), sizeof(rect));
1362
1363 if (!s->set_crop) {
1364 pix->width = rect.width;
1365 pix->height = rect.height;
1366 } else {
1367 rect.width = pix->width;
1368 rect.height = pix->height;
1369 }
1370
1371 if (rect.width < 8)
1372 rect.width = 8;
1373 if (rect.height < 8)
1374 rect.height = 8;
1375 if (rect.width > bounds->left + bounds->width - rect.left)
1376 rect.width = bounds->left + bounds->width - rect.left;
1377 if (rect.height > bounds->top + bounds->height - rect.top)
1378 rect.height = bounds->top + bounds->height - rect.top;
1379 rect.width &= ~7L;
1380 rect.height &= ~7L;
1381
1382 pix->width = rect.width;
1383 pix->height = rect.height;
1384 pix->pixelformat = pfmt->pixelformat;
1385 pix->priv = pfmt->priv;
1386 pix->colorspace = pfmt->colorspace;
1387 pix->bytesperline = 0;
1388 pix->sizeimage = pix->height * ((pix->width * pix->priv) / 8);
1389 pix->field = V4L2_FIELD_NONE;
1390
1391 if (cmd == VIDIOC_TRY_FMT) {
1392 if (copy_to_user(arg, &format, sizeof(format)))
1393 return -EFAULT;
1394 return 0;
1395 }
1396
1397 if (cam->module_param.force_munmap)
1398 for (i = 0; i < cam->nbuffers; i++)
1399 if (cam->frame[i].vma_use_count) {
1400 DBG(3, "VIDIOC_S_FMT failed. "
1401 "Unmap the buffers first.");
1402 return -EINVAL;
1403 }
1404
1405 if (cam->stream == STREAM_ON)
1406 if ((err = zc0301_stream_interrupt(cam)))
1407 return err;
1408
1409 if (copy_to_user(arg, &format, sizeof(format))) {
1410 cam->stream = stream;
1411 return -EFAULT;
1412 }
1413
1414 if (cam->module_param.force_munmap || cam->io == IO_READ)
1415 zc0301_release_buffers(cam);
1416
1417 if (s->set_crop)
1418 err += s->set_crop(cam, &rect);
1419
1420 if (err) { /* atomic, no rollback in ioctl() */
1421 cam->state |= DEV_MISCONFIGURED;
1422 DBG(1, "VIDIOC_S_FMT failed because of hardware problems. To "
1423 "use the camera, close and open /dev/video%d again.",
1424 cam->v4ldev->minor);
1425 return -EIO;
1426 }
1427
1428 memcpy(pfmt, pix, sizeof(*pix));
1429 memcpy(&(s->_rect), &rect, sizeof(rect));
1430
1431 if ((cam->module_param.force_munmap || cam->io == IO_READ) &&
1432 nbuffers != zc0301_request_buffers(cam, nbuffers, cam->io)) {
1433 cam->state |= DEV_MISCONFIGURED;
1434 DBG(1, "VIDIOC_S_FMT failed because of not enough memory. To "
1435 "use the camera, close and open /dev/video%d again.",
1436 cam->v4ldev->minor);
1437 return -ENOMEM;
1438 }
1439
1440 if (cam->io == IO_READ)
1441 zc0301_empty_framequeues(cam);
1442 else if (cam->module_param.force_munmap)
1443 zc0301_requeue_outqueue(cam);
1444
1445 cam->stream = stream;
1446
1447 return 0;
1448}
1449
1450
1451static int
1452zc0301_vidioc_g_jpegcomp(struct zc0301_device* cam, void __user * arg)
1453{
1454 if (copy_to_user(arg, &cam->compression, sizeof(cam->compression)))
1455 return -EFAULT;
1456
1457 return 0;
1458}
1459
1460
1461static int
1462zc0301_vidioc_s_jpegcomp(struct zc0301_device* cam, void __user * arg)
1463{
1464 struct v4l2_jpegcompression jc;
1465 const enum zc0301_stream_state stream = cam->stream;
1466 int err = 0;
1467
1468 if (copy_from_user(&jc, arg, sizeof(jc)))
1469 return -EFAULT;
1470
1471 if (jc.quality != 0)
1472 return -EINVAL;
1473
1474 if (cam->stream == STREAM_ON)
1475 if ((err = zc0301_stream_interrupt(cam)))
1476 return err;
1477
1478 err += zc0301_set_compression(cam, &jc);
1479 if (err) { /* atomic, no rollback in ioctl() */
1480 cam->state |= DEV_MISCONFIGURED;
1481 DBG(1, "VIDIOC_S_JPEGCOMP failed because of hardware "
1482 "problems. To use the camera, close and open "
1483 "/dev/video%d again.", cam->v4ldev->minor);
1484 return -EIO;
1485 }
1486
1487 cam->compression.quality = jc.quality;
1488
1489 cam->stream = stream;
1490
1491 return 0;
1492}
1493
1494
1495static int
1496zc0301_vidioc_reqbufs(struct zc0301_device* cam, void __user * arg)
1497{
1498 struct v4l2_requestbuffers rb;
1499 u32 i;
1500 int err;
1501
1502 if (copy_from_user(&rb, arg, sizeof(rb)))
1503 return -EFAULT;
1504
1505 if (rb.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1506 rb.memory != V4L2_MEMORY_MMAP)
1507 return -EINVAL;
1508
1509 if (cam->io == IO_READ) {
1510 DBG(3, "Close and open the device again to choose the mmap "
1511 "I/O method");
1512 return -EINVAL;
1513 }
1514
1515 for (i = 0; i < cam->nbuffers; i++)
1516 if (cam->frame[i].vma_use_count) {
1517 DBG(3, "VIDIOC_REQBUFS failed. "
1518 "Previous buffers are still mapped.");
1519 return -EINVAL;
1520 }
1521
1522 if (cam->stream == STREAM_ON)
1523 if ((err = zc0301_stream_interrupt(cam)))
1524 return err;
1525
1526 zc0301_empty_framequeues(cam);
1527
1528 zc0301_release_buffers(cam);
1529 if (rb.count)
1530 rb.count = zc0301_request_buffers(cam, rb.count, IO_MMAP);
1531
1532 if (copy_to_user(arg, &rb, sizeof(rb))) {
1533 zc0301_release_buffers(cam);
1534 cam->io = IO_NONE;
1535 return -EFAULT;
1536 }
1537
1538 cam->io = rb.count ? IO_MMAP : IO_NONE;
1539
1540 return 0;
1541}
1542
1543
1544static int
1545zc0301_vidioc_querybuf(struct zc0301_device* cam, void __user * arg)
1546{
1547 struct v4l2_buffer b;
1548
1549 if (copy_from_user(&b, arg, sizeof(b)))
1550 return -EFAULT;
1551
1552 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1553 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1554 return -EINVAL;
1555
1556 memcpy(&b, &cam->frame[b.index].buf, sizeof(b));
1557
1558 if (cam->frame[b.index].vma_use_count)
1559 b.flags |= V4L2_BUF_FLAG_MAPPED;
1560
1561 if (cam->frame[b.index].state == F_DONE)
1562 b.flags |= V4L2_BUF_FLAG_DONE;
1563 else if (cam->frame[b.index].state != F_UNUSED)
1564 b.flags |= V4L2_BUF_FLAG_QUEUED;
1565
1566 if (copy_to_user(arg, &b, sizeof(b)))
1567 return -EFAULT;
1568
1569 return 0;
1570}
1571
1572
1573static int
1574zc0301_vidioc_qbuf(struct zc0301_device* cam, void __user * arg)
1575{
1576 struct v4l2_buffer b;
1577 unsigned long lock_flags;
1578
1579 if (copy_from_user(&b, arg, sizeof(b)))
1580 return -EFAULT;
1581
1582 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1583 b.index >= cam->nbuffers || cam->io != IO_MMAP)
1584 return -EINVAL;
1585
1586 if (cam->frame[b.index].state != F_UNUSED)
1587 return -EINVAL;
1588
1589 cam->frame[b.index].state = F_QUEUED;
1590
1591 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1592 list_add_tail(&cam->frame[b.index].frame, &cam->inqueue);
1593 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1594
1595 PDBGG("Frame #%lu queued", (unsigned long)b.index);
1596
1597 return 0;
1598}
1599
1600
1601static int
1602zc0301_vidioc_dqbuf(struct zc0301_device* cam, struct file* filp,
1603 void __user * arg)
1604{
1605 struct v4l2_buffer b;
1606 struct zc0301_frame_t *f;
1607 unsigned long lock_flags;
1608 long timeout;
1609
1610 if (copy_from_user(&b, arg, sizeof(b)))
1611 return -EFAULT;
1612
1613 if (b.type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io!= IO_MMAP)
1614 return -EINVAL;
1615
1616 if (list_empty(&cam->outqueue)) {
1617 if (cam->stream == STREAM_OFF)
1618 return -EINVAL;
1619 if (filp->f_flags & O_NONBLOCK)
1620 return -EAGAIN;
1621 timeout = wait_event_interruptible_timeout
1622 ( cam->wait_frame,
1623 (!list_empty(&cam->outqueue)) ||
1624 (cam->state & DEV_DISCONNECTED) ||
1625 (cam->state & DEV_MISCONFIGURED),
1626 cam->module_param.frame_timeout *
1627 1000 * msecs_to_jiffies(1) );
1628 if (timeout < 0)
1629 return timeout;
1630 if (cam->state & DEV_DISCONNECTED)
1631 return -ENODEV;
1632 if (!timeout || (cam->state & DEV_MISCONFIGURED))
1633 return -EIO;
1634 }
1635
1636 spin_lock_irqsave(&cam->queue_lock, lock_flags);
1637 f = list_entry(cam->outqueue.next, struct zc0301_frame_t, frame);
1638 list_del(cam->outqueue.next);
1639 spin_unlock_irqrestore(&cam->queue_lock, lock_flags);
1640
1641 f->state = F_UNUSED;
1642
1643 memcpy(&b, &f->buf, sizeof(b));
1644 if (f->vma_use_count)
1645 b.flags |= V4L2_BUF_FLAG_MAPPED;
1646
1647 if (copy_to_user(arg, &b, sizeof(b)))
1648 return -EFAULT;
1649
1650 PDBGG("Frame #%lu dequeued", (unsigned long)f->buf.index);
1651
1652 return 0;
1653}
1654
1655
1656static int
1657zc0301_vidioc_streamon(struct zc0301_device* cam, void __user * arg)
1658{
1659 int type;
1660
1661 if (copy_from_user(&type, arg, sizeof(type)))
1662 return -EFAULT;
1663
1664 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1665 return -EINVAL;
1666
1667 if (list_empty(&cam->inqueue))
1668 return -EINVAL;
1669
1670 cam->stream = STREAM_ON;
1671
1672 DBG(3, "Stream on");
1673
1674 return 0;
1675}
1676
1677
1678static int
1679zc0301_vidioc_streamoff(struct zc0301_device* cam, void __user * arg)
1680{
1681 int type, err;
1682
1683 if (copy_from_user(&type, arg, sizeof(type)))
1684 return -EFAULT;
1685
1686 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || cam->io != IO_MMAP)
1687 return -EINVAL;
1688
1689 if (cam->stream == STREAM_ON)
1690 if ((err = zc0301_stream_interrupt(cam)))
1691 return err;
1692
1693 zc0301_empty_framequeues(cam);
1694
1695 DBG(3, "Stream off");
1696
1697 return 0;
1698}
1699
1700
1701static int
1702zc0301_vidioc_g_parm(struct zc0301_device* cam, void __user * arg)
1703{
1704 struct v4l2_streamparm sp;
1705
1706 if (copy_from_user(&sp, arg, sizeof(sp)))
1707 return -EFAULT;
1708
1709 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1710 return -EINVAL;
1711
1712 sp.parm.capture.extendedmode = 0;
1713 sp.parm.capture.readbuffers = cam->nreadbuffers;
1714
1715 if (copy_to_user(arg, &sp, sizeof(sp)))
1716 return -EFAULT;
1717
1718 return 0;
1719}
1720
1721
1722static int
1723zc0301_vidioc_s_parm(struct zc0301_device* cam, void __user * arg)
1724{
1725 struct v4l2_streamparm sp;
1726
1727 if (copy_from_user(&sp, arg, sizeof(sp)))
1728 return -EFAULT;
1729
1730 if (sp.type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1731 return -EINVAL;
1732
1733 sp.parm.capture.extendedmode = 0;
1734
1735 if (sp.parm.capture.readbuffers == 0)
1736 sp.parm.capture.readbuffers = cam->nreadbuffers;
1737
1738 if (sp.parm.capture.readbuffers > ZC0301_MAX_FRAMES)
1739 sp.parm.capture.readbuffers = ZC0301_MAX_FRAMES;
1740
1741 if (copy_to_user(arg, &sp, sizeof(sp)))
1742 return -EFAULT;
1743
1744 cam->nreadbuffers = sp.parm.capture.readbuffers;
1745
1746 return 0;
1747}
1748
1749
1750static int zc0301_ioctl_v4l2(struct inode* inode, struct file* filp,
1751 unsigned int cmd, void __user * arg)
1752{
1753 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1754
1755 switch (cmd) {
1756
1757 case VIDIOC_QUERYCAP:
1758 return zc0301_vidioc_querycap(cam, arg);
1759
1760 case VIDIOC_ENUMINPUT:
1761 return zc0301_vidioc_enuminput(cam, arg);
1762
1763 case VIDIOC_G_INPUT:
1764 return zc0301_vidioc_g_input(cam, arg);
1765
1766 case VIDIOC_S_INPUT:
1767 return zc0301_vidioc_s_input(cam, arg);
1768
1769 case VIDIOC_QUERYCTRL:
1770 return zc0301_vidioc_query_ctrl(cam, arg);
1771
1772 case VIDIOC_G_CTRL:
1773 return zc0301_vidioc_g_ctrl(cam, arg);
1774
1775 case VIDIOC_S_CTRL_OLD:
1776 case VIDIOC_S_CTRL:
1777 return zc0301_vidioc_s_ctrl(cam, arg);
1778
1779 case VIDIOC_CROPCAP_OLD:
1780 case VIDIOC_CROPCAP:
1781 return zc0301_vidioc_cropcap(cam, arg);
1782
1783 case VIDIOC_G_CROP:
1784 return zc0301_vidioc_g_crop(cam, arg);
1785
1786 case VIDIOC_S_CROP:
1787 return zc0301_vidioc_s_crop(cam, arg);
1788
1789 case VIDIOC_ENUM_FMT:
1790 return zc0301_vidioc_enum_fmt(cam, arg);
1791
1792 case VIDIOC_G_FMT:
1793 return zc0301_vidioc_g_fmt(cam, arg);
1794
1795 case VIDIOC_TRY_FMT:
1796 case VIDIOC_S_FMT:
1797 return zc0301_vidioc_try_s_fmt(cam, cmd, arg);
1798
1799 case VIDIOC_G_JPEGCOMP:
1800 return zc0301_vidioc_g_jpegcomp(cam, arg);
1801
1802 case VIDIOC_S_JPEGCOMP:
1803 return zc0301_vidioc_s_jpegcomp(cam, arg);
1804
1805 case VIDIOC_REQBUFS:
1806 return zc0301_vidioc_reqbufs(cam, arg);
1807
1808 case VIDIOC_QUERYBUF:
1809 return zc0301_vidioc_querybuf(cam, arg);
1810
1811 case VIDIOC_QBUF:
1812 return zc0301_vidioc_qbuf(cam, arg);
1813
1814 case VIDIOC_DQBUF:
1815 return zc0301_vidioc_dqbuf(cam, filp, arg);
1816
1817 case VIDIOC_STREAMON:
1818 return zc0301_vidioc_streamon(cam, arg);
1819
1820 case VIDIOC_STREAMOFF:
1821 return zc0301_vidioc_streamoff(cam, arg);
1822
1823 case VIDIOC_G_PARM:
1824 return zc0301_vidioc_g_parm(cam, arg);
1825
1826 case VIDIOC_S_PARM_OLD:
1827 case VIDIOC_S_PARM:
1828 return zc0301_vidioc_s_parm(cam, arg);
1829
1830 case VIDIOC_G_STD:
1831 case VIDIOC_S_STD:
1832 case VIDIOC_QUERYSTD:
1833 case VIDIOC_ENUMSTD:
1834 case VIDIOC_QUERYMENU:
1835 return -EINVAL;
1836
1837 default:
1838 return -EINVAL;
1839
1840 }
1841}
1842
1843
1844static int zc0301_ioctl(struct inode* inode, struct file* filp,
1845 unsigned int cmd, unsigned long arg)
1846{
1847 struct zc0301_device* cam = video_get_drvdata(video_devdata(filp));
1848 int err = 0;
1849
1850 if (mutex_lock_interruptible(&cam->fileop_mutex))
1851 return -ERESTARTSYS;
1852
1853 if (cam->state & DEV_DISCONNECTED) {
1854 DBG(1, "Device not present");
1855 mutex_unlock(&cam->fileop_mutex);
1856 return -ENODEV;
1857 }
1858
1859 if (cam->state & DEV_MISCONFIGURED) {
1860 DBG(1, "The camera is misconfigured. Close and open it "
1861 "again.");
1862 mutex_unlock(&cam->fileop_mutex);
1863 return -EIO;
1864 }
1865
1866 V4LDBG(3, "zc0301", cmd);
1867
1868 err = zc0301_ioctl_v4l2(inode, filp, cmd, (void __user *)arg);
1869
1870 mutex_unlock(&cam->fileop_mutex);
1871
1872 return err;
1873}
1874
1875
1876static struct file_operations zc0301_fops = {
1877 .owner = THIS_MODULE,
1878 .open = zc0301_open,
1879 .release = zc0301_release,
1880 .ioctl = zc0301_ioctl,
1881 .read = zc0301_read,
1882 .poll = zc0301_poll,
1883 .mmap = zc0301_mmap,
1884 .llseek = no_llseek,
1885};
1886
1887/*****************************************************************************/
1888
1889static int
1890zc0301_usb_probe(struct usb_interface* intf, const struct usb_device_id* id)
1891{
1892 struct usb_device *udev = interface_to_usbdev(intf);
1893 struct zc0301_device* cam;
1894 static unsigned int dev_nr = 0;
1895 unsigned int i;
1896 int err = 0;
1897
1898 if (!(cam = kzalloc(sizeof(struct zc0301_device), GFP_KERNEL)))
1899 return -ENOMEM;
1900
1901 cam->usbdev = udev;
1902
1903 if (!(cam->control_buffer = kzalloc(4, GFP_KERNEL))) {
1904 DBG(1, "kmalloc() failed");
1905 err = -ENOMEM;
1906 goto fail;
1907 }
1908
1909 if (!(cam->v4ldev = video_device_alloc())) {
1910 DBG(1, "video_device_alloc() failed");
1911 err = -ENOMEM;
1912 goto fail;
1913 }
1914
1915 mutex_init(&cam->dev_mutex);
1916
1917 DBG(2, "ZC0301 Image Processor and Control Chip detected "
1918 "(vid/pid 0x%04X/0x%04X)",id->idVendor, id->idProduct);
1919
1920 for (i = 0; zc0301_sensor_table[i]; i++) {
1921 err = zc0301_sensor_table[i](cam);
1922 if (!err)
1923 break;
1924 }
1925
1926 if (!err)
1927 DBG(2, "%s image sensor detected", cam->sensor.name);
1928 else {
1929 DBG(1, "No supported image sensor detected");
1930 err = -ENODEV;
1931 goto fail;
1932 }
1933
1934 if (zc0301_init(cam)) {
1935 DBG(1, "Initialization failed. I will retry on open().");
1936 cam->state |= DEV_MISCONFIGURED;
1937 }
1938
1939 strcpy(cam->v4ldev->name, "ZC0301 PC Camera");
1940 cam->v4ldev->owner = THIS_MODULE;
1941 cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
1942 cam->v4ldev->hardware = 0;
1943 cam->v4ldev->fops = &zc0301_fops;
1944 cam->v4ldev->minor = video_nr[dev_nr];
1945 cam->v4ldev->release = video_device_release;
1946 video_set_drvdata(cam->v4ldev, cam);
1947
1948 mutex_lock(&cam->dev_mutex);
1949
1950 err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
1951 video_nr[dev_nr]);
1952 if (err) {
1953 DBG(1, "V4L2 device registration failed");
1954 if (err == -ENFILE && video_nr[dev_nr] == -1)
1955 DBG(1, "Free /dev/videoX node not found");
1956 video_nr[dev_nr] = -1;
1957 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1958 mutex_unlock(&cam->dev_mutex);
1959 goto fail;
1960 }
1961
1962 DBG(2, "V4L2 device registered as /dev/video%d", cam->v4ldev->minor);
1963
1964 cam->module_param.force_munmap = force_munmap[dev_nr];
1965 cam->module_param.frame_timeout = frame_timeout[dev_nr];
1966
1967 dev_nr = (dev_nr < ZC0301_MAX_DEVICES-1) ? dev_nr+1 : 0;
1968
1969 usb_set_intfdata(intf, cam);
1970
1971 mutex_unlock(&cam->dev_mutex);
1972
1973 return 0;
1974
1975fail:
1976 if (cam) {
1977 kfree(cam->control_buffer);
1978 if (cam->v4ldev)
1979 video_device_release(cam->v4ldev);
1980 kfree(cam);
1981 }
1982 return err;
1983}
1984
1985
1986static void zc0301_usb_disconnect(struct usb_interface* intf)
1987{
1988 struct zc0301_device* cam = usb_get_intfdata(intf);
1989
1990 if (!cam)
1991 return;
1992
1993 down_write(&zc0301_disconnect);
1994
1995 mutex_lock(&cam->dev_mutex);
1996
1997 DBG(2, "Disconnecting %s...", cam->v4ldev->name);
1998
1999 wake_up_interruptible_all(&cam->open);
2000
2001 if (cam->users) {
2002 DBG(2, "Device /dev/video%d is open! Deregistration and "
2003 "memory deallocation are deferred on close.",
2004 cam->v4ldev->minor);
2005 cam->state |= DEV_MISCONFIGURED;
2006 zc0301_stop_transfer(cam);
2007 cam->state |= DEV_DISCONNECTED;
2008 wake_up_interruptible(&cam->wait_frame);
2009 wake_up(&cam->wait_stream);
2010 usb_get_dev(cam->usbdev);
2011 } else {
2012 cam->state |= DEV_DISCONNECTED;
2013 zc0301_release_resources(cam);
2014 }
2015
2016 mutex_unlock(&cam->dev_mutex);
2017
2018 if (!cam->users)
2019 kfree(cam);
2020
2021 up_write(&zc0301_disconnect);
2022}
2023
2024
2025static struct usb_driver zc0301_usb_driver = {
2026 .name = "zc0301",
2027 .id_table = zc0301_id_table,
2028 .probe = zc0301_usb_probe,
2029 .disconnect = zc0301_usb_disconnect,
2030};
2031
2032/*****************************************************************************/
2033
2034static int __init zc0301_module_init(void)
2035{
2036 int err = 0;
2037
2038 KDBG(2, ZC0301_MODULE_NAME " v" ZC0301_MODULE_VERSION);
2039 KDBG(3, ZC0301_MODULE_AUTHOR);
2040
2041 if ((err = usb_register(&zc0301_usb_driver)))
2042 KDBG(1, "usb_register() failed");
2043
2044 return err;
2045}
2046
2047
2048static void __exit zc0301_module_exit(void)
2049{
2050 usb_deregister(&zc0301_usb_driver);
2051}
2052
2053
2054module_init(zc0301_module_init);
2055module_exit(zc0301_module_exit);
diff --git a/drivers/usb/media/zc0301_pas202bcb.c b/drivers/usb/media/zc0301_pas202bcb.c
new file mode 100644
index 000000000000..9d282a22c15f
--- /dev/null
+++ b/drivers/usb/media/zc0301_pas202bcb.c
@@ -0,0 +1,361 @@
1/***************************************************************************
2 * Plug-in for PAS202BCB image sensor connected to the ZC030! Image *
3 * Processor and Control Chip *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * Initialization values of the ZC0301 have been taken from the SPCA5XX *
8 * driver maintained by Michel Xhaard <mxhaard@magic.fr> *
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 as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the Free Software *
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
23 ***************************************************************************/
24
25/*
26 NOTE: Sensor controls are disabled for now, becouse changing them while
27 streaming sometimes results in out-of-sync video frames. We'll use
28 the default initialization, until we know how to stop and start video
29 in the chip. However, the image quality still looks good under various
30 light conditions.
31*/
32
33#include <linux/delay.h>
34#include "zc0301_sensor.h"
35
36
37static struct zc0301_sensor pas202bcb;
38
39
40static int pas202bcb_init(struct zc0301_device* cam)
41{
42 int err = 0;
43
44 err += zc0301_write_reg(cam, 0x0002, 0x00);
45 err += zc0301_write_reg(cam, 0x0003, 0x02);
46 err += zc0301_write_reg(cam, 0x0004, 0x80);
47 err += zc0301_write_reg(cam, 0x0005, 0x01);
48 err += zc0301_write_reg(cam, 0x0006, 0xE0);
49 err += zc0301_write_reg(cam, 0x0098, 0x00);
50 err += zc0301_write_reg(cam, 0x009A, 0x03);
51 err += zc0301_write_reg(cam, 0x011A, 0x00);
52 err += zc0301_write_reg(cam, 0x011C, 0x03);
53 err += zc0301_write_reg(cam, 0x009B, 0x01);
54 err += zc0301_write_reg(cam, 0x009C, 0xE6);
55 err += zc0301_write_reg(cam, 0x009D, 0x02);
56 err += zc0301_write_reg(cam, 0x009E, 0x86);
57
58 err += zc0301_i2c_write(cam, 0x02, 0x02);
59 err += zc0301_i2c_write(cam, 0x0A, 0x01);
60 err += zc0301_i2c_write(cam, 0x0B, 0x01);
61 err += zc0301_i2c_write(cam, 0x0D, 0x00);
62 err += zc0301_i2c_write(cam, 0x12, 0x05);
63 err += zc0301_i2c_write(cam, 0x13, 0x63);
64 err += zc0301_i2c_write(cam, 0x15, 0x70);
65
66 err += zc0301_write_reg(cam, 0x0101, 0xB7);
67 err += zc0301_write_reg(cam, 0x0100, 0x0D);
68 err += zc0301_write_reg(cam, 0x0189, 0x06);
69 err += zc0301_write_reg(cam, 0x01AD, 0x00);
70 err += zc0301_write_reg(cam, 0x01C5, 0x03);
71 err += zc0301_write_reg(cam, 0x01CB, 0x13);
72 err += zc0301_write_reg(cam, 0x0250, 0x08);
73 err += zc0301_write_reg(cam, 0x0301, 0x08);
74 err += zc0301_write_reg(cam, 0x018D, 0x70);
75 err += zc0301_write_reg(cam, 0x0008, 0x03);
76 err += zc0301_write_reg(cam, 0x01C6, 0x04);
77 err += zc0301_write_reg(cam, 0x01CB, 0x07);
78 err += zc0301_write_reg(cam, 0x0120, 0x11);
79 err += zc0301_write_reg(cam, 0x0121, 0x37);
80 err += zc0301_write_reg(cam, 0x0122, 0x58);
81 err += zc0301_write_reg(cam, 0x0123, 0x79);
82 err += zc0301_write_reg(cam, 0x0124, 0x91);
83 err += zc0301_write_reg(cam, 0x0125, 0xA6);
84 err += zc0301_write_reg(cam, 0x0126, 0xB8);
85 err += zc0301_write_reg(cam, 0x0127, 0xC7);
86 err += zc0301_write_reg(cam, 0x0128, 0xD3);
87 err += zc0301_write_reg(cam, 0x0129, 0xDE);
88 err += zc0301_write_reg(cam, 0x012A, 0xE6);
89 err += zc0301_write_reg(cam, 0x012B, 0xED);
90 err += zc0301_write_reg(cam, 0x012C, 0xF3);
91 err += zc0301_write_reg(cam, 0x012D, 0xF8);
92 err += zc0301_write_reg(cam, 0x012E, 0xFB);
93 err += zc0301_write_reg(cam, 0x012F, 0xFF);
94 err += zc0301_write_reg(cam, 0x0130, 0x26);
95 err += zc0301_write_reg(cam, 0x0131, 0x23);
96 err += zc0301_write_reg(cam, 0x0132, 0x20);
97 err += zc0301_write_reg(cam, 0x0133, 0x1C);
98 err += zc0301_write_reg(cam, 0x0134, 0x16);
99 err += zc0301_write_reg(cam, 0x0135, 0x13);
100 err += zc0301_write_reg(cam, 0x0136, 0x10);
101 err += zc0301_write_reg(cam, 0x0137, 0x0D);
102 err += zc0301_write_reg(cam, 0x0138, 0x0B);
103 err += zc0301_write_reg(cam, 0x0139, 0x09);
104 err += zc0301_write_reg(cam, 0x013A, 0x07);
105 err += zc0301_write_reg(cam, 0x013B, 0x06);
106 err += zc0301_write_reg(cam, 0x013C, 0x05);
107 err += zc0301_write_reg(cam, 0x013D, 0x04);
108 err += zc0301_write_reg(cam, 0x013E, 0x03);
109 err += zc0301_write_reg(cam, 0x013F, 0x02);
110 err += zc0301_write_reg(cam, 0x010A, 0x4C);
111 err += zc0301_write_reg(cam, 0x010B, 0xF5);
112 err += zc0301_write_reg(cam, 0x010C, 0xFF);
113 err += zc0301_write_reg(cam, 0x010D, 0xF9);
114 err += zc0301_write_reg(cam, 0x010E, 0x51);
115 err += zc0301_write_reg(cam, 0x010F, 0xF5);
116 err += zc0301_write_reg(cam, 0x0110, 0xFB);
117 err += zc0301_write_reg(cam, 0x0111, 0xED);
118 err += zc0301_write_reg(cam, 0x0112, 0x5F);
119 err += zc0301_write_reg(cam, 0x0180, 0x00);
120 err += zc0301_write_reg(cam, 0x0019, 0x00);
121 err += zc0301_write_reg(cam, 0x0087, 0x20);
122 err += zc0301_write_reg(cam, 0x0088, 0x21);
123
124 err += zc0301_i2c_write(cam, 0x20, 0x02);
125 err += zc0301_i2c_write(cam, 0x21, 0x1B);
126 err += zc0301_i2c_write(cam, 0x03, 0x44);
127 err += zc0301_i2c_write(cam, 0x0E, 0x01);
128 err += zc0301_i2c_write(cam, 0x0F, 0x00);
129
130 err += zc0301_write_reg(cam, 0x01A9, 0x14);
131 err += zc0301_write_reg(cam, 0x01AA, 0x24);
132 err += zc0301_write_reg(cam, 0x0190, 0x00);
133 err += zc0301_write_reg(cam, 0x0191, 0x02);
134 err += zc0301_write_reg(cam, 0x0192, 0x1B);
135 err += zc0301_write_reg(cam, 0x0195, 0x00);
136 err += zc0301_write_reg(cam, 0x0196, 0x00);
137 err += zc0301_write_reg(cam, 0x0197, 0x4D);
138 err += zc0301_write_reg(cam, 0x018C, 0x10);
139 err += zc0301_write_reg(cam, 0x018F, 0x20);
140 err += zc0301_write_reg(cam, 0x001D, 0x44);
141 err += zc0301_write_reg(cam, 0x001E, 0x6F);
142 err += zc0301_write_reg(cam, 0x001F, 0xAD);
143 err += zc0301_write_reg(cam, 0x0020, 0xEB);
144 err += zc0301_write_reg(cam, 0x0087, 0x0F);
145 err += zc0301_write_reg(cam, 0x0088, 0x0E);
146 err += zc0301_write_reg(cam, 0x0180, 0x40);
147 err += zc0301_write_reg(cam, 0x0192, 0x1B);
148 err += zc0301_write_reg(cam, 0x0191, 0x02);
149 err += zc0301_write_reg(cam, 0x0190, 0x00);
150 err += zc0301_write_reg(cam, 0x0116, 0x1D);
151 err += zc0301_write_reg(cam, 0x0117, 0x40);
152 err += zc0301_write_reg(cam, 0x0118, 0x99);
153 err += zc0301_write_reg(cam, 0x0180, 0x42);
154 err += zc0301_write_reg(cam, 0x0116, 0x1D);
155 err += zc0301_write_reg(cam, 0x0117, 0x40);
156 err += zc0301_write_reg(cam, 0x0118, 0x99);
157 err += zc0301_write_reg(cam, 0x0007, 0x00);
158
159 err += zc0301_i2c_write(cam, 0x11, 0x01);
160
161 msleep(100);
162
163 return err;
164}
165
166
167static int pas202bcb_get_ctrl(struct zc0301_device* cam,
168 struct v4l2_control* ctrl)
169{
170 switch (ctrl->id) {
171 case V4L2_CID_EXPOSURE:
172 {
173 int r1 = zc0301_i2c_read(cam, 0x04, 1),
174 r2 = zc0301_i2c_read(cam, 0x05, 1);
175 if (r1 < 0 || r2 < 0)
176 return -EIO;
177 ctrl->value = (r1 << 6) | (r2 & 0x3f);
178 }
179 return 0;
180 case V4L2_CID_RED_BALANCE:
181 if ((ctrl->value = zc0301_i2c_read(cam, 0x09, 1)) < 0)
182 return -EIO;
183 ctrl->value &= 0x0f;
184 return 0;
185 case V4L2_CID_BLUE_BALANCE:
186 if ((ctrl->value = zc0301_i2c_read(cam, 0x07, 1)) < 0)
187 return -EIO;
188 ctrl->value &= 0x0f;
189 return 0;
190 case V4L2_CID_GAIN:
191 if ((ctrl->value = zc0301_i2c_read(cam, 0x10, 1)) < 0)
192 return -EIO;
193 ctrl->value &= 0x1f;
194 return 0;
195 case ZC0301_V4L2_CID_GREEN_BALANCE:
196 if ((ctrl->value = zc0301_i2c_read(cam, 0x08, 1)) < 0)
197 return -EIO;
198 ctrl->value &= 0x0f;
199 return 0;
200 case ZC0301_V4L2_CID_DAC_MAGNITUDE:
201 if ((ctrl->value = zc0301_i2c_read(cam, 0x0c, 1)) < 0)
202 return -EIO;
203 return 0;
204 default:
205 return -EINVAL;
206 }
207}
208
209
210static int pas202bcb_set_ctrl(struct zc0301_device* cam,
211 const struct v4l2_control* ctrl)
212{
213 int err = 0;
214
215 switch (ctrl->id) {
216 case V4L2_CID_EXPOSURE:
217 err += zc0301_i2c_write(cam, 0x04, ctrl->value >> 6);
218 err += zc0301_i2c_write(cam, 0x05, ctrl->value & 0x3f);
219 break;
220 case V4L2_CID_RED_BALANCE:
221 err += zc0301_i2c_write(cam, 0x09, ctrl->value);
222 break;
223 case V4L2_CID_BLUE_BALANCE:
224 err += zc0301_i2c_write(cam, 0x07, ctrl->value);
225 break;
226 case V4L2_CID_GAIN:
227 err += zc0301_i2c_write(cam, 0x10, ctrl->value);
228 break;
229 case ZC0301_V4L2_CID_GREEN_BALANCE:
230 err += zc0301_i2c_write(cam, 0x08, ctrl->value);
231 break;
232 case ZC0301_V4L2_CID_DAC_MAGNITUDE:
233 err += zc0301_i2c_write(cam, 0x0c, ctrl->value);
234 break;
235 default:
236 return -EINVAL;
237 }
238 err += zc0301_i2c_write(cam, 0x11, 0x01);
239
240 return err ? -EIO : 0;
241}
242
243
244static struct zc0301_sensor pas202bcb = {
245 .name = "PAS202BCB",
246 .init = &pas202bcb_init,
247 .qctrl = {
248 {
249 .id = V4L2_CID_EXPOSURE,
250 .type = V4L2_CTRL_TYPE_INTEGER,
251 .name = "exposure",
252 .minimum = 0x01e5,
253 .maximum = 0x3fff,
254 .step = 0x0001,
255 .default_value = 0x01e5,
256 .flags = V4L2_CTRL_FLAG_DISABLED,
257 },
258 {
259 .id = V4L2_CID_GAIN,
260 .type = V4L2_CTRL_TYPE_INTEGER,
261 .name = "global gain",
262 .minimum = 0x00,
263 .maximum = 0x1f,
264 .step = 0x01,
265 .default_value = 0x0c,
266 .flags = V4L2_CTRL_FLAG_DISABLED,
267 },
268 {
269 .id = ZC0301_V4L2_CID_DAC_MAGNITUDE,
270 .type = V4L2_CTRL_TYPE_INTEGER,
271 .name = "DAC magnitude",
272 .minimum = 0x00,
273 .maximum = 0xff,
274 .step = 0x01,
275 .default_value = 0x00,
276 .flags = V4L2_CTRL_FLAG_DISABLED,
277 },
278 {
279 .id = V4L2_CID_RED_BALANCE,
280 .type = V4L2_CTRL_TYPE_INTEGER,
281 .name = "red balance",
282 .minimum = 0x00,
283 .maximum = 0x0f,
284 .step = 0x01,
285 .default_value = 0x01,
286 .flags = V4L2_CTRL_FLAG_DISABLED,
287 },
288 {
289 .id = V4L2_CID_BLUE_BALANCE,
290 .type = V4L2_CTRL_TYPE_INTEGER,
291 .name = "blue balance",
292 .minimum = 0x00,
293 .maximum = 0x0f,
294 .step = 0x01,
295 .default_value = 0x05,
296 .flags = V4L2_CTRL_FLAG_DISABLED,
297 },
298 {
299 .id = ZC0301_V4L2_CID_GREEN_BALANCE,
300 .type = V4L2_CTRL_TYPE_INTEGER,
301 .name = "green balance",
302 .minimum = 0x00,
303 .maximum = 0x0f,
304 .step = 0x01,
305 .default_value = 0x00,
306 .flags = V4L2_CTRL_FLAG_DISABLED,
307 },
308 },
309 .get_ctrl = &pas202bcb_get_ctrl,
310 .set_ctrl = &pas202bcb_set_ctrl,
311 .cropcap = {
312 .bounds = {
313 .left = 0,
314 .top = 0,
315 .width = 640,
316 .height = 480,
317 },
318 .defrect = {
319 .left = 0,
320 .top = 0,
321 .width = 640,
322 .height = 480,
323 },
324 },
325 .pix_format = {
326 .width = 640,
327 .height = 480,
328 .pixelformat = V4L2_PIX_FMT_JPEG,
329 .priv = 8,
330 },
331};
332
333
334int zc0301_probe_pas202bcb(struct zc0301_device* cam)
335{
336 int r0 = 0, r1 = 0, err = 0;
337 unsigned int pid = 0;
338
339 err += zc0301_write_reg(cam, 0x0000, 0x01);
340 err += zc0301_write_reg(cam, 0x0010, 0x0e);
341 err += zc0301_write_reg(cam, 0x0001, 0x01);
342 err += zc0301_write_reg(cam, 0x0012, 0x03);
343 err += zc0301_write_reg(cam, 0x0012, 0x01);
344 err += zc0301_write_reg(cam, 0x008d, 0x08);
345
346 msleep(10);
347
348 r0 = zc0301_i2c_read(cam, 0x00, 1);
349 r1 = zc0301_i2c_read(cam, 0x01, 1);
350
351 if (r0 < 0 || r1 < 0 || err)
352 return -EIO;
353
354 pid = (r0 << 4) | ((r1 & 0xf0) >> 4);
355 if (pid != 0x017)
356 return -ENODEV;
357
358 zc0301_attach_sensor(cam, &pas202bcb);
359
360 return 0;
361}
diff --git a/drivers/usb/media/zc0301_sensor.h b/drivers/usb/media/zc0301_sensor.h
new file mode 100644
index 000000000000..cf0965a81d01
--- /dev/null
+++ b/drivers/usb/media/zc0301_sensor.h
@@ -0,0 +1,103 @@
1/***************************************************************************
2 * API for image sensors connected to the ZC030! Image Processor and *
3 * Control Chip *
4 * *
5 * Copyright (C) 2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
21
22#ifndef _ZC0301_SENSOR_H_
23#define _ZC0301_SENSOR_H_
24
25#include <linux/usb.h>
26#include <linux/videodev.h>
27#include <linux/device.h>
28#include <linux/stddef.h>
29#include <linux/errno.h>
30#include <asm/types.h>
31
32struct zc0301_device;
33struct zc0301_sensor;
34
35/*****************************************************************************/
36
37extern int zc0301_probe_pas202bcb(struct zc0301_device* cam);
38
39#define ZC0301_SENSOR_TABLE \
40/* Weak detections must go at the end of the list */ \
41static int (*zc0301_sensor_table[])(struct zc0301_device*) = { \
42 &zc0301_probe_pas202bcb, \
43 NULL, \
44};
45
46extern struct zc0301_device*
47zc0301_match_id(struct zc0301_device* cam, const struct usb_device_id *id);
48
49extern void
50zc0301_attach_sensor(struct zc0301_device* cam, struct zc0301_sensor* sensor);
51
52#define ZC0301_USB_DEVICE(vend, prod, intclass) \
53 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
54 USB_DEVICE_ID_MATCH_INT_CLASS, \
55 .idVendor = (vend), \
56 .idProduct = (prod), \
57 .bInterfaceClass = (intclass)
58
59#define ZC0301_ID_TABLE \
60static const struct usb_device_id zc0301_id_table[] = { \
61 { ZC0301_USB_DEVICE(0x041e, 0x4017, 0xff), }, \
62 { ZC0301_USB_DEVICE(0x041e, 0x401c, 0xff), }, /* PAS106 */ \
63 { ZC0301_USB_DEVICE(0x041e, 0x401e, 0xff), }, /* HV7131B */ \
64 { ZC0301_USB_DEVICE(0x041e, 0x4034, 0xff), }, /* PAS106 */ \
65 { ZC0301_USB_DEVICE(0x041e, 0x4035, 0xff), }, /* PAS106 */ \
66 { ZC0301_USB_DEVICE(0x046d, 0x08ae, 0xff), }, /* PAS202BCB */ \
67 { ZC0301_USB_DEVICE(0x0ac8, 0x0301, 0xff), }, \
68 { ZC0301_USB_DEVICE(0x10fd, 0x8050, 0xff), }, /* TAS5130D */ \
69 { } \
70};
71
72/*****************************************************************************/
73
74extern int zc0301_write_reg(struct zc0301_device*, u16 index, u16 value);
75extern int zc0301_read_reg(struct zc0301_device*, u16 index);
76extern int zc0301_i2c_write(struct zc0301_device*, u16 address, u16 value);
77extern int zc0301_i2c_read(struct zc0301_device*, u16 address, u8 length);
78
79/*****************************************************************************/
80
81#define ZC0301_MAX_CTRLS V4L2_CID_LASTP1-V4L2_CID_BASE+10
82#define ZC0301_V4L2_CID_DAC_MAGNITUDE V4L2_CID_PRIVATE_BASE
83#define ZC0301_V4L2_CID_GREEN_BALANCE V4L2_CID_PRIVATE_BASE + 1
84
85struct zc0301_sensor {
86 char name[32];
87
88 struct v4l2_queryctrl qctrl[ZC0301_MAX_CTRLS];
89 struct v4l2_cropcap cropcap;
90 struct v4l2_pix_format pix_format;
91
92 int (*init)(struct zc0301_device*);
93 int (*get_ctrl)(struct zc0301_device*, struct v4l2_control* ctrl);
94 int (*set_ctrl)(struct zc0301_device*,
95 const struct v4l2_control* ctrl);
96 int (*set_crop)(struct zc0301_device*, const struct v4l2_rect* rect);
97
98 /* Private */
99 struct v4l2_queryctrl _qctrl[ZC0301_MAX_CTRLS];
100 struct v4l2_rect _rect;
101};
102
103#endif /* _ZC0301_SENSOR_H_ */
diff --git a/drivers/usb/misc/auerswald.c b/drivers/usb/misc/auerswald.c
index ad2f4cccd388..1fef36e71c57 100644
--- a/drivers/usb/misc/auerswald.c
+++ b/drivers/usb/misc/auerswald.c
@@ -570,10 +570,9 @@ static int auerchain_setup (pauerchain_t acp, unsigned int numElements)
570 570
571 /* fill the list of free elements */ 571 /* fill the list of free elements */
572 for (;numElements; numElements--) { 572 for (;numElements; numElements--) {
573 acep = (pauerchainelement_t) kmalloc (sizeof (auerchainelement_t), GFP_KERNEL); 573 acep = kzalloc(sizeof(auerchainelement_t), GFP_KERNEL);
574 if (!acep) 574 if (!acep)
575 goto ac_fail; 575 goto ac_fail;
576 memset (acep, 0, sizeof (auerchainelement_t));
577 INIT_LIST_HEAD (&acep->list); 576 INIT_LIST_HEAD (&acep->list);
578 list_add_tail (&acep->list, &acp->free_list); 577 list_add_tail (&acep->list, &acp->free_list);
579 } 578 }
@@ -761,10 +760,9 @@ static int auerbuf_setup (pauerbufctl_t bcp, unsigned int numElements, unsigned
761 760
762 /* fill the list of free elements */ 761 /* fill the list of free elements */
763 for (;numElements; numElements--) { 762 for (;numElements; numElements--) {
764 bep = (pauerbuf_t) kmalloc (sizeof (auerbuf_t), GFP_KERNEL); 763 bep = kzalloc(sizeof(auerbuf_t), GFP_KERNEL);
765 if (!bep) 764 if (!bep)
766 goto bl_fail; 765 goto bl_fail;
767 memset (bep, 0, sizeof (auerbuf_t));
768 bep->list = bcp; 766 bep->list = bcp;
769 INIT_LIST_HEAD (&bep->buff_list); 767 INIT_LIST_HEAD (&bep->buff_list);
770 bep->bufp = kmalloc (bufsize, GFP_KERNEL); 768 bep->bufp = kmalloc (bufsize, GFP_KERNEL);
diff --git a/drivers/usb/misc/cytherm.c b/drivers/usb/misc/cytherm.c
index 6671317b495f..a04204292aa3 100644
--- a/drivers/usb/misc/cytherm.c
+++ b/drivers/usb/misc/cytherm.c
@@ -351,12 +351,11 @@ static int cytherm_probe(struct usb_interface *interface,
351 struct usb_cytherm *dev = NULL; 351 struct usb_cytherm *dev = NULL;
352 int retval = -ENOMEM; 352 int retval = -ENOMEM;
353 353
354 dev = kmalloc (sizeof(struct usb_cytherm), GFP_KERNEL); 354 dev = kzalloc (sizeof(struct usb_cytherm), GFP_KERNEL);
355 if (dev == NULL) { 355 if (dev == NULL) {
356 dev_err (&interface->dev, "Out of memory\n"); 356 dev_err (&interface->dev, "Out of memory\n");
357 goto error; 357 goto error;
358 } 358 }
359 memset (dev, 0x00, sizeof (*dev));
360 359
361 dev->udev = usb_get_dev(udev); 360 dev->udev = usb_get_dev(udev);
362 361
diff --git a/drivers/usb/misc/idmouse.c b/drivers/usb/misc/idmouse.c
index d8cde1017985..d0b167256699 100644
--- a/drivers/usb/misc/idmouse.c
+++ b/drivers/usb/misc/idmouse.c
@@ -25,6 +25,7 @@
25#include <linux/module.h> 25#include <linux/module.h>
26#include <linux/smp_lock.h> 26#include <linux/smp_lock.h>
27#include <linux/completion.h> 27#include <linux/completion.h>
28#include <linux/mutex.h>
28#include <asm/uaccess.h> 29#include <asm/uaccess.h>
29#include <linux/usb.h> 30#include <linux/usb.h>
30 31
@@ -121,7 +122,7 @@ static struct usb_driver idmouse_driver = {
121}; 122};
122 123
123/* prevent races between open() and disconnect() */ 124/* prevent races between open() and disconnect() */
124static DECLARE_MUTEX(disconnect_sem); 125static DEFINE_MUTEX(disconnect_mutex);
125 126
126static int idmouse_create_image(struct usb_idmouse *dev) 127static int idmouse_create_image(struct usb_idmouse *dev)
127{ 128{
@@ -213,18 +214,18 @@ static int idmouse_open(struct inode *inode, struct file *file)
213 int result = 0; 214 int result = 0;
214 215
215 /* prevent disconnects */ 216 /* prevent disconnects */
216 down(&disconnect_sem); 217 mutex_lock(&disconnect_mutex);
217 218
218 /* get the interface from minor number and driver information */ 219 /* get the interface from minor number and driver information */
219 interface = usb_find_interface (&idmouse_driver, iminor (inode)); 220 interface = usb_find_interface (&idmouse_driver, iminor (inode));
220 if (!interface) { 221 if (!interface) {
221 up(&disconnect_sem); 222 mutex_unlock(&disconnect_mutex);
222 return -ENODEV; 223 return -ENODEV;
223 } 224 }
224 /* get the device information block from the interface */ 225 /* get the device information block from the interface */
225 dev = usb_get_intfdata(interface); 226 dev = usb_get_intfdata(interface);
226 if (!dev) { 227 if (!dev) {
227 up(&disconnect_sem); 228 mutex_unlock(&disconnect_mutex);
228 return -ENODEV; 229 return -ENODEV;
229 } 230 }
230 231
@@ -258,7 +259,7 @@ error:
258 up(&dev->sem); 259 up(&dev->sem);
259 260
260 /* unlock the disconnect semaphore */ 261 /* unlock the disconnect semaphore */
261 up(&disconnect_sem); 262 mutex_unlock(&disconnect_mutex);
262 return result; 263 return result;
263} 264}
264 265
@@ -267,12 +268,12 @@ static int idmouse_release(struct inode *inode, struct file *file)
267 struct usb_idmouse *dev; 268 struct usb_idmouse *dev;
268 269
269 /* prevent a race condition with open() */ 270 /* prevent a race condition with open() */
270 down(&disconnect_sem); 271 mutex_lock(&disconnect_mutex);
271 272
272 dev = (struct usb_idmouse *) file->private_data; 273 dev = (struct usb_idmouse *) file->private_data;
273 274
274 if (dev == NULL) { 275 if (dev == NULL) {
275 up(&disconnect_sem); 276 mutex_unlock(&disconnect_mutex);
276 return -ENODEV; 277 return -ENODEV;
277 } 278 }
278 279
@@ -282,7 +283,7 @@ static int idmouse_release(struct inode *inode, struct file *file)
282 /* are we really open? */ 283 /* are we really open? */
283 if (dev->open <= 0) { 284 if (dev->open <= 0) {
284 up(&dev->sem); 285 up(&dev->sem);
285 up(&disconnect_sem); 286 mutex_unlock(&disconnect_mutex);
286 return -ENODEV; 287 return -ENODEV;
287 } 288 }
288 289
@@ -292,12 +293,12 @@ static int idmouse_release(struct inode *inode, struct file *file)
292 /* the device was unplugged before the file was released */ 293 /* the device was unplugged before the file was released */
293 up(&dev->sem); 294 up(&dev->sem);
294 idmouse_delete(dev); 295 idmouse_delete(dev);
295 up(&disconnect_sem); 296 mutex_unlock(&disconnect_mutex);
296 return 0; 297 return 0;
297 } 298 }
298 299
299 up(&dev->sem); 300 up(&dev->sem);
300 up(&disconnect_sem); 301 mutex_unlock(&disconnect_mutex);
301 return 0; 302 return 0;
302} 303}
303 304
@@ -340,10 +341,9 @@ static int idmouse_probe(struct usb_interface *interface,
340 return -ENODEV; 341 return -ENODEV;
341 342
342 /* allocate memory for our device state and initialize it */ 343 /* allocate memory for our device state and initialize it */
343 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 344 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
344 if (dev == NULL) 345 if (dev == NULL)
345 return -ENOMEM; 346 return -ENOMEM;
346 memset(dev, 0x00, sizeof(*dev));
347 347
348 init_MUTEX(&dev->sem); 348 init_MUTEX(&dev->sem);
349 dev->udev = udev; 349 dev->udev = udev;
@@ -400,7 +400,7 @@ static void idmouse_disconnect(struct usb_interface *interface)
400 struct usb_idmouse *dev; 400 struct usb_idmouse *dev;
401 401
402 /* prevent races with open() */ 402 /* prevent races with open() */
403 down(&disconnect_sem); 403 mutex_lock(&disconnect_mutex);
404 404
405 /* get device structure */ 405 /* get device structure */
406 dev = usb_get_intfdata(interface); 406 dev = usb_get_intfdata(interface);
@@ -422,7 +422,7 @@ static void idmouse_disconnect(struct usb_interface *interface)
422 if (!dev->open) 422 if (!dev->open)
423 idmouse_delete(dev); 423 idmouse_delete(dev);
424 424
425 up(&disconnect_sem); 425 mutex_unlock(&disconnect_mutex);
426 426
427 info("%s disconnected", DRIVER_DESC); 427 info("%s disconnected", DRIVER_DESC);
428} 428}
diff --git a/drivers/usb/misc/ldusb.c b/drivers/usb/misc/ldusb.c
index e2d1198623eb..966acb474f67 100644
--- a/drivers/usb/misc/ldusb.c
+++ b/drivers/usb/misc/ldusb.c
@@ -33,6 +33,7 @@
33#include <linux/init.h> 33#include <linux/init.h>
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/module.h> 35#include <linux/module.h>
36#include <linux/mutex.h>
36 37
37#include <asm/uaccess.h> 38#include <asm/uaccess.h>
38#include <linux/input.h> 39#include <linux/input.h>
@@ -172,7 +173,7 @@ struct ld_usb {
172}; 173};
173 174
174/* prevent races between open() and disconnect() */ 175/* prevent races between open() and disconnect() */
175static DECLARE_MUTEX(disconnect_sem); 176static DEFINE_MUTEX(disconnect_mutex);
176 177
177static struct usb_driver ld_usb_driver; 178static struct usb_driver ld_usb_driver;
178 179
@@ -293,7 +294,7 @@ static int ld_usb_open(struct inode *inode, struct file *file)
293 nonseekable_open(inode, file); 294 nonseekable_open(inode, file);
294 subminor = iminor(inode); 295 subminor = iminor(inode);
295 296
296 down(&disconnect_sem); 297 mutex_lock(&disconnect_mutex);
297 298
298 interface = usb_find_interface(&ld_usb_driver, subminor); 299 interface = usb_find_interface(&ld_usb_driver, subminor);
299 300
@@ -355,7 +356,7 @@ unlock_exit:
355 up(&dev->sem); 356 up(&dev->sem);
356 357
357unlock_disconnect_exit: 358unlock_disconnect_exit:
358 up(&disconnect_sem); 359 mutex_unlock(&disconnect_mutex);
359 360
360 return retval; 361 return retval;
361} 362}
@@ -626,12 +627,11 @@ static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *
626 627
627 /* allocate memory for our device state and intialize it */ 628 /* allocate memory for our device state and intialize it */
628 629
629 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 630 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
630 if (dev == NULL) { 631 if (dev == NULL) {
631 dev_err(&intf->dev, "Out of memory\n"); 632 dev_err(&intf->dev, "Out of memory\n");
632 goto exit; 633 goto exit;
633 } 634 }
634 memset(dev, 0x00, sizeof(*dev));
635 init_MUTEX(&dev->sem); 635 init_MUTEX(&dev->sem);
636 dev->intf = intf; 636 dev->intf = intf;
637 init_waitqueue_head(&dev->read_wait); 637 init_waitqueue_head(&dev->read_wait);
@@ -741,7 +741,7 @@ static void ld_usb_disconnect(struct usb_interface *intf)
741 struct ld_usb *dev; 741 struct ld_usb *dev;
742 int minor; 742 int minor;
743 743
744 down(&disconnect_sem); 744 mutex_lock(&disconnect_mutex);
745 745
746 dev = usb_get_intfdata(intf); 746 dev = usb_get_intfdata(intf);
747 usb_set_intfdata(intf, NULL); 747 usb_set_intfdata(intf, NULL);
@@ -762,7 +762,7 @@ static void ld_usb_disconnect(struct usb_interface *intf)
762 up(&dev->sem); 762 up(&dev->sem);
763 } 763 }
764 764
765 up(&disconnect_sem); 765 mutex_unlock(&disconnect_mutex);
766 766
767 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", 767 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
768 (minor - USB_LD_MINOR_BASE)); 768 (minor - USB_LD_MINOR_BASE));
diff --git a/drivers/usb/misc/legousbtower.c b/drivers/usb/misc/legousbtower.c
index 1336745b8f55..779bcf0373ad 100644
--- a/drivers/usb/misc/legousbtower.c
+++ b/drivers/usb/misc/legousbtower.c
@@ -83,6 +83,7 @@
83#include <linux/module.h> 83#include <linux/module.h>
84#include <linux/smp_lock.h> 84#include <linux/smp_lock.h>
85#include <linux/completion.h> 85#include <linux/completion.h>
86#include <linux/mutex.h>
86#include <asm/uaccess.h> 87#include <asm/uaccess.h>
87#include <linux/usb.h> 88#include <linux/usb.h>
88#include <linux/poll.h> 89#include <linux/poll.h>
@@ -256,7 +257,7 @@ static void tower_disconnect (struct usb_interface *interface);
256 257
257 258
258/* prevent races between open() and disconnect */ 259/* prevent races between open() and disconnect */
259static DECLARE_MUTEX (disconnect_sem); 260static DEFINE_MUTEX (disconnect_mutex);
260 261
261/* file operations needed when we register this driver */ 262/* file operations needed when we register this driver */
262static struct file_operations tower_fops = { 263static struct file_operations tower_fops = {
@@ -349,7 +350,7 @@ static int tower_open (struct inode *inode, struct file *file)
349 nonseekable_open(inode, file); 350 nonseekable_open(inode, file);
350 subminor = iminor(inode); 351 subminor = iminor(inode);
351 352
352 down (&disconnect_sem); 353 mutex_lock (&disconnect_mutex);
353 354
354 interface = usb_find_interface (&tower_driver, subminor); 355 interface = usb_find_interface (&tower_driver, subminor);
355 356
@@ -427,7 +428,7 @@ unlock_exit:
427 up (&dev->sem); 428 up (&dev->sem);
428 429
429unlock_disconnect_exit: 430unlock_disconnect_exit:
430 up (&disconnect_sem); 431 mutex_unlock (&disconnect_mutex);
431 432
432 dbg(2, "%s: leave, return value %d ", __FUNCTION__, retval); 433 dbg(2, "%s: leave, return value %d ", __FUNCTION__, retval);
433 434
@@ -1005,7 +1006,7 @@ static void tower_disconnect (struct usb_interface *interface)
1005 1006
1006 dbg(2, "%s: enter", __FUNCTION__); 1007 dbg(2, "%s: enter", __FUNCTION__);
1007 1008
1008 down (&disconnect_sem); 1009 mutex_lock (&disconnect_mutex);
1009 1010
1010 dev = usb_get_intfdata (interface); 1011 dev = usb_get_intfdata (interface);
1011 usb_set_intfdata (interface, NULL); 1012 usb_set_intfdata (interface, NULL);
@@ -1027,7 +1028,7 @@ static void tower_disconnect (struct usb_interface *interface)
1027 up (&dev->sem); 1028 up (&dev->sem);
1028 } 1029 }
1029 1030
1030 up (&disconnect_sem); 1031 mutex_unlock (&disconnect_mutex);
1031 1032
1032 info("LEGO USB Tower #%d now disconnected", (minor - LEGO_USB_TOWER_MINOR_BASE)); 1033 info("LEGO USB Tower #%d now disconnected", (minor - LEGO_USB_TOWER_MINOR_BASE));
1033 1034
diff --git a/drivers/usb/misc/phidgetkit.c b/drivers/usb/misc/phidgetkit.c
index 605a3c87e05c..997db5d8e35b 100644
--- a/drivers/usb/misc/phidgetkit.c
+++ b/drivers/usb/misc/phidgetkit.c
@@ -88,7 +88,7 @@ static int change_outputs(struct phidget_interfacekit *kit, int output_num, int
88 int retval; 88 int retval;
89 int n; 89 int n;
90 90
91 buffer = kmalloc(4, GFP_KERNEL); 91 buffer = kzalloc(4, GFP_KERNEL);
92 if (!buffer) { 92 if (!buffer) {
93 dev_err(&kit->udev->dev, "%s - out of memory\n", 93 dev_err(&kit->udev->dev, "%s - out of memory\n",
94 __FUNCTION__); 94 __FUNCTION__);
@@ -96,7 +96,6 @@ static int change_outputs(struct phidget_interfacekit *kit, int output_num, int
96 } 96 }
97 97
98 kit->outputs[output_num] = enable; 98 kit->outputs[output_num] = enable;
99 memset(buffer, 0, 4);
100 for (n=0; n<8; n++) { 99 for (n=0; n<8; n++) {
101 if (kit->outputs[n]) { 100 if (kit->outputs[n]) {
102 buffer[0] |= 1 << n; 101 buffer[0] |= 1 << n;
@@ -192,7 +191,7 @@ static ssize_t set_backlight(struct device *dev, struct device_attribute *attr,
192 unsigned char *buffer; 191 unsigned char *buffer;
193 int retval = -ENOMEM; 192 int retval = -ENOMEM;
194 193
195 buffer = kmalloc(8, GFP_KERNEL); 194 buffer = kzalloc(8, GFP_KERNEL);
196 if (!buffer) { 195 if (!buffer) {
197 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__); 196 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
198 goto exit; 197 goto exit;
@@ -202,7 +201,6 @@ static ssize_t set_backlight(struct device *dev, struct device_attribute *attr,
202 retval = -EINVAL; 201 retval = -EINVAL;
203 goto exit; 202 goto exit;
204 } 203 }
205 memset(buffer, 0x00, 8);
206 if (enabled) 204 if (enabled)
207 buffer[0] = 0x01; 205 buffer[0] = 0x01;
208 buffer[7] = 0x11; 206 buffer[7] = 0x11;
@@ -406,12 +404,11 @@ static int interfacekit_probe(struct usb_interface *intf, const struct usb_devic
406 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); 404 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
407 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); 405 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
408 406
409 kit = kmalloc(sizeof(*kit), GFP_KERNEL); 407 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
410 if (kit == NULL) { 408 if (kit == NULL) {
411 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__); 409 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__);
412 return -ENOMEM; 410 return -ENOMEM;
413 } 411 }
414 memset(kit, 0, sizeof(*kit));
415 kit->ifkit = ifkit; 412 kit->ifkit = ifkit;
416 413
417 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma); 414 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma);
diff --git a/drivers/usb/misc/phidgetservo.c b/drivers/usb/misc/phidgetservo.c
index b3418d2bcc69..5a040c205eed 100644
--- a/drivers/usb/misc/phidgetservo.c
+++ b/drivers/usb/misc/phidgetservo.c
@@ -252,12 +252,11 @@ servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
252 struct usb_device *udev = interface_to_usbdev(interface); 252 struct usb_device *udev = interface_to_usbdev(interface);
253 struct phidget_servo *dev; 253 struct phidget_servo *dev;
254 254
255 dev = kmalloc(sizeof (struct phidget_servo), GFP_KERNEL); 255 dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
256 if (dev == NULL) { 256 if (dev == NULL) {
257 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__); 257 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
258 return -ENOMEM; 258 return -ENOMEM;
259 } 259 }
260 memset(dev, 0x00, sizeof (*dev));
261 260
262 dev->udev = usb_get_dev(udev); 261 dev->udev = usb_get_dev(udev);
263 dev->type = id->driver_info; 262 dev->type = id->driver_info;
diff --git a/drivers/usb/misc/sisusbvga/sisusb.c b/drivers/usb/misc/sisusbvga/sisusb.c
index 3260d595441f..196c8794a73c 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.c
+++ b/drivers/usb/misc/sisusbvga/sisusb.c
@@ -3188,7 +3188,7 @@ sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
3188 break; 3188 break;
3189 3189
3190 default: 3190 default:
3191 retval = -EINVAL; 3191 retval = -ENOTTY;
3192 break; 3192 break;
3193 } 3193 }
3194 3194
@@ -3251,12 +3251,11 @@ static int sisusb_probe(struct usb_interface *intf,
3251 dev->devnum); 3251 dev->devnum);
3252 3252
3253 /* Allocate memory for our private */ 3253 /* Allocate memory for our private */
3254 if (!(sisusb = kmalloc(sizeof(*sisusb), GFP_KERNEL))) { 3254 if (!(sisusb = kzalloc(sizeof(*sisusb), GFP_KERNEL))) {
3255 printk(KERN_ERR 3255 printk(KERN_ERR
3256 "sisusb: Failed to allocate memory for private data\n"); 3256 "sisusb: Failed to allocate memory for private data\n");
3257 return -ENOMEM; 3257 return -ENOMEM;
3258 } 3258 }
3259 memset(sisusb, 0, sizeof(*sisusb));
3260 kref_init(&sisusb->kref); 3259 kref_init(&sisusb->kref);
3261 3260
3262 init_MUTEX(&(sisusb->lock)); 3261 init_MUTEX(&(sisusb->lock));
diff --git a/drivers/usb/misc/sisusbvga/sisusb.h b/drivers/usb/misc/sisusbvga/sisusb.h
index 1d7a77cc7c4a..a716825d1f9b 100644
--- a/drivers/usb/misc/sisusbvga/sisusb.h
+++ b/drivers/usb/misc/sisusbvga/sisusb.h
@@ -37,24 +37,16 @@
37#ifndef _SISUSB_H_ 37#ifndef _SISUSB_H_
38#define _SISUSB_H_ 38#define _SISUSB_H_
39 39
40#include <linux/version.h>
41#ifdef CONFIG_COMPAT 40#ifdef CONFIG_COMPAT
42#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,10)
43#include <linux/ioctl32.h>
44#define SISUSB_OLD_CONFIG_COMPAT
45#else
46#define SISUSB_NEW_CONFIG_COMPAT 41#define SISUSB_NEW_CONFIG_COMPAT
47#endif 42#endif
48#endif
49 43
50/* For older kernels, support for text consoles is by default 44/* For older kernels, support for text consoles is by default
51 * off. To ensable text console support, change the following: 45 * off. To ensable text console support, change the following:
52 */ 46 */
53#if 0 47#if 0
54#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,13)
55#define CONFIG_USB_SISUSBVGA_CON 48#define CONFIG_USB_SISUSBVGA_CON
56#endif 49#endif
57#endif
58 50
59/* Version Information */ 51/* Version Information */
60 52
diff --git a/drivers/usb/misc/usblcd.c b/drivers/usb/misc/usblcd.c
index cc3dae3f34e0..c82c402285a0 100644
--- a/drivers/usb/misc/usblcd.c
+++ b/drivers/usb/misc/usblcd.c
@@ -270,12 +270,11 @@ static int lcd_probe(struct usb_interface *interface, const struct usb_device_id
270 int retval = -ENOMEM; 270 int retval = -ENOMEM;
271 271
272 /* allocate memory for our device state and initialize it */ 272 /* allocate memory for our device state and initialize it */
273 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 273 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
274 if (dev == NULL) { 274 if (dev == NULL) {
275 err("Out of memory"); 275 err("Out of memory");
276 goto error; 276 goto error;
277 } 277 }
278 memset(dev, 0x00, sizeof(*dev));
279 kref_init(&dev->kref); 278 kref_init(&dev->kref);
280 279
281 dev->udev = usb_get_dev(interface_to_usbdev(interface)); 280 dev->udev = usb_get_dev(interface_to_usbdev(interface));
diff --git a/drivers/usb/misc/usbled.c b/drivers/usb/misc/usbled.c
index 877b081a3a6e..f441964132c0 100644
--- a/drivers/usb/misc/usbled.c
+++ b/drivers/usb/misc/usbled.c
@@ -106,12 +106,11 @@ static int led_probe(struct usb_interface *interface, const struct usb_device_id
106 struct usb_led *dev = NULL; 106 struct usb_led *dev = NULL;
107 int retval = -ENOMEM; 107 int retval = -ENOMEM;
108 108
109 dev = kmalloc(sizeof(struct usb_led), GFP_KERNEL); 109 dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL);
110 if (dev == NULL) { 110 if (dev == NULL) {
111 dev_err(&interface->dev, "Out of memory\n"); 111 dev_err(&interface->dev, "Out of memory\n");
112 goto error; 112 goto error;
113 } 113 }
114 memset (dev, 0x00, sizeof (*dev));
115 114
116 dev->udev = usb_get_dev(udev); 115 dev->udev = usb_get_dev(udev);
117 116
diff --git a/drivers/usb/misc/usbtest.c b/drivers/usb/misc/usbtest.c
index 84fa1728f052..9d59b901841c 100644
--- a/drivers/usb/misc/usbtest.c
+++ b/drivers/usb/misc/usbtest.c
@@ -382,12 +382,11 @@ alloc_sglist (int nents, int max, int vary)
382 for (i = 0; i < nents; i++) { 382 for (i = 0; i < nents; i++) {
383 char *buf; 383 char *buf;
384 384
385 buf = kmalloc (size, SLAB_KERNEL); 385 buf = kzalloc (size, SLAB_KERNEL);
386 if (!buf) { 386 if (!buf) {
387 free_sglist (sg, i); 387 free_sglist (sg, i);
388 return NULL; 388 return NULL;
389 } 389 }
390 memset (buf, 0, size);
391 390
392 /* kmalloc pages are always physically contiguous! */ 391 /* kmalloc pages are always physically contiguous! */
393 sg_init_one(&sg[i], buf, size); 392 sg_init_one(&sg[i], buf, size);
@@ -842,10 +841,9 @@ test_ctrl_queue (struct usbtest_dev *dev, struct usbtest_param *param)
842 * as with bulk/intr sglists, sglen is the queue depth; it also 841 * as with bulk/intr sglists, sglen is the queue depth; it also
843 * controls which subtests run (more tests than sglen) or rerun. 842 * controls which subtests run (more tests than sglen) or rerun.
844 */ 843 */
845 urb = kmalloc (param->sglen * sizeof (struct urb *), SLAB_KERNEL); 844 urb = kcalloc(param->sglen, sizeof(struct urb *), SLAB_KERNEL);
846 if (!urb) 845 if (!urb)
847 return -ENOMEM; 846 return -ENOMEM;
848 memset (urb, 0, param->sglen * sizeof (struct urb *));
849 for (i = 0; i < param->sglen; i++) { 847 for (i = 0; i < param->sglen; i++) {
850 int pipe = usb_rcvctrlpipe (udev, 0); 848 int pipe = usb_rcvctrlpipe (udev, 0);
851 unsigned len; 849 unsigned len;
@@ -1865,10 +1863,9 @@ usbtest_probe (struct usb_interface *intf, const struct usb_device_id *id)
1865 } 1863 }
1866#endif 1864#endif
1867 1865
1868 dev = kmalloc (sizeof *dev, SLAB_KERNEL); 1866 dev = kzalloc(sizeof(*dev), SLAB_KERNEL);
1869 if (!dev) 1867 if (!dev)
1870 return -ENOMEM; 1868 return -ENOMEM;
1871 memset (dev, 0, sizeof *dev);
1872 info = (struct usbtest_info *) id->driver_info; 1869 info = (struct usbtest_info *) id->driver_info;
1873 dev->info = info; 1870 dev->info = info;
1874 init_MUTEX (&dev->sem); 1871 init_MUTEX (&dev->sem);
diff --git a/drivers/usb/mon/mon_main.c b/drivers/usb/mon/mon_main.c
index c34944c75047..6ecc27302211 100644
--- a/drivers/usb/mon/mon_main.c
+++ b/drivers/usb/mon/mon_main.c
@@ -12,6 +12,7 @@
12#include <linux/debugfs.h> 12#include <linux/debugfs.h>
13#include <linux/smp_lock.h> 13#include <linux/smp_lock.h>
14#include <linux/notifier.h> 14#include <linux/notifier.h>
15#include <linux/mutex.h>
15 16
16#include "usb_mon.h" 17#include "usb_mon.h"
17#include "../core/hcd.h" 18#include "../core/hcd.h"
@@ -23,7 +24,7 @@ static void mon_dissolve(struct mon_bus *mbus, struct usb_bus *ubus);
23static void mon_bus_drop(struct kref *r); 24static void mon_bus_drop(struct kref *r);
24static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus); 25static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus);
25 26
26DECLARE_MUTEX(mon_lock); 27DEFINE_MUTEX(mon_lock);
27 28
28static struct dentry *mon_dir; /* /dbg/usbmon */ 29static struct dentry *mon_dir; /* /dbg/usbmon */
29static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */ 30static LIST_HEAD(mon_buses); /* All buses we know: struct mon_bus */
@@ -196,14 +197,14 @@ static void mon_bus_remove(struct usb_bus *ubus)
196{ 197{
197 struct mon_bus *mbus = ubus->mon_bus; 198 struct mon_bus *mbus = ubus->mon_bus;
198 199
199 down(&mon_lock); 200 mutex_lock(&mon_lock);
200 list_del(&mbus->bus_link); 201 list_del(&mbus->bus_link);
201 debugfs_remove(mbus->dent_t); 202 debugfs_remove(mbus->dent_t);
202 debugfs_remove(mbus->dent_s); 203 debugfs_remove(mbus->dent_s);
203 204
204 mon_dissolve(mbus, ubus); 205 mon_dissolve(mbus, ubus);
205 kref_put(&mbus->ref, mon_bus_drop); 206 kref_put(&mbus->ref, mon_bus_drop);
206 up(&mon_lock); 207 mutex_unlock(&mon_lock);
207} 208}
208 209
209static int mon_notify(struct notifier_block *self, unsigned long action, 210static int mon_notify(struct notifier_block *self, unsigned long action,
@@ -276,9 +277,8 @@ static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
276 char name[NAMESZ]; 277 char name[NAMESZ];
277 int rc; 278 int rc;
278 279
279 if ((mbus = kmalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL) 280 if ((mbus = kzalloc(sizeof(struct mon_bus), GFP_KERNEL)) == NULL)
280 goto err_alloc; 281 goto err_alloc;
281 memset(mbus, 0, sizeof(struct mon_bus));
282 kref_init(&mbus->ref); 282 kref_init(&mbus->ref);
283 spin_lock_init(&mbus->lock); 283 spin_lock_init(&mbus->lock);
284 INIT_LIST_HEAD(&mbus->r_list); 284 INIT_LIST_HEAD(&mbus->r_list);
@@ -307,9 +307,9 @@ static void mon_bus_init(struct dentry *mondir, struct usb_bus *ubus)
307 goto err_create_s; 307 goto err_create_s;
308 mbus->dent_s = d; 308 mbus->dent_s = d;
309 309
310 down(&mon_lock); 310 mutex_lock(&mon_lock);
311 list_add_tail(&mbus->bus_link, &mon_buses); 311 list_add_tail(&mbus->bus_link, &mon_buses);
312 up(&mon_lock); 312 mutex_unlock(&mon_lock);
313 return; 313 return;
314 314
315err_create_s: 315err_create_s:
@@ -347,11 +347,11 @@ static int __init mon_init(void)
347 347
348 usb_register_notify(&mon_nb); 348 usb_register_notify(&mon_nb);
349 349
350 down(&usb_bus_list_lock); 350 mutex_lock(&usb_bus_list_lock);
351 list_for_each_entry (ubus, &usb_bus_list, bus_list) { 351 list_for_each_entry (ubus, &usb_bus_list, bus_list) {
352 mon_bus_init(mondir, ubus); 352 mon_bus_init(mondir, ubus);
353 } 353 }
354 up(&usb_bus_list_lock); 354 mutex_unlock(&usb_bus_list_lock);
355 return 0; 355 return 0;
356} 356}
357 357
@@ -363,7 +363,7 @@ static void __exit mon_exit(void)
363 usb_unregister_notify(&mon_nb); 363 usb_unregister_notify(&mon_nb);
364 usb_mon_deregister(); 364 usb_mon_deregister();
365 365
366 down(&mon_lock); 366 mutex_lock(&mon_lock);
367 while (!list_empty(&mon_buses)) { 367 while (!list_empty(&mon_buses)) {
368 p = mon_buses.next; 368 p = mon_buses.next;
369 mbus = list_entry(p, struct mon_bus, bus_link); 369 mbus = list_entry(p, struct mon_bus, bus_link);
@@ -387,7 +387,7 @@ static void __exit mon_exit(void)
387 mon_dissolve(mbus, mbus->u_bus); 387 mon_dissolve(mbus, mbus->u_bus);
388 kref_put(&mbus->ref, mon_bus_drop); 388 kref_put(&mbus->ref, mon_bus_drop);
389 } 389 }
390 up(&mon_lock); 390 mutex_unlock(&mon_lock);
391 391
392 debugfs_remove(mon_dir); 392 debugfs_remove(mon_dir);
393} 393}
diff --git a/drivers/usb/mon/mon_text.c b/drivers/usb/mon/mon_text.c
index 611612146ae9..ac043ec2b8dc 100644
--- a/drivers/usb/mon/mon_text.c
+++ b/drivers/usb/mon/mon_text.c
@@ -8,6 +8,7 @@
8#include <linux/list.h> 8#include <linux/list.h>
9#include <linux/usb.h> 9#include <linux/usb.h>
10#include <linux/time.h> 10#include <linux/time.h>
11#include <linux/mutex.h>
11#include <asm/uaccess.h> 12#include <asm/uaccess.h>
12 13
13#include "usb_mon.h" 14#include "usb_mon.h"
@@ -54,7 +55,7 @@ struct mon_reader_text {
54 wait_queue_head_t wait; 55 wait_queue_head_t wait;
55 int printf_size; 56 int printf_size;
56 char *printf_buf; 57 char *printf_buf;
57 struct semaphore printf_lock; 58 struct mutex printf_lock;
58 59
59 char slab_name[SLAB_NAME_SZ]; 60 char slab_name[SLAB_NAME_SZ];
60}; 61};
@@ -208,19 +209,18 @@ static int mon_text_open(struct inode *inode, struct file *file)
208 struct mon_reader_text *rp; 209 struct mon_reader_text *rp;
209 int rc; 210 int rc;
210 211
211 down(&mon_lock); 212 mutex_lock(&mon_lock);
212 mbus = inode->u.generic_ip; 213 mbus = inode->u.generic_ip;
213 ubus = mbus->u_bus; 214 ubus = mbus->u_bus;
214 215
215 rp = kmalloc(sizeof(struct mon_reader_text), GFP_KERNEL); 216 rp = kzalloc(sizeof(struct mon_reader_text), GFP_KERNEL);
216 if (rp == NULL) { 217 if (rp == NULL) {
217 rc = -ENOMEM; 218 rc = -ENOMEM;
218 goto err_alloc; 219 goto err_alloc;
219 } 220 }
220 memset(rp, 0, sizeof(struct mon_reader_text));
221 INIT_LIST_HEAD(&rp->e_list); 221 INIT_LIST_HEAD(&rp->e_list);
222 init_waitqueue_head(&rp->wait); 222 init_waitqueue_head(&rp->wait);
223 init_MUTEX(&rp->printf_lock); 223 mutex_init(&rp->printf_lock);
224 224
225 rp->printf_size = PRINTF_DFL; 225 rp->printf_size = PRINTF_DFL;
226 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL); 226 rp->printf_buf = kmalloc(rp->printf_size, GFP_KERNEL);
@@ -247,7 +247,7 @@ static int mon_text_open(struct inode *inode, struct file *file)
247 mon_reader_add(mbus, &rp->r); 247 mon_reader_add(mbus, &rp->r);
248 248
249 file->private_data = rp; 249 file->private_data = rp;
250 up(&mon_lock); 250 mutex_unlock(&mon_lock);
251 return 0; 251 return 0;
252 252
253// err_busy: 253// err_busy:
@@ -257,7 +257,7 @@ err_slab:
257err_alloc_pr: 257err_alloc_pr:
258 kfree(rp); 258 kfree(rp);
259err_alloc: 259err_alloc:
260 up(&mon_lock); 260 mutex_unlock(&mon_lock);
261 return rc; 261 return rc;
262} 262}
263 263
@@ -301,7 +301,7 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
301 set_current_state(TASK_RUNNING); 301 set_current_state(TASK_RUNNING);
302 remove_wait_queue(&rp->wait, &waita); 302 remove_wait_queue(&rp->wait, &waita);
303 303
304 down(&rp->printf_lock); 304 mutex_lock(&rp->printf_lock);
305 cnt = 0; 305 cnt = 0;
306 pbuf = rp->printf_buf; 306 pbuf = rp->printf_buf;
307 limit = rp->printf_size; 307 limit = rp->printf_size;
@@ -358,7 +358,7 @@ static ssize_t mon_text_read(struct file *file, char __user *buf,
358 358
359 if (copy_to_user(buf, rp->printf_buf, cnt)) 359 if (copy_to_user(buf, rp->printf_buf, cnt))
360 cnt = -EFAULT; 360 cnt = -EFAULT;
361 up(&rp->printf_lock); 361 mutex_unlock(&rp->printf_lock);
362 kmem_cache_free(rp->e_slab, ep); 362 kmem_cache_free(rp->e_slab, ep);
363 return cnt; 363 return cnt;
364} 364}
@@ -371,12 +371,12 @@ static int mon_text_release(struct inode *inode, struct file *file)
371 struct list_head *p; 371 struct list_head *p;
372 struct mon_event_text *ep; 372 struct mon_event_text *ep;
373 373
374 down(&mon_lock); 374 mutex_lock(&mon_lock);
375 mbus = inode->u.generic_ip; 375 mbus = inode->u.generic_ip;
376 376
377 if (mbus->nreaders <= 0) { 377 if (mbus->nreaders <= 0) {
378 printk(KERN_ERR TAG ": consistency error on close\n"); 378 printk(KERN_ERR TAG ": consistency error on close\n");
379 up(&mon_lock); 379 mutex_unlock(&mon_lock);
380 return 0; 380 return 0;
381 } 381 }
382 mon_reader_del(mbus, &rp->r); 382 mon_reader_del(mbus, &rp->r);
@@ -402,7 +402,7 @@ static int mon_text_release(struct inode *inode, struct file *file)
402 kfree(rp->printf_buf); 402 kfree(rp->printf_buf);
403 kfree(rp); 403 kfree(rp);
404 404
405 up(&mon_lock); 405 mutex_unlock(&mon_lock);
406 return 0; 406 return 0;
407} 407}
408 408
diff --git a/drivers/usb/mon/usb_mon.h b/drivers/usb/mon/usb_mon.h
index 4be0f9346071..8e0613c350cc 100644
--- a/drivers/usb/mon/usb_mon.h
+++ b/drivers/usb/mon/usb_mon.h
@@ -49,7 +49,7 @@ void mon_reader_del(struct mon_bus *mbus, struct mon_reader *r);
49 */ 49 */
50extern char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len); 50extern char mon_dmapeek(unsigned char *dst, dma_addr_t dma_addr, int len);
51 51
52extern struct semaphore mon_lock; 52extern struct mutex mon_lock;
53 53
54extern struct file_operations mon_fops_text; 54extern struct file_operations mon_fops_text;
55extern struct file_operations mon_fops_stat; 55extern struct file_operations mon_fops_stat;
diff --git a/drivers/usb/net/pegasus.c b/drivers/usb/net/pegasus.c
index 156a2f1cb39a..5b6675684567 100644
--- a/drivers/usb/net/pegasus.c
+++ b/drivers/usb/net/pegasus.c
@@ -524,6 +524,7 @@ static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
524 ret = set_registers(pegasus, EthCtrl0, 3, data); 524 ret = set_registers(pegasus, EthCtrl0, 3, data);
525 525
526 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS || 526 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
527 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
527 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) { 528 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
528 u16 auxmode; 529 u16 auxmode;
529 read_mii_word(pegasus, 0, 0x1b, &auxmode); 530 read_mii_word(pegasus, 0, 0x1b, &auxmode);
diff --git a/drivers/usb/net/pegasus.h b/drivers/usb/net/pegasus.h
index 9fbd59b55cb6..a54752ce1493 100644
--- a/drivers/usb/net/pegasus.h
+++ b/drivers/usb/net/pegasus.h
@@ -25,7 +25,6 @@
25#define PHY_READ 0x40 25#define PHY_READ 0x40
26#define PHY_WRITE 0x20 26#define PHY_WRITE 0x20
27#define DEFAULT_GPIO_RESET 0x24 27#define DEFAULT_GPIO_RESET 0x24
28#define LINKSYS_GPIO_RESET 0x24
29#define DEFAULT_GPIO_SET 0x26 28#define DEFAULT_GPIO_SET 0x26
30 29
31#define PEGASUS_PRESENT 0x00000001 30#define PEGASUS_PRESENT 0x00000001
@@ -140,6 +139,7 @@ struct usb_eth_dev {
140#define VENDOR_KINGSTON 0x0951 139#define VENDOR_KINGSTON 0x0951
141#define VENDOR_LANEED 0x056e 140#define VENDOR_LANEED 0x056e
142#define VENDOR_LINKSYS 0x066b 141#define VENDOR_LINKSYS 0x066b
142#define VENDOR_LINKSYS2 0x077b
143#define VENDOR_MELCO 0x0411 143#define VENDOR_MELCO 0x0411
144#define VENDOR_MICROSOFT 0x045e 144#define VENDOR_MICROSOFT 0x045e
145#define VENDOR_MOBILITY 0x1342 145#define VENDOR_MOBILITY 0x1342
@@ -218,15 +218,15 @@ PEGASUS_DEV( "Corega FEter USB-TX", VENDOR_COREGA, 0x0004,
218PEGASUS_DEV( "Corega FEter USB-TXS", VENDOR_COREGA, 0x000d, 218PEGASUS_DEV( "Corega FEter USB-TXS", VENDOR_COREGA, 0x000d,
219 DEFAULT_GPIO_RESET | PEGASUS_II ) 219 DEFAULT_GPIO_RESET | PEGASUS_II )
220PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4001, 220PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4001,
221 LINKSYS_GPIO_RESET ) 221 DEFAULT_GPIO_RESET )
222PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4002, 222PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4002,
223 LINKSYS_GPIO_RESET ) 223 DEFAULT_GPIO_RESET )
224PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4102, 224PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x4102,
225 LINKSYS_GPIO_RESET | PEGASUS_II ) 225 DEFAULT_GPIO_RESET | PEGASUS_II )
226PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x400b, 226PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x400b,
227 LINKSYS_GPIO_RESET | PEGASUS_II ) 227 DEFAULT_GPIO_RESET | PEGASUS_II )
228PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x200c, 228PEGASUS_DEV( "D-Link DSB-650TX", VENDOR_DLINK, 0x200c,
229 LINKSYS_GPIO_RESET | PEGASUS_II ) 229 DEFAULT_GPIO_RESET | PEGASUS_II )
230PEGASUS_DEV( "D-Link DSB-650TX(PNA)", VENDOR_DLINK, 0x4003, 230PEGASUS_DEV( "D-Link DSB-650TX(PNA)", VENDOR_DLINK, 0x4003,
231 DEFAULT_GPIO_RESET | HAS_HOME_PNA ) 231 DEFAULT_GPIO_RESET | HAS_HOME_PNA )
232PEGASUS_DEV( "D-Link DSB-650", VENDOR_DLINK, 0xabc1, 232PEGASUS_DEV( "D-Link DSB-650", VENDOR_DLINK, 0xabc1,
@@ -260,17 +260,19 @@ PEGASUS_DEV( "LANEED USB Ethernet LD-USB/T", VENDOR_LANEED, 0xabc1,
260PEGASUS_DEV( "LANEED USB Ethernet LD-USB/TX", VENDOR_LANEED, 0x200c, 260PEGASUS_DEV( "LANEED USB Ethernet LD-USB/TX", VENDOR_LANEED, 0x200c,
261 DEFAULT_GPIO_RESET | PEGASUS_II ) 261 DEFAULT_GPIO_RESET | PEGASUS_II )
262PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x2202, 262PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x2202,
263 LINKSYS_GPIO_RESET ) 263 DEFAULT_GPIO_RESET )
264PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2203, 264PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2203,
265 LINKSYS_GPIO_RESET ) 265 DEFAULT_GPIO_RESET )
266PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2204, 266PEGASUS_DEV( "Linksys USB100TX", VENDOR_LINKSYS, 0x2204,
267 LINKSYS_GPIO_RESET | HAS_HOME_PNA ) 267 DEFAULT_GPIO_RESET | HAS_HOME_PNA )
268PEGASUS_DEV( "Linksys USB10T Ethernet Adapter", VENDOR_LINKSYS, 0x2206, 268PEGASUS_DEV( "Linksys USB10T Ethernet Adapter", VENDOR_LINKSYS, 0x2206,
269 LINKSYS_GPIO_RESET | PEGASUS_II) 269 DEFAULT_GPIO_RESET | PEGASUS_II)
270PEGASUS_DEV( "Linksys USBVPN1", VENDOR_LINKSYS2, 0x08b4,
271 DEFAULT_GPIO_RESET )
270PEGASUS_DEV( "Linksys USB USB100TX", VENDOR_LINKSYS, 0x400b, 272PEGASUS_DEV( "Linksys USB USB100TX", VENDOR_LINKSYS, 0x400b,
271 LINKSYS_GPIO_RESET | PEGASUS_II ) 273 DEFAULT_GPIO_RESET | PEGASUS_II )
272PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x200c, 274PEGASUS_DEV( "Linksys USB10TX", VENDOR_LINKSYS, 0x200c,
273 LINKSYS_GPIO_RESET | PEGASUS_II ) 275 DEFAULT_GPIO_RESET | PEGASUS_II )
274PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0001, 276PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0001,
275 DEFAULT_GPIO_RESET ) 277 DEFAULT_GPIO_RESET )
276PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0005, 278PEGASUS_DEV( "MELCO/BUFFALO LUA-TX", VENDOR_MELCO, 0x0005,
diff --git a/drivers/usb/net/rtl8150.c b/drivers/usb/net/rtl8150.c
index 8ca52be23976..1bbbae283c0b 100644
--- a/drivers/usb/net/rtl8150.c
+++ b/drivers/usb/net/rtl8150.c
@@ -880,7 +880,6 @@ static int rtl8150_probe(struct usb_interface *intf,
880 } 880 }
881 fill_skb_pool(dev); 881 fill_skb_pool(dev);
882 set_ethernet_addr(dev); 882 set_ethernet_addr(dev);
883 info("%s: rtl8150 is detected", netdev->name);
884 883
885 usb_set_intfdata(intf, dev); 884 usb_set_intfdata(intf, dev);
886 SET_NETDEV_DEV(netdev, &intf->dev); 885 SET_NETDEV_DEV(netdev, &intf->dev);
@@ -888,6 +887,9 @@ static int rtl8150_probe(struct usb_interface *intf,
888 err("couldn't register the device"); 887 err("couldn't register the device");
889 goto out2; 888 goto out2;
890 } 889 }
890
891 info("%s: rtl8150 is detected", netdev->name);
892
891 return 0; 893 return 0;
892 894
893out2: 895out2:
diff --git a/drivers/usb/net/zd1201.c b/drivers/usb/net/zd1201.c
index f3a8e2807c3b..fe9b60cd8d95 100644
--- a/drivers/usb/net/zd1201.c
+++ b/drivers/usb/net/zd1201.c
@@ -621,10 +621,9 @@ static int zd1201_drvr_start(struct zd1201 *zd)
621 __le16 zdmax; 621 __le16 zdmax;
622 unsigned char *buffer; 622 unsigned char *buffer;
623 623
624 buffer = kmalloc(ZD1201_RXSIZE, GFP_KERNEL); 624 buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
625 if (!buffer) 625 if (!buffer)
626 return -ENOMEM; 626 return -ENOMEM;
627 memset(buffer, 0, ZD1201_RXSIZE);
628 627
629 usb_fill_bulk_urb(zd->rx_urb, zd->usb, 628 usb_fill_bulk_urb(zd->rx_urb, zd->usb,
630 usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE, 629 usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
@@ -1750,11 +1749,9 @@ static int zd1201_probe(struct usb_interface *interface,
1750 1749
1751 usb = interface_to_usbdev(interface); 1750 usb = interface_to_usbdev(interface);
1752 1751
1753 zd = kmalloc(sizeof(struct zd1201), GFP_KERNEL); 1752 zd = kzalloc(sizeof(struct zd1201), GFP_KERNEL);
1754 if (!zd) { 1753 if (!zd)
1755 return -ENOMEM; 1754 return -ENOMEM;
1756 }
1757 memset(zd, 0, sizeof(struct zd1201));
1758 zd->ap = ap; 1755 zd->ap = ap;
1759 zd->usb = usb; 1756 zd->usb = usb;
1760 zd->removed = 0; 1757 zd->removed = 0;
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index be5dc80836c3..5a8a2c91c2b2 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -403,6 +403,13 @@ config USB_SERIAL_MCT_U232
403 To compile this driver as a module, choose M here: the 403 To compile this driver as a module, choose M here: the
404 module will be called mct_u232. 404 module will be called mct_u232.
405 405
406config USB_SERIAL_NAVMAN
407 tristate "USB Navman GPS device"
408 depends on USB_SERIAL
409 help
410 To compile this driver as a module, choose M here: the
411 module will be called navman.
412
406config USB_SERIAL_PL2303 413config USB_SERIAL_PL2303
407 tristate "USB Prolific 2303 Single Port Serial Driver" 414 tristate "USB Prolific 2303 Single Port Serial Driver"
408 depends on USB_SERIAL 415 depends on USB_SERIAL
diff --git a/drivers/usb/serial/Makefile b/drivers/usb/serial/Makefile
index f0b04420cea1..f7fe4172efed 100644
--- a/drivers/usb/serial/Makefile
+++ b/drivers/usb/serial/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_USB_SERIAL_KEYSPAN_PDA) += keyspan_pda.o
32obj-$(CONFIG_USB_SERIAL_KLSI) += kl5kusb105.o 32obj-$(CONFIG_USB_SERIAL_KLSI) += kl5kusb105.o
33obj-$(CONFIG_USB_SERIAL_KOBIL_SCT) += kobil_sct.o 33obj-$(CONFIG_USB_SERIAL_KOBIL_SCT) += kobil_sct.o
34obj-$(CONFIG_USB_SERIAL_MCT_U232) += mct_u232.o 34obj-$(CONFIG_USB_SERIAL_MCT_U232) += mct_u232.o
35obj-$(CONFIG_USB_SERIAL_NAVMAN) += navman.o
35obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o 36obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o
36obj-$(CONFIG_USB_SERIAL_OPTION) += option.o 37obj-$(CONFIG_USB_SERIAL_OPTION) += option.o
37obj-$(CONFIG_USB_SERIAL_PL2303) += pl2303.o 38obj-$(CONFIG_USB_SERIAL_PL2303) += pl2303.o
diff --git a/drivers/usb/serial/cp2101.c b/drivers/usb/serial/cp2101.c
index dc7a069503e0..e0c2acdb3f06 100644
--- a/drivers/usb/serial/cp2101.c
+++ b/drivers/usb/serial/cp2101.c
@@ -32,7 +32,7 @@
32/* 32/*
33 * Version Information 33 * Version Information
34 */ 34 */
35#define DRIVER_VERSION "v0.06" 35#define DRIVER_VERSION "v0.07"
36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver" 36#define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
37 37
38/* 38/*
@@ -58,6 +58,7 @@ static struct usb_device_id id_table [] = {
58 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */ 58 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
59 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */ 59 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
60 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */ 60 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
61 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
61 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */ 62 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
62 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */ 63 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
63 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */ 64 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
@@ -169,9 +170,7 @@ static int cp2101_get_config(struct usb_serial_port* port, u8 request,
169 /* Number of integers required to contain the array */ 170 /* Number of integers required to contain the array */
170 length = (((size - 1) | 3) + 1)/4; 171 length = (((size - 1) | 3) + 1)/4;
171 172
172 buf = kmalloc (length * sizeof(u32), GFP_KERNEL); 173 buf = kcalloc(length, sizeof(u32), GFP_KERNEL);
173 memset(buf, 0, length * sizeof(u32));
174
175 if (!buf) { 174 if (!buf) {
176 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); 175 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
177 return -ENOMEM; 176 return -ENOMEM;
diff --git a/drivers/usb/serial/cypress_m8.c b/drivers/usb/serial/cypress_m8.c
index 68067fe117a4..7212fbe3b6f2 100644
--- a/drivers/usb/serial/cypress_m8.c
+++ b/drivers/usb/serial/cypress_m8.c
@@ -98,10 +98,16 @@ static struct usb_device_id id_table_cyphidcomrs232 [] = {
98 { } /* Terminating entry */ 98 { } /* Terminating entry */
99}; 99};
100 100
101static struct usb_device_id id_table_nokiaca42v2 [] = {
102 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
103 { } /* Terminating entry */
104};
105
101static struct usb_device_id id_table_combined [] = { 106static struct usb_device_id id_table_combined [] = {
102 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) }, 107 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
103 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) }, 108 { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
104 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) }, 109 { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
110 { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
105 { } /* Terminating entry */ 111 { } /* Terminating entry */
106}; 112};
107 113
@@ -149,6 +155,7 @@ struct cypress_buf {
149/* function prototypes for the Cypress USB to serial device */ 155/* function prototypes for the Cypress USB to serial device */
150static int cypress_earthmate_startup (struct usb_serial *serial); 156static int cypress_earthmate_startup (struct usb_serial *serial);
151static int cypress_hidcom_startup (struct usb_serial *serial); 157static int cypress_hidcom_startup (struct usb_serial *serial);
158static int cypress_ca42v2_startup (struct usb_serial *serial);
152static void cypress_shutdown (struct usb_serial *serial); 159static void cypress_shutdown (struct usb_serial *serial);
153static int cypress_open (struct usb_serial_port *port, struct file *filp); 160static int cypress_open (struct usb_serial_port *port, struct file *filp);
154static void cypress_close (struct usb_serial_port *port, struct file *filp); 161static void cypress_close (struct usb_serial_port *port, struct file *filp);
@@ -235,6 +242,34 @@ static struct usb_serial_driver cypress_hidcom_device = {
235 .write_int_callback = cypress_write_int_callback, 242 .write_int_callback = cypress_write_int_callback,
236}; 243};
237 244
245static struct usb_serial_driver cypress_ca42v2_device = {
246 .driver = {
247 .owner = THIS_MODULE,
248 .name = "nokiaca42v2",
249 },
250 .description = "Nokia CA-42 V2 Adapter",
251 .id_table = id_table_nokiaca42v2,
252 .num_interrupt_in = 1,
253 .num_interrupt_out = 1,
254 .num_bulk_in = NUM_DONT_CARE,
255 .num_bulk_out = NUM_DONT_CARE,
256 .num_ports = 1,
257 .attach = cypress_ca42v2_startup,
258 .shutdown = cypress_shutdown,
259 .open = cypress_open,
260 .close = cypress_close,
261 .write = cypress_write,
262 .write_room = cypress_write_room,
263 .ioctl = cypress_ioctl,
264 .set_termios = cypress_set_termios,
265 .tiocmget = cypress_tiocmget,
266 .tiocmset = cypress_tiocmset,
267 .chars_in_buffer = cypress_chars_in_buffer,
268 .throttle = cypress_throttle,
269 .unthrottle = cypress_unthrottle,
270 .read_int_callback = cypress_read_int_callback,
271 .write_int_callback = cypress_write_int_callback,
272};
238 273
239/***************************************************************************** 274/*****************************************************************************
240 * Cypress serial helper functions 275 * Cypress serial helper functions
@@ -286,6 +321,12 @@ static int cypress_serial_control (struct usb_serial_port *port, unsigned baud_m
286 __FUNCTION__); 321 __FUNCTION__);
287 new_baudrate = priv->baud_rate; 322 new_baudrate = priv->baud_rate;
288 } 323 }
324 } else if (priv->chiptype == CT_CA42V2) {
325 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
326 err("%s - failed setting baud rate, unsupported speed",
327 __FUNCTION__);
328 new_baudrate = priv->baud_rate;
329 }
289 } else if (priv->chiptype == CT_GENERIC) { 330 } else if (priv->chiptype == CT_GENERIC) {
290 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) { 331 if ( (new_baudrate = mask_to_rate(baud_mask)) == -1) {
291 err("%s - failed setting baud rate, unsupported speed", 332 err("%s - failed setting baud rate, unsupported speed",
@@ -435,11 +476,10 @@ static int generic_startup (struct usb_serial *serial)
435 476
436 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number); 477 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
437 478
438 priv = kmalloc(sizeof (struct cypress_private), GFP_KERNEL); 479 priv = kzalloc(sizeof (struct cypress_private), GFP_KERNEL);
439 if (!priv) 480 if (!priv)
440 return -ENOMEM; 481 return -ENOMEM;
441 482
442 memset(priv, 0x00, sizeof (struct cypress_private));
443 spin_lock_init(&priv->lock); 483 spin_lock_init(&priv->lock);
444 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE); 484 priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
445 if (priv->buf == NULL) { 485 if (priv->buf == NULL) {
@@ -500,6 +540,25 @@ static int cypress_hidcom_startup (struct usb_serial *serial)
500} /* cypress_hidcom_startup */ 540} /* cypress_hidcom_startup */
501 541
502 542
543static int cypress_ca42v2_startup (struct usb_serial *serial)
544{
545 struct cypress_private *priv;
546
547 dbg("%s", __FUNCTION__);
548
549 if (generic_startup(serial)) {
550 dbg("%s - Failed setting up port %d", __FUNCTION__,
551 serial->port[0]->number);
552 return 1;
553 }
554
555 priv = usb_get_serial_port_data(serial->port[0]);
556 priv->chiptype = CT_CA42V2;
557
558 return 0;
559} /* cypress_ca42v2_startup */
560
561
503static void cypress_shutdown (struct usb_serial *serial) 562static void cypress_shutdown (struct usb_serial *serial)
504{ 563{
505 struct cypress_private *priv; 564 struct cypress_private *priv;
@@ -944,6 +1003,10 @@ static void cypress_set_termios (struct usb_serial_port *port,
944 *(tty->termios) = tty_std_termios; 1003 *(tty->termios) = tty_std_termios;
945 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | 1004 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
946 CLOCAL; 1005 CLOCAL;
1006 } else if (priv->chiptype == CT_CA42V2) {
1007 *(tty->termios) = tty_std_termios;
1008 tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
1009 CLOCAL;
947 } 1010 }
948 priv->termios_initialized = 1; 1011 priv->termios_initialized = 1;
949 } 1012 }
@@ -1542,6 +1605,9 @@ static int __init cypress_init(void)
1542 retval = usb_serial_register(&cypress_hidcom_device); 1605 retval = usb_serial_register(&cypress_hidcom_device);
1543 if (retval) 1606 if (retval)
1544 goto failed_hidcom_register; 1607 goto failed_hidcom_register;
1608 retval = usb_serial_register(&cypress_ca42v2_device);
1609 if (retval)
1610 goto failed_ca42v2_register;
1545 retval = usb_register(&cypress_driver); 1611 retval = usb_register(&cypress_driver);
1546 if (retval) 1612 if (retval)
1547 goto failed_usb_register; 1613 goto failed_usb_register;
@@ -1550,6 +1616,8 @@ static int __init cypress_init(void)
1550 return 0; 1616 return 0;
1551failed_usb_register: 1617failed_usb_register:
1552 usb_deregister(&cypress_driver); 1618 usb_deregister(&cypress_driver);
1619failed_ca42v2_register:
1620 usb_serial_deregister(&cypress_ca42v2_device);
1553failed_hidcom_register: 1621failed_hidcom_register:
1554 usb_serial_deregister(&cypress_hidcom_device); 1622 usb_serial_deregister(&cypress_hidcom_device);
1555failed_em_register: 1623failed_em_register:
@@ -1566,6 +1634,7 @@ static void __exit cypress_exit (void)
1566 usb_deregister (&cypress_driver); 1634 usb_deregister (&cypress_driver);
1567 usb_serial_deregister (&cypress_earthmate_device); 1635 usb_serial_deregister (&cypress_earthmate_device);
1568 usb_serial_deregister (&cypress_hidcom_device); 1636 usb_serial_deregister (&cypress_hidcom_device);
1637 usb_serial_deregister (&cypress_ca42v2_device);
1569} 1638}
1570 1639
1571 1640
diff --git a/drivers/usb/serial/cypress_m8.h b/drivers/usb/serial/cypress_m8.h
index 1fa119efe41a..e1c7c27e18b7 100644
--- a/drivers/usb/serial/cypress_m8.h
+++ b/drivers/usb/serial/cypress_m8.h
@@ -18,6 +18,10 @@
18/* Cypress HID->COM RS232 Adapter */ 18/* Cypress HID->COM RS232 Adapter */
19#define VENDOR_ID_CYPRESS 0x04b4 19#define VENDOR_ID_CYPRESS 0x04b4
20#define PRODUCT_ID_CYPHIDCOM 0x5500 20#define PRODUCT_ID_CYPHIDCOM 0x5500
21
22/* Nokia CA-42 USB to serial cable */
23#define VENDOR_ID_DAZZLE 0x07d0
24#define PRODUCT_ID_CA42 0x4101
21/* End of device listing */ 25/* End of device listing */
22 26
23/* Used for setting / requesting serial line settings */ 27/* Used for setting / requesting serial line settings */
@@ -34,6 +38,7 @@
34 38
35#define CT_EARTHMATE 0x01 39#define CT_EARTHMATE 0x01
36#define CT_CYPHIDCOM 0x02 40#define CT_CYPHIDCOM 0x02
41#define CT_CA42V2 0x03
37#define CT_GENERIC 0x0F 42#define CT_GENERIC 0x0F
38/* End of chiptype definitions */ 43/* End of chiptype definitions */
39 44
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index c145e1ed8429..f3af81b4dd29 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -492,6 +492,7 @@ static struct usb_device_id id_table_combined [] = {
492 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) }, 492 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_777_PID) },
493 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) }, 493 { USB_DEVICE(FTDI_VID, FTDI_WESTREX_MODEL_8900F_PID) },
494 { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) }, 494 { USB_DEVICE(FTDI_VID, FTDI_PCDJ_DAC2_PID) },
495 { USB_DEVICE(ICOM_ID1_VID, ICOM_ID1_PID) },
495 { }, /* Optional parameter entry */ 496 { }, /* Optional parameter entry */
496 { } /* Terminating entry */ 497 { } /* Terminating entry */
497}; 498};
@@ -1141,12 +1142,11 @@ static int ftdi_sio_attach (struct usb_serial *serial)
1141 1142
1142 dbg("%s",__FUNCTION__); 1143 dbg("%s",__FUNCTION__);
1143 1144
1144 priv = kmalloc(sizeof(struct ftdi_private), GFP_KERNEL); 1145 priv = kzalloc(sizeof(struct ftdi_private), GFP_KERNEL);
1145 if (!priv){ 1146 if (!priv){
1146 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private)); 1147 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct ftdi_private));
1147 return -ENOMEM; 1148 return -ENOMEM;
1148 } 1149 }
1149 memset(priv, 0, sizeof(*priv));
1150 1150
1151 spin_lock_init(&priv->rx_lock); 1151 spin_lock_init(&priv->rx_lock);
1152 init_waitqueue_head(&priv->delta_msr_wait); 1152 init_waitqueue_head(&priv->delta_msr_wait);
diff --git a/drivers/usb/serial/ftdi_sio.h b/drivers/usb/serial/ftdi_sio.h
index bdef3b8c731f..8da773c2744d 100644
--- a/drivers/usb/serial/ftdi_sio.h
+++ b/drivers/usb/serial/ftdi_sio.h
@@ -146,6 +146,13 @@
146#define KOBIL_CONV_KAAN_PID 0x2021 /* KOBIL_Konverter for KAAN */ 146#define KOBIL_CONV_KAAN_PID 0x2021 /* KOBIL_Konverter for KAAN */
147 147
148/* 148/*
149 * Icom ID-1 digital transceiver
150 */
151
152#define ICOM_ID1_VID 0x0C26
153#define ICOM_ID1_PID 0x0004
154
155/*
149 * DSS-20 Sync Station for Sony Ericsson P800 156 * DSS-20 Sync Station for Sony Ericsson P800
150 */ 157 */
151 158
diff --git a/drivers/usb/serial/garmin_gps.c b/drivers/usb/serial/garmin_gps.c
index d6f55e9dccae..5ec9bf5bac8d 100644
--- a/drivers/usb/serial/garmin_gps.c
+++ b/drivers/usb/serial/garmin_gps.c
@@ -1422,12 +1422,11 @@ static int garmin_attach (struct usb_serial *serial)
1422 1422
1423 dbg("%s", __FUNCTION__); 1423 dbg("%s", __FUNCTION__);
1424 1424
1425 garmin_data_p = kmalloc (sizeof(struct garmin_data), GFP_KERNEL); 1425 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1426 if (garmin_data_p == NULL) { 1426 if (garmin_data_p == NULL) {
1427 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__); 1427 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1428 return -ENOMEM; 1428 return -ENOMEM;
1429 } 1429 }
1430 memset (garmin_data_p, 0, sizeof(struct garmin_data));
1431 init_timer(&garmin_data_p->timer); 1430 init_timer(&garmin_data_p->timer);
1432 spin_lock_init(&garmin_data_p->lock); 1431 spin_lock_init(&garmin_data_p->lock);
1433 INIT_LIST_HEAD(&garmin_data_p->pktlist); 1432 INIT_LIST_HEAD(&garmin_data_p->pktlist);
diff --git a/drivers/usb/serial/io_edgeport.c b/drivers/usb/serial/io_edgeport.c
index 3f29e6b0fd19..b606c5968102 100644
--- a/drivers/usb/serial/io_edgeport.c
+++ b/drivers/usb/serial/io_edgeport.c
@@ -2725,12 +2725,11 @@ static int edge_startup (struct usb_serial *serial)
2725 dev = serial->dev; 2725 dev = serial->dev;
2726 2726
2727 /* create our private serial structure */ 2727 /* create our private serial structure */
2728 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL); 2728 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2729 if (edge_serial == NULL) { 2729 if (edge_serial == NULL) {
2730 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2730 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2731 return -ENOMEM; 2731 return -ENOMEM;
2732 } 2732 }
2733 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2734 spin_lock_init(&edge_serial->es_lock); 2733 spin_lock_init(&edge_serial->es_lock);
2735 edge_serial->serial = serial; 2734 edge_serial->serial = serial;
2736 usb_set_serial_data(serial, edge_serial); 2735 usb_set_serial_data(serial, edge_serial);
diff --git a/drivers/usb/serial/io_ti.c b/drivers/usb/serial/io_ti.c
index afc0f34b3a46..8e1e2253748b 100644
--- a/drivers/usb/serial/io_ti.c
+++ b/drivers/usb/serial/io_ti.c
@@ -2727,12 +2727,11 @@ static int edge_startup (struct usb_serial *serial)
2727 dev = serial->dev; 2727 dev = serial->dev;
2728 2728
2729 /* create our private serial structure */ 2729 /* create our private serial structure */
2730 edge_serial = kmalloc (sizeof(struct edgeport_serial), GFP_KERNEL); 2730 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2731 if (edge_serial == NULL) { 2731 if (edge_serial == NULL) {
2732 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2732 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2733 return -ENOMEM; 2733 return -ENOMEM;
2734 } 2734 }
2735 memset (edge_serial, 0, sizeof(struct edgeport_serial));
2736 sema_init(&edge_serial->es_sem, 1); 2735 sema_init(&edge_serial->es_sem, 1);
2737 edge_serial->serial = serial; 2736 edge_serial->serial = serial;
2738 usb_set_serial_data(serial, edge_serial); 2737 usb_set_serial_data(serial, edge_serial);
@@ -2745,12 +2744,11 @@ static int edge_startup (struct usb_serial *serial)
2745 2744
2746 /* set up our port private structures */ 2745 /* set up our port private structures */
2747 for (i = 0; i < serial->num_ports; ++i) { 2746 for (i = 0; i < serial->num_ports; ++i) {
2748 edge_port = kmalloc (sizeof(struct edgeport_port), GFP_KERNEL); 2747 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
2749 if (edge_port == NULL) { 2748 if (edge_port == NULL) {
2750 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__); 2749 dev_err(&serial->dev->dev, "%s - Out of memory\n", __FUNCTION__);
2751 goto cleanup; 2750 goto cleanup;
2752 } 2751 }
2753 memset (edge_port, 0, sizeof(struct edgeport_port));
2754 spin_lock_init(&edge_port->ep_lock); 2752 spin_lock_init(&edge_port->ep_lock);
2755 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE); 2753 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2756 if (edge_port->ep_out_buf == NULL) { 2754 if (edge_port->ep_out_buf == NULL) {
diff --git a/drivers/usb/serial/ir-usb.c b/drivers/usb/serial/ir-usb.c
index a59010421444..426182ddc42a 100644
--- a/drivers/usb/serial/ir-usb.c
+++ b/drivers/usb/serial/ir-usb.c
@@ -184,10 +184,9 @@ static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev,
184 struct irda_class_desc *desc; 184 struct irda_class_desc *desc;
185 int ret; 185 int ret;
186 186
187 desc = kmalloc(sizeof (struct irda_class_desc), GFP_KERNEL); 187 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
188 if (desc == NULL) 188 if (desc == NULL)
189 return NULL; 189 return NULL;
190 memset(desc, 0, sizeof(struct irda_class_desc));
191 190
192 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0), 191 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
193 IU_REQ_GET_CLASS_DESC, 192 IU_REQ_GET_CLASS_DESC,
diff --git a/drivers/usb/serial/keyspan.c b/drivers/usb/serial/keyspan.c
index 3b958e60f5e8..052b735c4fbd 100644
--- a/drivers/usb/serial/keyspan.c
+++ b/drivers/usb/serial/keyspan.c
@@ -2250,12 +2250,11 @@ static int keyspan_startup (struct usb_serial *serial)
2250 } 2250 }
2251 2251
2252 /* Setup private data for serial driver */ 2252 /* Setup private data for serial driver */
2253 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL); 2253 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2254 if (!s_priv) { 2254 if (!s_priv) {
2255 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__); 2255 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2256 return -ENOMEM; 2256 return -ENOMEM;
2257 } 2257 }
2258 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2259 2258
2260 s_priv->device_details = d_details; 2259 s_priv->device_details = d_details;
2261 usb_set_serial_data(serial, s_priv); 2260 usb_set_serial_data(serial, s_priv);
@@ -2263,12 +2262,11 @@ static int keyspan_startup (struct usb_serial *serial)
2263 /* Now setup per port private data */ 2262 /* Now setup per port private data */
2264 for (i = 0; i < serial->num_ports; i++) { 2263 for (i = 0; i < serial->num_ports; i++) {
2265 port = serial->port[i]; 2264 port = serial->port[i];
2266 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL); 2265 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2267 if (!p_priv) { 2266 if (!p_priv) {
2268 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i); 2267 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2269 return (1); 2268 return (1);
2270 } 2269 }
2271 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2272 p_priv->device_details = d_details; 2270 p_priv->device_details = d_details;
2273 usb_set_serial_port_data(port, p_priv); 2271 usb_set_serial_port_data(port, p_priv);
2274 } 2272 }
diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
index b8b213185d0f..87dfcd89ffab 100644
--- a/drivers/usb/serial/kobil_sct.c
+++ b/drivers/usb/serial/kobil_sct.c
@@ -255,11 +255,9 @@ static int kobil_open (struct usb_serial_port *port, struct file *filp)
255 } 255 }
256 256
257 // allocate memory for transfer buffer 257 // allocate memory for transfer buffer
258 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 258 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
259 if (! transfer_buffer) { 259 if (! transfer_buffer) {
260 return -ENOMEM; 260 return -ENOMEM;
261 } else {
262 memset(transfer_buffer, 0, transfer_buffer_length);
263 } 261 }
264 262
265 // allocate write_urb 263 // allocate write_urb
@@ -383,11 +381,10 @@ static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
383 381
384 // BEGIN DEBUG 382 // BEGIN DEBUG
385 /* 383 /*
386 dbg_data = (unsigned char *) kmalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL); 384 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
387 if (! dbg_data) { 385 if (! dbg_data) {
388 return; 386 return;
389 } 387 }
390 memset(dbg_data, 0, (3 * purb->actual_length + 10));
391 for (i = 0; i < purb->actual_length; i++) { 388 for (i = 0; i < purb->actual_length; i++) {
392 sprintf(dbg_data +3*i, "%02X ", data[i]); 389 sprintf(dbg_data +3*i, "%02X ", data[i]);
393 } 390 }
@@ -518,11 +515,10 @@ static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
518 } 515 }
519 516
520 // allocate memory for transfer buffer 517 // allocate memory for transfer buffer
521 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 518 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
522 if (!transfer_buffer) { 519 if (!transfer_buffer) {
523 return -ENOMEM; 520 return -ENOMEM;
524 } 521 }
525 memset(transfer_buffer, 0, transfer_buffer_length);
526 522
527 result = usb_control_msg( port->serial->dev, 523 result = usb_control_msg( port->serial->dev,
528 usb_rcvctrlpipe(port->serial->dev, 0 ), 524 usb_rcvctrlpipe(port->serial->dev, 0 ),
@@ -564,11 +560,10 @@ static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
564 } 560 }
565 561
566 // allocate memory for transfer buffer 562 // allocate memory for transfer buffer
567 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL); 563 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
568 if (! transfer_buffer) { 564 if (! transfer_buffer) {
569 return -ENOMEM; 565 return -ENOMEM;
570 } 566 }
571 memset(transfer_buffer, 0, transfer_buffer_length);
572 567
573 if (set & TIOCM_RTS) 568 if (set & TIOCM_RTS)
574 rts = 1; 569 rts = 1;
@@ -655,11 +650,10 @@ static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
655 (struct termios __user *)arg)) 650 (struct termios __user *)arg))
656 return -EFAULT; 651 return -EFAULT;
657 652
658 settings = (unsigned char *) kmalloc(50, GFP_KERNEL); 653 settings = kzalloc(50, GFP_KERNEL);
659 if (! settings) { 654 if (! settings) {
660 return -ENOBUFS; 655 return -ENOBUFS;
661 } 656 }
662 memset(settings, 0, 50);
663 657
664 switch (priv->internal_termios.c_cflag & CBAUD) { 658 switch (priv->internal_termios.c_cflag & CBAUD) {
665 case B1200: 659 case B1200:
diff --git a/drivers/usb/serial/mct_u232.c b/drivers/usb/serial/mct_u232.c
index b6d6cab9c859..35bd29b6c408 100644
--- a/drivers/usb/serial/mct_u232.c
+++ b/drivers/usb/serial/mct_u232.c
@@ -348,10 +348,9 @@ static int mct_u232_startup (struct usb_serial *serial)
348 struct mct_u232_private *priv; 348 struct mct_u232_private *priv;
349 struct usb_serial_port *port, *rport; 349 struct usb_serial_port *port, *rport;
350 350
351 priv = kmalloc(sizeof(struct mct_u232_private), GFP_KERNEL); 351 priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
352 if (!priv) 352 if (!priv)
353 return -ENOMEM; 353 return -ENOMEM;
354 memset(priv, 0, sizeof(struct mct_u232_private));
355 spin_lock_init(&priv->lock); 354 spin_lock_init(&priv->lock);
356 usb_set_serial_port_data(serial->port[0], priv); 355 usb_set_serial_port_data(serial->port[0], priv);
357 356
diff --git a/drivers/usb/serial/navman.c b/drivers/usb/serial/navman.c
new file mode 100644
index 000000000000..7f544081032e
--- /dev/null
+++ b/drivers/usb/serial/navman.c
@@ -0,0 +1,157 @@
1/*
2 * Navman Serial USB driver
3 *
4 * Copyright (C) 2006 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/tty.h>
14#include <linux/tty_flip.h>
15#include <linux/module.h>
16#include <linux/usb.h>
17#include "usb-serial.h"
18
19static int debug;
20
21static struct usb_device_id id_table [] = {
22 { USB_DEVICE(0x0a99, 0x0001) }, /* Talon Technology device */
23 { },
24};
25MODULE_DEVICE_TABLE(usb, id_table);
26
27static struct usb_driver navman_driver = {
28 .name = "navman",
29 .probe = usb_serial_probe,
30 .disconnect = usb_serial_disconnect,
31 .id_table = id_table,
32 .no_dynamic_id = 1,
33};
34
35static void navman_read_int_callback(struct urb *urb, struct pt_regs *regs)
36{
37 struct usb_serial_port *port = urb->context;
38 unsigned char *data = urb->transfer_buffer;
39 struct tty_struct *tty;
40 int result;
41
42 switch (urb->status) {
43 case 0:
44 /* success */
45 break;
46 case -ECONNRESET:
47 case -ENOENT:
48 case -ESHUTDOWN:
49 /* this urb is terminated, clean up */
50 dbg("%s - urb shutting down with status: %d",
51 __FUNCTION__, urb->status);
52 return;
53 default:
54 dbg("%s - nonzero urb status received: %d",
55 __FUNCTION__, urb->status);
56 goto exit;
57 }
58
59 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
60 urb->actual_length, data);
61
62 tty = port->tty;
63 if (tty && urb->actual_length) {
64 tty_buffer_request_room(tty, urb->actual_length);
65 tty_insert_flip_string(tty, data, urb->actual_length);
66 tty_flip_buffer_push(tty);
67 }
68
69exit:
70 result = usb_submit_urb(urb, GFP_ATOMIC);
71 if (result)
72 dev_err(&urb->dev->dev,
73 "%s - Error %d submitting interrupt urb\n",
74 __FUNCTION__, result);
75}
76
77static int navman_open(struct usb_serial_port *port, struct file *filp)
78{
79 int result = 0;
80
81 dbg("%s - port %d", __FUNCTION__, port->number);
82
83 if (port->interrupt_in_urb) {
84 dbg("%s - adding interrupt input for treo", __FUNCTION__);
85 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
86 if (result)
87 dev_err(&port->dev,
88 "%s - failed submitting interrupt urb, error %d\n",
89 __FUNCTION__, result);
90 }
91 return result;
92}
93
94static void navman_close(struct usb_serial_port *port, struct file *filp)
95{
96 dbg("%s - port %d", __FUNCTION__, port->number);
97
98 if (port->interrupt_in_urb)
99 usb_kill_urb(port->interrupt_in_urb);
100}
101
102static int navman_write(struct usb_serial_port *port,
103 const unsigned char *buf, int count)
104{
105 dbg("%s - port %d", __FUNCTION__, port->number);
106
107 /*
108 * This device can't write any data, only read from the device
109 * so we just silently eat all data sent to us and say it was
110 * successfully sent.
111 * Evil, I know, but do you have a better idea?
112 */
113
114 return count;
115}
116
117static struct usb_serial_driver navman_device = {
118 .driver = {
119 .owner = THIS_MODULE,
120 .name = "navman",
121 },
122 .id_table = id_table,
123 .num_interrupt_in = NUM_DONT_CARE,
124 .num_bulk_in = NUM_DONT_CARE,
125 .num_bulk_out = NUM_DONT_CARE,
126 .num_ports = 1,
127 .open = navman_open,
128 .close = navman_close,
129 .write = navman_write,
130 .read_int_callback = navman_read_int_callback,
131};
132
133static int __init navman_init(void)
134{
135 int retval;
136
137 retval = usb_serial_register(&navman_device);
138 if (retval)
139 return retval;
140 retval = usb_register(&navman_driver);
141 if (retval)
142 usb_serial_deregister(&navman_device);
143 return retval;
144}
145
146static void __exit navman_exit(void)
147{
148 usb_deregister(&navman_driver);
149 usb_serial_deregister(&navman_device);
150}
151
152module_init(navman_init);
153module_exit(navman_exit);
154MODULE_LICENSE("GPL");
155
156module_param(debug, bool, S_IRUGO | S_IWUSR);
157MODULE_PARM_DESC(debug, "Debug enabled or not");
diff --git a/drivers/usb/serial/omninet.c b/drivers/usb/serial/omninet.c
index 762d8ff9a1e4..4d40704dea2c 100644
--- a/drivers/usb/serial/omninet.c
+++ b/drivers/usb/serial/omninet.c
@@ -204,7 +204,7 @@ static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
204 int i; 204 int i;
205 int result; 205 int result;
206 206
207// dbg("omninet_read_bulk_callback"); 207 dbg("%s - port %d", __FUNCTION__, port->number);
208 208
209 if (urb->status) { 209 if (urb->status) {
210 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); 210 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
@@ -250,7 +250,7 @@ static int omninet_write (struct usb_serial_port *port, const unsigned char *buf
250 250
251 int result; 251 int result;
252 252
253// dbg("omninet_write port %d", port->number); 253 dbg("%s - port %d", __FUNCTION__, port->number);
254 254
255 if (count == 0) { 255 if (count == 0) {
256 dbg("%s - write request of 0 bytes", __FUNCTION__); 256 dbg("%s - write request of 0 bytes", __FUNCTION__);
@@ -302,7 +302,7 @@ static int omninet_write_room (struct usb_serial_port *port)
302 if (wport->write_urb_busy) 302 if (wport->write_urb_busy)
303 room = wport->bulk_out_size - OMNINET_HEADERLEN; 303 room = wport->bulk_out_size - OMNINET_HEADERLEN;
304 304
305// dbg("omninet_write_room returns %d", room); 305 dbg("%s - returns %d", __FUNCTION__, room);
306 306
307 return (room); 307 return (room);
308} 308}
@@ -312,7 +312,7 @@ static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
312/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */ 312/* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
313 struct usb_serial_port *port = (struct usb_serial_port *) urb->context; 313 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
314 314
315// dbg("omninet_write_bulk_callback, port %0x\n", port); 315 dbg("%s - port %0x\n", __FUNCTION__, port->number);
316 316
317 port->write_urb_busy = 0; 317 port->write_urb_busy = 0;
318 if (urb->status) { 318 if (urb->status) {
@@ -321,8 +321,6 @@ static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
321 } 321 }
322 322
323 schedule_work(&port->work); 323 schedule_work(&port->work);
324
325// dbg("omninet_write_bulk_callback, tty %0x\n", tty);
326} 324}
327 325
328 326
diff --git a/drivers/usb/serial/option.c b/drivers/usb/serial/option.c
index 52bdf6fe46f2..a8455c9e79dd 100644
--- a/drivers/usb/serial/option.c
+++ b/drivers/usb/serial/option.c
@@ -631,13 +631,12 @@ static int option_startup(struct usb_serial *serial)
631 /* Now setup per port private data */ 631 /* Now setup per port private data */
632 for (i = 0; i < serial->num_ports; i++) { 632 for (i = 0; i < serial->num_ports; i++) {
633 port = serial->port[i]; 633 port = serial->port[i];
634 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL); 634 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
635 if (!portdata) { 635 if (!portdata) {
636 dbg("%s: kmalloc for option_port_private (%d) failed!.", 636 dbg("%s: kmalloc for option_port_private (%d) failed!.",
637 __FUNCTION__, i); 637 __FUNCTION__, i);
638 return (1); 638 return (1);
639 } 639 }
640 memset(portdata, 0, sizeof(struct option_port_private));
641 640
642 usb_set_serial_port_data(port, portdata); 641 usb_set_serial_port_data(port, portdata);
643 642
diff --git a/drivers/usb/serial/pl2303.c b/drivers/usb/serial/pl2303.c
index 37c81c08faad..b3014fda645c 100644
--- a/drivers/usb/serial/pl2303.c
+++ b/drivers/usb/serial/pl2303.c
@@ -77,6 +77,7 @@ static struct usb_device_id id_table [] = {
77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) }, 77 { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) }, 78 { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
79 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) }, 79 { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
80 { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
80 { } /* Terminating entry */ 81 { } /* Terminating entry */
81}; 82};
82 83
@@ -218,10 +219,9 @@ static int pl2303_startup (struct usb_serial *serial)
218 dbg("device type: %d", type); 219 dbg("device type: %d", type);
219 220
220 for (i = 0; i < serial->num_ports; ++i) { 221 for (i = 0; i < serial->num_ports; ++i) {
221 priv = kmalloc (sizeof (struct pl2303_private), GFP_KERNEL); 222 priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
222 if (!priv) 223 if (!priv)
223 goto cleanup; 224 goto cleanup;
224 memset (priv, 0x00, sizeof (struct pl2303_private));
225 spin_lock_init(&priv->lock); 225 spin_lock_init(&priv->lock);
226 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE); 226 priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
227 if (priv->buf == NULL) { 227 if (priv->buf == NULL) {
@@ -383,12 +383,11 @@ static void pl2303_set_termios (struct usb_serial_port *port, struct termios *ol
383 } 383 }
384 } 384 }
385 385
386 buf = kmalloc (7, GFP_KERNEL); 386 buf = kzalloc (7, GFP_KERNEL);
387 if (!buf) { 387 if (!buf) {
388 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__); 388 dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
389 return; 389 return;
390 } 390 }
391 memset (buf, 0x00, 0x07);
392 391
393 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0), 392 i = usb_control_msg (serial->dev, usb_rcvctrlpipe (serial->dev, 0),
394 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE, 393 GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
@@ -828,6 +827,7 @@ static void pl2303_update_line_status(struct usb_serial_port *port,
828 spin_lock_irqsave(&priv->lock, flags); 827 spin_lock_irqsave(&priv->lock, flags);
829 priv->line_status = data[status_idx]; 828 priv->line_status = data[status_idx];
830 spin_unlock_irqrestore(&priv->lock, flags); 829 spin_unlock_irqrestore(&priv->lock, flags);
830 wake_up_interruptible (&priv->delta_msr_wait);
831 831
832exit: 832exit:
833 return; 833 return;
diff --git a/drivers/usb/serial/pl2303.h b/drivers/usb/serial/pl2303.h
index 9bc4755162ad..77901d143979 100644
--- a/drivers/usb/serial/pl2303.h
+++ b/drivers/usb/serial/pl2303.h
@@ -75,3 +75,7 @@
75/* Leadtek GPS 9531 (ID 0413:2101) */ 75/* Leadtek GPS 9531 (ID 0413:2101) */
76#define LEADTEK_VENDOR_ID 0x0413 76#define LEADTEK_VENDOR_ID 0x0413
77#define LEADTEK_9531_PRODUCT_ID 0x2101 77#define LEADTEK_9531_PRODUCT_ID 0x2101
78
79/* USB GSM cable from Speed Dragon Multimedia, Ltd */
80#define SPEEDDRAGON_VENDOR_ID 0x0e55
81#define SPEEDDRAGON_PRODUCT_ID 0x110b
diff --git a/drivers/usb/serial/ti_usb_3410_5052.c b/drivers/usb/serial/ti_usb_3410_5052.c
index c18db3257073..c3a2071b802d 100644
--- a/drivers/usb/serial/ti_usb_3410_5052.c
+++ b/drivers/usb/serial/ti_usb_3410_5052.c
@@ -416,12 +416,11 @@ static int ti_startup(struct usb_serial *serial)
416 dev->actconfig->desc.bConfigurationValue); 416 dev->actconfig->desc.bConfigurationValue);
417 417
418 /* create device structure */ 418 /* create device structure */
419 tdev = kmalloc(sizeof(struct ti_device), GFP_KERNEL); 419 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
420 if (tdev == NULL) { 420 if (tdev == NULL) {
421 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__); 421 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
422 return -ENOMEM; 422 return -ENOMEM;
423 } 423 }
424 memset(tdev, 0, sizeof(struct ti_device));
425 sema_init(&tdev->td_open_close_sem, 1); 424 sema_init(&tdev->td_open_close_sem, 1);
426 tdev->td_serial = serial; 425 tdev->td_serial = serial;
427 usb_set_serial_data(serial, tdev); 426 usb_set_serial_data(serial, tdev);
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index b5c96e74a903..097f4e8488fe 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -564,12 +564,11 @@ static struct usb_serial * create_serial (struct usb_device *dev,
564{ 564{
565 struct usb_serial *serial; 565 struct usb_serial *serial;
566 566
567 serial = kmalloc (sizeof (*serial), GFP_KERNEL); 567 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
568 if (!serial) { 568 if (!serial) {
569 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__); 569 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
570 return NULL; 570 return NULL;
571 } 571 }
572 memset (serial, 0, sizeof(*serial));
573 serial->dev = usb_get_dev(dev); 572 serial->dev = usb_get_dev(dev);
574 serial->type = driver; 573 serial->type = driver;
575 serial->interface = interface; 574 serial->interface = interface;
@@ -778,10 +777,9 @@ int usb_serial_probe(struct usb_interface *interface,
778 serial->num_port_pointers = max_endpoints; 777 serial->num_port_pointers = max_endpoints;
779 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints); 778 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
780 for (i = 0; i < max_endpoints; ++i) { 779 for (i = 0; i < max_endpoints; ++i) {
781 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL); 780 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
782 if (!port) 781 if (!port)
783 goto probe_error; 782 goto probe_error;
784 memset(port, 0x00, sizeof(struct usb_serial_port));
785 port->number = i + serial->minor; 783 port->number = i + serial->minor;
786 port->serial = serial; 784 port->serial = serial;
787 spin_lock_init(&port->lock); 785 spin_lock_init(&port->lock);
diff --git a/drivers/usb/serial/visor.c b/drivers/usb/serial/visor.c
index 11a48d874752..f5c3841d4843 100644
--- a/drivers/usb/serial/visor.c
+++ b/drivers/usb/serial/visor.c
@@ -763,10 +763,9 @@ static int generic_startup(struct usb_serial *serial)
763 int i; 763 int i;
764 764
765 for (i = 0; i < serial->num_ports; ++i) { 765 for (i = 0; i < serial->num_ports; ++i) {
766 priv = kmalloc (sizeof(*priv), GFP_KERNEL); 766 priv = kzalloc (sizeof(*priv), GFP_KERNEL);
767 if (!priv) 767 if (!priv)
768 return -ENOMEM; 768 return -ENOMEM;
769 memset (priv, 0x00, sizeof(*priv));
770 spin_lock_init(&priv->lock); 769 spin_lock_init(&priv->lock);
771 usb_set_serial_port_data(serial->port[i], priv); 770 usb_set_serial_port_data(serial->port[i], priv);
772 } 771 }
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index 54e3e6c7ecd8..01d8971ad7db 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -512,13 +512,12 @@ int datafab_transport(struct scsi_cmnd * srb, struct us_data *us)
512 }; 512 };
513 513
514 if (!us->extra) { 514 if (!us->extra) {
515 us->extra = kmalloc(sizeof(struct datafab_info), GFP_NOIO); 515 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO);
516 if (!us->extra) { 516 if (!us->extra) {
517 US_DEBUGP("datafab_transport: Gah! " 517 US_DEBUGP("datafab_transport: Gah! "
518 "Can't allocate storage for Datafab info struct!\n"); 518 "Can't allocate storage for Datafab info struct!\n");
519 return USB_STOR_TRANSPORT_ERROR; 519 return USB_STOR_TRANSPORT_ERROR;
520 } 520 }
521 memset(us->extra, 0, sizeof(struct datafab_info));
522 us->extra_destructor = datafab_info_destructor; 521 us->extra_destructor = datafab_info_destructor;
523 ((struct datafab_info *)us->extra)->lun = -1; 522 ((struct datafab_info *)us->extra)->lun = -1;
524 } 523 }
diff --git a/drivers/usb/storage/isd200.c b/drivers/usb/storage/isd200.c
index ecb328aa9ea1..6831dca93c1b 100644
--- a/drivers/usb/storage/isd200.c
+++ b/drivers/usb/storage/isd200.c
@@ -1361,21 +1361,19 @@ static int isd200_init_info(struct us_data *us)
1361 struct isd200_info *info; 1361 struct isd200_info *info;
1362 1362
1363 info = (struct isd200_info *) 1363 info = (struct isd200_info *)
1364 kmalloc(sizeof(struct isd200_info), GFP_KERNEL); 1364 kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1365 if (!info) 1365 if (!info)
1366 retStatus = ISD200_ERROR; 1366 retStatus = ISD200_ERROR;
1367 else { 1367 else {
1368 memset(info, 0, sizeof(struct isd200_info));
1369 info->id = (struct hd_driveid *) 1368 info->id = (struct hd_driveid *)
1370 kmalloc(sizeof(struct hd_driveid), GFP_KERNEL); 1369 kzalloc(sizeof(struct hd_driveid), GFP_KERNEL);
1371 info->RegsBuf = (unsigned char *) 1370 info->RegsBuf = (unsigned char *)
1372 kmalloc(sizeof(info->ATARegs), GFP_KERNEL); 1371 kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1373 if (!info->id || !info->RegsBuf) { 1372 if (!info->id || !info->RegsBuf) {
1374 isd200_free_info_ptrs(info); 1373 isd200_free_info_ptrs(info);
1375 kfree(info); 1374 kfree(info);
1376 retStatus = ISD200_ERROR; 1375 retStatus = ISD200_ERROR;
1377 } else 1376 }
1378 memset(info->id, 0, sizeof(struct hd_driveid));
1379 } 1377 }
1380 1378
1381 if (retStatus == ISD200_GOOD) { 1379 if (retStatus == ISD200_GOOD) {
@@ -1384,7 +1382,7 @@ static int isd200_init_info(struct us_data *us)
1384 } else 1382 } else
1385 US_DEBUGP("ERROR - kmalloc failure\n"); 1383 US_DEBUGP("ERROR - kmalloc failure\n");
1386 1384
1387 return(retStatus); 1385 return retStatus;
1388} 1386}
1389 1387
1390/************************************************************************** 1388/**************************************************************************
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index aff9d51c327c..5031aa98f6a9 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -441,12 +441,11 @@ int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
441 }; 441 };
442 442
443 if (!us->extra) { 443 if (!us->extra) {
444 us->extra = kmalloc(sizeof(struct jumpshot_info), GFP_NOIO); 444 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
445 if (!us->extra) { 445 if (!us->extra) {
446 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n"); 446 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
447 return USB_STOR_TRANSPORT_ERROR; 447 return USB_STOR_TRANSPORT_ERROR;
448 } 448 }
449 memset(us->extra, 0, sizeof(struct jumpshot_info));
450 us->extra_destructor = jumpshot_info_destructor; 449 us->extra_destructor = jumpshot_info_destructor;
451 } 450 }
452 451
diff --git a/drivers/usb/storage/scsiglue.c b/drivers/usb/storage/scsiglue.c
index 4ef5527028c5..5f11e19eaae3 100644
--- a/drivers/usb/storage/scsiglue.c
+++ b/drivers/usb/storage/scsiglue.c
@@ -47,6 +47,7 @@
47 47
48#include <linux/slab.h> 48#include <linux/slab.h>
49#include <linux/module.h> 49#include <linux/module.h>
50#include <linux/mutex.h>
50 51
51#include <scsi/scsi.h> 52#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h> 53#include <scsi/scsi_cmnd.h>
@@ -271,9 +272,9 @@ static int device_reset(struct scsi_cmnd *srb)
271 US_DEBUGP("%s called\n", __FUNCTION__); 272 US_DEBUGP("%s called\n", __FUNCTION__);
272 273
273 /* lock the device pointers and do the reset */ 274 /* lock the device pointers and do the reset */
274 down(&(us->dev_semaphore)); 275 mutex_lock(&(us->dev_mutex));
275 result = us->transport_reset(us); 276 result = us->transport_reset(us);
276 up(&(us->dev_semaphore)); 277 mutex_unlock(&us->dev_mutex);
277 278
278 return result < 0 ? FAILED : SUCCESS; 279 return result < 0 ? FAILED : SUCCESS;
279} 280}
@@ -286,9 +287,9 @@ static int bus_reset(struct scsi_cmnd *srb)
286 287
287 US_DEBUGP("%s called\n", __FUNCTION__); 288 US_DEBUGP("%s called\n", __FUNCTION__);
288 289
289 down(&(us->dev_semaphore)); 290 mutex_lock(&(us->dev_mutex));
290 result = usb_stor_port_reset(us); 291 result = usb_stor_port_reset(us);
291 up(&(us->dev_semaphore)); 292 mutex_unlock(&us->dev_mutex);
292 293
293 return result < 0 ? FAILED : SUCCESS; 294 return result < 0 ? FAILED : SUCCESS;
294} 295}
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index 8451779f4269..0b1b5b59ca7b 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -751,11 +751,10 @@ int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
751 struct sddr55_card_info *info; 751 struct sddr55_card_info *info;
752 752
753 if (!us->extra) { 753 if (!us->extra) {
754 us->extra = kmalloc( 754 us->extra = kzalloc(
755 sizeof(struct sddr55_card_info), GFP_NOIO); 755 sizeof(struct sddr55_card_info), GFP_NOIO);
756 if (!us->extra) 756 if (!us->extra)
757 return USB_STOR_TRANSPORT_ERROR; 757 return USB_STOR_TRANSPORT_ERROR;
758 memset(us->extra, 0, sizeof(struct sddr55_card_info));
759 us->extra_destructor = sddr55_card_info_destructor; 758 us->extra_destructor = sddr55_card_info_destructor;
760 } 759 }
761 760
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index fea176d7e79a..f2bc5c9e23d5 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -1318,12 +1318,11 @@ int init_usbat(struct us_data *us)
1318 unsigned char subcountL = USBAT_ATA_LBA_ME; 1318 unsigned char subcountL = USBAT_ATA_LBA_ME;
1319 unsigned char *status = us->iobuf; 1319 unsigned char *status = us->iobuf;
1320 1320
1321 us->extra = kmalloc(sizeof(struct usbat_info), GFP_NOIO); 1321 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1322 if (!us->extra) { 1322 if (!us->extra) {
1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n"); 1323 US_DEBUGP("init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1324 return 1; 1324 return 1;
1325 } 1325 }
1326 memset(us->extra, 0, sizeof(struct usbat_info));
1327 info = (struct usbat_info *) (us->extra); 1326 info = (struct usbat_info *) (us->extra);
1328 1327
1329 /* Enable peripheral control signals */ 1328 /* Enable peripheral control signals */
diff --git a/drivers/usb/storage/unusual_devs.h b/drivers/usb/storage/unusual_devs.h
index 31ca92056c27..c4a9dcff5f2b 100644
--- a/drivers/usb/storage/unusual_devs.h
+++ b/drivers/usb/storage/unusual_devs.h
@@ -62,6 +62,13 @@ UNUSUAL_DEV( 0x03ee, 0x6901, 0x0000, 0x0100,
62 US_SC_DEVICE, US_PR_DEVICE, NULL, 62 US_SC_DEVICE, US_PR_DEVICE, NULL,
63 US_FL_SINGLE_LUN ), 63 US_FL_SINGLE_LUN ),
64 64
65/* Reported by Rodolfo Quesada <rquesada@roqz.net> */
66UNUSUAL_DEV( 0x03ee, 0x6906, 0x0003, 0x0003,
67 "VIA Technologies Inc.",
68 "Mitsumi multi cardreader",
69 US_SC_DEVICE, US_PR_DEVICE, NULL,
70 US_FL_IGNORE_RESIDUE ),
71
65UNUSUAL_DEV( 0x03f0, 0x0107, 0x0200, 0x0200, 72UNUSUAL_DEV( 0x03f0, 0x0107, 0x0200, 0x0200,
66 "HP", 73 "HP",
67 "CD-Writer+", 74 "CD-Writer+",
@@ -120,6 +127,12 @@ UNUSUAL_DEV( 0x0419, 0xaaf6, 0x0100, 0x0100,
120 US_SC_DEVICE, US_PR_DEVICE, NULL, 127 US_SC_DEVICE, US_PR_DEVICE, NULL,
121 US_FL_IGNORE_RESIDUE ), 128 US_FL_IGNORE_RESIDUE ),
122 129
130/* Reported by Pete Zaitcev <zaitcev@redhat.com>, bz#176584 */
131UNUSUAL_DEV( 0x0420, 0x0001, 0x0100, 0x0100,
132 "GENERIC", "MP3 PLAYER", /* MyMusix PD-205 on the outside. */
133 US_SC_DEVICE, US_PR_DEVICE, NULL,
134 US_FL_IGNORE_RESIDUE ),
135
123/* Reported by Olaf Hering <olh@suse.de> from novell bug #105878 */ 136/* Reported by Olaf Hering <olh@suse.de> from novell bug #105878 */
124UNUSUAL_DEV( 0x0424, 0x0fdc, 0x0210, 0x0210, 137UNUSUAL_DEV( 0x0424, 0x0fdc, 0x0210, 0x0210,
125 "SMSC", 138 "SMSC",
@@ -760,12 +773,19 @@ UNUSUAL_DEV( 0x069b, 0x3004, 0x0001, 0x0001,
760 US_SC_DEVICE, US_PR_DEVICE, NULL, 773 US_SC_DEVICE, US_PR_DEVICE, NULL,
761 US_FL_FIX_CAPACITY ), 774 US_FL_FIX_CAPACITY ),
762 775
763UNUSUAL_DEV( 0x0781, 0x0001, 0x0200, 0x0200, 776/* Submitted by Roman Hodek <roman@hodek.net> */
777UNUSUAL_DEV( 0x0781, 0x0001, 0x0200, 0x0200,
764 "Sandisk", 778 "Sandisk",
765 "ImageMate SDDR-05a", 779 "ImageMate SDDR-05a",
766 US_SC_SCSI, US_PR_CB, NULL, 780 US_SC_SCSI, US_PR_CB, NULL,
767 US_FL_SINGLE_LUN ), 781 US_FL_SINGLE_LUN ),
768 782
783UNUSUAL_DEV( 0x0781, 0x0002, 0x0009, 0x0009,
784 "SanDisk Corporation",
785 "ImageMate CompactFlash USB",
786 US_SC_DEVICE, US_PR_DEVICE, NULL,
787 US_FL_FIX_CAPACITY ),
788
769#ifdef CONFIG_USB_STORAGE_USBAT 789#ifdef CONFIG_USB_STORAGE_USBAT
770UNUSUAL_DEV( 0x0781, 0x0005, 0x0005, 0x0005, 790UNUSUAL_DEV( 0x0781, 0x0005, 0x0005, 0x0005,
771 "Sandisk", 791 "Sandisk",
@@ -1073,6 +1093,16 @@ UNUSUAL_DEV( 0x0c0b, 0xa109, 0x0000, 0xffff,
1073 0), 1093 0),
1074#endif 1094#endif
1075 1095
1096/*
1097 * Pete Zaitcev <zaitcev@yahoo.com>, bz#164688.
1098 * The device blatantly ignores LUN and returns 1 in GetMaxLUN.
1099 */
1100UNUSUAL_DEV( 0x0c45, 0x1060, 0x0100, 0x0100,
1101 "Unknown",
1102 "Unknown",
1103 US_SC_DEVICE, US_PR_DEVICE, NULL,
1104 US_FL_SINGLE_LUN ),
1105
1076/* Submitted by Joris Struyve <joris@struyve.be> */ 1106/* Submitted by Joris Struyve <joris@struyve.be> */
1077UNUSUAL_DEV( 0x0d96, 0x410a, 0x0001, 0xffff, 1107UNUSUAL_DEV( 0x0d96, 0x410a, 0x0001, 0xffff,
1078 "Medion", 1108 "Medion",
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index dbcf23980ff1..dd108634348e 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -55,6 +55,7 @@
55#include <linux/init.h> 55#include <linux/init.h>
56#include <linux/slab.h> 56#include <linux/slab.h>
57#include <linux/kthread.h> 57#include <linux/kthread.h>
58#include <linux/mutex.h>
58 59
59#include <scsi/scsi.h> 60#include <scsi/scsi.h>
60#include <scsi/scsi_cmnd.h> 61#include <scsi/scsi_cmnd.h>
@@ -188,7 +189,7 @@ static int storage_suspend(struct usb_interface *iface, pm_message_t message)
188 struct us_data *us = usb_get_intfdata(iface); 189 struct us_data *us = usb_get_intfdata(iface);
189 190
190 /* Wait until no command is running */ 191 /* Wait until no command is running */
191 down(&us->dev_semaphore); 192 mutex_lock(&us->dev_mutex);
192 193
193 US_DEBUGP("%s\n", __FUNCTION__); 194 US_DEBUGP("%s\n", __FUNCTION__);
194 if (us->suspend_resume_hook) 195 if (us->suspend_resume_hook)
@@ -198,7 +199,7 @@ static int storage_suspend(struct usb_interface *iface, pm_message_t message)
198 /* When runtime PM is working, we'll set a flag to indicate 199 /* When runtime PM is working, we'll set a flag to indicate
199 * whether we should autoresume when a SCSI request arrives. */ 200 * whether we should autoresume when a SCSI request arrives. */
200 201
201 up(&us->dev_semaphore); 202 mutex_unlock(&us->dev_mutex);
202 return 0; 203 return 0;
203} 204}
204 205
@@ -206,14 +207,14 @@ static int storage_resume(struct usb_interface *iface)
206{ 207{
207 struct us_data *us = usb_get_intfdata(iface); 208 struct us_data *us = usb_get_intfdata(iface);
208 209
209 down(&us->dev_semaphore); 210 mutex_lock(&us->dev_mutex);
210 211
211 US_DEBUGP("%s\n", __FUNCTION__); 212 US_DEBUGP("%s\n", __FUNCTION__);
212 if (us->suspend_resume_hook) 213 if (us->suspend_resume_hook)
213 (us->suspend_resume_hook)(us, US_RESUME); 214 (us->suspend_resume_hook)(us, US_RESUME);
214 iface->dev.power.power_state.event = PM_EVENT_ON; 215 iface->dev.power.power_state.event = PM_EVENT_ON;
215 216
216 up(&us->dev_semaphore); 217 mutex_unlock(&us->dev_mutex);
217 return 0; 218 return 0;
218} 219}
219 220
@@ -276,12 +277,12 @@ static int usb_stor_control_thread(void * __us)
276 US_DEBUGP("*** thread awakened.\n"); 277 US_DEBUGP("*** thread awakened.\n");
277 278
278 /* lock the device pointers */ 279 /* lock the device pointers */
279 down(&(us->dev_semaphore)); 280 mutex_lock(&(us->dev_mutex));
280 281
281 /* if the device has disconnected, we are free to exit */ 282 /* if the device has disconnected, we are free to exit */
282 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 283 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
283 US_DEBUGP("-- exiting\n"); 284 US_DEBUGP("-- exiting\n");
284 up(&(us->dev_semaphore)); 285 mutex_unlock(&us->dev_mutex);
285 break; 286 break;
286 } 287 }
287 288
@@ -370,7 +371,7 @@ SkipForAbort:
370 scsi_unlock(host); 371 scsi_unlock(host);
371 372
372 /* unlock the device pointers */ 373 /* unlock the device pointers */
373 up(&(us->dev_semaphore)); 374 mutex_unlock(&us->dev_mutex);
374 } /* for (;;) */ 375 } /* for (;;) */
375 376
376 scsi_host_put(host); 377 scsi_host_put(host);
@@ -815,8 +816,8 @@ static void quiesce_and_remove_host(struct us_data *us)
815 * The thread will exit when it sees the DISCONNECTING flag. */ 816 * The thread will exit when it sees the DISCONNECTING flag. */
816 817
817 /* Wait for the current command to finish, then remove the host */ 818 /* Wait for the current command to finish, then remove the host */
818 down(&us->dev_semaphore); 819 mutex_lock(&us->dev_mutex);
819 up(&us->dev_semaphore); 820 mutex_unlock(&us->dev_mutex);
820 821
821 /* queuecommand won't accept any new commands and the control 822 /* queuecommand won't accept any new commands and the control
822 * thread won't execute a previously-queued command. If there 823 * thread won't execute a previously-queued command. If there
@@ -870,9 +871,9 @@ retry:
870 /* For bulk-only devices, determine the max LUN value */ 871 /* For bulk-only devices, determine the max LUN value */
871 if (us->protocol == US_PR_BULK && 872 if (us->protocol == US_PR_BULK &&
872 !(us->flags & US_FL_SINGLE_LUN)) { 873 !(us->flags & US_FL_SINGLE_LUN)) {
873 down(&us->dev_semaphore); 874 mutex_lock(&us->dev_mutex);
874 us->max_lun = usb_stor_Bulk_max_lun(us); 875 us->max_lun = usb_stor_Bulk_max_lun(us);
875 up(&us->dev_semaphore); 876 mutex_unlock(&us->dev_mutex);
876 } 877 }
877 scsi_scan_host(us_to_host(us)); 878 scsi_scan_host(us_to_host(us));
878 printk(KERN_DEBUG "usb-storage: device scan complete\n"); 879 printk(KERN_DEBUG "usb-storage: device scan complete\n");
@@ -912,7 +913,7 @@ static int storage_probe(struct usb_interface *intf,
912 913
913 us = host_to_us(host); 914 us = host_to_us(host);
914 memset(us, 0, sizeof(struct us_data)); 915 memset(us, 0, sizeof(struct us_data));
915 init_MUTEX(&(us->dev_semaphore)); 916 mutex_init(&(us->dev_mutex));
916 init_MUTEX_LOCKED(&(us->sema)); 917 init_MUTEX_LOCKED(&(us->sema));
917 init_completion(&(us->notify)); 918 init_completion(&(us->notify));
918 init_waitqueue_head(&us->delay_wait); 919 init_waitqueue_head(&us->delay_wait);
diff --git a/drivers/usb/storage/usb.h b/drivers/usb/storage/usb.h
index 7259fd1f6b0d..009fb0953a56 100644
--- a/drivers/usb/storage/usb.h
+++ b/drivers/usb/storage/usb.h
@@ -49,6 +49,7 @@
49#include <linux/blkdev.h> 49#include <linux/blkdev.h>
50#include <linux/smp_lock.h> 50#include <linux/smp_lock.h>
51#include <linux/completion.h> 51#include <linux/completion.h>
52#include <linux/mutex.h>
52#include <scsi/scsi_host.h> 53#include <scsi/scsi_host.h>
53 54
54struct us_data; 55struct us_data;
@@ -103,9 +104,9 @@ typedef void (*pm_hook)(struct us_data *, int); /* power management hook */
103struct us_data { 104struct us_data {
104 /* The device we're working with 105 /* The device we're working with
105 * It's important to note: 106 * It's important to note:
106 * (o) you must hold dev_semaphore to change pusb_dev 107 * (o) you must hold dev_mutex to change pusb_dev
107 */ 108 */
108 struct semaphore dev_semaphore; /* protect pusb_dev */ 109 struct mutex dev_mutex; /* protect pusb_dev */
109 struct usb_device *pusb_dev; /* this usb_device */ 110 struct usb_device *pusb_dev; /* this usb_device */
110 struct usb_interface *pusb_intf; /* this interface */ 111 struct usb_interface *pusb_intf; /* this interface */
111 struct us_unusual_dev *unusual_dev; /* device-filter entry */ 112 struct us_unusual_dev *unusual_dev; /* device-filter entry */