1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89#include <linux/jiffies.h>
90#include <linux/ctype.h>
91#include <linux/slab.h>
92#include <linux/workqueue.h>
93#include <linux/export.h>
94#include "wusbhc.h"
95
96static void wusbhc_devconnect_acked_work(struct work_struct *work);
97
98static void wusb_dev_free(struct wusb_dev *wusb_dev)
99{
100 kfree(wusb_dev);
101}
102
103static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
104{
105 struct wusb_dev *wusb_dev;
106
107 wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
108 if (wusb_dev == NULL)
109 goto err;
110
111 wusb_dev->wusbhc = wusbhc;
112
113 INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
114
115 return wusb_dev;
116err:
117 wusb_dev_free(wusb_dev);
118 return NULL;
119}
120
121
122
123
124
125
126
127
128
129
130
131
132static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
133{
134 unsigned cnt;
135 struct wusb_dev *dev_itr;
136 struct wuie_connect_ack *cack_ie;
137
138 cack_ie = &wusbhc->cack_ie;
139 cnt = 0;
140 list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
141 cack_ie->blk[cnt].CDID = dev_itr->cdid;
142 cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
143 if (++cnt >= WUIE_ELT_MAX)
144 break;
145 }
146 cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
147 + cnt * sizeof(cack_ie->blk[0]);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
163 struct wusb_dn_connect *dnc,
164 const char *pr_cdid, u8 port_idx)
165{
166 struct device *dev = wusbhc->dev;
167 struct wusb_dev *wusb_dev;
168 int new_connection = wusb_dn_connect_new_connection(dnc);
169 u8 dev_addr;
170 int result;
171
172
173 list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
174 if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
175 sizeof(wusb_dev->cdid)))
176 return wusb_dev;
177
178 wusb_dev = wusb_dev_alloc(wusbhc);
179 if (wusb_dev == NULL)
180 return NULL;
181 wusb_dev_init(wusb_dev);
182 wusb_dev->cdid = dnc->CDID;
183 wusb_dev->port_idx = port_idx;
184
185
186
187
188
189
190
191
192 bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
193
194
195
196 if (1 && new_connection == 0)
197 new_connection = 1;
198 if (new_connection) {
199 dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
200
201 dev_info(dev, "Connecting new WUSB device to address %u, "
202 "port %u\n", dev_addr, port_idx);
203
204 result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
205 if (result < 0)
206 return NULL;
207 }
208 wusb_dev->entry_ts = jiffies;
209 list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
210 wusbhc->cack_count++;
211 wusbhc_fill_cack_ie(wusbhc);
212
213 return wusb_dev;
214}
215
216
217
218
219
220
221static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
222{
223 list_del_init(&wusb_dev->cack_node);
224 wusbhc->cack_count--;
225 wusbhc_fill_cack_ie(wusbhc);
226}
227
228
229
230static
231void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
232{
233 wusbhc_cack_rm(wusbhc, wusb_dev);
234 if (wusbhc->cack_count)
235 wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
236 else
237 wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
238}
239
240static void wusbhc_devconnect_acked_work(struct work_struct *work)
241{
242 struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
243 devconnect_acked_work);
244 struct wusbhc *wusbhc = wusb_dev->wusbhc;
245
246 mutex_lock(&wusbhc->mutex);
247 wusbhc_devconnect_acked(wusbhc, wusb_dev);
248 mutex_unlock(&wusbhc->mutex);
249
250 wusb_dev_put(wusb_dev);
251}
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279static
280void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
281 const char *pr_cdid)
282{
283 int result;
284 struct device *dev = wusbhc->dev;
285 struct wusb_dev *wusb_dev;
286 struct wusb_port *port;
287 unsigned idx;
288
289 mutex_lock(&wusbhc->mutex);
290
291
292 for (idx = 0; idx < wusbhc->ports_max; idx++) {
293 port = wusb_port_by_idx(wusbhc, idx);
294 if (port->wusb_dev
295 && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
296 goto error_unlock;
297 }
298
299 for (idx = 0; idx < wusbhc->ports_max; idx++) {
300 port = wusb_port_by_idx(wusbhc, idx);
301 if ((port->status & USB_PORT_STAT_POWER)
302 && !(port->status & USB_PORT_STAT_CONNECTION))
303 break;
304 }
305 if (idx >= wusbhc->ports_max) {
306 dev_err(dev, "Host controller can't connect more devices "
307 "(%u already connected); device %s rejected\n",
308 wusbhc->ports_max, pr_cdid);
309
310
311
312 goto error_unlock;
313 }
314
315
316 wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
317
318
319
320 wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
321 if (wusb_dev == NULL)
322 goto error_unlock;
323 result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
324 if (result < 0)
325 goto error_unlock;
326
327
328 msleep(3);
329 port->wusb_dev = wusb_dev;
330 port->status |= USB_PORT_STAT_CONNECTION;
331 port->change |= USB_PORT_STAT_C_CONNECTION;
332
333
334
335
336
337error_unlock:
338 mutex_unlock(&wusbhc->mutex);
339 return;
340
341}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
359 struct wusb_port *port)
360{
361 struct wusb_dev *wusb_dev = port->wusb_dev;
362
363 port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
364 | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
365 | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
366 port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
367 if (wusb_dev) {
368 dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
369 if (!list_empty(&wusb_dev->cack_node))
370 list_del_init(&wusb_dev->cack_node);
371
372 wusb_dev_put(wusb_dev);
373 }
374 port->wusb_dev = NULL;
375
376
377
378 if (wusbhc->active)
379 wusbhc_gtk_rekey(wusbhc);
380
381
382
383
384
385}
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
404{
405 struct device *dev = wusbhc->dev;
406 unsigned cnt;
407 struct wusb_dev *wusb_dev;
408 struct wusb_port *wusb_port;
409 struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
410 unsigned keep_alives, old_keep_alives;
411
412 old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
413 keep_alives = 0;
414 for (cnt = 0;
415 keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;
416 cnt++) {
417 unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
418
419 wusb_port = wusb_port_by_idx(wusbhc, cnt);
420 wusb_dev = wusb_port->wusb_dev;
421
422 if (wusb_dev == NULL)
423 continue;
424 if (wusb_dev->usb_dev == NULL)
425 continue;
426
427 if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
428 dev_err(dev, "KEEPALIVE: device %u timed out\n",
429 wusb_dev->addr);
430 __wusbhc_dev_disconnect(wusbhc, wusb_port);
431 } else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {
432
433 ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
434 }
435 }
436 if (keep_alives & 0x1)
437 ie->bDeviceAddress[keep_alives++] = 0x7f;
438 ie->hdr.bLength = sizeof(ie->hdr) +
439 keep_alives*sizeof(ie->bDeviceAddress[0]);
440 if (keep_alives > 0)
441 wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
442 else if (old_keep_alives != 0)
443 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
444}
445
446
447
448
449static void wusbhc_keep_alive_run(struct work_struct *ws)
450{
451 struct delayed_work *dw = to_delayed_work(ws);
452 struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
453
454 mutex_lock(&wusbhc->mutex);
455 __wusbhc_keep_alive(wusbhc);
456 mutex_unlock(&wusbhc->mutex);
457
458 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
459 msecs_to_jiffies(wusbhc->trust_timeout / 2));
460}
461
462
463
464
465
466
467
468
469static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
470{
471 int p;
472
473 if (addr == 0xff)
474 return NULL;
475
476 if (addr > 0) {
477 int port = (addr & ~0x80) - 2;
478 if (port < 0 || port >= wusbhc->ports_max)
479 return NULL;
480 return wusb_port_by_idx(wusbhc, port)->wusb_dev;
481 }
482
483
484 for (p = 0; p < wusbhc->ports_max; p++) {
485 struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
486 if (wusb_dev && wusb_dev->addr == addr)
487 return wusb_dev;
488 }
489 return NULL;
490}
491
492
493
494
495
496
497
498
499
500static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, u8 srcaddr)
501{
502 struct wusb_dev *wusb_dev;
503
504 mutex_lock(&wusbhc->mutex);
505 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
506 if (wusb_dev == NULL) {
507 dev_dbg(wusbhc->dev, "ignoring DN_Alive from unconnected device %02x\n",
508 srcaddr);
509 } else {
510 wusb_dev->entry_ts = jiffies;
511 __wusbhc_keep_alive(wusbhc);
512 }
513 mutex_unlock(&wusbhc->mutex);
514}
515
516
517
518
519
520
521
522
523
524
525
526
527
528static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
529 struct wusb_dn_hdr *dn_hdr,
530 size_t size)
531{
532 struct device *dev = wusbhc->dev;
533 struct wusb_dn_connect *dnc;
534 char pr_cdid[WUSB_CKHDID_STRSIZE];
535 static const char *beacon_behaviour[] = {
536 "reserved",
537 "self-beacon",
538 "directed-beacon",
539 "no-beacon"
540 };
541
542 if (size < sizeof(*dnc)) {
543 dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
544 size, sizeof(*dnc));
545 return;
546 }
547
548 dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
549 ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
550 dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
551 pr_cdid,
552 wusb_dn_connect_prev_dev_addr(dnc),
553 beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
554 wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
555
556 wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
557}
558
559
560
561
562
563
564
565
566static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, u8 srcaddr)
567{
568 struct device *dev = wusbhc->dev;
569 struct wusb_dev *wusb_dev;
570
571 mutex_lock(&wusbhc->mutex);
572 wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
573 if (wusb_dev == NULL) {
574 dev_dbg(dev, "ignoring DN DISCONNECT from unconnected device %02x\n",
575 srcaddr);
576 } else {
577 dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n",
578 wusb_dev->addr);
579 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc,
580 wusb_dev->port_idx));
581 }
582 mutex_unlock(&wusbhc->mutex);
583}
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
600 struct wusb_dn_hdr *dn_hdr, size_t size)
601{
602 struct device *dev = wusbhc->dev;
603
604 if (size < sizeof(struct wusb_dn_hdr)) {
605 dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
606 (int)size, (int)sizeof(struct wusb_dn_hdr));
607 return;
608 }
609 switch (dn_hdr->bType) {
610 case WUSB_DN_CONNECT:
611 wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
612 break;
613 case WUSB_DN_ALIVE:
614 wusbhc_handle_dn_alive(wusbhc, srcaddr);
615 break;
616 case WUSB_DN_DISCONNECT:
617 wusbhc_handle_dn_disconnect(wusbhc, srcaddr);
618 break;
619 case WUSB_DN_MASAVAILCHANGED:
620 case WUSB_DN_RWAKE:
621 case WUSB_DN_SLEEP:
622
623 break;
624 case WUSB_DN_EPRDY:
625
626 break;
627 default:
628 dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
629 dn_hdr->bType, (int)size, srcaddr);
630 }
631}
632EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
654{
655 int result;
656 struct device *dev = wusbhc->dev;
657 struct wusb_dev *wusb_dev;
658 struct wuie_disconnect *ie;
659
660 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
661 if (wusb_dev == NULL) {
662
663 dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
664 port_idx);
665 return;
666 }
667 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
668
669 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
670 if (ie == NULL)
671 return;
672 ie->hdr.bLength = sizeof(*ie);
673 ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
674 ie->bDeviceAddress = wusb_dev->addr;
675 result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
676 if (result < 0)
677 dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
678 else {
679
680 msleep(7*4);
681 wusbhc_mmcie_rm(wusbhc, &ie->hdr);
682 }
683 kfree(ie);
684}
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702static int wusb_dev_bos_grok(struct usb_device *usb_dev,
703 struct wusb_dev *wusb_dev,
704 struct usb_bos_descriptor *bos, size_t desc_size)
705{
706 ssize_t result;
707 struct device *dev = &usb_dev->dev;
708 void *itr, *top;
709
710
711 itr = (void *)bos + sizeof(*bos);
712 top = itr + desc_size - sizeof(*bos);
713 while (itr < top) {
714 struct usb_dev_cap_header *cap_hdr = itr;
715 size_t cap_size;
716 u8 cap_type;
717 if (top - itr < sizeof(*cap_hdr)) {
718 dev_err(dev, "Device BUG? premature end of BOS header "
719 "data [offset 0x%02x]: only %zu bytes left\n",
720 (int)(itr - (void *)bos), top - itr);
721 result = -ENOSPC;
722 goto error_bad_cap;
723 }
724 cap_size = cap_hdr->bLength;
725 cap_type = cap_hdr->bDevCapabilityType;
726 if (cap_size == 0)
727 break;
728 if (cap_size > top - itr) {
729 dev_err(dev, "Device BUG? premature end of BOS data "
730 "[offset 0x%02x cap %02x %zu bytes]: "
731 "only %zu bytes left\n",
732 (int)(itr - (void *)bos),
733 cap_type, cap_size, top - itr);
734 result = -EBADF;
735 goto error_bad_cap;
736 }
737 switch (cap_type) {
738 case USB_CAP_TYPE_WIRELESS_USB:
739 if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
740 dev_err(dev, "Device BUG? WUSB Capability "
741 "descriptor is %zu bytes vs %zu "
742 "needed\n", cap_size,
743 sizeof(*wusb_dev->wusb_cap_descr));
744 else
745 wusb_dev->wusb_cap_descr = itr;
746 break;
747 default:
748 dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
749 "(%zu bytes) at offset 0x%02x\n", cap_type,
750 cap_size, (int)(itr - (void *)bos));
751 }
752 itr += cap_size;
753 }
754 result = 0;
755error_bad_cap:
756 return result;
757}
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773static int wusb_dev_bos_add(struct usb_device *usb_dev,
774 struct wusb_dev *wusb_dev)
775{
776 ssize_t result;
777 struct device *dev = &usb_dev->dev;
778 struct usb_bos_descriptor *bos;
779 size_t alloc_size = 32, desc_size = 4;
780
781 bos = kmalloc(alloc_size, GFP_KERNEL);
782 if (bos == NULL)
783 return -ENOMEM;
784 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
785 if (result < 4) {
786 dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
787 result);
788 goto error_get_descriptor;
789 }
790 desc_size = le16_to_cpu(bos->wTotalLength);
791 if (desc_size >= alloc_size) {
792 kfree(bos);
793 alloc_size = desc_size;
794 bos = kmalloc(alloc_size, GFP_KERNEL);
795 if (bos == NULL)
796 return -ENOMEM;
797 }
798 result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
799 if (result < 0 || result != desc_size) {
800 dev_err(dev, "Can't get BOS descriptor or too short (need "
801 "%zu bytes): %zd\n", desc_size, result);
802 goto error_get_descriptor;
803 }
804 if (result < sizeof(*bos)
805 || le16_to_cpu(bos->wTotalLength) != desc_size) {
806 dev_err(dev, "Can't get BOS descriptor or too short (need "
807 "%zu bytes): %zd\n", desc_size, result);
808 goto error_get_descriptor;
809 }
810
811 result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
812 if (result < 0)
813 goto error_bad_bos;
814 wusb_dev->bos = bos;
815 return 0;
816
817error_bad_bos:
818error_get_descriptor:
819 kfree(bos);
820 wusb_dev->wusb_cap_descr = NULL;
821 return result;
822}
823
824static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
825{
826 kfree(wusb_dev->bos);
827 wusb_dev->wusb_cap_descr = NULL;
828};
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852static void wusb_dev_add_ncb(struct usb_device *usb_dev)
853{
854 int result = 0;
855 struct wusb_dev *wusb_dev;
856 struct wusbhc *wusbhc;
857 struct device *dev = &usb_dev->dev;
858 u8 port_idx;
859
860 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
861 return;
862
863 usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
864
865 wusbhc = wusbhc_get_by_usb_dev(usb_dev);
866 if (wusbhc == NULL)
867 goto error_nodev;
868 mutex_lock(&wusbhc->mutex);
869 wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
870 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
871 mutex_unlock(&wusbhc->mutex);
872 if (wusb_dev == NULL)
873 goto error_nodev;
874 wusb_dev->usb_dev = usb_get_dev(usb_dev);
875 usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
876 result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
877 if (result < 0) {
878 dev_err(dev, "Cannot enable security: %d\n", result);
879 goto error_sec_add;
880 }
881
882 result = wusb_dev_bos_add(usb_dev, wusb_dev);
883 if (result < 0) {
884 dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
885 goto error_bos_add;
886 }
887 result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
888 if (result < 0)
889 goto error_add_sysfs;
890out:
891 wusb_dev_put(wusb_dev);
892 wusbhc_put(wusbhc);
893error_nodev:
894 return;
895
896error_add_sysfs:
897 wusb_dev_bos_rm(wusb_dev);
898error_bos_add:
899 wusb_dev_sec_rm(wusb_dev);
900error_sec_add:
901 mutex_lock(&wusbhc->mutex);
902 __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
903 mutex_unlock(&wusbhc->mutex);
904 goto out;
905}
906
907
908
909
910
911
912static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
913{
914 struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
915
916 if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
917 return;
918
919 wusb_dev_sysfs_rm(wusb_dev);
920 wusb_dev_bos_rm(wusb_dev);
921 wusb_dev_sec_rm(wusb_dev);
922 wusb_dev->usb_dev = NULL;
923 usb_dev->wusb_dev = NULL;
924 wusb_dev_put(wusb_dev);
925 usb_put_dev(usb_dev);
926}
927
928
929
930
931
932
933
934
935
936int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
937 void *priv)
938{
939 int result = NOTIFY_OK;
940
941 switch (val) {
942 case USB_DEVICE_ADD:
943 wusb_dev_add_ncb(priv);
944 break;
945 case USB_DEVICE_REMOVE:
946 wusb_dev_rm_ncb(priv);
947 break;
948 case USB_BUS_ADD:
949
950 case USB_BUS_REMOVE:
951 break;
952 default:
953 WARN_ON(1);
954 result = NOTIFY_BAD;
955 }
956 return result;
957}
958
959
960
961
962struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
963 struct usb_device *usb_dev)
964{
965 struct wusb_dev *wusb_dev;
966 u8 port_idx;
967
968 port_idx = wusb_port_no_to_idx(usb_dev->portnum);
969 BUG_ON(port_idx > wusbhc->ports_max);
970 wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
971 if (wusb_dev != NULL)
972 wusb_dev_get(wusb_dev);
973 return wusb_dev;
974}
975EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
976
977void wusb_dev_destroy(struct kref *_wusb_dev)
978{
979 struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
980
981 list_del_init(&wusb_dev->cack_node);
982 wusb_dev_free(wusb_dev);
983}
984EXPORT_SYMBOL_GPL(wusb_dev_destroy);
985
986
987
988
989
990
991
992int wusbhc_devconnect_create(struct wusbhc *wusbhc)
993{
994 wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
995 wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
996 INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
997
998 wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
999 wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
1000 INIT_LIST_HEAD(&wusbhc->cack_list);
1001
1002 return 0;
1003}
1004
1005
1006
1007
1008void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
1009{
1010
1011}
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022int wusbhc_devconnect_start(struct wusbhc *wusbhc)
1023{
1024 struct device *dev = wusbhc->dev;
1025 struct wuie_host_info *hi;
1026 int result;
1027
1028 hi = kzalloc(sizeof(*hi), GFP_KERNEL);
1029 if (hi == NULL)
1030 return -ENOMEM;
1031
1032 hi->hdr.bLength = sizeof(*hi);
1033 hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
1034 hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
1035 hi->CHID = wusbhc->chid;
1036 result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
1037 if (result < 0) {
1038 dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
1039 goto error_mmcie_set;
1040 }
1041 wusbhc->wuie_host_info = hi;
1042
1043 queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
1044 msecs_to_jiffies(wusbhc->trust_timeout / 2));
1045
1046 return 0;
1047
1048error_mmcie_set:
1049 kfree(hi);
1050 return result;
1051}
1052
1053
1054
1055
1056
1057
1058
1059
1060void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
1061{
1062 int i;
1063
1064 mutex_lock(&wusbhc->mutex);
1065 for (i = 0; i < wusbhc->ports_max; i++) {
1066 if (wusbhc->port[i].wusb_dev)
1067 __wusbhc_dev_disconnect(wusbhc, &wusbhc->port[i]);
1068 }
1069 mutex_unlock(&wusbhc->mutex);
1070
1071 cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
1072 wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
1073 kfree(wusbhc->wuie_host_info);
1074 wusbhc->wuie_host_info = NULL;
1075}
1076
1077
1078
1079
1080
1081
1082
1083int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
1084{
1085 int result;
1086
1087 wusb_dev->addr = addr;
1088 result = wusbhc->dev_info_set(wusbhc, wusb_dev);
1089 if (result < 0)
1090 dev_err(wusbhc->dev, "device %d: failed to set device "
1091 "address\n", wusb_dev->port_idx);
1092 else
1093 dev_info(wusbhc->dev, "device %d: %s addr %u\n",
1094 wusb_dev->port_idx,
1095 (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",
1096 wusb_dev->addr);
1097
1098 return result;
1099}
1100