Repository navigation
Expand file tree
/
Copy pathif_upgt.c
More file actions
2378 lines (1983 loc) · 58.9 KB
/
Copy pathif_upgt.c
File metadata and controls
2378 lines (1983 loc) · 58.9 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: if_upgt.c,v 1.60 2013/08/21 05:21:45 dlg Exp $ */
/*
* Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/timeout.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/endian.h>
#include <machine/intr.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/if_upgtvar.h>
/*
* Driver for the USB PrismGT devices.
*
* For now just USB 2.0 devices with the GW3887 chipset are supported.
* The driver has been written based on the firmware version 2.13.1.0_LM87.
*
* TODO's:
* - Fix MONITOR mode (MAC filter).
* - Add HOSTAP mode.
* - Add IBSS mode.
* - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
*
* Parts of this driver has been influenced by reading the p54u driver
* written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
* Sebastien Bourdeauducq <lekernel@prism54.org>.
*/
#ifdef UPGT_DEBUG
int upgt_debug = 2;
#define DPRINTF(l, x...) do { if ((l) <= upgt_debug) printf(x); } while (0)
#else
#define DPRINTF(l, x...)
#endif
/*
* Prototypes.
*/
int upgt_match(struct device *, void *, void *);
void upgt_attach(struct device *, struct device *, void *);
void upgt_attach_hook(void *);
int upgt_detach(struct device *, int);
int upgt_activate(struct device *, int);
int upgt_device_type(struct upgt_softc *, uint16_t, uint16_t);
int upgt_device_init(struct upgt_softc *);
int upgt_mem_init(struct upgt_softc *);
uint32_t upgt_mem_alloc(struct upgt_softc *);
void upgt_mem_free(struct upgt_softc *, uint32_t);
int upgt_fw_alloc(struct upgt_softc *);
void upgt_fw_free(struct upgt_softc *);
int upgt_fw_verify(struct upgt_softc *);
int upgt_fw_load(struct upgt_softc *);
int upgt_fw_copy(char *, char *, int);
int upgt_eeprom_read(struct upgt_softc *);
int upgt_eeprom_parse(struct upgt_softc *);
void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
int upgt_ioctl(struct ifnet *, u_long, caddr_t);
int upgt_init(struct ifnet *);
void upgt_stop(struct upgt_softc *);
int upgt_media_change(struct ifnet *);
void upgt_newassoc(struct ieee80211com *, struct ieee80211_node *,
int);
int upgt_newstate(struct ieee80211com *, enum ieee80211_state, int);
void upgt_newstate_task(void *);
void upgt_next_scan(void *);
void upgt_start(struct ifnet *);
void upgt_watchdog(struct ifnet *);
void upgt_tx_task(void *);
void upgt_tx_done(struct upgt_softc *, uint8_t *);
void upgt_rx_cb(struct usbd_xfer *, void *, usbd_status);
void upgt_rx(struct upgt_softc *, uint8_t *, int);
void upgt_setup_rates(struct upgt_softc *);
uint8_t upgt_rx_rate(struct upgt_softc *, const int);
int upgt_set_macfilter(struct upgt_softc *, uint8_t state);
int upgt_set_channel(struct upgt_softc *, unsigned);
void upgt_set_led(struct upgt_softc *, int);
void upgt_set_led_blink(void *);
int upgt_get_stats(struct upgt_softc *);
int upgt_alloc_tx(struct upgt_softc *);
int upgt_alloc_rx(struct upgt_softc *);
int upgt_alloc_cmd(struct upgt_softc *);
void upgt_free_tx(struct upgt_softc *);
void upgt_free_rx(struct upgt_softc *);
void upgt_free_cmd(struct upgt_softc *);
int upgt_bulk_xmit(struct upgt_softc *, struct upgt_data *,
struct usbd_pipe *, uint32_t *, int);
void upgt_hexdump(void *, int);
uint32_t upgt_crc32_le(const void *, size_t);
uint32_t upgt_chksum_le(const uint32_t *, size_t);
struct cfdriver upgt_cd = {
NULL, "upgt", DV_IFNET
};
const struct cfattach upgt_ca = {
sizeof(struct upgt_softc),
upgt_match,
upgt_attach,
upgt_detach,
upgt_activate,
};
static const struct usb_devno upgt_devs_1[] = {
/* version 1 devices */
{ USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST120G }
};
static const struct usb_devno upgt_devs_2[] = {
/* version 2 devices */
{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_PRISM_GT },
{ USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST121G },
{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
{ USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54AG },
{ USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GV2 },
{ USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_PRISM_GT },
{ USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_1 },
{ USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_2 },
{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122A2 },
{ USB_VENDOR_FSC, USB_PRODUCT_FSC_E5400 },
{ USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_1 },
{ USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_2 },
{ USB_VENDOR_INTERSIL, USB_PRODUCT_INTERSIL_PRISM_GT },
{ USB_VENDOR_PHEENET, USB_PRODUCT_PHEENET_GWU513 },
{ USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_CPWUA054 },
{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG },
{ USB_VENDOR_USR, USB_PRODUCT_USR_USR5422 },
{ USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR045G },
{ USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_1 },
{ USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_2 },
{ USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_MD40900 },
{ USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XG703A }
};
int
upgt_match(struct device *parent, void *match, void *aux)
{
struct usb_attach_arg *uaa = aux;
if (uaa->iface != NULL)
return (UMATCH_NONE);
if (usb_lookup(upgt_devs_1, uaa->vendor, uaa->product) != NULL)
return (UMATCH_VENDOR_PRODUCT);
if (usb_lookup(upgt_devs_2, uaa->vendor, uaa->product) != NULL)
return (UMATCH_VENDOR_PRODUCT);
return (UMATCH_NONE);
}
void
upgt_attach(struct device *parent, struct device *self, void *aux)
{
struct upgt_softc *sc = (struct upgt_softc *)self;
struct usb_attach_arg *uaa = aux;
usb_interface_descriptor_t *id;
usb_endpoint_descriptor_t *ed;
<
8BD0
div>
usbd_status error;
int i;
/*
* Attach USB device.
*/
sc->sc_udev = uaa->device;
/* check device type */
if (upgt_device_type(sc, uaa->vendor, uaa->product) != 0)
return;
/* set configuration number */
if (usbd_set_config_no(sc->sc_udev, UPGT_CONFIG_NO, 0) != 0) {
printf("%s: could not set configuration no!\n",
sc->sc_dev.dv_xname);
return;
}
/* get the first interface handle */
error = usbd_device2interface_handle(sc->sc_udev, UPGT_IFACE_INDEX,
&sc->sc_iface);
if (error != 0) {
printf("%s: could not get interface handle!\n",
sc->sc_dev.dv_xname);
return;
}
/* find endpoints */
id = usbd_get_interface_descriptor(sc->sc_iface);
sc->sc_rx_no = sc->sc_tx_no = -1;
for (i = 0; i < id->bNumEndpoints; i++) {
ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
if (ed == NULL) {
printf("%s: no endpoint descriptor for iface %d!\n",
sc->sc_dev.dv_xname, i);
return;
}
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
sc->sc_tx_no = ed->bEndpointAddress;
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
sc->sc_rx_no = ed->bEndpointAddress;
/*
* 0x01 TX pipe
* 0x81 RX pipe
*
* Deprecated scheme (not used with fw version >2.5.6.x):
* 0x02 TX MGMT pipe
* 0x82 TX MGMT pipe
*/
if (sc->sc_tx_no != -1 && sc->sc_rx_no != -1)
break;
}
if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
printf("%s: missing endpoint!\n", sc->sc_dev.dv_xname);
return;
}
/* setup tasks and timeouts */
usb_init_task(&sc->sc_task_newstate, upgt_newstate_task, sc,
USB_TASK_TYPE_GENERIC);
usb_init_task(&sc->sc_task_tx, upgt_tx_task, sc, USB_TASK_TYPE_GENERIC);
timeout_set(&sc->scan_to, upgt_next_scan, sc);
timeout_set(&sc->led_to, upgt_set_led_blink, sc);
/*
* Open TX and RX USB bulk pipes.
*/
error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
&sc->sc_tx_pipeh);
if (error != 0) {
printf("%s: could not open TX pipe: %s!\n",
sc->sc_dev.dv_xname, usbd_errstr(error));
goto fail;
}
error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
&sc->sc_rx_pipeh);
if (error != 0) {
printf("%s: could not open RX pipe: %s!\n",
sc->sc_dev.dv_xname, usbd_errstr(error));
goto fail;
}
/*
* Allocate TX, RX, and CMD xfers.
*/
if (upgt_alloc_tx(sc) != 0)
goto fail;
if (upgt_alloc_rx(sc) != 0)
goto fail;
if (upgt_alloc_cmd(sc) != 0)
goto fail;
/*
* We need the firmware loaded to complete the attach.
*/
if (rootvp == NULL)
mountroothook_establish(upgt_attach_hook, sc);
else
upgt_attach_hook(sc);
return;
fail:
printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__);
}
void
upgt_attach_hook(void *arg)
{
struct upgt_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
usbd_status error;
int i;
/*
* Load firmware file into memory.
*/
if (upgt_fw_alloc(sc) != 0)
goto fail;
/*
* Initialize the device.
*/
if (upgt_device_init(sc) != 0)
goto fail;
/*
* Verify the firmware.
*/
if (upgt_fw_verify(sc) != 0)
goto fail;
/*
* Calculate device memory space.
*/
if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
printf("%s: could not find memory space addresses on FW!\n",
sc->sc_dev.dv_xname);
goto fail;
}
sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
DPRINTF(1, "%s: memory address frame start=0x%08x\n",
sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start);
DPRINTF(1, "%s: memory address frame end=0x%08x\n",
sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end);
DPRINTF(1, "%s: memory address rx start=0x%08x\n",
sc->sc_dev.dv_xname, sc->sc_memaddr_rx_start);
upgt_mem_init(sc);
/*
* Load the firmware.
*/
if (upgt_fw_load(sc) != 0)
goto fail;
/*
* Startup the RX pipe.
*/
struct upgt_data *data_rx = &sc->rx_data;
usbd_setup_xfer(data_rx->xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf,
MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb);
error = usbd_transfer(data_rx->xfer);
if (error != 0 && error != USBD_IN_PROGRESS) {
printf("%s: could not queue RX transfer!\n",
sc->sc_dev.dv_xname);
goto fail;
}
usbd_delay_ms(sc->sc_udev, 100);
/*
* Read the whole EEPROM content and parse it.
*/
if (upgt_eeprom_read(sc) != 0)
goto fail;
if (upgt_eeprom_parse(sc) != 0)
goto fail;
/*
* Setup the 802.11 device.
*/
ic->ic_phytype = IEEE80211_T_OFDM;
ic->ic_opmode = IEEE80211_M_STA;
ic->ic_state = IEEE80211_S_INIT;
ic->ic_caps =
IEEE80211_C_MONITOR |
IEEE80211_C_SHPREAMBLE |
IEEE80211_C_SHSLOT |
IEEE80211_C_WEP |
IEEE80211_C_RSN;
ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
for (i = 1; i <= 14; i++) {
ic->ic_channels[i].ic_freq =
ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
ic->ic_channels[i].ic_flags =
IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
}
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = upgt_ioctl;
ifp->if_start = upgt_start;
ifp->if_watchdog = upgt_watchdog;
IFQ_SET_READY(&ifp->if_snd);
memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
if_attach(ifp);
ieee80211_ifattach(ifp);
ic->ic_newassoc = upgt_newassoc;
sc->sc_newstate = ic->ic_newstate;
ic->ic_newstate = upgt_newstate;
ieee80211_media_init(ifp, upgt_media_change, ieee80211_media_status);
#if NBPFILTER > 0
bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
sc->sc_rxtap.wr_ihdr.it_present = htole32(UPGT_RX_RADIOTAP_PRESENT);
sc->sc_txtap_len = sizeof(sc->sc_txtapu);
sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
sc->sc_txtap.wt_ihdr.it_present = htole32(UPGT_TX_RADIOTAP_PRESENT);
#endif
printf("%s: address %s\n",
sc->sc_dev.dv_xname, ether_sprintf(ic->ic_myaddr));
return;
fail:
printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__);
}
int
upgt_detach(struct device *self, int flags)
{
struct upgt_softc *sc = (struct upgt_softc *)self;
struct ifnet *ifp = &sc->sc_ic.ic_if;
int s;
DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
s = splusb();
/* abort and close TX / RX pipes */
if (sc->sc_tx_pipeh != NULL) {
usbd_abort_pipe(sc->sc_tx_pipeh);
usbd_close_pipe(sc->sc_tx_pipeh);
}
if (sc->sc_rx_pipeh != NULL) {
usbd_abort_pipe(sc->sc_rx_pipeh);
usbd_close_pipe(sc->sc_rx_pipeh);
}
/* remove tasks and timeouts */
usb_rem_task(sc->sc_udev, &sc->sc_task_newstate);
usb_rem_task(sc->sc_udev, &sc->sc_task_tx);
if (timeout_initialized(&sc->scan_to))
timeout_del(&sc->scan_to);
if (timeout_initialized(&sc->led_to))
timeout_del(&sc->led_to);
/* free xfers */
upgt_free_tx(sc);
upgt_free_rx(sc);
upgt_free_cmd(sc);
/* free firmware */
upgt_fw_free(sc);
if (ifp->if_softc != NULL) {
/* detach interface */
ieee80211_ifdetach(ifp);
if_detach(ifp);
}
splx(s);
return (0);
}
int
upgt_activate(struct device *self, int act)
{
struct upgt_softc *sc = (struct upgt_softc *)self;
switch (act) {
case DVACT_DEACTIVATE:
usbd_deactivate(sc->sc_udev);
break;
}
return (0);
}
int
upgt_device_type(struct upgt_softc *sc, uint16_t vendor, uint16_t product)
{
if (usb_lookup(upgt_devs_1, vendor, product) != NULL) {
sc->sc_device_type = 1;
/* XXX */
printf("%s: version 1 devices not supported yet!\n",
sc->sc_dev.dv_xname);
return (1);
} else {
sc->sc_device_type = 2;
}
return (0);
}
int
upgt_device_init(struct upgt_softc *sc)
{
struct upgt_data *data_cmd = &sc->cmd_data;
char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
int len;
len = sizeof(init_cmd);
bcopy(init_cmd, data_cmd->buf, len);
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
printf("%s: could not send device init string!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
usbd_delay_ms(sc->sc_udev, 100);
DPRINTF(1, "%s: device initialized\n", sc->sc_dev.dv_xname);
return (0);
}
int
upgt_mem_init(struct upgt_softc *sc)
{
int i;
for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
sc->sc_memory.page[i].used = 0;
if (i == 0) {
/*
* The first memory page is always reserved for
* command data.
*/
sc->sc_memory.page[i].addr =
sc->sc_memaddr_frame_start + MCLBYTES;
} else {
sc->sc_memory.page[i].addr =
sc->sc_memory.page[i - 1].addr + MCLBYTES;
}
if (sc->sc_memory.page[i].addr + MCLBYTES >=
sc->sc_memaddr_frame_end)
break;
DPRINTF(2, "%s: memory address page %d=0x%08x\n",
sc->sc_dev.dv_xname, i, sc->sc_memory.page[i].addr);
}
sc->sc_memory.pages = i;
DPRINTF(2, "%s: memory pages=%d\n",
sc->sc_dev.dv_xname, sc->sc_memory.pages);
return (0);
}
uint32_t
upgt_mem_alloc(struct upgt_softc *sc)
{
int i;
for (i = 0; i < sc->sc_memory.pages; i++) {
if (sc->sc_memory.page[i].used == 0) {
sc->sc_memory.page[i].used = 1;
return (sc->sc_memory.page[i].addr);
}
}
return (0);
}
void
upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
{
int i;
for (i = 0; i < sc->sc_memory.pages; i++) {
if (sc->sc_memory.page[i].addr == addr) {
sc->sc_memory.page[i].used = 0;
return;
}
}
printf("%s: could n
10BC8
ot free memory address 0x%08x!\n",
sc->sc_dev.dv_xname, addr);
}
int
upgt_fw_alloc(struct upgt_softc *sc)
{
const char *name = "upgt-gw3887";
int error;
if (sc->sc_fw == NULL) {
error = loadfirmware(name, &sc->sc_fw, &sc->sc_fw_size);
if (error != 0) {
printf("%s: error %d, could not read firmware %s!\n",
sc->sc_dev.dv_xname, error, name);
return (EIO);
}
}
DPRINTF(1, "%s: firmware %s allocated\n", sc->sc_dev.dv_xname, name);
return (0);
}
void
upgt_fw_free(struct upgt_softc *sc)
{
if (sc->sc_fw != NULL) {
free(sc->sc_fw, M_DEVBUF);
sc->sc_fw = NULL;
DPRINTF(1, "%s: firmware freed\n", sc->sc_dev.dv_xname);
}
}
int
upgt_fw_verify(struct upgt_softc *sc)
{
struct upgt_fw_bra_option *bra_option;
uint32_t bra_option_type, bra_option_len;
uint32_t *uc;
int offset, bra_end = 0;
/*
* Seek to beginning of Boot Record Area (BRA).
*/
for (offset = 0; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
uc = (uint32_t *)(sc->sc_fw + offset);
if (*uc == 0)
break;
}
for (; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
uc = (uint32_t *)(sc->sc_fw + offset);
if (*uc != 0)
break;
}
if (offset == sc->sc_fw_size) {
printf("%s: firmware Boot Record Area not found!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
DPRINTF(1, "%s: firmware Boot Record Area found at offset %d\n",
sc->sc_dev.dv_xname, offset);
/*
* Parse Boot Record Area (BRA) options.
*/
while (offset < sc->sc_fw_size && bra_end == 0) {
/* get current BRA option */
bra_option = (struct upgt_fw_bra_option *)(sc->sc_fw + offset);
bra_option_type = letoh32(bra_option->type);
bra_option_len = letoh32(bra_option->len) * sizeof(*uc);
switch (bra_option_type) {
case UPGT_BRA_TYPE_FW:
DPRINTF(1, "%s: UPGT_BRA_TYPE_FW len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
printf("%s: wrong UPGT_BRA_TYPE_FW len!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_option->data,
bra_option_len) == 0) {
sc->sc_fw_type = UPGT_FWTYPE_LM86;
break;
}
if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_option->data,
bra_option_len) == 0) {
sc->sc_fw_type = UPGT_FWTYPE_LM87;
break;
}
if (memcmp(UPGT_BRA_FWTYPE_FMAC, bra_option->data,
bra_option_len) == 0) {
sc->sc_fw_type = UPGT_FWTYPE_FMAC;
break;
}
printf("%s: unsupported firmware type!\n",
sc->sc_dev.dv_xname);
return (EIO);
case UPGT_BRA_TYPE_VERSION:
DPRINTF(1, "%s: UPGT_BRA_TYPE_VERSION len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
break;
case UPGT_BRA_TYPE_DEPIF:
DPRINTF(1, "%s: UPGT_BRA_TYPE_DEPIF len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
break;
case UPGT_BRA_TYPE_EXPIF:
DPRINTF(1, "%s: UPGT_BRA_TYPE_EXPIF len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
break;
case UPGT_BRA_TYPE_DESCR:
DPRINTF(1, "%s: UPGT_BRA_TYPE_DESCR len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
struct upgt_fw_bra_descr *descr =
(struct upgt_fw_bra_descr *)bra_option->data;
sc->sc_memaddr_frame_start =
letoh32(descr->memaddr_space_start);
sc->sc_memaddr_frame_end =
letoh32(descr->memaddr_space_end);
DPRINTF(2, "%s: memory address space start=0x%08x\n",
sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start);
DPRINTF(2, "%s: memory address space end=0x%08x\n",
sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end);
break;
case UPGT_BRA_TYPE_END:
DPRINTF(1, "%s: UPGT_BRA_TYPE_END len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
bra_end = 1;
break;
default:
DPRINTF(1, "%s: unknown BRA option len=%d\n",
sc->sc_dev.dv_xname, bra_option_len);
return (EIO);
}
/* jump to next BRA option */
offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
}
DPRINTF(1, "%s: firmware verified\n", sc->sc_dev.dv_xname);
return (0);
}
int
upgt_fw_load(struct upgt_softc *sc)
{
struct upgt_data *data_cmd = &sc->cmd_data;
struct upgt_data *data_rx = &sc->rx_data;
char start_fwload_cmd[] = { 0x3c, 0x0d };
int offset, bsize, n, i, len;
uint32_t crc32;
/* send firmware start load command */
len = sizeof(start_fwload_cmd);
bcopy(start_fwload_cmd, data_cmd->buf, len);
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
printf("%s: could not send start_firmware_load command!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
/* send X2 header */
len = sizeof(struct upgt_fw_x2_header);
struct upgt_fw_x2_header *x2 = data_cmd->buf;
bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE);
x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
x2->len = htole32(sc->sc_fw_size);
x2->crc = upgt_crc32_le(data_cmd->buf + UPGT_X2_SIGNATURE_SIZE,
sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
sizeof(uint32_t));
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
printf("%s: could not send firmware X2 header!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
/* download firmware */
for (offset = 0; offset < sc->sc_fw_size; offset += bsize) {
if (sc->sc_fw_size - offset > UPGT_FW_BLOCK_SIZE)
bsize = UPGT_FW_BLOCK_SIZE;
else
bsize = sc->sc_fw_size - offset;
n = upgt_fw_copy(sc->sc_fw + offset, data_cmd->buf, bsize);
DPRINTF(1, "%s: FW offset=%d, read=%d, sent=%d\n",
sc->sc_dev.dv_xname, offset, n, bsize);
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &bsize, 0)
!= 0) {
printf("%s: error while downloading firmware block!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
bsize = n;
}
DPRINTF(1, "%s: firmware downloaded\n", sc->sc_dev.dv_xname);
/* load firmware */
crc32 = upgt_crc32_le(sc->sc_fw, sc->sc_fw_size);
*((uint32_t *)(data_cmd->buf) ) = crc32;
*((uint8_t *)(data_cmd->buf) + 4) = 'g';
*((uint8_t *)(data_cmd->buf) + 5) = '\r';
len = 6;
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
printf("%s: could not send load_firmware command!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
for (i = 0; i < UPGT_FIRMWARE_TIMEOUT; i++) {
len = UPGT_FW_BLOCK_SIZE;
bzero(data_rx->buf, MCLBYTES);
if (upgt_bulk_xmit(sc, data_rx, sc->sc_rx_pipeh, &len,
USBD_SHORT_XFER_OK) != 0) {
printf("%s: could not read firmware response!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
if (memcmp(data_rx->buf, "OK", 2) == 0)
break; /* firmware load was successful */
}
if (i == UPGT_FIRMWARE_TIMEOUT) {
printf("%s: firmware load failed!\n", sc->sc_dev.dv_xname);
return (EIO);
}
DPRINTF(1, "%s: firmware loaded\n", sc->sc_dev.dv_xname);
return (0);
}
/*
* While copying the version 2 firmware, we need to replace two characters:
*
* 0x7e -> 0x7d 0x5e
* 0x7d -> 0x7d 0x5d
*/
int
upgt_fw_copy(char *src, char *dst, int size)
{
int i, j;
for (i = 0, j = 0; i < size && j < size; i++) {
switch (src[i]) {
case 0x7e:
dst[j] = 0x7d;
j++;
dst[j] = 0x5e;
j++;
break;
case 0x7d:
dst[j] = 0x7d;
j++;
dst[j] = 0x5d;
j++;
break;
default:
dst[j] = src[i];
j++;
break;
}
}
return (i);
}
int
upgt_eeprom_read(struct upgt_softc *sc)
{
struct upgt_data *data_cmd = &sc->cmd_data;
struct upgt_lmac_mem *mem;
struct upgt_lmac_eeprom *eeprom;
int offset, block, len;
offset = 0;
block = UPGT_EEPROM_BLOCK_SIZE;
while (offset < UPGT_EEPROM_SIZE) {
DPRINTF(1, "%s: request EEPROM block (offset=%d, len=%d)\n",
sc->sc_dev.dv_xname, offset, block);
/*
* Transmit the URB containing the CMD data.
*/
bzero(data_cmd->buf, MCLBYTES);
mem = (struct upgt_lmac_mem *)data_cmd->buf;
mem->addr = htole32(sc->sc_memaddr_frame_start +
UPGT_MEMSIZE_FRAME_HEAD);
eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
eeprom->header1.flags = 0;
eeprom->header1.type = UPGT_H1_TYPE_CTRL;
eeprom->header1.len = htole16((
sizeof(struct upgt_lmac_eeprom) -
sizeof(struct upgt_lmac_header)) + block);
eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
eeprom->header2.flags = 0;
eeprom->offset = htole16(offset);
eeprom->len = htole16(block);
len = sizeof(*mem) + sizeof(*eeprom) + block;
mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
len - sizeof(*mem));
if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len,
USBD_FORCE_SHORT_XFER) != 0) {
printf("%s: could not transmit EEPROM data URB!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) {
printf("%s: timeout while waiting for EEPROM data!\n",
sc->sc_dev.dv_xname);
return (EIO);
}
offset += block;
if (UPGT_EEPROM_SIZE - offset < block)
block = UPGT_EEPROM_SIZE - offset;
}
return (0);
}
int
upgt_eeprom_parse(struct upgt_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct upgt_eeprom_header *eeprom_header;
struct upgt_eeprom_option *eeprom_option;
uint16_t option_len;
uint16_t option_type;
uint16_t preamble_len;
int option_end = 0;
/* calculate eeprom options start offset */
eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
preamble_len = letoh16(eeprom_header->preamble_len);
eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
(sizeof(struct upgt_eeprom_header) + preamble_len));
while (!option_end) {
/* the eeprom option length is stored in words */
option_len =
(letoh16(eeprom_option->len) - 1) * sizeof(uint16_t);
option_type =
letoh16(eeprom_option->type);
switch (option_type) {
case UPGT_EEPROM_TYPE_NAME:
DPRINTF(1, "%s: EEPROM name len=%d\n",
sc->sc_dev.dv_xname, option_len);
break;
case UPGT_EEPROM_TYPE_SERIAL:
DPRINTF(1, "%s: EEPROM serial len=%d\n",
sc->sc_dev.dv_xname, option_len);
break;
case UPGT_EEPROM_TYPE_MAC:
DPRINTF(1, "%s: EEPROM mac len=%d\n",
sc->sc_dev.dv_xname, option_len);
IEEE80211_ADDR_COPY(ic->ic_myaddr, eeprom_option->data);
break;
case UPGT_EEPROM_TYPE_HWRX:
DPRINTF(1, "%s: EEPROM hwrx len=%d\n",
sc->sc_dev.dv_xname, option_len);