cbaf.c 20 KB

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