Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Wireless USB - Cable Based Association |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 2006 Intel Corporation |
| 6 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 7 | * Copyright (C) 2008 Cambridge Silicon Radio Ltd. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 8 | * |
| 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 Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 24 | * WUSB devices have to be paired (associated in WUSB lingo) so |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 25 | * that they can connect to the system. |
| 26 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 27 | * 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-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 31 | * |
| 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 Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 45 | * 1. Device plugs, cbaf is loaded, notifications happen. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 46 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 47 | * 2. The connection manager (CM) sees a device with CBAF capability |
| 48 | * (the wusb_chid etc. files in /sys/devices/blah/OURDEVICE). |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 49 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 50 | * 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-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 54 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 55 | * 4. The CM can verify that the device's supported band groups |
| 56 | * (wusb_device_band_groups) are compatible with the host. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 57 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 58 | * 5. The CM reads the wusb_cdid file. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 59 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 60 | * 6. The CM looks up its database |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 61 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 62 | * 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-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 65 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 66 | * 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-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 71 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 72 | * CMD may choose to prompt the user before associating with a new |
| 73 | * device. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 74 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 75 | * 7. Device is unplugged. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 76 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 77 | * 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-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 84 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 85 | * References: |
| 86 | * [WUSB-AM] Association Models Supplement to the Certified Wireless |
| 87 | * Universal Serial Bus Specification, version 1.0. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 88 | */ |
| 89 | #include <linux/module.h> |
| 90 | #include <linux/ctype.h> |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 91 | #include <linux/usb.h> |
| 92 | #include <linux/interrupt.h> |
| 93 | #include <linux/delay.h> |
| 94 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 95 | #include <linux/slab.h> |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 96 | #include <linux/mutex.h> |
| 97 | #include <linux/uwb.h> |
| 98 | #include <linux/usb/wusb.h> |
| 99 | #include <linux/usb/association.h> |
| 100 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 101 | #define CBA_NAME_LEN 0x40 /* [WUSB-AM] table 4-7 */ |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 102 | |
| 103 | /* An instance of a Cable-Based-Association-Framework device */ |
| 104 | struct cbaf { |
| 105 | struct usb_device *usb_dev; |
| 106 | struct usb_interface *usb_iface; |
| 107 | void *buffer; |
| 108 | size_t buffer_size; |
| 109 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 110 | struct wusb_ckhdid chid; |
| 111 | char host_name[CBA_NAME_LEN]; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 112 | u16 host_band_groups; |
| 113 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 114 | struct wusb_ckhdid cdid; |
| 115 | char device_name[CBA_NAME_LEN]; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 116 | u16 device_band_groups; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 117 | |
| 118 | struct wusb_ckhdid ck; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /* |
| 122 | * Verify that a CBAF USB-interface has what we need |
| 123 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 124 | * According to [WUSB-AM], CBA devices should provide at least two |
| 125 | * interfaces: |
| 126 | * - RETRIEVE_HOST_INFO |
| 127 | * - ASSOCIATE |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 128 | * |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 129 | * If the device doesn't provide these interfaces, we do not know how |
| 130 | * to deal with it. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 131 | */ |
| 132 | static int cbaf_check(struct cbaf *cbaf) |
| 133 | { |
| 134 | int result; |
| 135 | struct device *dev = &cbaf->usb_iface->dev; |
| 136 | struct wusb_cbaf_assoc_info *assoc_info; |
| 137 | struct wusb_cbaf_assoc_request *assoc_request; |
| 138 | size_t assoc_size; |
| 139 | void *itr, *top; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 140 | int ar_rhi = 0, ar_assoc = 0; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 141 | |
| 142 | result = usb_control_msg( |
| 143 | cbaf->usb_dev, usb_rcvctrlpipe(cbaf->usb_dev, 0), |
| 144 | CBAF_REQ_GET_ASSOCIATION_INFORMATION, |
| 145 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 146 | 0, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 147 | cbaf->buffer, cbaf->buffer_size, USB_CTRL_GET_TIMEOUT); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 148 | if (result < 0) { |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 149 | dev_err(dev, "Cannot get available association types: %d\n", |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 150 | result); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 151 | return result; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 152 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 153 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 154 | assoc_info = cbaf->buffer; |
| 155 | if (result < sizeof(*assoc_info)) { |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 156 | dev_err(dev, "Not enough data to decode association info " |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 157 | "header (%zu vs %zu bytes required)\n", |
| 158 | (size_t)result, sizeof(*assoc_info)); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 159 | return result; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 160 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 161 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 162 | assoc_size = le16_to_cpu(assoc_info->Length); |
| 163 | if (result < assoc_size) { |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 164 | dev_err(dev, "Not enough data to decode association info " |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 165 | "(%zu vs %zu bytes required)\n", |
| 166 | (size_t)assoc_size, sizeof(*assoc_info)); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 167 | return result; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 168 | } |
| 169 | /* |
| 170 | * From now on, we just verify, but won't error out unless we |
| 171 | * don't find the AR_TYPE_WUSB_{RETRIEVE_HOST_INFO,ASSOCIATE} |
| 172 | * types. |
| 173 | */ |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 174 | itr = cbaf->buffer + sizeof(*assoc_info); |
| 175 | top = cbaf->buffer + assoc_size; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 176 | dev_dbg(dev, "Found %u association requests (%zu bytes)\n", |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 177 | assoc_info->NumAssociationRequests, assoc_size); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 178 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 179 | while (itr < top) { |
| 180 | u16 ar_type, ar_subtype; |
| 181 | u32 ar_size; |
| 182 | const char *ar_name; |
| 183 | |
| 184 | assoc_request = itr; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 185 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 186 | if (top - itr < sizeof(*assoc_request)) { |
Rahul Bedarkar | 1076e7a | 2014-01-04 12:37:52 +0530 | [diff] [blame] | 187 | dev_err(dev, "Not enough data to decode association " |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 188 | "request (%zu vs %zu bytes needed)\n", |
| 189 | top - itr, sizeof(*assoc_request)); |
| 190 | break; |
| 191 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 192 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 193 | ar_type = le16_to_cpu(assoc_request->AssociationTypeId); |
| 194 | ar_subtype = le16_to_cpu(assoc_request->AssociationSubTypeId); |
| 195 | ar_size = le32_to_cpu(assoc_request->AssociationTypeInfoSize); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 196 | ar_name = "unknown"; |
| 197 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 198 | switch (ar_type) { |
| 199 | case AR_TYPE_WUSB: |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 200 | /* Verify we have what is mandated by [WUSB-AM]. */ |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 201 | switch (ar_subtype) { |
| 202 | case AR_TYPE_WUSB_RETRIEVE_HOST_INFO: |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 203 | ar_name = "RETRIEVE_HOST_INFO"; |
| 204 | ar_rhi = 1; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 205 | break; |
| 206 | case AR_TYPE_WUSB_ASSOCIATE: |
| 207 | /* send assoc data */ |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 208 | ar_name = "ASSOCIATE"; |
| 209 | ar_assoc = 1; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 210 | break; |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 211 | } |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 212 | break; |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 213 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 214 | |
| 215 | dev_dbg(dev, "Association request #%02u: 0x%04x/%04x " |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 216 | "(%zu bytes): %s\n", |
| 217 | assoc_request->AssociationDataIndex, ar_type, |
| 218 | ar_subtype, (size_t)ar_size, ar_name); |
| 219 | |
| 220 | itr += sizeof(*assoc_request); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 221 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 222 | |
| 223 | if (!ar_rhi) { |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 224 | dev_err(dev, "Missing RETRIEVE_HOST_INFO association " |
| 225 | "request\n"); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 226 | return -EINVAL; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 227 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 228 | if (!ar_assoc) { |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 229 | dev_err(dev, "Missing ASSOCIATE association request\n"); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 230 | return -EINVAL; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 231 | } |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 232 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 233 | return 0; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static const struct wusb_cbaf_host_info cbaf_host_info_defaults = { |
| 237 | .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId, |
Rahul Bedarkar | b0b4cb5 | 2014-01-04 11:19:04 +0530 | [diff] [blame] | 238 | .AssociationTypeId = cpu_to_le16(AR_TYPE_WUSB), |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 239 | .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId, |
| 240 | .AssociationSubTypeId = cpu_to_le16(AR_TYPE_WUSB_RETRIEVE_HOST_INFO), |
| 241 | .CHID_hdr = WUSB_AR_CHID, |
| 242 | .LangID_hdr = WUSB_AR_LangID, |
| 243 | .HostFriendlyName_hdr = WUSB_AR_HostFriendlyName, |
| 244 | }; |
| 245 | |
| 246 | /* Send WUSB host information (CHID and name) to a CBAF device */ |
| 247 | static int cbaf_send_host_info(struct cbaf *cbaf) |
| 248 | { |
| 249 | struct wusb_cbaf_host_info *hi; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 250 | size_t name_len; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 251 | size_t hi_size; |
| 252 | |
| 253 | hi = cbaf->buffer; |
| 254 | memset(hi, 0, sizeof(*hi)); |
| 255 | *hi = cbaf_host_info_defaults; |
| 256 | hi->CHID = cbaf->chid; |
| 257 | hi->LangID = 0; /* FIXME: I guess... */ |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 258 | strlcpy(hi->HostFriendlyName, cbaf->host_name, CBA_NAME_LEN); |
| 259 | name_len = strlen(cbaf->host_name); |
| 260 | hi->HostFriendlyName_hdr.len = cpu_to_le16(name_len); |
| 261 | hi_size = sizeof(*hi) + name_len; |
| 262 | |
Rahul Bedarkar | b0b4cb5 | 2014-01-04 11:19:04 +0530 | [diff] [blame] | 263 | return usb_control_msg(cbaf->usb_dev, |
| 264 | usb_sndctrlpipe(cbaf->usb_dev, 0), |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 265 | CBAF_REQ_SET_ASSOCIATION_RESPONSE, |
| 266 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 267 | 0x0101, |
| 268 | cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 269 | hi, hi_size, USB_CTRL_SET_TIMEOUT); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Get device's information (CDID) associated to CHID |
| 274 | * |
| 275 | * The device will return it's information (CDID, name, bandgroups) |
| 276 | * associated to the CHID we have set before, or 0 CDID and default |
| 277 | * name and bandgroup if no CHID set or unknown. |
| 278 | */ |
| 279 | static int cbaf_cdid_get(struct cbaf *cbaf) |
| 280 | { |
| 281 | int result; |
| 282 | struct device *dev = &cbaf->usb_iface->dev; |
| 283 | struct wusb_cbaf_device_info *di; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 284 | size_t needed; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 285 | |
| 286 | di = cbaf->buffer; |
| 287 | result = usb_control_msg( |
| 288 | cbaf->usb_dev, usb_rcvctrlpipe(cbaf->usb_dev, 0), |
| 289 | CBAF_REQ_GET_ASSOCIATION_REQUEST, |
| 290 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 291 | 0x0200, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 292 | di, cbaf->buffer_size, USB_CTRL_GET_TIMEOUT); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 293 | if (result < 0) { |
Rahul Bedarkar | b0b4cb5 | 2014-01-04 11:19:04 +0530 | [diff] [blame] | 294 | dev_err(dev, "Cannot request device information: %d\n", |
| 295 | result); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 296 | return result; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 297 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 298 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 299 | needed = result < sizeof(*di) ? sizeof(*di) : le32_to_cpu(di->Length); |
| 300 | if (result < needed) { |
| 301 | dev_err(dev, "Not enough data in DEVICE_INFO reply (%zu vs " |
| 302 | "%zu bytes needed)\n", (size_t)result, needed); |
Dan Carpenter | e90ed12 | 2011-06-04 09:10:21 +0300 | [diff] [blame] | 303 | return -ENOENT; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 304 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 305 | |
| 306 | strlcpy(cbaf->device_name, di->DeviceFriendlyName, CBA_NAME_LEN); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 307 | cbaf->cdid = di->CDID; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 308 | cbaf->device_band_groups = le16_to_cpu(di->BandGroups); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 309 | |
| 310 | return 0; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 313 | static ssize_t cbaf_wusb_chid_show(struct device *dev, |
| 314 | struct device_attribute *attr, |
| 315 | char *buf) |
| 316 | { |
| 317 | struct usb_interface *iface = to_usb_interface(dev); |
| 318 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 319 | char pr_chid[WUSB_CKHDID_STRSIZE]; |
| 320 | |
| 321 | ckhdid_printf(pr_chid, sizeof(pr_chid), &cbaf->chid); |
| 322 | return scnprintf(buf, PAGE_SIZE, "%s\n", pr_chid); |
| 323 | } |
| 324 | |
| 325 | static ssize_t cbaf_wusb_chid_store(struct device *dev, |
| 326 | struct device_attribute *attr, |
| 327 | const char *buf, size_t size) |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 328 | { |
| 329 | ssize_t result; |
| 330 | struct usb_interface *iface = to_usb_interface(dev); |
| 331 | struct cbaf *cbaf = usb_get_intfdata(iface); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 332 | |
| 333 | result = sscanf(buf, |
| 334 | "%02hhx %02hhx %02hhx %02hhx " |
| 335 | "%02hhx %02hhx %02hhx %02hhx " |
| 336 | "%02hhx %02hhx %02hhx %02hhx " |
| 337 | "%02hhx %02hhx %02hhx %02hhx", |
| 338 | &cbaf->chid.data[0] , &cbaf->chid.data[1], |
| 339 | &cbaf->chid.data[2] , &cbaf->chid.data[3], |
| 340 | &cbaf->chid.data[4] , &cbaf->chid.data[5], |
| 341 | &cbaf->chid.data[6] , &cbaf->chid.data[7], |
| 342 | &cbaf->chid.data[8] , &cbaf->chid.data[9], |
| 343 | &cbaf->chid.data[10], &cbaf->chid.data[11], |
| 344 | &cbaf->chid.data[12], &cbaf->chid.data[13], |
| 345 | &cbaf->chid.data[14], &cbaf->chid.data[15]); |
| 346 | |
| 347 | if (result != 16) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | result = cbaf_send_host_info(cbaf); |
| 351 | if (result < 0) |
| 352 | return result; |
| 353 | result = cbaf_cdid_get(cbaf); |
| 354 | if (result < 0) |
Dan Carpenter | e90ed12 | 2011-06-04 09:10:21 +0300 | [diff] [blame] | 355 | return result; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 356 | return size; |
| 357 | } |
| 358 | static DEVICE_ATTR(wusb_chid, 0600, cbaf_wusb_chid_show, cbaf_wusb_chid_store); |
| 359 | |
| 360 | static ssize_t cbaf_wusb_host_name_show(struct device *dev, |
| 361 | struct device_attribute *attr, |
| 362 | char *buf) |
| 363 | { |
| 364 | struct usb_interface *iface = to_usb_interface(dev); |
| 365 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 366 | |
| 367 | return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->host_name); |
| 368 | } |
| 369 | |
| 370 | static ssize_t cbaf_wusb_host_name_store(struct device *dev, |
| 371 | struct device_attribute *attr, |
| 372 | const char *buf, size_t size) |
| 373 | { |
| 374 | ssize_t result; |
| 375 | struct usb_interface *iface = to_usb_interface(dev); |
| 376 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 377 | |
| 378 | result = sscanf(buf, "%63s", cbaf->host_name); |
| 379 | if (result != 1) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | return size; |
| 383 | } |
| 384 | static DEVICE_ATTR(wusb_host_name, 0600, cbaf_wusb_host_name_show, |
| 385 | cbaf_wusb_host_name_store); |
| 386 | |
| 387 | static ssize_t cbaf_wusb_host_band_groups_show(struct device *dev, |
| 388 | struct device_attribute *attr, |
| 389 | char *buf) |
| 390 | { |
| 391 | struct usb_interface *iface = to_usb_interface(dev); |
| 392 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 393 | |
| 394 | return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->host_band_groups); |
| 395 | } |
| 396 | |
| 397 | static ssize_t cbaf_wusb_host_band_groups_store(struct device *dev, |
| 398 | struct device_attribute *attr, |
| 399 | const char *buf, size_t size) |
| 400 | { |
| 401 | ssize_t result; |
| 402 | struct usb_interface *iface = to_usb_interface(dev); |
| 403 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 404 | u16 band_groups = 0; |
| 405 | |
| 406 | result = sscanf(buf, "%04hx", &band_groups); |
| 407 | if (result != 1) |
| 408 | return -EINVAL; |
| 409 | |
| 410 | cbaf->host_band_groups = band_groups; |
| 411 | |
| 412 | return size; |
| 413 | } |
| 414 | |
| 415 | static DEVICE_ATTR(wusb_host_band_groups, 0600, |
| 416 | cbaf_wusb_host_band_groups_show, |
| 417 | cbaf_wusb_host_band_groups_store); |
| 418 | |
| 419 | static const struct wusb_cbaf_device_info cbaf_device_info_defaults = { |
| 420 | .Length_hdr = WUSB_AR_Length, |
| 421 | .CDID_hdr = WUSB_AR_CDID, |
| 422 | .BandGroups_hdr = WUSB_AR_BandGroups, |
| 423 | .LangID_hdr = WUSB_AR_LangID, |
| 424 | .DeviceFriendlyName_hdr = WUSB_AR_DeviceFriendlyName, |
| 425 | }; |
| 426 | |
| 427 | static ssize_t cbaf_wusb_cdid_show(struct device *dev, |
| 428 | struct device_attribute *attr, char *buf) |
| 429 | { |
| 430 | struct usb_interface *iface = to_usb_interface(dev); |
| 431 | struct cbaf *cbaf = usb_get_intfdata(iface); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 432 | char pr_cdid[WUSB_CKHDID_STRSIZE]; |
| 433 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 434 | ckhdid_printf(pr_cdid, sizeof(pr_cdid), &cbaf->cdid); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 435 | return scnprintf(buf, PAGE_SIZE, "%s\n", pr_cdid); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 436 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 437 | |
| 438 | static ssize_t cbaf_wusb_cdid_store(struct device *dev, |
| 439 | struct device_attribute *attr, |
| 440 | const char *buf, size_t size) |
| 441 | { |
| 442 | ssize_t result; |
| 443 | struct usb_interface *iface = to_usb_interface(dev); |
| 444 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 445 | struct wusb_ckhdid cdid; |
| 446 | |
| 447 | result = sscanf(buf, |
| 448 | "%02hhx %02hhx %02hhx %02hhx " |
| 449 | "%02hhx %02hhx %02hhx %02hhx " |
| 450 | "%02hhx %02hhx %02hhx %02hhx " |
| 451 | "%02hhx %02hhx %02hhx %02hhx", |
| 452 | &cdid.data[0] , &cdid.data[1], |
| 453 | &cdid.data[2] , &cdid.data[3], |
| 454 | &cdid.data[4] , &cdid.data[5], |
| 455 | &cdid.data[6] , &cdid.data[7], |
| 456 | &cdid.data[8] , &cdid.data[9], |
| 457 | &cdid.data[10], &cdid.data[11], |
| 458 | &cdid.data[12], &cdid.data[13], |
| 459 | &cdid.data[14], &cdid.data[15]); |
| 460 | if (result != 16) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | cbaf->cdid = cdid; |
| 464 | |
| 465 | return size; |
| 466 | } |
| 467 | static DEVICE_ATTR(wusb_cdid, 0600, cbaf_wusb_cdid_show, cbaf_wusb_cdid_store); |
| 468 | |
| 469 | static ssize_t cbaf_wusb_device_band_groups_show(struct device *dev, |
| 470 | struct device_attribute *attr, |
| 471 | char *buf) |
| 472 | { |
| 473 | struct usb_interface *iface = to_usb_interface(dev); |
| 474 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 475 | |
| 476 | return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->device_band_groups); |
| 477 | } |
| 478 | |
| 479 | static DEVICE_ATTR(wusb_device_band_groups, 0600, |
| 480 | cbaf_wusb_device_band_groups_show, |
| 481 | NULL); |
| 482 | |
| 483 | static ssize_t cbaf_wusb_device_name_show(struct device *dev, |
| 484 | struct device_attribute *attr, |
| 485 | char *buf) |
| 486 | { |
| 487 | struct usb_interface *iface = to_usb_interface(dev); |
| 488 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 489 | |
| 490 | return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->device_name); |
| 491 | } |
| 492 | static DEVICE_ATTR(wusb_device_name, 0600, cbaf_wusb_device_name_show, NULL); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 493 | |
| 494 | static const struct wusb_cbaf_cc_data cbaf_cc_data_defaults = { |
| 495 | .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId, |
Rahul Bedarkar | b0b4cb5 | 2014-01-04 11:19:04 +0530 | [diff] [blame] | 496 | .AssociationTypeId = cpu_to_le16(AR_TYPE_WUSB), |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 497 | .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId, |
| 498 | .AssociationSubTypeId = cpu_to_le16(AR_TYPE_WUSB_ASSOCIATE), |
| 499 | .Length_hdr = WUSB_AR_Length, |
Rahul Bedarkar | b0b4cb5 | 2014-01-04 11:19:04 +0530 | [diff] [blame] | 500 | .Length = cpu_to_le32(sizeof(struct wusb_cbaf_cc_data)), |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 501 | .ConnectionContext_hdr = WUSB_AR_ConnectionContext, |
| 502 | .BandGroups_hdr = WUSB_AR_BandGroups, |
| 503 | }; |
| 504 | |
| 505 | static const struct wusb_cbaf_cc_data_fail cbaf_cc_data_fail_defaults = { |
| 506 | .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId, |
| 507 | .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId, |
| 508 | .Length_hdr = WUSB_AR_Length, |
| 509 | .AssociationStatus_hdr = WUSB_AR_AssociationStatus, |
| 510 | }; |
| 511 | |
| 512 | /* |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 513 | * Send a new CC to the device. |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 514 | */ |
| 515 | static int cbaf_cc_upload(struct cbaf *cbaf) |
| 516 | { |
| 517 | int result; |
| 518 | struct device *dev = &cbaf->usb_iface->dev; |
| 519 | struct wusb_cbaf_cc_data *ccd; |
| 520 | char pr_cdid[WUSB_CKHDID_STRSIZE]; |
| 521 | |
| 522 | ccd = cbaf->buffer; |
| 523 | *ccd = cbaf_cc_data_defaults; |
| 524 | ccd->CHID = cbaf->chid; |
| 525 | ccd->CDID = cbaf->cdid; |
| 526 | ccd->CK = cbaf->ck; |
| 527 | ccd->BandGroups = cpu_to_le16(cbaf->host_band_groups); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 528 | |
| 529 | dev_dbg(dev, "Trying to upload CC:\n"); |
| 530 | ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CHID); |
| 531 | dev_dbg(dev, " CHID %s\n", pr_cdid); |
| 532 | ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CDID); |
| 533 | dev_dbg(dev, " CDID %s\n", pr_cdid); |
| 534 | dev_dbg(dev, " Bandgroups 0x%04x\n", cbaf->host_band_groups); |
| 535 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 536 | result = usb_control_msg( |
| 537 | cbaf->usb_dev, usb_sndctrlpipe(cbaf->usb_dev, 0), |
| 538 | CBAF_REQ_SET_ASSOCIATION_RESPONSE, |
| 539 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 540 | 0x0201, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber, |
Thomas Pugliese | 7b3e374 | 2013-12-09 13:19:08 -0600 | [diff] [blame] | 541 | ccd, sizeof(*ccd), USB_CTRL_SET_TIMEOUT); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 542 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 543 | return result; |
| 544 | } |
| 545 | |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 546 | static ssize_t cbaf_wusb_ck_store(struct device *dev, |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 547 | struct device_attribute *attr, |
| 548 | const char *buf, size_t size) |
| 549 | { |
| 550 | ssize_t result; |
| 551 | struct usb_interface *iface = to_usb_interface(dev); |
| 552 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 553 | |
| 554 | result = sscanf(buf, |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 555 | "%02hhx %02hhx %02hhx %02hhx " |
| 556 | "%02hhx %02hhx %02hhx %02hhx " |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 557 | "%02hhx %02hhx %02hhx %02hhx " |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 558 | "%02hhx %02hhx %02hhx %02hhx", |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 559 | &cbaf->ck.data[0] , &cbaf->ck.data[1], |
| 560 | &cbaf->ck.data[2] , &cbaf->ck.data[3], |
| 561 | &cbaf->ck.data[4] , &cbaf->ck.data[5], |
| 562 | &cbaf->ck.data[6] , &cbaf->ck.data[7], |
| 563 | &cbaf->ck.data[8] , &cbaf->ck.data[9], |
| 564 | &cbaf->ck.data[10], &cbaf->ck.data[11], |
| 565 | &cbaf->ck.data[12], &cbaf->ck.data[13], |
| 566 | &cbaf->ck.data[14], &cbaf->ck.data[15]); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 567 | if (result != 16) |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 568 | return -EINVAL; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 569 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 570 | result = cbaf_cc_upload(cbaf); |
| 571 | if (result < 0) |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 572 | return result; |
| 573 | |
| 574 | return size; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 575 | } |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 576 | static DEVICE_ATTR(wusb_ck, 0600, NULL, cbaf_wusb_ck_store); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 577 | |
| 578 | static struct attribute *cbaf_dev_attrs[] = { |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 579 | &dev_attr_wusb_host_name.attr, |
| 580 | &dev_attr_wusb_host_band_groups.attr, |
| 581 | &dev_attr_wusb_chid.attr, |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 582 | &dev_attr_wusb_cdid.attr, |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 583 | &dev_attr_wusb_device_name.attr, |
| 584 | &dev_attr_wusb_device_band_groups.attr, |
| 585 | &dev_attr_wusb_ck.attr, |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 586 | NULL, |
| 587 | }; |
| 588 | |
| 589 | static struct attribute_group cbaf_dev_attr_group = { |
| 590 | .name = NULL, /* we want them in the same directory */ |
| 591 | .attrs = cbaf_dev_attrs, |
| 592 | }; |
| 593 | |
| 594 | static int cbaf_probe(struct usb_interface *iface, |
| 595 | const struct usb_device_id *id) |
| 596 | { |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 597 | struct cbaf *cbaf; |
| 598 | struct device *dev = &iface->dev; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 599 | int result = -ENOMEM; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 600 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 601 | cbaf = kzalloc(sizeof(*cbaf), GFP_KERNEL); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 602 | if (cbaf == NULL) |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 603 | goto error_kzalloc; |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 604 | cbaf->buffer = kmalloc(512, GFP_KERNEL); |
| 605 | if (cbaf->buffer == NULL) |
| 606 | goto error_kmalloc_buffer; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 607 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 608 | cbaf->buffer_size = 512; |
| 609 | cbaf->usb_dev = usb_get_dev(interface_to_usbdev(iface)); |
| 610 | cbaf->usb_iface = usb_get_intf(iface); |
| 611 | result = cbaf_check(cbaf); |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 612 | if (result < 0) { |
Colin Ian King | f409440 | 2016-09-12 14:19:17 +0100 | [diff] [blame] | 613 | dev_err(dev, "This device is not WUSB-CBAF compliant and is not supported yet.\n"); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 614 | goto error_check; |
Felipe Zimmerle | f1fa035 | 2008-09-17 16:34:42 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 617 | result = sysfs_create_group(&dev->kobj, &cbaf_dev_attr_group); |
| 618 | if (result < 0) { |
| 619 | dev_err(dev, "Can't register sysfs attr group: %d\n", result); |
| 620 | goto error_create_group; |
| 621 | } |
| 622 | usb_set_intfdata(iface, cbaf); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 623 | return 0; |
| 624 | |
| 625 | error_create_group: |
| 626 | error_check: |
Alexey Khoroshilov | f2c28cf | 2013-10-19 01:49:38 +0400 | [diff] [blame] | 627 | usb_put_intf(iface); |
| 628 | usb_put_dev(cbaf->usb_dev); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 629 | kfree(cbaf->buffer); |
| 630 | error_kmalloc_buffer: |
| 631 | kfree(cbaf); |
| 632 | error_kzalloc: |
| 633 | return result; |
| 634 | } |
| 635 | |
| 636 | static void cbaf_disconnect(struct usb_interface *iface) |
| 637 | { |
| 638 | struct cbaf *cbaf = usb_get_intfdata(iface); |
| 639 | struct device *dev = &iface->dev; |
| 640 | sysfs_remove_group(&dev->kobj, &cbaf_dev_attr_group); |
| 641 | usb_set_intfdata(iface, NULL); |
| 642 | usb_put_intf(iface); |
Alexey Khoroshilov | f2c28cf | 2013-10-19 01:49:38 +0400 | [diff] [blame] | 643 | usb_put_dev(cbaf->usb_dev); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 644 | kfree(cbaf->buffer); |
| 645 | /* paranoia: clean up crypto keys */ |
Johannes Weiner | 31dbb80 | 2009-03-04 12:06:15 -0800 | [diff] [blame] | 646 | kzfree(cbaf); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 649 | static const struct usb_device_id cbaf_id_table[] = { |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 650 | { USB_INTERFACE_INFO(0xef, 0x03, 0x01), }, |
| 651 | { }, |
| 652 | }; |
| 653 | MODULE_DEVICE_TABLE(usb, cbaf_id_table); |
| 654 | |
| 655 | static struct usb_driver cbaf_driver = { |
| 656 | .name = "wusb-cbaf", |
| 657 | .id_table = cbaf_id_table, |
| 658 | .probe = cbaf_probe, |
| 659 | .disconnect = cbaf_disconnect, |
| 660 | }; |
| 661 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 662 | module_usb_driver(cbaf_driver); |
Inaky Perez-Gonzalez | 870d539 | 2008-09-17 16:34:27 +0100 | [diff] [blame] | 663 | |
| 664 | MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>"); |
| 665 | MODULE_DESCRIPTION("Wireless USB Cable Based Association"); |
| 666 | MODULE_LICENSE("GPL"); |