cbaf.c 19 KB

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