blob: 25eae405f62252bd9cffef3c4dba28ab100ad1de [file] [log] [blame]
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +01001/*
2 * Wireless USB - Cable Based Association
3 *
4 *
5 * Copyright (C) 2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +01007 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +01008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010024 * WUSB devices have to be paired (associated in WUSB lingo) so
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010025 * that they can connect to the system.
26 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010027 * One way of pairing is using CBA-Cable Based Association. First
28 * time you plug the device with a cable, association is done between
29 * host and device and subsequent times, you can connect wirelessly
30 * without having to associate again. That's the idea.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010031 *
32 * This driver does nothing Earth shattering. It just provides an
33 * interface to chat with the wire-connected device so we can get a
34 * CDID (device ID) that might have been previously associated to a
35 * CHID (host ID) and to set up a new <CHID,CDID,CK> triplet
36 * (connection context), with the CK being the secret, or connection
37 * key. This is the pairing data.
38 *
39 * When a device with the CBA capability connects, the probe routine
40 * just creates a bunch of sysfs files that a user space enumeration
41 * manager uses to allow it to connect wirelessly to the system or not.
42 *
43 * The process goes like this:
44 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010045 * 1. Device plugs, cbaf is loaded, notifications happen.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010046 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010047 * 2. The connection manager (CM) sees a device with CBAF capability
48 * (the wusb_chid etc. files in /sys/devices/blah/OURDEVICE).
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010049 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010050 * 3. The CM writes the host name, supported band groups, and the CHID
51 * (host ID) into the wusb_host_name, wusb_host_band_groups and
52 * wusb_chid files. These get sent to the device and the CDID (if
53 * any) for this host is requested.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010054 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010055 * 4. The CM can verify that the device's supported band groups
56 * (wusb_device_band_groups) are compatible with the host.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010057 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010058 * 5. The CM reads the wusb_cdid file.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010059 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010060 * 6. The CM looks up its database
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010061 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010062 * 6.1 If it has a matching CHID,CDID entry, the device has been
63 * authorized before (paired) and nothing further needs to be
64 * done.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010065 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010066 * 6.2 If the CDID is zero (or the CM doesn't find a matching CDID in
67 * its database), the device is assumed to be not known. The CM
68 * may associate the host with device by: writing a randomly
69 * generated CDID to wusb_cdid and then a random CK to wusb_ck
70 * (this uploads the new CC to the device).
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010071 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010072 * CMD may choose to prompt the user before associating with a new
73 * device.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010074 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010075 * 7. Device is unplugged.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010076 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010077 * When the device tries to connect wirelessly, it will present its
78 * CDID to the WUSB host controller. The CM will query the
79 * database. If the CHID/CDID pair found, it will (with a 4-way
80 * handshake) challenge the device to demonstrate it has the CK secret
81 * key (from our database) without actually exchanging it. Once
82 * satisfied, crypto keys are derived from the CK, the device is
83 * connected and all communication is encrypted.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010084 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +010085 * References:
86 * [WUSB-AM] Association Models Supplement to the Certified Wireless
87 * Universal Serial Bus Specification, version 1.0.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010088 */
89#include <linux/module.h>
90#include <linux/ctype.h>
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +010091#include <linux/usb.h>
92#include <linux/interrupt.h>
93#include <linux/delay.h>
94#include <linux/random.h>
95#include <linux/mutex.h>
96#include <linux/uwb.h>
97#include <linux/usb/wusb.h>
98#include <linux/usb/association.h>
99
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100100#define CBA_NAME_LEN 0x40 /* [WUSB-AM] table 4-7 */
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100101
102/* An instance of a Cable-Based-Association-Framework device */
103struct cbaf {
104 struct usb_device *usb_dev;
105 struct usb_interface *usb_iface;
106 void *buffer;
107 size_t buffer_size;
108
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100109 struct wusb_ckhdid chid;
110 char host_name[CBA_NAME_LEN];
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100111 u16 host_band_groups;
112
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100113 struct wusb_ckhdid cdid;
114 char device_name[CBA_NAME_LEN];
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100115 u16 device_band_groups;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100116
117 struct wusb_ckhdid ck;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100118};
119
120/*
121 * Verify that a CBAF USB-interface has what we need
122 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100123 * According to [WUSB-AM], CBA devices should provide at least two
124 * interfaces:
125 * - RETRIEVE_HOST_INFO
126 * - ASSOCIATE
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100127 *
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100128 * If the device doesn't provide these interfaces, we do not know how
129 * to deal with it.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100130 */
131static int cbaf_check(struct cbaf *cbaf)
132{
133 int result;
134 struct device *dev = &cbaf->usb_iface->dev;
135 struct wusb_cbaf_assoc_info *assoc_info;
136 struct wusb_cbaf_assoc_request *assoc_request;
137 size_t assoc_size;
138 void *itr, *top;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100139 int ar_rhi = 0, ar_assoc = 0;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100140
141 result = usb_control_msg(
142 cbaf->usb_dev, usb_rcvctrlpipe(cbaf->usb_dev, 0),
143 CBAF_REQ_GET_ASSOCIATION_INFORMATION,
144 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
145 0, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
146 cbaf->buffer, cbaf->buffer_size, 1000 /* FIXME: arbitrary */);
147 if (result < 0) {
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100148 dev_err(dev, "Cannot get available association types: %d\n",
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100149 result);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100150 return result;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100151 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100152
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100153 assoc_info = cbaf->buffer;
154 if (result < sizeof(*assoc_info)) {
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100155 dev_err(dev, "Not enough data to decode association info "
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100156 "header (%zu vs %zu bytes required)\n",
157 (size_t)result, sizeof(*assoc_info));
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100158 return result;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100159 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100160
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100161 assoc_size = le16_to_cpu(assoc_info->Length);
162 if (result < assoc_size) {
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100163 dev_err(dev, "Not enough data to decode association info "
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100164 "(%zu vs %zu bytes required)\n",
165 (size_t)assoc_size, sizeof(*assoc_info));
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100166 return result;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100167 }
168 /*
169 * From now on, we just verify, but won't error out unless we
170 * don't find the AR_TYPE_WUSB_{RETRIEVE_HOST_INFO,ASSOCIATE}
171 * types.
172 */
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100173 itr = cbaf->buffer + sizeof(*assoc_info);
174 top = cbaf->buffer + assoc_size;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100175 dev_dbg(dev, "Found %u association requests (%zu bytes)\n",
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100176 assoc_info->NumAssociationRequests, assoc_size);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100177
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100178 while (itr < top) {
179 u16 ar_type, ar_subtype;
180 u32 ar_size;
181 const char *ar_name;
182
183 assoc_request = itr;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100184
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100185 if (top - itr < sizeof(*assoc_request)) {
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100186 dev_err(dev, "Not enough data to decode associaton "
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100187 "request (%zu vs %zu bytes needed)\n",
188 top - itr, sizeof(*assoc_request));
189 break;
190 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100191
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100192 ar_type = le16_to_cpu(assoc_request->AssociationTypeId);
193 ar_subtype = le16_to_cpu(assoc_request->AssociationSubTypeId);
194 ar_size = le32_to_cpu(assoc_request->AssociationTypeInfoSize);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100195 ar_name = "unknown";
196
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100197 switch (ar_type) {
198 case AR_TYPE_WUSB:
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100199 /* Verify we have what is mandated by [WUSB-AM]. */
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100200 switch (ar_subtype) {
201 case AR_TYPE_WUSB_RETRIEVE_HOST_INFO:
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100202 ar_name = "RETRIEVE_HOST_INFO";
203 ar_rhi = 1;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100204 break;
205 case AR_TYPE_WUSB_ASSOCIATE:
206 /* send assoc data */
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100207 ar_name = "ASSOCIATE";
208 ar_assoc = 1;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100209 break;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100210 };
211 break;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100212 };
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100213
214 dev_dbg(dev, "Association request #%02u: 0x%04x/%04x "
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100215 "(%zu bytes): %s\n",
216 assoc_request->AssociationDataIndex, ar_type,
217 ar_subtype, (size_t)ar_size, ar_name);
218
219 itr += sizeof(*assoc_request);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100220 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100221
222 if (!ar_rhi) {
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100223 dev_err(dev, "Missing RETRIEVE_HOST_INFO association "
224 "request\n");
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100225 return -EINVAL;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100226 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100227 if (!ar_assoc) {
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100228 dev_err(dev, "Missing ASSOCIATE association request\n");
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100229 return -EINVAL;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100230 }
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100231
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100232 return 0;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100233}
234
235static const struct wusb_cbaf_host_info cbaf_host_info_defaults = {
236 .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId,
237 .AssociationTypeId = cpu_to_le16(AR_TYPE_WUSB),
238 .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId,
239 .AssociationSubTypeId = cpu_to_le16(AR_TYPE_WUSB_RETRIEVE_HOST_INFO),
240 .CHID_hdr = WUSB_AR_CHID,
241 .LangID_hdr = WUSB_AR_LangID,
242 .HostFriendlyName_hdr = WUSB_AR_HostFriendlyName,
243};
244
245/* Send WUSB host information (CHID and name) to a CBAF device */
246static int cbaf_send_host_info(struct cbaf *cbaf)
247{
248 struct wusb_cbaf_host_info *hi;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100249 size_t name_len;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100250 size_t hi_size;
251
252 hi = cbaf->buffer;
253 memset(hi, 0, sizeof(*hi));
254 *hi = cbaf_host_info_defaults;
255 hi->CHID = cbaf->chid;
256 hi->LangID = 0; /* FIXME: I guess... */
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100257 strlcpy(hi->HostFriendlyName, cbaf->host_name, CBA_NAME_LEN);
258 name_len = strlen(cbaf->host_name);
259 hi->HostFriendlyName_hdr.len = cpu_to_le16(name_len);
260 hi_size = sizeof(*hi) + name_len;
261
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100262 return usb_control_msg(cbaf->usb_dev, usb_sndctrlpipe(cbaf->usb_dev, 0),
263 CBAF_REQ_SET_ASSOCIATION_RESPONSE,
264 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
265 0x0101,
266 cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
267 hi, hi_size, 1000 /* FIXME: arbitrary */);
268}
269
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100270/*
271 * Get device's information (CDID) associated to CHID
272 *
273 * The device will return it's information (CDID, name, bandgroups)
274 * associated to the CHID we have set before, or 0 CDID and default
275 * name and bandgroup if no CHID set or unknown.
276 */
277static int cbaf_cdid_get(struct cbaf *cbaf)
278{
279 int result;
280 struct device *dev = &cbaf->usb_iface->dev;
281 struct wusb_cbaf_device_info *di;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100282 size_t needed;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100283
284 di = cbaf->buffer;
285 result = usb_control_msg(
286 cbaf->usb_dev, usb_rcvctrlpipe(cbaf->usb_dev, 0),
287 CBAF_REQ_GET_ASSOCIATION_REQUEST,
288 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
289 0x0200, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
290 di, cbaf->buffer_size, 1000 /* FIXME: arbitrary */);
291 if (result < 0) {
292 dev_err(dev, "Cannot request device information: %d\n", result);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100293 return result;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100294 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100295
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100296 needed = result < sizeof(*di) ? sizeof(*di) : le32_to_cpu(di->Length);
297 if (result < needed) {
298 dev_err(dev, "Not enough data in DEVICE_INFO reply (%zu vs "
299 "%zu bytes needed)\n", (size_t)result, needed);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100300 return result;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100301 }
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100302
303 strlcpy(cbaf->device_name, di->DeviceFriendlyName, CBA_NAME_LEN);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100304 cbaf->cdid = di->CDID;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100305 cbaf->device_band_groups = le16_to_cpu(di->BandGroups);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100306
307 return 0;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100308}
309
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100310static ssize_t cbaf_wusb_chid_show(struct device *dev,
311 struct device_attribute *attr,
312 char *buf)
313{
314 struct usb_interface *iface = to_usb_interface(dev);
315 struct cbaf *cbaf = usb_get_intfdata(iface);
316 char pr_chid[WUSB_CKHDID_STRSIZE];
317
318 ckhdid_printf(pr_chid, sizeof(pr_chid), &cbaf->chid);
319 return scnprintf(buf, PAGE_SIZE, "%s\n", pr_chid);
320}
321
322static ssize_t cbaf_wusb_chid_store(struct device *dev,
323 struct device_attribute *attr,
324 const char *buf, size_t size)
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100325{
326 ssize_t result;
327 struct usb_interface *iface = to_usb_interface(dev);
328 struct cbaf *cbaf = usb_get_intfdata(iface);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100329
330 result = sscanf(buf,
331 "%02hhx %02hhx %02hhx %02hhx "
332 "%02hhx %02hhx %02hhx %02hhx "
333 "%02hhx %02hhx %02hhx %02hhx "
334 "%02hhx %02hhx %02hhx %02hhx",
335 &cbaf->chid.data[0] , &cbaf->chid.data[1],
336 &cbaf->chid.data[2] , &cbaf->chid.data[3],
337 &cbaf->chid.data[4] , &cbaf->chid.data[5],
338 &cbaf->chid.data[6] , &cbaf->chid.data[7],
339 &cbaf->chid.data[8] , &cbaf->chid.data[9],
340 &cbaf->chid.data[10], &cbaf->chid.data[11],
341 &cbaf->chid.data[12], &cbaf->chid.data[13],
342 &cbaf->chid.data[14], &cbaf->chid.data[15]);
343
344 if (result != 16)
345 return -EINVAL;
346
347 result = cbaf_send_host_info(cbaf);
348 if (result < 0)
349 return result;
350 result = cbaf_cdid_get(cbaf);
351 if (result < 0)
352 return -result;
353 return size;
354}
355static DEVICE_ATTR(wusb_chid, 0600, cbaf_wusb_chid_show, cbaf_wusb_chid_store);
356
357static ssize_t cbaf_wusb_host_name_show(struct device *dev,
358 struct device_attribute *attr,
359 char *buf)
360{
361 struct usb_interface *iface = to_usb_interface(dev);
362 struct cbaf *cbaf = usb_get_intfdata(iface);
363
364 return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->host_name);
365}
366
367static ssize_t cbaf_wusb_host_name_store(struct device *dev,
368 struct device_attribute *attr,
369 const char *buf, size_t size)
370{
371 ssize_t result;
372 struct usb_interface *iface = to_usb_interface(dev);
373 struct cbaf *cbaf = usb_get_intfdata(iface);
374
375 result = sscanf(buf, "%63s", cbaf->host_name);
376 if (result != 1)
377 return -EINVAL;
378
379 return size;
380}
381static DEVICE_ATTR(wusb_host_name, 0600, cbaf_wusb_host_name_show,
382 cbaf_wusb_host_name_store);
383
384static ssize_t cbaf_wusb_host_band_groups_show(struct device *dev,
385 struct device_attribute *attr,
386 char *buf)
387{
388 struct usb_interface *iface = to_usb_interface(dev);
389 struct cbaf *cbaf = usb_get_intfdata(iface);
390
391 return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->host_band_groups);
392}
393
394static ssize_t cbaf_wusb_host_band_groups_store(struct device *dev,
395 struct device_attribute *attr,
396 const char *buf, size_t size)
397{
398 ssize_t result;
399 struct usb_interface *iface = to_usb_interface(dev);
400 struct cbaf *cbaf = usb_get_intfdata(iface);
401 u16 band_groups = 0;
402
403 result = sscanf(buf, "%04hx", &band_groups);
404 if (result != 1)
405 return -EINVAL;
406
407 cbaf->host_band_groups = band_groups;
408
409 return size;
410}
411
412static DEVICE_ATTR(wusb_host_band_groups, 0600,
413 cbaf_wusb_host_band_groups_show,
414 cbaf_wusb_host_band_groups_store);
415
416static const struct wusb_cbaf_device_info cbaf_device_info_defaults = {
417 .Length_hdr = WUSB_AR_Length,
418 .CDID_hdr = WUSB_AR_CDID,
419 .BandGroups_hdr = WUSB_AR_BandGroups,
420 .LangID_hdr = WUSB_AR_LangID,
421 .DeviceFriendlyName_hdr = WUSB_AR_DeviceFriendlyName,
422};
423
424static ssize_t cbaf_wusb_cdid_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426{
427 struct usb_interface *iface = to_usb_interface(dev);
428 struct cbaf *cbaf = usb_get_intfdata(iface);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100429 char pr_cdid[WUSB_CKHDID_STRSIZE];
430
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100431 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &cbaf->cdid);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100432 return scnprintf(buf, PAGE_SIZE, "%s\n", pr_cdid);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100433}
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100434
435static ssize_t cbaf_wusb_cdid_store(struct device *dev,
436 struct device_attribute *attr,
437 const char *buf, size_t size)
438{
439 ssize_t result;
440 struct usb_interface *iface = to_usb_interface(dev);
441 struct cbaf *cbaf = usb_get_intfdata(iface);
442 struct wusb_ckhdid cdid;
443
444 result = sscanf(buf,
445 "%02hhx %02hhx %02hhx %02hhx "
446 "%02hhx %02hhx %02hhx %02hhx "
447 "%02hhx %02hhx %02hhx %02hhx "
448 "%02hhx %02hhx %02hhx %02hhx",
449 &cdid.data[0] , &cdid.data[1],
450 &cdid.data[2] , &cdid.data[3],
451 &cdid.data[4] , &cdid.data[5],
452 &cdid.data[6] , &cdid.data[7],
453 &cdid.data[8] , &cdid.data[9],
454 &cdid.data[10], &cdid.data[11],
455 &cdid.data[12], &cdid.data[13],
456 &cdid.data[14], &cdid.data[15]);
457 if (result != 16)
458 return -EINVAL;
459
460 cbaf->cdid = cdid;
461
462 return size;
463}
464static DEVICE_ATTR(wusb_cdid, 0600, cbaf_wusb_cdid_show, cbaf_wusb_cdid_store);
465
466static ssize_t cbaf_wusb_device_band_groups_show(struct device *dev,
467 struct device_attribute *attr,
468 char *buf)
469{
470 struct usb_interface *iface = to_usb_interface(dev);
471 struct cbaf *cbaf = usb_get_intfdata(iface);
472
473 return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->device_band_groups);
474}
475
476static DEVICE_ATTR(wusb_device_band_groups, 0600,
477 cbaf_wusb_device_band_groups_show,
478 NULL);
479
480static ssize_t cbaf_wusb_device_name_show(struct device *dev,
481 struct device_attribute *attr,
482 char *buf)
483{
484 struct usb_interface *iface = to_usb_interface(dev);
485 struct cbaf *cbaf = usb_get_intfdata(iface);
486
487 return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->device_name);
488}
489static DEVICE_ATTR(wusb_device_name, 0600, cbaf_wusb_device_name_show, NULL);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100490
491static const struct wusb_cbaf_cc_data cbaf_cc_data_defaults = {
492 .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId,
493 .AssociationTypeId = cpu_to_le16(AR_TYPE_WUSB),
494 .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId,
495 .AssociationSubTypeId = cpu_to_le16(AR_TYPE_WUSB_ASSOCIATE),
496 .Length_hdr = WUSB_AR_Length,
497 .Length = cpu_to_le32(sizeof(struct wusb_cbaf_cc_data)),
498 .ConnectionContext_hdr = WUSB_AR_ConnectionContext,
499 .BandGroups_hdr = WUSB_AR_BandGroups,
500};
501
502static const struct wusb_cbaf_cc_data_fail cbaf_cc_data_fail_defaults = {
503 .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId,
504 .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId,
505 .Length_hdr = WUSB_AR_Length,
506 .AssociationStatus_hdr = WUSB_AR_AssociationStatus,
507};
508
509/*
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100510 * Send a new CC to the device.
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100511 */
512static int cbaf_cc_upload(struct cbaf *cbaf)
513{
514 int result;
515 struct device *dev = &cbaf->usb_iface->dev;
516 struct wusb_cbaf_cc_data *ccd;
517 char pr_cdid[WUSB_CKHDID_STRSIZE];
518
519 ccd = cbaf->buffer;
520 *ccd = cbaf_cc_data_defaults;
521 ccd->CHID = cbaf->chid;
522 ccd->CDID = cbaf->cdid;
523 ccd->CK = cbaf->ck;
524 ccd->BandGroups = cpu_to_le16(cbaf->host_band_groups);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100525
526 dev_dbg(dev, "Trying to upload CC:\n");
527 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CHID);
528 dev_dbg(dev, " CHID %s\n", pr_cdid);
529 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CDID);
530 dev_dbg(dev, " CDID %s\n", pr_cdid);
531 dev_dbg(dev, " Bandgroups 0x%04x\n", cbaf->host_band_groups);
532
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100533 result = usb_control_msg(
534 cbaf->usb_dev, usb_sndctrlpipe(cbaf->usb_dev, 0),
535 CBAF_REQ_SET_ASSOCIATION_RESPONSE,
536 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
537 0x0201, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
538 ccd, sizeof(*ccd), 1000 /* FIXME: arbitrary */);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100539
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100540 return result;
541}
542
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100543static ssize_t cbaf_wusb_ck_store(struct device *dev,
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100544 struct device_attribute *attr,
545 const char *buf, size_t size)
546{
547 ssize_t result;
548 struct usb_interface *iface = to_usb_interface(dev);
549 struct cbaf *cbaf = usb_get_intfdata(iface);
550
551 result = sscanf(buf,
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100552 "%02hhx %02hhx %02hhx %02hhx "
553 "%02hhx %02hhx %02hhx %02hhx "
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100554 "%02hhx %02hhx %02hhx %02hhx "
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100555 "%02hhx %02hhx %02hhx %02hhx",
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100556 &cbaf->ck.data[0] , &cbaf->ck.data[1],
557 &cbaf->ck.data[2] , &cbaf->ck.data[3],
558 &cbaf->ck.data[4] , &cbaf->ck.data[5],
559 &cbaf->ck.data[6] , &cbaf->ck.data[7],
560 &cbaf->ck.data[8] , &cbaf->ck.data[9],
561 &cbaf->ck.data[10], &cbaf->ck.data[11],
562 &cbaf->ck.data[12], &cbaf->ck.data[13],
563 &cbaf->ck.data[14], &cbaf->ck.data[15]);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100564 if (result != 16)
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100565 return -EINVAL;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100566
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100567 result = cbaf_cc_upload(cbaf);
568 if (result < 0)
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100569 return result;
570
571 return size;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100572}
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100573static DEVICE_ATTR(wusb_ck, 0600, NULL, cbaf_wusb_ck_store);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100574
575static struct attribute *cbaf_dev_attrs[] = {
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100576 &dev_attr_wusb_host_name.attr,
577 &dev_attr_wusb_host_band_groups.attr,
578 &dev_attr_wusb_chid.attr,
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100579 &dev_attr_wusb_cdid.attr,
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100580 &dev_attr_wusb_device_name.attr,
581 &dev_attr_wusb_device_band_groups.attr,
582 &dev_attr_wusb_ck.attr,
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100583 NULL,
584};
585
586static struct attribute_group cbaf_dev_attr_group = {
587 .name = NULL, /* we want them in the same directory */
588 .attrs = cbaf_dev_attrs,
589};
590
591static int cbaf_probe(struct usb_interface *iface,
592 const struct usb_device_id *id)
593{
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100594 struct cbaf *cbaf;
595 struct device *dev = &iface->dev;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100596 int result = -ENOMEM;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100597
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100598 cbaf = kzalloc(sizeof(*cbaf), GFP_KERNEL);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100599 if (cbaf == NULL)
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100600 goto error_kzalloc;
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100601 cbaf->buffer = kmalloc(512, GFP_KERNEL);
602 if (cbaf->buffer == NULL)
603 goto error_kmalloc_buffer;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100604
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100605 cbaf->buffer_size = 512;
606 cbaf->usb_dev = usb_get_dev(interface_to_usbdev(iface));
607 cbaf->usb_iface = usb_get_intf(iface);
608 result = cbaf_check(cbaf);
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100609 if (result < 0) {
610 dev_err(dev, "This device is not WUSB-CBAF compliant"
611 "and is not supported yet.\n");
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100612 goto error_check;
Felipe Zimmerlef1fa0352008-09-17 16:34:42 +0100613 }
614
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100615 result = sysfs_create_group(&dev->kobj, &cbaf_dev_attr_group);
616 if (result < 0) {
617 dev_err(dev, "Can't register sysfs attr group: %d\n", result);
618 goto error_create_group;
619 }
620 usb_set_intfdata(iface, cbaf);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100621 return 0;
622
623error_create_group:
624error_check:
625 kfree(cbaf->buffer);
626error_kmalloc_buffer:
627 kfree(cbaf);
628error_kzalloc:
629 return result;
630}
631
632static void cbaf_disconnect(struct usb_interface *iface)
633{
634 struct cbaf *cbaf = usb_get_intfdata(iface);
635 struct device *dev = &iface->dev;
636 sysfs_remove_group(&dev->kobj, &cbaf_dev_attr_group);
637 usb_set_intfdata(iface, NULL);
638 usb_put_intf(iface);
639 kfree(cbaf->buffer);
640 /* paranoia: clean up crypto keys */
Johannes Weiner31dbb802009-03-04 12:06:15 -0800641 kzfree(cbaf);
Inaky Perez-Gonzalez870d5392008-09-17 16:34:27 +0100642}
643
644static struct usb_device_id cbaf_id_table[] = {
645 { USB_INTERFACE_INFO(0xef, 0x03, 0x01), },
646 { },
647};
648MODULE_DEVICE_TABLE(usb, cbaf_id_table);
649
650static struct usb_driver cbaf_driver = {
651 .name = "wusb-cbaf",
652 .id_table = cbaf_id_table,
653 .probe = cbaf_probe,
654 .disconnect = cbaf_disconnect,
655};
656
657static int __init cbaf_driver_init(void)
658{
659 return usb_register(&cbaf_driver);
660}
661module_init(cbaf_driver_init);
662
663static void __exit cbaf_driver_exit(void)
664{
665 usb_deregister(&cbaf_driver);
666}
667module_exit(cbaf_driver_exit);
668
669MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
670MODULE_DESCRIPTION("Wireless USB Cable Based Association");
671MODULE_LICENSE("GPL");