]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/gadget/legacy/gmidi.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / legacy / gmidi.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
f2ebf92c
BW
2/*
3 * gmidi.c -- USB MIDI Gadget Driver
4 *
5 * Copyright (C) 2006 Thumtronics Pty Ltd.
6 * Developed for Thumtronics by Grey Innovation
7 * Ben Williamson <ben.williamson@greyinnovation.com>
8 *
9 * This software is distributed under the terms of the GNU General Public
10 * License ("GPL") version 2, as published by the Free Software Foundation.
11 *
12 * This code is based in part on:
13 *
14 * Gadget Zero driver, Copyright (C) 2003-2004 David Brownell.
15 * USB Audio driver, Copyright (C) 2002 by Takashi Iwai.
16 * USB MIDI driver, Copyright (C) 2002-2005 Clemens Ladisch.
17 *
18 * Refer to the USB Device Class Definition for MIDI Devices:
19 * http://www.usb.org/developers/devclass_docs/midi10.pdf
20 */
21
8c070216 22/* #define VERBOSE_DEBUG */
f2ebf92c 23
f2ebf92c 24#include <linux/kernel.h>
6eb0de82 25#include <linux/module.h>
f2ebf92c 26
f2ebf92c 27#include <sound/initval.h>
f2ebf92c 28
721e2e91 29#include <linux/usb/composite.h>
9454a57a 30#include <linux/usb/gadget.h>
f2ebf92c 31
32522a51 32#include "u_midi.h"
77f754c4
DB
33
34/*-------------------------------------------------------------------------*/
35
f2ebf92c
BW
36MODULE_AUTHOR("Ben Williamson");
37MODULE_LICENSE("GPL v2");
38
f2ebf92c
BW
39static const char longname[] = "MIDI Gadget";
40
7d16e8d3
SAS
41USB_GADGET_COMPOSITE_OPTIONS();
42
f2ebf92c 43static int index = SNDRV_DEFAULT_IDX1;
2672eea5 44module_param(index, int, S_IRUGO);
f2ebf92c 45MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
f2ebf92c 46
2672eea5
DM
47static char *id = SNDRV_DEFAULT_STR1;
48module_param(id, charp, S_IRUGO);
49MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
f2ebf92c 50
7ea9fde7 51static unsigned int buflen = 512;
f2ebf92c 52module_param(buflen, uint, S_IRUGO);
2672eea5 53MODULE_PARM_DESC(buflen, "MIDI buffer length");
f2ebf92c 54
2672eea5
DM
55static unsigned int qlen = 32;
56module_param(qlen, uint, S_IRUGO);
e1e3d7ec 57MODULE_PARM_DESC(qlen, "USB read and write request queue length");
f2ebf92c 58
c8933c3f
DM
59static unsigned int in_ports = 1;
60module_param(in_ports, uint, S_IRUGO);
61MODULE_PARM_DESC(in_ports, "Number of MIDI input ports");
62
63static unsigned int out_ports = 1;
64module_param(out_ports, uint, S_IRUGO);
65MODULE_PARM_DESC(out_ports, "Number of MIDI output ports");
66
f2ebf92c
BW
67/* Thanks to Grey Innovation for donating this product ID.
68 *
69 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
70 * Instead: allocate your own, using normal USB-IF procedures.
71 */
72#define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
73#define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
74
2672eea5 75/* string IDs are assigned dynamically */
f2ebf92c 76
276e2e4f 77#define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
f2ebf92c 78
f2ebf92c
BW
79static struct usb_device_descriptor device_desc = {
80 .bLength = USB_DT_DEVICE_SIZE,
81 .bDescriptorType = USB_DT_DEVICE,
0aecfc1b 82 /* .bcdUSB = DYNAMIC */
f2ebf92c 83 .bDeviceClass = USB_CLASS_PER_INTERFACE,
b8464bcf
VT
84 .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
85 .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
2672eea5
DM
86 /* .iManufacturer = DYNAMIC */
87 /* .iProduct = DYNAMIC */
f2ebf92c
BW
88 .bNumConfigurations = 1,
89};
90
2672eea5 91static struct usb_string strings_dev[] = {
276e2e4f
SAS
92 [USB_GADGET_MANUFACTURER_IDX].s = "Grey Innovation",
93 [USB_GADGET_PRODUCT_IDX].s = "MIDI Gadget",
94 [USB_GADGET_SERIAL_IDX].s = "",
2672eea5
DM
95 [STRING_DESCRIPTION_IDX].s = "MIDI",
96 { } /* end of list */
f2ebf92c
BW
97};
98
2672eea5 99static struct usb_gadget_strings stringtab_dev = {
f2ebf92c 100 .language = 0x0409, /* en-us */
2672eea5 101 .strings = strings_dev,
f2ebf92c
BW
102};
103
2672eea5
DM
104static struct usb_gadget_strings *dev_strings[] = {
105 &stringtab_dev,
106 NULL,
f2ebf92c
BW
107};
108
f509fee8
FW
109static struct usb_function_instance *fi_midi;
110static struct usb_function *f_midi;
32522a51 111
c94e289f 112static int midi_unbind(struct usb_composite_dev *dev)
f2ebf92c 113{
32522a51
AP
114 usb_put_function(f_midi);
115 usb_put_function_instance(fi_midi);
f2ebf92c
BW
116 return 0;
117}
118
2672eea5
DM
119static struct usb_configuration midi_config = {
120 .label = "MIDI Gadget",
121 .bConfigurationValue = 1,
122 /* .iConfiguration = DYNAMIC */
123 .bmAttributes = USB_CONFIG_ATT_ONE,
8f900a9a 124 .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
f2ebf92c
BW
125};
126
c94e289f 127static int midi_bind_config(struct usb_configuration *c)
f2ebf92c 128{
32522a51
AP
129 int status;
130
131 f_midi = usb_get_function(fi_midi);
132 if (IS_ERR(f_midi))
133 return PTR_ERR(f_midi);
134
135 status = usb_add_function(c, f_midi);
136 if (status < 0) {
137 usb_put_function(f_midi);
138 return status;
139 }
140
141 return 0;
f2ebf92c
BW
142}
143
c94e289f 144static int midi_bind(struct usb_composite_dev *cdev)
f2ebf92c 145{
32522a51 146 struct f_midi_opts *midi_opts;
ed9cbda6 147 int status;
f2ebf92c 148
32522a51
AP
149 fi_midi = usb_get_function_instance("midi");
150 if (IS_ERR(fi_midi))
151 return PTR_ERR(fi_midi);
152
153 midi_opts = container_of(fi_midi, struct f_midi_opts, func_inst);
154 midi_opts->index = index;
155 midi_opts->id = id;
156 midi_opts->in_ports = in_ports;
157 midi_opts->out_ports = out_ports;
158 midi_opts->buflen = buflen;
159 midi_opts->qlen = qlen;
160
e1f15ccb 161 status = usb_string_ids_tab(cdev, strings_dev);
2672eea5 162 if (status < 0)
32522a51 163 goto put;
276e2e4f
SAS
164 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
165 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
e1f15ccb 166 midi_config.iConfiguration = strings_dev[STRING_DESCRIPTION_IDX].id;
f2ebf92c 167
2672eea5
DM
168 status = usb_add_config(cdev, &midi_config, midi_bind_config);
169 if (status < 0)
32522a51 170 goto put;
7d16e8d3 171 usb_composite_overwrite_options(cdev, &coverwrite);
2672eea5 172 pr_info("%s\n", longname);
f2ebf92c 173 return 0;
32522a51
AP
174put:
175 usb_put_function_instance(fi_midi);
176 return status;
f2ebf92c
BW
177}
178
c94e289f 179static struct usb_composite_driver midi_driver = {
2672eea5
DM
180 .name = (char *) longname,
181 .dev = &device_desc,
182 .strings = dev_strings,
183 .max_speed = USB_SPEED_HIGH,
03e42bd5 184 .bind = midi_bind,
c94e289f 185 .unbind = midi_unbind,
f2ebf92c
BW
186};
187
909346a8 188module_usb_composite_driver(midi_driver);