diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/usb/class |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/usb/class')
-rw-r--r-- | drivers/usb/class/Kconfig | 86 | ||||
-rw-r--r-- | drivers/usb/class/Makefile | 10 | ||||
-rw-r--r-- | drivers/usb/class/audio.c | 3882 | ||||
-rw-r--r-- | drivers/usb/class/audio.h | 110 | ||||
-rw-r--r-- | drivers/usb/class/bluetty.c | 1279 | ||||
-rw-r--r-- | drivers/usb/class/cdc-acm.c | 942 | ||||
-rw-r--r-- | drivers/usb/class/cdc-acm.h | 82 | ||||
-rw-r--r-- | drivers/usb/class/usb-midi.c | 2154 | ||||
-rw-r--r-- | drivers/usb/class/usb-midi.h | 164 | ||||
-rw-r--r-- | drivers/usb/class/usblp.c | 1214 |
10 files changed, 9923 insertions, 0 deletions
diff --git a/drivers/usb/class/Kconfig b/drivers/usb/class/Kconfig new file mode 100644 index 000000000000..0561d0234f23 --- /dev/null +++ b/drivers/usb/class/Kconfig | |||
@@ -0,0 +1,86 @@ | |||
1 | # | ||
2 | # USB Class driver configuration | ||
3 | # | ||
4 | comment "USB Device Class drivers" | ||
5 | depends on USB | ||
6 | |||
7 | config USB_AUDIO | ||
8 | tristate "USB Audio support" | ||
9 | depends on USB && SOUND | ||
10 | help | ||
11 | Say Y here if you want to connect USB audio equipment such as | ||
12 | speakers to your computer's USB port. You only need this if you use | ||
13 | the OSS sound driver; ALSA has its own option for usb audio support. | ||
14 | |||
15 | To compile this driver as a module, choose M here: the | ||
16 | module will be called audio. | ||
17 | |||
18 | comment "USB Bluetooth TTY can only be used with disabled Bluetooth subsystem" | ||
19 | depends on USB && BT | ||
20 | |||
21 | config USB_BLUETOOTH_TTY | ||
22 | tristate "USB Bluetooth TTY support" | ||
23 | depends on USB && BT=n | ||
24 | ---help--- | ||
25 | This driver implements a nonstandard tty interface to a Bluetooth | ||
26 | device that can be used only by specialized Bluetooth HCI software. | ||
27 | |||
28 | Say Y here if you want to use OpenBT Bluetooth stack (available | ||
29 | at <http://developer.axis.com/software>), or other TTY based | ||
30 | Bluetooth stacks, and want to connect a USB Bluetooth device | ||
31 | to your computer's USB port. | ||
32 | |||
33 | Do *not* enable this driver if you want to use generic Linux | ||
34 | Bluetooth support. | ||
35 | |||
36 | If in doubt, say N here. | ||
37 | |||
38 | To compile this driver as a module, choose M here: the | ||
39 | module will be called bluetty. | ||
40 | |||
41 | config USB_MIDI | ||
42 | tristate "USB MIDI support" | ||
43 | depends on USB && SOUND | ||
44 | ---help--- | ||
45 | Say Y here if you want to connect a USB MIDI device to your | ||
46 | computer's USB port. This driver is for devices that comply with | ||
47 | 'Universal Serial Bus Device Class Definition for MIDI Device'. | ||
48 | |||
49 | The following devices are known to work: | ||
50 | * Steinberg USB2MIDI | ||
51 | * Roland MPU64 | ||
52 | * Roland PC-300 | ||
53 | * Roland SC8850 | ||
54 | * Roland UM-1 | ||
55 | * Roland UM-2 | ||
56 | * Roland UA-100 | ||
57 | * Yamaha MU1000 | ||
58 | |||
59 | To compile this driver as a module, choose M here: the | ||
60 | module will be called usb-midi. | ||
61 | |||
62 | config USB_ACM | ||
63 | tristate "USB Modem (CDC ACM) support" | ||
64 | depends on USB | ||
65 | ---help--- | ||
66 | This driver supports USB modems and ISDN adapters which support the | ||
67 | Communication Device Class Abstract Control Model interface. | ||
68 | Please read <file:Documentation/usb/acm.txt> for details. | ||
69 | |||
70 | If your modem only reports "Cls=ff(vend.)" in the descriptors in | ||
71 | /proc/bus/usb/devices, then your modem will not work with this | ||
72 | driver. | ||
73 | |||
74 | To compile this driver as a module, choose M here: the | ||
75 | module will be called cdc-acm. | ||
76 | |||
77 | config USB_PRINTER | ||
78 | tristate "USB Printer support" | ||
79 | depends on USB | ||
80 | help | ||
81 | Say Y here if you want to connect a USB printer to your computer's | ||
82 | USB port. | ||
83 | |||
84 | To compile this driver as a module, choose M here: the | ||
85 | module will be called usblp. | ||
86 | |||
diff --git a/drivers/usb/class/Makefile b/drivers/usb/class/Makefile new file mode 100644 index 000000000000..971e5497a3fd --- /dev/null +++ b/drivers/usb/class/Makefile | |||
@@ -0,0 +1,10 @@ | |||
1 | # | ||
2 | # Makefile for USB Class drivers | ||
3 | # (one step up from the misc category) | ||
4 | # | ||
5 | |||
6 | obj-$(CONFIG_USB_ACM) += cdc-acm.o | ||
7 | obj-$(CONFIG_USB_AUDIO) += audio.o | ||
8 | obj-$(CONFIG_USB_BLUETOOTH_TTY) += bluetty.o | ||
9 | obj-$(CONFIG_USB_MIDI) += usb-midi.o | ||
10 | obj-$(CONFIG_USB_PRINTER) += usblp.o | ||
diff --git a/drivers/usb/class/audio.c b/drivers/usb/class/audio.c new file mode 100644 index 000000000000..f432b7d5b235 --- /dev/null +++ b/drivers/usb/class/audio.c | |||
@@ -0,0 +1,3882 @@ | |||
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 | */ | ||
220 | static struct list_head audiodevs = LIST_HEAD_INIT(audiodevs); | ||
221 | static DECLARE_MUTEX(open_sem); | ||
222 | |||
223 | /* | ||
224 | * wait queue for processes wanting to open an USB audio device | ||
225 | */ | ||
226 | static 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 | |||
246 | struct 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 | |||
257 | struct audioformat { | ||
258 | unsigned int format; | ||
259 | unsigned int sratelo; | ||
260 | unsigned int sratehi; | ||
261 | unsigned char altsetting; | ||
262 | unsigned char attributes; | ||
263 | }; | ||
264 | |||
265 | struct 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 | |||
290 | struct 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 | |||
299 | struct my_data_urb { | ||
300 | struct urb *urb; | ||
301 | }; | ||
302 | |||
303 | struct my_sync_urb { | ||
304 | struct urb *urb; | ||
305 | }; | ||
306 | |||
307 | |||
308 | struct 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 | |||
360 | struct 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 | |||
376 | struct 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 | |||
398 | static 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 | |||
435 | static 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 | |||
450 | static 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 | |||
500 | static 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 | |||
522 | static 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 | |||
545 | static 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 | |||
568 | static 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 | |||
593 | static 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 | |||
623 | static 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 | set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); | ||
635 | schedule_timeout(1); | ||
636 | spin_lock_irqsave(&as->lock, flags); | ||
637 | i = u->flags; | ||
638 | spin_unlock_irqrestore(&as->lock, flags); | ||
639 | if (notkilled && signal_pending(current)) { | ||
640 | if (i & FLG_URB0RUNNING) | ||
641 | usb_kill_urb(u->durb[0].urb); | ||
642 | if (i & FLG_URB1RUNNING) | ||
643 | usb_kill_urb(u->durb[1].urb); | ||
644 | if (i & FLG_SYNC0RUNNING) | ||
645 | usb_kill_urb(u->surb[0].urb); | ||
646 | if (i & FLG_SYNC1RUNNING) | ||
647 | usb_kill_urb(u->surb[1].urb); | ||
648 | notkilled = 0; | ||
649 | } | ||
650 | } | ||
651 | set_current_state(TASK_RUNNING); | ||
652 | if (u->durb[0].urb->transfer_buffer) | ||
653 | kfree(u->durb[0].urb->transfer_buffer); | ||
654 | if (u->durb[1].urb->transfer_buffer) | ||
655 | kfree(u->durb[1].urb->transfer_buffer); | ||
656 | if (u->surb[0].urb->transfer_buffer) | ||
657 | kfree(u->surb[0].urb->transfer_buffer); | ||
658 | if (u->surb[1].urb->transfer_buffer) | ||
659 | kfree(u->surb[1].urb->transfer_buffer); | ||
660 | u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer = | ||
661 | u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL; | ||
662 | } | ||
663 | |||
664 | static inline void usbin_release(struct usb_audiodev *as) | ||
665 | { | ||
666 | usbin_stop(as); | ||
667 | } | ||
668 | |||
669 | static void usbin_disc(struct usb_audiodev *as) | ||
670 | { | ||
671 | struct usbin *u = &as->usbin; | ||
672 | |||
673 | unsigned long flags; | ||
674 | |||
675 | spin_lock_irqsave(&as->lock, flags); | ||
676 | u->flags &= ~(FLG_RUNNING | FLG_CONNECTED); | ||
677 | spin_unlock_irqrestore(&as->lock, flags); | ||
678 | usbin_stop(as); | ||
679 | } | ||
680 | |||
681 | static void conversion(const void *ibuf, unsigned int ifmt, void *obuf, unsigned int ofmt, void *tmp, unsigned int scnt) | ||
682 | { | ||
683 | unsigned int cnt, i; | ||
684 | __s16 *sp, *sp2, s; | ||
685 | unsigned char *bp; | ||
686 | |||
687 | cnt = scnt; | ||
688 | if (AFMT_ISSTEREO(ifmt)) | ||
689 | cnt <<= 1; | ||
690 | sp = ((__s16 *)tmp) + cnt; | ||
691 | switch (ifmt & ~AFMT_STEREO) { | ||
692 | case AFMT_U8: | ||
693 | for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) { | ||
694 | bp--; | ||
695 | sp--; | ||
696 | *sp = (*bp ^ 0x80) << 8; | ||
697 | } | ||
698 | break; | ||
699 | |||
700 | case AFMT_S8: | ||
701 | for (bp = ((unsigned char *)ibuf)+cnt, i = 0; i < cnt; i++) { | ||
702 | bp--; | ||
703 | sp--; | ||
704 | *sp = *bp << 8; | ||
705 | } | ||
706 | break; | ||
707 | |||
708 | case AFMT_U16_LE: | ||
709 | for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { | ||
710 | bp -= 2; | ||
711 | sp--; | ||
712 | *sp = (bp[0] | (bp[1] << 8)) ^ 0x8000; | ||
713 | } | ||
714 | break; | ||
715 | |||
716 | case AFMT_U16_BE: | ||
717 | for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { | ||
718 | bp -= 2; | ||
719 | sp--; | ||
720 | *sp = (bp[1] | (bp[0] << 8)) ^ 0x8000; | ||
721 | } | ||
722 | break; | ||
723 | |||
724 | case AFMT_S16_LE: | ||
725 | for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { | ||
726 | bp -= 2; | ||
727 | sp--; | ||
728 | *sp = bp[0] | (bp[1] << 8); | ||
729 | } | ||
730 | break; | ||
731 | |||
732 | case AFMT_S16_BE: | ||
733 | for (bp = ((unsigned char *)ibuf)+2*cnt, i = 0; i < cnt; i++) { | ||
734 | bp -= 2; | ||
735 | sp--; | ||
736 | *sp = bp[1] | (bp[0] << 8); | ||
737 | } | ||
738 | break; | ||
739 | } | ||
740 | if (!AFMT_ISSTEREO(ifmt) && AFMT_ISSTEREO(ofmt)) { | ||
741 | /* expand from mono to stereo */ | ||
742 | for (sp = ((__s16 *)tmp)+scnt, sp2 = ((__s16 *)tmp)+2*scnt, i = 0; i < scnt; i++) { | ||
743 | sp--; | ||
744 | sp2 -= 2; | ||
745 | sp2[0] = sp2[1] = sp[0]; | ||
746 | } | ||
747 | } | ||
748 | if (AFMT_ISSTEREO(ifmt) && !AFMT_ISSTEREO(ofmt)) { | ||
749 | /* contract from stereo to mono */ | ||
750 | for (sp = sp2 = ((__s16 *)tmp), i = 0; i < scnt; i++, sp++, sp2 += 2) | ||
751 | sp[0] = (sp2[0] + sp2[1]) >> 1; | ||
752 | } | ||
753 | cnt = scnt; | ||
754 | if (AFMT_ISSTEREO(ofmt)) | ||
755 | cnt <<= 1; | ||
756 | sp = ((__s16 *)tmp); | ||
757 | bp = ((unsigned char *)obuf); | ||
758 | switch (ofmt & ~AFMT_STEREO) { | ||
759 | case AFMT_U8: | ||
760 | for (i = 0; i < cnt; i++, sp++, bp++) | ||
761 | *bp = (*sp >> 8) ^ 0x80; | ||
762 | break; | ||
763 | |||
764 | case AFMT_S8: | ||
765 | for (i = 0; i < cnt; i++, sp++, bp++) | ||
766 | *bp = *sp >> 8; | ||
767 | break; | ||
768 | |||
769 | case AFMT_U16_LE: | ||
770 | for (i = 0; i < cnt; i++, sp++, bp += 2) { | ||
771 | s = *sp; | ||
772 | bp[0] = s; | ||
773 | bp[1] = (s >> 8) ^ 0x80; | ||
774 | } | ||
775 | break; | ||
776 | |||
777 | case AFMT_U16_BE: | ||
778 | for (i = 0; i < cnt; i++, sp++, bp += 2) { | ||
779 | s = *sp; | ||
780 | bp[1] = s; | ||
781 | bp[0] = (s >> 8) ^ 0x80; | ||
782 | } | ||
783 | break; | ||
784 | |||
785 | case AFMT_S16_LE: | ||
786 | for (i = 0; i < cnt; i++, sp++, bp += 2) { | ||
787 | s = *sp; | ||
788 | bp[0] = s; | ||
789 | bp[1] = s >> 8; | ||
790 | } | ||
791 | break; | ||
792 | |||
793 | case AFMT_S16_BE: | ||
794 | for (i = 0; i < cnt; i++, sp++, bp += 2) { | ||
795 | s = *sp; | ||
796 | bp[1] = s; | ||
797 | bp[0] = s >> 8; | ||
798 | } | ||
799 | break; | ||
800 | } | ||
801 | |||
802 | } | ||
803 | |||
804 | static void usbin_convert(struct usbin *u, unsigned char *buffer, unsigned int samples) | ||
805 | { | ||
806 | union { | ||
807 | __s16 s[64]; | ||
808 | unsigned char b[0]; | ||
809 | } tmp; | ||
810 | unsigned int scnt, maxs, ufmtsh, dfmtsh; | ||
811 | |||
812 | ufmtsh = AFMT_BYTESSHIFT(u->format); | ||
813 | dfmtsh = AFMT_BYTESSHIFT(u->dma.format); | ||
814 | maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64; | ||
815 | while (samples > 0) { | ||
816 | scnt = samples; | ||
817 | if (scnt > maxs) | ||
818 | scnt = maxs; | ||
819 | conversion(buffer, u->format, tmp.b, u->dma.format, tmp.b, scnt); | ||
820 | dmabuf_copyin(&u->dma, tmp.b, scnt << dfmtsh); | ||
821 | buffer += scnt << ufmtsh; | ||
822 | samples -= scnt; | ||
823 | } | ||
824 | } | ||
825 | |||
826 | static int usbin_prepare_desc(struct usbin *u, struct urb *urb) | ||
827 | { | ||
828 | unsigned int i, maxsize, offs; | ||
829 | |||
830 | maxsize = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); | ||
831 | //printk(KERN_DEBUG "usbin_prepare_desc: maxsize %d freq 0x%x format 0x%x\n", maxsize, u->freqn, u->format); | ||
832 | for (i = offs = 0; i < DESCFRAMES; i++, offs += maxsize) { | ||
833 | urb->iso_frame_desc[i].length = maxsize; | ||
834 | urb->iso_frame_desc[i].offset = offs; | ||
835 | } | ||
836 | urb->interval = 1; | ||
837 | return 0; | ||
838 | } | ||
839 | |||
840 | /* | ||
841 | * return value: 0 if descriptor should be restarted, -1 otherwise | ||
842 | * convert sample format on the fly if necessary | ||
843 | */ | ||
844 | static int usbin_retire_desc(struct usbin *u, struct urb *urb) | ||
845 | { | ||
846 | unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, dmafree; | ||
847 | unsigned char *cp; | ||
848 | |||
849 | ufmtsh = AFMT_BYTESSHIFT(u->format); | ||
850 | dfmtsh = AFMT_BYTESSHIFT(u->dma.format); | ||
851 | for (i = 0; i < DESCFRAMES; i++) { | ||
852 | cp = ((unsigned char *)urb->transfer_buffer) + urb->iso_frame_desc[i].offset; | ||
853 | if (urb->iso_frame_desc[i].status) { | ||
854 | dprintk((KERN_DEBUG "usbin_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); | ||
855 | continue; | ||
856 | } | ||
857 | scnt = urb->iso_frame_desc[i].actual_length >> ufmtsh; | ||
858 | if (!scnt) | ||
859 | continue; | ||
860 | cnt = scnt << dfmtsh; | ||
861 | if (!u->dma.mapped) { | ||
862 | dmafree = u->dma.dmasize - u->dma.count; | ||
863 | if (cnt > dmafree) { | ||
864 | scnt = dmafree >> dfmtsh; | ||
865 | cnt = scnt << dfmtsh; | ||
866 | err++; | ||
867 | } | ||
868 | } | ||
869 | u->dma.count += cnt; | ||
870 | if (u->format == u->dma.format) { | ||
871 | /* we do not need format conversion */ | ||
872 | dprintk((KERN_DEBUG "usbaudio: no sample format conversion\n")); | ||
873 | dmabuf_copyin(&u->dma, cp, cnt); | ||
874 | } else { | ||
875 | /* we need sampling format conversion */ | ||
876 | dprintk((KERN_DEBUG "usbaudio: sample format conversion %x != %x\n", u->format, u->dma.format)); | ||
877 | usbin_convert(u, cp, scnt); | ||
878 | } | ||
879 | } | ||
880 | if (err) | ||
881 | u->dma.error++; | ||
882 | if (u->dma.count >= (signed)u->dma.fragsize) | ||
883 | wake_up(&u->dma.wait); | ||
884 | return err ? -1 : 0; | ||
885 | } | ||
886 | |||
887 | static void usbin_completed(struct urb *urb, struct pt_regs *regs) | ||
888 | { | ||
889 | struct usb_audiodev *as = (struct usb_audiodev *)urb->context; | ||
890 | struct usbin *u = &as->usbin; | ||
891 | unsigned long flags; | ||
892 | unsigned int mask; | ||
893 | int suret = 0; | ||
894 | |||
895 | #if 0 | ||
896 | printk(KERN_DEBUG "usbin_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); | ||
897 | #endif | ||
898 | if (urb == u->durb[0].urb) | ||
899 | mask = FLG_URB0RUNNING; | ||
900 | else if (urb == u->durb[1].urb) | ||
901 | mask = FLG_URB1RUNNING; | ||
902 | else { | ||
903 | mask = 0; | ||
904 | printk(KERN_ERR "usbin_completed: panic: unknown URB\n"); | ||
905 | } | ||
906 | urb->dev = as->state->usbdev; | ||
907 | spin_lock_irqsave(&as->lock, flags); | ||
908 | if (!usbin_retire_desc(u, urb) && | ||
909 | u->flags & FLG_RUNNING && | ||
910 | !usbin_prepare_desc(u, urb) && | ||
911 | (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { | ||
912 | u->flags |= mask; | ||
913 | } else { | ||
914 | u->flags &= ~(mask | FLG_RUNNING); | ||
915 | wake_up(&u->dma.wait); | ||
916 | printk(KERN_DEBUG "usbin_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret); | ||
917 | } | ||
918 | spin_unlock_irqrestore(&as->lock, flags); | ||
919 | } | ||
920 | |||
921 | /* | ||
922 | * we output sync data | ||
923 | */ | ||
924 | static int usbin_sync_prepare_desc(struct usbin *u, struct urb *urb) | ||
925 | { | ||
926 | unsigned char *cp = urb->transfer_buffer; | ||
927 | unsigned int i, offs; | ||
928 | |||
929 | for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3, cp += 3) { | ||
930 | urb->iso_frame_desc[i].length = 3; | ||
931 | urb->iso_frame_desc[i].offset = offs; | ||
932 | cp[0] = u->freqn; | ||
933 | cp[1] = u->freqn >> 8; | ||
934 | cp[2] = u->freqn >> 16; | ||
935 | } | ||
936 | urb->interval = 1; | ||
937 | return 0; | ||
938 | } | ||
939 | |||
940 | /* | ||
941 | * return value: 0 if descriptor should be restarted, -1 otherwise | ||
942 | */ | ||
943 | static int usbin_sync_retire_desc(struct usbin *u, struct urb *urb) | ||
944 | { | ||
945 | unsigned int i; | ||
946 | |||
947 | for (i = 0; i < SYNCFRAMES; i++) | ||
948 | if (urb->iso_frame_desc[0].status) | ||
949 | dprintk((KERN_DEBUG "usbin_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); | ||
950 | return 0; | ||
951 | } | ||
952 | |||
953 | static void usbin_sync_completed(struct urb *urb, struct pt_regs *regs) | ||
954 | { | ||
955 | struct usb_audiodev *as = (struct usb_audiodev *)urb->context; | ||
956 | struct usbin *u = &as->usbin; | ||
957 | unsigned long flags; | ||
958 | unsigned int mask; | ||
959 | int suret = 0; | ||
960 | |||
961 | #if 0 | ||
962 | printk(KERN_DEBUG "usbin_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); | ||
963 | #endif | ||
964 | if (urb == u->surb[0].urb) | ||
965 | mask = FLG_SYNC0RUNNING; | ||
966 | else if (urb == u->surb[1].urb) | ||
967 | mask = FLG_SYNC1RUNNING; | ||
968 | else { | ||
969 | mask = 0; | ||
970 | printk(KERN_ERR "usbin_sync_completed: panic: unknown URB\n"); | ||
971 | } | ||
972 | urb->dev = as->state->usbdev; | ||
973 | spin_lock_irqsave(&as->lock, flags); | ||
974 | if (!usbin_sync_retire_desc(u, urb) && | ||
975 | u->flags & FLG_RUNNING && | ||
976 | !usbin_sync_prepare_desc(u, urb) && | ||
977 | (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { | ||
978 | u->flags |= mask; | ||
979 | } else { | ||
980 | u->flags &= ~(mask | FLG_RUNNING); | ||
981 | wake_up(&u->dma.wait); | ||
982 | dprintk((KERN_DEBUG "usbin_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); | ||
983 | } | ||
984 | spin_unlock_irqrestore(&as->lock, flags); | ||
985 | } | ||
986 | |||
987 | static int usbin_start(struct usb_audiodev *as) | ||
988 | { | ||
989 | struct usb_device *dev = as->state->usbdev; | ||
990 | struct usbin *u = &as->usbin; | ||
991 | struct urb *urb; | ||
992 | unsigned long flags; | ||
993 | unsigned int maxsze, bufsz; | ||
994 | |||
995 | #if 0 | ||
996 | printk(KERN_DEBUG "usbin_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n", | ||
997 | dev->devnum, u->format, u->dma.format, u->dma.srate); | ||
998 | #endif | ||
999 | /* allocate USB storage if not already done */ | ||
1000 | spin_lock_irqsave(&as->lock, flags); | ||
1001 | if (!(u->flags & FLG_CONNECTED)) { | ||
1002 | spin_unlock_irqrestore(&as->lock, flags); | ||
1003 | return -EIO; | ||
1004 | } | ||
1005 | if (!(u->flags & FLG_RUNNING)) { | ||
1006 | spin_unlock_irqrestore(&as->lock, flags); | ||
1007 | u->freqn = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */ | ||
1008 | u->freqmax = u->freqn + (u->freqn >> 2); | ||
1009 | u->phase = 0; | ||
1010 | maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); | ||
1011 | bufsz = DESCFRAMES * maxsze; | ||
1012 | if (u->durb[0].urb->transfer_buffer) | ||
1013 | kfree(u->durb[0].urb->transfer_buffer); | ||
1014 | u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); | ||
1015 | u->durb[0].urb->transfer_buffer_length = bufsz; | ||
1016 | if (u->durb[1].urb->transfer_buffer) | ||
1017 | kfree(u->durb[1].urb->transfer_buffer); | ||
1018 | u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); | ||
1019 | u->durb[1].urb->transfer_buffer_length = bufsz; | ||
1020 | if (u->syncpipe) { | ||
1021 | if (u->surb[0].urb->transfer_buffer) | ||
1022 | kfree(u->surb[0].urb->transfer_buffer); | ||
1023 | u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); | ||
1024 | u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES; | ||
1025 | if (u->surb[1].urb->transfer_buffer) | ||
1026 | kfree(u->surb[1].urb->transfer_buffer); | ||
1027 | u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); | ||
1028 | u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES; | ||
1029 | } | ||
1030 | if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer || | ||
1031 | (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) { | ||
1032 | printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum); | ||
1033 | return 0; | ||
1034 | } | ||
1035 | spin_lock_irqsave(&as->lock, flags); | ||
1036 | } | ||
1037 | if (u->dma.count >= u->dma.dmasize && !u->dma.mapped) { | ||
1038 | spin_unlock_irqrestore(&as->lock, flags); | ||
1039 | return 0; | ||
1040 | } | ||
1041 | u->flags |= FLG_RUNNING; | ||
1042 | if (!(u->flags & FLG_URB0RUNNING)) { | ||
1043 | urb = u->durb[0].urb; | ||
1044 | urb->dev = dev; | ||
1045 | urb->pipe = u->datapipe; | ||
1046 | urb->transfer_flags = URB_ISO_ASAP; | ||
1047 | urb->number_of_packets = DESCFRAMES; | ||
1048 | urb->context = as; | ||
1049 | urb->complete = usbin_completed; | ||
1050 | if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) | ||
1051 | u->flags |= FLG_URB0RUNNING; | ||
1052 | else | ||
1053 | u->flags &= ~FLG_RUNNING; | ||
1054 | } | ||
1055 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) { | ||
1056 | urb = u->durb[1].urb; | ||
1057 | urb->dev = dev; | ||
1058 | urb->pipe = u->datapipe; | ||
1059 | urb->transfer_flags = URB_ISO_ASAP; | ||
1060 | urb->number_of_packets = DESCFRAMES; | ||
1061 | urb->context = as; | ||
1062 | urb->complete = usbin_completed; | ||
1063 | if (!usbin_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) | ||
1064 | u->flags |= FLG_URB1RUNNING; | ||
1065 | else | ||
1066 | u->flags &= ~FLG_RUNNING; | ||
1067 | } | ||
1068 | if (u->syncpipe) { | ||
1069 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) { | ||
1070 | urb = u->surb[0].urb; | ||
1071 | urb->dev = dev; | ||
1072 | urb->pipe = u->syncpipe; | ||
1073 | urb->transfer_flags = URB_ISO_ASAP; | ||
1074 | urb->number_of_packets = SYNCFRAMES; | ||
1075 | urb->context = as; | ||
1076 | urb->complete = usbin_sync_completed; | ||
1077 | /* stride: u->syncinterval */ | ||
1078 | if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) | ||
1079 | u->flags |= FLG_SYNC0RUNNING; | ||
1080 | else | ||
1081 | u->flags &= ~FLG_RUNNING; | ||
1082 | } | ||
1083 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) { | ||
1084 | urb = u->surb[1].urb; | ||
1085 | urb->dev = dev; | ||
1086 | urb->pipe = u->syncpipe; | ||
1087 | urb->transfer_flags = URB_ISO_ASAP; | ||
1088 | urb->number_of_packets = SYNCFRAMES; | ||
1089 | urb->context = as; | ||
1090 | urb->complete = usbin_sync_completed; | ||
1091 | /* stride: u->syncinterval */ | ||
1092 | if (!usbin_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_KERNEL)) | ||
1093 | u->flags |= FLG_SYNC1RUNNING; | ||
1094 | else | ||
1095 | u->flags &= ~FLG_RUNNING; | ||
1096 | } | ||
1097 | } | ||
1098 | spin_unlock_irqrestore(&as->lock, flags); | ||
1099 | return 0; | ||
1100 | } | ||
1101 | |||
1102 | static void usbout_stop(struct usb_audiodev *as) | ||
1103 | { | ||
1104 | struct usbout *u = &as->usbout; | ||
1105 | unsigned long flags; | ||
1106 | unsigned int i, notkilled = 1; | ||
1107 | |||
1108 | spin_lock_irqsave(&as->lock, flags); | ||
1109 | u->flags &= ~FLG_RUNNING; | ||
1110 | i = u->flags; | ||
1111 | spin_unlock_irqrestore(&as->lock, flags); | ||
1112 | while (i & (FLG_URB0RUNNING|FLG_URB1RUNNING|FLG_SYNC0RUNNING|FLG_SYNC1RUNNING)) { | ||
1113 | set_current_state(notkilled ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); | ||
1114 | schedule_timeout(1); | ||
1115 | spin_lock_irqsave(&as->lock, flags); | ||
1116 | i = u->flags; | ||
1117 | spin_unlock_irqrestore(&as->lock, flags); | ||
1118 | if (notkilled && signal_pending(current)) { | ||
1119 | if (i & FLG_URB0RUNNING) | ||
1120 | usb_kill_urb(u->durb[0].urb); | ||
1121 | if (i & FLG_URB1RUNNING) | ||
1122 | usb_kill_urb(u->durb[1].urb); | ||
1123 | if (i & FLG_SYNC0RUNNING) | ||
1124 | usb_kill_urb(u->surb[0].urb); | ||
1125 | if (i & FLG_SYNC1RUNNING) | ||
1126 | usb_kill_urb(u->surb[1].urb); | ||
1127 | notkilled = 0; | ||
1128 | } | ||
1129 | } | ||
1130 | set_current_state(TASK_RUNNING); | ||
1131 | if (u->durb[0].urb->transfer_buffer) | ||
1132 | kfree(u->durb[0].urb->transfer_buffer); | ||
1133 | if (u->durb[1].urb->transfer_buffer) | ||
1134 | kfree(u->durb[1].urb->transfer_buffer); | ||
1135 | if (u->surb[0].urb->transfer_buffer) | ||
1136 | kfree(u->surb[0].urb->transfer_buffer); | ||
1137 | if (u->surb[1].urb->transfer_buffer) | ||
1138 | kfree(u->surb[1].urb->transfer_buffer); | ||
1139 | u->durb[0].urb->transfer_buffer = u->durb[1].urb->transfer_buffer = | ||
1140 | u->surb[0].urb->transfer_buffer = u->surb[1].urb->transfer_buffer = NULL; | ||
1141 | } | ||
1142 | |||
1143 | static inline void usbout_release(struct usb_audiodev *as) | ||
1144 | { | ||
1145 | usbout_stop(as); | ||
1146 | } | ||
1147 | |||
1148 | static void usbout_disc(struct usb_audiodev *as) | ||
1149 | { | ||
1150 | struct usbout *u = &as->usbout; | ||
1151 | unsigned long flags; | ||
1152 | |||
1153 | spin_lock_irqsave(&as->lock, flags); | ||
1154 | u->flags &= ~(FLG_RUNNING | FLG_CONNECTED); | ||
1155 | spin_unlock_irqrestore(&as->lock, flags); | ||
1156 | usbout_stop(as); | ||
1157 | } | ||
1158 | |||
1159 | static void usbout_convert(struct usbout *u, unsigned char *buffer, unsigned int samples) | ||
1160 | { | ||
1161 | union { | ||
1162 | __s16 s[64]; | ||
1163 | unsigned char b[0]; | ||
1164 | } tmp; | ||
1165 | unsigned int scnt, maxs, ufmtsh, dfmtsh; | ||
1166 | |||
1167 | ufmtsh = AFMT_BYTESSHIFT(u->format); | ||
1168 | dfmtsh = AFMT_BYTESSHIFT(u->dma.format); | ||
1169 | maxs = (AFMT_ISSTEREO(u->dma.format | u->format)) ? 32 : 64; | ||
1170 | while (samples > 0) { | ||
1171 | scnt = samples; | ||
1172 | if (scnt > maxs) | ||
1173 | scnt = maxs; | ||
1174 | dmabuf_copyout(&u->dma, tmp.b, scnt << dfmtsh); | ||
1175 | conversion(tmp.b, u->dma.format, buffer, u->format, tmp.b, scnt); | ||
1176 | buffer += scnt << ufmtsh; | ||
1177 | samples -= scnt; | ||
1178 | } | ||
1179 | } | ||
1180 | |||
1181 | static int usbout_prepare_desc(struct usbout *u, struct urb *urb) | ||
1182 | { | ||
1183 | unsigned int i, ufmtsh, dfmtsh, err = 0, cnt, scnt, offs; | ||
1184 | unsigned char *cp = urb->transfer_buffer; | ||
1185 | |||
1186 | ufmtsh = AFMT_BYTESSHIFT(u->format); | ||
1187 | dfmtsh = AFMT_BYTESSHIFT(u->dma.format); | ||
1188 | for (i = offs = 0; i < DESCFRAMES; i++) { | ||
1189 | urb->iso_frame_desc[i].offset = offs; | ||
1190 | u->phase = (u->phase & 0x3fff) + u->freqm; | ||
1191 | scnt = u->phase >> 14; | ||
1192 | if (!scnt) { | ||
1193 | urb->iso_frame_desc[i].length = 0; | ||
1194 | continue; | ||
1195 | } | ||
1196 | cnt = scnt << dfmtsh; | ||
1197 | if (!u->dma.mapped) { | ||
1198 | if (cnt > u->dma.count) { | ||
1199 | scnt = u->dma.count >> dfmtsh; | ||
1200 | cnt = scnt << dfmtsh; | ||
1201 | err++; | ||
1202 | } | ||
1203 | u->dma.count -= cnt; | ||
1204 | } else | ||
1205 | u->dma.count += cnt; | ||
1206 | if (u->format == u->dma.format) { | ||
1207 | /* we do not need format conversion */ | ||
1208 | dmabuf_copyout(&u->dma, cp, cnt); | ||
1209 | } else { | ||
1210 | /* we need sampling format conversion */ | ||
1211 | usbout_convert(u, cp, scnt); | ||
1212 | } | ||
1213 | cnt = scnt << ufmtsh; | ||
1214 | urb->iso_frame_desc[i].length = cnt; | ||
1215 | offs += cnt; | ||
1216 | cp += cnt; | ||
1217 | } | ||
1218 | urb->interval = 1; | ||
1219 | if (err) | ||
1220 | u->dma.error++; | ||
1221 | if (u->dma.mapped) { | ||
1222 | if (u->dma.count >= (signed)u->dma.fragsize) | ||
1223 | wake_up(&u->dma.wait); | ||
1224 | } else { | ||
1225 | if ((signed)u->dma.dmasize >= u->dma.count + (signed)u->dma.fragsize) | ||
1226 | wake_up(&u->dma.wait); | ||
1227 | } | ||
1228 | return err ? -1 : 0; | ||
1229 | } | ||
1230 | |||
1231 | /* | ||
1232 | * return value: 0 if descriptor should be restarted, -1 otherwise | ||
1233 | */ | ||
1234 | static int usbout_retire_desc(struct usbout *u, struct urb *urb) | ||
1235 | { | ||
1236 | unsigned int i; | ||
1237 | |||
1238 | for (i = 0; i < DESCFRAMES; i++) { | ||
1239 | if (urb->iso_frame_desc[i].status) { | ||
1240 | dprintk((KERN_DEBUG "usbout_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); | ||
1241 | continue; | ||
1242 | } | ||
1243 | } | ||
1244 | return 0; | ||
1245 | } | ||
1246 | |||
1247 | static void usbout_completed(struct urb *urb, struct pt_regs *regs) | ||
1248 | { | ||
1249 | struct usb_audiodev *as = (struct usb_audiodev *)urb->context; | ||
1250 | struct usbout *u = &as->usbout; | ||
1251 | unsigned long flags; | ||
1252 | unsigned int mask; | ||
1253 | int suret = 0; | ||
1254 | |||
1255 | #if 0 | ||
1256 | printk(KERN_DEBUG "usbout_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); | ||
1257 | #endif | ||
1258 | if (urb == u->durb[0].urb) | ||
1259 | mask = FLG_URB0RUNNING; | ||
1260 | else if (urb == u->durb[1].urb) | ||
1261 | mask = FLG_URB1RUNNING; | ||
1262 | else { | ||
1263 | mask = 0; | ||
1264 | printk(KERN_ERR "usbout_completed: panic: unknown URB\n"); | ||
1265 | } | ||
1266 | urb->dev = as->state->usbdev; | ||
1267 | spin_lock_irqsave(&as->lock, flags); | ||
1268 | if (!usbout_retire_desc(u, urb) && | ||
1269 | u->flags & FLG_RUNNING && | ||
1270 | !usbout_prepare_desc(u, urb) && | ||
1271 | (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { | ||
1272 | u->flags |= mask; | ||
1273 | } else { | ||
1274 | u->flags &= ~(mask | FLG_RUNNING); | ||
1275 | wake_up(&u->dma.wait); | ||
1276 | dprintk((KERN_DEBUG "usbout_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); | ||
1277 | } | ||
1278 | spin_unlock_irqrestore(&as->lock, flags); | ||
1279 | } | ||
1280 | |||
1281 | static int usbout_sync_prepare_desc(struct usbout *u, struct urb *urb) | ||
1282 | { | ||
1283 | unsigned int i, offs; | ||
1284 | |||
1285 | for (i = offs = 0; i < SYNCFRAMES; i++, offs += 3) { | ||
1286 | urb->iso_frame_desc[i].length = 3; | ||
1287 | urb->iso_frame_desc[i].offset = offs; | ||
1288 | } | ||
1289 | urb->interval = 1; | ||
1290 | return 0; | ||
1291 | } | ||
1292 | |||
1293 | /* | ||
1294 | * return value: 0 if descriptor should be restarted, -1 otherwise | ||
1295 | */ | ||
1296 | static int usbout_sync_retire_desc(struct usbout *u, struct urb *urb) | ||
1297 | { | ||
1298 | unsigned char *cp = urb->transfer_buffer; | ||
1299 | unsigned int f, i; | ||
1300 | |||
1301 | for (i = 0; i < SYNCFRAMES; i++, cp += 3) { | ||
1302 | if (urb->iso_frame_desc[i].status) { | ||
1303 | dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u status %d\n", i, urb->iso_frame_desc[i].status)); | ||
1304 | continue; | ||
1305 | } | ||
1306 | if (urb->iso_frame_desc[i].actual_length < 3) { | ||
1307 | dprintk((KERN_DEBUG "usbout_sync_retire_desc: frame %u length %d\n", i, urb->iso_frame_desc[i].actual_length)); | ||
1308 | continue; | ||
1309 | } | ||
1310 | f = cp[0] | (cp[1] << 8) | (cp[2] << 16); | ||
1311 | if (abs(f - u->freqn) > (u->freqn >> 3) || f > u->freqmax) { | ||
1312 | printk(KERN_WARNING "usbout_sync_retire_desc: requested frequency %u (nominal %u) out of range!\n", f, u->freqn); | ||
1313 | continue; | ||
1314 | } | ||
1315 | u->freqm = f; | ||
1316 | } | ||
1317 | return 0; | ||
1318 | } | ||
1319 | |||
1320 | static void usbout_sync_completed(struct urb *urb, struct pt_regs *regs) | ||
1321 | { | ||
1322 | struct usb_audiodev *as = (struct usb_audiodev *)urb->context; | ||
1323 | struct usbout *u = &as->usbout; | ||
1324 | unsigned long flags; | ||
1325 | unsigned int mask; | ||
1326 | int suret = 0; | ||
1327 | |||
1328 | #if 0 | ||
1329 | printk(KERN_DEBUG "usbout_sync_completed: status %d errcnt %d flags 0x%x\n", urb->status, urb->error_count, u->flags); | ||
1330 | #endif | ||
1331 | if (urb == u->surb[0].urb) | ||
1332 | mask = FLG_SYNC0RUNNING; | ||
1333 | else if (urb == u->surb[1].urb) | ||
1334 | mask = FLG_SYNC1RUNNING; | ||
1335 | else { | ||
1336 | mask = 0; | ||
1337 | printk(KERN_ERR "usbout_sync_completed: panic: unknown URB\n"); | ||
1338 | } | ||
1339 | urb->dev = as->state->usbdev; | ||
1340 | spin_lock_irqsave(&as->lock, flags); | ||
1341 | if (!usbout_sync_retire_desc(u, urb) && | ||
1342 | u->flags & FLG_RUNNING && | ||
1343 | !usbout_sync_prepare_desc(u, urb) && | ||
1344 | (suret = usb_submit_urb(urb, GFP_ATOMIC)) == 0) { | ||
1345 | u->flags |= mask; | ||
1346 | } else { | ||
1347 | u->flags &= ~(mask | FLG_RUNNING); | ||
1348 | wake_up(&u->dma.wait); | ||
1349 | dprintk((KERN_DEBUG "usbout_sync_completed: descriptor not restarted (usb_submit_urb: %d)\n", suret)); | ||
1350 | } | ||
1351 | spin_unlock_irqrestore(&as->lock, flags); | ||
1352 | } | ||
1353 | |||
1354 | static int usbout_start(struct usb_audiodev *as) | ||
1355 | { | ||
1356 | struct usb_device *dev = as->state->usbdev; | ||
1357 | struct usbout *u = &as->usbout; | ||
1358 | struct urb *urb; | ||
1359 | unsigned long flags; | ||
1360 | unsigned int maxsze, bufsz; | ||
1361 | |||
1362 | #if 0 | ||
1363 | printk(KERN_DEBUG "usbout_start: device %d ufmt 0x%08x dfmt 0x%08x srate %d\n", | ||
1364 | dev->devnum, u->format, u->dma.format, u->dma.srate); | ||
1365 | #endif | ||
1366 | /* allocate USB storage if not already done */ | ||
1367 | spin_lock_irqsave(&as->lock, flags); | ||
1368 | if (!(u->flags & FLG_CONNECTED)) { | ||
1369 | spin_unlock_irqrestore(&as->lock, flags); | ||
1370 | return -EIO; | ||
1371 | } | ||
1372 | if (!(u->flags & FLG_RUNNING)) { | ||
1373 | spin_unlock_irqrestore(&as->lock, flags); | ||
1374 | u->freqn = u->freqm = ((u->dma.srate << 11) + 62) / 125; /* this will overflow at approx 2MSPS */ | ||
1375 | u->freqmax = u->freqn + (u->freqn >> 2); | ||
1376 | u->phase = 0; | ||
1377 | maxsze = (u->freqmax + 0x3fff) >> (14 - AFMT_BYTESSHIFT(u->format)); | ||
1378 | bufsz = DESCFRAMES * maxsze; | ||
1379 | if (u->durb[0].urb->transfer_buffer) | ||
1380 | kfree(u->durb[0].urb->transfer_buffer); | ||
1381 | u->durb[0].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); | ||
1382 | u->durb[0].urb->transfer_buffer_length = bufsz; | ||
1383 | if (u->durb[1].urb->transfer_buffer) | ||
1384 | kfree(u->durb[1].urb->transfer_buffer); | ||
1385 | u->durb[1].urb->transfer_buffer = kmalloc(bufsz, GFP_KERNEL); | ||
1386 | u->durb[1].urb->transfer_buffer_length = bufsz; | ||
1387 | if (u->syncpipe) { | ||
1388 | if (u->surb[0].urb->transfer_buffer) | ||
1389 | kfree(u->surb[0].urb->transfer_buffer); | ||
1390 | u->surb[0].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); | ||
1391 | u->surb[0].urb->transfer_buffer_length = 3*SYNCFRAMES; | ||
1392 | if (u->surb[1].urb->transfer_buffer) | ||
1393 | kfree(u->surb[1].urb->transfer_buffer); | ||
1394 | u->surb[1].urb->transfer_buffer = kmalloc(3*SYNCFRAMES, GFP_KERNEL); | ||
1395 | u->surb[1].urb->transfer_buffer_length = 3*SYNCFRAMES; | ||
1396 | } | ||
1397 | if (!u->durb[0].urb->transfer_buffer || !u->durb[1].urb->transfer_buffer || | ||
1398 | (u->syncpipe && (!u->surb[0].urb->transfer_buffer || !u->surb[1].urb->transfer_buffer))) { | ||
1399 | printk(KERN_ERR "usbaudio: cannot start playback device %d\n", dev->devnum); | ||
1400 | return 0; | ||
1401 | } | ||
1402 | spin_lock_irqsave(&as->lock, flags); | ||
1403 | } | ||
1404 | if (u->dma.count <= 0 && !u->dma.mapped) { | ||
1405 | spin_unlock_irqrestore(&as->lock, flags); | ||
1406 | return 0; | ||
1407 | } | ||
1408 | u->flags |= FLG_RUNNING; | ||
1409 | if (!(u->flags & FLG_URB0RUNNING)) { | ||
1410 | urb = u->durb[0].urb; | ||
1411 | urb->dev = dev; | ||
1412 | urb->pipe = u->datapipe; | ||
1413 | urb->transfer_flags = URB_ISO_ASAP; | ||
1414 | urb->number_of_packets = DESCFRAMES; | ||
1415 | urb->context = as; | ||
1416 | urb->complete = usbout_completed; | ||
1417 | if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) | ||
1418 | u->flags |= FLG_URB0RUNNING; | ||
1419 | else | ||
1420 | u->flags &= ~FLG_RUNNING; | ||
1421 | } | ||
1422 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_URB1RUNNING)) { | ||
1423 | urb = u->durb[1].urb; | ||
1424 | urb->dev = dev; | ||
1425 | urb->pipe = u->datapipe; | ||
1426 | urb->transfer_flags = URB_ISO_ASAP; | ||
1427 | urb->number_of_packets = DESCFRAMES; | ||
1428 | urb->context = as; | ||
1429 | urb->complete = usbout_completed; | ||
1430 | if (!usbout_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) | ||
1431 | u->flags |= FLG_URB1RUNNING; | ||
1432 | else | ||
1433 | u->flags &= ~FLG_RUNNING; | ||
1434 | } | ||
1435 | if (u->syncpipe) { | ||
1436 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC0RUNNING)) { | ||
1437 | urb = u->surb[0].urb; | ||
1438 | urb->dev = dev; | ||
1439 | urb->pipe = u->syncpipe; | ||
1440 | urb->transfer_flags = URB_ISO_ASAP; | ||
1441 | urb->number_of_packets = SYNCFRAMES; | ||
1442 | urb->context = as; | ||
1443 | urb->complete = usbout_sync_completed; | ||
1444 | /* stride: u->syncinterval */ | ||
1445 | if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) | ||
1446 | u->flags |= FLG_SYNC0RUNNING; | ||
1447 | else | ||
1448 | u->flags &= ~FLG_RUNNING; | ||
1449 | } | ||
1450 | if (u->flags & FLG_RUNNING && !(u->flags & FLG_SYNC1RUNNING)) { | ||
1451 | urb = u->surb[1].urb; | ||
1452 | urb->dev = dev; | ||
1453 | urb->pipe = u->syncpipe; | ||
1454 | urb->transfer_flags = URB_ISO_ASAP; | ||
1455 | urb->number_of_packets = SYNCFRAMES; | ||
1456 | urb->context = as; | ||
1457 | urb->complete = usbout_sync_completed; | ||
1458 | /* stride: u->syncinterval */ | ||
1459 | if (!usbout_sync_prepare_desc(u, urb) && !usb_submit_urb(urb, GFP_ATOMIC)) | ||
1460 | u->flags |= FLG_SYNC1RUNNING; | ||
1461 | else | ||
1462 | u->flags &= ~FLG_RUNNING; | ||
1463 | } | ||
1464 | } | ||
1465 | spin_unlock_irqrestore(&as->lock, flags); | ||
1466 | return 0; | ||
1467 | } | ||
1468 | |||
1469 | /* --------------------------------------------------------------------- */ | ||
1470 | |||
1471 | static unsigned int format_goodness(struct audioformat *afp, unsigned int fmt, unsigned int srate) | ||
1472 | { | ||
1473 | unsigned int g = 0; | ||
1474 | |||
1475 | if (srate < afp->sratelo) | ||
1476 | g += afp->sratelo - srate; | ||
1477 | if (srate > afp->sratehi) | ||
1478 | g += srate - afp->sratehi; | ||
1479 | if (AFMT_ISSTEREO(afp->format) && !AFMT_ISSTEREO(fmt)) | ||
1480 | g += 0x100000; | ||
1481 | if (!AFMT_ISSTEREO(afp->format) && AFMT_ISSTEREO(fmt)) | ||
1482 | g += 0x400000; | ||
1483 | if (AFMT_IS16BIT(afp->format) && !AFMT_IS16BIT(fmt)) | ||
1484 | g += 0x100000; | ||
1485 | if (!AFMT_IS16BIT(afp->format) && AFMT_IS16BIT(fmt)) | ||
1486 | g += 0x400000; | ||
1487 | return g; | ||
1488 | } | ||
1489 | |||
1490 | static int find_format(struct audioformat *afp, unsigned int nr, unsigned int fmt, unsigned int srate) | ||
1491 | { | ||
1492 | unsigned int i, g, gb = ~0; | ||
1493 | int j = -1; /* default to failure */ | ||
1494 | |||
1495 | /* find "best" format (according to format_goodness) */ | ||
1496 | for (i = 0; i < nr; i++) { | ||
1497 | g = format_goodness(&afp[i], fmt, srate); | ||
1498 | if (g >= gb) | ||
1499 | continue; | ||
1500 | j = i; | ||
1501 | gb = g; | ||
1502 | } | ||
1503 | return j; | ||
1504 | } | ||
1505 | |||
1506 | static int set_format_in(struct usb_audiodev *as) | ||
1507 | { | ||
1508 | struct usb_device *dev = as->state->usbdev; | ||
1509 | struct usb_host_interface *alts; | ||
1510 | struct usb_interface *iface; | ||
1511 | struct usbin *u = &as->usbin; | ||
1512 | struct dmabuf *d = &u->dma; | ||
1513 | struct audioformat *fmt; | ||
1514 | unsigned int ep; | ||
1515 | unsigned char data[3]; | ||
1516 | int fmtnr, ret; | ||
1517 | |||
1518 | iface = usb_ifnum_to_if(dev, u->interface); | ||
1519 | if (!iface) | ||
1520 | return 0; | ||
1521 | |||
1522 | fmtnr = find_format(as->fmtin, as->numfmtin, d->format, d->srate); | ||
1523 | if (fmtnr < 0) { | ||
1524 | printk(KERN_ERR "usbaudio: set_format_in(): failed to find desired format/speed combination.\n"); | ||
1525 | return -1; | ||
1526 | } | ||
1527 | |||
1528 | fmt = as->fmtin + fmtnr; | ||
1529 | alts = usb_altnum_to_altsetting(iface, fmt->altsetting); | ||
1530 | u->format = fmt->format; | ||
1531 | u->datapipe = usb_rcvisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf); | ||
1532 | u->syncpipe = u->syncinterval = 0; | ||
1533 | if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x08) { | ||
1534 | if (alts->desc.bNumEndpoints < 2 || | ||
1535 | alts->endpoint[1].desc.bmAttributes != 0x01 || | ||
1536 | alts->endpoint[1].desc.bSynchAddress != 0 || | ||
1537 | alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress & 0x7f)) { | ||
1538 | printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims adaptive in " | ||
1539 | "but has invalid synch pipe; treating as asynchronous in\n", | ||
1540 | dev->devnum, u->interface, fmt->altsetting); | ||
1541 | } else { | ||
1542 | u->syncpipe = usb_sndisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf); | ||
1543 | u->syncinterval = alts->endpoint[1].desc.bRefresh; | ||
1544 | } | ||
1545 | } | ||
1546 | if (d->srate < fmt->sratelo) | ||
1547 | d->srate = fmt->sratelo; | ||
1548 | if (d->srate > fmt->sratehi) | ||
1549 | d->srate = fmt->sratehi; | ||
1550 | dprintk((KERN_DEBUG "usbaudio: set_format_in: usb_set_interface %u %u\n", | ||
1551 | u->interface, fmt->altsetting)); | ||
1552 | if (usb_set_interface(dev, alts->desc.bInterfaceNumber, fmt->altsetting) < 0) { | ||
1553 | printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n", | ||
1554 | dev->devnum, u->interface, fmt->altsetting); | ||
1555 | return -1; | ||
1556 | } | ||
1557 | if (fmt->sratelo == fmt->sratehi) | ||
1558 | return 0; | ||
1559 | ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN); | ||
1560 | /* if endpoint has pitch control, enable it */ | ||
1561 | if (fmt->attributes & 0x02) { | ||
1562 | data[0] = 1; | ||
1563 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | ||
1564 | PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) { | ||
1565 | printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n", | ||
1566 | ret, dev->devnum, u->interface, ep, d->srate); | ||
1567 | return -1; | ||
1568 | } | ||
1569 | } | ||
1570 | /* if endpoint has sampling rate control, set it */ | ||
1571 | if (fmt->attributes & 0x01) { | ||
1572 | data[0] = d->srate; | ||
1573 | data[1] = d->srate >> 8; | ||
1574 | data[2] = d->srate >> 16; | ||
1575 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | ||
1576 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | ||
1577 | printk(KERN_ERR "usbaudio: failure (error %d) to set input sampling frequency device %d interface %u endpoint 0x%x to %u\n", | ||
1578 | ret, dev->devnum, u->interface, ep, d->srate); | ||
1579 | return -1; | ||
1580 | } | ||
1581 | if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, | ||
1582 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | ||
1583 | printk(KERN_ERR "usbaudio: failure (error %d) to get input sampling frequency device %d interface %u endpoint 0x%x\n", | ||
1584 | ret, dev->devnum, u->interface, ep); | ||
1585 | return -1; | ||
1586 | } | ||
1587 | dprintk((KERN_DEBUG "usbaudio: set_format_in: device %d interface %d altsetting %d srate req: %u real %u\n", | ||
1588 | dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16))); | ||
1589 | d->srate = data[0] | (data[1] << 8) | (data[2] << 16); | ||
1590 | } | ||
1591 | dprintk((KERN_DEBUG "usbaudio: set_format_in: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate)); | ||
1592 | return 0; | ||
1593 | } | ||
1594 | |||
1595 | static int set_format_out(struct usb_audiodev *as) | ||
1596 | { | ||
1597 | struct usb_device *dev = as->state->usbdev; | ||
1598 | struct usb_host_interface *alts; | ||
1599 | struct usb_interface *iface; | ||
1600 | struct usbout *u = &as->usbout; | ||
1601 | struct dmabuf *d = &u->dma; | ||
1602 | struct audioformat *fmt; | ||
1603 | unsigned int ep; | ||
1604 | unsigned char data[3]; | ||
1605 | int fmtnr, ret; | ||
1606 | |||
1607 | iface = usb_ifnum_to_if(dev, u->interface); | ||
1608 | if (!iface) | ||
1609 | return 0; | ||
1610 | |||
1611 | fmtnr = find_format(as->fmtout, as->numfmtout, d->format, d->srate); | ||
1612 | if (fmtnr < 0) { | ||
1613 | printk(KERN_ERR "usbaudio: set_format_out(): failed to find desired format/speed combination.\n"); | ||
1614 | return -1; | ||
1615 | } | ||
1616 | |||
1617 | fmt = as->fmtout + fmtnr; | ||
1618 | u->format = fmt->format; | ||
1619 | alts = usb_altnum_to_altsetting(iface, fmt->altsetting); | ||
1620 | u->datapipe = usb_sndisocpipe(dev, alts->endpoint[0].desc.bEndpointAddress & 0xf); | ||
1621 | u->syncpipe = u->syncinterval = 0; | ||
1622 | if ((alts->endpoint[0].desc.bmAttributes & 0x0c) == 0x04) { | ||
1623 | #if 0 | ||
1624 | printk(KERN_DEBUG "bNumEndpoints 0x%02x endpoint[1].bmAttributes 0x%02x\n" | ||
1625 | KERN_DEBUG "endpoint[1].bSynchAddress 0x%02x endpoint[1].bEndpointAddress 0x%02x\n" | ||
1626 | KERN_DEBUG "endpoint[0].bSynchAddress 0x%02x\n", alts->bNumEndpoints, | ||
1627 | alts->endpoint[1].bmAttributes, alts->endpoint[1].bSynchAddress, | ||
1628 | alts->endpoint[1].bEndpointAddress, alts->endpoint[0].bSynchAddress); | ||
1629 | #endif | ||
1630 | if (alts->desc.bNumEndpoints < 2 || | ||
1631 | alts->endpoint[1].desc.bmAttributes != 0x01 || | ||
1632 | alts->endpoint[1].desc.bSynchAddress != 0 || | ||
1633 | alts->endpoint[1].desc.bEndpointAddress != (alts->endpoint[0].desc.bSynchAddress | 0x80)) { | ||
1634 | printk(KERN_WARNING "usbaudio: device %d interface %d altsetting %d claims asynch out " | ||
1635 | "but has invalid synch pipe; treating as adaptive out\n", | ||
1636 | dev->devnum, u->interface, fmt->altsetting); | ||
1637 | } else { | ||
1638 | u->syncpipe = usb_rcvisocpipe(dev, alts->endpoint[1].desc.bEndpointAddress & 0xf); | ||
1639 | u->syncinterval = alts->endpoint[1].desc.bRefresh; | ||
1640 | } | ||
1641 | } | ||
1642 | if (d->srate < fmt->sratelo) | ||
1643 | d->srate = fmt->sratelo; | ||
1644 | if (d->srate > fmt->sratehi) | ||
1645 | d->srate = fmt->sratehi; | ||
1646 | dprintk((KERN_DEBUG "usbaudio: set_format_out: usb_set_interface %u %u\n", | ||
1647 | u->interface, fmt->altsetting)); | ||
1648 | if (usb_set_interface(dev, u->interface, fmt->altsetting) < 0) { | ||
1649 | printk(KERN_WARNING "usbaudio: usb_set_interface failed, device %d interface %d altsetting %d\n", | ||
1650 | dev->devnum, u->interface, fmt->altsetting); | ||
1651 | return -1; | ||
1652 | } | ||
1653 | if (fmt->sratelo == fmt->sratehi) | ||
1654 | return 0; | ||
1655 | ep = usb_pipeendpoint(u->datapipe) | (u->datapipe & USB_DIR_IN); | ||
1656 | /* if endpoint has pitch control, enable it */ | ||
1657 | if (fmt->attributes & 0x02) { | ||
1658 | data[0] = 1; | ||
1659 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | ||
1660 | PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) { | ||
1661 | printk(KERN_ERR "usbaudio: failure (error %d) to set output pitch control device %d interface %u endpoint 0x%x to %u\n", | ||
1662 | ret, dev->devnum, u->interface, ep, d->srate); | ||
1663 | return -1; | ||
1664 | } | ||
1665 | } | ||
1666 | /* if endpoint has sampling rate control, set it */ | ||
1667 | if (fmt->attributes & 0x01) { | ||
1668 | data[0] = d->srate; | ||
1669 | data[1] = d->srate >> 8; | ||
1670 | data[2] = d->srate >> 16; | ||
1671 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | ||
1672 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | ||
1673 | printk(KERN_ERR "usbaudio: failure (error %d) to set output sampling frequency device %d interface %u endpoint 0x%x to %u\n", | ||
1674 | ret, dev->devnum, u->interface, ep, d->srate); | ||
1675 | return -1; | ||
1676 | } | ||
1677 | if ((ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, | ||
1678 | SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { | ||
1679 | printk(KERN_ERR "usbaudio: failure (error %d) to get output sampling frequency device %d interface %u endpoint 0x%x\n", | ||
1680 | ret, dev->devnum, u->interface, ep); | ||
1681 | return -1; | ||
1682 | } | ||
1683 | dprintk((KERN_DEBUG "usbaudio: set_format_out: device %d interface %d altsetting %d srate req: %u real %u\n", | ||
1684 | dev->devnum, u->interface, fmt->altsetting, d->srate, data[0] | (data[1] << 8) | (data[2] << 16))); | ||
1685 | d->srate = data[0] | (data[1] << 8) | (data[2] << 16); | ||
1686 | } | ||
1687 | dprintk((KERN_DEBUG "usbaudio: set_format_out: USB format 0x%x, DMA format 0x%x srate %u\n", u->format, d->format, d->srate)); | ||
1688 | return 0; | ||
1689 | } | ||
1690 | |||
1691 | static int set_format(struct usb_audiodev *s, unsigned int fmode, unsigned int fmt, unsigned int srate) | ||
1692 | { | ||
1693 | int ret1 = 0, ret2 = 0; | ||
1694 | |||
1695 | if (!(fmode & (FMODE_READ|FMODE_WRITE))) | ||
1696 | return -EINVAL; | ||
1697 | if (fmode & FMODE_READ) { | ||
1698 | usbin_stop(s); | ||
1699 | s->usbin.dma.ready = 0; | ||
1700 | if (fmt == AFMT_QUERY) | ||
1701 | fmt = s->usbin.dma.format; | ||
1702 | else | ||
1703 | s->usbin.dma.format = fmt; | ||
1704 | if (!srate) | ||
1705 | srate = s->usbin.dma.srate; | ||
1706 | else | ||
1707 | s->usbin.dma.srate = srate; | ||
1708 | } | ||
1709 | if (fmode & FMODE_WRITE) { | ||
1710 | usbout_stop(s); | ||
1711 | s->usbout.dma.ready = 0; | ||
1712 | if (fmt == AFMT_QUERY) | ||
1713 | fmt = s->usbout.dma.format; | ||
1714 | else | ||
1715 | s->usbout.dma.format = fmt; | ||
1716 | if (!srate) | ||
1717 | srate = s->usbout.dma.srate; | ||
1718 | else | ||
1719 | s->usbout.dma.srate = srate; | ||
1720 | } | ||
1721 | if (fmode & FMODE_READ) | ||
1722 | ret1 = set_format_in(s); | ||
1723 | if (fmode & FMODE_WRITE) | ||
1724 | ret2 = set_format_out(s); | ||
1725 | return ret1 ? ret1 : ret2; | ||
1726 | } | ||
1727 | |||
1728 | /* --------------------------------------------------------------------- */ | ||
1729 | |||
1730 | static int wrmixer(struct usb_mixerdev *ms, unsigned mixch, unsigned value) | ||
1731 | { | ||
1732 | struct usb_device *dev = ms->state->usbdev; | ||
1733 | unsigned char data[2]; | ||
1734 | struct mixerchannel *ch; | ||
1735 | int v1, v2, v3; | ||
1736 | |||
1737 | if (mixch >= ms->numch) | ||
1738 | return -1; | ||
1739 | ch = &ms->ch[mixch]; | ||
1740 | v3 = ch->maxval - ch->minval; | ||
1741 | v1 = value & 0xff; | ||
1742 | v2 = (value >> 8) & 0xff; | ||
1743 | if (v1 > 100) | ||
1744 | v1 = 100; | ||
1745 | if (v2 > 100) | ||
1746 | v2 = 100; | ||
1747 | if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) | ||
1748 | v2 = v1; | ||
1749 | ch->value = v1 | (v2 << 8); | ||
1750 | v1 = (v1 * v3) / 100 + ch->minval; | ||
1751 | v2 = (v2 * v3) / 100 + ch->minval; | ||
1752 | switch (ch->selector) { | ||
1753 | case 0: /* mixer unit request */ | ||
1754 | data[0] = v1; | ||
1755 | data[1] = v1 >> 8; | ||
1756 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1757 | (ch->chnum << 8) | 1, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0) | ||
1758 | goto err; | ||
1759 | if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) | ||
1760 | return 0; | ||
1761 | data[0] = v2; | ||
1762 | data[1] = v2 >> 8; | ||
1763 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1764 | ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)), | ||
1765 | ms->iface | (ch->unitid << 8), data, 2, 1000) < 0) | ||
1766 | goto err; | ||
1767 | return 0; | ||
1768 | |||
1769 | /* various feature unit controls */ | ||
1770 | case VOLUME_CONTROL: | ||
1771 | data[0] = v1; | ||
1772 | data[1] = v1 >> 8; | ||
1773 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1774 | (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 2, 1000) < 0) | ||
1775 | goto err; | ||
1776 | if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) | ||
1777 | return 0; | ||
1778 | data[0] = v2; | ||
1779 | data[1] = v2 >> 8; | ||
1780 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1781 | (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 2, 1000) < 0) | ||
1782 | goto err; | ||
1783 | return 0; | ||
1784 | |||
1785 | case BASS_CONTROL: | ||
1786 | case MID_CONTROL: | ||
1787 | case TREBLE_CONTROL: | ||
1788 | data[0] = v1 >> 8; | ||
1789 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1790 | (ch->selector << 8) | ch->chnum, ms->iface | (ch->unitid << 8), data, 1, 1000) < 0) | ||
1791 | goto err; | ||
1792 | if (!(ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) | ||
1793 | return 0; | ||
1794 | data[0] = v2 >> 8; | ||
1795 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1796 | (ch->selector << 8) | (ch->chnum + 1), ms->iface | (ch->unitid << 8), data, 1, 1000) < 0) | ||
1797 | goto err; | ||
1798 | return 0; | ||
1799 | |||
1800 | default: | ||
1801 | return -1; | ||
1802 | } | ||
1803 | return 0; | ||
1804 | |||
1805 | err: | ||
1806 | printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n", | ||
1807 | dev->devnum, ms->iface, ch->unitid, ch->chnum, ch->selector); | ||
1808 | return -1; | ||
1809 | } | ||
1810 | |||
1811 | static int get_rec_src(struct usb_mixerdev *ms) | ||
1812 | { | ||
1813 | struct usb_device *dev = ms->state->usbdev; | ||
1814 | unsigned int mask = 0, retmask = 0; | ||
1815 | unsigned int i, j; | ||
1816 | unsigned char buf; | ||
1817 | int err = 0; | ||
1818 | |||
1819 | for (i = 0; i < ms->numch; i++) { | ||
1820 | if (!ms->ch[i].slctunitid || (mask & (1 << i))) | ||
1821 | continue; | ||
1822 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
1823 | 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) { | ||
1824 | err = -EIO; | ||
1825 | printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n", | ||
1826 | dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff); | ||
1827 | continue; | ||
1828 | } | ||
1829 | for (j = i; j < ms->numch; j++) { | ||
1830 | if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) | ||
1831 | continue; | ||
1832 | mask |= 1 << j; | ||
1833 | if (buf == (ms->ch[j].slctunitid >> 8)) | ||
1834 | retmask |= 1 << ms->ch[j].osschannel; | ||
1835 | } | ||
1836 | } | ||
1837 | if (err) | ||
1838 | return -EIO; | ||
1839 | return retmask; | ||
1840 | } | ||
1841 | |||
1842 | static int set_rec_src(struct usb_mixerdev *ms, int srcmask) | ||
1843 | { | ||
1844 | struct usb_device *dev = ms->state->usbdev; | ||
1845 | unsigned int mask = 0, smask, bmask; | ||
1846 | unsigned int i, j; | ||
1847 | unsigned char buf; | ||
1848 | int err = 0; | ||
1849 | |||
1850 | for (i = 0; i < ms->numch; i++) { | ||
1851 | if (!ms->ch[i].slctunitid || (mask & (1 << i))) | ||
1852 | continue; | ||
1853 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
1854 | 0, ms->iface | (ms->ch[i].slctunitid << 8), &buf, 1, 1000) < 0) { | ||
1855 | err = -EIO; | ||
1856 | printk(KERN_ERR "usbaudio: selector read request device %u if %u unit %u failed\n", | ||
1857 | dev->devnum, ms->iface, ms->ch[i].slctunitid & 0xff); | ||
1858 | continue; | ||
1859 | } | ||
1860 | /* first generate smask */ | ||
1861 | smask = bmask = 0; | ||
1862 | for (j = i; j < ms->numch; j++) { | ||
1863 | if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) | ||
1864 | continue; | ||
1865 | smask |= 1 << ms->ch[j].osschannel; | ||
1866 | if (buf == (ms->ch[j].slctunitid >> 8)) | ||
1867 | bmask |= 1 << ms->ch[j].osschannel; | ||
1868 | mask |= 1 << j; | ||
1869 | } | ||
1870 | /* check for multiple set sources */ | ||
1871 | j = hweight32(srcmask & smask); | ||
1872 | if (j == 0) | ||
1873 | continue; | ||
1874 | if (j > 1) | ||
1875 | srcmask &= ~bmask; | ||
1876 | for (j = i; j < ms->numch; j++) { | ||
1877 | if ((ms->ch[i].slctunitid ^ ms->ch[j].slctunitid) & 0xff) | ||
1878 | continue; | ||
1879 | if (!(srcmask & (1 << ms->ch[j].osschannel))) | ||
1880 | continue; | ||
1881 | buf = ms->ch[j].slctunitid >> 8; | ||
1882 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
1883 | 0, ms->iface | (ms->ch[j].slctunitid << 8), &buf, 1, 1000) < 0) { | ||
1884 | err = -EIO; | ||
1885 | printk(KERN_ERR "usbaudio: selector write request device %u if %u unit %u failed\n", | ||
1886 | dev->devnum, ms->iface, ms->ch[j].slctunitid & 0xff); | ||
1887 | continue; | ||
1888 | } | ||
1889 | } | ||
1890 | } | ||
1891 | return err ? -EIO : 0; | ||
1892 | } | ||
1893 | |||
1894 | /* --------------------------------------------------------------------- */ | ||
1895 | |||
1896 | /* | ||
1897 | * should be called with open_sem hold, so that no new processes | ||
1898 | * look at the audio device to be destroyed | ||
1899 | */ | ||
1900 | |||
1901 | static void release(struct usb_audio_state *s) | ||
1902 | { | ||
1903 | struct usb_audiodev *as; | ||
1904 | struct usb_mixerdev *ms; | ||
1905 | |||
1906 | s->count--; | ||
1907 | if (s->count) { | ||
1908 | up(&open_sem); | ||
1909 | return; | ||
1910 | } | ||
1911 | up(&open_sem); | ||
1912 | wake_up(&open_wait); | ||
1913 | while (!list_empty(&s->audiolist)) { | ||
1914 | as = list_entry(s->audiolist.next, struct usb_audiodev, list); | ||
1915 | list_del(&as->list); | ||
1916 | usbin_release(as); | ||
1917 | usbout_release(as); | ||
1918 | dmabuf_release(&as->usbin.dma); | ||
1919 | dmabuf_release(&as->usbout.dma); | ||
1920 | usb_free_urb(as->usbin.durb[0].urb); | ||
1921 | usb_free_urb(as->usbin.durb[1].urb); | ||
1922 | usb_free_urb(as->usbin.surb[0].urb); | ||
1923 | usb_free_urb(as->usbin.surb[1].urb); | ||
1924 | usb_free_urb(as->usbout.durb[0].urb); | ||
1925 | usb_free_urb(as->usbout.durb[1].urb); | ||
1926 | usb_free_urb(as->usbout.surb[0].urb); | ||
1927 | usb_free_urb(as->usbout.surb[1].urb); | ||
1928 | kfree(as); | ||
1929 | } | ||
1930 | while (!list_empty(&s->mixerlist)) { | ||
1931 | ms = list_entry(s->mixerlist.next, struct usb_mixerdev, list); | ||
1932 | list_del(&ms->list); | ||
1933 | kfree(ms); | ||
1934 | } | ||
1935 | kfree(s); | ||
1936 | } | ||
1937 | |||
1938 | static inline int prog_dmabuf_in(struct usb_audiodev *as) | ||
1939 | { | ||
1940 | usbin_stop(as); | ||
1941 | return dmabuf_init(&as->usbin.dma); | ||
1942 | } | ||
1943 | |||
1944 | static inline int prog_dmabuf_out(struct usb_audiodev *as) | ||
1945 | { | ||
1946 | usbout_stop(as); | ||
1947 | return dmabuf_init(&as->usbout.dma); | ||
1948 | } | ||
1949 | |||
1950 | /* --------------------------------------------------------------------- */ | ||
1951 | |||
1952 | static int usb_audio_open_mixdev(struct inode *inode, struct file *file) | ||
1953 | { | ||
1954 | unsigned int minor = iminor(inode); | ||
1955 | struct usb_mixerdev *ms; | ||
1956 | struct usb_audio_state *s; | ||
1957 | |||
1958 | down(&open_sem); | ||
1959 | list_for_each_entry(s, &audiodevs, audiodev) { | ||
1960 | list_for_each_entry(ms, &s->mixerlist, list) { | ||
1961 | if (ms->dev_mixer == minor) | ||
1962 | goto mixer_found; | ||
1963 | } | ||
1964 | } | ||
1965 | up(&open_sem); | ||
1966 | return -ENODEV; | ||
1967 | |||
1968 | mixer_found: | ||
1969 | if (!s->usbdev) { | ||
1970 | up(&open_sem); | ||
1971 | return -EIO; | ||
1972 | } | ||
1973 | file->private_data = ms; | ||
1974 | s->count++; | ||
1975 | |||
1976 | up(&open_sem); | ||
1977 | return nonseekable_open(inode, file); | ||
1978 | } | ||
1979 | |||
1980 | static int usb_audio_release_mixdev(struct inode *inode, struct file *file) | ||
1981 | { | ||
1982 | struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data; | ||
1983 | struct usb_audio_state *s; | ||
1984 | |||
1985 | lock_kernel(); | ||
1986 | s = ms->state; | ||
1987 | down(&open_sem); | ||
1988 | release(s); | ||
1989 | unlock_kernel(); | ||
1990 | return 0; | ||
1991 | } | ||
1992 | |||
1993 | static int usb_audio_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | ||
1994 | { | ||
1995 | struct usb_mixerdev *ms = (struct usb_mixerdev *)file->private_data; | ||
1996 | int i, j, val; | ||
1997 | int __user *user_arg = (int __user *)arg; | ||
1998 | |||
1999 | if (!ms->state->usbdev) | ||
2000 | return -ENODEV; | ||
2001 | |||
2002 | if (cmd == SOUND_MIXER_INFO) { | ||
2003 | mixer_info info; | ||
2004 | |||
2005 | memset(&info, 0, sizeof(info)); | ||
2006 | strncpy(info.id, "USB_AUDIO", sizeof(info.id)); | ||
2007 | strncpy(info.name, "USB Audio Class Driver", sizeof(info.name)); | ||
2008 | info.modify_counter = ms->modcnt; | ||
2009 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | ||
2010 | return -EFAULT; | ||
2011 | return 0; | ||
2012 | } | ||
2013 | if (cmd == SOUND_OLD_MIXER_INFO) { | ||
2014 | _old_mixer_info info; | ||
2015 | |||
2016 | memset(&info, 0, sizeof(info)); | ||
2017 | strncpy(info.id, "USB_AUDIO", sizeof(info.id)); | ||
2018 | strncpy(info.name, "USB Audio Class Driver", sizeof(info.name)); | ||
2019 | if (copy_to_user((void __user *)arg, &info, sizeof(info))) | ||
2020 | return -EFAULT; | ||
2021 | return 0; | ||
2022 | } | ||
2023 | if (cmd == OSS_GETVERSION) | ||
2024 | return put_user(SOUND_VERSION, user_arg); | ||
2025 | if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int)) | ||
2026 | return -EINVAL; | ||
2027 | if (_IOC_DIR(cmd) == _IOC_READ) { | ||
2028 | switch (_IOC_NR(cmd)) { | ||
2029 | case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */ | ||
2030 | val = get_rec_src(ms); | ||
2031 | if (val < 0) | ||
2032 | return val; | ||
2033 | return put_user(val, user_arg); | ||
2034 | |||
2035 | case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */ | ||
2036 | for (val = i = 0; i < ms->numch; i++) | ||
2037 | val |= 1 << ms->ch[i].osschannel; | ||
2038 | return put_user(val, user_arg); | ||
2039 | |||
2040 | case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */ | ||
2041 | for (val = i = 0; i < ms->numch; i++) | ||
2042 | if (ms->ch[i].slctunitid) | ||
2043 | val |= 1 << ms->ch[i].osschannel; | ||
2044 | return put_user(val, user_arg); | ||
2045 | |||
2046 | case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */ | ||
2047 | for (val = i = 0; i < ms->numch; i++) | ||
2048 | if (ms->ch[i].flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) | ||
2049 | val |= 1 << ms->ch[i].osschannel; | ||
2050 | return put_user(val, user_arg); | ||
2051 | |||
2052 | case SOUND_MIXER_CAPS: | ||
2053 | return put_user(SOUND_CAP_EXCL_INPUT, user_arg); | ||
2054 | |||
2055 | default: | ||
2056 | i = _IOC_NR(cmd); | ||
2057 | if (i >= SOUND_MIXER_NRDEVICES) | ||
2058 | return -EINVAL; | ||
2059 | for (j = 0; j < ms->numch; j++) { | ||
2060 | if (ms->ch[j].osschannel == i) { | ||
2061 | return put_user(ms->ch[j].value, user_arg); | ||
2062 | } | ||
2063 | } | ||
2064 | return -EINVAL; | ||
2065 | } | ||
2066 | } | ||
2067 | if (_IOC_DIR(cmd) != (_IOC_READ|_IOC_WRITE)) | ||
2068 | return -EINVAL; | ||
2069 | ms->modcnt++; | ||
2070 | switch (_IOC_NR(cmd)) { | ||
2071 | case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */ | ||
2072 | if (get_user(val, user_arg)) | ||
2073 | return -EFAULT; | ||
2074 | return set_rec_src(ms, val); | ||
2075 | |||
2076 | default: | ||
2077 | i = _IOC_NR(cmd); | ||
2078 | if (i >= SOUND_MIXER_NRDEVICES) | ||
2079 | return -EINVAL; | ||
2080 | for (j = 0; j < ms->numch && ms->ch[j].osschannel != i; j++); | ||
2081 | if (j >= ms->numch) | ||
2082 | return -EINVAL; | ||
2083 | if (get_user(val, user_arg)) | ||
2084 | return -EFAULT; | ||
2085 | if (wrmixer(ms, j, val)) | ||
2086 | return -EIO; | ||
2087 | return put_user(ms->ch[j].value, user_arg); | ||
2088 | } | ||
2089 | } | ||
2090 | |||
2091 | static /*const*/ struct file_operations usb_mixer_fops = { | ||
2092 | .owner = THIS_MODULE, | ||
2093 | .llseek = no_llseek, | ||
2094 | .ioctl = usb_audio_ioctl_mixdev, | ||
2095 | .open = usb_audio_open_mixdev, | ||
2096 | .release = usb_audio_release_mixdev, | ||
2097 | }; | ||
2098 | |||
2099 | /* --------------------------------------------------------------------- */ | ||
2100 | |||
2101 | static int drain_out(struct usb_audiodev *as, int nonblock) | ||
2102 | { | ||
2103 | DECLARE_WAITQUEUE(wait, current); | ||
2104 | unsigned long flags; | ||
2105 | int count, tmo; | ||
2106 | |||
2107 | if (as->usbout.dma.mapped || !as->usbout.dma.ready) | ||
2108 | return 0; | ||
2109 | usbout_start(as); | ||
2110 | add_wait_queue(&as->usbout.dma.wait, &wait); | ||
2111 | for (;;) { | ||
2112 | __set_current_state(TASK_INTERRUPTIBLE); | ||
2113 | spin_lock_irqsave(&as->lock, flags); | ||
2114 | count = as->usbout.dma.count; | ||
2115 | spin_unlock_irqrestore(&as->lock, flags); | ||
2116 | if (count <= 0) | ||
2117 | break; | ||
2118 | if (signal_pending(current)) | ||
2119 | break; | ||
2120 | if (nonblock) { | ||
2121 | remove_wait_queue(&as->usbout.dma.wait, &wait); | ||
2122 | set_current_state(TASK_RUNNING); | ||
2123 | return -EBUSY; | ||
2124 | } | ||
2125 | tmo = 3 * HZ * count / as->usbout.dma.srate; | ||
2126 | tmo >>= AFMT_BYTESSHIFT(as->usbout.dma.format); | ||
2127 | if (!schedule_timeout(tmo + 1)) { | ||
2128 | printk(KERN_DEBUG "usbaudio: dma timed out??\n"); | ||
2129 | break; | ||
2130 | } | ||
2131 | } | ||
2132 | remove_wait_queue(&as->usbout.dma.wait, &wait); | ||
2133 | set_current_state(TASK_RUNNING); | ||
2134 | if (signal_pending(current)) | ||
2135 | return -ERESTARTSYS; | ||
2136 | return 0; | ||
2137 | } | ||
2138 | |||
2139 | /* --------------------------------------------------------------------- */ | ||
2140 | |||
2141 | static ssize_t usb_audio_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) | ||
2142 | { | ||
2143 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2144 | DECLARE_WAITQUEUE(wait, current); | ||
2145 | ssize_t ret = 0; | ||
2146 | unsigned long flags; | ||
2147 | unsigned int ptr; | ||
2148 | int cnt, err; | ||
2149 | |||
2150 | if (as->usbin.dma.mapped) | ||
2151 | return -ENXIO; | ||
2152 | if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as))) | ||
2153 | return ret; | ||
2154 | if (!access_ok(VERIFY_WRITE, buffer, count)) | ||
2155 | return -EFAULT; | ||
2156 | add_wait_queue(&as->usbin.dma.wait, &wait); | ||
2157 | while (count > 0) { | ||
2158 | spin_lock_irqsave(&as->lock, flags); | ||
2159 | ptr = as->usbin.dma.rdptr; | ||
2160 | cnt = as->usbin.dma.count; | ||
2161 | /* set task state early to avoid wakeup races */ | ||
2162 | if (cnt <= 0) | ||
2163 | __set_current_state(TASK_INTERRUPTIBLE); | ||
2164 | spin_unlock_irqrestore(&as->lock, flags); | ||
2165 | if (cnt > count) | ||
2166 | cnt = count; | ||
2167 | if (cnt <= 0) { | ||
2168 | if (usbin_start(as)) { | ||
2169 | if (!ret) | ||
2170 | ret = -ENODEV; | ||
2171 | break; | ||
2172 | } | ||
2173 | if (file->f_flags & O_NONBLOCK) { | ||
2174 | if (!ret) | ||
2175 | ret = -EAGAIN; | ||
2176 | break; | ||
2177 | } | ||
2178 | schedule(); | ||
2179 | if (signal_pending(current)) { | ||
2180 | if (!ret) | ||
2181 | ret = -ERESTARTSYS; | ||
2182 | break; | ||
2183 | } | ||
2184 | continue; | ||
2185 | } | ||
2186 | if ((err = dmabuf_copyout_user(&as->usbin.dma, ptr, buffer, cnt))) { | ||
2187 | if (!ret) | ||
2188 | ret = err; | ||
2189 | break; | ||
2190 | } | ||
2191 | ptr += cnt; | ||
2192 | if (ptr >= as->usbin.dma.dmasize) | ||
2193 | ptr -= as->usbin.dma.dmasize; | ||
2194 | spin_lock_irqsave(&as->lock, flags); | ||
2195 | as->usbin.dma.rdptr = ptr; | ||
2196 | as->usbin.dma.count -= cnt; | ||
2197 | spin_unlock_irqrestore(&as->lock, flags); | ||
2198 | count -= cnt; | ||
2199 | buffer += cnt; | ||
2200 | ret += cnt; | ||
2201 | } | ||
2202 | __set_current_state(TASK_RUNNING); | ||
2203 | remove_wait_queue(&as->usbin.dma.wait, &wait); | ||
2204 | return ret; | ||
2205 | } | ||
2206 | |||
2207 | static ssize_t usb_audio_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | ||
2208 | { | ||
2209 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2210 | DECLARE_WAITQUEUE(wait, current); | ||
2211 | ssize_t ret = 0; | ||
2212 | unsigned long flags; | ||
2213 | unsigned int ptr; | ||
2214 | unsigned int start_thr; | ||
2215 | int cnt, err; | ||
2216 | |||
2217 | if (as->usbout.dma.mapped) | ||
2218 | return -ENXIO; | ||
2219 | if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as))) | ||
2220 | return ret; | ||
2221 | if (!access_ok(VERIFY_READ, buffer, count)) | ||
2222 | return -EFAULT; | ||
2223 | start_thr = (as->usbout.dma.srate << AFMT_BYTESSHIFT(as->usbout.dma.format)) / (1000 / (3 * DESCFRAMES)); | ||
2224 | add_wait_queue(&as->usbout.dma.wait, &wait); | ||
2225 | while (count > 0) { | ||
2226 | #if 0 | ||
2227 | 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", | ||
2228 | count, as->usbout.dma.count, as->usbout.dma.rdptr, as->usbout.dma.wrptr, as->usbout.dma.dmasize, as->usbout.dma.fragsize, | ||
2229 | as->usbout.flags, current->state); | ||
2230 | #endif | ||
2231 | spin_lock_irqsave(&as->lock, flags); | ||
2232 | if (as->usbout.dma.count < 0) { | ||
2233 | as->usbout.dma.count = 0; | ||
2234 | as->usbout.dma.rdptr = as->usbout.dma.wrptr; | ||
2235 | } | ||
2236 | ptr = as->usbout.dma.wrptr; | ||
2237 | cnt = as->usbout.dma.dmasize - as->usbout.dma.count; | ||
2238 | /* set task state early to avoid wakeup races */ | ||
2239 | if (cnt <= 0) | ||
2240 | __set_current_state(TASK_INTERRUPTIBLE); | ||
2241 | spin_unlock_irqrestore(&as->lock, flags); | ||
2242 | if (cnt > count) | ||
2243 | cnt = count; | ||
2244 | if (cnt <= 0) { | ||
2245 | if (usbout_start(as)) { | ||
2246 | if (!ret) | ||
2247 | ret = -ENODEV; | ||
2248 | break; | ||
2249 | } | ||
2250 | if (file->f_flags & O_NONBLOCK) { | ||
2251 | if (!ret) | ||
2252 | ret = -EAGAIN; | ||
2253 | break; | ||
2254 | } | ||
2255 | schedule(); | ||
2256 | if (signal_pending(current)) { | ||
2257 | if (!ret) | ||
2258 | ret = -ERESTARTSYS; | ||
2259 | break; | ||
2260 | } | ||
2261 | continue; | ||
2262 | } | ||
2263 | if ((err = dmabuf_copyin_user(&as->usbout.dma, ptr, buffer, cnt))) { | ||
2264 | if (!ret) | ||
2265 | ret = err; | ||
2266 | break; | ||
2267 | } | ||
2268 | ptr += cnt; | ||
2269 | if (ptr >= as->usbout.dma.dmasize) | ||
2270 | ptr -= as->usbout.dma.dmasize; | ||
2271 | spin_lock_irqsave(&as->lock, flags); | ||
2272 | as->usbout.dma.wrptr = ptr; | ||
2273 | as->usbout.dma.count += cnt; | ||
2274 | spin_unlock_irqrestore(&as->lock, flags); | ||
2275 | count -= cnt; | ||
2276 | buffer += cnt; | ||
2277 | ret += cnt; | ||
2278 | if (as->usbout.dma.count >= start_thr && usbout_start(as)) { | ||
2279 | if (!ret) | ||
2280 | ret = -ENODEV; | ||
2281 | break; | ||
2282 | } | ||
2283 | } | ||
2284 | __set_current_state(TASK_RUNNING); | ||
2285 | remove_wait_queue(&as->usbout.dma.wait, &wait); | ||
2286 | return ret; | ||
2287 | } | ||
2288 | |||
2289 | /* Called without the kernel lock - fine */ | ||
2290 | static unsigned int usb_audio_poll(struct file *file, struct poll_table_struct *wait) | ||
2291 | { | ||
2292 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2293 | unsigned long flags; | ||
2294 | unsigned int mask = 0; | ||
2295 | |||
2296 | if (file->f_mode & FMODE_WRITE) { | ||
2297 | if (!as->usbout.dma.ready) | ||
2298 | prog_dmabuf_out(as); | ||
2299 | poll_wait(file, &as->usbout.dma.wait, wait); | ||
2300 | } | ||
2301 | if (file->f_mode & FMODE_READ) { | ||
2302 | if (!as->usbin.dma.ready) | ||
2303 | prog_dmabuf_in(as); | ||
2304 | poll_wait(file, &as->usbin.dma.wait, wait); | ||
2305 | } | ||
2306 | spin_lock_irqsave(&as->lock, flags); | ||
2307 | if (file->f_mode & FMODE_READ) { | ||
2308 | if (as->usbin.dma.count >= (signed)as->usbin.dma.fragsize) | ||
2309 | mask |= POLLIN | POLLRDNORM; | ||
2310 | } | ||
2311 | if (file->f_mode & FMODE_WRITE) { | ||
2312 | if (as->usbout.dma.mapped) { | ||
2313 | if (as->usbout.dma.count >= (signed)as->usbout.dma.fragsize) | ||
2314 | mask |= POLLOUT | POLLWRNORM; | ||
2315 | } else { | ||
2316 | if ((signed)as->usbout.dma.dmasize >= as->usbout.dma.count + (signed)as->usbout.dma.fragsize) | ||
2317 | mask |= POLLOUT | POLLWRNORM; | ||
2318 | } | ||
2319 | } | ||
2320 | spin_unlock_irqrestore(&as->lock, flags); | ||
2321 | return mask; | ||
2322 | } | ||
2323 | |||
2324 | static int usb_audio_mmap(struct file *file, struct vm_area_struct *vma) | ||
2325 | { | ||
2326 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2327 | struct dmabuf *db; | ||
2328 | int ret = -EINVAL; | ||
2329 | |||
2330 | lock_kernel(); | ||
2331 | if (vma->vm_flags & VM_WRITE) { | ||
2332 | if ((ret = prog_dmabuf_out(as)) != 0) | ||
2333 | goto out; | ||
2334 | db = &as->usbout.dma; | ||
2335 | } else if (vma->vm_flags & VM_READ) { | ||
2336 | if ((ret = prog_dmabuf_in(as)) != 0) | ||
2337 | goto out; | ||
2338 | db = &as->usbin.dma; | ||
2339 | } else | ||
2340 | goto out; | ||
2341 | |||
2342 | ret = -EINVAL; | ||
2343 | if (vma->vm_pgoff != 0) | ||
2344 | goto out; | ||
2345 | |||
2346 | ret = dmabuf_mmap(vma, db, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot); | ||
2347 | out: | ||
2348 | unlock_kernel(); | ||
2349 | return ret; | ||
2350 | } | ||
2351 | |||
2352 | static int usb_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | ||
2353 | { | ||
2354 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2355 | struct usb_audio_state *s = as->state; | ||
2356 | int __user *user_arg = (int __user *)arg; | ||
2357 | unsigned long flags; | ||
2358 | audio_buf_info abinfo; | ||
2359 | count_info cinfo; | ||
2360 | int val = 0; | ||
2361 | int val2, mapped, ret; | ||
2362 | |||
2363 | if (!s->usbdev) | ||
2364 | return -EIO; | ||
2365 | mapped = ((file->f_mode & FMODE_WRITE) && as->usbout.dma.mapped) || | ||
2366 | ((file->f_mode & FMODE_READ) && as->usbin.dma.mapped); | ||
2367 | #if 0 | ||
2368 | if (arg) | ||
2369 | get_user(val, (int *)arg); | ||
2370 | printk(KERN_DEBUG "usbaudio: usb_audio_ioctl cmd=%x arg=%lx *arg=%d\n", cmd, arg, val) | ||
2371 | #endif | ||
2372 | switch (cmd) { | ||
2373 | case OSS_GETVERSION: | ||
2374 | return put_user(SOUND_VERSION, user_arg); | ||
2375 | |||
2376 | case SNDCTL_DSP_SYNC: | ||
2377 | if (file->f_mode & FMODE_WRITE) | ||
2378 | return drain_out(as, 0/*file->f_flags & O_NONBLOCK*/); | ||
2379 | return 0; | ||
2380 | |||
2381 | case SNDCTL_DSP_SETDUPLEX: | ||
2382 | return 0; | ||
2383 | |||
2384 | case SNDCTL_DSP_GETCAPS: | ||
2385 | return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | | ||
2386 | DSP_CAP_MMAP | DSP_CAP_BATCH, user_arg); | ||
2387 | |||
2388 | case SNDCTL_DSP_RESET: | ||
2389 | if (file->f_mode & FMODE_WRITE) { | ||
2390 | usbout_stop(as); | ||
2391 | as->usbout.dma.rdptr = as->usbout.dma.wrptr = as->usbout.dma.count = as->usbout.dma.total_bytes = 0; | ||
2392 | } | ||
2393 | if (file->f_mode & FMODE_READ) { | ||
2394 | usbin_stop(as); | ||
2395 | as->usbin.dma.rdptr = as->usbin.dma.wrptr = as->usbin.dma.count = as->usbin.dma.total_bytes = 0; | ||
2396 | } | ||
2397 | return 0; | ||
2398 | |||
2399 | case SNDCTL_DSP_SPEED: | ||
2400 | if (get_user(val, user_arg)) | ||
2401 | return -EFAULT; | ||
2402 | if (val >= 0) { | ||
2403 | if (val < 4000) | ||
2404 | val = 4000; | ||
2405 | if (val > 100000) | ||
2406 | val = 100000; | ||
2407 | if (set_format(as, file->f_mode, AFMT_QUERY, val)) | ||
2408 | return -EIO; | ||
2409 | } | ||
2410 | return put_user((file->f_mode & FMODE_READ) ? | ||
2411 | as->usbin.dma.srate : as->usbout.dma.srate, | ||
2412 | user_arg); | ||
2413 | |||
2414 | case SNDCTL_DSP_STEREO: | ||
2415 | if (get_user(val, user_arg)) | ||
2416 | return -EFAULT; | ||
2417 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2418 | if (val) | ||
2419 | val2 |= AFMT_STEREO; | ||
2420 | else | ||
2421 | val2 &= ~AFMT_STEREO; | ||
2422 | if (set_format(as, file->f_mode, val2, 0)) | ||
2423 | return -EIO; | ||
2424 | return 0; | ||
2425 | |||
2426 | case SNDCTL_DSP_CHANNELS: | ||
2427 | if (get_user(val, user_arg)) | ||
2428 | return -EFAULT; | ||
2429 | if (val != 0) { | ||
2430 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2431 | if (val == 1) | ||
2432 | val2 &= ~AFMT_STEREO; | ||
2433 | else | ||
2434 | val2 |= AFMT_STEREO; | ||
2435 | if (set_format(as, file->f_mode, val2, 0)) | ||
2436 | return -EIO; | ||
2437 | } | ||
2438 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2439 | return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg); | ||
2440 | |||
2441 | case SNDCTL_DSP_GETFMTS: /* Returns a mask */ | ||
2442 | return put_user(AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE | | ||
2443 | AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE, user_arg); | ||
2444 | |||
2445 | case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/ | ||
2446 | if (get_user(val, user_arg)) | ||
2447 | return -EFAULT; | ||
2448 | if (val != AFMT_QUERY) { | ||
2449 | if (hweight32(val) != 1) | ||
2450 | return -EINVAL; | ||
2451 | if (!(val & (AFMT_U8 | AFMT_U16_LE | AFMT_U16_BE | | ||
2452 | AFMT_S8 | AFMT_S16_LE | AFMT_S16_BE))) | ||
2453 | return -EINVAL; | ||
2454 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2455 | val |= val2 & AFMT_STEREO; | ||
2456 | if (set_format(as, file->f_mode, val, 0)) | ||
2457 | return -EIO; | ||
2458 | } | ||
2459 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2460 | return put_user(val2 & ~AFMT_STEREO, user_arg); | ||
2461 | |||
2462 | case SNDCTL_DSP_POST: | ||
2463 | return 0; | ||
2464 | |||
2465 | case SNDCTL_DSP_GETTRIGGER: | ||
2466 | val = 0; | ||
2467 | if (file->f_mode & FMODE_READ && as->usbin.flags & FLG_RUNNING) | ||
2468 | val |= PCM_ENABLE_INPUT; | ||
2469 | if (file->f_mode & FMODE_WRITE && as->usbout.flags & FLG_RUNNING) | ||
2470 | val |= PCM_ENABLE_OUTPUT; | ||
2471 | return put_user(val, user_arg); | ||
2472 | |||
2473 | case SNDCTL_DSP_SETTRIGGER: | ||
2474 | if (get_user(val, user_arg)) | ||
2475 | return -EFAULT; | ||
2476 | if (file->f_mode & FMODE_READ) { | ||
2477 | if (val & PCM_ENABLE_INPUT) { | ||
2478 | if (!as->usbin.dma.ready && (ret = prog_dmabuf_in(as))) | ||
2479 | return ret; | ||
2480 | if (usbin_start(as)) | ||
2481 | return -ENODEV; | ||
2482 | } else | ||
2483 | usbin_stop(as); | ||
2484 | } | ||
2485 | if (file->f_mode & FMODE_WRITE) { | ||
2486 | if (val & PCM_ENABLE_OUTPUT) { | ||
2487 | if (!as->usbout.dma.ready && (ret = prog_dmabuf_out(as))) | ||
2488 | return ret; | ||
2489 | if (usbout_start(as)) | ||
2490 | return -ENODEV; | ||
2491 | } else | ||
2492 | usbout_stop(as); | ||
2493 | } | ||
2494 | return 0; | ||
2495 | |||
2496 | case SNDCTL_DSP_GETOSPACE: | ||
2497 | if (!(file->f_mode & FMODE_WRITE)) | ||
2498 | return -EINVAL; | ||
2499 | if (!(as->usbout.flags & FLG_RUNNING) && (val = prog_dmabuf_out(as)) != 0) | ||
2500 | return val; | ||
2501 | spin_lock_irqsave(&as->lock, flags); | ||
2502 | abinfo.fragsize = as->usbout.dma.fragsize; | ||
2503 | abinfo.bytes = as->usbout.dma.dmasize - as->usbout.dma.count; | ||
2504 | abinfo.fragstotal = as->usbout.dma.numfrag; | ||
2505 | abinfo.fragments = abinfo.bytes >> as->usbout.dma.fragshift; | ||
2506 | spin_unlock_irqrestore(&as->lock, flags); | ||
2507 | return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0; | ||
2508 | |||
2509 | case SNDCTL_DSP_GETISPACE: | ||
2510 | if (!(file->f_mode & FMODE_READ)) | ||
2511 | return -EINVAL; | ||
2512 | if (!(as->usbin.flags & FLG_RUNNING) && (val = prog_dmabuf_in(as)) != 0) | ||
2513 | return val; | ||
2514 | spin_lock_irqsave(&as->lock, flags); | ||
2515 | abinfo.fragsize = as->usbin.dma.fragsize; | ||
2516 | abinfo.bytes = as->usbin.dma.count; | ||
2517 | abinfo.fragstotal = as->usbin.dma.numfrag; | ||
2518 | abinfo.fragments = abinfo.bytes >> as->usbin.dma.fragshift; | ||
2519 | spin_unlock_irqrestore(&as->lock, flags); | ||
2520 | return copy_to_user((void __user *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0; | ||
2521 | |||
2522 | case SNDCTL_DSP_NONBLOCK: | ||
2523 | file->f_flags |= O_NONBLOCK; | ||
2524 | return 0; | ||
2525 | |||
2526 | case SNDCTL_DSP_GETODELAY: | ||
2527 | if (!(file->f_mode & FMODE_WRITE)) | ||
2528 | return -EINVAL; | ||
2529 | spin_lock_irqsave(&as->lock, flags); | ||
2530 | val = as->usbout.dma.count; | ||
2531 | spin_unlock_irqrestore(&as->lock, flags); | ||
2532 | return put_user(val, user_arg); | ||
2533 | |||
2534 | case SNDCTL_DSP_GETIPTR: | ||
2535 | if (!(file->f_mode & FMODE_READ)) | ||
2536 | return -EINVAL; | ||
2537 | spin_lock_irqsave(&as->lock, flags); | ||
2538 | cinfo.bytes = as->usbin.dma.total_bytes; | ||
2539 | cinfo.blocks = as->usbin.dma.count >> as->usbin.dma.fragshift; | ||
2540 | cinfo.ptr = as->usbin.dma.wrptr; | ||
2541 | if (as->usbin.dma.mapped) | ||
2542 | as->usbin.dma.count &= as->usbin.dma.fragsize-1; | ||
2543 | spin_unlock_irqrestore(&as->lock, flags); | ||
2544 | if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo))) | ||
2545 | return -EFAULT; | ||
2546 | return 0; | ||
2547 | |||
2548 | case SNDCTL_DSP_GETOPTR: | ||
2549 | if (!(file->f_mode & FMODE_WRITE)) | ||
2550 | return -EINVAL; | ||
2551 | spin_lock_irqsave(&as->lock, flags); | ||
2552 | cinfo.bytes = as->usbout.dma.total_bytes; | ||
2553 | cinfo.blocks = as->usbout.dma.count >> as->usbout.dma.fragshift; | ||
2554 | cinfo.ptr = as->usbout.dma.rdptr; | ||
2555 | if (as->usbout.dma.mapped) | ||
2556 | as->usbout.dma.count &= as->usbout.dma.fragsize-1; | ||
2557 | spin_unlock_irqrestore(&as->lock, flags); | ||
2558 | if (copy_to_user((void __user *)arg, &cinfo, sizeof(cinfo))) | ||
2559 | return -EFAULT; | ||
2560 | return 0; | ||
2561 | |||
2562 | case SNDCTL_DSP_GETBLKSIZE: | ||
2563 | if (file->f_mode & FMODE_WRITE) { | ||
2564 | if ((val = prog_dmabuf_out(as))) | ||
2565 | return val; | ||
2566 | return put_user(as->usbout.dma.fragsize, user_arg); | ||
2567 | } | ||
2568 | if ((val = prog_dmabuf_in(as))) | ||
2569 | return val; | ||
2570 | return put_user(as->usbin.dma.fragsize, user_arg); | ||
2571 | |||
2572 | case SNDCTL_DSP_SETFRAGMENT: | ||
2573 | if (get_user(val, user_arg)) | ||
2574 | return -EFAULT; | ||
2575 | if (file->f_mode & FMODE_READ) { | ||
2576 | as->usbin.dma.ossfragshift = val & 0xffff; | ||
2577 | as->usbin.dma.ossmaxfrags = (val >> 16) & 0xffff; | ||
2578 | if (as->usbin.dma.ossfragshift < 4) | ||
2579 | as->usbin.dma.ossfragshift = 4; | ||
2580 | if (as->usbin.dma.ossfragshift > 15) | ||
2581 | as->usbin.dma.ossfragshift = 15; | ||
2582 | if (as->usbin.dma.ossmaxfrags < 4) | ||
2583 | as->usbin.dma.ossmaxfrags = 4; | ||
2584 | } | ||
2585 | if (file->f_mode & FMODE_WRITE) { | ||
2586 | as->usbout.dma.ossfragshift = val & 0xffff; | ||
2587 | as->usbout.dma.ossmaxfrags = (val >> 16) & 0xffff; | ||
2588 | if (as->usbout.dma.ossfragshift < 4) | ||
2589 | as->usbout.dma.ossfragshift = 4; | ||
2590 | if (as->usbout.dma.ossfragshift > 15) | ||
2591 | as->usbout.dma.ossfragshift = 15; | ||
2592 | if (as->usbout.dma.ossmaxfrags < 4) | ||
2593 | as->usbout.dma.ossmaxfrags = 4; | ||
2594 | } | ||
2595 | return 0; | ||
2596 | |||
2597 | case SNDCTL_DSP_SUBDIVIDE: | ||
2598 | if ((file->f_mode & FMODE_READ && as->usbin.dma.subdivision) || | ||
2599 | (file->f_mode & FMODE_WRITE && as->usbout.dma.subdivision)) | ||
2600 | return -EINVAL; | ||
2601 | if (get_user(val, user_arg)) | ||
2602 | return -EFAULT; | ||
2603 | if (val != 1 && val != 2 && val != 4) | ||
2604 | return -EINVAL; | ||
2605 | if (file->f_mode & FMODE_READ) | ||
2606 | as->usbin.dma.subdivision = val; | ||
2607 | if (file->f_mode & FMODE_WRITE) | ||
2608 | as->usbout.dma.subdivision = val; | ||
2609 | return 0; | ||
2610 | |||
2611 | case SOUND_PCM_READ_RATE: | ||
2612 | return put_user((file->f_mode & FMODE_READ) ? | ||
2613 | as->usbin.dma.srate : as->usbout.dma.srate, | ||
2614 | user_arg); | ||
2615 | |||
2616 | case SOUND_PCM_READ_CHANNELS: | ||
2617 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2618 | return put_user(AFMT_ISSTEREO(val2) ? 2 : 1, user_arg); | ||
2619 | |||
2620 | case SOUND_PCM_READ_BITS: | ||
2621 | val2 = (file->f_mode & FMODE_READ) ? as->usbin.dma.format : as->usbout.dma.format; | ||
2622 | return put_user(AFMT_IS16BIT(val2) ? 16 : 8, user_arg); | ||
2623 | |||
2624 | case SOUND_PCM_WRITE_FILTER: | ||
2625 | case SNDCTL_DSP_SETSYNCRO: | ||
2626 | case SOUND_PCM_READ_FILTER: | ||
2627 | return -EINVAL; | ||
2628 | } | ||
2629 | dprintk((KERN_DEBUG "usbaudio: usb_audio_ioctl - no command found\n")); | ||
2630 | return -ENOIOCTLCMD; | ||
2631 | } | ||
2632 | |||
2633 | static int usb_audio_open(struct inode *inode, struct file *file) | ||
2634 | { | ||
2635 | unsigned int minor = iminor(inode); | ||
2636 | DECLARE_WAITQUEUE(wait, current); | ||
2637 | struct usb_audiodev *as; | ||
2638 | struct usb_audio_state *s; | ||
2639 | |||
2640 | for (;;) { | ||
2641 | down(&open_sem); | ||
2642 | list_for_each_entry(s, &audiodevs, audiodev) { | ||
2643 | list_for_each_entry(as, &s->audiolist, list) { | ||
2644 | if (!((as->dev_audio ^ minor) & ~0xf)) | ||
2645 | goto device_found; | ||
2646 | } | ||
2647 | } | ||
2648 | up(&open_sem); | ||
2649 | return -ENODEV; | ||
2650 | |||
2651 | device_found: | ||
2652 | if (!s->usbdev) { | ||
2653 | up(&open_sem); | ||
2654 | return -EIO; | ||
2655 | } | ||
2656 | /* wait for device to become free */ | ||
2657 | if (!(as->open_mode & file->f_mode)) | ||
2658 | break; | ||
2659 | if (file->f_flags & O_NONBLOCK) { | ||
2660 | up(&open_sem); | ||
2661 | return -EBUSY; | ||
2662 | } | ||
2663 | __set_current_state(TASK_INTERRUPTIBLE); | ||
2664 | add_wait_queue(&open_wait, &wait); | ||
2665 | up(&open_sem); | ||
2666 | schedule(); | ||
2667 | __set_current_state(TASK_RUNNING); | ||
2668 | remove_wait_queue(&open_wait, &wait); | ||
2669 | if (signal_pending(current)) | ||
2670 | return -ERESTARTSYS; | ||
2671 | } | ||
2672 | if (file->f_mode & FMODE_READ) | ||
2673 | as->usbin.dma.ossfragshift = as->usbin.dma.ossmaxfrags = as->usbin.dma.subdivision = 0; | ||
2674 | if (file->f_mode & FMODE_WRITE) | ||
2675 | as->usbout.dma.ossfragshift = as->usbout.dma.ossmaxfrags = as->usbout.dma.subdivision = 0; | ||
2676 | if (set_format(as, file->f_mode, ((minor & 0xf) == SND_DEV_DSP16) ? AFMT_S16_LE : AFMT_U8 /* AFMT_ULAW */, 8000)) { | ||
2677 | up(&open_sem); | ||
2678 | return -EIO; | ||
2679 | } | ||
2680 | file->private_data = as; | ||
2681 | as->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE); | ||
2682 | s->count++; | ||
2683 | up(&open_sem); | ||
2684 | return nonseekable_open(inode, file); | ||
2685 | } | ||
2686 | |||
2687 | static int usb_audio_release(struct inode *inode, struct file *file) | ||
2688 | { | ||
2689 | struct usb_audiodev *as = (struct usb_audiodev *)file->private_data; | ||
2690 | struct usb_audio_state *s; | ||
2691 | struct usb_device *dev; | ||
2692 | |||
2693 | lock_kernel(); | ||
2694 | s = as->state; | ||
2695 | dev = s->usbdev; | ||
2696 | if (file->f_mode & FMODE_WRITE) | ||
2697 | drain_out(as, file->f_flags & O_NONBLOCK); | ||
2698 | down(&open_sem); | ||
2699 | if (file->f_mode & FMODE_WRITE) { | ||
2700 | usbout_stop(as); | ||
2701 | if (dev && as->usbout.interface >= 0) | ||
2702 | usb_set_interface(dev, as->usbout.interface, 0); | ||
2703 | dmabuf_release(&as->usbout.dma); | ||
2704 | usbout_release(as); | ||
2705 | } | ||
2706 | if (file->f_mode & FMODE_READ) { | ||
2707 | usbin_stop(as); | ||
2708 | if (dev && as->usbin.interface >= 0) | ||
2709 | usb_set_interface(dev, as->usbin.interface, 0); | ||
2710 | dmabuf_release(&as->usbin.dma); | ||
2711 | usbin_release(as); | ||
2712 | } | ||
2713 | as->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE); | ||
2714 | release(s); | ||
2715 | wake_up(&open_wait); | ||
2716 | unlock_kernel(); | ||
2717 | return 0; | ||
2718 | } | ||
2719 | |||
2720 | static /*const*/ struct file_operations usb_audio_fops = { | ||
2721 | .owner = THIS_MODULE, | ||
2722 | .llseek = no_llseek, | ||
2723 | .read = usb_audio_read, | ||
2724 | .write = usb_audio_write, | ||
2725 | .poll = usb_audio_poll, | ||
2726 | .ioctl = usb_audio_ioctl, | ||
2727 | .mmap = usb_audio_mmap, | ||
2728 | .open = usb_audio_open, | ||
2729 | .release = usb_audio_release, | ||
2730 | }; | ||
2731 | |||
2732 | /* --------------------------------------------------------------------- */ | ||
2733 | |||
2734 | static int usb_audio_probe(struct usb_interface *iface, | ||
2735 | const struct usb_device_id *id); | ||
2736 | static void usb_audio_disconnect(struct usb_interface *iface); | ||
2737 | |||
2738 | static struct usb_device_id usb_audio_ids [] = { | ||
2739 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), | ||
2740 | .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = 1}, | ||
2741 | { } /* Terminating entry */ | ||
2742 | }; | ||
2743 | |||
2744 | MODULE_DEVICE_TABLE (usb, usb_audio_ids); | ||
2745 | |||
2746 | static struct usb_driver usb_audio_driver = { | ||
2747 | .owner = THIS_MODULE, | ||
2748 | .name = "audio", | ||
2749 | .probe = usb_audio_probe, | ||
2750 | .disconnect = usb_audio_disconnect, | ||
2751 | .id_table = usb_audio_ids, | ||
2752 | }; | ||
2753 | |||
2754 | static void *find_descriptor(void *descstart, unsigned int desclen, void *after, | ||
2755 | u8 dtype, int iface, int altsetting) | ||
2756 | { | ||
2757 | u8 *p, *end, *next; | ||
2758 | int ifc = -1, as = -1; | ||
2759 | |||
2760 | p = descstart; | ||
2761 | end = p + desclen; | ||
2762 | for (; p < end;) { | ||
2763 | if (p[0] < 2) | ||
2764 | return NULL; | ||
2765 | next = p + p[0]; | ||
2766 | if (next > end) | ||
2767 | return NULL; | ||
2768 | if (p[1] == USB_DT_INTERFACE) { | ||
2769 | /* minimum length of interface descriptor */ | ||
2770 | if (p[0] < 9) | ||
2771 | return NULL; | ||
2772 | ifc = p[2]; | ||
2773 | as = p[3]; | ||
2774 | } | ||
2775 | if (p[1] == dtype && (!after || (void *)p > after) && | ||
2776 | (iface == -1 || iface == ifc) && (altsetting == -1 || altsetting == as)) { | ||
2777 | return p; | ||
2778 | } | ||
2779 | p = next; | ||
2780 | } | ||
2781 | return NULL; | ||
2782 | } | ||
2783 | |||
2784 | static void *find_csinterface_descriptor(void *descstart, unsigned int desclen, void *after, u8 dsubtype, int iface, int altsetting) | ||
2785 | { | ||
2786 | unsigned char *p; | ||
2787 | |||
2788 | p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, altsetting); | ||
2789 | while (p) { | ||
2790 | if (p[0] >= 3 && p[2] == dsubtype) | ||
2791 | return p; | ||
2792 | p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, altsetting); | ||
2793 | } | ||
2794 | return NULL; | ||
2795 | } | ||
2796 | |||
2797 | static void *find_audiocontrol_unit(void *descstart, unsigned int desclen, void *after, u8 unit, int iface) | ||
2798 | { | ||
2799 | unsigned char *p; | ||
2800 | |||
2801 | p = find_descriptor(descstart, desclen, after, USB_DT_CS_INTERFACE, iface, -1); | ||
2802 | while (p) { | ||
2803 | if (p[0] >= 4 && p[2] >= INPUT_TERMINAL && p[2] <= EXTENSION_UNIT && p[3] == unit) | ||
2804 | return p; | ||
2805 | p = find_descriptor(descstart, desclen, p, USB_DT_CS_INTERFACE, iface, -1); | ||
2806 | } | ||
2807 | return NULL; | ||
2808 | } | ||
2809 | |||
2810 | static void usb_audio_parsestreaming(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, int asifin, int asifout) | ||
2811 | { | ||
2812 | struct usb_device *dev = s->usbdev; | ||
2813 | struct usb_audiodev *as; | ||
2814 | struct usb_host_interface *alts; | ||
2815 | struct usb_interface *iface; | ||
2816 | struct audioformat *fp; | ||
2817 | unsigned char *fmt, *csep; | ||
2818 | unsigned int i, j, k, format, idx; | ||
2819 | |||
2820 | if (!(as = kmalloc(sizeof(struct usb_audiodev), GFP_KERNEL))) | ||
2821 | return; | ||
2822 | memset(as, 0, sizeof(struct usb_audiodev)); | ||
2823 | init_waitqueue_head(&as->usbin.dma.wait); | ||
2824 | init_waitqueue_head(&as->usbout.dma.wait); | ||
2825 | spin_lock_init(&as->lock); | ||
2826 | as->usbin.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); | ||
2827 | as->usbin.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); | ||
2828 | as->usbin.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); | ||
2829 | as->usbin.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); | ||
2830 | as->usbout.durb[0].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); | ||
2831 | as->usbout.durb[1].urb = usb_alloc_urb (DESCFRAMES, GFP_KERNEL); | ||
2832 | as->usbout.surb[0].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); | ||
2833 | as->usbout.surb[1].urb = usb_alloc_urb (SYNCFRAMES, GFP_KERNEL); | ||
2834 | if ((!as->usbin.durb[0].urb) || | ||
2835 | (!as->usbin.durb[1].urb) || | ||
2836 | (!as->usbin.surb[0].urb) || | ||
2837 | (!as->usbin.surb[1].urb) || | ||
2838 | (!as->usbout.durb[0].urb) || | ||
2839 | (!as->usbout.durb[1].urb) || | ||
2840 | (!as->usbout.surb[0].urb) || | ||
2841 | (!as->usbout.surb[1].urb)) { | ||
2842 | usb_free_urb(as->usbin.durb[0].urb); | ||
2843 | usb_free_urb(as->usbin.durb[1].urb); | ||
2844 | usb_free_urb(as->usbin.surb[0].urb); | ||
2845 | usb_free_urb(as->usbin.surb[1].urb); | ||
2846 | usb_free_urb(as->usbout.durb[0].urb); | ||
2847 | usb_free_urb(as->usbout.durb[1].urb); | ||
2848 | usb_free_urb(as->usbout.surb[0].urb); | ||
2849 | usb_free_urb(as->usbout.surb[1].urb); | ||
2850 | kfree(as); | ||
2851 | return; | ||
2852 | } | ||
2853 | as->state = s; | ||
2854 | as->usbin.interface = asifin; | ||
2855 | as->usbout.interface = asifout; | ||
2856 | /* search for input formats */ | ||
2857 | if (asifin >= 0) { | ||
2858 | as->usbin.flags = FLG_CONNECTED; | ||
2859 | iface = usb_ifnum_to_if(dev, asifin); | ||
2860 | for (idx = 0; idx < iface->num_altsetting; idx++) { | ||
2861 | alts = &iface->altsetting[idx]; | ||
2862 | i = alts->desc.bAlternateSetting; | ||
2863 | if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2) | ||
2864 | continue; | ||
2865 | if (alts->desc.bNumEndpoints < 1) { | ||
2866 | if (i != 0) { /* altsetting 0 has no endpoints (Section B.3.4.1) */ | ||
2867 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n", | ||
2868 | dev->devnum, asifin, i); | ||
2869 | } | ||
2870 | continue; | ||
2871 | } | ||
2872 | if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 || | ||
2873 | !(alts->endpoint[0].desc.bEndpointAddress & 0x80)) { | ||
2874 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous in\n", | ||
2875 | dev->devnum, asifin, i); | ||
2876 | continue; | ||
2877 | } | ||
2878 | fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifin, i); | ||
2879 | if (!fmt) { | ||
2880 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", | ||
2881 | dev->devnum, asifin, i); | ||
2882 | continue; | ||
2883 | } | ||
2884 | if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) { | ||
2885 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n", | ||
2886 | dev->devnum, asifin, i); | ||
2887 | continue; | ||
2888 | } | ||
2889 | format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8); | ||
2890 | fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifin, i); | ||
2891 | if (!fmt) { | ||
2892 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", | ||
2893 | dev->devnum, asifin, i); | ||
2894 | continue; | ||
2895 | } | ||
2896 | if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) { | ||
2897 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n", | ||
2898 | dev->devnum, asifin, i); | ||
2899 | continue; | ||
2900 | } | ||
2901 | if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) { | ||
2902 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n", | ||
2903 | dev->devnum, asifin, i, fmt[4], fmt[5]); | ||
2904 | continue; | ||
2905 | } | ||
2906 | csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifin, i); | ||
2907 | if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { | ||
2908 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n", | ||
2909 | dev->devnum, asifin, i); | ||
2910 | continue; | ||
2911 | } | ||
2912 | if (as->numfmtin >= MAXFORMATS) | ||
2913 | continue; | ||
2914 | fp = &as->fmtin[as->numfmtin++]; | ||
2915 | if (fmt[5] == 2) | ||
2916 | format &= (AFMT_U16_LE | AFMT_S16_LE); | ||
2917 | else | ||
2918 | format &= (AFMT_U8 | AFMT_S8); | ||
2919 | if (fmt[4] == 2) | ||
2920 | format |= AFMT_STEREO; | ||
2921 | fp->format = format; | ||
2922 | fp->altsetting = i; | ||
2923 | fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16); | ||
2924 | printk(KERN_INFO "usbaudio: valid input sample rate %u\n", fp->sratelo); | ||
2925 | for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) { | ||
2926 | k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16); | ||
2927 | printk(KERN_INFO "usbaudio: valid input sample rate %u\n", k); | ||
2928 | if (k > fp->sratehi) | ||
2929 | fp->sratehi = k; | ||
2930 | if (k < fp->sratelo) | ||
2931 | fp->sratelo = k; | ||
2932 | } | ||
2933 | fp->attributes = csep[3]; | ||
2934 | printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n", | ||
2935 | dev->devnum, asifin, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes); | ||
2936 | } | ||
2937 | } | ||
2938 | /* search for output formats */ | ||
2939 | if (asifout >= 0) { | ||
2940 | as->usbout.flags = FLG_CONNECTED; | ||
2941 | iface = usb_ifnum_to_if(dev, asifout); | ||
2942 | for (idx = 0; idx < iface->num_altsetting; idx++) { | ||
2943 | alts = &iface->altsetting[idx]; | ||
2944 | i = alts->desc.bAlternateSetting; | ||
2945 | if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || alts->desc.bInterfaceSubClass != 2) | ||
2946 | continue; | ||
2947 | if (alts->desc.bNumEndpoints < 1) { | ||
2948 | /* altsetting 0 should never have iso EPs */ | ||
2949 | if (i != 0) | ||
2950 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u does not have an endpoint\n", | ||
2951 | dev->devnum, asifout, i); | ||
2952 | continue; | ||
2953 | } | ||
2954 | if ((alts->endpoint[0].desc.bmAttributes & 0x03) != 0x01 || | ||
2955 | (alts->endpoint[0].desc.bEndpointAddress & 0x80)) { | ||
2956 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u first endpoint not isochronous out\n", | ||
2957 | dev->devnum, asifout, i); | ||
2958 | continue; | ||
2959 | } | ||
2960 | /* See USB audio formats manual, section 2 */ | ||
2961 | fmt = find_csinterface_descriptor(buffer, buflen, NULL, AS_GENERAL, asifout, i); | ||
2962 | if (!fmt) { | ||
2963 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", | ||
2964 | dev->devnum, asifout, i); | ||
2965 | continue; | ||
2966 | } | ||
2967 | if (fmt[0] < 7 || fmt[6] != 0 || (fmt[5] != 1 && fmt[5] != 2)) { | ||
2968 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u format not supported\n", | ||
2969 | dev->devnum, asifout, i); | ||
2970 | continue; | ||
2971 | } | ||
2972 | format = (fmt[5] == 2) ? (AFMT_U16_LE | AFMT_U8) : (AFMT_S16_LE | AFMT_S8); | ||
2973 | /* Dallas DS4201 workaround */ | ||
2974 | if (le16_to_cpu(dev->descriptor.idVendor) == 0x04fa && | ||
2975 | le16_to_cpu(dev->descriptor.idProduct) == 0x4201) | ||
2976 | format = (AFMT_S16_LE | AFMT_S8); | ||
2977 | fmt = find_csinterface_descriptor(buffer, buflen, NULL, FORMAT_TYPE, asifout, i); | ||
2978 | if (!fmt) { | ||
2979 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not found\n", | ||
2980 | dev->devnum, asifout, i); | ||
2981 | continue; | ||
2982 | } | ||
2983 | if (fmt[0] < 8+3*(fmt[7] ? fmt[7] : 2) || fmt[3] != 1) { | ||
2984 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u FORMAT_TYPE descriptor not supported\n", | ||
2985 | dev->devnum, asifout, i); | ||
2986 | continue; | ||
2987 | } | ||
2988 | if (fmt[4] < 1 || fmt[4] > 2 || fmt[5] < 1 || fmt[5] > 2) { | ||
2989 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u unsupported channels %u framesize %u\n", | ||
2990 | dev->devnum, asifout, i, fmt[4], fmt[5]); | ||
2991 | continue; | ||
2992 | } | ||
2993 | csep = find_descriptor(buffer, buflen, NULL, USB_DT_CS_ENDPOINT, asifout, i); | ||
2994 | if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) { | ||
2995 | printk(KERN_ERR "usbaudio: device %u interface %u altsetting %u no or invalid class specific endpoint descriptor\n", | ||
2996 | dev->devnum, asifout, i); | ||
2997 | continue; | ||
2998 | } | ||
2999 | if (as->numfmtout >= MAXFORMATS) | ||
3000 | continue; | ||
3001 | fp = &as->fmtout[as->numfmtout++]; | ||
3002 | if (fmt[5] == 2) | ||
3003 | format &= (AFMT_U16_LE | AFMT_S16_LE); | ||
3004 | else | ||
3005 | format &= (AFMT_U8 | AFMT_S8); | ||
3006 | if (fmt[4] == 2) | ||
3007 | format |= AFMT_STEREO; | ||
3008 | fp->format = format; | ||
3009 | fp->altsetting = i; | ||
3010 | fp->sratelo = fp->sratehi = fmt[8] | (fmt[9] << 8) | (fmt[10] << 16); | ||
3011 | printk(KERN_INFO "usbaudio: valid output sample rate %u\n", fp->sratelo); | ||
3012 | for (j = fmt[7] ? (fmt[7]-1) : 1; j > 0; j--) { | ||
3013 | k = fmt[8+3*j] | (fmt[9+3*j] << 8) | (fmt[10+3*j] << 16); | ||
3014 | printk(KERN_INFO "usbaudio: valid output sample rate %u\n", k); | ||
3015 | if (k > fp->sratehi) | ||
3016 | fp->sratehi = k; | ||
3017 | if (k < fp->sratelo) | ||
3018 | fp->sratelo = k; | ||
3019 | } | ||
3020 | fp->attributes = csep[3]; | ||
3021 | printk(KERN_INFO "usbaudio: device %u interface %u altsetting %u: format 0x%08x sratelo %u sratehi %u attributes 0x%02x\n", | ||
3022 | dev->devnum, asifout, i, fp->format, fp->sratelo, fp->sratehi, fp->attributes); | ||
3023 | } | ||
3024 | } | ||
3025 | if (as->numfmtin == 0 && as->numfmtout == 0) { | ||
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 | if ((as->dev_audio = register_sound_dsp(&usb_audio_fops, -1)) < 0) { | ||
3038 | printk(KERN_ERR "usbaudio: cannot register dsp\n"); | ||
3039 | usb_free_urb(as->usbin.durb[0].urb); | ||
3040 | usb_free_urb(as->usbin.durb[1].urb); | ||
3041 | usb_free_urb(as->usbin.surb[0].urb); | ||
3042 | usb_free_urb(as->usbin.surb[1].urb); | ||
3043 | usb_free_urb(as->usbout.durb[0].urb); | ||
3044 | usb_free_urb(as->usbout.durb[1].urb); | ||
3045 | usb_free_urb(as->usbout.surb[0].urb); | ||
3046 | usb_free_urb(as->usbout.surb[1].urb); | ||
3047 | kfree(as); | ||
3048 | return; | ||
3049 | } | ||
3050 | printk(KERN_INFO "usbaudio: registered dsp 14,%d\n", as->dev_audio); | ||
3051 | /* everything successful */ | ||
3052 | list_add_tail(&as->list, &s->audiolist); | ||
3053 | } | ||
3054 | |||
3055 | struct consmixstate { | ||
3056 | struct usb_audio_state *s; | ||
3057 | unsigned char *buffer; | ||
3058 | unsigned int buflen; | ||
3059 | unsigned int ctrlif; | ||
3060 | struct mixerchannel mixch[SOUND_MIXER_NRDEVICES]; | ||
3061 | unsigned int nrmixch; | ||
3062 | unsigned int mixchmask; | ||
3063 | unsigned long unitbitmap[32/sizeof(unsigned long)]; | ||
3064 | /* return values */ | ||
3065 | unsigned int nrchannels; | ||
3066 | unsigned int termtype; | ||
3067 | unsigned int chconfig; | ||
3068 | }; | ||
3069 | |||
3070 | static struct mixerchannel *getmixchannel(struct consmixstate *state, unsigned int nr) | ||
3071 | { | ||
3072 | struct mixerchannel *c; | ||
3073 | |||
3074 | if (nr >= SOUND_MIXER_NRDEVICES) { | ||
3075 | printk(KERN_ERR "usbaudio: invalid OSS mixer channel %u\n", nr); | ||
3076 | return NULL; | ||
3077 | } | ||
3078 | if (!(state->mixchmask & (1 << nr))) { | ||
3079 | printk(KERN_WARNING "usbaudio: OSS mixer channel %u already in use\n", nr); | ||
3080 | return NULL; | ||
3081 | } | ||
3082 | c = &state->mixch[state->nrmixch++]; | ||
3083 | c->osschannel = nr; | ||
3084 | state->mixchmask &= ~(1 << nr); | ||
3085 | return c; | ||
3086 | } | ||
3087 | |||
3088 | static unsigned int getvolchannel(struct consmixstate *state) | ||
3089 | { | ||
3090 | unsigned int u; | ||
3091 | |||
3092 | if ((state->termtype & 0xff00) == 0x0000 && (state->mixchmask & SOUND_MASK_VOLUME)) | ||
3093 | return SOUND_MIXER_VOLUME; | ||
3094 | if ((state->termtype & 0xff00) == 0x0100) { | ||
3095 | if (state->mixchmask & SOUND_MASK_PCM) | ||
3096 | return SOUND_MIXER_PCM; | ||
3097 | if (state->mixchmask & SOUND_MASK_ALTPCM) | ||
3098 | return SOUND_MIXER_ALTPCM; | ||
3099 | } | ||
3100 | if ((state->termtype & 0xff00) == 0x0200 && (state->mixchmask & SOUND_MASK_MIC)) | ||
3101 | return SOUND_MIXER_MIC; | ||
3102 | if ((state->termtype & 0xff00) == 0x0300 && (state->mixchmask & SOUND_MASK_SPEAKER)) | ||
3103 | return SOUND_MIXER_SPEAKER; | ||
3104 | if ((state->termtype & 0xff00) == 0x0500) { | ||
3105 | if (state->mixchmask & SOUND_MASK_PHONEIN) | ||
3106 | return SOUND_MIXER_PHONEIN; | ||
3107 | if (state->mixchmask & SOUND_MASK_PHONEOUT) | ||
3108 | return SOUND_MIXER_PHONEOUT; | ||
3109 | } | ||
3110 | if (state->termtype >= 0x710 && state->termtype <= 0x711 && (state->mixchmask & SOUND_MASK_RADIO)) | ||
3111 | return SOUND_MIXER_RADIO; | ||
3112 | if (state->termtype >= 0x709 && state->termtype <= 0x70f && (state->mixchmask & SOUND_MASK_VIDEO)) | ||
3113 | return SOUND_MIXER_VIDEO; | ||
3114 | u = ffs(state->mixchmask & (SOUND_MASK_LINE | SOUND_MASK_CD | SOUND_MASK_LINE1 | SOUND_MASK_LINE2 | SOUND_MASK_LINE3 | | ||
3115 | SOUND_MASK_DIGITAL1 | SOUND_MASK_DIGITAL2 | SOUND_MASK_DIGITAL3)); | ||
3116 | return u-1; | ||
3117 | } | ||
3118 | |||
3119 | static void prepmixch(struct consmixstate *state) | ||
3120 | { | ||
3121 | struct usb_device *dev = state->s->usbdev; | ||
3122 | struct mixerchannel *ch; | ||
3123 | unsigned char *buf; | ||
3124 | __s16 v1; | ||
3125 | unsigned int v2, v3; | ||
3126 | |||
3127 | if (!state->nrmixch || state->nrmixch > SOUND_MIXER_NRDEVICES) | ||
3128 | return; | ||
3129 | buf = kmalloc(sizeof(*buf) * 2, GFP_KERNEL); | ||
3130 | if (!buf) { | ||
3131 | printk(KERN_ERR "prepmixch: out of memory\n") ; | ||
3132 | return; | ||
3133 | } | ||
3134 | |||
3135 | ch = &state->mixch[state->nrmixch-1]; | ||
3136 | switch (ch->selector) { | ||
3137 | case 0: /* mixer unit request */ | ||
3138 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3139 | (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3140 | goto err; | ||
3141 | ch->minval = buf[0] | (buf[1] << 8); | ||
3142 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3143 | (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3144 | goto err; | ||
3145 | ch->maxval = buf[0] | (buf[1] << 8); | ||
3146 | v2 = ch->maxval - ch->minval; | ||
3147 | if (!v2) | ||
3148 | v2 = 1; | ||
3149 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3150 | (ch->chnum << 8) | 1, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3151 | goto err; | ||
3152 | v1 = buf[0] | (buf[1] << 8); | ||
3153 | v3 = v1 - ch->minval; | ||
3154 | v3 = 100 * v3 / v2; | ||
3155 | if (v3 > 100) | ||
3156 | v3 = 100; | ||
3157 | ch->value = v3; | ||
3158 | if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { | ||
3159 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3160 | ((ch->chnum + !!(ch->flags & MIXFLG_STEREOIN)) << 8) | (1 + !!(ch->flags & MIXFLG_STEREOOUT)), | ||
3161 | state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3162 | goto err; | ||
3163 | v1 = buf[0] | (buf[1] << 8); | ||
3164 | v3 = v1 - ch->minval; | ||
3165 | v3 = 100 * v3 / v2; | ||
3166 | if (v3 > 100) | ||
3167 | v3 = 100; | ||
3168 | } | ||
3169 | ch->value |= v3 << 8; | ||
3170 | break; | ||
3171 | |||
3172 | /* various feature unit controls */ | ||
3173 | case VOLUME_CONTROL: | ||
3174 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3175 | (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3176 | goto err; | ||
3177 | ch->minval = buf[0] | (buf[1] << 8); | ||
3178 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3179 | (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3180 | goto err; | ||
3181 | ch->maxval = buf[0] | (buf[1] << 8); | ||
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, state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3184 | goto err; | ||
3185 | v1 = buf[0] | (buf[1] << 8); | ||
3186 | v2 = ch->maxval - ch->minval; | ||
3187 | v3 = v1 - ch->minval; | ||
3188 | if (!v2) | ||
3189 | v2 = 1; | ||
3190 | v3 = 100 * v3 / v2; | ||
3191 | if (v3 > 100) | ||
3192 | v3 = 100; | ||
3193 | ch->value = v3; | ||
3194 | if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { | ||
3195 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3196 | (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 2, 1000) < 0) | ||
3197 | goto err; | ||
3198 | v1 = buf[0] | (buf[1] << 8); | ||
3199 | v3 = v1 - ch->minval; | ||
3200 | v3 = 100 * v3 / v2; | ||
3201 | if (v3 > 100) | ||
3202 | v3 = 100; | ||
3203 | } | ||
3204 | ch->value |= v3 << 8; | ||
3205 | break; | ||
3206 | |||
3207 | case BASS_CONTROL: | ||
3208 | case MID_CONTROL: | ||
3209 | case TREBLE_CONTROL: | ||
3210 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MIN, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3211 | (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0) | ||
3212 | goto err; | ||
3213 | ch->minval = buf[0] << 8; | ||
3214 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_MAX, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3215 | (ch->selector << 8) | ch->chnum, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0) | ||
3216 | goto err; | ||
3217 | ch->maxval = buf[0] << 8; | ||
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, state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0) | ||
3220 | goto err; | ||
3221 | v1 = buf[0] << 8; | ||
3222 | v2 = ch->maxval - ch->minval; | ||
3223 | v3 = v1 - ch->minval; | ||
3224 | if (!v2) | ||
3225 | v2 = 1; | ||
3226 | v3 = 100 * v3 / v2; | ||
3227 | if (v3 > 100) | ||
3228 | v3 = 100; | ||
3229 | ch->value = v3; | ||
3230 | if (ch->flags & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT)) { | ||
3231 | if (usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, | ||
3232 | (ch->selector << 8) | (ch->chnum + 1), state->ctrlif | (ch->unitid << 8), buf, 1, 1000) < 0) | ||
3233 | goto err; | ||
3234 | v1 = buf[0] << 8; | ||
3235 | v3 = v1 - ch->minval; | ||
3236 | v3 = 100 * v3 / v2; | ||
3237 | if (v3 > 100) | ||
3238 | v3 = 100; | ||
3239 | } | ||
3240 | ch->value |= v3 << 8; | ||
3241 | break; | ||
3242 | |||
3243 | default: | ||
3244 | goto err; | ||
3245 | } | ||
3246 | |||
3247 | freebuf: | ||
3248 | kfree(buf); | ||
3249 | return; | ||
3250 | err: | ||
3251 | printk(KERN_ERR "usbaudio: mixer request device %u if %u unit %u ch %u selector %u failed\n", | ||
3252 | dev->devnum, state->ctrlif, ch->unitid, ch->chnum, ch->selector); | ||
3253 | if (state->nrmixch) | ||
3254 | state->nrmixch--; | ||
3255 | goto freebuf; | ||
3256 | } | ||
3257 | |||
3258 | |||
3259 | static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid); | ||
3260 | |||
3261 | static inline int checkmixbmap(unsigned char *bmap, unsigned char flg, unsigned int inidx, unsigned int numoch) | ||
3262 | { | ||
3263 | unsigned int idx; | ||
3264 | |||
3265 | idx = inidx*numoch; | ||
3266 | if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7)))) | ||
3267 | return 0; | ||
3268 | if (!(flg & (MIXFLG_STEREOIN | MIXFLG_STEREOOUT))) | ||
3269 | return 1; | ||
3270 | idx = (inidx+!!(flg & MIXFLG_STEREOIN))*numoch+!!(flg & MIXFLG_STEREOOUT); | ||
3271 | if (!(bmap[-(idx >> 3)] & (0x80 >> (idx & 7)))) | ||
3272 | return 0; | ||
3273 | return 1; | ||
3274 | } | ||
3275 | |||
3276 | static void usb_audio_mixerunit(struct consmixstate *state, unsigned char *mixer) | ||
3277 | { | ||
3278 | unsigned int nroutch = mixer[5+mixer[4]]; | ||
3279 | unsigned int chidx[SOUND_MIXER_NRDEVICES+1]; | ||
3280 | unsigned int termt[SOUND_MIXER_NRDEVICES]; | ||
3281 | unsigned char flg = (nroutch >= 2) ? MIXFLG_STEREOOUT : 0; | ||
3282 | unsigned char *bmap = &mixer[9+mixer[4]]; | ||
3283 | unsigned int bmapsize; | ||
3284 | struct mixerchannel *ch; | ||
3285 | unsigned int i; | ||
3286 | |||
3287 | if (!mixer[4]) { | ||
3288 | printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor\n", mixer[3]); | ||
3289 | return; | ||
3290 | } | ||
3291 | if (mixer[4] > SOUND_MIXER_NRDEVICES) { | ||
3292 | printk(KERN_ERR "usbaudio: mixer unit %u: too many input pins\n", mixer[3]); | ||
3293 | return; | ||
3294 | } | ||
3295 | chidx[0] = 0; | ||
3296 | for (i = 0; i < mixer[4]; i++) { | ||
3297 | usb_audio_recurseunit(state, mixer[5+i]); | ||
3298 | chidx[i+1] = chidx[i] + state->nrchannels; | ||
3299 | termt[i] = state->termtype; | ||
3300 | } | ||
3301 | state->termtype = 0; | ||
3302 | state->chconfig = mixer[6+mixer[4]] | (mixer[7+mixer[4]] << 8); | ||
3303 | bmapsize = (nroutch * chidx[mixer[4]] + 7) >> 3; | ||
3304 | bmap += bmapsize - 1; | ||
3305 | if (mixer[0] < 10+mixer[4]+bmapsize) { | ||
3306 | printk(KERN_ERR "usbaudio: unit %u invalid MIXER_UNIT descriptor (bitmap too small)\n", mixer[3]); | ||
3307 | return; | ||
3308 | } | ||
3309 | for (i = 0; i < mixer[4]; i++) { | ||
3310 | state->termtype = termt[i]; | ||
3311 | if (chidx[i+1]-chidx[i] >= 2) { | ||
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 | continue; | ||
3323 | } | ||
3324 | } | ||
3325 | flg &= ~MIXFLG_STEREOIN; | ||
3326 | if (checkmixbmap(bmap, flg, chidx[i], nroutch)) { | ||
3327 | ch = getmixchannel(state, getvolchannel(state)); | ||
3328 | if (ch) { | ||
3329 | ch->unitid = mixer[3]; | ||
3330 | ch->selector = 0; | ||
3331 | ch->chnum = chidx[i]+1; | ||
3332 | ch->flags = flg; | ||
3333 | prepmixch(state); | ||
3334 | } | ||
3335 | } | ||
3336 | } | ||
3337 | state->termtype = 0; | ||
3338 | } | ||
3339 | |||
3340 | static struct mixerchannel *slctsrc_findunit(struct consmixstate *state, __u8 unitid) | ||
3341 | { | ||
3342 | unsigned int i; | ||
3343 | |||
3344 | for (i = 0; i < state->nrmixch; i++) | ||
3345 | if (state->mixch[i].unitid == unitid) | ||
3346 | return &state->mixch[i]; | ||
3347 | return NULL; | ||
3348 | } | ||
3349 | |||
3350 | static void usb_audio_selectorunit(struct consmixstate *state, unsigned char *selector) | ||
3351 | { | ||
3352 | unsigned int chnum, i, mixch; | ||
3353 | struct mixerchannel *mch; | ||
3354 | |||
3355 | if (!selector[4]) { | ||
3356 | printk(KERN_ERR "usbaudio: unit %u invalid SELECTOR_UNIT descriptor\n", selector[3]); | ||
3357 | return; | ||
3358 | } | ||
3359 | mixch = state->nrmixch; | ||
3360 | usb_audio_recurseunit(state, selector[5]); | ||
3361 | if (state->nrmixch != mixch) { | ||
3362 | mch = &state->mixch[state->nrmixch-1]; | ||
3363 | mch->slctunitid = selector[3] | (1 << 8); | ||
3364 | } else if ((mch = slctsrc_findunit(state, selector[5]))) { | ||
3365 | mch->slctunitid = selector[3] | (1 << 8); | ||
3366 | } else { | ||
3367 | printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel 1\n", selector[3]); | ||
3368 | } | ||
3369 | chnum = state->nrchannels; | ||
3370 | for (i = 1; i < selector[4]; i++) { | ||
3371 | mixch = state->nrmixch; | ||
3372 | usb_audio_recurseunit(state, selector[5+i]); | ||
3373 | if (chnum != state->nrchannels) { | ||
3374 | printk(KERN_ERR "usbaudio: selector unit %u: input pins with varying channel numbers\n", selector[3]); | ||
3375 | state->termtype = 0; | ||
3376 | state->chconfig = 0; | ||
3377 | state->nrchannels = 0; | ||
3378 | return; | ||
3379 | } | ||
3380 | if (state->nrmixch != mixch) { | ||
3381 | mch = &state->mixch[state->nrmixch-1]; | ||
3382 | mch->slctunitid = selector[3] | ((i + 1) << 8); | ||
3383 | } else if ((mch = slctsrc_findunit(state, selector[5+i]))) { | ||
3384 | mch->slctunitid = selector[3] | ((i + 1) << 8); | ||
3385 | } else { | ||
3386 | printk(KERN_INFO "usbaudio: selector unit %u: ignoring channel %u\n", selector[3], i+1); | ||
3387 | } | ||
3388 | } | ||
3389 | state->termtype = 0; | ||
3390 | state->chconfig = 0; | ||
3391 | } | ||
3392 | |||
3393 | /* in the future we might try to handle 3D etc. effect units */ | ||
3394 | |||
3395 | static void usb_audio_processingunit(struct consmixstate *state, unsigned char *proc) | ||
3396 | { | ||
3397 | unsigned int i; | ||
3398 | |||
3399 | for (i = 0; i < proc[6]; i++) | ||
3400 | usb_audio_recurseunit(state, proc[7+i]); | ||
3401 | state->nrchannels = proc[7+proc[6]]; | ||
3402 | state->termtype = 0; | ||
3403 | state->chconfig = proc[8+proc[6]] | (proc[9+proc[6]] << 8); | ||
3404 | } | ||
3405 | |||
3406 | |||
3407 | /* See Audio Class Spec, section 4.3.2.5 */ | ||
3408 | static void usb_audio_featureunit(struct consmixstate *state, unsigned char *ftr) | ||
3409 | { | ||
3410 | struct mixerchannel *ch; | ||
3411 | unsigned short chftr, mchftr; | ||
3412 | #if 0 | ||
3413 | struct usb_device *dev = state->s->usbdev; | ||
3414 | unsigned char data[1]; | ||
3415 | #endif | ||
3416 | unsigned char nr_logical_channels, i; | ||
3417 | |||
3418 | usb_audio_recurseunit(state, ftr[4]); | ||
3419 | |||
3420 | if (ftr[5] == 0 ) { | ||
3421 | printk(KERN_ERR "usbaudio: wrong controls size in feature unit %u\n",ftr[3]); | ||
3422 | return; | ||
3423 | } | ||
3424 | |||
3425 | if (state->nrchannels == 0) { | ||
3426 | printk(KERN_ERR "usbaudio: feature unit %u source has no channels\n", ftr[3]); | ||
3427 | return; | ||
3428 | } | ||
3429 | if (state->nrchannels > 2) | ||
3430 | printk(KERN_WARNING "usbaudio: feature unit %u: OSS mixer interface does not support more than 2 channels\n", ftr[3]); | ||
3431 | |||
3432 | nr_logical_channels=(ftr[0]-7)/ftr[5]-1; | ||
3433 | |||
3434 | if (nr_logical_channels != state->nrchannels) { | ||
3435 | printk(KERN_WARNING "usbaudio: warning: found %d of %d logical channels.\n", state->nrchannels,nr_logical_channels); | ||
3436 | |||
3437 | if (state->nrchannels == 1 && nr_logical_channels==0) { | ||
3438 | printk(KERN_INFO "usbaudio: assuming the channel found is the master channel (got a Philips camera?). Should be fine.\n"); | ||
3439 | } else if (state->nrchannels == 1 && nr_logical_channels==2) { | ||
3440 | 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"); | ||
3441 | state->nrchannels=nr_logical_channels; | ||
3442 | } else { | ||
3443 | printk(KERN_WARNING "usbaudio: no idea what's going on..., contact linux-usb-devel@lists.sourceforge.net\n"); | ||
3444 | } | ||
3445 | } | ||
3446 | |||
3447 | /* There is always a master channel */ | ||
3448 | mchftr = ftr[6]; | ||
3449 | /* Binary AND over logical channels if they exist */ | ||
3450 | if (nr_logical_channels) { | ||
3451 | chftr = ftr[6+ftr[5]]; | ||
3452 | for (i = 2; i <= nr_logical_channels; i++) | ||
3453 | chftr &= ftr[6+i*ftr[5]]; | ||
3454 | } else { | ||
3455 | chftr = 0; | ||
3456 | } | ||
3457 | |||
3458 | /* volume control */ | ||
3459 | if (chftr & 2) { | ||
3460 | ch = getmixchannel(state, getvolchannel(state)); | ||
3461 | if (ch) { | ||
3462 | ch->unitid = ftr[3]; | ||
3463 | ch->selector = VOLUME_CONTROL; | ||
3464 | ch->chnum = 1; | ||
3465 | ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; | ||
3466 | prepmixch(state); | ||
3467 | } | ||
3468 | } else if (mchftr & 2) { | ||
3469 | ch = getmixchannel(state, getvolchannel(state)); | ||
3470 | if (ch) { | ||
3471 | ch->unitid = ftr[3]; | ||
3472 | ch->selector = VOLUME_CONTROL; | ||
3473 | ch->chnum = 0; | ||
3474 | ch->flags = 0; | ||
3475 | prepmixch(state); | ||
3476 | } | ||
3477 | } | ||
3478 | /* bass control */ | ||
3479 | if (chftr & 4) { | ||
3480 | ch = getmixchannel(state, SOUND_MIXER_BASS); | ||
3481 | if (ch) { | ||
3482 | ch->unitid = ftr[3]; | ||
3483 | ch->selector = BASS_CONTROL; | ||
3484 | ch->chnum = 1; | ||
3485 | ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; | ||
3486 | prepmixch(state); | ||
3487 | } | ||
3488 | } else if (mchftr & 4) { | ||
3489 | ch = getmixchannel(state, SOUND_MIXER_BASS); | ||
3490 | if (ch) { | ||
3491 | ch->unitid = ftr[3]; | ||
3492 | ch->selector = BASS_CONTROL; | ||
3493 | ch->chnum = 0; | ||
3494 | ch->flags = 0; | ||
3495 | prepmixch(state); | ||
3496 | } | ||
3497 | } | ||
3498 | /* treble control */ | ||
3499 | if (chftr & 16) { | ||
3500 | ch = getmixchannel(state, SOUND_MIXER_TREBLE); | ||
3501 | if (ch) { | ||
3502 | ch->unitid = ftr[3]; | ||
3503 | ch->selector = TREBLE_CONTROL; | ||
3504 | ch->chnum = 1; | ||
3505 | ch->flags = (state->nrchannels > 1) ? (MIXFLG_STEREOIN | MIXFLG_STEREOOUT) : 0; | ||
3506 | prepmixch(state); | ||
3507 | } | ||
3508 | } else if (mchftr & 16) { | ||
3509 | ch = getmixchannel(state, SOUND_MIXER_TREBLE); | ||
3510 | if (ch) { | ||
3511 | ch->unitid = ftr[3]; | ||
3512 | ch->selector = TREBLE_CONTROL; | ||
3513 | ch->chnum = 0; | ||
3514 | ch->flags = 0; | ||
3515 | prepmixch(state); | ||
3516 | } | ||
3517 | } | ||
3518 | #if 0 | ||
3519 | /* if there are mute controls, unmute them */ | ||
3520 | /* does not seem to be necessary, and the Dallas chip does not seem to support the "all" channel (255) */ | ||
3521 | if ((chftr & 1) || (mchftr & 1)) { | ||
3522 | printk(KERN_DEBUG "usbaudio: unmuting feature unit %u interface %u\n", ftr[3], state->ctrlif); | ||
3523 | data[0] = 0; | ||
3524 | if (usb_control_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, | ||
3525 | (MUTE_CONTROL << 8) | 0xff, state->ctrlif | (ftr[3] << 8), data, 1, 1000) < 0) | ||
3526 | printk(KERN_WARNING "usbaudio: failure to unmute feature unit %u interface %u\n", ftr[3], state->ctrlif); | ||
3527 | } | ||
3528 | #endif | ||
3529 | } | ||
3530 | |||
3531 | static void usb_audio_recurseunit(struct consmixstate *state, unsigned char unitid) | ||
3532 | { | ||
3533 | unsigned char *p1; | ||
3534 | unsigned int i, j; | ||
3535 | |||
3536 | if (test_and_set_bit(unitid, state->unitbitmap)) { | ||
3537 | printk(KERN_INFO "usbaudio: mixer path revisits unit %d\n", unitid); | ||
3538 | return; | ||
3539 | } | ||
3540 | p1 = find_audiocontrol_unit(state->buffer, state->buflen, NULL, unitid, state->ctrlif); | ||
3541 | if (!p1) { | ||
3542 | printk(KERN_ERR "usbaudio: unit %d not found!\n", unitid); | ||
3543 | return; | ||
3544 | } | ||
3545 | state->nrchannels = 0; | ||
3546 | state->termtype = 0; | ||
3547 | state->chconfig = 0; | ||
3548 | switch (p1[2]) { | ||
3549 | case INPUT_TERMINAL: | ||
3550 | if (p1[0] < 12) { | ||
3551 | printk(KERN_ERR "usbaudio: unit %u: invalid INPUT_TERMINAL descriptor\n", unitid); | ||
3552 | return; | ||
3553 | } | ||
3554 | state->nrchannels = p1[7]; | ||
3555 | state->termtype = p1[4] | (p1[5] << 8); | ||
3556 | state->chconfig = p1[8] | (p1[9] << 8); | ||
3557 | return; | ||
3558 | |||
3559 | case MIXER_UNIT: | ||
3560 | if (p1[0] < 10 || p1[0] < 10+p1[4]) { | ||
3561 | printk(KERN_ERR "usbaudio: unit %u: invalid MIXER_UNIT descriptor\n", unitid); | ||
3562 | return; | ||
3563 | } | ||
3564 | usb_audio_mixerunit(state, p1); | ||
3565 | return; | ||
3566 | |||
3567 | case SELECTOR_UNIT: | ||
3568 | if (p1[0] < 6 || p1[0] < 6+p1[4]) { | ||
3569 | printk(KERN_ERR "usbaudio: unit %u: invalid SELECTOR_UNIT descriptor\n", unitid); | ||
3570 | return; | ||
3571 | } | ||
3572 | usb_audio_selectorunit(state, p1); | ||
3573 | return; | ||
3574 | |||
3575 | case FEATURE_UNIT: /* See USB Audio Class Spec 4.3.2.5 */ | ||
3576 | if (p1[0] < 7 || p1[0] < 7+p1[5]) { | ||
3577 | printk(KERN_ERR "usbaudio: unit %u: invalid FEATURE_UNIT descriptor\n", unitid); | ||
3578 | return; | ||
3579 | } | ||
3580 | usb_audio_featureunit(state, p1); | ||
3581 | return; | ||
3582 | |||
3583 | case PROCESSING_UNIT: | ||
3584 | if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) { | ||
3585 | printk(KERN_ERR "usbaudio: unit %u: invalid PROCESSING_UNIT descriptor\n", unitid); | ||
3586 | return; | ||
3587 | } | ||
3588 | usb_audio_processingunit(state, p1); | ||
3589 | return; | ||
3590 | |||
3591 | case EXTENSION_UNIT: | ||
3592 | if (p1[0] < 13 || p1[0] < 13+p1[6] || p1[0] < 13+p1[6]+p1[11+p1[6]]) { | ||
3593 | printk(KERN_ERR "usbaudio: unit %u: invalid EXTENSION_UNIT descriptor\n", unitid); | ||
3594 | return; | ||
3595 | } | ||
3596 | for (j = i = 0; i < p1[6]; i++) { | ||
3597 | usb_audio_recurseunit(state, p1[7+i]); | ||
3598 | if (!i) | ||
3599 | j = state->termtype; | ||
3600 | else if (j != state->termtype) | ||
3601 | j = 0; | ||
3602 | } | ||
3603 | state->nrchannels = p1[7+p1[6]]; | ||
3604 | state->chconfig = p1[8+p1[6]] | (p1[9+p1[6]] << 8); | ||
3605 | state->termtype = j; | ||
3606 | return; | ||
3607 | |||
3608 | default: | ||
3609 | printk(KERN_ERR "usbaudio: unit %u: unexpected type 0x%02x\n", unitid, p1[2]); | ||
3610 | return; | ||
3611 | } | ||
3612 | } | ||
3613 | |||
3614 | static void usb_audio_constructmixer(struct usb_audio_state *s, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif, unsigned char *oterm) | ||
3615 | { | ||
3616 | struct usb_mixerdev *ms; | ||
3617 | struct consmixstate state; | ||
3618 | |||
3619 | memset(&state, 0, sizeof(state)); | ||
3620 | state.s = s; | ||
3621 | state.nrmixch = 0; | ||
3622 | state.mixchmask = ~0; | ||
3623 | state.buffer = buffer; | ||
3624 | state.buflen = buflen; | ||
3625 | state.ctrlif = ctrlif; | ||
3626 | set_bit(oterm[3], state.unitbitmap); /* mark terminal ID as visited */ | ||
3627 | printk(KERN_DEBUG "usbaudio: constructing mixer for Terminal %u type 0x%04x\n", | ||
3628 | oterm[3], oterm[4] | (oterm[5] << 8)); | ||
3629 | usb_audio_recurseunit(&state, oterm[7]); | ||
3630 | if (!state.nrmixch) { | ||
3631 | printk(KERN_INFO "usbaudio: no mixer controls found for Terminal %u\n", oterm[3]); | ||
3632 | return; | ||
3633 | } | ||
3634 | if (!(ms = kmalloc(sizeof(struct usb_mixerdev)+state.nrmixch*sizeof(struct mixerchannel), GFP_KERNEL))) | ||
3635 | return; | ||
3636 | memset(ms, 0, sizeof(struct usb_mixerdev)); | ||
3637 | memcpy(&ms->ch, &state.mixch, state.nrmixch*sizeof(struct mixerchannel)); | ||
3638 | ms->state = s; | ||
3639 | ms->iface = ctrlif; | ||
3640 | ms->numch = state.nrmixch; | ||
3641 | if ((ms->dev_mixer = register_sound_mixer(&usb_mixer_fops, -1)) < 0) { | ||
3642 | printk(KERN_ERR "usbaudio: cannot register mixer\n"); | ||
3643 | kfree(ms); | ||
3644 | return; | ||
3645 | } | ||
3646 | printk(KERN_INFO "usbaudio: registered mixer 14,%d\n", ms->dev_mixer); | ||
3647 | list_add_tail(&ms->list, &s->mixerlist); | ||
3648 | } | ||
3649 | |||
3650 | /* arbitrary limit, we won't check more interfaces than this */ | ||
3651 | #define USB_MAXINTERFACES 32 | ||
3652 | |||
3653 | static struct usb_audio_state *usb_audio_parsecontrol(struct usb_device *dev, unsigned char *buffer, unsigned int buflen, unsigned int ctrlif) | ||
3654 | { | ||
3655 | struct usb_audio_state *s; | ||
3656 | struct usb_interface *iface; | ||
3657 | struct usb_host_interface *alt; | ||
3658 | unsigned char ifin[USB_MAXINTERFACES], ifout[USB_MAXINTERFACES]; | ||
3659 | unsigned char *p1; | ||
3660 | unsigned int i, j, k, numifin = 0, numifout = 0; | ||
3661 | |||
3662 | if (!(s = kmalloc(sizeof(struct usb_audio_state), GFP_KERNEL))) | ||
3663 | return NULL; | ||
3664 | memset(s, 0, sizeof(struct usb_audio_state)); | ||
3665 | INIT_LIST_HEAD(&s->audiolist); | ||
3666 | INIT_LIST_HEAD(&s->mixerlist); | ||
3667 | s->usbdev = dev; | ||
3668 | s->count = 1; | ||
3669 | |||
3670 | /* find audiocontrol interface */ | ||
3671 | if (!(p1 = find_csinterface_descriptor(buffer, buflen, NULL, HEADER, ctrlif, -1))) { | ||
3672 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u no HEADER found\n", | ||
3673 | dev->devnum, ctrlif); | ||
3674 | goto ret; | ||
3675 | } | ||
3676 | if (p1[0] < 8 + p1[7]) { | ||
3677 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u HEADER error\n", | ||
3678 | dev->devnum, ctrlif); | ||
3679 | goto ret; | ||
3680 | } | ||
3681 | if (!p1[7]) | ||
3682 | printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has no AudioStreaming and MidiStreaming interfaces\n", | ||
3683 | dev->devnum, ctrlif); | ||
3684 | for (i = 0; i < p1[7]; i++) { | ||
3685 | j = p1[8+i]; | ||
3686 | iface = usb_ifnum_to_if(dev, j); | ||
3687 | if (!iface) { | ||
3688 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u does not exist\n", | ||
3689 | dev->devnum, ctrlif, j); | ||
3690 | continue; | ||
3691 | } | ||
3692 | if (iface->num_altsetting == 1) { | ||
3693 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u has only 1 altsetting.\n", dev->devnum, ctrlif); | ||
3694 | continue; | ||
3695 | } | ||
3696 | alt = usb_altnum_to_altsetting(iface, 0); | ||
3697 | if (!alt) { | ||
3698 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 0\n", | ||
3699 | dev->devnum, ctrlif, j); | ||
3700 | continue; | ||
3701 | } | ||
3702 | if (alt->desc.bInterfaceClass != USB_CLASS_AUDIO) { | ||
3703 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u is not an AudioClass interface\n", | ||
3704 | dev->devnum, ctrlif, j); | ||
3705 | continue; | ||
3706 | } | ||
3707 | if (alt->desc.bInterfaceSubClass == 3) { | ||
3708 | printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u interface %u MIDIStreaming not supported\n", | ||
3709 | dev->devnum, ctrlif, j); | ||
3710 | continue; | ||
3711 | } | ||
3712 | if (alt->desc.bInterfaceSubClass != 2) { | ||
3713 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u invalid AudioClass subtype\n", | ||
3714 | dev->devnum, ctrlif, j); | ||
3715 | continue; | ||
3716 | } | ||
3717 | if (alt->desc.bNumEndpoints > 0) { | ||
3718 | /* Check all endpoints; should they all have a bandwidth of 0 ? */ | ||
3719 | for (k = 0; k < alt->desc.bNumEndpoints; k++) { | ||
3720 | if (le16_to_cpu(alt->endpoint[k].desc.wMaxPacketSize) > 0) { | ||
3721 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u endpoint %d does not have 0 bandwidth at alt[0]\n", dev->devnum, ctrlif, k); | ||
3722 | break; | ||
3723 | } | ||
3724 | } | ||
3725 | if (k < alt->desc.bNumEndpoints) | ||
3726 | continue; | ||
3727 | } | ||
3728 | |||
3729 | alt = usb_altnum_to_altsetting(iface, 1); | ||
3730 | if (!alt) { | ||
3731 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no altsetting 1\n", | ||
3732 | dev->devnum, ctrlif, j); | ||
3733 | continue; | ||
3734 | } | ||
3735 | if (alt->desc.bNumEndpoints < 1) { | ||
3736 | printk(KERN_ERR "usbaudio: device %d audiocontrol interface %u interface %u has no endpoint\n", | ||
3737 | dev->devnum, ctrlif, j); | ||
3738 | continue; | ||
3739 | } | ||
3740 | /* note: this requires the data endpoint to be ep0 and the optional sync | ||
3741 | ep to be ep1, which seems to be the case */ | ||
3742 | if (alt->endpoint[0].desc.bEndpointAddress & USB_DIR_IN) { | ||
3743 | if (numifin < USB_MAXINTERFACES) { | ||
3744 | ifin[numifin++] = j; | ||
3745 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1); | ||
3746 | } | ||
3747 | } else { | ||
3748 | if (numifout < USB_MAXINTERFACES) { | ||
3749 | ifout[numifout++] = j; | ||
3750 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1); | ||
3751 | } | ||
3752 | } | ||
3753 | } | ||
3754 | printk(KERN_INFO "usbaudio: device %d audiocontrol interface %u has %u input and %u output AudioStreaming interfaces\n", | ||
3755 | dev->devnum, ctrlif, numifin, numifout); | ||
3756 | for (i = 0; i < numifin && i < numifout; i++) | ||
3757 | usb_audio_parsestreaming(s, buffer, buflen, ifin[i], ifout[i]); | ||
3758 | for (j = i; j < numifin; j++) | ||
3759 | usb_audio_parsestreaming(s, buffer, buflen, ifin[i], -1); | ||
3760 | for (j = i; j < numifout; j++) | ||
3761 | usb_audio_parsestreaming(s, buffer, buflen, -1, ifout[i]); | ||
3762 | /* now walk through all OUTPUT_TERMINAL descriptors to search for mixers */ | ||
3763 | p1 = find_csinterface_descriptor(buffer, buflen, NULL, OUTPUT_TERMINAL, ctrlif, -1); | ||
3764 | while (p1) { | ||
3765 | if (p1[0] >= 9) | ||
3766 | usb_audio_constructmixer(s, buffer, buflen, ctrlif, p1); | ||
3767 | p1 = find_csinterface_descriptor(buffer, buflen, p1, OUTPUT_TERMINAL, ctrlif, -1); | ||
3768 | } | ||
3769 | |||
3770 | ret: | ||
3771 | if (list_empty(&s->audiolist) && list_empty(&s->mixerlist)) { | ||
3772 | kfree(s); | ||
3773 | return NULL; | ||
3774 | } | ||
3775 | /* everything successful */ | ||
3776 | down(&open_sem); | ||
3777 | list_add_tail(&s->audiodev, &audiodevs); | ||
3778 | up(&open_sem); | ||
3779 | printk(KERN_DEBUG "usb_audio_parsecontrol: usb_audio_state at %p\n", s); | ||
3780 | return s; | ||
3781 | } | ||
3782 | |||
3783 | /* we only care for the currently active configuration */ | ||
3784 | |||
3785 | static int usb_audio_probe(struct usb_interface *intf, | ||
3786 | const struct usb_device_id *id) | ||
3787 | { | ||
3788 | struct usb_device *dev = interface_to_usbdev (intf); | ||
3789 | struct usb_audio_state *s; | ||
3790 | unsigned char *buffer; | ||
3791 | unsigned int buflen; | ||
3792 | |||
3793 | #if 0 | ||
3794 | printk(KERN_DEBUG "usbaudio: Probing if %i: IC %x, ISC %x\n", ifnum, | ||
3795 | config->interface[ifnum].altsetting[0].desc.bInterfaceClass, | ||
3796 | config->interface[ifnum].altsetting[0].desc.bInterfaceSubClass); | ||
3797 | #endif | ||
3798 | |||
3799 | /* | ||
3800 | * audiocontrol interface found | ||
3801 | * find which configuration number is active | ||
3802 | */ | ||
3803 | buffer = dev->rawdescriptors[dev->actconfig - dev->config]; | ||
3804 | buflen = le16_to_cpu(dev->actconfig->desc.wTotalLength); | ||
3805 | s = usb_audio_parsecontrol(dev, buffer, buflen, intf->altsetting->desc.bInterfaceNumber); | ||
3806 | if (s) { | ||
3807 | usb_set_intfdata (intf, s); | ||
3808 | return 0; | ||
3809 | } | ||
3810 | return -ENODEV; | ||
3811 | } | ||
3812 | |||
3813 | |||
3814 | /* a revoke facility would make things simpler */ | ||
3815 | |||
3816 | static void usb_audio_disconnect(struct usb_interface *intf) | ||
3817 | { | ||
3818 | struct usb_audio_state *s = usb_get_intfdata (intf); | ||
3819 | struct usb_audiodev *as; | ||
3820 | struct usb_mixerdev *ms; | ||
3821 | |||
3822 | if (!s) | ||
3823 | return; | ||
3824 | |||
3825 | /* we get called with -1 for every audiostreaming interface registered */ | ||
3826 | if (s == (struct usb_audio_state *)-1) { | ||
3827 | dprintk((KERN_DEBUG "usbaudio: note, usb_audio_disconnect called with -1\n")); | ||
3828 | return; | ||
3829 | } | ||
3830 | if (!s->usbdev) { | ||
3831 | dprintk((KERN_DEBUG "usbaudio: error, usb_audio_disconnect already called for %p!\n", s)); | ||
3832 | return; | ||
3833 | } | ||
3834 | down(&open_sem); | ||
3835 | list_del_init(&s->audiodev); | ||
3836 | s->usbdev = NULL; | ||
3837 | usb_set_intfdata (intf, NULL); | ||
3838 | |||
3839 | /* deregister all audio and mixer devices, so no new processes can open this device */ | ||
3840 | list_for_each_entry(as, &s->audiolist, list) { | ||
3841 | usbin_disc(as); | ||
3842 | usbout_disc(as); | ||
3843 | wake_up(&as->usbin.dma.wait); | ||
3844 | wake_up(&as->usbout.dma.wait); | ||
3845 | if (as->dev_audio >= 0) { | ||
3846 | unregister_sound_dsp(as->dev_audio); | ||
3847 | printk(KERN_INFO "usbaudio: unregister dsp 14,%d\n", as->dev_audio); | ||
3848 | } | ||
3849 | as->dev_audio = -1; | ||
3850 | } | ||
3851 | list_for_each_entry(ms, &s->mixerlist, list) { | ||
3852 | if (ms->dev_mixer >= 0) { | ||
3853 | unregister_sound_mixer(ms->dev_mixer); | ||
3854 | printk(KERN_INFO "usbaudio: unregister mixer 14,%d\n", ms->dev_mixer); | ||
3855 | } | ||
3856 | ms->dev_mixer = -1; | ||
3857 | } | ||
3858 | release(s); | ||
3859 | wake_up(&open_wait); | ||
3860 | } | ||
3861 | |||
3862 | static int __init usb_audio_init(void) | ||
3863 | { | ||
3864 | int result = usb_register(&usb_audio_driver); | ||
3865 | if (result == 0) | ||
3866 | info(DRIVER_VERSION ":" DRIVER_DESC); | ||
3867 | return result; | ||
3868 | } | ||
3869 | |||
3870 | |||
3871 | static void __exit usb_audio_cleanup(void) | ||
3872 | { | ||
3873 | usb_deregister(&usb_audio_driver); | ||
3874 | } | ||
3875 | |||
3876 | module_init(usb_audio_init); | ||
3877 | module_exit(usb_audio_cleanup); | ||
3878 | |||
3879 | MODULE_AUTHOR( DRIVER_AUTHOR ); | ||
3880 | MODULE_DESCRIPTION( DRIVER_DESC ); | ||
3881 | MODULE_LICENSE("GPL"); | ||
3882 | |||
diff --git a/drivers/usb/class/audio.h b/drivers/usb/class/audio.h new file mode 100644 index 000000000000..45916eb12103 --- /dev/null +++ b/drivers/usb/class/audio.h | |||
@@ -0,0 +1,110 @@ | |||
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 | |||
29 | struct 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 | |||
39 | struct 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 | |||
49 | struct 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 | |||
61 | struct 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/bluetty.c b/drivers/usb/class/bluetty.c new file mode 100644 index 000000000000..6bac65e0ade7 --- /dev/null +++ b/drivers/usb/class/bluetty.c | |||
@@ -0,0 +1,1279 @@ | |||
1 | /* | ||
2 | * bluetty.c Version 0.13 | ||
3 | * | ||
4 | * Copyright (C) 2000, 2001 Greg Kroah-Hartman <greg@kroah.com> | ||
5 | * Copyright (C) 2000 Mark Douglas Corner <mcorner@umich.edu> | ||
6 | * | ||
7 | * USB Bluetooth TTY driver, based on the Bluetooth Spec version 1.0B | ||
8 | * | ||
9 | * (2001/11/30) Version 0.13 gkh | ||
10 | * - added locking patch from Masoodur Rahman <rmasoodu@in.ibm.com> | ||
11 | * - removed active variable, as open_count will do. | ||
12 | * | ||
13 | * (2001/07/09) Version 0.12 gkh | ||
14 | * - removed in_interrupt() call, as it doesn't make sense to do | ||
15 | * that anymore. | ||
16 | * | ||
17 | * (2001/06/05) Version 0.11 gkh | ||
18 | * - Fixed problem with read urb status saying that we have shutdown, | ||
19 | * and that we shouldn't resubmit the urb. Patch from unknown. | ||
20 | * | ||
21 | * (2001/05/28) Version 0.10 gkh | ||
22 | * - Fixed problem with using data from userspace in the bluetooth_write | ||
23 | * function as found by the CHECKER project. | ||
24 | * - Added a buffer to the write_urb_pool which reduces the number of | ||
25 | * buffers being created and destroyed for ever write. Also cleans | ||
26 | * up the logic a bit. | ||
27 | * - Added a buffer to the control_urb_pool which fixes a memory leak | ||
28 | * when the device is removed from the system. | ||
29 | * | ||
30 | * (2001/05/28) Version 0.9 gkh | ||
31 | * Fixed problem with bluetooth==NULL for bluetooth_read_bulk_callback | ||
32 | * which was found by both the CHECKER project and Mikko Rahkonen. | ||
33 | * | ||
34 | * (08/04/2001) gb | ||
35 | * Identify version on module load. | ||
36 | * | ||
37 | * (2001/03/10) Version 0.8 gkh | ||
38 | * Fixed problem with not unlinking interrupt urb on device close | ||
39 | * and resubmitting the read urb on error with bluetooth struct. | ||
40 | * Thanks to Narayan Mohanram <narayan@RovingNetworks.com> for the | ||
41 | * fixes. | ||
42 | * | ||
43 | * (11/29/2000) Version 0.7 gkh | ||
44 | * Fixed problem with overrunning the tty flip buffer. | ||
45 | * Removed unneeded NULL pointer initialization. | ||
46 | * | ||
47 | * (10/05/2000) Version 0.6 gkh | ||
48 | * Fixed bug with urb->dev not being set properly, now that the usb | ||
49 | * core needs it. | ||
50 | * Got a real major id number and name. | ||
51 | * | ||
52 | * (08/06/2000) Version 0.5 gkh | ||
53 | * Fixed problem of not resubmitting the bulk read urb if there is | ||
54 | * an error in the callback. Ericsson devices seem to need this. | ||
55 | * | ||
56 | * (07/11/2000) Version 0.4 gkh | ||
57 | * Fixed bug in disconnect for when we call tty_hangup | ||
58 | * Fixed bug in bluetooth_ctrl_msg where the bluetooth struct was not | ||
59 | * getting attached to the control urb properly. | ||
60 | * Fixed bug in bluetooth_write where we pay attention to the result | ||
61 | * of bluetooth_ctrl_msg. | ||
62 | * | ||
63 | * (08/03/2000) Version 0.3 gkh mdc | ||
64 | * Merged in Mark's changes to make the driver play nice with the Axis | ||
65 | * stack. | ||
66 | * Made the write bulk use an urb pool to enable larger transfers with | ||
67 | * fewer calls to the driver. | ||
68 | * Fixed off by one bug in acl pkt receive | ||
69 | * Made packet counters specific to each bluetooth device | ||
70 | * Added checks for zero length callbacks | ||
71 | * Added buffers for int and bulk packets. Had to do this otherwise | ||
72 | * packet types could intermingle. | ||
73 | * Made a control urb pool for the control messages. | ||
74 | * | ||
75 | * (07/11/2000) Version 0.2 gkh | ||
76 | * Fixed a small bug found by Nils Faerber in the usb_bluetooth_probe | ||
77 | * function. | ||
78 | * | ||
79 | * (07/09/2000) Version 0.1 gkh | ||
80 | * Initial release. Has support for sending ACL data (which is really just | ||
81 | * a HCI frame.) Raw HCI commands and HCI events are not supported. | ||
82 | * A ioctl will probably be needed for the HCI commands and events in the | ||
83 | * future. All isoch endpoints are ignored at this time also. | ||
84 | * This driver should work for all currently shipping USB Bluetooth | ||
85 | * devices at this time :) | ||
86 | * | ||
87 | */ | ||
88 | |||
89 | /* | ||
90 | * This program is free software; you can redistribute it and/or modify | ||
91 | * it under the terms of the GNU General Public License as published by | ||
92 | * the Free Software Foundation; either version 2 of the License, or | ||
93 | * (at your option) any later version. | ||
94 | * | ||
95 | * This program is distributed in the hope that it will be useful, | ||
96 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
97 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
98 | * GNU General Public License for more details. | ||
99 | * | ||
100 | * You should have received a copy of the GNU General Public License | ||
101 | * along with this program; if not, write to the Free Software | ||
102 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
103 | */ | ||
104 | |||
105 | |||
106 | #include <linux/kernel.h> | ||
107 | #include <linux/errno.h> | ||
108 | #include <linux/init.h> | ||
109 | #include <linux/slab.h> | ||
110 | #include <linux/tty.h> | ||
111 | #include <linux/tty_driver.h> | ||
112 | #include <linux/tty_flip.h> | ||
113 | #include <linux/module.h> | ||
114 | #include <asm/uaccess.h> | ||
115 | |||
116 | #define DEBUG | ||
117 | #include <linux/usb.h> | ||
118 | |||
119 | /* | ||
120 | * Version Information | ||
121 | */ | ||
122 | #define DRIVER_VERSION "v0.13" | ||
123 | #define DRIVER_AUTHOR "Greg Kroah-Hartman, Mark Douglas Corner" | ||
124 | #define DRIVER_DESC "USB Bluetooth tty driver" | ||
125 | |||
126 | /* define this if you have hardware that is not good */ | ||
127 | /*#define BTBUGGYHARDWARE */ | ||
128 | |||
129 | /* Class, SubClass, and Protocol codes that describe a Bluetooth device */ | ||
130 | #define WIRELESS_CLASS_CODE 0xe0 | ||
131 | #define RF_SUBCLASS_CODE 0x01 | ||
132 | #define BLUETOOTH_PROGRAMMING_PROTOCOL_CODE 0x01 | ||
133 | |||
134 | |||
135 | #define BLUETOOTH_TTY_MAJOR 216 /* real device node major id */ | ||
136 | #define BLUETOOTH_TTY_MINORS 256 /* whole lotta bluetooth devices */ | ||
137 | |||
138 | #define USB_BLUETOOTH_MAGIC 0x6d02 /* magic number for bluetooth struct */ | ||
139 | |||
140 | #define BLUETOOTH_CONTROL_REQUEST_TYPE 0x20 | ||
141 | |||
142 | /* Bluetooth packet types */ | ||
143 | #define CMD_PKT 0x01 | ||
144 | #define ACL_PKT 0x02 | ||
145 | #define SCO_PKT 0x03 | ||
146 | #define EVENT_PKT 0x04 | ||
147 | #define ERROR_PKT 0x05 | ||
148 | #define NEG_PKT 0x06 | ||
149 | |||
150 | /* Message sizes */ | ||
151 | #define MAX_EVENT_SIZE 0xFF | ||
152 | #define EVENT_HDR_SIZE 3 /* 2 for the header + 1 for the type indicator */ | ||
153 | #define EVENT_BUFFER_SIZE (MAX_EVENT_SIZE + EVENT_HDR_SIZE) | ||
154 | |||
155 | #define MAX_ACL_SIZE 0xFFFF | ||
156 | #define ACL_HDR_SIZE 5 /* 4 for the header + 1 for the type indicator */ | ||
157 | #define ACL_BUFFER_SIZE (MAX_ACL_SIZE + ACL_HDR_SIZE) | ||
158 | |||
159 | /* parity check flag */ | ||
160 | #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) | ||
161 | |||
162 | #define CHAR2INT16(c1,c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff)) | ||
163 | |||
164 | #define NUM_BULK_URBS 24 | ||
165 | #define NUM_CONTROL_URBS 16 | ||
166 | |||
167 | struct usb_bluetooth { | ||
168 | int magic; | ||
169 | struct usb_device * dev; | ||
170 | struct tty_driver * tty_driver; /* the tty_driver for this device */ | ||
171 | struct tty_struct * tty; /* the corresponding tty for this port */ | ||
172 | |||
173 | unsigned char minor; /* the starting minor number for this device */ | ||
174 | int throttle; /* throttled by tty layer */ | ||
175 | int open_count; | ||
176 | |||
177 | __u8 control_out_bInterfaceNum; | ||
178 | struct urb * control_urb_pool[NUM_CONTROL_URBS]; | ||
179 | struct usb_ctrlrequest dr[NUM_CONTROL_URBS]; | ||
180 | |||
181 | unsigned char * interrupt_in_buffer; | ||
182 | struct urb * interrupt_in_urb; | ||
183 | __u8 interrupt_in_endpointAddress; | ||
184 | __u8 interrupt_in_interval; | ||
185 | int interrupt_in_buffer_size; | ||
186 | |||
187 | unsigned char * bulk_in_buffer; | ||
188 | struct urb * read_urb; | ||
189 | __u8 bulk_in_endpointAddress; | ||
190 | int bulk_in_buffer_size; | ||
191 | |||
192 | int bulk_out_buffer_size; | ||
193 | __u8 bulk_out_endpointAddress; | ||
194 | |||
195 | wait_queue_head_t write_wait; | ||
196 | |||
197 | struct work_struct work; /* work queue entry for line discipline waking up */ | ||
198 | |||
199 | unsigned int int_packet_pos; | ||
200 | unsigned char int_buffer[EVENT_BUFFER_SIZE]; | ||
201 | unsigned int bulk_packet_pos; | ||
202 | unsigned char bulk_buffer[ACL_BUFFER_SIZE]; /* 64k preallocated, fix? */ | ||
203 | struct semaphore lock; | ||
204 | }; | ||
205 | |||
206 | |||
207 | /* local function prototypes */ | ||
208 | static int bluetooth_open (struct tty_struct *tty, struct file *filp); | ||
209 | static void bluetooth_close (struct tty_struct *tty, struct file *filp); | ||
210 | static int bluetooth_write (struct tty_struct *tty, const unsigned char *buf, int count); | ||
211 | static int bluetooth_write_room (struct tty_struct *tty); | ||
212 | static int bluetooth_chars_in_buffer (struct tty_struct *tty); | ||
213 | static void bluetooth_throttle (struct tty_struct *tty); | ||
214 | static void bluetooth_unthrottle (struct tty_struct *tty); | ||
215 | static int bluetooth_ioctl (struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg); | ||
216 | static void bluetooth_set_termios (struct tty_struct *tty, struct termios *old); | ||
217 | |||
218 | static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs); | ||
219 | static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs); | ||
220 | static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs); | ||
221 | static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs); | ||
222 | |||
223 | static int usb_bluetooth_probe (struct usb_interface *intf, | ||
224 | const struct usb_device_id *id); | ||
225 | static void usb_bluetooth_disconnect (struct usb_interface *intf); | ||
226 | |||
227 | |||
228 | static struct usb_device_id usb_bluetooth_ids [] = { | ||
229 | { USB_DEVICE_INFO(WIRELESS_CLASS_CODE, RF_SUBCLASS_CODE, BLUETOOTH_PROGRAMMING_PROTOCOL_CODE) }, | ||
230 | { } /* Terminating entry */ | ||
231 | }; | ||
232 | |||
233 | MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids); | ||
234 | |||
235 | static struct usb_driver usb_bluetooth_driver = { | ||
236 | .owner = THIS_MODULE, | ||
237 | .name = "bluetty", | ||
238 | .probe = usb_bluetooth_probe, | ||
239 | .disconnect = usb_bluetooth_disconnect, | ||
240 | .id_table = usb_bluetooth_ids, | ||
241 | }; | ||
242 | |||
243 | static struct tty_driver *bluetooth_tty_driver; | ||
244 | static struct usb_bluetooth *bluetooth_table[BLUETOOTH_TTY_MINORS]; | ||
245 | |||
246 | |||
247 | static inline int bluetooth_paranoia_check (struct usb_bluetooth *bluetooth, const char *function) | ||
248 | { | ||
249 | if (!bluetooth) { | ||
250 | dbg("%s - bluetooth == NULL", function); | ||
251 | return -1; | ||
252 | } | ||
253 | if (bluetooth->magic != USB_BLUETOOTH_MAGIC) { | ||
254 | dbg("%s - bad magic number for bluetooth", function); | ||
255 | return -1; | ||
256 | } | ||
257 | |||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | |||
262 | static inline struct usb_bluetooth* get_usb_bluetooth (struct usb_bluetooth *bluetooth, const char *function) | ||
263 | { | ||
264 | if (!bluetooth || | ||
265 | bluetooth_paranoia_check (bluetooth, function)) { | ||
266 | /* then say that we don't have a valid usb_bluetooth thing, which will | ||
267 | * end up generating -ENODEV return values */ | ||
268 | return NULL; | ||
269 | } | ||
270 | |||
271 | return bluetooth; | ||
272 | } | ||
273 | |||
274 | |||
275 | static inline struct usb_bluetooth *get_bluetooth_by_index (int index) | ||
276 | { | ||
277 | return bluetooth_table[index]; | ||
278 | } | ||
279 | |||
280 | |||
281 | static int bluetooth_ctrl_msg (struct usb_bluetooth *bluetooth, int request, int value, const unsigned char *buf, int len) | ||
282 | { | ||
283 | struct urb *urb = NULL; | ||
284 | struct usb_ctrlrequest *dr = NULL; | ||
285 | int i; | ||
286 | int status; | ||
287 | |||
288 | dbg ("%s", __FUNCTION__); | ||
289 | |||
290 | /* try to find a free urb in our list */ | ||
291 | for (i = 0; i < NUM_CONTROL_URBS; ++i) { | ||
292 | if (bluetooth->control_urb_pool[i]->status != -EINPROGRESS) { | ||
293 | urb = bluetooth->control_urb_pool[i]; | ||
294 | dr = &bluetooth->dr[i]; | ||
295 | break; | ||
296 | } | ||
297 | } | ||
298 | if (urb == NULL) { | ||
299 | dbg ("%s - no free urbs", __FUNCTION__); | ||
300 | return -ENOMEM; | ||
301 | } | ||
302 | |||
303 | /* keep increasing the urb transfer buffer to fit the size of the message */ | ||
304 | if (urb->transfer_buffer == NULL) { | ||
305 | urb->transfer_buffer = kmalloc (len, GFP_KERNEL); | ||
306 | if (urb->transfer_buffer == NULL) { | ||
307 | err ("%s - out of memory", __FUNCTION__); | ||
308 | return -ENOMEM; | ||
309 | } | ||
310 | } | ||
311 | if (urb->transfer_buffer_length < len) { | ||
312 | kfree (urb->transfer_buffer); | ||
313 | urb->transfer_buffer = kmalloc (len, GFP_KERNEL); | ||
314 | if (urb->transfer_buffer == NULL) { | ||
315 | err ("%s - out of memory", __FUNCTION__); | ||
316 | return -ENOMEM; | ||
317 | } | ||
318 | } | ||
319 | memcpy (urb->transfer_buffer, buf, len); | ||
320 | |||
321 | dr->bRequestType= BLUETOOTH_CONTROL_REQUEST_TYPE; | ||
322 | dr->bRequest = request; | ||
323 | dr->wValue = cpu_to_le16((u16) value); | ||
324 | dr->wIndex = cpu_to_le16((u16) bluetooth->control_out_bInterfaceNum); | ||
325 | dr->wLength = cpu_to_le16((u16) len); | ||
326 | |||
327 | usb_fill_control_urb (urb, bluetooth->dev, usb_sndctrlpipe(bluetooth->dev, 0), | ||
328 | (unsigned char*)dr, urb->transfer_buffer, len, bluetooth_ctrl_callback, bluetooth); | ||
329 | |||
330 | /* send it down the pipe */ | ||
331 | status = usb_submit_urb(urb, GFP_KERNEL); | ||
332 | if (status) | ||
333 | dbg("%s - usb_submit_urb(control) failed with status = %d", __FUNCTION__, status); | ||
334 | |||
335 | return status; | ||
336 | } | ||
337 | |||
338 | |||
339 | |||
340 | |||
341 | |||
342 | /***************************************************************************** | ||
343 | * Driver tty interface functions | ||
344 | *****************************************************************************/ | ||
345 | static int bluetooth_open (struct tty_struct *tty, struct file * filp) | ||
346 | { | ||
347 | struct usb_bluetooth *bluetooth; | ||
348 | int result; | ||
349 | |||
350 | dbg("%s", __FUNCTION__); | ||
351 | |||
352 | /* initialize the pointer incase something fails */ | ||
353 | tty->driver_data = NULL; | ||
354 | |||
355 | /* get the bluetooth object associated with this tty pointer */ | ||
356 | bluetooth = get_bluetooth_by_index (tty->index); | ||
357 | |||
358 | if (bluetooth_paranoia_check (bluetooth, __FUNCTION__)) { | ||
359 | return -ENODEV; | ||
360 | } | ||
361 | |||
362 | down (&bluetooth->lock); | ||
363 | |||
364 | ++bluetooth->open_count; | ||
365 | if (bluetooth->open_count == 1) { | ||
366 | /* set up our structure making the tty driver remember our object, and us it */ | ||
367 | tty->driver_data = bluetooth; | ||
368 | bluetooth->tty = tty; | ||
369 | |||
370 | /* force low_latency on so that our tty_push actually forces the data through, | ||
371 | * otherwise it is scheduled, and with high data rates (like with OHCI) data | ||
372 | * can get lost. */ | ||
373 | bluetooth->tty->low_latency = 1; | ||
374 | |||
375 | /* Reset the packet position counters */ | ||
376 | bluetooth->int_packet_pos = 0; | ||
377 | bluetooth->bulk_packet_pos = 0; | ||
378 | |||
379 | #ifndef BTBUGGYHARDWARE | ||
380 | /* Start reading from the device */ | ||
381 | usb_fill_bulk_urb (bluetooth->read_urb, bluetooth->dev, | ||
382 | usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress), | ||
383 | bluetooth->bulk_in_buffer, | ||
384 | bluetooth->bulk_in_buffer_size, | ||
385 | bluetooth_read_bulk_callback, bluetooth); | ||
386 | result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL); | ||
387 | if (result) | ||
388 | dbg("%s - usb_submit_urb(read bulk) failed with status %d", __FUNCTION__, result); | ||
389 | #endif | ||
390 | usb_fill_int_urb (bluetooth->interrupt_in_urb, bluetooth->dev, | ||
391 | usb_rcvintpipe(bluetooth->dev, bluetooth->interrupt_in_endpointAddress), | ||
392 | bluetooth->interrupt_in_buffer, | ||
393 | bluetooth->interrupt_in_buffer_size, | ||
394 | bluetooth_int_callback, bluetooth, | ||
395 | bluetooth->interrupt_in_interval); | ||
396 | result = usb_submit_urb(bluetooth->interrupt_in_urb, GFP_KERNEL); | ||
397 | if (result) | ||
398 | dbg("%s - usb_submit_urb(interrupt in) failed with status %d", __FUNCTION__, result); | ||
399 | } | ||
400 | |||
401 | up(&bluetooth->lock); | ||
402 | |||
403 | return 0; | ||
404 | } | ||
405 | |||
406 | |||
407 | static void bluetooth_close (struct tty_struct *tty, struct file * filp) | ||
408 | { | ||
409 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
410 | |||
411 | if (!bluetooth) { | ||
412 | return; | ||
413 | } | ||
414 | |||
415 | dbg("%s", __FUNCTION__); | ||
416 | |||
417 | if (!bluetooth->open_count) { | ||
418 | dbg ("%s - device not opened", __FUNCTION__); | ||
419 | return; | ||
420 | } | ||
421 | |||
422 | down (&bluetooth->lock); | ||
423 | |||
424 | --bluetooth->open_count; | ||
425 | if (bluetooth->open_count <= 0) { | ||
426 | bluetooth->open_count = 0; | ||
427 | |||
428 | /* shutdown any in-flight urbs that we know about */ | ||
429 | usb_kill_urb (bluetooth->read_urb); | ||
430 | usb_kill_urb (bluetooth->interrupt_in_urb); | ||
431 | } | ||
432 | up(&bluetooth->lock); | ||
433 | } | ||
434 | |||
435 | |||
436 | static int bluetooth_write (struct tty_struct * tty, const unsigned char *buf, int count) | ||
437 | { | ||
438 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
439 | struct urb *urb = NULL; | ||
440 | unsigned char *temp_buffer = NULL; | ||
441 | const unsigned char *current_buffer; | ||
442 | unsigned char *urb_buffer; | ||
443 | int i; | ||
444 | int retval = 0; | ||
445 | |||
446 | if (!bluetooth) { | ||
447 | return -ENODEV; | ||
448 | } | ||
449 | |||
450 | dbg("%s - %d byte(s)", __FUNCTION__, count); | ||
451 | |||
452 | if (!bluetooth->open_count) { | ||
453 | dbg ("%s - device not opened", __FUNCTION__); | ||
454 | return -EINVAL; | ||
455 | } | ||
456 | |||
457 | if (count == 0) { | ||
458 | dbg("%s - write request of 0 bytes", __FUNCTION__); | ||
459 | return 0; | ||
460 | } | ||
461 | if (count == 1) { | ||
462 | dbg("%s - write request only included type %d", __FUNCTION__, buf[0]); | ||
463 | return 1; | ||
464 | } | ||
465 | |||
466 | #ifdef DEBUG | ||
467 | printk (KERN_DEBUG __FILE__ ": %s - length = %d, data = ", __FUNCTION__, count); | ||
468 | for (i = 0; i < count; ++i) { | ||
469 | printk ("%.2x ", buf[i]); | ||
470 | } | ||
471 | printk ("\n"); | ||
472 | #endif | ||
473 | |||
474 | current_buffer = buf; | ||
475 | |||
476 | switch (*current_buffer) { | ||
477 | /* First byte indicates the type of packet */ | ||
478 | case CMD_PKT: | ||
479 | /* dbg("%s- Send cmd_pkt len:%d", __FUNCTION__, count);*/ | ||
480 | |||
481 | retval = bluetooth_ctrl_msg (bluetooth, 0x00, 0x00, ¤t_buffer[1], count-1); | ||
482 | if (retval) { | ||
483 | goto exit; | ||
484 | } | ||
485 | retval = count; | ||
486 | break; | ||
487 | |||
488 | case ACL_PKT: | ||
489 | ++current_buffer; | ||
490 | --count; | ||
491 | |||
492 | urb_buffer = kmalloc (count, GFP_ATOMIC); | ||
493 | if (!urb_buffer) { | ||
494 | dev_err(&bluetooth->dev->dev, "out of memory\n"); | ||
495 | retval = -ENOMEM; | ||
496 | goto exit; | ||
497 | } | ||
498 | |||
499 | urb = usb_alloc_urb(0, GFP_ATOMIC); | ||
500 | if (!urb) { | ||
501 | dev_err(&bluetooth->dev->dev, "no more free urbs\n"); | ||
502 | kfree(urb_buffer); | ||
503 | retval = -ENOMEM; | ||
504 | goto exit; | ||
505 | } | ||
506 | memcpy (urb_buffer, current_buffer, count); | ||
507 | |||
508 | /* build up our urb */ | ||
509 | usb_fill_bulk_urb(urb, bluetooth->dev, | ||
510 | usb_sndbulkpipe(bluetooth->dev, | ||
511 | bluetooth->bulk_out_endpointAddress), | ||
512 | urb_buffer, | ||
513 | count, | ||
514 | bluetooth_write_bulk_callback, | ||
515 | bluetooth); | ||
516 | |||
517 | |||
518 | /* send it down the pipe */ | ||
519 | retval = usb_submit_urb(urb, GFP_KERNEL); | ||
520 | if (retval) { | ||
521 | dbg("%s - usb_submit_urb(write bulk) failed with error = %d", __FUNCTION__, retval); | ||
522 | goto exit; | ||
523 | } | ||
524 | |||
525 | /* we are done with this urb, so let the host driver | ||
526 | * really free it when it is finished with it */ | ||
527 | usb_free_urb (urb); | ||
528 | retval = count + 1; | ||
529 | break; | ||
530 | |||
531 | default : | ||
532 | dbg("%s - unsupported (at this time) write type", __FUNCTION__); | ||
533 | retval = -EINVAL; | ||
534 | break; | ||
535 | } | ||
536 | |||
537 | exit: | ||
538 | kfree (temp_buffer); | ||
539 | |||
540 | return retval; | ||
541 | } | ||
542 | |||
543 | |||
544 | static int bluetooth_write_room (struct tty_struct *tty) | ||
545 | { | ||
546 | dbg("%s", __FUNCTION__); | ||
547 | |||
548 | /* | ||
549 | * We really can take anything the user throws at us | ||
550 | * but let's pick a nice big number to tell the tty | ||
551 | * layer that we have lots of free space | ||
552 | */ | ||
553 | return 2048; | ||
554 | } | ||
555 | |||
556 | |||
557 | static int bluetooth_chars_in_buffer (struct tty_struct *tty) | ||
558 | { | ||
559 | dbg("%s", __FUNCTION__); | ||
560 | |||
561 | /* | ||
562 | * We can't really account for how much data we | ||
563 | * have sent out, but hasn't made it through to the | ||
564 | * device, so just tell the tty layer that everything | ||
565 | * is flushed. | ||
566 | */ | ||
567 | return 0; | ||
568 | } | ||
569 | |||
570 | |||
571 | static void bluetooth_throttle (struct tty_struct * tty) | ||
572 | { | ||
573 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
574 | |||
575 | if (!bluetooth) { | ||
576 | return; | ||
577 | } | ||
578 | |||
579 | dbg("%s", __FUNCTION__); | ||
580 | |||
581 | if (!bluetooth->open_count) { | ||
582 | dbg ("%s - device not open", __FUNCTION__); | ||
583 | return; | ||
584 | } | ||
585 | |||
586 | dbg("%s unsupported (at this time)", __FUNCTION__); | ||
587 | |||
588 | return; | ||
589 | } | ||
590 | |||
591 | |||
592 | static void bluetooth_unthrottle (struct tty_struct * tty) | ||
593 | { | ||
594 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
595 | |||
596 | if (!bluetooth) { | ||
597 | return; | ||
598 | } | ||
599 | |||
600 | dbg("%s", __FUNCTION__); | ||
601 | |||
602 | if (!bluetooth->open_count) { | ||
603 | dbg ("%s - device not open", __FUNCTION__); | ||
604 | return; | ||
605 | } | ||
606 | |||
607 | dbg("%s unsupported (at this time)", __FUNCTION__); | ||
608 | } | ||
609 | |||
610 | |||
611 | static int bluetooth_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg) | ||
612 | { | ||
613 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
614 | |||
615 | if (!bluetooth) { | ||
616 | return -ENODEV; | ||
617 | } | ||
618 | |||
619 | dbg("%s - cmd 0x%.4x", __FUNCTION__, cmd); | ||
620 | |||
621 | if (!bluetooth->open_count) { | ||
622 | dbg ("%s - device not open", __FUNCTION__); | ||
623 | return -ENODEV; | ||
624 | } | ||
625 | |||
626 | /* FIXME!!! */ | ||
627 | return -ENOIOCTLCMD; | ||
628 | } | ||
629 | |||
630 | |||
631 | static void bluetooth_set_termios (struct tty_struct *tty, struct termios * old) | ||
632 | { | ||
633 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
634 | |||
635 | if (!bluetooth) { | ||
636 | return; | ||
637 | } | ||
638 | |||
639 | dbg("%s", __FUNCTION__); | ||
640 | |||
641 | if (!bluetooth->open_count) { | ||
642 | dbg ("%s - device not open", __FUNCTION__); | ||
643 | return; | ||
644 | } | ||
645 | |||
646 | /* FIXME!!! */ | ||
647 | |||
648 | return; | ||
649 | } | ||
650 | |||
651 | |||
652 | #ifdef BTBUGGYHARDWARE | ||
653 | void btusb_enable_bulk_read(struct tty_struct *tty){ | ||
654 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
655 | int result; | ||
656 | |||
657 | if (!bluetooth) { | ||
658 | return; | ||
659 | } | ||
660 | |||
661 | dbg("%s", __FUNCTION__); | ||
662 | |||
663 | if (!bluetooth->open_count) { | ||
664 | dbg ("%s - device not open", __FUNCTION__); | ||
665 | return; | ||
666 | } | ||
667 | |||
668 | if (bluetooth->read_urb) { | ||
669 | usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev, | ||
670 | usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress), | ||
671 | bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, | ||
672 | bluetooth_read_bulk_callback, bluetooth); | ||
673 | result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL); | ||
674 | if (result) | ||
675 | err ("%s - failed submitting read urb, error %d", __FUNCTION__, result); | ||
676 | } | ||
677 | } | ||
678 | |||
679 | void btusb_disable_bulk_read(struct tty_struct *tty){ | ||
680 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)tty->driver_data, __FUNCTION__); | ||
681 | |||
682 | if (!bluetooth) { | ||
683 | return; | ||
684 | } | ||
685 | |||
686 | dbg("%s", __FUNCTION__); | ||
687 | |||
688 | if (!bluetooth->open_count) { | ||
689 | dbg ("%s - device not open", __FUNCTION__); | ||
690 | return; | ||
691 | } | ||
692 | |||
693 | if ((bluetooth->read_urb) && (bluetooth->read_urb->actual_length)) | ||
694 | usb_kill_urb(bluetooth->read_urb); | ||
695 | } | ||
696 | #endif | ||
697 | |||
698 | |||
699 | /***************************************************************************** | ||
700 | * urb callback functions | ||
701 | *****************************************************************************/ | ||
702 | |||
703 | |||
704 | static void bluetooth_int_callback (struct urb *urb, struct pt_regs *regs) | ||
705 | { | ||
706 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__); | ||
707 | unsigned char *data = urb->transfer_buffer; | ||
708 | unsigned int i; | ||
709 | unsigned int count = urb->actual_length; | ||
710 | unsigned int packet_size; | ||
711 | int status; | ||
712 | |||
713 | dbg("%s", __FUNCTION__); | ||
714 | |||
715 | if (!bluetooth) { | ||
716 | dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__); | ||
717 | return; | ||
718 | } | ||
719 | |||
720 | switch (urb->status) { | ||
721 | case 0: | ||
722 | /* success */ | ||
723 | break; | ||
724 | case -ECONNRESET: | ||
725 | case -ENOENT: | ||
726 | case -ESHUTDOWN: | ||
727 | /* this urb is terminated, clean up */ | ||
728 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); | ||
729 | return; | ||
730 | default: | ||
731 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); | ||
732 | goto exit; | ||
733 | } | ||
734 | |||
735 | if (!count) { | ||
736 | dbg("%s - zero length int", __FUNCTION__); | ||
737 | goto exit; | ||
738 | } | ||
739 | |||
740 | |||
741 | #ifdef DEBUG | ||
742 | if (count) { | ||
743 | printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count); | ||
744 | for (i = 0; i < count; ++i) { | ||
745 | printk ("%.2x ", data[i]); | ||
746 | } | ||
747 | printk ("\n"); | ||
748 | } | ||
749 | #endif | ||
750 | |||
751 | #ifdef BTBUGGYHARDWARE | ||
752 | if ((count >= 2) && (data[0] == 0xFF) && (data[1] == 0x00)) { | ||
753 | data += 2; | ||
754 | count -= 2; | ||
755 | } | ||
756 | if (count == 0) { | ||
757 | urb->actual_length = 0; | ||
758 | goto exit; | ||
759 | } | ||
760 | #endif | ||
761 | /* We add a packet type identifier to the beginning of each | ||
762 | HCI frame. This makes the data in the tty look like a | ||
763 | serial USB devices. Each HCI frame can be broken across | ||
764 | multiple URBs so we buffer them until we have a full hci | ||
765 | packet */ | ||
766 | |||
767 | if (!bluetooth->int_packet_pos) { | ||
768 | bluetooth->int_buffer[0] = EVENT_PKT; | ||
769 | bluetooth->int_packet_pos++; | ||
770 | } | ||
771 | |||
772 | if (bluetooth->int_packet_pos + count > EVENT_BUFFER_SIZE) { | ||
773 | err("%s - exceeded EVENT_BUFFER_SIZE", __FUNCTION__); | ||
774 | bluetooth->int_packet_pos = 0; | ||
775 | goto exit; | ||
776 | } | ||
777 | |||
778 | memcpy (&bluetooth->int_buffer[bluetooth->int_packet_pos], | ||
779 | urb->transfer_buffer, count); | ||
780 | bluetooth->int_packet_pos += count; | ||
781 | urb->actual_length = 0; | ||
782 | |||
783 | if (bluetooth->int_packet_pos >= EVENT_HDR_SIZE) | ||
784 | packet_size = bluetooth->int_buffer[2]; | ||
785 | else | ||
786 | goto exit; | ||
787 | |||
788 | if (packet_size + EVENT_HDR_SIZE < bluetooth->int_packet_pos) { | ||
789 | err("%s - packet was too long", __FUNCTION__); | ||
790 | bluetooth->int_packet_pos = 0; | ||
791 | goto exit; | ||
792 | } | ||
793 | |||
794 | if (packet_size + EVENT_HDR_SIZE == bluetooth->int_packet_pos) { | ||
795 | for (i = 0; i < bluetooth->int_packet_pos; ++i) { | ||
796 | /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them */ | ||
797 | if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) { | ||
798 | tty_flip_buffer_push(bluetooth->tty); | ||
799 | } | ||
800 | tty_insert_flip_char(bluetooth->tty, bluetooth->int_buffer[i], 0); | ||
801 | } | ||
802 | tty_flip_buffer_push(bluetooth->tty); | ||
803 | |||
804 | bluetooth->int_packet_pos = 0; | ||
805 | } | ||
806 | |||
807 | exit: | ||
808 | status = usb_submit_urb (urb, GFP_ATOMIC); | ||
809 | if (status) | ||
810 | err ("%s - usb_submit_urb failed with result %d", | ||
811 | __FUNCTION__, status); | ||
812 | } | ||
813 | |||
814 | |||
815 | static void bluetooth_ctrl_callback (struct urb *urb, struct pt_regs *regs) | ||
816 | { | ||
817 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__); | ||
818 | |||
819 | dbg("%s", __FUNCTION__); | ||
820 | |||
821 | if (!bluetooth) { | ||
822 | dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__); | ||
823 | return; | ||
824 | } | ||
825 | |||
826 | if (urb->status) { | ||
827 | dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); | ||
828 | return; | ||
829 | } | ||
830 | } | ||
831 | |||
832 | |||
833 | static void bluetooth_read_bulk_callback (struct urb *urb, struct pt_regs *regs) | ||
834 | { | ||
835 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__); | ||
836 | unsigned char *data = urb->transfer_buffer; | ||
837 | unsigned int count = urb->actual_length; | ||
838 | unsigned int i; | ||
839 | unsigned int packet_size; | ||
840 | int result; | ||
841 | |||
842 | |||
843 | dbg("%s", __FUNCTION__); | ||
844 | |||
845 | if (!bluetooth) { | ||
846 | dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__); | ||
847 | return; | ||
848 | } | ||
849 | |||
850 | if (urb->status) { | ||
851 | dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status); | ||
852 | if (urb->status == -ENOENT) { | ||
853 | dbg("%s - URB canceled, won't reschedule", __FUNCTION__); | ||
854 | return; | ||
855 | } | ||
856 | goto exit; | ||
857 | } | ||
858 | |||
859 | if (!count) { | ||
860 | dbg("%s - zero length read bulk", __FUNCTION__); | ||
861 | goto exit; | ||
862 | } | ||
863 | |||
864 | #ifdef DEBUG | ||
865 | if (count) { | ||
866 | printk (KERN_DEBUG __FILE__ ": %s- length = %d, data = ", __FUNCTION__, count); | ||
867 | for (i = 0; i < count; ++i) { | ||
868 | printk ("%.2x ", data[i]); | ||
869 | } | ||
870 | printk ("\n"); | ||
871 | } | ||
872 | #endif | ||
873 | #ifdef BTBUGGYHARDWARE | ||
874 | if ((count == 4) && (data[0] == 0x00) && (data[1] == 0x00) | ||
875 | && (data[2] == 0x00) && (data[3] == 0x00)) { | ||
876 | urb->actual_length = 0; | ||
877 | usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev, | ||
878 | usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress), | ||
879 | bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, | ||
880 | bluetooth_read_bulk_callback, bluetooth); | ||
881 | result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL); | ||
882 | if (result) | ||
883 | err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result); | ||
884 | |||
885 | return; | ||
886 | } | ||
887 | #endif | ||
888 | /* We add a packet type identifier to the beginning of each | ||
889 | HCI frame. This makes the data in the tty look like a | ||
890 | serial USB devices. Each HCI frame can be broken across | ||
891 | multiple URBs so we buffer them until we have a full hci | ||
892 | packet */ | ||
893 | |||
894 | if (!bluetooth->bulk_packet_pos) { | ||
895 | bluetooth->bulk_buffer[0] = ACL_PKT; | ||
896 | bluetooth->bulk_packet_pos++; | ||
897 | } | ||
898 | |||
899 | if (bluetooth->bulk_packet_pos + count > ACL_BUFFER_SIZE) { | ||
900 | err("%s - exceeded ACL_BUFFER_SIZE", __FUNCTION__); | ||
901 | bluetooth->bulk_packet_pos = 0; | ||
902 | goto exit; | ||
903 | } | ||
904 | |||
905 | memcpy (&bluetooth->bulk_buffer[bluetooth->bulk_packet_pos], | ||
906 | urb->transfer_buffer, count); | ||
907 | bluetooth->bulk_packet_pos += count; | ||
908 | urb->actual_length = 0; | ||
909 | |||
910 | if (bluetooth->bulk_packet_pos >= ACL_HDR_SIZE) { | ||
911 | packet_size = CHAR2INT16(bluetooth->bulk_buffer[4],bluetooth->bulk_buffer[3]); | ||
912 | } else { | ||
913 | goto exit; | ||
914 | } | ||
915 | |||
916 | if (packet_size + ACL_HDR_SIZE < bluetooth->bulk_packet_pos) { | ||
917 | err("%s - packet was too long", __FUNCTION__); | ||
918 | bluetooth->bulk_packet_pos = 0; | ||
919 | goto exit; | ||
920 | } | ||
921 | |||
922 | if (packet_size + ACL_HDR_SIZE == bluetooth->bulk_packet_pos) { | ||
923 | for (i = 0; i < bluetooth->bulk_packet_pos; ++i) { | ||
924 | /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */ | ||
925 | if (bluetooth->tty->flip.count >= TTY_FLIPBUF_SIZE) { | ||
926 | tty_flip_buffer_push(bluetooth->tty); | ||
927 | } | ||
928 | tty_insert_flip_char(bluetooth->tty, bluetooth->bulk_buffer[i], 0); | ||
929 | } | ||
930 | tty_flip_buffer_push(bluetooth->tty); | ||
931 | bluetooth->bulk_packet_pos = 0; | ||
932 | } | ||
933 | |||
934 | exit: | ||
935 | if (!bluetooth || !bluetooth->open_count) | ||
936 | return; | ||
937 | |||
938 | usb_fill_bulk_urb(bluetooth->read_urb, bluetooth->dev, | ||
939 | usb_rcvbulkpipe(bluetooth->dev, bluetooth->bulk_in_endpointAddress), | ||
940 | bluetooth->bulk_in_buffer, bluetooth->bulk_in_buffer_size, | ||
941 | bluetooth_read_bulk_callback, bluetooth); | ||
942 | result = usb_submit_urb(bluetooth->read_urb, GFP_KERNEL); | ||
943 | if (result) | ||
944 | err ("%s - failed resubmitting read urb, error %d", __FUNCTION__, result); | ||
945 | |||
946 | return; | ||
947 | } | ||
948 | |||
949 | |||
950 | static void bluetooth_write_bulk_callback (struct urb *urb, struct pt_regs *regs) | ||
951 | { | ||
952 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)urb->context, __FUNCTION__); | ||
953 | |||
954 | dbg("%s", __FUNCTION__); | ||
955 | |||
956 | /* free up the transfer buffer, as usb_free_urb() does not do this */ | ||
957 | kfree(urb->transfer_buffer); | ||
958 | |||
959 | if (!bluetooth) { | ||
960 | dbg("%s - bad bluetooth pointer, exiting", __FUNCTION__); | ||
961 | return; | ||
962 | } | ||
963 | |||
964 | if (urb->status) { | ||
965 | dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status); | ||
966 | return; | ||
967 | } | ||
968 | |||
969 | /* wake up our little function to let the tty layer know that something happened */ | ||
970 | schedule_work(&bluetooth->work); | ||
971 | } | ||
972 | |||
973 | |||
974 | static void bluetooth_softint(void *private) | ||
975 | { | ||
976 | struct usb_bluetooth *bluetooth = get_usb_bluetooth ((struct usb_bluetooth *)private, __FUNCTION__); | ||
977 | |||
978 | dbg("%s", __FUNCTION__); | ||
979 | |||
980 | if (!bluetooth) | ||
981 | return; | ||
982 | |||
983 | tty_wakeup(bluetooth->tty); | ||
984 | } | ||
985 | |||
986 | |||
987 | static int usb_bluetooth_probe (struct usb_interface *intf, | ||
988 | const struct usb_device_id *id) | ||
989 | { | ||
990 | struct usb_device *dev = interface_to_usbdev (intf); | ||
991 | struct usb_bluetooth *bluetooth = NULL; | ||
992 | struct usb_host_interface *interface; | ||
993 | struct usb_endpoint_descriptor *endpoint; | ||
994 | struct usb_endpoint_descriptor *interrupt_in_endpoint[8]; | ||
995 | struct usb_endpoint_descriptor *bulk_in_endpoint[8]; | ||
996 | struct usb_endpoint_descriptor *bulk_out_endpoint[8]; | ||
997 | int control_out_endpoint; | ||
998 | |||
999 | int minor; | ||
1000 | int buffer_size; | ||
1001 | int i; | ||
1002 | int num_interrupt_in = 0; | ||
1003 | int num_bulk_in = 0; | ||
1004 | int num_bulk_out = 0; | ||
1005 | |||
1006 | interface = intf->cur_altsetting; | ||
1007 | control_out_endpoint = interface->desc.bInterfaceNumber; | ||
1008 | |||
1009 | /* find the endpoints that we need */ | ||
1010 | for (i = 0; i < interface->desc.bNumEndpoints; ++i) { | ||
1011 | endpoint = &interface->endpoint[i].desc; | ||
1012 | |||
1013 | if ((endpoint->bEndpointAddress & 0x80) && | ||
1014 | ((endpoint->bmAttributes & 3) == 0x02)) { | ||
1015 | /* we found a bulk in endpoint */ | ||
1016 | dbg("found bulk in"); | ||
1017 | bulk_in_endpoint[num_bulk_in] = endpoint; | ||
1018 | ++num_bulk_in; | ||
1019 | } | ||
1020 | |||
1021 | if (((endpoint->bEndpointAddress & 0x80) == 0x00) && | ||
1022 | ((endpoint->bmAttributes & 3) == 0x02)) { | ||
1023 | /* we found a bulk out endpoint */ | ||
1024 | dbg("found bulk out"); | ||
1025 | bulk_out_endpoint[num_bulk_out] = endpoint; | ||
1026 | ++num_bulk_out; | ||
1027 | } | ||
1028 | |||
1029 | if ((endpoint->bEndpointAddress & 0x80) && | ||
1030 | ((endpoint->bmAttributes & 3) == 0x03)) { | ||
1031 | /* we found a interrupt in endpoint */ | ||
1032 | dbg("found interrupt in"); | ||
1033 | interrupt_in_endpoint[num_interrupt_in] = endpoint; | ||
1034 | ++num_interrupt_in; | ||
1035 | } | ||
1036 | } | ||
1037 | |||
1038 | /* according to the spec, we can only have 1 bulk_in, 1 bulk_out, and 1 interrupt_in endpoints */ | ||
1039 | if ((num_bulk_in != 1) || | ||
1040 | (num_bulk_out != 1) || | ||
1041 | (num_interrupt_in != 1)) { | ||
1042 | dbg ("%s - improper number of endpoints. Bluetooth driver not bound.", __FUNCTION__); | ||
1043 | return -EIO; | ||
1044 | } | ||
1045 | |||
1046 | info("USB Bluetooth converter detected"); | ||
1047 | |||
1048 | for (minor = 0; minor < BLUETOOTH_TTY_MINORS && bluetooth_table[minor]; ++minor) | ||
1049 | ; | ||
1050 | if (bluetooth_table[minor]) { | ||
1051 | err("No more free Bluetooth devices"); | ||
1052 | return -ENODEV; | ||
1053 | } | ||
1054 | |||
1055 | if (!(bluetooth = kmalloc(sizeof(struct usb_bluetooth), GFP_KERNEL))) { | ||
1056 | err("Out of memory"); | ||
1057 | return -ENOMEM; | ||
1058 | } | ||
1059 | |||
1060 | memset(bluetooth, 0, sizeof(struct usb_bluetooth)); | ||
1061 | |||
1062 | bluetooth->magic = USB_BLUETOOTH_MAGIC; | ||
1063 | bluetooth->dev = dev; | ||
1064 | bluetooth->minor = minor; | ||
1065 | INIT_WORK(&bluetooth->work, bluetooth_softint, bluetooth); | ||
1066 | init_MUTEX(&bluetooth->lock); | ||
1067 | |||
1068 | /* record the interface number for the control out */ | ||
1069 | bluetooth->control_out_bInterfaceNum = control_out_endpoint; | ||
1070 | |||
1071 | /* create our control out urb pool */ | ||
1072 | for (i = 0; i < NUM_CONTROL_URBS; ++i) { | ||
1073 | struct urb *urb = usb_alloc_urb(0, GFP_KERNEL); | ||
1074 | if (urb == NULL) { | ||
1075 | err("No free urbs available"); | ||
1076 | goto probe_error; | ||
1077 | } | ||
1078 | urb->transfer_buffer = NULL; | ||
1079 | bluetooth->control_urb_pool[i] = urb; | ||
1080 | } | ||
1081 | |||
1082 | /* set up the endpoint information */ | ||
1083 | endpoint = bulk_in_endpoint[0]; | ||
1084 | bluetooth->read_urb = usb_alloc_urb (0, GFP_KERNEL); | ||
1085 | if (!bluetooth->read_urb) { | ||
1086 | err("No free urbs available"); | ||
1087 | goto probe_error; | ||
1088 | } | ||
1089 | bluetooth->bulk_in_buffer_size = buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); | ||
1090 | bluetooth->bulk_in_endpointAddress = endpoint->bEndpointAddress; | ||
1091 | bluetooth->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL); | ||
1092 | if (!bluetooth->bulk_in_buffer) { | ||
1093 | err("Couldn't allocate bulk_in_buffer"); | ||
1094 | goto probe_error; | ||
1095 | } | ||
1096 | usb_fill_bulk_urb(bluetooth->read_urb, dev, usb_rcvbulkpipe(dev, endpoint->bEndpointAddress), | ||
1097 | bluetooth->bulk_in_buffer, buffer_size, bluetooth_read_bulk_callback, bluetooth); | ||
1098 | |||
1099 | endpoint = bulk_out_endpoint[0]; | ||
1100 | bluetooth->bulk_out_endpointAddress = endpoint->bEndpointAddress; | ||
1101 | bluetooth->bulk_out_buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2; | ||
1102 | |||
1103 | endpoint = interrupt_in_endpoint[0]; | ||
1104 | bluetooth->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); | ||
1105 | if (!bluetooth->interrupt_in_urb) { | ||
1106 | err("No free urbs available"); | ||
1107 | goto probe_error; | ||
1108 | } | ||
1109 | bluetooth->interrupt_in_buffer_size = buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); | ||
1110 | bluetooth->interrupt_in_endpointAddress = endpoint->bEndpointAddress; | ||
1111 | bluetooth->interrupt_in_interval = endpoint->bInterval; | ||
1112 | bluetooth->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL); | ||
1113 | if (!bluetooth->interrupt_in_buffer) { | ||
1114 | err("Couldn't allocate interrupt_in_buffer"); | ||
1115 | goto probe_error; | ||
1116 | } | ||
1117 | usb_fill_int_urb(bluetooth->interrupt_in_urb, dev, usb_rcvintpipe(dev, endpoint->bEndpointAddress), | ||
1118 | bluetooth->interrupt_in_buffer, buffer_size, bluetooth_int_callback, | ||
1119 | bluetooth, endpoint->bInterval); | ||
1120 | |||
1121 | /* initialize the devfs nodes for this device and let the user know what bluetooths we are bound to */ | ||
1122 | tty_register_device (bluetooth_tty_driver, minor, &intf->dev); | ||
1123 | info("Bluetooth converter now attached to ttyUB%d (or usb/ttub/%d for devfs)", minor, minor); | ||
1124 | |||
1125 | bluetooth_table[minor] = bluetooth; | ||
1126 | |||
1127 | /* success */ | ||
1128 | usb_set_intfdata (intf, bluetooth); | ||
1129 | return 0; | ||
1130 | |||
1131 | probe_error: | ||
1132 | if (bluetooth->read_urb) | ||
1133 | usb_free_urb (bluetooth->read_urb); | ||
1134 | if (bluetooth->bulk_in_buffer) | ||
1135 | kfree (bluetooth->bulk_in_buffer); | ||
1136 | if (bluetooth->interrupt_in_urb) | ||
1137 | usb_free_urb (bluetooth->interrupt_in_urb); | ||
1138 | if (bluetooth->interrupt_in_buffer) | ||
1139 | kfree (bluetooth->interrupt_in_buffer); | ||
1140 | for (i = 0; i < NUM_CONTROL_URBS; ++i) | ||
1141 | if (bluetooth->control_urb_pool[i]) { | ||
1142 | if (bluetooth->control_urb_pool[i]->transfer_buffer) | ||
1143 | kfree (bluetooth->control_urb_pool[i]->transfer_buffer); | ||
1144 | usb_free_urb (bluetooth->control_urb_pool[i]); | ||
1145 | } | ||
1146 | |||
1147 | bluetooth_table[minor] = NULL; | ||
1148 | |||
1149 | /* free up any memory that we allocated */ | ||
1150 | kfree (bluetooth); | ||
1151 | return -EIO; | ||
1152 | } | ||
1153 | |||
1154 | |||
1155 | static void usb_bluetooth_disconnect(struct usb_interface *intf) | ||
1156 | { | ||
1157 | struct usb_bluetooth *bluetooth = usb_get_intfdata (intf); | ||
1158 | int i; | ||
1159 | |||
1160 | usb_set_intfdata (intf, NULL); | ||
1161 | if (bluetooth) { | ||
1162 | if ((bluetooth->open_count) && (bluetooth->tty)) | ||
1163 | tty_hangup(bluetooth->tty); | ||
1164 | |||
1165 | bluetooth->open_count = 0; | ||
1166 | |||
1167 | if (bluetooth->read_urb) { | ||
1168 | usb_kill_urb (bluetooth->read_urb); | ||
1169 | usb_free_urb (bluetooth->read_urb); | ||
1170 | } | ||
1171 | if (bluetooth->bulk_in_buffer) | ||
1172 | kfree (bluetooth->bulk_in_buffer); | ||
1173 | |||
1174 | if (bluetooth->interrupt_in_urb) { | ||
1175 | usb_kill_urb (bluetooth->interrupt_in_urb); | ||
1176 | usb_free_urb (bluetooth->interrupt_in_urb); | ||
1177 | } | ||
1178 | if (bluetooth->interrupt_in_buffer) | ||
1179 | kfree (bluetooth->interrupt_in_buffer); | ||
1180 | |||
1181 | tty_unregister_device (bluetooth_tty_driver, bluetooth->minor); | ||
1182 | |||
1183 | for (i = 0; i < NUM_CONTROL_URBS; ++i) { | ||
1184 | if (bluetooth->control_urb_pool[i]) { | ||
1185 | usb_kill_urb (bluetooth->control_urb_pool[i]); | ||
1186 | if (bluetooth->control_urb_pool[i]->transfer_buffer) | ||
1187 | kfree (bluetooth->control_urb_pool[i]->transfer_buffer); | ||
1188 | usb_free_urb (bluetooth->control_urb_pool[i]); | ||
1189 | } | ||
1190 | } | ||
1191 | |||
1192 | info("Bluetooth converter now disconnected from ttyUB%d", bluetooth->minor); | ||
1193 | |||
1194 | bluetooth_table[bluetooth->minor] = NULL; | ||
1195 | |||
1196 | /* free up any memory that we allocated */ | ||
1197 | kfree (bluetooth); | ||
1198 | } else { | ||
1199 | info("device disconnected"); | ||
1200 | } | ||
1201 | } | ||
1202 | |||
1203 | static struct tty_operations bluetooth_ops = { | ||
1204 | .open = bluetooth_open, | ||
1205 | .close = bluetooth_close, | ||
1206 | .write = bluetooth_write, | ||
1207 | .write_room = bluetooth_write_room, | ||
1208 | .ioctl = bluetooth_ioctl, | ||
1209 | .set_termios = bluetooth_set_termios, | ||
1210 | .throttle = bluetooth_throttle, | ||
1211 | .unthrottle = bluetooth_unthrottle, | ||
1212 | .chars_in_buffer = bluetooth_chars_in_buffer, | ||
1213 | }; | ||
1214 | |||
1215 | static int usb_bluetooth_init(void) | ||
1216 | { | ||
1217 | int i; | ||
1218 | int result; | ||
1219 | |||
1220 | /* Initialize our global data */ | ||
1221 | for (i = 0; i < BLUETOOTH_TTY_MINORS; ++i) { | ||
1222 | bluetooth_table[i] = NULL; | ||
1223 | } | ||
1224 | |||
1225 | info ("USB Bluetooth support registered"); | ||
1226 | |||
1227 | bluetooth_tty_driver = alloc_tty_driver(BLUETOOTH_TTY_MINORS); | ||
1228 | if (!bluetooth_tty_driver) | ||
1229 | return -ENOMEM; | ||
1230 | |||
1231 | bluetooth_tty_driver->owner = THIS_MODULE; | ||
1232 | bluetooth_tty_driver->driver_name = "usb-bluetooth"; | ||
1233 | bluetooth_tty_driver->name = "ttyUB"; | ||
1234 | bluetooth_tty_driver->devfs_name = "usb/ttub/"; | ||
1235 | bluetooth_tty_driver->major = BLUETOOTH_TTY_MAJOR; | ||
1236 | bluetooth_tty_driver->minor_start = 0; | ||
1237 | bluetooth_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; | ||
1238 | bluetooth_tty_driver->subtype = SERIAL_TYPE_NORMAL; | ||
1239 | bluetooth_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS; | ||
1240 | bluetooth_tty_driver->init_termios = tty_std_termios; | ||
1241 | bluetooth_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; | ||
1242 | tty_set_operations(bluetooth_tty_driver, &bluetooth_ops); | ||
1243 | if (tty_register_driver (bluetooth_tty_driver)) { | ||
1244 | err("%s - failed to register tty driver", __FUNCTION__); | ||
1245 | put_tty_driver(bluetooth_tty_driver); | ||
1246 | return -1; | ||
1247 | } | ||
1248 | |||
1249 | /* register the USB driver */ | ||
1250 | result = usb_register(&usb_bluetooth_driver); | ||
1251 | if (result < 0) { | ||
1252 | tty_unregister_driver(bluetooth_tty_driver); | ||
1253 | put_tty_driver(bluetooth_tty_driver); | ||
1254 | err("usb_register failed for the USB bluetooth driver. Error number %d", result); | ||
1255 | return -1; | ||
1256 | } | ||
1257 | |||
1258 | info(DRIVER_DESC " " DRIVER_VERSION); | ||
1259 | |||
1260 | return 0; | ||
1261 | } | ||
1262 | |||
1263 | |||
1264 | static void usb_bluetooth_exit(void) | ||
1265 | { | ||
1266 | usb_deregister(&usb_bluetooth_driver); | ||
1267 | tty_unregister_driver(bluetooth_tty_driver); | ||
1268 | put_tty_driver(bluetooth_tty_driver); | ||
1269 | } | ||
1270 | |||
1271 | |||
1272 | module_init(usb_bluetooth_init); | ||
1273 | module_exit(usb_bluetooth_exit); | ||
1274 | |||
1275 | /* Module information */ | ||
1276 | MODULE_AUTHOR( DRIVER_AUTHOR ); | ||
1277 | MODULE_DESCRIPTION( DRIVER_DESC ); | ||
1278 | MODULE_LICENSE("GPL"); | ||
1279 | |||
diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c new file mode 100644 index 000000000000..6d1f9b6aecff --- /dev/null +++ b/drivers/usb/class/cdc-acm.c | |||
@@ -0,0 +1,942 @@ | |||
1 | /* | ||
2 | * cdc-acm.c | ||
3 | * | ||
4 | * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de> | ||
5 | * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> | ||
6 | * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com> | ||
7 | * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> | ||
8 | * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name> | ||
9 | * | ||
10 | * USB Abstract Control Model driver for USB modems and ISDN adapters | ||
11 | * | ||
12 | * Sponsored by SuSE | ||
13 | * | ||
14 | * ChangeLog: | ||
15 | * v0.9 - thorough cleaning, URBification, almost a rewrite | ||
16 | * v0.10 - some more cleanups | ||
17 | * v0.11 - fixed flow control, read error doesn't stop reads | ||
18 | * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced | ||
19 | * v0.13 - added termios, added hangup | ||
20 | * v0.14 - sized down struct acm | ||
21 | * v0.15 - fixed flow control again - characters could be lost | ||
22 | * v0.16 - added code for modems with swapped data and control interfaces | ||
23 | * v0.17 - added new style probing | ||
24 | * v0.18 - fixed new style probing for devices with more configurations | ||
25 | * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan) | ||
26 | * v0.20 - switched to probing on interface (rather than device) class | ||
27 | * v0.21 - revert to probing on device for devices with multiple configs | ||
28 | * v0.22 - probe only the control interface. if usbcore doesn't choose the | ||
29 | * config we want, sysadmin changes bConfigurationValue in sysfs. | ||
30 | * v0.23 - use softirq for rx processing, as needed by tty layer | ||
31 | * v0.24 - change probe method to evaluate CDC union descriptor | ||
32 | */ | ||
33 | |||
34 | /* | ||
35 | * This program is free software; you can redistribute it and/or modify | ||
36 | * it under the terms of the GNU General Public License as published by | ||
37 | * the Free Software Foundation; either version 2 of the License, or | ||
38 | * (at your option) any later version. | ||
39 | * | ||
40 | * This program is distributed in the hope that it will be useful, | ||
41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
43 | * GNU General Public License for more details. | ||
44 | * | ||
45 | * You should have received a copy of the GNU General Public License | ||
46 | * along with this program; if not, write to the Free Software | ||
47 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
48 | */ | ||
49 | |||
50 | #undef DEBUG | ||
51 | |||
52 | #include <linux/kernel.h> | ||
53 | #include <linux/errno.h> | ||
54 | #include <linux/init.h> | ||
55 | #include <linux/slab.h> | ||
56 | #include <linux/tty.h> | ||
57 | #include <linux/tty_driver.h> | ||
58 | #include <linux/tty_flip.h> | ||
59 | #include <linux/module.h> | ||
60 | #include <linux/smp_lock.h> | ||
61 | #include <asm/uaccess.h> | ||
62 | #include <linux/usb.h> | ||
63 | #include <linux/usb_cdc.h> | ||
64 | #include <asm/byteorder.h> | ||
65 | #include <asm/unaligned.h> | ||
66 | |||
67 | #include "cdc-acm.h" | ||
68 | |||
69 | /* | ||
70 | * Version Information | ||
71 | */ | ||
72 | #define DRIVER_VERSION "v0.23" | ||
73 | #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik" | ||
74 | #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters" | ||
75 | |||
76 | static struct usb_driver acm_driver; | ||
77 | static struct tty_driver *acm_tty_driver; | ||
78 | static struct acm *acm_table[ACM_TTY_MINORS]; | ||
79 | |||
80 | static DECLARE_MUTEX(open_sem); | ||
81 | |||
82 | #define ACM_READY(acm) (acm && acm->dev && acm->used) | ||
83 | |||
84 | /* | ||
85 | * Functions for ACM control messages. | ||
86 | */ | ||
87 | |||
88 | static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len) | ||
89 | { | ||
90 | int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), | ||
91 | request, USB_RT_ACM, value, | ||
92 | acm->control->altsetting[0].desc.bInterfaceNumber, | ||
93 | buf, len, 5000); | ||
94 | dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval); | ||
95 | return retval < 0 ? retval : 0; | ||
96 | } | ||
97 | |||
98 | /* devices aren't required to support these requests. | ||
99 | * the cdc acm descriptor tells whether they do... | ||
100 | */ | ||
101 | #define acm_set_control(acm, control) \ | ||
102 | acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0) | ||
103 | #define acm_set_line(acm, line) \ | ||
104 | acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) | ||
105 | #define acm_send_break(acm, ms) \ | ||
106 | acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0) | ||
107 | |||
108 | /* | ||
109 | * Interrupt handlers for various ACM device responses | ||
110 | */ | ||
111 | |||
112 | /* control interface reports status changes with "interrupt" transfers */ | ||
113 | static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs) | ||
114 | { | ||
115 | struct acm *acm = urb->context; | ||
116 | struct usb_cdc_notification *dr = urb->transfer_buffer; | ||
117 | unsigned char *data; | ||
118 | int newctrl; | ||
119 | int status; | ||
120 | |||
121 | switch (urb->status) { | ||
122 | case 0: | ||
123 | /* success */ | ||
124 | break; | ||
125 | case -ECONNRESET: | ||
126 | case -ENOENT: | ||
127 | case -ESHUTDOWN: | ||
128 | /* this urb is terminated, clean up */ | ||
129 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); | ||
130 | return; | ||
131 | default: | ||
132 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); | ||
133 | goto exit; | ||
134 | } | ||
135 | |||
136 | if (!ACM_READY(acm)) | ||
137 | goto exit; | ||
138 | |||
139 | data = (unsigned char *)(dr + 1); | ||
140 | switch (dr->bNotificationType) { | ||
141 | |||
142 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: | ||
143 | |||
144 | dbg("%s network", dr->wValue ? "connected to" : "disconnected from"); | ||
145 | break; | ||
146 | |||
147 | case USB_CDC_NOTIFY_SERIAL_STATE: | ||
148 | |||
149 | newctrl = le16_to_cpu(get_unaligned((__le16 *) data)); | ||
150 | |||
151 | if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { | ||
152 | dbg("calling hangup"); | ||
153 | tty_hangup(acm->tty); | ||
154 | } | ||
155 | |||
156 | acm->ctrlin = newctrl; | ||
157 | |||
158 | dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c", | ||
159 | acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', | ||
160 | acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-', | ||
161 | acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', | ||
162 | acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); | ||
163 | |||
164 | break; | ||
165 | |||
166 | default: | ||
167 | dbg("unknown notification %d received: index %d len %d data0 %d data1 %d", | ||
168 | dr->bNotificationType, dr->wIndex, | ||
169 | dr->wLength, data[0], data[1]); | ||
170 | break; | ||
171 | } | ||
172 | exit: | ||
173 | status = usb_submit_urb (urb, GFP_ATOMIC); | ||
174 | if (status) | ||
175 | err ("%s - usb_submit_urb failed with result %d", | ||
176 | __FUNCTION__, status); | ||
177 | } | ||
178 | |||
179 | /* data interface returns incoming bytes, or we got unthrottled */ | ||
180 | static void acm_read_bulk(struct urb *urb, struct pt_regs *regs) | ||
181 | { | ||
182 | struct acm *acm = urb->context; | ||
183 | dbg("Entering acm_read_bulk with status %d\n", urb->status); | ||
184 | |||
185 | if (!ACM_READY(acm)) | ||
186 | return; | ||
187 | |||
188 | if (urb->status) | ||
189 | dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status); | ||
190 | |||
191 | /* calling tty_flip_buffer_push() in_irq() isn't allowed */ | ||
192 | tasklet_schedule(&acm->bh); | ||
193 | } | ||
194 | |||
195 | static void acm_rx_tasklet(unsigned long _acm) | ||
196 | { | ||
197 | struct acm *acm = (void *)_acm; | ||
198 | struct urb *urb = acm->readurb; | ||
199 | struct tty_struct *tty = acm->tty; | ||
200 | unsigned char *data = urb->transfer_buffer; | ||
201 | int i = 0; | ||
202 | dbg("Entering acm_rx_tasklet"); | ||
203 | |||
204 | if (urb->actual_length > 0 && !acm->throttle) { | ||
205 | for (i = 0; i < urb->actual_length && !acm->throttle; i++) { | ||
206 | /* if we insert more than TTY_FLIPBUF_SIZE characters, | ||
207 | * we drop them. */ | ||
208 | if (tty->flip.count >= TTY_FLIPBUF_SIZE) { | ||
209 | tty_flip_buffer_push(tty); | ||
210 | } | ||
211 | tty_insert_flip_char(tty, data[i], 0); | ||
212 | } | ||
213 | dbg("Handed %d bytes to tty layer", i+1); | ||
214 | tty_flip_buffer_push(tty); | ||
215 | } | ||
216 | |||
217 | spin_lock(&acm->throttle_lock); | ||
218 | if (acm->throttle) { | ||
219 | dbg("Throtteling noticed"); | ||
220 | memmove(data, data + i, urb->actual_length - i); | ||
221 | urb->actual_length -= i; | ||
222 | acm->resubmit_to_unthrottle = 1; | ||
223 | spin_unlock(&acm->throttle_lock); | ||
224 | return; | ||
225 | } | ||
226 | spin_unlock(&acm->throttle_lock); | ||
227 | |||
228 | urb->actual_length = 0; | ||
229 | urb->dev = acm->dev; | ||
230 | |||
231 | i = usb_submit_urb(urb, GFP_ATOMIC); | ||
232 | if (i) | ||
233 | dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i); | ||
234 | } | ||
235 | |||
236 | /* data interface wrote those outgoing bytes */ | ||
237 | static void acm_write_bulk(struct urb *urb, struct pt_regs *regs) | ||
238 | { | ||
239 | struct acm *acm = (struct acm *)urb->context; | ||
240 | dbg("Entering acm_write_bulk with status %d\n", urb->status); | ||
241 | |||
242 | if (!ACM_READY(acm)) | ||
243 | goto out; | ||
244 | |||
245 | if (urb->status) | ||
246 | dbg("nonzero write bulk status received: %d", urb->status); | ||
247 | |||
248 | schedule_work(&acm->work); | ||
249 | out: | ||
250 | acm->ready_for_write = 1; | ||
251 | } | ||
252 | |||
253 | static void acm_softint(void *private) | ||
254 | { | ||
255 | struct acm *acm = private; | ||
256 | dbg("Entering acm_softint.\n"); | ||
257 | |||
258 | if (!ACM_READY(acm)) | ||
259 | return; | ||
260 | tty_wakeup(acm->tty); | ||
261 | } | ||
262 | |||
263 | /* | ||
264 | * TTY handlers | ||
265 | */ | ||
266 | |||
267 | static int acm_tty_open(struct tty_struct *tty, struct file *filp) | ||
268 | { | ||
269 | struct acm *acm; | ||
270 | int rv = -EINVAL; | ||
271 | dbg("Entering acm_tty_open.\n"); | ||
272 | |||
273 | down(&open_sem); | ||
274 | |||
275 | acm = acm_table[tty->index]; | ||
276 | if (!acm || !acm->dev) | ||
277 | goto err_out; | ||
278 | else | ||
279 | rv = 0; | ||
280 | |||
281 | tty->driver_data = acm; | ||
282 | acm->tty = tty; | ||
283 | |||
284 | |||
285 | |||
286 | if (acm->used++) { | ||
287 | goto done; | ||
288 | } | ||
289 | |||
290 | acm->ctrlurb->dev = acm->dev; | ||
291 | if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { | ||
292 | dbg("usb_submit_urb(ctrl irq) failed"); | ||
293 | goto bail_out; | ||
294 | } | ||
295 | |||
296 | acm->readurb->dev = acm->dev; | ||
297 | if (usb_submit_urb(acm->readurb, GFP_KERNEL)) { | ||
298 | dbg("usb_submit_urb(read bulk) failed"); | ||
299 | goto bail_out_and_unlink; | ||
300 | } | ||
301 | |||
302 | if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS)) | ||
303 | goto full_bailout; | ||
304 | |||
305 | /* force low_latency on so that our tty_push actually forces the data through, | ||
306 | otherwise it is scheduled, and with high data rates data can get lost. */ | ||
307 | tty->low_latency = 1; | ||
308 | |||
309 | done: | ||
310 | err_out: | ||
311 | up(&open_sem); | ||
312 | return rv; | ||
313 | |||
314 | full_bailout: | ||
315 | usb_kill_urb(acm->readurb); | ||
316 | bail_out_and_unlink: | ||
317 | usb_kill_urb(acm->ctrlurb); | ||
318 | bail_out: | ||
319 | acm->used--; | ||
320 | up(&open_sem); | ||
321 | return -EIO; | ||
322 | } | ||
323 | |||
324 | static void acm_tty_close(struct tty_struct *tty, struct file *filp) | ||
325 | { | ||
326 | struct acm *acm = tty->driver_data; | ||
327 | |||
328 | if (!acm || !acm->used) | ||
329 | return; | ||
330 | |||
331 | down(&open_sem); | ||
332 | if (!--acm->used) { | ||
333 | if (acm->dev) { | ||
334 | acm_set_control(acm, acm->ctrlout = 0); | ||
335 | usb_kill_urb(acm->ctrlurb); | ||
336 | usb_kill_urb(acm->writeurb); | ||
337 | usb_kill_urb(acm->readurb); | ||
338 | } else { | ||
339 | tty_unregister_device(acm_tty_driver, acm->minor); | ||
340 | acm_table[acm->minor] = NULL; | ||
341 | usb_free_urb(acm->ctrlurb); | ||
342 | usb_free_urb(acm->readurb); | ||
343 | usb_free_urb(acm->writeurb); | ||
344 | kfree(acm); | ||
345 | } | ||
346 | } | ||
347 | up(&open_sem); | ||
348 | } | ||
349 | |||
350 | static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) | ||
351 | { | ||
352 | struct acm *acm = tty->driver_data; | ||
353 | int stat; | ||
354 | dbg("Entering acm_tty_write to write %d bytes,\n", count); | ||
355 | |||
356 | if (!ACM_READY(acm)) | ||
357 | return -EINVAL; | ||
358 | if (!acm->ready_for_write) | ||
359 | return 0; | ||
360 | if (!count) | ||
361 | return 0; | ||
362 | |||
363 | count = (count > acm->writesize) ? acm->writesize : count; | ||
364 | |||
365 | dbg("Get %d bytes...", count); | ||
366 | memcpy(acm->write_buffer, buf, count); | ||
367 | dbg(" Successfully copied.\n"); | ||
368 | |||
369 | acm->writeurb->transfer_buffer_length = count; | ||
370 | acm->writeurb->dev = acm->dev; | ||
371 | |||
372 | acm->ready_for_write = 0; | ||
373 | stat = usb_submit_urb(acm->writeurb, GFP_ATOMIC); | ||
374 | if (stat < 0) { | ||
375 | dbg("usb_submit_urb(write bulk) failed"); | ||
376 | acm->ready_for_write = 1; | ||
377 | return stat; | ||
378 | } | ||
379 | |||
380 | return count; | ||
381 | } | ||
382 | |||
383 | static int acm_tty_write_room(struct tty_struct *tty) | ||
384 | { | ||
385 | struct acm *acm = tty->driver_data; | ||
386 | if (!ACM_READY(acm)) | ||
387 | return -EINVAL; | ||
388 | return !acm->ready_for_write ? 0 : acm->writesize; | ||
389 | } | ||
390 | |||
391 | static int acm_tty_chars_in_buffer(struct tty_struct *tty) | ||
392 | { | ||
393 | struct acm *acm = tty->driver_data; | ||
394 | if (!ACM_READY(acm)) | ||
395 | return -EINVAL; | ||
396 | return !acm->ready_for_write ? acm->writeurb->transfer_buffer_length : 0; | ||
397 | } | ||
398 | |||
399 | static void acm_tty_throttle(struct tty_struct *tty) | ||
400 | { | ||
401 | struct acm *acm = tty->driver_data; | ||
402 | if (!ACM_READY(acm)) | ||
403 | return; | ||
404 | spin_lock_bh(&acm->throttle_lock); | ||
405 | acm->throttle = 1; | ||
406 | spin_unlock_bh(&acm->throttle_lock); | ||
407 | } | ||
408 | |||
409 | static void acm_tty_unthrottle(struct tty_struct *tty) | ||
410 | { | ||
411 | struct acm *acm = tty->driver_data; | ||
412 | if (!ACM_READY(acm)) | ||
413 | return; | ||
414 | spin_lock_bh(&acm->throttle_lock); | ||
415 | acm->throttle = 0; | ||
416 | spin_unlock_bh(&acm->throttle_lock); | ||
417 | if (acm->resubmit_to_unthrottle) { | ||
418 | acm->resubmit_to_unthrottle = 0; | ||
419 | acm_read_bulk(acm->readurb, NULL); | ||
420 | } | ||
421 | } | ||
422 | |||
423 | static void acm_tty_break_ctl(struct tty_struct *tty, int state) | ||
424 | { | ||
425 | struct acm *acm = tty->driver_data; | ||
426 | if (!ACM_READY(acm)) | ||
427 | return; | ||
428 | if (acm_send_break(acm, state ? 0xffff : 0)) | ||
429 | dbg("send break failed"); | ||
430 | } | ||
431 | |||
432 | static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file) | ||
433 | { | ||
434 | struct acm *acm = tty->driver_data; | ||
435 | |||
436 | if (!ACM_READY(acm)) | ||
437 | return -EINVAL; | ||
438 | |||
439 | return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | | ||
440 | (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | | ||
441 | (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | | ||
442 | (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | | ||
443 | (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | | ||
444 | TIOCM_CTS; | ||
445 | } | ||
446 | |||
447 | static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file, | ||
448 | unsigned int set, unsigned int clear) | ||
449 | { | ||
450 | struct acm *acm = tty->driver_data; | ||
451 | unsigned int newctrl; | ||
452 | |||
453 | if (!ACM_READY(acm)) | ||
454 | return -EINVAL; | ||
455 | |||
456 | newctrl = acm->ctrlout; | ||
457 | set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); | ||
458 | clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); | ||
459 | |||
460 | newctrl = (newctrl & ~clear) | set; | ||
461 | |||
462 | if (acm->ctrlout == newctrl) | ||
463 | return 0; | ||
464 | return acm_set_control(acm, acm->ctrlout = newctrl); | ||
465 | } | ||
466 | |||
467 | static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) | ||
468 | { | ||
469 | struct acm *acm = tty->driver_data; | ||
470 | |||
471 | if (!ACM_READY(acm)) | ||
472 | return -EINVAL; | ||
473 | |||
474 | return -ENOIOCTLCMD; | ||
475 | } | ||
476 | |||
477 | static __u32 acm_tty_speed[] = { | ||
478 | 0, 50, 75, 110, 134, 150, 200, 300, 600, | ||
479 | 1200, 1800, 2400, 4800, 9600, 19200, 38400, | ||
480 | 57600, 115200, 230400, 460800, 500000, 576000, | ||
481 | 921600, 1000000, 1152000, 1500000, 2000000, | ||
482 | 2500000, 3000000, 3500000, 4000000 | ||
483 | }; | ||
484 | |||
485 | static __u8 acm_tty_size[] = { | ||
486 | 5, 6, 7, 8 | ||
487 | }; | ||
488 | |||
489 | static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old) | ||
490 | { | ||
491 | struct acm *acm = tty->driver_data; | ||
492 | struct termios *termios = tty->termios; | ||
493 | struct usb_cdc_line_coding newline; | ||
494 | int newctrl = acm->ctrlout; | ||
495 | |||
496 | if (!ACM_READY(acm)) | ||
497 | return; | ||
498 | |||
499 | newline.dwDTERate = cpu_to_le32p(acm_tty_speed + | ||
500 | (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0)); | ||
501 | newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; | ||
502 | newline.bParityType = termios->c_cflag & PARENB ? | ||
503 | (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; | ||
504 | newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4]; | ||
505 | |||
506 | acm->clocal = ((termios->c_cflag & CLOCAL) != 0); | ||
507 | |||
508 | if (!newline.dwDTERate) { | ||
509 | newline.dwDTERate = acm->line.dwDTERate; | ||
510 | newctrl &= ~ACM_CTRL_DTR; | ||
511 | } else newctrl |= ACM_CTRL_DTR; | ||
512 | |||
513 | if (newctrl != acm->ctrlout) | ||
514 | acm_set_control(acm, acm->ctrlout = newctrl); | ||
515 | |||
516 | if (memcmp(&acm->line, &newline, sizeof newline)) { | ||
517 | memcpy(&acm->line, &newline, sizeof newline); | ||
518 | dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate), | ||
519 | newline.bCharFormat, newline.bParityType, | ||
520 | newline.bDataBits); | ||
521 | acm_set_line(acm, &acm->line); | ||
522 | } | ||
523 | } | ||
524 | |||
525 | /* | ||
526 | * USB probe and disconnect routines. | ||
527 | */ | ||
528 | |||
529 | static int acm_probe (struct usb_interface *intf, | ||
530 | const struct usb_device_id *id) | ||
531 | { | ||
532 | struct usb_cdc_union_desc *union_header = NULL; | ||
533 | char *buffer = intf->altsetting->extra; | ||
534 | int buflen = intf->altsetting->extralen; | ||
535 | struct usb_interface *control_interface; | ||
536 | struct usb_interface *data_interface; | ||
537 | struct usb_endpoint_descriptor *epctrl; | ||
538 | struct usb_endpoint_descriptor *epread; | ||
539 | struct usb_endpoint_descriptor *epwrite; | ||
540 | struct usb_device *usb_dev = interface_to_usbdev(intf); | ||
541 | struct acm *acm; | ||
542 | int minor; | ||
543 | int ctrlsize,readsize; | ||
544 | u8 *buf; | ||
545 | u8 ac_management_function = 0; | ||
546 | u8 call_management_function = 0; | ||
547 | int call_interface_num = -1; | ||
548 | int data_interface_num; | ||
549 | unsigned long quirks; | ||
550 | |||
551 | /* handle quirks deadly to normal probing*/ | ||
552 | quirks = (unsigned long)id->driver_info; | ||
553 | if (quirks == NO_UNION_NORMAL) { | ||
554 | data_interface = usb_ifnum_to_if(usb_dev, 1); | ||
555 | control_interface = usb_ifnum_to_if(usb_dev, 0); | ||
556 | goto skip_normal_probe; | ||
557 | } | ||
558 | |||
559 | /* normal probing*/ | ||
560 | if (!buffer) { | ||
561 | err("Wierd descriptor references\n"); | ||
562 | return -EINVAL; | ||
563 | } | ||
564 | |||
565 | if (!buflen) { | ||
566 | if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) { | ||
567 | dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n"); | ||
568 | buflen = intf->cur_altsetting->endpoint->extralen; | ||
569 | buffer = intf->cur_altsetting->endpoint->extra; | ||
570 | } else { | ||
571 | err("Zero length descriptor references\n"); | ||
572 | return -EINVAL; | ||
573 | } | ||
574 | } | ||
575 | |||
576 | while (buflen > 0) { | ||
577 | if (buffer [1] != USB_DT_CS_INTERFACE) { | ||
578 | err("skipping garbage\n"); | ||
579 | goto next_desc; | ||
580 | } | ||
581 | |||
582 | switch (buffer [2]) { | ||
583 | case USB_CDC_UNION_TYPE: /* we've found it */ | ||
584 | if (union_header) { | ||
585 | err("More than one union descriptor, skipping ..."); | ||
586 | goto next_desc; | ||
587 | } | ||
588 | union_header = (struct usb_cdc_union_desc *) | ||
589 | buffer; | ||
590 | break; | ||
591 | case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */ | ||
592 | break; /* for now we ignore it */ | ||
593 | case USB_CDC_HEADER_TYPE: /* maybe check version */ | ||
594 | break; /* for now we ignore it */ | ||
595 | case USB_CDC_ACM_TYPE: | ||
596 | ac_management_function = buffer[3]; | ||
597 | break; | ||
598 | case USB_CDC_CALL_MANAGEMENT_TYPE: | ||
599 | call_management_function = buffer[3]; | ||
600 | call_interface_num = buffer[4]; | ||
601 | if ((call_management_function & 3) != 3) | ||
602 | err("This device cannot do calls on its own. It is no modem."); | ||
603 | break; | ||
604 | |||
605 | default: | ||
606 | err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]); | ||
607 | break; | ||
608 | } | ||
609 | next_desc: | ||
610 | buflen -= buffer[0]; | ||
611 | buffer += buffer[0]; | ||
612 | } | ||
613 | |||
614 | if (!union_header) { | ||
615 | if (call_interface_num > 0) { | ||
616 | dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n"); | ||
617 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); | ||
618 | control_interface = intf; | ||
619 | } else { | ||
620 | dev_dbg(&intf->dev,"No union descriptor, giving up\n"); | ||
621 | return -ENODEV; | ||
622 | } | ||
623 | } else { | ||
624 | control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); | ||
625 | data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); | ||
626 | if (!control_interface || !data_interface) { | ||
627 | dev_dbg(&intf->dev,"no interfaces\n"); | ||
628 | return -ENODEV; | ||
629 | } | ||
630 | } | ||
631 | |||
632 | if (data_interface_num != call_interface_num) | ||
633 | dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n"); | ||
634 | |||
635 | skip_normal_probe: | ||
636 | |||
637 | /*workaround for switched interfaces */ | ||
638 | if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) { | ||
639 | if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) { | ||
640 | struct usb_interface *t; | ||
641 | dev_dbg(&intf->dev,"Your device has switched interfaces.\n"); | ||
642 | |||
643 | t = control_interface; | ||
644 | control_interface = data_interface; | ||
645 | data_interface = t; | ||
646 | } else { | ||
647 | return -EINVAL; | ||
648 | } | ||
649 | } | ||
650 | |||
651 | if (usb_interface_claimed(data_interface)) { /* valid in this context */ | ||
652 | dev_dbg(&intf->dev,"The data interface isn't available\n"); | ||
653 | return -EBUSY; | ||
654 | } | ||
655 | |||
656 | |||
657 | if (data_interface->cur_altsetting->desc.bNumEndpoints < 2) | ||
658 | return -EINVAL; | ||
659 | |||
660 | epctrl = &control_interface->cur_altsetting->endpoint[0].desc; | ||
661 | epread = &data_interface->cur_altsetting->endpoint[0].desc; | ||
662 | epwrite = &data_interface->cur_altsetting->endpoint[1].desc; | ||
663 | |||
664 | |||
665 | /* workaround for switched endpoints */ | ||
666 | if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) { | ||
667 | /* descriptors are swapped */ | ||
668 | struct usb_endpoint_descriptor *t; | ||
669 | dev_dbg(&intf->dev,"The data interface has switched endpoints\n"); | ||
670 | |||
671 | t = epread; | ||
672 | epread = epwrite; | ||
673 | epwrite = t; | ||
674 | } | ||
675 | dbg("interfaces are valid"); | ||
676 | for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++); | ||
677 | |||
678 | if (minor == ACM_TTY_MINORS) { | ||
679 | err("no more free acm devices"); | ||
680 | return -ENODEV; | ||
681 | } | ||
682 | |||
683 | if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) { | ||
684 | dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n"); | ||
685 | goto alloc_fail; | ||
686 | } | ||
687 | memset(acm, 0, sizeof(struct acm)); | ||
688 | |||
689 | ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); | ||
690 | readsize = le16_to_cpu(epread->wMaxPacketSize); | ||
691 | acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize); | ||
692 | acm->control = control_interface; | ||
693 | acm->data = data_interface; | ||
694 | acm->minor = minor; | ||
695 | acm->dev = usb_dev; | ||
696 | acm->ctrl_caps = ac_management_function; | ||
697 | acm->ctrlsize = ctrlsize; | ||
698 | acm->readsize = readsize; | ||
699 | acm->bh.func = acm_rx_tasklet; | ||
700 | acm->bh.data = (unsigned long) acm; | ||
701 | INIT_WORK(&acm->work, acm_softint, acm); | ||
702 | spin_lock_init(&acm->throttle_lock); | ||
703 | acm->ready_for_write = 1; | ||
704 | |||
705 | buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); | ||
706 | if (!buf) { | ||
707 | dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n"); | ||
708 | goto alloc_fail2; | ||
709 | } | ||
710 | acm->ctrl_buffer = buf; | ||
711 | |||
712 | buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma); | ||
713 | if (!buf) { | ||
714 | dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n"); | ||
715 | goto alloc_fail3; | ||
716 | } | ||
717 | acm->read_buffer = buf; | ||
718 | |||
719 | buf = usb_buffer_alloc(usb_dev, acm->writesize, GFP_KERNEL, &acm->write_dma); | ||
720 | if (!buf) { | ||
721 | dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n"); | ||
722 | goto alloc_fail4; | ||
723 | } | ||
724 | acm->write_buffer = buf; | ||
725 | |||
726 | acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); | ||
727 | if (!acm->ctrlurb) { | ||
728 | dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); | ||
729 | goto alloc_fail5; | ||
730 | } | ||
731 | acm->readurb = usb_alloc_urb(0, GFP_KERNEL); | ||
732 | if (!acm->readurb) { | ||
733 | dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n"); | ||
734 | goto alloc_fail6; | ||
735 | } | ||
736 | acm->writeurb = usb_alloc_urb(0, GFP_KERNEL); | ||
737 | if (!acm->writeurb) { | ||
738 | dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n"); | ||
739 | goto alloc_fail7; | ||
740 | } | ||
741 | |||
742 | usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), | ||
743 | acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); | ||
744 | acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | ||
745 | acm->ctrlurb->transfer_dma = acm->ctrl_dma; | ||
746 | |||
747 | usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress), | ||
748 | acm->read_buffer, readsize, acm_read_bulk, acm); | ||
749 | acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP; | ||
750 | acm->readurb->transfer_dma = acm->read_dma; | ||
751 | |||
752 | usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), | ||
753 | acm->write_buffer, acm->writesize, acm_write_bulk, acm); | ||
754 | acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP; | ||
755 | acm->writeurb->transfer_dma = acm->write_dma; | ||
756 | |||
757 | dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); | ||
758 | |||
759 | acm_set_control(acm, acm->ctrlout); | ||
760 | |||
761 | acm->line.dwDTERate = cpu_to_le32(9600); | ||
762 | acm->line.bDataBits = 8; | ||
763 | acm_set_line(acm, &acm->line); | ||
764 | |||
765 | usb_driver_claim_interface(&acm_driver, data_interface, acm); | ||
766 | |||
767 | tty_register_device(acm_tty_driver, minor, &intf->dev); | ||
768 | |||
769 | acm_table[minor] = acm; | ||
770 | usb_set_intfdata (intf, acm); | ||
771 | return 0; | ||
772 | |||
773 | alloc_fail7: | ||
774 | usb_free_urb(acm->readurb); | ||
775 | alloc_fail6: | ||
776 | usb_free_urb(acm->ctrlurb); | ||
777 | alloc_fail5: | ||
778 | usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma); | ||
779 | alloc_fail4: | ||
780 | usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma); | ||
781 | alloc_fail3: | ||
782 | usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); | ||
783 | alloc_fail2: | ||
784 | kfree(acm); | ||
785 | alloc_fail: | ||
786 | return -ENOMEM; | ||
787 | } | ||
788 | |||
789 | static void acm_disconnect(struct usb_interface *intf) | ||
790 | { | ||
791 | struct acm *acm = usb_get_intfdata (intf); | ||
792 | struct usb_device *usb_dev = interface_to_usbdev(intf); | ||
793 | |||
794 | if (!acm || !acm->dev) { | ||
795 | dbg("disconnect on nonexisting interface"); | ||
796 | return; | ||
797 | } | ||
798 | |||
799 | down(&open_sem); | ||
800 | acm->dev = NULL; | ||
801 | usb_set_intfdata (intf, NULL); | ||
802 | |||
803 | usb_kill_urb(acm->ctrlurb); | ||
804 | usb_kill_urb(acm->readurb); | ||
805 | usb_kill_urb(acm->writeurb); | ||
806 | |||
807 | flush_scheduled_work(); /* wait for acm_softint */ | ||
808 | |||
809 | usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma); | ||
810 | usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma); | ||
811 | usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); | ||
812 | |||
813 | usb_driver_release_interface(&acm_driver, acm->data); | ||
814 | |||
815 | if (!acm->used) { | ||
816 | tty_unregister_device(acm_tty_driver, acm->minor); | ||
817 | acm_table[acm->minor] = NULL; | ||
818 | usb_free_urb(acm->ctrlurb); | ||
819 | usb_free_urb(acm->readurb); | ||
820 | usb_free_urb(acm->writeurb); | ||
821 | kfree(acm); | ||
822 | up(&open_sem); | ||
823 | return; | ||
824 | } | ||
825 | |||
826 | up(&open_sem); | ||
827 | |||
828 | if (acm->tty) | ||
829 | tty_hangup(acm->tty); | ||
830 | } | ||
831 | |||
832 | /* | ||
833 | * USB driver structure. | ||
834 | */ | ||
835 | |||
836 | static struct usb_device_id acm_ids[] = { | ||
837 | /* quirky and broken devices */ | ||
838 | { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ | ||
839 | .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ | ||
840 | }, | ||
841 | /* control interfaces with various AT-command sets */ | ||
842 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
843 | USB_CDC_ACM_PROTO_AT_V25TER) }, | ||
844 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
845 | USB_CDC_ACM_PROTO_AT_PCCA101) }, | ||
846 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
847 | USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, | ||
848 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
849 | USB_CDC_ACM_PROTO_AT_GSM) }, | ||
850 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
851 | USB_CDC_ACM_PROTO_AT_3G ) }, | ||
852 | { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, | ||
853 | USB_CDC_ACM_PROTO_AT_CDMA) }, | ||
854 | |||
855 | /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */ | ||
856 | { } | ||
857 | }; | ||
858 | |||
859 | MODULE_DEVICE_TABLE (usb, acm_ids); | ||
860 | |||
861 | static struct usb_driver acm_driver = { | ||
862 | .owner = THIS_MODULE, | ||
863 | .name = "cdc_acm", | ||
864 | .probe = acm_probe, | ||
865 | .disconnect = acm_disconnect, | ||
866 | .id_table = acm_ids, | ||
867 | }; | ||
868 | |||
869 | /* | ||
870 | * TTY driver structures. | ||
871 | */ | ||
872 | |||
873 | static struct tty_operations acm_ops = { | ||
874 | .open = acm_tty_open, | ||
875 | .close = acm_tty_close, | ||
876 | .write = acm_tty_write, | ||
877 | .write_room = acm_tty_write_room, | ||
878 | .ioctl = acm_tty_ioctl, | ||
879 | .throttle = acm_tty_throttle, | ||
880 | .unthrottle = acm_tty_unthrottle, | ||
881 | .chars_in_buffer = acm_tty_chars_in_buffer, | ||
882 | .break_ctl = acm_tty_break_ctl, | ||
883 | .set_termios = acm_tty_set_termios, | ||
884 | .tiocmget = acm_tty_tiocmget, | ||
885 | .tiocmset = acm_tty_tiocmset, | ||
886 | }; | ||
887 | |||
888 | /* | ||
889 | * Init / exit. | ||
890 | */ | ||
891 | |||
892 | static int __init acm_init(void) | ||
893 | { | ||
894 | int retval; | ||
895 | acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); | ||
896 | if (!acm_tty_driver) | ||
897 | return -ENOMEM; | ||
898 | acm_tty_driver->owner = THIS_MODULE, | ||
899 | acm_tty_driver->driver_name = "acm", | ||
900 | acm_tty_driver->name = "ttyACM", | ||
901 | acm_tty_driver->devfs_name = "usb/acm/", | ||
902 | acm_tty_driver->major = ACM_TTY_MAJOR, | ||
903 | acm_tty_driver->minor_start = 0, | ||
904 | acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, | ||
905 | acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, | ||
906 | acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS, | ||
907 | acm_tty_driver->init_termios = tty_std_termios; | ||
908 | acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; | ||
909 | tty_set_operations(acm_tty_driver, &acm_ops); | ||
910 | |||
911 | retval = tty_register_driver(acm_tty_driver); | ||
912 | if (retval) { | ||
913 | put_tty_driver(acm_tty_driver); | ||
914 | return retval; | ||
915 | } | ||
916 | |||
917 | retval = usb_register(&acm_driver); | ||
918 | if (retval) { | ||
919 | tty_unregister_driver(acm_tty_driver); | ||
920 | put_tty_driver(acm_tty_driver); | ||
921 | return retval; | ||
922 | } | ||
923 | |||
924 | info(DRIVER_VERSION ":" DRIVER_DESC); | ||
925 | |||
926 | return 0; | ||
927 | } | ||
928 | |||
929 | static void __exit acm_exit(void) | ||
930 | { | ||
931 | usb_deregister(&acm_driver); | ||
932 | tty_unregister_driver(acm_tty_driver); | ||
933 | put_tty_driver(acm_tty_driver); | ||
934 | } | ||
935 | |||
936 | module_init(acm_init); | ||
937 | module_exit(acm_exit); | ||
938 | |||
939 | MODULE_AUTHOR( DRIVER_AUTHOR ); | ||
940 | MODULE_DESCRIPTION( DRIVER_DESC ); | ||
941 | MODULE_LICENSE("GPL"); | ||
942 | |||
diff --git a/drivers/usb/class/cdc-acm.h b/drivers/usb/class/cdc-acm.h new file mode 100644 index 000000000000..9009114e311b --- /dev/null +++ b/drivers/usb/class/cdc-acm.h | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Includes for cdc-acm.c | ||
4 | * | ||
5 | * Mainly take from usbnet's cdc-ether part | ||
6 | * | ||
7 | */ | ||
8 | |||
9 | /* | ||
10 | * CMSPAR, some architectures can't have space and mark parity. | ||
11 | */ | ||
12 | |||
13 | #ifndef CMSPAR | ||
14 | #define CMSPAR 0 | ||
15 | #endif | ||
16 | |||
17 | /* | ||
18 | * Major and minor numbers. | ||
19 | */ | ||
20 | |||
21 | #define ACM_TTY_MAJOR 166 | ||
22 | #define ACM_TTY_MINORS 32 | ||
23 | |||
24 | /* | ||
25 | * Requests. | ||
26 | */ | ||
27 | |||
28 | #define USB_RT_ACM (USB_TYPE_CLASS | USB_RECIP_INTERFACE) | ||
29 | |||
30 | /* | ||
31 | * Output control lines. | ||
32 | */ | ||
33 | |||
34 | #define ACM_CTRL_DTR 0x01 | ||
35 | #define ACM_CTRL_RTS 0x02 | ||
36 | |||
37 | /* | ||
38 | * Input control lines and line errors. | ||
39 | */ | ||
40 | |||
41 | #define ACM_CTRL_DCD 0x01 | ||
42 | #define ACM_CTRL_DSR 0x02 | ||
43 | #define ACM_CTRL_BRK 0x04 | ||
44 | #define ACM_CTRL_RI 0x08 | ||
45 | |||
46 | #define ACM_CTRL_FRAMING 0x10 | ||
47 | #define ACM_CTRL_PARITY 0x20 | ||
48 | #define ACM_CTRL_OVERRUN 0x40 | ||
49 | |||
50 | /* | ||
51 | * Internal driver structures. | ||
52 | */ | ||
53 | |||
54 | struct acm { | ||
55 | struct usb_device *dev; /* the corresponding usb device */ | ||
56 | struct usb_interface *control; /* control interface */ | ||
57 | struct usb_interface *data; /* data interface */ | ||
58 | struct tty_struct *tty; /* the corresponding tty */ | ||
59 | struct urb *ctrlurb, *readurb, *writeurb; /* urbs */ | ||
60 | u8 *ctrl_buffer, *read_buffer, *write_buffer; /* buffers of urbs */ | ||
61 | dma_addr_t ctrl_dma, read_dma, write_dma; /* dma handles of buffers */ | ||
62 | struct usb_cdc_line_coding line; /* bits, stop, parity */ | ||
63 | struct work_struct work; /* work queue entry for line discipline waking up */ | ||
64 | struct tasklet_struct bh; /* rx processing */ | ||
65 | spinlock_t throttle_lock; /* synchronize throtteling and read callback */ | ||
66 | unsigned int ctrlin; /* input control lines (DCD, DSR, RI, break, overruns) */ | ||
67 | unsigned int ctrlout; /* output control lines (DTR, RTS) */ | ||
68 | unsigned int writesize; /* max packet size for the output bulk endpoint */ | ||
69 | unsigned int readsize,ctrlsize; /* buffer sizes for freeing */ | ||
70 | unsigned int used; /* someone has this acm's device open */ | ||
71 | unsigned int minor; /* acm minor number */ | ||
72 | unsigned char throttle; /* throttled by tty layer */ | ||
73 | unsigned char clocal; /* termios CLOCAL */ | ||
74 | unsigned char ready_for_write; /* write urb can be used */ | ||
75 | unsigned char resubmit_to_unthrottle; /* throtteling has disabled the read urb */ | ||
76 | unsigned int ctrl_caps; /* control capabilities from the class specific header */ | ||
77 | }; | ||
78 | |||
79 | #define CDC_DATA_INTERFACE_TYPE 0x0a | ||
80 | |||
81 | /* constants describing various quirks and errors */ | ||
82 | #define NO_UNION_NORMAL 1 | ||
diff --git a/drivers/usb/class/usb-midi.c b/drivers/usb/class/usb-midi.c new file mode 100644 index 000000000000..5f8af35e7633 --- /dev/null +++ b/drivers/usb/class/usb-midi.c | |||
@@ -0,0 +1,2154 @@ | |||
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 | |||
57 | static int singlebyte = 0; | ||
58 | module_param(singlebyte, int, 0); | ||
59 | MODULE_PARM_DESC(singlebyte,"Enable sending MIDI messages with single message packet"); | ||
60 | |||
61 | static int maxdevices = 4; | ||
62 | module_param(maxdevices, int, 0); | ||
63 | MODULE_PARM_DESC(maxdevices,"Max number of allocatable MIDI device"); | ||
64 | |||
65 | static int uvendor = -1; | ||
66 | module_param(uvendor, int, 0); | ||
67 | MODULE_PARM_DESC(uvendor, "The USB Vendor ID of a semi-compliant interface"); | ||
68 | |||
69 | static int uproduct = -1; | ||
70 | module_param(uproduct, int, 0); | ||
71 | MODULE_PARM_DESC(uproduct, "The USB Product ID of a semi-compliant interface"); | ||
72 | |||
73 | static int uinterface = -1; | ||
74 | module_param(uinterface, int, 0); | ||
75 | MODULE_PARM_DESC(uinterface, "The Interface number of a semi-compliant interface"); | ||
76 | |||
77 | static int ualt = -1; | ||
78 | module_param(ualt, int, 0); | ||
79 | MODULE_PARM_DESC(ualt, "The optional alternative setting of a semi-compliant interface"); | ||
80 | |||
81 | static int umin = -1; | ||
82 | module_param(umin, int, 0); | ||
83 | MODULE_PARM_DESC(umin, "The input endpoint of a semi-compliant interface"); | ||
84 | |||
85 | static int umout = -1; | ||
86 | module_param(umout, int, 0); | ||
87 | MODULE_PARM_DESC(umout, "The output endpoint of a semi-compliant interface"); | ||
88 | |||
89 | static int ucable = -1; | ||
90 | module_param(ucable, int, 0); | ||
91 | MODULE_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 | **/ | ||
97 | static unsigned short ulangid = 0x0409; /** 0x0411 for Japanese **/ | ||
98 | module_param(ulangid, ushort, 0); | ||
99 | MODULE_PARM_DESC(ulangid, "The optional preferred USB Language ID for all devices"); | ||
100 | |||
101 | MODULE_AUTHOR("NAGANO Daisuke <breeze.nagano@nifty.ne.jp>"); | ||
102 | MODULE_DESCRIPTION("USB-MIDI driver"); | ||
103 | MODULE_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 */ | ||
149 | struct 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 */ | ||
164 | struct 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 */ | ||
180 | struct 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 */ | ||
198 | struct 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 | |||
236 | static 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 | **/ | ||
249 | static 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 | **/ | ||
262 | static 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 | **/ | ||
287 | static 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 | |||
297 | static struct list_head mididevs = LIST_HEAD_INIT(mididevs); | ||
298 | |||
299 | static DECLARE_MUTEX(open_sem); | ||
300 | static DECLARE_WAIT_QUEUE_HEAD(open_wait); | ||
301 | |||
302 | |||
303 | /* ------------------------------------------------------------------------- */ | ||
304 | |||
305 | static 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 | |||
314 | static 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 | |||
352 | error: | ||
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 | |||
363 | static 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 | **/ | ||
418 | static 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 | **/ | ||
440 | static 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 | **/ | ||
461 | static 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 | **/ | ||
501 | static 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 | |||
534 | static 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 | **/ | ||
629 | static 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 | |||
642 | static 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 | |||
722 | static 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 | **/ | ||
768 | static 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 | |||
804 | static 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 | |||
926 | static 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 | |||
965 | static 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 | |||
986 | static 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 | |||
1040 | static 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 | **/ | ||
1058 | static 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 | |||
1100 | static 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 | **/ | ||
1117 | static 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 | |||
1167 | static 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 | */ | ||
1219 | static 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 | **/ | ||
1253 | static 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 | **/ | ||
1273 | static 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 | |||
1474 | error_end: | ||
1475 | kfree(u); | ||
1476 | return NULL; | ||
1477 | } | ||
1478 | |||
1479 | /* ------------------------------------------------------------------------- */ | ||
1480 | |||
1481 | /** Returns number between 0 and 16. | ||
1482 | * | ||
1483 | **/ | ||
1484 | static 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 | |||
1505 | static 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 | **/ | ||
1552 | static 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 | |||
1771 | static 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 | |||
1826 | static 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 | **/ | ||
1852 | static 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 | **/ | ||
1914 | static 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 | |||
1954 | static 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 | |||
1991 | static 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 */ | ||
2024 | static struct usb_device_id id_table[] = { | ||
2025 | {.driver_info = 42}, | ||
2026 | {} | ||
2027 | }; | ||
2028 | |||
2029 | static struct usb_driver usb_midi_driver = { | ||
2030 | .owner = THIS_MODULE, | ||
2031 | .name = "midi", | ||
2032 | .probe = usb_midi_probe, | ||
2033 | .disconnect = usb_midi_disconnect, | ||
2034 | .id_table = id_table, | ||
2035 | }; | ||
2036 | |||
2037 | /* ------------------------------------------------------------------------- */ | ||
2038 | |||
2039 | static int __init usb_midi_init(void) | ||
2040 | { | ||
2041 | return usb_register(&usb_midi_driver); | ||
2042 | } | ||
2043 | |||
2044 | static void __exit usb_midi_exit(void) | ||
2045 | { | ||
2046 | usb_deregister(&usb_midi_driver); | ||
2047 | } | ||
2048 | |||
2049 | module_init(usb_midi_init) ; | ||
2050 | module_exit(usb_midi_exit) ; | ||
2051 | |||
2052 | #ifdef HAVE_ALSA_SUPPORT | ||
2053 | #define SNDRV_MAIN_OBJECT_FILE | ||
2054 | #include "../../include/driver.h" | ||
2055 | #include "../../include/control.h" | ||
2056 | #include "../../include/info.h" | ||
2057 | #include "../../include/cs46xx.h" | ||
2058 | |||
2059 | /* ------------------------------------------------------------------------- */ | ||
2060 | |||
2061 | static int snd_usbmidi_input_close(snd_rawmidi_substream_t * substream) | ||
2062 | { | ||
2063 | return 0; | ||
2064 | } | ||
2065 | |||
2066 | static int snd_usbmidi_input_open(snd_rawmidi_substream_t * substream ) | ||
2067 | { | ||
2068 | return 0; | ||
2069 | } | ||
2070 | |||
2071 | static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t * substream, int up) | ||
2072 | { | ||
2073 | return 0; | ||
2074 | } | ||
2075 | |||
2076 | |||
2077 | /* ------------------------------------------------------------------------- */ | ||
2078 | |||
2079 | static int snd_usbmidi_output_close(snd_rawmidi_substream_t * substream) | ||
2080 | { | ||
2081 | return 0; | ||
2082 | } | ||
2083 | |||
2084 | static int snd_usbmidi_output_open(snd_rawmidi_substream_t * substream) | ||
2085 | { | ||
2086 | return 0; | ||
2087 | } | ||
2088 | |||
2089 | static void snd_usb_midi_output_trigger(snd_rawmidi_substream_t * substream, | ||
2090 | int up) | ||
2091 | { | ||
2092 | return 0; | ||
2093 | } | ||
2094 | |||
2095 | /* ------------------------------------------------------------------------- */ | ||
2096 | |||
2097 | static snd_rawmidi_ops_t snd_usbmidi_output = | ||
2098 | { | ||
2099 | .open = snd_usbmidi_output_open, | ||
2100 | .close = snd_usbmidi_output_close, | ||
2101 | .trigger = snd_usbmidi_output_trigger, | ||
2102 | }; | ||
2103 | static snd_rawmidi_ops_t snd_usbmidi_input = | ||
2104 | { | ||
2105 | .open = snd_usbmidi_input_open, | ||
2106 | .close = snd_usbmidi_input_close, | ||
2107 | .trigger = snd_usbmidi_input_trigger, | ||
2108 | }; | ||
2109 | |||
2110 | int snd_usbmidi_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi) | ||
2111 | { | ||
2112 | snd_rawmidi_t *rmidi; | ||
2113 | int err; | ||
2114 | |||
2115 | if (rrawmidi) | ||
2116 | *rrawmidi = NULL; | ||
2117 | if ((err = snd_rawmidi_new(chip->card, "USB-MIDI", device, 1, 1, &rmidi)) < 0) | ||
2118 | return err; | ||
2119 | strcpy(rmidi->name, "USB-MIDI"); | ||
2120 | |||
2121 | snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output ); | ||
2122 | snd_rawmidi_set_ops( rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input ); | ||
2123 | |||
2124 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX; | ||
2125 | |||
2126 | rmidi->private_data = chip; | ||
2127 | chip->rmidi = rmidi; | ||
2128 | if (rrawmidi) | ||
2129 | *rrawmidi = NULL; | ||
2130 | |||
2131 | return 0; | ||
2132 | } | ||
2133 | |||
2134 | int snd_usbmidi_create( snd_card_t * card, | ||
2135 | struct pci_dev * pci, | ||
2136 | usbmidi_t ** rchip ) | ||
2137 | { | ||
2138 | usbmidi_t *chip; | ||
2139 | int err, idx; | ||
2140 | snd_region_t *region; | ||
2141 | static snd_device_opt_t ops = { | ||
2142 | .dev_free = snd_usbmidi_dev_free, | ||
2143 | }; | ||
2144 | |||
2145 | *rchip = NULL; | ||
2146 | chip = snd_magic_kcalloc( usbmidi_t, 0, GFP_KERNEL ); | ||
2147 | if ( chip == NULL ) | ||
2148 | return -ENOMEM; | ||
2149 | } | ||
2150 | |||
2151 | EXPORT_SYMBOL(snd_usbmidi_create); | ||
2152 | EXPORT_SYMBOL(snd_usbmidi_midi); | ||
2153 | #endif /* HAVE_ALSA_SUPPORT */ | ||
2154 | |||
diff --git a/drivers/usb/class/usb-midi.h b/drivers/usb/class/usb-midi.h new file mode 100644 index 000000000000..358cdef8492e --- /dev/null +++ b/drivers/usb/class/usb-midi.h | |||
@@ -0,0 +1,164 @@ | |||
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 | |||
58 | struct usb_midi_endpoint { | ||
59 | int endpoint; | ||
60 | int cableId; /* if bit-n == 1 then cableId-n is enabled (n: 0 - 15) */ | ||
61 | }; | ||
62 | |||
63 | struct 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 | |||
75 | static 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 | |||
144 | static 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 | |||
159 | MODULE_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 new file mode 100644 index 000000000000..bba22e97ea0f --- /dev/null +++ b/drivers/usb/class/usblp.c | |||
@@ -0,0 +1,1214 @@ | |||
1 | /* | ||
2 | * usblp.c Version 0.13 | ||
3 | * | ||
4 | * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com> | ||
5 | * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> | ||
6 | * Copyright (c) 2000 Randy Dunlap <rddunlap@osdl.org> | ||
7 | * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> | ||
8 | # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com> | ||
9 | # Copyright (c) 2001 David Paschal <paschal@rcsis.com> | ||
10 | * | ||
11 | * USB Printer Device Class driver for USB printers and printer cables | ||
12 | * | ||
13 | * Sponsored by SuSE | ||
14 | * | ||
15 | * ChangeLog: | ||
16 | * v0.1 - thorough cleaning, URBification, almost a rewrite | ||
17 | * v0.2 - some more cleanups | ||
18 | * v0.3 - cleaner again, waitqueue fixes | ||
19 | * v0.4 - fixes in unidirectional mode | ||
20 | * v0.5 - add DEVICE_ID string support | ||
21 | * v0.6 - never time out | ||
22 | * v0.7 - fixed bulk-IN read and poll (David Paschal) | ||
23 | * v0.8 - add devfs support | ||
24 | * v0.9 - fix unplug-while-open paths | ||
25 | * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org) | ||
26 | * v0.11 - add proto_bias option (Pete Zaitcev) | ||
27 | * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal) | ||
28 | * v0.13 - alloc space for statusbuf (<status> not on stack); | ||
29 | * use usb_buffer_alloc() for read buf & write buf; | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * This program is free software; you can redistribute it and/or modify | ||
34 | * it under the terms of the GNU General Public License as published by | ||
35 | * the Free Software Foundation; either version 2 of the License, or | ||
36 | * (at your option) any later version. | ||
37 | * | ||
38 | * This program is distributed in the hope that it will be useful, | ||
39 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
40 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
41 | * GNU General Public License for more details. | ||
42 | * | ||
43 | * You should have received a copy of the GNU General Public License | ||
44 | * along with this program; if not, write to the Free Software | ||
45 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
46 | */ | ||
47 | |||
48 | #include <linux/module.h> | ||
49 | #include <linux/kernel.h> | ||
50 | #include <linux/sched.h> | ||
51 | #include <linux/smp_lock.h> | ||
52 | #include <linux/signal.h> | ||
53 | #include <linux/poll.h> | ||
54 | #include <linux/init.h> | ||
55 | #include <linux/slab.h> | ||
56 | #include <linux/lp.h> | ||
57 | #undef DEBUG | ||
58 | #include <linux/usb.h> | ||
59 | |||
60 | /* | ||
61 | * Version Information | ||
62 | */ | ||
63 | #define DRIVER_VERSION "v0.13" | ||
64 | #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal" | ||
65 | #define DRIVER_DESC "USB Printer Device Class driver" | ||
66 | |||
67 | #define USBLP_BUF_SIZE 8192 | ||
68 | #define USBLP_DEVICE_ID_SIZE 1024 | ||
69 | |||
70 | /* ioctls: */ | ||
71 | #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */ | ||
72 | #define IOCNR_GET_DEVICE_ID 1 | ||
73 | #define IOCNR_GET_PROTOCOLS 2 | ||
74 | #define IOCNR_SET_PROTOCOL 3 | ||
75 | #define IOCNR_HP_SET_CHANNEL 4 | ||
76 | #define IOCNR_GET_BUS_ADDRESS 5 | ||
77 | #define IOCNR_GET_VID_PID 6 | ||
78 | #define IOCNR_SOFT_RESET 7 | ||
79 | /* Get device_id string: */ | ||
80 | #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len) | ||
81 | /* The following ioctls were added for http://hpoj.sourceforge.net: */ | ||
82 | /* Get two-int array: | ||
83 | * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3), | ||
84 | * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */ | ||
85 | #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len) | ||
86 | /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */ | ||
87 | #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0) | ||
88 | /* Set channel number (HP Vendor-specific command): */ | ||
89 | #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0) | ||
90 | /* Get two-int array: [0]=bus number, [1]=device address: */ | ||
91 | #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len) | ||
92 | /* Get two-int array: [0]=vendor ID, [1]=product ID: */ | ||
93 | #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len) | ||
94 | /* Perform class specific soft reset */ | ||
95 | #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0); | ||
96 | |||
97 | /* | ||
98 | * A DEVICE_ID string may include the printer's serial number. | ||
99 | * It should end with a semi-colon (';'). | ||
100 | * An example from an HP 970C DeskJet printer is (this is one long string, | ||
101 | * with the serial number changed): | ||
102 | MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ: ; | ||
103 | */ | ||
104 | |||
105 | /* | ||
106 | * USB Printer Requests | ||
107 | */ | ||
108 | |||
109 | #define USBLP_REQ_GET_ID 0x00 | ||
110 | #define USBLP_REQ_GET_STATUS 0x01 | ||
111 | #define USBLP_REQ_RESET 0x02 | ||
112 | #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */ | ||
113 | |||
114 | #define USBLP_MINORS 16 | ||
115 | #define USBLP_MINOR_BASE 0 | ||
116 | |||
117 | #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */ | ||
118 | |||
119 | #define USBLP_FIRST_PROTOCOL 1 | ||
120 | #define USBLP_LAST_PROTOCOL 3 | ||
121 | #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1) | ||
122 | |||
123 | /* | ||
124 | * some arbitrary status buffer size; | ||
125 | * need a status buffer that is allocated via kmalloc(), not on stack | ||
126 | */ | ||
127 | #define STATUS_BUF_SIZE 8 | ||
128 | |||
129 | struct usblp { | ||
130 | struct usb_device *dev; /* USB device */ | ||
131 | struct semaphore sem; /* locks this struct, especially "dev" */ | ||
132 | char *writebuf; /* write transfer_buffer */ | ||
133 | char *readbuf; /* read transfer_buffer */ | ||
134 | char *statusbuf; /* status transfer_buffer */ | ||
135 | struct urb *readurb, *writeurb; /* The urbs */ | ||
136 | wait_queue_head_t wait; /* Zzzzz ... */ | ||
137 | int readcount; /* Counter for reads */ | ||
138 | int ifnum; /* Interface number */ | ||
139 | struct usb_interface *intf; /* The interface */ | ||
140 | /* Alternate-setting numbers and endpoints for each protocol | ||
141 | * (7/1/{index=1,2,3}) that the device supports: */ | ||
142 | struct { | ||
143 | int alt_setting; | ||
144 | struct usb_endpoint_descriptor *epwrite; | ||
145 | struct usb_endpoint_descriptor *epread; | ||
146 | } protocol[USBLP_MAX_PROTOCOLS]; | ||
147 | int current_protocol; | ||
148 | int minor; /* minor number of device */ | ||
149 | int wcomplete; /* writing is completed */ | ||
150 | int rcomplete; /* reading is completed */ | ||
151 | unsigned int quirks; /* quirks flags */ | ||
152 | unsigned char used; /* True if open */ | ||
153 | unsigned char present; /* True if not disconnected */ | ||
154 | unsigned char bidir; /* interface is bidirectional */ | ||
155 | unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */ | ||
156 | /* first 2 bytes are (big-endian) length */ | ||
157 | }; | ||
158 | |||
159 | #ifdef DEBUG | ||
160 | static void usblp_dump(struct usblp *usblp) { | ||
161 | int p; | ||
162 | |||
163 | dbg("usblp=0x%p", usblp); | ||
164 | dbg("dev=0x%p", usblp->dev); | ||
165 | dbg("present=%d", usblp->present); | ||
166 | dbg("readbuf=0x%p", usblp->readbuf); | ||
167 | dbg("writebuf=0x%p", usblp->writebuf); | ||
168 | dbg("readurb=0x%p", usblp->readurb); | ||
169 | dbg("writeurb=0x%p", usblp->writeurb); | ||
170 | dbg("readcount=%d", usblp->readcount); | ||
171 | dbg("ifnum=%d", usblp->ifnum); | ||
172 | for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) { | ||
173 | dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting); | ||
174 | dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite); | ||
175 | dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread); | ||
176 | } | ||
177 | dbg("current_protocol=%d", usblp->current_protocol); | ||
178 | dbg("minor=%d", usblp->minor); | ||
179 | dbg("wcomplete=%d", usblp->wcomplete); | ||
180 | dbg("rcomplete=%d", usblp->rcomplete); | ||
181 | dbg("quirks=%d", usblp->quirks); | ||
182 | dbg("used=%d", usblp->used); | ||
183 | dbg("bidir=%d", usblp->bidir); | ||
184 | dbg("device_id_string=\"%s\"", | ||
185 | usblp->device_id_string ? | ||
186 | usblp->device_id_string + 2 : | ||
187 | (unsigned char *)"(null)"); | ||
188 | } | ||
189 | #endif | ||
190 | |||
191 | /* Quirks: various printer quirks are handled by this table & its flags. */ | ||
192 | |||
193 | struct quirk_printer_struct { | ||
194 | __u16 vendorId; | ||
195 | __u16 productId; | ||
196 | unsigned int quirks; | ||
197 | }; | ||
198 | |||
199 | #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */ | ||
200 | #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */ | ||
201 | |||
202 | static struct quirk_printer_struct quirk_printers[] = { | ||
203 | { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */ | ||
204 | { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */ | ||
205 | { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */ | ||
206 | { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */ | ||
207 | { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */ | ||
208 | { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */ | ||
209 | { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */ | ||
210 | { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */ | ||
211 | { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */ | ||
212 | { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */ | ||
213 | { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */ | ||
214 | { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */ | ||
215 | { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */ | ||
216 | { 0, 0 } | ||
217 | }; | ||
218 | |||
219 | static int usblp_select_alts(struct usblp *usblp); | ||
220 | static int usblp_set_protocol(struct usblp *usblp, int protocol); | ||
221 | static int usblp_cache_device_id_string(struct usblp *usblp); | ||
222 | |||
223 | /* forward reference to make our lives easier */ | ||
224 | static struct usb_driver usblp_driver; | ||
225 | static DECLARE_MUTEX(usblp_sem); /* locks the existence of usblp's */ | ||
226 | |||
227 | /* | ||
228 | * Functions for usblp control messages. | ||
229 | */ | ||
230 | |||
231 | static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len) | ||
232 | { | ||
233 | int retval; | ||
234 | int index = usblp->ifnum; | ||
235 | |||
236 | /* High byte has the interface index. | ||
237 | Low byte has the alternate setting. | ||
238 | */ | ||
239 | if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) { | ||
240 | index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting; | ||
241 | } | ||
242 | |||
243 | retval = usb_control_msg(usblp->dev, | ||
244 | dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0), | ||
245 | request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT); | ||
246 | dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d", | ||
247 | request, !!dir, recip, value, index, len, retval); | ||
248 | return retval < 0 ? retval : 0; | ||
249 | } | ||
250 | |||
251 | #define usblp_read_status(usblp, status)\ | ||
252 | usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1) | ||
253 | #define usblp_get_id(usblp, config, id, maxlen)\ | ||
254 | usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen) | ||
255 | #define usblp_reset(usblp)\ | ||
256 | usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0) | ||
257 | |||
258 | #define usblp_hp_channel_change_request(usblp, channel, buffer) \ | ||
259 | usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1) | ||
260 | |||
261 | /* | ||
262 | * See the description for usblp_select_alts() below for the usage | ||
263 | * explanation. Look into your /proc/bus/usb/devices and dmesg in | ||
264 | * case of any trouble. | ||
265 | */ | ||
266 | static int proto_bias = -1; | ||
267 | |||
268 | /* | ||
269 | * URB callback. | ||
270 | */ | ||
271 | |||
272 | static void usblp_bulk_read(struct urb *urb, struct pt_regs *regs) | ||
273 | { | ||
274 | struct usblp *usblp = urb->context; | ||
275 | |||
276 | if (!usblp || !usblp->dev || !usblp->used || !usblp->present) | ||
277 | return; | ||
278 | |||
279 | if (unlikely(urb->status)) | ||
280 | warn("usblp%d: nonzero read/write bulk status received: %d", | ||
281 | usblp->minor, urb->status); | ||
282 | usblp->rcomplete = 1; | ||
283 | wake_up_interruptible(&usblp->wait); | ||
284 | } | ||
285 | |||
286 | static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs) | ||
287 | { | ||
288 | struct usblp *usblp = urb->context; | ||
289 | |||
290 | if (!usblp || !usblp->dev || !usblp->used || !usblp->present) | ||
291 | return; | ||
292 | |||
293 | if (unlikely(urb->status)) | ||
294 | warn("usblp%d: nonzero read/write bulk status received: %d", | ||
295 | usblp->minor, urb->status); | ||
296 | usblp->wcomplete = 1; | ||
297 | wake_up_interruptible(&usblp->wait); | ||
298 | } | ||
299 | |||
300 | /* | ||
301 | * Get and print printer errors. | ||
302 | */ | ||
303 | |||
304 | static char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" }; | ||
305 | |||
306 | static int usblp_check_status(struct usblp *usblp, int err) | ||
307 | { | ||
308 | unsigned char status, newerr = 0; | ||
309 | int error; | ||
310 | |||
311 | error = usblp_read_status (usblp, usblp->statusbuf); | ||
312 | if (error < 0) { | ||
313 | err("usblp%d: error %d reading printer status", | ||
314 | usblp->minor, error); | ||
315 | return 0; | ||
316 | } | ||
317 | |||
318 | status = *usblp->statusbuf; | ||
319 | |||
320 | if (~status & LP_PERRORP) | ||
321 | newerr = 3; | ||
322 | if (status & LP_POUTPA) | ||
323 | newerr = 1; | ||
324 | if (~status & LP_PSELECD) | ||
325 | newerr = 2; | ||
326 | |||
327 | if (newerr != err) | ||
328 | info("usblp%d: %s", usblp->minor, usblp_messages[newerr]); | ||
329 | |||
330 | return newerr; | ||
331 | } | ||
332 | |||
333 | /* | ||
334 | * File op functions. | ||
335 | */ | ||
336 | |||
337 | static int usblp_open(struct inode *inode, struct file *file) | ||
338 | { | ||
339 | int minor = iminor(inode); | ||
340 | struct usblp *usblp; | ||
341 | struct usb_interface *intf; | ||
342 | int retval; | ||
343 | |||
344 | if (minor < 0) | ||
345 | return -ENODEV; | ||
346 | |||
347 | down (&usblp_sem); | ||
348 | |||
349 | retval = -ENODEV; | ||
350 | intf = usb_find_interface(&usblp_driver, minor); | ||
351 | if (!intf) { | ||
352 | goto out; | ||
353 | } | ||
354 | usblp = usb_get_intfdata (intf); | ||
355 | if (!usblp || !usblp->dev || !usblp->present) | ||
356 | goto out; | ||
357 | |||
358 | retval = -EBUSY; | ||
359 | if (usblp->used) | ||
360 | goto out; | ||
361 | |||
362 | /* | ||
363 | * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ??? | ||
364 | * This is #if 0-ed because we *don't* want to fail an open | ||
365 | * just because the printer is off-line. | ||
366 | */ | ||
367 | #if 0 | ||
368 | if ((retval = usblp_check_status(usblp, 0))) { | ||
369 | retval = retval > 1 ? -EIO : -ENOSPC; | ||
370 | goto out; | ||
371 | } | ||
372 | #else | ||
373 | retval = 0; | ||
374 | #endif | ||
375 | |||
376 | usblp->used = 1; | ||
377 | file->private_data = usblp; | ||
378 | |||
379 | usblp->writeurb->transfer_buffer_length = 0; | ||
380 | usblp->wcomplete = 1; /* we begin writeable */ | ||
381 | usblp->rcomplete = 0; | ||
382 | |||
383 | if (usblp->bidir) { | ||
384 | usblp->readcount = 0; | ||
385 | usblp->readurb->dev = usblp->dev; | ||
386 | if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) { | ||
387 | retval = -EIO; | ||
388 | usblp->used = 0; | ||
389 | file->private_data = NULL; | ||
390 | } | ||
391 | } | ||
392 | out: | ||
393 | up (&usblp_sem); | ||
394 | return retval; | ||
395 | } | ||
396 | |||
397 | static void usblp_cleanup (struct usblp *usblp) | ||
398 | { | ||
399 | info("usblp%d: removed", usblp->minor); | ||
400 | |||
401 | kfree (usblp->device_id_string); | ||
402 | kfree (usblp->statusbuf); | ||
403 | usb_free_urb(usblp->writeurb); | ||
404 | usb_free_urb(usblp->readurb); | ||
405 | kfree (usblp); | ||
406 | } | ||
407 | |||
408 | static void usblp_unlink_urbs(struct usblp *usblp) | ||
409 | { | ||
410 | usb_kill_urb(usblp->writeurb); | ||
411 | if (usblp->bidir) | ||
412 | usb_kill_urb(usblp->readurb); | ||
413 | } | ||
414 | |||
415 | static int usblp_release(struct inode *inode, struct file *file) | ||
416 | { | ||
417 | struct usblp *usblp = file->private_data; | ||
418 | |||
419 | down (&usblp_sem); | ||
420 | usblp->used = 0; | ||
421 | if (usblp->present) { | ||
422 | usblp_unlink_urbs(usblp); | ||
423 | } else /* finish cleanup from disconnect */ | ||
424 | usblp_cleanup (usblp); | ||
425 | up (&usblp_sem); | ||
426 | return 0; | ||
427 | } | ||
428 | |||
429 | /* No kernel lock - fine */ | ||
430 | static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait) | ||
431 | { | ||
432 | struct usblp *usblp = file->private_data; | ||
433 | poll_wait(file, &usblp->wait, wait); | ||
434 | return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM) | ||
435 | | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM); | ||
436 | } | ||
437 | |||
438 | static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | ||
439 | { | ||
440 | struct usblp *usblp = file->private_data; | ||
441 | int length, err, i; | ||
442 | unsigned char newChannel; | ||
443 | int status; | ||
444 | int twoints[2]; | ||
445 | int retval = 0; | ||
446 | |||
447 | down (&usblp->sem); | ||
448 | if (!usblp->present) { | ||
449 | retval = -ENODEV; | ||
450 | goto done; | ||
451 | } | ||
452 | |||
453 | dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd), | ||
454 | _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) ); | ||
455 | |||
456 | if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */ | ||
457 | |||
458 | switch (_IOC_NR(cmd)) { | ||
459 | |||
460 | case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */ | ||
461 | if (_IOC_DIR(cmd) != _IOC_READ) { | ||
462 | retval = -EINVAL; | ||
463 | goto done; | ||
464 | } | ||
465 | |||
466 | length = usblp_cache_device_id_string(usblp); | ||
467 | if (length < 0) { | ||
468 | retval = length; | ||
469 | goto done; | ||
470 | } | ||
471 | if (length > _IOC_SIZE(cmd)) | ||
472 | length = _IOC_SIZE(cmd); /* truncate */ | ||
473 | |||
474 | if (copy_to_user((void __user *) arg, | ||
475 | usblp->device_id_string, | ||
476 | (unsigned long) length)) { | ||
477 | retval = -EFAULT; | ||
478 | goto done; | ||
479 | } | ||
480 | |||
481 | break; | ||
482 | |||
483 | case IOCNR_GET_PROTOCOLS: | ||
484 | if (_IOC_DIR(cmd) != _IOC_READ || | ||
485 | _IOC_SIZE(cmd) < sizeof(twoints)) { | ||
486 | retval = -EINVAL; | ||
487 | goto done; | ||
488 | } | ||
489 | |||
490 | twoints[0] = usblp->current_protocol; | ||
491 | twoints[1] = 0; | ||
492 | for (i = USBLP_FIRST_PROTOCOL; | ||
493 | i <= USBLP_LAST_PROTOCOL; i++) { | ||
494 | if (usblp->protocol[i].alt_setting >= 0) | ||
495 | twoints[1] |= (1<<i); | ||
496 | } | ||
497 | |||
498 | if (copy_to_user((void __user *)arg, | ||
499 | (unsigned char *)twoints, | ||
500 | sizeof(twoints))) { | ||
501 | retval = -EFAULT; | ||
502 | goto done; | ||
503 | } | ||
504 | |||
505 | break; | ||
506 | |||
507 | case IOCNR_SET_PROTOCOL: | ||
508 | if (_IOC_DIR(cmd) != _IOC_WRITE) { | ||
509 | retval = -EINVAL; | ||
510 | goto done; | ||
511 | } | ||
512 | |||
513 | #ifdef DEBUG | ||
514 | if (arg == -10) { | ||
515 | usblp_dump(usblp); | ||
516 | break; | ||
517 | } | ||
518 | #endif | ||
519 | |||
520 | usblp_unlink_urbs(usblp); | ||
521 | retval = usblp_set_protocol(usblp, arg); | ||
522 | if (retval < 0) { | ||
523 | usblp_set_protocol(usblp, | ||
524 | usblp->current_protocol); | ||
525 | } | ||
526 | break; | ||
527 | |||
528 | case IOCNR_HP_SET_CHANNEL: | ||
529 | if (_IOC_DIR(cmd) != _IOC_WRITE || | ||
530 | le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 || | ||
531 | usblp->quirks & USBLP_QUIRK_BIDIR) { | ||
532 | retval = -EINVAL; | ||
533 | goto done; | ||
534 | } | ||
535 | |||
536 | err = usblp_hp_channel_change_request(usblp, | ||
537 | arg, &newChannel); | ||
538 | if (err < 0) { | ||
539 | err("usblp%d: error = %d setting " | ||
540 | "HP channel", | ||
541 | usblp->minor, err); | ||
542 | retval = -EIO; | ||
543 | goto done; | ||
544 | } | ||
545 | |||
546 | dbg("usblp%d requested/got HP channel %ld/%d", | ||
547 | usblp->minor, arg, newChannel); | ||
548 | break; | ||
549 | |||
550 | case IOCNR_GET_BUS_ADDRESS: | ||
551 | if (_IOC_DIR(cmd) != _IOC_READ || | ||
552 | _IOC_SIZE(cmd) < sizeof(twoints)) { | ||
553 | retval = -EINVAL; | ||
554 | goto done; | ||
555 | } | ||
556 | |||
557 | twoints[0] = usblp->dev->bus->busnum; | ||
558 | twoints[1] = usblp->dev->devnum; | ||
559 | if (copy_to_user((void __user *)arg, | ||
560 | (unsigned char *)twoints, | ||
561 | sizeof(twoints))) { | ||
562 | retval = -EFAULT; | ||
563 | goto done; | ||
564 | } | ||
565 | |||
566 | dbg("usblp%d is bus=%d, device=%d", | ||
567 | usblp->minor, twoints[0], twoints[1]); | ||
568 | break; | ||
569 | |||
570 | case IOCNR_GET_VID_PID: | ||
571 | if (_IOC_DIR(cmd) != _IOC_READ || | ||
572 | _IOC_SIZE(cmd) < sizeof(twoints)) { | ||
573 | retval = -EINVAL; | ||
574 | goto done; | ||
575 | } | ||
576 | |||
577 | twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor); | ||
578 | twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct); | ||
579 | if (copy_to_user((void __user *)arg, | ||
580 | (unsigned char *)twoints, | ||
581 | sizeof(twoints))) { | ||
582 | retval = -EFAULT; | ||
583 | goto done; | ||
584 | } | ||
585 | |||
586 | dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X", | ||
587 | usblp->minor, twoints[0], twoints[1]); | ||
588 | break; | ||
589 | |||
590 | case IOCNR_SOFT_RESET: | ||
591 | if (_IOC_DIR(cmd) != _IOC_NONE) { | ||
592 | retval = -EINVAL; | ||
593 | goto done; | ||
594 | } | ||
595 | retval = usblp_reset(usblp); | ||
596 | break; | ||
597 | default: | ||
598 | retval = -ENOTTY; | ||
599 | } | ||
600 | else /* old-style ioctl value */ | ||
601 | switch (cmd) { | ||
602 | |||
603 | case LPGETSTATUS: | ||
604 | if (usblp_read_status(usblp, usblp->statusbuf)) { | ||
605 | err("usblp%d: failed reading printer status", usblp->minor); | ||
606 | retval = -EIO; | ||
607 | goto done; | ||
608 | } | ||
609 | status = *usblp->statusbuf; | ||
610 | if (copy_to_user ((void __user *)arg, &status, sizeof(int))) | ||
611 | retval = -EFAULT; | ||
612 | break; | ||
613 | |||
614 | default: | ||
615 | retval = -ENOTTY; | ||
616 | } | ||
617 | |||
618 | done: | ||
619 | up (&usblp->sem); | ||
620 | return retval; | ||
621 | } | ||
622 | |||
623 | static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | ||
624 | { | ||
625 | DECLARE_WAITQUEUE(wait, current); | ||
626 | struct usblp *usblp = file->private_data; | ||
627 | int timeout, err = 0, transfer_length = 0; | ||
628 | size_t writecount = 0; | ||
629 | |||
630 | while (writecount < count) { | ||
631 | if (!usblp->wcomplete) { | ||
632 | barrier(); | ||
633 | if (file->f_flags & O_NONBLOCK) { | ||
634 | writecount += transfer_length; | ||
635 | return writecount ? writecount : -EAGAIN; | ||
636 | } | ||
637 | |||
638 | timeout = USBLP_WRITE_TIMEOUT; | ||
639 | add_wait_queue(&usblp->wait, &wait); | ||
640 | while ( 1==1 ) { | ||
641 | |||
642 | if (signal_pending(current)) { | ||
643 | remove_wait_queue(&usblp->wait, &wait); | ||
644 | return writecount ? writecount : -EINTR; | ||
645 | } | ||
646 | set_current_state(TASK_INTERRUPTIBLE); | ||
647 | if (timeout && !usblp->wcomplete) { | ||
648 | timeout = schedule_timeout(timeout); | ||
649 | } else { | ||
650 | set_current_state(TASK_RUNNING); | ||
651 | break; | ||
652 | } | ||
653 | } | ||
654 | remove_wait_queue(&usblp->wait, &wait); | ||
655 | } | ||
656 | |||
657 | down (&usblp->sem); | ||
658 | if (!usblp->present) { | ||
659 | up (&usblp->sem); | ||
660 | return -ENODEV; | ||
661 | } | ||
662 | |||
663 | if (usblp->writeurb->status != 0) { | ||
664 | if (usblp->quirks & USBLP_QUIRK_BIDIR) { | ||
665 | if (!usblp->wcomplete) | ||
666 | err("usblp%d: error %d writing to printer", | ||
667 | usblp->minor, usblp->writeurb->status); | ||
668 | err = usblp->writeurb->status; | ||
669 | } else | ||
670 | err = usblp_check_status(usblp, err); | ||
671 | up (&usblp->sem); | ||
672 | |||
673 | /* if the fault was due to disconnect, let khubd's | ||
674 | * call to usblp_disconnect() grab usblp->sem ... | ||
675 | */ | ||
676 | schedule (); | ||
677 | continue; | ||
678 | } | ||
679 | |||
680 | /* We must increment writecount here, and not at the | ||
681 | * end of the loop. Otherwise, the final loop iteration may | ||
682 | * be skipped, leading to incomplete printer output. | ||
683 | */ | ||
684 | writecount += transfer_length; | ||
685 | if (writecount == count) { | ||
686 | up(&usblp->sem); | ||
687 | break; | ||
688 | } | ||
689 | |||
690 | transfer_length=(count - writecount); | ||
691 | if (transfer_length > USBLP_BUF_SIZE) | ||
692 | transfer_length = USBLP_BUF_SIZE; | ||
693 | |||
694 | usblp->writeurb->transfer_buffer_length = transfer_length; | ||
695 | |||
696 | if (copy_from_user(usblp->writeurb->transfer_buffer, | ||
697 | buffer + writecount, transfer_length)) { | ||
698 | up(&usblp->sem); | ||
699 | return writecount ? writecount : -EFAULT; | ||
700 | } | ||
701 | |||
702 | usblp->writeurb->dev = usblp->dev; | ||
703 | usblp->wcomplete = 0; | ||
704 | err = usb_submit_urb(usblp->writeurb, GFP_KERNEL); | ||
705 | if (err) { | ||
706 | if (err != -ENOMEM) | ||
707 | count = -EIO; | ||
708 | else | ||
709 | count = writecount ? writecount : -ENOMEM; | ||
710 | up (&usblp->sem); | ||
711 | break; | ||
712 | } | ||
713 | up (&usblp->sem); | ||
714 | } | ||
715 | |||
716 | return count; | ||
717 | } | ||
718 | |||
719 | static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) | ||
720 | { | ||
721 | struct usblp *usblp = file->private_data; | ||
722 | DECLARE_WAITQUEUE(wait, current); | ||
723 | |||
724 | if (!usblp->bidir) | ||
725 | return -EINVAL; | ||
726 | |||
727 | down (&usblp->sem); | ||
728 | if (!usblp->present) { | ||
729 | count = -ENODEV; | ||
730 | goto done; | ||
731 | } | ||
732 | |||
733 | if (!usblp->rcomplete) { | ||
734 | barrier(); | ||
735 | |||
736 | if (file->f_flags & O_NONBLOCK) { | ||
737 | count = -EAGAIN; | ||
738 | goto done; | ||
739 | } | ||
740 | |||
741 | add_wait_queue(&usblp->wait, &wait); | ||
742 | while (1==1) { | ||
743 | if (signal_pending(current)) { | ||
744 | count = -EINTR; | ||
745 | remove_wait_queue(&usblp->wait, &wait); | ||
746 | goto done; | ||
747 | } | ||
748 | up (&usblp->sem); | ||
749 | set_current_state(TASK_INTERRUPTIBLE); | ||
750 | if (!usblp->rcomplete) { | ||
751 | schedule(); | ||
752 | } else { | ||
753 | set_current_state(TASK_RUNNING); | ||
754 | break; | ||
755 | } | ||
756 | down (&usblp->sem); | ||
757 | } | ||
758 | remove_wait_queue(&usblp->wait, &wait); | ||
759 | } | ||
760 | |||
761 | if (!usblp->present) { | ||
762 | count = -ENODEV; | ||
763 | goto done; | ||
764 | } | ||
765 | |||
766 | if (usblp->readurb->status) { | ||
767 | err("usblp%d: error %d reading from printer", | ||
768 | usblp->minor, usblp->readurb->status); | ||
769 | usblp->readurb->dev = usblp->dev; | ||
770 | usblp->readcount = 0; | ||
771 | usblp->rcomplete = 0; | ||
772 | if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) | ||
773 | dbg("error submitting urb"); | ||
774 | count = -EIO; | ||
775 | goto done; | ||
776 | } | ||
777 | |||
778 | count = count < usblp->readurb->actual_length - usblp->readcount ? | ||
779 | count : usblp->readurb->actual_length - usblp->readcount; | ||
780 | |||
781 | if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) { | ||
782 | count = -EFAULT; | ||
783 | goto done; | ||
784 | } | ||
785 | |||
786 | if ((usblp->readcount += count) == usblp->readurb->actual_length) { | ||
787 | usblp->readcount = 0; | ||
788 | usblp->readurb->dev = usblp->dev; | ||
789 | usblp->rcomplete = 0; | ||
790 | if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) { | ||
791 | count = -EIO; | ||
792 | goto done; | ||
793 | } | ||
794 | } | ||
795 | |||
796 | done: | ||
797 | up (&usblp->sem); | ||
798 | return count; | ||
799 | } | ||
800 | |||
801 | /* | ||
802 | * Checks for printers that have quirks, such as requiring unidirectional | ||
803 | * communication but reporting bidirectional; currently some HP printers | ||
804 | * have this flaw (HP 810, 880, 895, etc.), or needing an init string | ||
805 | * sent at each open (like some Epsons). | ||
806 | * Returns 1 if found, 0 if not found. | ||
807 | * | ||
808 | * HP recommended that we use the bidirectional interface but | ||
809 | * don't attempt any bulk IN transfers from the IN endpoint. | ||
810 | * Here's some more detail on the problem: | ||
811 | * The problem is not that it isn't bidirectional though. The problem | ||
812 | * is that if you request a device ID, or status information, while | ||
813 | * the buffers are full, the return data will end up in the print data | ||
814 | * buffer. For example if you make sure you never request the device ID | ||
815 | * while you are sending print data, and you don't try to query the | ||
816 | * printer status every couple of milliseconds, you will probably be OK. | ||
817 | */ | ||
818 | static unsigned int usblp_quirks (__u16 vendor, __u16 product) | ||
819 | { | ||
820 | int i; | ||
821 | |||
822 | for (i = 0; quirk_printers[i].vendorId; i++) { | ||
823 | if (vendor == quirk_printers[i].vendorId && | ||
824 | product == quirk_printers[i].productId) | ||
825 | return quirk_printers[i].quirks; | ||
826 | } | ||
827 | return 0; | ||
828 | } | ||
829 | |||
830 | static struct file_operations usblp_fops = { | ||
831 | .owner = THIS_MODULE, | ||
832 | .read = usblp_read, | ||
833 | .write = usblp_write, | ||
834 | .poll = usblp_poll, | ||
835 | .ioctl = usblp_ioctl, | ||
836 | .open = usblp_open, | ||
837 | .release = usblp_release, | ||
838 | }; | ||
839 | |||
840 | static struct usb_class_driver usblp_class = { | ||
841 | .name = "usb/lp%d", | ||
842 | .fops = &usblp_fops, | ||
843 | .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, | ||
844 | .minor_base = USBLP_MINOR_BASE, | ||
845 | }; | ||
846 | |||
847 | static int usblp_probe(struct usb_interface *intf, | ||
848 | const struct usb_device_id *id) | ||
849 | { | ||
850 | struct usb_device *dev = interface_to_usbdev (intf); | ||
851 | struct usblp *usblp = NULL; | ||
852 | int protocol; | ||
853 | int retval; | ||
854 | |||
855 | /* Malloc and start initializing usblp structure so we can use it | ||
856 | * directly. */ | ||
857 | if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) { | ||
858 | err("out of memory for usblp"); | ||
859 | goto abort; | ||
860 | } | ||
861 | memset(usblp, 0, sizeof(struct usblp)); | ||
862 | usblp->dev = dev; | ||
863 | init_MUTEX (&usblp->sem); | ||
864 | init_waitqueue_head(&usblp->wait); | ||
865 | usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; | ||
866 | usblp->intf = intf; | ||
867 | |||
868 | usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL); | ||
869 | if (!usblp->writeurb) { | ||
870 | err("out of memory"); | ||
871 | goto abort; | ||
872 | } | ||
873 | usblp->readurb = usb_alloc_urb(0, GFP_KERNEL); | ||
874 | if (!usblp->readurb) { | ||
875 | err("out of memory"); | ||
876 | goto abort; | ||
877 | } | ||
878 | |||
879 | /* Malloc device ID string buffer to the largest expected length, | ||
880 | * since we can re-query it on an ioctl and a dynamic string | ||
881 | * could change in length. */ | ||
882 | if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) { | ||
883 | err("out of memory for device_id_string"); | ||
884 | goto abort; | ||
885 | } | ||
886 | |||
887 | usblp->writebuf = usblp->readbuf = NULL; | ||
888 | usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | ||
889 | usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | ||
890 | /* Malloc write & read buffers. We somewhat wastefully | ||
891 | * malloc both regardless of bidirectionality, because the | ||
892 | * alternate setting can be changed later via an ioctl. */ | ||
893 | if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE, | ||
894 | GFP_KERNEL, &usblp->writeurb->transfer_dma))) { | ||
895 | err("out of memory for write buf"); | ||
896 | goto abort; | ||
897 | } | ||
898 | if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE, | ||
899 | GFP_KERNEL, &usblp->readurb->transfer_dma))) { | ||
900 | err("out of memory for read buf"); | ||
901 | goto abort; | ||
902 | } | ||
903 | |||
904 | /* Allocate buffer for printer status */ | ||
905 | usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL); | ||
906 | if (!usblp->statusbuf) { | ||
907 | err("out of memory for statusbuf"); | ||
908 | goto abort; | ||
909 | } | ||
910 | |||
911 | /* Lookup quirks for this printer. */ | ||
912 | usblp->quirks = usblp_quirks( | ||
913 | le16_to_cpu(dev->descriptor.idVendor), | ||
914 | le16_to_cpu(dev->descriptor.idProduct)); | ||
915 | |||
916 | /* Analyze and pick initial alternate settings and endpoints. */ | ||
917 | protocol = usblp_select_alts(usblp); | ||
918 | if (protocol < 0) { | ||
919 | dbg("incompatible printer-class device 0x%4.4X/0x%4.4X", | ||
920 | le16_to_cpu(dev->descriptor.idVendor), | ||
921 | le16_to_cpu(dev->descriptor.idProduct)); | ||
922 | goto abort; | ||
923 | } | ||
924 | |||
925 | /* Setup the selected alternate setting and endpoints. */ | ||
926 | if (usblp_set_protocol(usblp, protocol) < 0) | ||
927 | goto abort; | ||
928 | |||
929 | /* Retrieve and store the device ID string. */ | ||
930 | usblp_cache_device_id_string(usblp); | ||
931 | |||
932 | #ifdef DEBUG | ||
933 | usblp_check_status(usblp, 0); | ||
934 | #endif | ||
935 | |||
936 | info("usblp%d: USB %sdirectional printer dev %d " | ||
937 | "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X", | ||
938 | usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum, | ||
939 | usblp->ifnum, | ||
940 | usblp->protocol[usblp->current_protocol].alt_setting, | ||
941 | usblp->current_protocol, | ||
942 | le16_to_cpu(usblp->dev->descriptor.idVendor), | ||
943 | le16_to_cpu(usblp->dev->descriptor.idProduct)); | ||
944 | |||
945 | usb_set_intfdata (intf, usblp); | ||
946 | |||
947 | usblp->present = 1; | ||
948 | |||
949 | retval = usb_register_dev(intf, &usblp_class); | ||
950 | if (retval) { | ||
951 | err("Not able to get a minor for this device."); | ||
952 | goto abort_intfdata; | ||
953 | } | ||
954 | usblp->minor = intf->minor; | ||
955 | |||
956 | return 0; | ||
957 | |||
958 | abort_intfdata: | ||
959 | usb_set_intfdata (intf, NULL); | ||
960 | abort: | ||
961 | if (usblp) { | ||
962 | if (usblp->writebuf) | ||
963 | usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, | ||
964 | usblp->writebuf, usblp->writeurb->transfer_dma); | ||
965 | if (usblp->readbuf) | ||
966 | usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, | ||
967 | usblp->readbuf, usblp->writeurb->transfer_dma); | ||
968 | kfree(usblp->statusbuf); | ||
969 | kfree(usblp->device_id_string); | ||
970 | usb_free_urb(usblp->writeurb); | ||
971 | usb_free_urb(usblp->readurb); | ||
972 | kfree(usblp); | ||
973 | } | ||
974 | return -EIO; | ||
975 | } | ||
976 | |||
977 | /* | ||
978 | * We are a "new" style driver with usb_device_id table, | ||
979 | * but our requirements are too intricate for simple match to handle. | ||
980 | * | ||
981 | * The "proto_bias" option may be used to specify the preferred protocol | ||
982 | * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device | ||
983 | * supports the preferred protocol, then we bind to it. | ||
984 | * | ||
985 | * The best interface for us is 7/1/2, because it is compatible | ||
986 | * with a stream of characters. If we find it, we bind to it. | ||
987 | * | ||
988 | * Note that the people from hpoj.sourceforge.net need to be able to | ||
989 | * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose. | ||
990 | * | ||
991 | * Failing 7/1/2, we look for 7/1/3, even though it's probably not | ||
992 | * stream-compatible, because this matches the behaviour of the old code. | ||
993 | * | ||
994 | * If nothing else, we bind to 7/1/1 - the unidirectional interface. | ||
995 | */ | ||
996 | static int usblp_select_alts(struct usblp *usblp) | ||
997 | { | ||
998 | struct usb_interface *if_alt; | ||
999 | struct usb_host_interface *ifd; | ||
1000 | struct usb_endpoint_descriptor *epd, *epwrite, *epread; | ||
1001 | int p, i, e; | ||
1002 | |||
1003 | if_alt = usblp->intf; | ||
1004 | |||
1005 | for (p = 0; p < USBLP_MAX_PROTOCOLS; p++) | ||
1006 | usblp->protocol[p].alt_setting = -1; | ||
1007 | |||
1008 | /* Find out what we have. */ | ||
1009 | for (i = 0; i < if_alt->num_altsetting; i++) { | ||
1010 | ifd = &if_alt->altsetting[i]; | ||
1011 | |||
1012 | if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1) | ||
1013 | continue; | ||
1014 | |||
1015 | if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL || | ||
1016 | ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL) | ||
1017 | continue; | ||
1018 | |||
1019 | /* Look for bulk OUT and IN endpoints. */ | ||
1020 | epwrite = epread = NULL; | ||
1021 | for (e = 0; e < ifd->desc.bNumEndpoints; e++) { | ||
1022 | epd = &ifd->endpoint[e].desc; | ||
1023 | |||
1024 | if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!= | ||
1025 | USB_ENDPOINT_XFER_BULK) | ||
1026 | continue; | ||
1027 | |||
1028 | if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) { | ||
1029 | if (!epwrite) | ||
1030 | epwrite = epd; | ||
1031 | |||
1032 | } else { | ||
1033 | if (!epread) | ||
1034 | epread = epd; | ||
1035 | } | ||
1036 | } | ||
1037 | |||
1038 | /* Ignore buggy hardware without the right endpoints. */ | ||
1039 | if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread)) | ||
1040 | continue; | ||
1041 | |||
1042 | /* Turn off reads for 7/1/1 (unidirectional) interfaces | ||
1043 | * and buggy bidirectional printers. */ | ||
1044 | if (ifd->desc.bInterfaceProtocol == 1) { | ||
1045 | epread = NULL; | ||
1046 | } else if (usblp->quirks & USBLP_QUIRK_BIDIR) { | ||
1047 | info("Disabling reads from problem bidirectional " | ||
1048 | "printer on usblp%d", usblp->minor); | ||
1049 | epread = NULL; | ||
1050 | } | ||
1051 | |||
1052 | usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting = | ||
1053 | ifd->desc.bAlternateSetting; | ||
1054 | usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite; | ||
1055 | usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread; | ||
1056 | } | ||
1057 | |||
1058 | /* If our requested protocol is supported, then use it. */ | ||
1059 | if (proto_bias >= USBLP_FIRST_PROTOCOL && | ||
1060 | proto_bias <= USBLP_LAST_PROTOCOL && | ||
1061 | usblp->protocol[proto_bias].alt_setting != -1) | ||
1062 | return proto_bias; | ||
1063 | |||
1064 | /* Ordering is important here. */ | ||
1065 | if (usblp->protocol[2].alt_setting != -1) | ||
1066 | return 2; | ||
1067 | if (usblp->protocol[1].alt_setting != -1) | ||
1068 | return 1; | ||
1069 | if (usblp->protocol[3].alt_setting != -1) | ||
1070 | return 3; | ||
1071 | |||
1072 | /* If nothing is available, then don't bind to this device. */ | ||
1073 | return -1; | ||
1074 | } | ||
1075 | |||
1076 | static int usblp_set_protocol(struct usblp *usblp, int protocol) | ||
1077 | { | ||
1078 | int r, alts; | ||
1079 | |||
1080 | if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL) | ||
1081 | return -EINVAL; | ||
1082 | |||
1083 | alts = usblp->protocol[protocol].alt_setting; | ||
1084 | if (alts < 0) | ||
1085 | return -EINVAL; | ||
1086 | r = usb_set_interface(usblp->dev, usblp->ifnum, alts); | ||
1087 | if (r < 0) { | ||
1088 | err("can't set desired altsetting %d on interface %d", | ||
1089 | alts, usblp->ifnum); | ||
1090 | return r; | ||
1091 | } | ||
1092 | |||
1093 | usb_fill_bulk_urb(usblp->writeurb, usblp->dev, | ||
1094 | usb_sndbulkpipe(usblp->dev, | ||
1095 | usblp->protocol[protocol].epwrite->bEndpointAddress), | ||
1096 | usblp->writebuf, 0, | ||
1097 | usblp_bulk_write, usblp); | ||
1098 | |||
1099 | usblp->bidir = (usblp->protocol[protocol].epread != NULL); | ||
1100 | if (usblp->bidir) | ||
1101 | usb_fill_bulk_urb(usblp->readurb, usblp->dev, | ||
1102 | usb_rcvbulkpipe(usblp->dev, | ||
1103 | usblp->protocol[protocol].epread->bEndpointAddress), | ||
1104 | usblp->readbuf, USBLP_BUF_SIZE, | ||
1105 | usblp_bulk_read, usblp); | ||
1106 | |||
1107 | usblp->current_protocol = protocol; | ||
1108 | dbg("usblp%d set protocol %d", usblp->minor, protocol); | ||
1109 | return 0; | ||
1110 | } | ||
1111 | |||
1112 | /* Retrieves and caches device ID string. | ||
1113 | * Returns length, including length bytes but not null terminator. | ||
1114 | * On error, returns a negative errno value. */ | ||
1115 | static int usblp_cache_device_id_string(struct usblp *usblp) | ||
1116 | { | ||
1117 | int err, length; | ||
1118 | |||
1119 | err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1); | ||
1120 | if (err < 0) { | ||
1121 | dbg("usblp%d: error = %d reading IEEE-1284 Device ID string", | ||
1122 | usblp->minor, err); | ||
1123 | usblp->device_id_string[0] = usblp->device_id_string[1] = '\0'; | ||
1124 | return -EIO; | ||
1125 | } | ||
1126 | |||
1127 | /* First two bytes are length in big-endian. | ||
1128 | * They count themselves, and we copy them into | ||
1129 | * the user's buffer. */ | ||
1130 | length = be16_to_cpu(*((__be16 *)usblp->device_id_string)); | ||
1131 | if (length < 2) | ||
1132 | length = 2; | ||
1133 | else if (length >= USBLP_DEVICE_ID_SIZE) | ||
1134 | length = USBLP_DEVICE_ID_SIZE - 1; | ||
1135 | usblp->device_id_string[length] = '\0'; | ||
1136 | |||
1137 | dbg("usblp%d Device ID string [len=%d]=\"%s\"", | ||
1138 | usblp->minor, length, &usblp->device_id_string[2]); | ||
1139 | |||
1140 | return length; | ||
1141 | } | ||
1142 | |||
1143 | static void usblp_disconnect(struct usb_interface *intf) | ||
1144 | { | ||
1145 | struct usblp *usblp = usb_get_intfdata (intf); | ||
1146 | |||
1147 | usb_deregister_dev(intf, &usblp_class); | ||
1148 | |||
1149 | if (!usblp || !usblp->dev) { | ||
1150 | err("bogus disconnect"); | ||
1151 | BUG (); | ||
1152 | } | ||
1153 | |||
1154 | down (&usblp_sem); | ||
1155 | down (&usblp->sem); | ||
1156 | usblp->present = 0; | ||
1157 | usb_set_intfdata (intf, NULL); | ||
1158 | |||
1159 | usblp_unlink_urbs(usblp); | ||
1160 | usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, | ||
1161 | usblp->writebuf, usblp->writeurb->transfer_dma); | ||
1162 | usb_buffer_free (usblp->dev, USBLP_BUF_SIZE, | ||
1163 | usblp->readbuf, usblp->readurb->transfer_dma); | ||
1164 | up (&usblp->sem); | ||
1165 | |||
1166 | if (!usblp->used) | ||
1167 | usblp_cleanup (usblp); | ||
1168 | up (&usblp_sem); | ||
1169 | } | ||
1170 | |||
1171 | static struct usb_device_id usblp_ids [] = { | ||
1172 | { USB_DEVICE_INFO(7, 1, 1) }, | ||
1173 | { USB_DEVICE_INFO(7, 1, 2) }, | ||
1174 | { USB_DEVICE_INFO(7, 1, 3) }, | ||
1175 | { USB_INTERFACE_INFO(7, 1, 1) }, | ||
1176 | { USB_INTERFACE_INFO(7, 1, 2) }, | ||
1177 | { USB_INTERFACE_INFO(7, 1, 3) }, | ||
1178 | { } /* Terminating entry */ | ||
1179 | }; | ||
1180 | |||
1181 | MODULE_DEVICE_TABLE (usb, usblp_ids); | ||
1182 | |||
1183 | static struct usb_driver usblp_driver = { | ||
1184 | .owner = THIS_MODULE, | ||
1185 | .name = "usblp", | ||
1186 | .probe = usblp_probe, | ||
1187 | .disconnect = usblp_disconnect, | ||
1188 | .id_table = usblp_ids, | ||
1189 | }; | ||
1190 | |||
1191 | static int __init usblp_init(void) | ||
1192 | { | ||
1193 | int retval; | ||
1194 | retval = usb_register(&usblp_driver); | ||
1195 | if (retval) | ||
1196 | goto out; | ||
1197 | info(DRIVER_VERSION ": " DRIVER_DESC); | ||
1198 | out: | ||
1199 | return retval; | ||
1200 | } | ||
1201 | |||
1202 | static void __exit usblp_exit(void) | ||
1203 | { | ||
1204 | usb_deregister(&usblp_driver); | ||
1205 | } | ||
1206 | |||
1207 | module_init(usblp_init); | ||
1208 | module_exit(usblp_exit); | ||
1209 | |||
1210 | MODULE_AUTHOR( DRIVER_AUTHOR ); | ||
1211 | MODULE_DESCRIPTION( DRIVER_DESC ); | ||
1212 | module_param(proto_bias, int, S_IRUGO | S_IWUSR); | ||
1213 | MODULE_PARM_DESC(proto_bias, "Favourite protocol number"); | ||
1214 | MODULE_LICENSE("GPL"); | ||