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