cbaf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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, 1000 /* FIXME: arbitrary */);
  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 associaton "
  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, usb_sndctrlpipe(cbaf->usb_dev, 0),
  242. CBAF_REQ_SET_ASSOCIATION_RESPONSE,
  243. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  244. 0x0101,
  245. cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  246. hi, hi_size, 1000 /* FIXME: arbitrary */);
  247. }
  248. /*
  249. * Get device's information (CDID) associated to CHID
  250. *
  251. * The device will return it's information (CDID, name, bandgroups)
  252. * associated to the CHID we have set before, or 0 CDID and default
  253. * name and bandgroup if no CHID set or unknown.
  254. */
  255. static int cbaf_cdid_get(struct cbaf *cbaf)
  256. {
  257. int result;
  258. struct device *dev = &cbaf->usb_iface->dev;
  259. struct wusb_cbaf_device_info *di;
  260. size_t needed;
  261. di = cbaf->buffer;
  262. result = usb_control_msg(
  263. cbaf->usb_dev, usb_rcvctrlpipe(cbaf->usb_dev, 0),
  264. CBAF_REQ_GET_ASSOCIATION_REQUEST,
  265. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  266. 0x0200, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  267. di, cbaf->buffer_size, 1000 /* FIXME: arbitrary */);
  268. if (result < 0) {
  269. dev_err(dev, "Cannot request device information: %d\n", result);
  270. return result;
  271. }
  272. needed = result < sizeof(*di) ? sizeof(*di) : le32_to_cpu(di->Length);
  273. if (result < needed) {
  274. dev_err(dev, "Not enough data in DEVICE_INFO reply (%zu vs "
  275. "%zu bytes needed)\n", (size_t)result, needed);
  276. return result;
  277. }
  278. strlcpy(cbaf->device_name, di->DeviceFriendlyName, CBA_NAME_LEN);
  279. cbaf->cdid = di->CDID;
  280. cbaf->device_band_groups = le16_to_cpu(di->BandGroups);
  281. return 0;
  282. }
  283. static ssize_t cbaf_wusb_chid_show(struct device *dev,
  284. struct device_attribute *attr,
  285. char *buf)
  286. {
  287. struct usb_interface *iface = to_usb_interface(dev);
  288. struct cbaf *cbaf = usb_get_intfdata(iface);
  289. char pr_chid[WUSB_CKHDID_STRSIZE];
  290. ckhdid_printf(pr_chid, sizeof(pr_chid), &cbaf->chid);
  291. return scnprintf(buf, PAGE_SIZE, "%s\n", pr_chid);
  292. }
  293. static ssize_t cbaf_wusb_chid_store(struct device *dev,
  294. struct device_attribute *attr,
  295. const char *buf, size_t size)
  296. {
  297. ssize_t result;
  298. struct usb_interface *iface = to_usb_interface(dev);
  299. struct cbaf *cbaf = usb_get_intfdata(iface);
  300. result = sscanf(buf,
  301. "%02hhx %02hhx %02hhx %02hhx "
  302. "%02hhx %02hhx %02hhx %02hhx "
  303. "%02hhx %02hhx %02hhx %02hhx "
  304. "%02hhx %02hhx %02hhx %02hhx",
  305. &cbaf->chid.data[0] , &cbaf->chid.data[1],
  306. &cbaf->chid.data[2] , &cbaf->chid.data[3],
  307. &cbaf->chid.data[4] , &cbaf->chid.data[5],
  308. &cbaf->chid.data[6] , &cbaf->chid.data[7],
  309. &cbaf->chid.data[8] , &cbaf->chid.data[9],
  310. &cbaf->chid.data[10], &cbaf->chid.data[11],
  311. &cbaf->chid.data[12], &cbaf->chid.data[13],
  312. &cbaf->chid.data[14], &cbaf->chid.data[15]);
  313. if (result != 16)
  314. return -EINVAL;
  315. result = cbaf_send_host_info(cbaf);
  316. if (result < 0)
  317. return result;
  318. result = cbaf_cdid_get(cbaf);
  319. if (result < 0)
  320. return -result;
  321. return size;
  322. }
  323. static DEVICE_ATTR(wusb_chid, 0600, cbaf_wusb_chid_show, cbaf_wusb_chid_store);
  324. static ssize_t cbaf_wusb_host_name_show(struct device *dev,
  325. struct device_attribute *attr,
  326. char *buf)
  327. {
  328. struct usb_interface *iface = to_usb_interface(dev);
  329. struct cbaf *cbaf = usb_get_intfdata(iface);
  330. return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->host_name);
  331. }
  332. static ssize_t cbaf_wusb_host_name_store(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf, size_t size)
  335. {
  336. ssize_t result;
  337. struct usb_interface *iface = to_usb_interface(dev);
  338. struct cbaf *cbaf = usb_get_intfdata(iface);
  339. result = sscanf(buf, "%63s", cbaf->host_name);
  340. if (result != 1)
  341. return -EINVAL;
  342. return size;
  343. }
  344. static DEVICE_ATTR(wusb_host_name, 0600, cbaf_wusb_host_name_show,
  345. cbaf_wusb_host_name_store);
  346. static ssize_t cbaf_wusb_host_band_groups_show(struct device *dev,
  347. struct device_attribute *attr,
  348. char *buf)
  349. {
  350. struct usb_interface *iface = to_usb_interface(dev);
  351. struct cbaf *cbaf = usb_get_intfdata(iface);
  352. return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->host_band_groups);
  353. }
  354. static ssize_t cbaf_wusb_host_band_groups_store(struct device *dev,
  355. struct device_attribute *attr,
  356. const char *buf, size_t size)
  357. {
  358. ssize_t result;
  359. struct usb_interface *iface = to_usb_interface(dev);
  360. struct cbaf *cbaf = usb_get_intfdata(iface);
  361. u16 band_groups = 0;
  362. result = sscanf(buf, "%04hx", &band_groups);
  363. if (result != 1)
  364. return -EINVAL;
  365. cbaf->host_band_groups = band_groups;
  366. return size;
  367. }
  368. static DEVICE_ATTR(wusb_host_band_groups, 0600,
  369. cbaf_wusb_host_band_groups_show,
  370. cbaf_wusb_host_band_groups_store);
  371. static const struct wusb_cbaf_device_info cbaf_device_info_defaults = {
  372. .Length_hdr = WUSB_AR_Length,
  373. .CDID_hdr = WUSB_AR_CDID,
  374. .BandGroups_hdr = WUSB_AR_BandGroups,
  375. .LangID_hdr = WUSB_AR_LangID,
  376. .DeviceFriendlyName_hdr = WUSB_AR_DeviceFriendlyName,
  377. };
  378. static ssize_t cbaf_wusb_cdid_show(struct device *dev,
  379. struct device_attribute *attr, char *buf)
  380. {
  381. struct usb_interface *iface = to_usb_interface(dev);
  382. struct cbaf *cbaf = usb_get_intfdata(iface);
  383. char pr_cdid[WUSB_CKHDID_STRSIZE];
  384. ckhdid_printf(pr_cdid, sizeof(pr_cdid), &cbaf->cdid);
  385. return scnprintf(buf, PAGE_SIZE, "%s\n", pr_cdid);
  386. }
  387. static ssize_t cbaf_wusb_cdid_store(struct device *dev,
  388. struct device_attribute *attr,
  389. const char *buf, size_t size)
  390. {
  391. ssize_t result;
  392. struct usb_interface *iface = to_usb_interface(dev);
  393. struct cbaf *cbaf = usb_get_intfdata(iface);
  394. struct wusb_ckhdid cdid;
  395. result = sscanf(buf,
  396. "%02hhx %02hhx %02hhx %02hhx "
  397. "%02hhx %02hhx %02hhx %02hhx "
  398. "%02hhx %02hhx %02hhx %02hhx "
  399. "%02hhx %02hhx %02hhx %02hhx",
  400. &cdid.data[0] , &cdid.data[1],
  401. &cdid.data[2] , &cdid.data[3],
  402. &cdid.data[4] , &cdid.data[5],
  403. &cdid.data[6] , &cdid.data[7],
  404. &cdid.data[8] , &cdid.data[9],
  405. &cdid.data[10], &cdid.data[11],
  406. &cdid.data[12], &cdid.data[13],
  407. &cdid.data[14], &cdid.data[15]);
  408. if (result != 16)
  409. return -EINVAL;
  410. cbaf->cdid = cdid;
  411. return size;
  412. }
  413. static DEVICE_ATTR(wusb_cdid, 0600, cbaf_wusb_cdid_show, cbaf_wusb_cdid_store);
  414. static ssize_t cbaf_wusb_device_band_groups_show(struct device *dev,
  415. struct device_attribute *attr,
  416. char *buf)
  417. {
  418. struct usb_interface *iface = to_usb_interface(dev);
  419. struct cbaf *cbaf = usb_get_intfdata(iface);
  420. return scnprintf(buf, PAGE_SIZE, "0x%04x\n", cbaf->device_band_groups);
  421. }
  422. static DEVICE_ATTR(wusb_device_band_groups, 0600,
  423. cbaf_wusb_device_band_groups_show,
  424. NULL);
  425. static ssize_t cbaf_wusb_device_name_show(struct device *dev,
  426. struct device_attribute *attr,
  427. char *buf)
  428. {
  429. struct usb_interface *iface = to_usb_interface(dev);
  430. struct cbaf *cbaf = usb_get_intfdata(iface);
  431. return scnprintf(buf, PAGE_SIZE, "%s\n", cbaf->device_name);
  432. }
  433. static DEVICE_ATTR(wusb_device_name, 0600, cbaf_wusb_device_name_show, NULL);
  434. static const struct wusb_cbaf_cc_data cbaf_cc_data_defaults = {
  435. .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId,
  436. .AssociationTypeId = cpu_to_le16(AR_TYPE_WUSB),
  437. .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId,
  438. .AssociationSubTypeId = cpu_to_le16(AR_TYPE_WUSB_ASSOCIATE),
  439. .Length_hdr = WUSB_AR_Length,
  440. .Length = cpu_to_le32(sizeof(struct wusb_cbaf_cc_data)),
  441. .ConnectionContext_hdr = WUSB_AR_ConnectionContext,
  442. .BandGroups_hdr = WUSB_AR_BandGroups,
  443. };
  444. static const struct wusb_cbaf_cc_data_fail cbaf_cc_data_fail_defaults = {
  445. .AssociationTypeId_hdr = WUSB_AR_AssociationTypeId,
  446. .AssociationSubTypeId_hdr = WUSB_AR_AssociationSubTypeId,
  447. .Length_hdr = WUSB_AR_Length,
  448. .AssociationStatus_hdr = WUSB_AR_AssociationStatus,
  449. };
  450. /*
  451. * Send a new CC to the device.
  452. */
  453. static int cbaf_cc_upload(struct cbaf *cbaf)
  454. {
  455. int result;
  456. struct device *dev = &cbaf->usb_iface->dev;
  457. struct wusb_cbaf_cc_data *ccd;
  458. char pr_cdid[WUSB_CKHDID_STRSIZE];
  459. ccd = cbaf->buffer;
  460. *ccd = cbaf_cc_data_defaults;
  461. ccd->CHID = cbaf->chid;
  462. ccd->CDID = cbaf->cdid;
  463. ccd->CK = cbaf->ck;
  464. ccd->BandGroups = cpu_to_le16(cbaf->host_band_groups);
  465. dev_dbg(dev, "Trying to upload CC:\n");
  466. ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CHID);
  467. dev_dbg(dev, " CHID %s\n", pr_cdid);
  468. ckhdid_printf(pr_cdid, sizeof(pr_cdid), &ccd->CDID);
  469. dev_dbg(dev, " CDID %s\n", pr_cdid);
  470. dev_dbg(dev, " Bandgroups 0x%04x\n", cbaf->host_band_groups);
  471. result = usb_control_msg(
  472. cbaf->usb_dev, usb_sndctrlpipe(cbaf->usb_dev, 0),
  473. CBAF_REQ_SET_ASSOCIATION_RESPONSE,
  474. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  475. 0x0201, cbaf->usb_iface->cur_altsetting->desc.bInterfaceNumber,
  476. ccd, sizeof(*ccd), 1000 /* FIXME: arbitrary */);
  477. return result;
  478. }
  479. static ssize_t cbaf_wusb_ck_store(struct device *dev,
  480. struct device_attribute *attr,
  481. const char *buf, size_t size)
  482. {
  483. ssize_t result;
  484. struct usb_interface *iface = to_usb_interface(dev);
  485. struct cbaf *cbaf = usb_get_intfdata(iface);
  486. result = sscanf(buf,
  487. "%02hhx %02hhx %02hhx %02hhx "
  488. "%02hhx %02hhx %02hhx %02hhx "
  489. "%02hhx %02hhx %02hhx %02hhx "
  490. "%02hhx %02hhx %02hhx %02hhx",
  491. &cbaf->ck.data[0] , &cbaf->ck.data[1],
  492. &cbaf->ck.data[2] , &cbaf->ck.data[3],
  493. &cbaf->ck.data[4] , &cbaf->ck.data[5],
  494. &cbaf->ck.data[6] , &cbaf->ck.data[7],
  495. &cbaf->ck.data[8] , &cbaf->ck.data[9],
  496. &cbaf->ck.data[10], &cbaf->ck.data[11],
  497. &cbaf->ck.data[12], &cbaf->ck.data[13],
  498. &cbaf->ck.data[14], &cbaf->ck.data[15]);
  499. if (result != 16)
  500. return -EINVAL;
  501. result = cbaf_cc_upload(cbaf);
  502. if (result < 0)
  503. return result;
  504. return size;
  505. }
  506. static DEVICE_ATTR(wusb_ck, 0600, NULL, cbaf_wusb_ck_store);
  507. static struct attribute *cbaf_dev_attrs[] = {
  508. &dev_attr_wusb_host_name.attr,
  509. &dev_attr_wusb_host_band_groups.attr,
  510. &dev_attr_wusb_chid.attr,
  511. &dev_attr_wusb_cdid.attr,
  512. &dev_attr_wusb_device_name.attr,
  513. &dev_attr_wusb_device_band_groups.attr,
  514. &dev_attr_wusb_ck.attr,
  515. NULL,
  516. };
  517. static struct attribute_group cbaf_dev_attr_group = {
  518. .name = NULL, /* we want them in the same directory */
  519. .attrs = cbaf_dev_attrs,
  520. };
  521. static int cbaf_probe(struct usb_interface *iface,
  522. const struct usb_device_id *id)
  523. {
  524. struct cbaf *cbaf;
  525. struct device *dev = &iface->dev;
  526. int result = -ENOMEM;
  527. cbaf = kzalloc(sizeof(*cbaf), GFP_KERNEL);
  528. if (cbaf == NULL)
  529. goto error_kzalloc;
  530. cbaf->buffer = kmalloc(512, GFP_KERNEL);
  531. if (cbaf->buffer == NULL)
  532. goto error_kmalloc_buffer;
  533. cbaf->buffer_size = 512;
  534. cbaf->usb_dev = usb_get_dev(interface_to_usbdev(iface));
  535. cbaf->usb_iface = usb_get_intf(iface);
  536. result = cbaf_check(cbaf);
  537. if (result < 0) {
  538. dev_err(dev, "This device is not WUSB-CBAF compliant"
  539. "and is not supported yet.\n");
  540. goto error_check;
  541. }
  542. result = sysfs_create_group(&dev->kobj, &cbaf_dev_attr_group);
  543. if (result < 0) {
  544. dev_err(dev, "Can't register sysfs attr group: %d\n", result);
  545. goto error_create_group;
  546. }
  547. usb_set_intfdata(iface, cbaf);
  548. return 0;
  549. error_create_group:
  550. error_check:
  551. kfree(cbaf->buffer);
  552. error_kmalloc_buffer:
  553. kfree(cbaf);
  554. error_kzalloc:
  555. return result;
  556. }
  557. static void cbaf_disconnect(struct usb_interface *iface)
  558. {
  559. struct cbaf *cbaf = usb_get_intfdata(iface);
  560. struct device *dev = &iface->dev;
  561. sysfs_remove_group(&dev->kobj, &cbaf_dev_attr_group);
  562. usb_set_intfdata(iface, NULL);
  563. usb_put_intf(iface);
  564. kfree(cbaf->buffer);
  565. /* paranoia: clean up crypto keys */
  566. kzfree(cbaf);
  567. }
  568. static const struct usb_device_id cbaf_id_table[] = {
  569. { USB_INTERFACE_INFO(0xef, 0x03, 0x01), },
  570. { },
  571. };
  572. MODULE_DEVICE_TABLE(usb, cbaf_id_table);
  573. static struct usb_driver cbaf_driver = {
  574. .name = "wusb-cbaf",
  575. .id_table = cbaf_id_table,
  576. .probe = cbaf_probe,
  577. .disconnect = cbaf_disconnect,
  578. };
  579. static int __init cbaf_driver_init(void)
  580. {
  581. return usb_register(&cbaf_driver);
  582. }
  583. module_init(cbaf_driver_init);
  584. static void __exit cbaf_driver_exit(void)
  585. {
  586. usb_deregister(&cbaf_driver);
  587. }
  588. module_exit(cbaf_driver_exit);
  589. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  590. MODULE_DESCRIPTION("Wireless USB Cable Based Association");
  591. MODULE_LICENSE("GPL");