cbaf.c 20 KB

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