-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.c
1929 lines (1657 loc) · 51.6 KB
/
usb.c
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
/***********************************************************************
** Copyright (C) 2003 ACX100 Open Source Project
**
** The contents of this file are subject to the Mozilla Public
** License Version 1.1 (the "License"); you may not use this file
** except in compliance with the License. You may obtain a copy of
** the License at http://www.mozilla.org/MPL/
**
** Software distributed under the License is distributed on an "AS
** IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
** implied. See the License for the specific language governing
** rights and limitations under the License.
**
** Alternatively, the contents of this file may be used under the
** terms of the GNU Public License version 2 (the "GPL"), in which
** case the provisions of the GPL are applicable instead of the
** above. If you wish to allow the use of your version of this file
** only under the terms of the GPL and not to allow others to use
** your version of this file under the MPL, indicate your decision
** by deleting the provisions above and replace them with the notice
** and other provisions required by the GPL. If you do not delete
** the provisions above, a recipient may use your version of this
** file under either the MPL or the GPL.
** ---------------------------------------------------------------------
** Inquiries regarding the ACX100 Open Source Project can be
** made directly to:
**
** http://acx100.sf.net
** ---------------------------------------------------------------------
*/
/***********************************************************************
** USB support for TI ACX100 based devices. Many parts are taken from
** the PCI driver.
**
** Authors:
** Martin Wawro <martin.wawro AT uni-dortmund.de>
** Andreas Mohr <andi AT lisas.de>
**
** LOCKING
** callback functions called by USB core are running in interrupt context
** and thus have names with _i_.
*/
#define ACX_USB 1
#include <linux/version.h>
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
#include <linux/config.h>
#endif
#include <linux/types.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <linux/vmalloc.h>
#include "acx.h"
/***********************************************************************
*/
/* number of endpoints of an interface */
#define NUM_EP(intf) (intf)->altsetting[0].desc.bNumEndpoints
#define EP(intf, nr) (intf)->altsetting[0].endpoint[(nr)].desc
#define GET_DEV(udev) usb_get_dev((udev))
#define PUT_DEV(udev) usb_put_dev((udev))
#define SET_NETDEV_OWNER(ndev, owner) /* not needed anymore ??? */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
/* removed in 2.6.14. We will use fake value for now */
#define URB_ASYNC_UNLINK 0
#endif
/***********************************************************************
*/
/* ACX100 (TNETW1100) USB device: D-Link DWL-120+ */
#define ACX100_VENDOR_ID 0x2001
#define ACX100_PRODUCT_ID_UNBOOTED 0x3B01
#define ACX100_PRODUCT_ID_BOOTED 0x3B00
/* TNETW1450 USB devices */
#define VENDOR_ID_DLINK 0x07b8 /* D-Link Corp. */
#define PRODUCT_ID_WUG2400 0xb21a /* AboCom WUG2400 or SafeCom SWLUT-54125 */
#define VENDOR_ID_AVM_GMBH 0x057c
#define PRODUCT_ID_AVM_WLAN_USB 0x5601
#define PRODUCT_ID_AVM_WLAN_USB_si 0x6201 /* "self install" named Version: driver kills kernel on inbound scans from fritz box ??? */
#define VENDOR_ID_ZCOM 0x0cde
#define PRODUCT_ID_ZCOM_XG750 0x0017 /* not tested yet */
#define VENDOR_ID_TI 0x0451
#define PRODUCT_ID_TI_UNKNOWN 0x60c5 /* not tested yet */
#define ACX_USB_CTRL_TIMEOUT 5500 /* steps in ms */
/* Buffer size for fw upload, same for both ACX100 USB and TNETW1450 */
#define USB_RWMEM_MAXLEN 2048
/* The number of bulk URBs to use */
#define ACX_TX_URB_CNT 8
#define ACX_RX_URB_CNT 2
/* Should be sent to the bulkout endpoint */
#define ACX_USB_REQ_UPLOAD_FW 0x10
#define ACX_USB_REQ_ACK_CS 0x11
#define ACX_USB_REQ_CMD 0x12
/***********************************************************************
** Prototypes
*/
static int acxusb_e_probe(struct usb_interface *, const struct usb_device_id *);
static void acxusb_e_disconnect(struct usb_interface *);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
static void acxusb_i_complete_tx(struct urb *);
static void acxusb_i_complete_rx(struct urb *);
#else
static void acxusb_i_complete_tx(struct urb *, struct pt_regs *);
static void acxusb_i_complete_rx(struct urb *, struct pt_regs *);
#endif
static int acxusb_e_open(struct net_device *);
static int acxusb_e_close(struct net_device *);
static void acxusb_i_set_rx_mode(struct net_device *);
static int acxusb_boot(struct usb_device *, int is_tnetw1450, int *radio_type);
static void acxusb_l_poll_rx(acx_device_t *adev, usb_rx_t* rx);
static void acxusb_i_tx_timeout(struct net_device *);
/* static void dump_device(struct usb_device *); */
/* static void dump_device_descriptor(struct usb_device_descriptor *); */
/* static void dump_config_descriptor(struct usb_config_descriptor *); */
/***********************************************************************
** Module Data
*/
#define TXBUFSIZE sizeof(usb_txbuffer_t)
/*
* Now, this is just plain lying, but the device insists in giving us
* huge packets. We supply extra space after rxbuffer. Need to understand
* it better...
*/
#define RXBUFSIZE (sizeof(rxbuffer_t) + \
(sizeof(usb_rx_t) - sizeof(struct usb_rx_plain)))
static const struct usb_device_id
acxusb_ids[] = {
{ USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_BOOTED) },
{ USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_UNBOOTED) },
{ USB_DEVICE(VENDOR_ID_DLINK, PRODUCT_ID_WUG2400) },
{ USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB) },
{ USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB_si) },
{ USB_DEVICE(VENDOR_ID_ZCOM, PRODUCT_ID_ZCOM_XG750) },
{ USB_DEVICE(VENDOR_ID_TI, PRODUCT_ID_TI_UNKNOWN) },
{}
};
MODULE_DEVICE_TABLE(usb, acxusb_ids);
/* USB driver data structure as required by the kernel's USB core */
static struct usb_driver
acxusb_driver = {
.name = "acx_usb",
.probe = acxusb_e_probe,
.disconnect = acxusb_e_disconnect,
.id_table = acxusb_ids
};
static const struct net_device_ops acxusb_netdev_ops = {
.ndo_open = &acxusb_e_open,
.ndo_stop = &acxusb_e_close,
.ndo_start_xmit = &acx_i_start_xmit,
.ndo_set_multicast_list = &acxusb_i_set_rx_mode,
.ndo_change_mtu = &acx_e_change_mtu,
#ifdef HAVE_TX_TIMEOUT
.ndo_tx_timeout = &acxusb_i_tx_timeout,
#endif
.ndo_get_stats = &acx_e_get_stats,
};
/***********************************************************************
** USB helper
**
** ldd3 ch13 says:
** When the function is usb_kill_urb, the urb lifecycle is stopped. This
** function is usually used when the device is disconnected from the system,
** in the disconnect callback. For some drivers, the usb_unlink_urb function
** should be used to tell the USB core to stop an urb. This function does not
** wait for the urb to be fully stopped before returning to the caller.
** This is useful for stoppingthe urb while in an interrupt handler or when
** a spinlock is held, as waiting for a urb to fully stop requires the ability
** for the USB core to put the calling process to sleep. This function requires
** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
** to be stopped in order to work properly.
**
** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
** asynchronous while usb_kill_urb is synchronous and should be called
** directly (drivers/usb/core/urb.c))
**
** In light of this, timeout is just for paranoid reasons...
*
* Actually, it's useful for debugging. If we reach timeout, we're doing
* something wrong with the urbs.
*/
static void
acxusb_unlink_urb(struct urb* urb)
{
if (!urb)
return;
if (urb->status == -EINPROGRESS) {
int timeout = 10;
usb_unlink_urb(urb);
while (--timeout && urb->status == -EINPROGRESS) {
mdelay(1);
}
if (!timeout) {
printk("acx_usb: urb unlink timeout!\n");
}
}
}
/***********************************************************************
** EEPROM and PHY read/write helpers
*/
/***********************************************************************
** acxusb_s_read_phy_reg
*/
int
acxusb_s_read_phy_reg(acx_device_t *adev, u32 reg, u8 *charbuf)
{
/* mem_read_write_t mem; */
FN_ENTER;
printk("%s doesn't seem to work yet, disabled.\n", __func__);
/*
mem.addr = cpu_to_le16(reg);
mem.type = cpu_to_le16(0x82);
mem.len = cpu_to_le32(4);
acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
*charbuf = mem.data;
log(L_DEBUG, "read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
*/
FN_EXIT1(OK);
return OK;
}
/***********************************************************************
*/
int
acxusb_s_write_phy_reg(acx_device_t *adev, u32 reg, u8 value)
{
mem_read_write_t mem;
FN_ENTER;
mem.addr = cpu_to_le16(reg);
mem.type = cpu_to_le16(0x82);
mem.len = cpu_to_le32(4);
mem.data = value;
acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
log(L_DEBUG, "write radio PHY[0x%04X]=0x%02X\n", reg, value);
FN_EXIT1(OK);
return OK;
}
/***********************************************************************
** acxusb_s_issue_cmd_timeo
** Excecutes a command in the command mailbox
**
** buffer = a pointer to the data.
** The data must not include 4 byte command header
*/
/* TODO: ideally we shall always know how much we need
** and this shall be 0 */
#define BOGUS_SAFETY_PADDING 0x40
#undef FUNC
#define FUNC "issue_cmd"
#if !ACX_DEBUG
int
acxusb_s_issue_cmd_timeo(
acx_device_t *adev,
unsigned cmd,
void *buffer,
unsigned buflen,
unsigned timeout)
{
#else
int
acxusb_s_issue_cmd_timeo_debug(
acx_device_t *adev,
unsigned cmd,
void *buffer,
unsigned buflen,
unsigned timeout,
const char* cmdstr)
{
#endif
/* USB ignores timeout param */
struct usb_device *usbdev;
struct {
u16 cmd;
u16 status;
u8 data[1];
} ACX_PACKED *loc;
const char *devname;
int acklen, blocklen, inpipe, outpipe;
int cmd_status;
int result;
FN_ENTER;
devname = adev->ndev->name;
/* no "wlan%%d: ..." please */
if (!devname || !devname[0] || devname[4]=='%')
devname = "acx";
log(L_CTL, FUNC"(cmd:%s,buflen:%u,type:0x%04X)\n",
cmdstr, buflen,
buffer ? le16_to_cpu(((acx_ie_generic_t *)buffer)->type) : -1);
loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
if (!loc) {
printk("%s: "FUNC"(): no memory for data buffer\n", devname);
goto bad;
}
/* get context from acx_device */
usbdev = adev->usbdev;
/* check which kind of command was issued */
loc->cmd = cpu_to_le16(cmd);
loc->status = 0;
/* NB: buflen == frmlen + 4
**
** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
** read (cmd,status,rid,frmlen,data[frmlen]) back
**
** Configure: write (cmd,status,rid,frmlen,data[frmlen])
**
** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
*/
/* now write the parameters of the command if needed */
acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
blocklen = buflen;
if (buffer && buflen) {
/* if it's an INTERROGATE command, just pass the length
* of parameters to read, as data */
if (cmd == ACX1xx_CMD_INTERROGATE) {
blocklen = 4;
acklen = buflen + 4;
}
memcpy(loc->data, buffer, blocklen);
}
blocklen += 4; /* account for cmd,status */
/* obtain the I/O pipes */
outpipe = usb_sndctrlpipe(usbdev, 0);
inpipe = usb_rcvctrlpipe(usbdev, 0);
log(L_CTL, "ctrl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
log(L_CTL, "sending USB control msg (out) (blocklen=%d)\n", blocklen);
if (acx_debug & L_DATA)
acx_dump_bytes(loc, blocklen);
result = usb_control_msg(usbdev, outpipe,
ACX_USB_REQ_CMD, /* request */
USB_TYPE_VENDOR|USB_DIR_OUT, /* requesttype */
0, /* value */
0, /* index */
loc, /* dataptr */
blocklen, /* size */
ACX_USB_CTRL_TIMEOUT /* timeout in ms */
);
if (result == -ENODEV) {
log(L_CTL, "no device present (unplug?)\n");
goto good;
}
log(L_CTL, "wrote %d bytes\n", result);
if (result < 0) {
goto bad;
}
/* check for device acknowledge */
log(L_CTL, "sending USB control msg (in) (acklen=%d)\n", acklen);
loc->status = 0; /* delete old status flag -> set to IDLE */
/* shall we zero out the rest? */
result = usb_control_msg(usbdev, inpipe,
ACX_USB_REQ_CMD, /* request */
USB_TYPE_VENDOR|USB_DIR_IN, /* requesttype */
0, /* value */
0, /* index */
loc, /* dataptr */
acklen, /* size */
ACX_USB_CTRL_TIMEOUT /* timeout in ms */
);
if (result < 0) {
printk("%s: "FUNC"(): USB read error %d\n", devname, result);
goto bad;
}
if (acx_debug & L_CTL) {
printk("read %d bytes: ", result);
acx_dump_bytes(loc, result);
}
/*
check for result==buflen+4? Was seen:
interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
ctrl inpipe=0x80000280 outpipe=0x80000200
sending USB control msg (out) (blocklen=8)
01 00 00 00 0F 10 04 00
wrote 8 bytes
sending USB control msg (in) (acklen=12) sizeof(loc->data
read 4 bytes <==== MUST BE 12!!
*/
cmd_status = le16_to_cpu(loc->status);
if (cmd_status != 1) {
printk("%s: "FUNC"(): cmd_status is not SUCCESS: %d (%s)\n",
devname, cmd_status, acx_cmd_status_str(cmd_status));
/* TODO: goto bad; ? */
}
if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
memcpy(buffer, loc->data, buflen);
log(L_CTL, "response frame: cmd=0x%04X status=%d\n",
le16_to_cpu(loc->cmd),
cmd_status);
}
good:
kfree(loc);
FN_EXIT1(OK);
return OK;
bad:
/* Give enough info so that callers can avoid
** printing their own diagnostic messages */
#if ACX_DEBUG
printk("%s: "FUNC"(cmd:%s) FAILED\n", devname, cmdstr);
#else
printk("%s: "FUNC"(cmd:0x%04X) FAILED\n", devname, cmd);
#endif
dump_stack();
kfree(loc);
FN_EXIT1(NOT_OK);
return NOT_OK;
}
/***********************************************************************
** acxusb_boot()
** Inputs:
** usbdev -> Pointer to kernel's usb_device structure
**
** Returns:
** (int) Errorcode or 0 on success
**
** This function triggers the loading of the firmware image from harddisk
** and then uploads the firmware to the USB device. After uploading the
** firmware and transmitting the checksum, the device resets and appears
** as a new device on the USB bus (the device we can finally deal with)
*/
static inline int
acxusb_fw_needs_padding(firmware_image_t *fw_image, unsigned int usb_maxlen)
{
unsigned int num_xfers = ((fw_image->size - 1) / usb_maxlen) + 1;
return ((num_xfers % 2) == 0);
}
static int
acxusb_boot(struct usb_device *usbdev, int is_tnetw1450, int *radio_type)
{
char filename[sizeof("tiacx1NNusbcRR")];
firmware_image_t *fw_image = NULL;
char *usbbuf;
unsigned int offset;
unsigned int blk_len, inpipe, outpipe;
u32 num_processed;
u32 img_checksum, sum;
u32 file_size;
int result = -EIO;
int i;
FN_ENTER;
/* dump_device(usbdev); */
usbbuf = kmalloc(USB_RWMEM_MAXLEN, GFP_KERNEL);
if (!usbbuf) {
printk(KERN_ERR "acx: no memory for USB transfer buffer (%d bytes)\n", USB_RWMEM_MAXLEN);
result = -ENOMEM;
goto end;
}
if (is_tnetw1450) {
/* Obtain the I/O pipes */
outpipe = usb_sndbulkpipe(usbdev, 1);
inpipe = usb_rcvbulkpipe(usbdev, 2);
printk(KERN_DEBUG "wait for device ready\n");
for (i = 0; i <= 2; i++) {
result = usb_bulk_msg(usbdev, inpipe,
usbbuf,
USB_RWMEM_MAXLEN,
&num_processed,
2000
);
if ((*(u32 *)&usbbuf[4] == 0x40000001)
&& (*(u16 *)&usbbuf[2] == 0x1)
&& ((*(u16 *)usbbuf & 0x3fff) == 0)
&& ((*(u16 *)usbbuf & 0xc000) == 0xc000))
break;
msleep(10);
}
if (i == 2)
goto fw_end;
*radio_type = usbbuf[8];
} else {
/* Obtain the I/O pipes */
outpipe = usb_sndctrlpipe(usbdev, 0);
inpipe = usb_rcvctrlpipe(usbdev, 0);
/* FIXME: shouldn't be hardcoded */
*radio_type = RADIO_MAXIM_0D;
}
snprintf(filename, sizeof(filename), "tiacx1%02dusbc%02X",
is_tnetw1450 * 11, *radio_type);
fw_image = acx_s_read_fw(&usbdev->dev, filename, &file_size);
if (!fw_image) {
result = -EIO;
goto end;
}
log(L_INIT, "firmware size: %d bytes\n", file_size);
img_checksum = le32_to_cpu(fw_image->chksum);
if (is_tnetw1450) {
u8 cmdbuf[20];
const u8 *p;
u8 need_padding;
u32 tmplen, val;
memset(cmdbuf, 0, 16);
need_padding = acxusb_fw_needs_padding(fw_image, USB_RWMEM_MAXLEN);
tmplen = need_padding ? file_size-4 : file_size-8;
*(u16 *)&cmdbuf[0] = 0xc000;
*(u16 *)&cmdbuf[2] = 0x000b;
*(u32 *)&cmdbuf[4] = tmplen;
*(u32 *)&cmdbuf[8] = file_size-8;
*(u32 *)&cmdbuf[12] = img_checksum;
result = usb_bulk_msg(usbdev, outpipe, cmdbuf, 16, &num_processed, HZ);
if (result < 0)
goto fw_end;
p = (const u8 *)&fw_image->size;
/* first calculate checksum for image size part */
sum = p[0]+p[1]+p[2]+p[3];
p += 4;
/* now continue checksum for firmware data part */
tmplen = le32_to_cpu(fw_image->size);
for (i = 0; i < tmplen /* image size */; i++) {
sum += *p++;
}
if (sum != le32_to_cpu(fw_image->chksum)) {
printk("acx: FATAL: firmware upload: "
"checksums don't match! "
"(0x%08x vs. 0x%08x)\n",
sum, fw_image->chksum);
goto fw_end;
}
offset = 8;
while (offset < file_size) {
blk_len = file_size - offset;
if (blk_len > USB_RWMEM_MAXLEN) {
blk_len = USB_RWMEM_MAXLEN;
}
log(L_INIT, "uploading firmware (%d bytes, offset=%d)\n",
blk_len, offset);
memcpy(usbbuf, ((u8 *)fw_image) + offset, blk_len);
p = usbbuf;
for (i = 0; i < blk_len; i += 4) {
*(u32 *)p = be32_to_cpu(*(u32 *)p);
p += 4;
}
result = usb_bulk_msg(usbdev, outpipe, usbbuf, blk_len, &num_processed, HZ);
if ((result < 0) || (num_processed != blk_len))
goto fw_end;
offset += blk_len;
}
if (need_padding) {
printk(KERN_DEBUG "send padding\n");
memset(usbbuf, 0, 4);
result = usb_bulk_msg(usbdev, outpipe, usbbuf, 4, &num_processed, HZ);
if ((result < 0) || (num_processed != 4))
goto fw_end;
}
printk(KERN_DEBUG "read firmware upload result\n");
memset(cmdbuf, 0, 20); /* additional memset */
result = usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed, 2000);
if (result < 0)
goto fw_end;
if (*(u32 *)&cmdbuf[4] == 0x40000003)
goto fw_end;
if (*(u32 *)&cmdbuf[4])
goto fw_end;
if (*(u16 *)&cmdbuf[16] != 1)
goto fw_end;
val = *(u32 *)&cmdbuf[0];
if ((val & 0x3fff)
|| ((val & 0xc000) != 0xc000))
goto fw_end;
val = *(u32 *)&cmdbuf[8];
if (val & 2) {
result = usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed, 2000);
if (result < 0)
goto fw_end;
val = *(u32 *)&cmdbuf[8];
}
/* yup, no "else" here! */
if (val & 1) {
memset(usbbuf, 0, 4);
result = usb_bulk_msg(usbdev, outpipe, usbbuf, 4, &num_processed, HZ);
if ((result < 0) || (!num_processed))
goto fw_end;
}
printk("TNETW1450 firmware upload successful!\n");
result = 0;
goto end;
fw_end:
result = -EIO;
goto end;
} else {
/* ACX100 USB */
/* now upload the firmware, slice the data into blocks */
offset = 8;
while (offset < file_size) {
blk_len = file_size - offset;
if (blk_len > USB_RWMEM_MAXLEN) {
blk_len = USB_RWMEM_MAXLEN;
}
log(L_INIT, "uploading firmware (%d bytes, offset=%d)\n",
blk_len, offset);
memcpy(usbbuf, ((u8 *)fw_image) + offset, blk_len);
result = usb_control_msg(usbdev, outpipe,
ACX_USB_REQ_UPLOAD_FW,
USB_TYPE_VENDOR|USB_DIR_OUT,
(file_size - 8) & 0xffff, /* value */
(file_size - 8) >> 16, /* index */
usbbuf, /* dataptr */
blk_len, /* size */
3000 /* timeout in ms */
);
offset += blk_len;
if (result < 0) {
printk(KERN_ERR "acx: error %d during upload "
"of firmware, aborting\n", result);
goto end;
}
}
/* finally, send the checksum and reboot the device */
/* does this trigger the reboot? */
result = usb_control_msg(usbdev, outpipe,
ACX_USB_REQ_UPLOAD_FW,
USB_TYPE_VENDOR|USB_DIR_OUT,
img_checksum & 0xffff, /* value */
img_checksum >> 16, /* index */
NULL, /* dataptr */
0, /* size */
3000 /* timeout in ms */
);
if (result < 0) {
printk(KERN_ERR "acx: error %d during tx of checksum, "
"aborting\n", result);
goto end;
}
result = usb_control_msg(usbdev, inpipe,
ACX_USB_REQ_ACK_CS,
USB_TYPE_VENDOR|USB_DIR_IN,
img_checksum & 0xffff, /* value */
img_checksum >> 16, /* index */
usbbuf, /* dataptr */
8, /* size */
3000 /* timeout in ms */
);
if (result < 0) {
printk(KERN_ERR "acx: error %d during ACK of checksum, "
"aborting\n", result);
goto end;
}
if (*usbbuf != 0x10) {
printk(KERN_ERR "acx: invalid checksum?\n");
result = -EINVAL;
goto end;
}
result = 0;
}
end:
vfree(fw_image);
kfree(usbbuf);
FN_EXIT1(result);
return result;
}
/* FIXME: maybe merge it with usual eeprom reading, into common code? */
static void
acxusb_s_read_eeprom_version(acx_device_t *adev)
{
u8 eeprom_ver[0x8];
memset(eeprom_ver, 0, sizeof(eeprom_ver));
acx_s_interrogate(adev, &eeprom_ver, ACX1FF_IE_EEPROM_VER);
/* FIXME: which one of those values to take? */
adev->eeprom_version = eeprom_ver[5];
}
/*
* temporary helper function to at least fill important cfgopt members with
* useful replacement values until we figure out how one manages to fetch
* the configoption struct in the USB device case...
*/
static int
acxusb_s_fill_configoption(acx_device_t *adev)
{
adev->cfgopt_probe_delay = 200;
adev->cfgopt_dot11CCAModes = 4;
adev->cfgopt_dot11Diversity = 1;
adev->cfgopt_dot11ShortPreambleOption = 1;
adev->cfgopt_dot11PBCCOption = 1;
adev->cfgopt_dot11ChannelAgility = 0;
adev->cfgopt_dot11PhyType = 5;
adev->cfgopt_dot11TempType = 1;
return OK;
}
/***********************************************************************
** acxusb_e_probe()
**
** This function is invoked by the kernel's USB core whenever a new device is
** attached to the system or the module is loaded. It is presented a usb_device
** structure from which information regarding the device is obtained and evaluated.
** In case this driver is able to handle one of the offered devices, it returns
** a non-null pointer to a driver context and thereby claims the device.
*/
static void
dummy_netdev_init(struct net_device *ndev) {}
static int
acxusb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
{
struct usb_device *usbdev = interface_to_usbdev(intf);
acx_device_t *adev = NULL;
struct net_device *ndev = NULL;
struct usb_config_descriptor *config;
struct usb_endpoint_descriptor *epdesc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
struct usb_host_endpoint *ep;
#endif
struct usb_interface_descriptor *ifdesc;
const char* msg;
int numconfigs, numfaces, numep;
int result = OK;
int i;
int radio_type;
/* this one needs to be more precise in case there appears a TNETW1450 from the same vendor */
int is_tnetw1450 = (usbdev->descriptor.idVendor != ACX100_VENDOR_ID);
FN_ENTER;
if (is_tnetw1450) {
/* Boot the device (i.e. upload the firmware) */
acxusb_boot(usbdev, is_tnetw1450, &radio_type);
/* TNETW1450-based cards will continue right away with
* the same USB ID after booting */
} else {
/* First check if this is the "unbooted" hardware */
if (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED) {
/* Boot the device (i.e. upload the firmware) */
acxusb_boot(usbdev, is_tnetw1450, &radio_type);
/* DWL-120+ will first boot the firmware,
* then later have a *separate* probe() run
* since its USB ID will have changed after
* firmware boot!
* Since the first probe() run has no
* other purpose than booting the firmware,
* simply return immediately.
*/
log(L_INIT, "finished booting, returning from probe()\n");
result = OK; /* success */
goto end;
}
else
/* device not unbooted, but invalid USB ID!? */
if (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)
goto end_nodev;
}
/* Ok, so it's our device and it has already booted */
/* Allocate memory for a network device */
ndev = alloc_netdev(sizeof(*adev), "wlan%d", dummy_netdev_init);
/* (NB: memsets to 0 entire area) */
if (!ndev) {
msg = "acx: no memory for netdev\n";
goto end_nomem;
}
/* Register the callbacks for the network device functions */
ether_setup(ndev);
ndev->netdev_ops = &acxusb_netdev_ops,
#if IW_HANDLER_VERSION <= 5
ndev->get_wireless_stats = (void *)&acx_e_get_wireless_stats;
#endif
ndev->wireless_handlers = (struct iw_handler_def *)&acx_ioctl_handler_def;
#ifdef HAVE_TX_TIMEOUT
ndev->watchdog_timeo = 4 * HZ;
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)) && defined(SET_MODULE_OWNER)
SET_MODULE_OWNER(ndev);
#endif
/* Setup private driver context */
adev = ndev2adev(ndev);
adev->ndev = ndev;
adev->dev_type = DEVTYPE_USB;
adev->radio_type = radio_type;
if (is_tnetw1450) {
/* well, actually it's a TNETW1450, but since it
* seems to be sufficiently similar to TNETW1130,
* I don't want to change large amounts of code now */
adev->chip_type = CHIPTYPE_ACX111;
} else {
adev->chip_type = CHIPTYPE_ACX100;
}
adev->usbdev = usbdev;
spin_lock_init(&adev->lock); /* initial state: unlocked */
sema_init(&adev->sem, 1); /* initial state: 1 (upped) */
/* Check that this is really the hardware we know about.
** If not sure, at least notify the user that he
** may be in trouble...
*/
numconfigs = (int)usbdev->descriptor.bNumConfigurations;
if (numconfigs != 1)
printk("acx: number of configurations is %d, "
"this driver only knows how to handle 1, "
"be prepared for surprises\n", numconfigs);
config = &usbdev->config->desc;
numfaces = config->bNumInterfaces;
if (numfaces != 1)
printk("acx: number of interfaces is %d, "
"this driver only knows how to handle 1, "
"be prepared for surprises\n", numfaces);
ifdesc = &intf->altsetting->desc;
numep = ifdesc->bNumEndpoints;
log(L_DEBUG, "# of endpoints: %d\n", numep);
if (is_tnetw1450) {
adev->bulkoutep = 1;
adev->bulkinep = 2;
} else {
/* obtain information about the endpoint
** addresses, begin with some default values
*/
adev->bulkoutep = 1;
adev->bulkinep = 1;
for (i = 0; i < numep; i++) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
ep = usbdev->ep_in[i];
if (!ep)
continue;
epdesc = &ep->desc;
#else
epdesc = usb_epnum_to_ep_desc(usbdev, i);
if (!epdesc)
continue;
#endif
if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
if (epdesc->bEndpointAddress & 0x80)
adev->bulkinep = epdesc->bEndpointAddress & 0xF;
else
adev->bulkoutep = epdesc->bEndpointAddress & 0xF;
}
}
}
log(L_DEBUG, "bulkout ep: 0x%X\n", adev->bulkoutep);
log(L_DEBUG, "bulkin ep: 0x%X\n", adev->bulkinep);
/* already done by memset: adev->rxtruncsize = 0; */
log(L_DEBUG, "TXBUFSIZE=%d RXBUFSIZE=%d\n",
(int) TXBUFSIZE, (int) RXBUFSIZE);
/* Allocate the RX/TX containers. */
adev->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX_TX_URB_CNT, GFP_KERNEL);
if (!adev->usb_tx) {
msg = "acx: no memory for tx container";
goto end_nomem;
}
adev->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX_RX_URB_CNT, GFP_KERNEL);
if (!adev->usb_rx) {
msg = "acx: no memory for rx container";
goto end_nomem;
}
/* Setup URBs for bulk-in/out messages */
for (i = 0; i < ACX_RX_URB_CNT; i++) {
adev->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
if (!adev->usb_rx[i].urb) {
msg = "acx: no memory for input URB\n";
goto end_nomem;
}
adev->usb_rx[i].urb->status = 0;
adev->usb_rx[i].adev = adev;
adev->usb_rx[i].busy = 0;
}
for (i = 0; i< ACX_TX_URB_CNT; i++) {
adev->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
if (!adev->usb_tx[i].urb) {
msg = "acx: no memory for output URB\n";
goto end_nomem;
}
adev->usb_tx[i].urb->status = 0;
adev->usb_tx[i].adev = adev;
adev->usb_tx[i].busy = 0;
}
adev->tx_free = ACX_TX_URB_CNT;
usb_set_intfdata(intf, adev);
SET_NETDEV_DEV(ndev, &intf->dev);
/* TODO: move all of fw cmds to open()? But then we won't know our MAC addr
until ifup (it's available via reading ACX1xx_IE_DOT11_STATION_ID)... */
/* put acx out of sleep mode and initialize it */
acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
result = acx_s_init_mac(adev);
if (result)
goto end;
/* TODO: see similar code in pci.c */
acxusb_s_read_eeprom_version(adev);
acxusb_s_fill_configoption(adev);
acx_s_set_defaults(adev);
acx_s_get_firmware_version(adev);