usb4bsd: FWIW, port usb_template and hook it into the build.
[dragonfly.git] / sys / bus / u4b / template / usb_template_modem.c
1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_modem.c 246125 2013-01-30 16:05:54Z hselasky $ */
2 /*-
3  * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * This file contains the USB template for an USB Modem Device.
29  */
30
31 #ifdef USB_GLOBAL_INCLUDE_FILE
32 #include USB_GLOBAL_INCLUDE_FILE
33 #else
34 #include <sys/stdint.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/lock.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49
50 #include <bus/u4b/usb.h>
51 #include <bus/u4b/usbdi.h>
52 #include <bus/u4b/usb_core.h>
53 #include <bus/u4b/usb_cdc.h>
54
55 #include <bus/u4b/template/usb_template.h>
56 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
57
58 enum {
59         INDEX_LANG,
60         INDEX_MODEM,
61         INDEX_PRODUCT,
62         INDEX_MAX,
63 };
64
65 #define STRING_PRODUCT \
66   "M\0o\0d\0e\0m\0 \0T\0e\0s\0t\0 \0D\0e\0v\0i\0c\0e"
67
68 #define STRING_MODEM \
69   "M\0o\0d\0e\0m\0 \0i\0n\0t\0e\0r\0f\0a\0c\0e"
70
71 /* make the real string descriptors */
72
73 USB_MAKE_STRING_DESC(STRING_MODEM, string_modem);
74 USB_MAKE_STRING_DESC(STRING_PRODUCT, string_product);
75
76 #define MODEM_IFACE_0 0
77 #define MODEM_IFACE_1 1
78
79 /* prototypes */
80
81 static const struct usb_temp_packet_size modem_bulk_mps = {
82         .mps[USB_SPEED_LOW] = 8,
83         .mps[USB_SPEED_FULL] = 64,
84         .mps[USB_SPEED_HIGH] = 512,
85 };
86
87 static const struct usb_temp_packet_size modem_intr_mps = {
88         .mps[USB_SPEED_LOW] = 8,
89         .mps[USB_SPEED_FULL] = 8,
90         .mps[USB_SPEED_HIGH] = 8,
91 };
92
93 static const struct usb_temp_interval modem_intr_interval = {
94         .bInterval[USB_SPEED_LOW] = 8,  /* 8ms */
95         .bInterval[USB_SPEED_FULL] = 8, /* 8ms */
96         .bInterval[USB_SPEED_HIGH] = 7, /* 8ms */
97 };
98
99 static const struct usb_temp_endpoint_desc modem_ep_0 = {
100         .pPacketSize = &modem_intr_mps,
101         .pIntervals = &modem_intr_interval,
102         .bEndpointAddress = UE_DIR_IN,
103         .bmAttributes = UE_INTERRUPT,
104 };
105
106 static const struct usb_temp_endpoint_desc modem_ep_1 = {
107         .pPacketSize = &modem_bulk_mps,
108         .bEndpointAddress = UE_DIR_OUT,
109         .bmAttributes = UE_BULK,
110 };
111
112 static const struct usb_temp_endpoint_desc modem_ep_2 = {
113         .pPacketSize = &modem_bulk_mps,
114         .bEndpointAddress = UE_DIR_IN,
115         .bmAttributes = UE_BULK,
116 };
117
118 static const struct usb_temp_endpoint_desc *modem_iface_0_ep[] = {
119         &modem_ep_0,
120         NULL,
121 };
122
123 static const struct usb_temp_endpoint_desc *modem_iface_1_ep[] = {
124         &modem_ep_1,
125         &modem_ep_2,
126         NULL,
127 };
128
129 static const uint8_t modem_raw_desc_0[] = {
130         0x05, 0x24, 0x00, 0x10, 0x01
131 };
132
133 static const uint8_t modem_raw_desc_1[] = {
134         0x05, 0x24, 0x06, MODEM_IFACE_0, MODEM_IFACE_1
135 };
136
137 static const uint8_t modem_raw_desc_2[] = {
138         0x05, 0x24, 0x01, 0x03, MODEM_IFACE_1
139 };
140
141 static const uint8_t modem_raw_desc_3[] = {
142         0x04, 0x24, 0x02, 0x07
143 };
144
145 static const void *modem_iface_0_desc[] = {
146         &modem_raw_desc_0,
147         &modem_raw_desc_1,
148         &modem_raw_desc_2,
149         &modem_raw_desc_3,
150         NULL,
151 };
152
153 static const struct usb_temp_interface_desc modem_iface_0 = {
154         .ppRawDesc = modem_iface_0_desc,
155         .ppEndpoints = modem_iface_0_ep,
156         .bInterfaceClass = 2,
157         .bInterfaceSubClass = 2,
158         .bInterfaceProtocol = 1,
159         .iInterface = INDEX_MODEM,
160 };
161
162 static const struct usb_temp_interface_desc modem_iface_1 = {
163         .ppEndpoints = modem_iface_1_ep,
164         .bInterfaceClass = 10,
165         .bInterfaceSubClass = 0,
166         .bInterfaceProtocol = 0,
167         .iInterface = INDEX_MODEM,
168 };
169
170 static const struct usb_temp_interface_desc *modem_interfaces[] = {
171         &modem_iface_0,
172         &modem_iface_1,
173         NULL,
174 };
175
176 static const struct usb_temp_config_desc modem_config_desc = {
177         .ppIfaceDesc = modem_interfaces,
178         .bmAttributes = UC_BUS_POWERED,
179         .bMaxPower = 25,                /* 50 mA */
180         .iConfiguration = INDEX_PRODUCT,
181 };
182
183 static const struct usb_temp_config_desc *modem_configs[] = {
184         &modem_config_desc,
185         NULL,
186 };
187
188 static usb_temp_get_string_desc_t modem_get_string_desc;
189 static usb_temp_get_vendor_desc_t modem_get_vendor_desc;
190
191 const struct usb_temp_device_desc usb_template_modem = {
192         .getStringDesc = &modem_get_string_desc,
193         .getVendorDesc = &modem_get_vendor_desc,
194         .ppConfigDesc = modem_configs,
195         .idVendor = USB_TEMPLATE_VENDOR,
196         .idProduct = 0x000E,
197         .bcdDevice = 0x0100,
198         .bDeviceClass = UDCLASS_COMM,
199         .bDeviceSubClass = 0,
200         .bDeviceProtocol = 0,
201         .iManufacturer = 0,
202         .iProduct = INDEX_PRODUCT,
203         .iSerialNumber = 0,
204 };
205
206 /*------------------------------------------------------------------------*
207  *      modem_get_vendor_desc
208  *
209  * Return values:
210  * NULL: Failure. No such vendor descriptor.
211  * Else: Success. Pointer to vendor descriptor is returned.
212  *------------------------------------------------------------------------*/
213 static const void *
214 modem_get_vendor_desc(const struct usb_device_request *req, uint16_t *plen)
215 {
216         return (NULL);
217 }
218
219 /*------------------------------------------------------------------------*
220  *      modem_get_string_desc
221  *
222  * Return values:
223  * NULL: Failure. No such string.
224  * Else: Success. Pointer to string descriptor is returned.
225  *------------------------------------------------------------------------*/
226 static const void *
227 modem_get_string_desc(uint16_t lang_id, uint8_t string_index)
228 {
229         static const void *ptr[INDEX_MAX] = {
230                 [INDEX_LANG] = &usb_string_lang_en,
231                 [INDEX_MODEM] = &string_modem,
232                 [INDEX_PRODUCT] = &string_product,
233         };
234
235         if (string_index == 0) {
236                 return (&usb_string_lang_en);
237         }
238         if (lang_id != 0x0409) {
239                 return (NULL);
240         }
241         if (string_index < INDEX_MAX) {
242                 return (ptr[string_index]);
243         }
244         return (NULL);
245 }