2 * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
3 * MMC (Microscheduled Management Command) handling
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
23 * WUIEs and MMC IEs...well, they are almost the same at the end. MMC
24 * IEs are Wireless USB IEs that go into the MMC period...[what is
25 * that? look in Design-overview.txt].
28 * This is a simple subsystem to keep track of which IEs are being
29 * sent by the host in the MMC period.
31 * For each WUIE we ask to send, we keep it in an array, so we can
32 * request its removal later, or replace the content. They are tracked
33 * by pointer, so be sure to use the same pointer if you want to
34 * remove it or update the contents.
37 * - add timers that autoremove intervalled IEs?
39 #include <linux/usb/wusb.h>
40 #include <linux/slab.h>
43 /* Initialize the MMCIEs handling mechanism */
44 int wusbhc_mmcie_create(struct wusbhc
*wusbhc
)
46 u8 mmcies
= wusbhc
->mmcies_max
;
47 wusbhc
->mmcie
= kcalloc(mmcies
, sizeof(wusbhc
->mmcie
[0]), GFP_KERNEL
);
48 if (wusbhc
->mmcie
== NULL
)
50 mutex_init(&wusbhc
->mmcie_mutex
);
54 /* Release resources used by the MMCIEs handling mechanism */
55 void wusbhc_mmcie_destroy(struct wusbhc
*wusbhc
)
61 * Add or replace an MMC Wireless USB IE.
63 * @interval: See WUSB1.0[8.5.3.1]
64 * @repeat_cnt: See WUSB1.0[8.5.3.1]
65 * @handle: See WUSB1.0[8.5.3.1]
66 * @wuie: Pointer to the header of the WUSB IE data to add.
67 * MUST BE allocated in a kmalloc buffer (no stack or
69 * THE CALLER ALWAYS OWNS THE POINTER (we don't free it
70 * on remove, we just forget about it).
71 * @returns: 0 if ok, < 0 errno code on error.
73 * Goes over the *whole* @wusbhc->mmcie array looking for (a) the
74 * first free spot and (b) if @wuie is already in the array (aka:
75 * transmitted in the MMCs) the spot were it is.
77 * If present, we "overwrite it" (update).
80 * NOTE: Need special ordering rules -- see below WUSB1.0 Table 7-38.
81 * The host uses the handle as the 'sort' index. We
82 * allocate the last one always for the WUIE_ID_HOST_INFO, and
83 * the rest, first come first serve in inverse order.
85 * Host software must make sure that it adds the other IEs in
86 * the right order... the host hardware is responsible for
87 * placing the WCTA IEs in the right place with the other IEs
88 * set by host software.
90 * NOTE: we can access wusbhc->wa_descr without locking because it is
93 int wusbhc_mmcie_set(struct wusbhc
*wusbhc
, u8 interval
, u8 repeat_cnt
,
94 struct wuie_hdr
*wuie
)
96 int result
= -ENOBUFS
;
99 /* Search a handle, taking into account the ordering */
100 mutex_lock(&wusbhc
->mmcie_mutex
);
101 switch (wuie
->bIEIdentifier
) {
102 case WUIE_ID_HOST_INFO
:
104 handle
= wusbhc
->mmcies_max
- 1;
106 case WUIE_ID_ISOCH_DISCARD
:
107 dev_err(wusbhc
->dev
, "Special ordering case for WUIE ID 0x%x "
108 "unimplemented\n", wuie
->bIEIdentifier
);
112 /* search for it or find the last empty slot */
114 for (itr
= 0; itr
< wusbhc
->mmcies_max
- 1; itr
++) {
115 if (wusbhc
->mmcie
[itr
] == wuie
) {
119 if (wusbhc
->mmcie
[itr
] == NULL
)
125 result
= (wusbhc
->mmcie_add
)(wusbhc
, interval
, repeat_cnt
, handle
,
128 wusbhc
->mmcie
[handle
] = wuie
;
130 mutex_unlock(&wusbhc
->mmcie_mutex
);
133 EXPORT_SYMBOL_GPL(wusbhc_mmcie_set
);
136 * Remove an MMC IE previously added with wusbhc_mmcie_set()
138 * @wuie Pointer used to add the WUIE
140 void wusbhc_mmcie_rm(struct wusbhc
*wusbhc
, struct wuie_hdr
*wuie
)
143 unsigned handle
, itr
;
145 mutex_lock(&wusbhc
->mmcie_mutex
);
146 for (itr
= 0; itr
< wusbhc
->mmcies_max
; itr
++) {
147 if (wusbhc
->mmcie
[itr
] == wuie
) {
152 mutex_unlock(&wusbhc
->mmcie_mutex
);
156 result
= (wusbhc
->mmcie_rm
)(wusbhc
, handle
);
158 wusbhc
->mmcie
[itr
] = NULL
;
159 mutex_unlock(&wusbhc
->mmcie_mutex
);
161 EXPORT_SYMBOL_GPL(wusbhc_mmcie_rm
);
163 static int wusbhc_mmc_start(struct wusbhc
*wusbhc
)
167 mutex_lock(&wusbhc
->mutex
);
168 ret
= wusbhc
->start(wusbhc
);
171 mutex_unlock(&wusbhc
->mutex
);
176 static void wusbhc_mmc_stop(struct wusbhc
*wusbhc
)
178 mutex_lock(&wusbhc
->mutex
);
180 wusbhc
->stop(wusbhc
, WUSB_CHANNEL_STOP_DELAY_MS
);
181 mutex_unlock(&wusbhc
->mutex
);
185 * wusbhc_start - start transmitting MMCs and accepting connections
186 * @wusbhc: the HC to start
188 * Establishes a cluster reservation, enables device connections, and
189 * starts MMCs with appropriate DNTS parameters.
191 int wusbhc_start(struct wusbhc
*wusbhc
)
194 struct device
*dev
= wusbhc
->dev
;
196 WARN_ON(wusbhc
->wuie_host_info
!= NULL
);
198 result
= wusbhc_rsv_establish(wusbhc
);
200 dev_err(dev
, "cannot establish cluster reservation: %d\n",
202 goto error_rsv_establish
;
205 result
= wusbhc_devconnect_start(wusbhc
);
207 dev_err(dev
, "error enabling device connections: %d\n", result
);
208 goto error_devconnect_start
;
211 result
= wusbhc_sec_start(wusbhc
);
213 dev_err(dev
, "error starting security in the HC: %d\n", result
);
214 goto error_sec_start
;
216 /* FIXME: the choice of the DNTS parameters is somewhat
218 result
= wusbhc
->set_num_dnts(wusbhc
, 0, 15);
220 dev_err(dev
, "Cannot set DNTS parameters: %d\n", result
);
221 goto error_set_num_dnts
;
223 result
= wusbhc_mmc_start(wusbhc
);
225 dev_err(dev
, "error starting wusbch: %d\n", result
);
226 goto error_wusbhc_start
;
232 wusbhc_sec_stop(wusbhc
);
235 wusbhc_devconnect_stop(wusbhc
);
236 error_devconnect_start
:
237 wusbhc_rsv_terminate(wusbhc
);
243 * wusbhc_stop - stop transmitting MMCs
244 * @wusbhc: the HC to stop
246 * Stops the WUSB channel and removes the cluster reservation.
248 void wusbhc_stop(struct wusbhc
*wusbhc
)
250 wusbhc_mmc_stop(wusbhc
);
251 wusbhc_sec_stop(wusbhc
);
252 wusbhc_devconnect_stop(wusbhc
);
253 wusbhc_rsv_terminate(wusbhc
);
257 * Set/reset/update a new CHID
259 * Depending on the previous state of the MMCs, start, stop or change
260 * the sent MMC. This effectively switches the host controller on and
263 int wusbhc_chid_set(struct wusbhc
*wusbhc
, const struct wusb_ckhdid
*chid
)
267 if (memcmp(chid
, &wusb_ckhdid_zero
, sizeof(*chid
)) == 0)
270 mutex_lock(&wusbhc
->mutex
);
272 if (wusbhc
->active
) {
273 mutex_unlock(&wusbhc
->mutex
);
276 wusbhc
->chid
= *chid
;
278 mutex_unlock(&wusbhc
->mutex
);
281 result
= uwb_radio_start(&wusbhc
->pal
);
283 uwb_radio_stop(&wusbhc
->pal
);
286 EXPORT_SYMBOL_GPL(wusbhc_chid_set
);