Cleanups, Changelog additions, new release numbering scheme
[acx-mac80211.git] / usb.c
blobcf3290dc9dccf97f0ce362fe9a62062c1e96e545
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 * The ACX100 Open Source Project <acx100-devel@lists.sourceforge.net>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * USB support for TI ACX100/TNETW1450 based devices. Many parts are taken from
19 * the PCI driver.
21 * Authors:
22 * Martin Wawro <martin.wawro AT uni-dortmund.de>
23 * Andreas Mohr <andi AT lisas.de>
25 * LOCKING
26 * callback functions called by USB core are running in interrupt context
27 * and thus have names with _i_.
29 #define ACX_MAC80211_USB 1
31 #include <linux/version.h>
32 #include <linux/types.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/kernel.h>
36 #include <linux/usb.h>
37 #include <linux/netdevice.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/etherdevice.h>
40 #include <linux/wireless.h>
41 #include <net/iw_handler.h>
42 #include <linux/vmalloc.h>
43 #include <linux/ethtool.h>
44 #include <linux/workqueue.h>
46 #include "acx.h"
49 /***********************************************************************
51 /* number of endpoints of an interface */
52 #define NUM_EP(intf) (intf)->altsetting[0].desc.bNumEndpoints
53 #define EP(intf, nr) (intf)->altsetting[0].endpoint[(nr)].desc
54 #define GET_DEV(udev) usb_get_dev((udev))
55 #define PUT_DEV(udev) usb_put_dev((udev))
57 /* removed in 2.6.14. We will use fake value for now
58 * TODO: maybe we should just remove all lines that include
59 * URB_ASYNC_UNLINK somewhere?
61 #define URB_ASYNC_UNLINK 0
63 /***********************************************************************
65 /* ACX100 (TNETW1100) USB device: D-Link DWL-120+ */
66 #define ACX100_VENDOR_ID 0x2001
67 #define ACX100_PRODUCT_ID_UNBOOTED 0x3B01
68 #define ACX100_PRODUCT_ID_BOOTED 0x3B00
70 /* TNETW1450 USB devices */
71 #define VENDOR_ID_DLINK 0x07b8 /* D-Link Corp. */
72 #define PRODUCT_ID_WUG2400 0xb21a /* AboCom WUG2400 or SafeCom SWLUT-54125 */
73 #define VENDOR_ID_AVM_GMBH 0x057c
74 #define PRODUCT_ID_AVM_WLAN_USB 0x5601
75 #define PRODUCT_ID_AVM_WLAN_USB_si 0x6201 /* "self install" named Version:
76 * driver kills kernel on inbound scans from fritz box ?? */
77 #define VENDOR_ID_ZCOM 0x0cde
78 #define PRODUCT_ID_ZCOM_XG750 0x0017 /* not tested yet */
79 #define VENDOR_ID_TI 0x0451
80 #define PRODUCT_ID_TI_UNKNOWN 0x60c5 /* not tested yet */
82 #define ACX_USB_CTRL_TIMEOUT 5500 /* steps in ms */
84 /* Buffer size for fw upload, same for both ACX100 USB and TNETW1450 */
85 #define USB_RWMEM_MAXLEN 2048
87 /* The number of bulk URBs to use */
88 #define ACX_TX_URB_CNT 8
89 #define ACX_RX_URB_CNT 2
91 /* Should be sent to the bulkout endpoint */
92 #define ACX_USB_REQ_UPLOAD_FW 0x10
93 #define ACX_USB_REQ_ACK_CS 0x11
94 #define ACX_USB_REQ_CMD 0x12
96 /***********************************************************************
97 ** Prototypes
99 static int acxusb_e_probe(struct usb_interface *, const struct usb_device_id *);
100 static void acxusb_e_disconnect(struct usb_interface *);
101 static void acxusb_i_complete_tx(struct urb *);
102 static void acxusb_i_complete_rx(struct urb *);
103 static int acxusb_e_open(struct ieee80211_hw *);
104 static void acxusb_e_close(struct ieee80211_hw *);
105 //static void acxusb_i_set_rx_mode(struct net_device *);
106 static int acxusb_boot(struct usb_device *, int is_tnetw1450, int *radio_type);
108 static void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx);
110 /*static void acxusb_i_tx_timeout(struct net_device *);*/
112 /* static void dump_device(struct usb_device *); */
113 /* static void dump_device_descriptor(struct usb_device_descriptor *); */
114 /* static void dump_config_descriptor(struct usb_config_descriptor *); */
116 /***********************************************************************
117 ** Module Data
119 #define TXBUFSIZE sizeof(usb_txbuffer_t)
121 * Now, this is just plain lying, but the device insists in giving us
122 * huge packets. We supply extra space after rxbuffer. Need to understand
123 * it better...
125 #define RXBUFSIZE (sizeof(rxbuffer_t) + \
126 (sizeof(usb_rx_t) - sizeof(struct usb_rx_plain)))
128 static const struct usb_device_id acxusb_ids[] = {
129 {USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_BOOTED)},
130 {USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_UNBOOTED)},
131 {USB_DEVICE(VENDOR_ID_DLINK, PRODUCT_ID_WUG2400)},
132 {USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB)},
133 {USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB_si)},
134 {USB_DEVICE(VENDOR_ID_ZCOM, PRODUCT_ID_ZCOM_XG750)},
135 {USB_DEVICE(VENDOR_ID_TI, PRODUCT_ID_TI_UNKNOWN)},
139 MODULE_DEVICE_TABLE(usb, acxusb_ids);
141 /* USB driver data structure as required by the kernel's USB core */
142 static struct usb_driver
143 acxusb_driver = {
144 .name = "acx_usb",
145 .probe = acxusb_e_probe,
146 .disconnect = acxusb_e_disconnect,
147 .id_table = acxusb_ids
150 /***********************************************************************
151 ** USB helper
153 ** ldd3 ch13 says:
154 ** When the function is usb_kill_urb, the urb lifecycle is stopped. This
155 ** function is usually used when the device is disconnected from the system,
156 ** in the disconnect callback. For some drivers, the usb_unlink_urb function
157 ** should be used to tell the USB core to stop an urb. This function does not
158 ** wait for the urb to be fully stopped before returning to the caller.
159 ** This is useful for stoppingthe urb while in an interrupt handler or when
160 ** a spinlock is held, as waiting for a urb to fully stop requires the ability
161 ** for the USB core to put the calling process to sleep. This function requires
162 ** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
163 ** to be stopped in order to work properly.
165 ** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
166 ** asynchronous while usb_kill_urb is synchronous and should be called
167 ** directly (drivers/usb/core/urb.c))
169 ** In light of this, timeout is just for paranoid reasons...
171 ** Actually, it's useful for debugging. If we reach timeout, we're doing
172 ** something wrong with the urbs.
174 static void acxusb_unlink_urb(struct urb *urb)
176 if (!urb)
177 return;
179 if (urb->status == -EINPROGRESS) {
180 int timeout = 10;
182 usb_unlink_urb(urb);
183 while (--timeout && urb->status == -EINPROGRESS) {
184 mdelay(1);
186 if (!timeout) {
187 printk(KERN_ERR "acx_usb: urb unlink timeout!\n");
193 /***********************************************************************
194 ** EEPROM and PHY read/write helpers
196 /***********************************************************************
197 ** acxusb_s_read_phy_reg
199 int acxusb_s_read_phy_reg(acx_device_t * adev, u32 reg, u8 * charbuf)
201 /* mem_read_write_t mem; */
203 FN_ENTER;
205 printk("%s doesn't seem to work yet, disabled.\n", __func__);
208 mem.addr = cpu_to_le16(reg);
209 mem.type = cpu_to_le16(0x82);
210 mem.len = cpu_to_le32(4);
211 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
212 *charbuf = mem.data;
213 log(L_DEBUG, "acx: read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
216 FN_EXIT1(OK);
217 return OK;
221 /***********************************************************************
223 int acxusb_s_write_phy_reg(acx_device_t * adev, u32 reg, u8 value)
225 mem_read_write_t mem;
227 FN_ENTER;
229 mem.addr = cpu_to_le16(reg);
230 mem.type = cpu_to_le16(0x82);
231 mem.len = cpu_to_le32(4);
232 mem.data = value;
233 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
234 log(L_DEBUG, "acx: write radio PHY[0x%04X]=0x%02X\n", reg, value);
236 FN_EXIT1(OK);
237 return OK;
241 /***********************************************************************
242 ** acxusb_s_issue_cmd_timeo
243 ** Excecutes a command in the command mailbox
245 ** buffer = a pointer to the data.
246 ** The data must not include 4 byte command header
249 /* TODO: ideally we shall always know how much we need
250 ** and this shall be 0 */
251 #define BOGUS_SAFETY_PADDING 0x40
253 #undef FUNC
254 #define FUNC "issue_cmd"
256 #if !ACX_DEBUG
258 acxusb_s_issue_cmd_timeo(acx_device_t * adev,
259 unsigned cmd,
260 void *buffer, unsigned buflen, unsigned timeout)
262 #else
264 acxusb_s_issue_cmd_timeo_debug(acx_device_t * adev,
265 unsigned cmd,
266 void *buffer,
267 unsigned buflen,
268 unsigned timeout, const char *cmdstr)
270 #endif
271 /* USB ignores timeout param */
273 struct usb_device *usbdev;
274 struct {
275 u16 cmd;
276 u16 status;
277 u8 data[1];
278 } ACX_PACKED *loc;
279 const char *devname;
280 int acklen, blocklen, inpipe, outpipe;
281 int cmd_status;
282 int result;
284 FN_ENTER;
286 devname = wiphy_name(adev->ieee->wiphy);
287 /* no "wlan%%d: ..." please */
288 if (!devname || !devname[0] || devname[4] == '%')
289 devname = "acx";
291 log(L_CTL, "acx: " FUNC "(cmd:%s,buflen:%u,type:0x%04X)\n",
292 cmdstr, buflen,
293 buffer ? le16_to_cpu(((acx_ie_generic_t *) buffer)->type) : -1);
295 loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
296 if (!loc) {
297 printk("acx: %s: " FUNC "(): no memory for data buffer\n", devname);
298 goto bad;
301 /* get context from acx_device */
302 usbdev = adev->usbdev;
304 /* check which kind of command was issued */
305 loc->cmd = cpu_to_le16(cmd);
306 loc->status = 0;
308 /* NB: buflen == frmlen + 4
310 ** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
311 ** read (cmd,status,rid,frmlen,data[frmlen]) back
313 ** Configure: write (cmd,status,rid,frmlen,data[frmlen])
315 ** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
318 /* now write the parameters of the command if needed */
319 acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
320 blocklen = buflen;
321 if (buffer && buflen) {
322 /* if it's an INTERROGATE command, just pass the length
323 * of parameters to read, as data */
324 if (cmd == ACX1xx_CMD_INTERROGATE) {
325 blocklen = 4;
326 acklen = buflen + 4;
328 memcpy(loc->data, buffer, blocklen);
330 blocklen += 4; /* account for cmd,status */
332 /* obtain the I/O pipes */
333 outpipe = usb_sndctrlpipe(usbdev, 0);
334 inpipe = usb_rcvctrlpipe(usbdev, 0);
335 log(L_CTL, "acx: trl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
336 log(L_CTL, "acx: sending USB control msg (out) (blocklen=%d)\n", blocklen);
337 if (acx_debug & L_DATA)
338 acx_dump_bytes(loc, blocklen);
340 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_CMD, /* request */
341 USB_TYPE_VENDOR | USB_DIR_OUT, /* requesttype */
342 0, /* value */
343 0, /* index */
344 loc, /* dataptr */
345 blocklen, /* size */
346 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
349 if (result == -ENODEV) {
350 log(L_CTL, "acx: no device present (unplug?)\n");
351 goto good;
354 log(L_CTL, "acx: wrote %d bytes\n", result);
355 if (result < 0) {
356 goto bad;
359 /* check for device acknowledge */
360 log(L_CTL, "acx: sending USB control msg (in) (acklen=%d)\n", acklen);
361 loc->status = 0; /* delete old status flag -> set to IDLE */
362 /* shall we zero out the rest? */
363 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_CMD, /* request */
364 USB_TYPE_VENDOR | USB_DIR_IN, /* requesttype */
365 0, /* value */
366 0, /* index */
367 loc, /* dataptr */
368 acklen, /* size */
369 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
371 if (result < 0) {
372 printk("acx: %s: " FUNC "(): USB read error %d\n", devname, result);
373 goto bad;
375 if (acx_debug & L_CTL) {
376 printk("acx: read %d bytes: ", result);
377 acx_dump_bytes(loc, result);
381 check for result==buflen+4? Was seen:
383 interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
384 issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
385 ctrl inpipe=0x80000280 outpipe=0x80000200
386 sending USB control msg (out) (blocklen=8)
387 01 00 00 00 0F 10 04 00
388 wrote 8 bytes
389 sending USB control msg (in) (acklen=12) sizeof(loc->data
390 read 4 bytes <==== MUST BE 12!!
393 cmd_status = le16_to_cpu(loc->status);
394 if (cmd_status != 1) {
395 printk("acx: %s: " FUNC "(): cmd_status is not SUCCESS: %d (%s)\n",
396 devname, cmd_status, acx_cmd_status_str(cmd_status));
397 /* TODO: goto bad; ? */
399 if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
400 memcpy(buffer, loc->data, buflen);
401 log(L_CTL, "acx: response frame: cmd=0x%04X status=%d\n",
402 le16_to_cpu(loc->cmd), cmd_status);
404 good:
405 kfree(loc);
406 FN_EXIT1(OK);
407 return OK;
408 bad:
409 /* Give enough info so that callers can avoid
410 ** printing their own diagnostic messages */
411 #if ACX_DEBUG
412 printk("acx: %s: " FUNC "(cmd:%s) FAILED\n", devname, cmdstr);
413 #else
414 printk("acx: %s: " FUNC "(cmd:0x%04X) FAILED\n", devname, cmd);
415 #endif
416 dump_stack();
417 kfree(loc);
418 FN_EXIT1(NOT_OK);
419 return NOT_OK;
423 /***********************************************************************
424 ** acxusb_boot()
425 ** Inputs:
426 ** usbdev -> Pointer to kernel's usb_device structure
428 ** Returns:
429 ** (int) Errorcode or 0 on success
431 ** This function triggers the loading of the firmware image from harddisk
432 ** and then uploads the firmware to the USB device. After uploading the
433 ** firmware and transmitting the checksum, the device resets and appears
434 ** as a new device on the USB bus (the device we can finally deal with)
436 static inline int
437 acxusb_fw_needs_padding(firmware_image_t *fw_image, unsigned int usb_maxlen)
439 unsigned int num_xfers = ((fw_image->size - 1) / usb_maxlen) + 1;
441 return ((num_xfers % 2) == 0);
444 static int
445 acxusb_boot(struct usb_device *usbdev, int is_tnetw1450, int *radio_type)
447 char filename[sizeof("tiacx1NNusbcRR")];
449 firmware_image_t *fw_image = NULL;
450 char *usbbuf;
451 unsigned int offset;
452 unsigned int blk_len, inpipe, outpipe;
453 u32 num_processed;
454 u32 img_checksum, sum;
455 u32 file_size;
456 int result = -EIO;
457 int i;
459 FN_ENTER;
461 /* dump_device(usbdev); */
463 usbbuf = kmalloc(USB_RWMEM_MAXLEN, GFP_KERNEL);
464 if (!usbbuf) {
465 printk(KERN_ERR
466 "acx: no memory for USB transfer buffer (%d bytes)\n",
467 USB_RWMEM_MAXLEN);
468 result = -ENOMEM;
469 goto end;
471 if (is_tnetw1450) {
472 /* Obtain the I/O pipes */
473 outpipe = usb_sndbulkpipe(usbdev, 1);
474 inpipe = usb_rcvbulkpipe(usbdev, 2);
476 printk(KERN_DEBUG "acx: wait for device ready\n");
477 for (i = 0; i <= 2; i++) {
478 result = usb_bulk_msg(usbdev, inpipe,
479 usbbuf,
480 USB_RWMEM_MAXLEN,
481 &num_processed, 2000);
483 if ((*(u32 *) & usbbuf[4] == 0x40000001)
484 && (*(u16 *) & usbbuf[2] == 0x1)
485 && ((*(u16 *) usbbuf & 0x3fff) == 0)
486 && ((*(u16 *) usbbuf & 0xc000) == 0xc000))
487 break;
488 acx_s_mwait(10);
490 if (i == 2)
491 goto fw_end;
493 *radio_type = usbbuf[8];
494 } else {
495 /* Obtain the I/O pipes */
496 outpipe = usb_sndctrlpipe(usbdev, 0);
497 inpipe = usb_rcvctrlpipe(usbdev, 0);
499 /* FIXME: shouldn't be hardcoded */
500 *radio_type = RADIO_MAXIM_0D;
503 snprintf(filename, sizeof(filename), "tiacx1%02dusbc%02X",
504 is_tnetw1450 * 11, *radio_type);
506 fw_image = acx_s_read_fw(&usbdev->dev, filename, &file_size);
507 if (!fw_image) {
508 result = -EIO;
509 goto end;
511 log(L_INIT, "acx: firmware size: %d bytes\n", file_size);
513 img_checksum = le32_to_cpu(fw_image->chksum);
515 if (is_tnetw1450) {
516 u8 cmdbuf[20];
517 const u8 *p;
518 u8 need_padding;
519 u32 tmplen, val;
521 memset(cmdbuf, 0, 16);
523 need_padding =
524 acxusb_fw_needs_padding(fw_image, USB_RWMEM_MAXLEN);
525 tmplen = need_padding ? file_size - 4 : file_size - 8;
526 *(u16 *) & cmdbuf[0] = 0xc000;
527 *(u16 *) & cmdbuf[2] = 0x000b;
528 *(u32 *) & cmdbuf[4] = tmplen;
529 *(u32 *) & cmdbuf[8] = file_size - 8;
530 *(u32 *) & cmdbuf[12] = img_checksum;
532 result =
533 usb_bulk_msg(usbdev, outpipe, cmdbuf, 16, &num_processed,
534 HZ);
535 if (result < 0)
536 goto fw_end;
538 p = (const u8 *)&fw_image->size;
540 /* first calculate checksum for image size part */
541 sum = p[0] + p[1] + p[2] + p[3];
542 p += 4;
544 /* now continue checksum for firmware data part */
545 tmplen = le32_to_cpu(fw_image->size);
546 for (i = 0; i < tmplen /* image size */ ; i++) {
547 sum += *p++;
550 if (sum != le32_to_cpu(fw_image->chksum)) {
551 printk("acx: FATAL: firmware upload: "
552 "checksums don't match! "
553 "(0x%08x vs. 0x%08x)\n", sum, fw_image->chksum);
554 goto fw_end;
557 offset = 8;
558 while (offset < file_size) {
559 blk_len = file_size - offset;
560 if (blk_len > USB_RWMEM_MAXLEN) {
561 blk_len = USB_RWMEM_MAXLEN;
564 log(L_INIT,
565 "acx: uploading firmware (%d bytes, offset=%d)\n",
566 blk_len, offset);
567 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
569 p = usbbuf;
570 for (i = 0; i < blk_len; i += 4) {
571 *(u32 *) p = be32_to_cpu(*(u32 *) p);
572 p += 4;
575 result =
576 usb_bulk_msg(usbdev, outpipe, usbbuf, blk_len,
577 &num_processed, HZ);
578 if ((result < 0) || (num_processed != blk_len))
579 goto fw_end;
580 offset += blk_len;
582 if (need_padding) {
583 printk(KERN_DEBUG "acx: send padding\n");
584 memset(usbbuf, 0, 4);
585 result =
586 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
587 &num_processed, HZ);
588 if ((result < 0) || (num_processed != 4))
589 goto fw_end;
591 printk(KERN_DEBUG "acx: read firmware upload result\n");
592 memset(cmdbuf, 0, 20); /* additional memset */
593 result =
594 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed,
595 2000);
596 if (result < 0)
597 goto fw_end;
598 if (*(u32 *) & cmdbuf[4] == 0x40000003)
599 goto fw_end;
600 if (*(u32 *) & cmdbuf[4])
601 goto fw_end;
602 if (*(u16 *) & cmdbuf[16] != 1)
603 goto fw_end;
605 val = *(u32 *) & cmdbuf[0];
606 if ((val & 0x3fff)
607 || ((val & 0xc000) != 0xc000))
608 goto fw_end;
610 val = *(u32 *) & cmdbuf[8];
611 if (val & 2) {
612 result =
613 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20,
614 &num_processed, 2000);
615 if (result < 0)
616 goto fw_end;
617 val = *(u32 *) & cmdbuf[8];
619 /* yup, no "else" here! */
620 if (val & 1) {
621 memset(usbbuf, 0, 4);
622 result =
623 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
624 &num_processed, HZ);
625 if ((result < 0) || (!num_processed))
626 goto fw_end;
629 printk("acx: TNETW1450 firmware successfully uploaded\n");
630 result = 0;
631 goto end;
632 fw_end:
633 result = -EIO;
634 goto end;
635 } else {
636 /* ACX100 USB */
638 /* now upload the firmware, slice the data into blocks */
639 offset = 8;
640 while (offset < file_size) {
641 blk_len = file_size - offset;
642 if (blk_len > USB_RWMEM_MAXLEN) {
643 blk_len = USB_RWMEM_MAXLEN;
645 log(L_INIT,
646 "acx: uploading firmware (%d bytes, offset=%d)\n",
647 blk_len, offset);
648 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
649 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, (file_size - 8) & 0xffff, /* value */
650 (file_size - 8) >> 16, /* index */
651 usbbuf, /* dataptr */
652 blk_len, /* size */
653 3000 /* timeout in ms */
655 offset += blk_len;
656 if (result < 0) {
657 printk(KERN_ERR "acx: error %d while uploading "
658 "the firmware, aborting\n", result);
659 goto end;
663 /* finally, send the checksum and reboot the device */
664 /* does this trigger the reboot? */
665 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, img_checksum & 0xffff, /* value */
666 img_checksum >> 16, /* index */
667 NULL, /* dataptr */
668 0, /* size */
669 3000 /* timeout in ms */
671 if (result < 0) {
672 printk(KERN_ERR "acx: error %d during tx of checksum, "
673 "aborting\n", result);
674 goto end;
676 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_ACK_CS, USB_TYPE_VENDOR | USB_DIR_IN, img_checksum & 0xffff, /* value */
677 img_checksum >> 16, /* index */
678 usbbuf, /* dataptr */
679 8, /* size */
680 3000 /* timeout in ms */
682 if (result < 0) {
683 printk(KERN_ERR "acx: error %d during ACK of checksum, "
684 "aborting\n", result);
685 goto end;
687 if (*usbbuf != 0x10) {
688 printk(KERN_ERR "acx: invalid checksum?\n");
689 result = -EINVAL;
690 goto end;
692 result = 0;
695 end:
696 vfree(fw_image);
697 kfree(usbbuf);
699 FN_EXIT1(result);
700 return result;
704 /* FIXME: maybe merge it with usual eeprom reading, into common code? */
705 static void acxusb_s_read_eeprom_version(acx_device_t * adev)
707 u8 eeprom_ver[0x8];
709 memset(eeprom_ver, 0, sizeof(eeprom_ver));
710 acx_s_interrogate(adev, &eeprom_ver, ACX1FF_IE_EEPROM_VER);
712 /* FIXME: which one of those values to take? */
713 adev->eeprom_version = eeprom_ver[5];
718 * temporary helper function to at least fill important cfgopt members with
719 * useful replacement values until we figure out how one manages to fetch
720 * the configoption struct in the USB device case...
722 static int acxusb_s_fill_configoption(acx_device_t * adev)
724 adev->cfgopt_probe_delay = 200;
725 adev->cfgopt_dot11CCAModes = 4;
726 adev->cfgopt_dot11Diversity = 1;
727 adev->cfgopt_dot11ShortPreambleOption = 1;
728 adev->cfgopt_dot11PBCCOption = 1;
729 adev->cfgopt_dot11ChannelAgility = 0;
730 adev->cfgopt_dot11PhyType = 5;
731 adev->cfgopt_dot11TempType = 1;
732 return OK;
735 static const struct ieee80211_ops acxusb_hw_ops = {
736 .tx = acx_i_start_xmit,
737 .conf_tx = acx_net_conf_tx,
738 .add_interface = acx_add_interface,
739 .remove_interface = acx_remove_interface,
740 .start = acxusb_e_open,
741 .configure_filter = acx_i_set_multicast_list,
742 .stop = acxusb_e_close,
743 .config = acx_net_config,
744 .config_interface = acx_config_interface,
745 .set_key = acx_net_set_key,
746 .get_stats = acx_e_get_stats,
747 .get_tx_stats = acx_net_get_tx_stats,
750 /***********************************************************************
751 ** acxusb_e_probe()
753 ** This function is invoked by the kernel's USB core whenever a new device is
754 ** attached to the system or the module is loaded. It is presented a usb_device
755 ** structure from which information regarding the device is obtained and evaluated.
756 ** In case this driver is able to handle one of the offered devices, it returns
757 ** a non-null pointer to a driver context and thereby claims the device.
761 static int
762 acxusb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
764 struct usb_device *usbdev = interface_to_usbdev(intf);
765 acx_device_t *adev = NULL;
766 struct usb_config_descriptor *config;
767 struct usb_endpoint_descriptor *epdesc;
768 struct usb_host_endpoint *ep;
769 struct usb_interface_descriptor *ifdesc;
770 const char *msg;
771 int numconfigs, numfaces, numep;
772 int result = OK;
773 int i;
774 int err;
775 int radio_type;
776 /* this one needs to be more precise in case there appears
777 * a TNETW1450 from the same vendor */
778 int is_tnetw1450 = (usbdev->descriptor.idVendor != ACX100_VENDOR_ID);
779 struct ieee80211_hw *ieee;
781 FN_ENTER;
783 if (is_tnetw1450) {
784 /* Boot the device (i.e. upload the firmware) */
785 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
787 /* TNETW1450-based cards will continue right away with
788 * the same USB ID after booting */
789 } else {
790 /* First check if this is the "unbooted" hardware */
791 if (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED) {
793 /* Boot the device (i.e. upload the firmware) */
794 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
796 /* DWL-120+ will first boot the firmware,
797 * then later have a *separate* probe() run
798 * since its USB ID will have changed after
799 * firmware boot!
800 * Since the first probe() run has no
801 * other purpose than booting the firmware,
802 * simply return immediately.
804 log(L_INIT,
805 "acx: finished booting, returning from probe()\n");
806 result = OK; /* success */
807 goto end;
808 } else {
809 if (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)
810 /* device not unbooted, but invalid USB ID!? */
811 goto end_nodev;
815 /* Ok, so it's our device and it has already booted */
817 /* Allocate memory for a network device */
819 ieee = ieee80211_alloc_hw(sizeof(*adev), &acxusb_hw_ops);
820 if (!ieee) {
821 msg = "acx: no memory for ieee80211_dev\n";
822 goto end_nomem;
826 ieee->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
827 /* TODO: mainline doesn't support the following flags yet */
829 ~IEEE80211_HW_MONITOR_DURING_OPER &
830 ~IEEE80211_HW_WEP_INCLUDE_IV;
832 ieee->queues = 1;
834 /* Register the callbacks for the network device functions */
837 /* Setup private driver context */
839 adev = ieee2adev(ieee);
840 adev->ieee = ieee;
842 adev->dev_type = DEVTYPE_USB;
843 adev->radio_type = radio_type;
844 if (is_tnetw1450) {
845 /* well, actually it's a TNETW1450, but since it
846 * seems to be sufficiently similar to TNETW1130,
847 * I don't want to change large amounts of code now */
848 adev->chip_type = CHIPTYPE_ACX111;
849 } else {
850 adev->chip_type = CHIPTYPE_ACX100;
853 adev->usbdev = usbdev;
854 spin_lock_init(&adev->spinlock); /* initial state: unlocked */
855 mutex_init(&adev->mutex);
857 /* Check that this is really the hardware we know about.
858 ** If not sure, at least notify the user that he
859 ** may be in trouble...
861 numconfigs = (int)usbdev->descriptor.bNumConfigurations;
862 if (numconfigs != 1)
863 printk("acx: number of configurations is %d, "
864 "this driver only knows how to handle 1, "
865 "be prepared for surprises\n", numconfigs);
867 config = &usbdev->config->desc;
868 numfaces = config->bNumInterfaces;
869 if (numfaces != 1)
870 printk("acx: number of interfaces is %d, "
871 "this driver only knows how to handle 1, "
872 "be prepared for surprises\n", numfaces);
874 ifdesc = &intf->altsetting->desc;
875 numep = ifdesc->bNumEndpoints;
876 log(L_DEBUG, "acx: # of endpoints: %d\n", numep);
878 if (is_tnetw1450) {
879 adev->bulkoutep = 1;
880 adev->bulkinep = 2;
881 } else {
882 /* obtain information about the endpoint
883 ** addresses, begin with some default values
885 adev->bulkoutep = 1;
886 adev->bulkinep = 1;
887 for (i = 0; i < numep; i++) {
888 ep = usbdev->ep_in[i];
889 if (!ep)
890 continue;
891 epdesc = &ep->desc;
892 if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
893 if (epdesc->bEndpointAddress & 0x80)
894 adev->bulkinep =
895 epdesc->bEndpointAddress & 0xF;
896 else
897 adev->bulkoutep =
898 epdesc->bEndpointAddress & 0xF;
902 log(L_DEBUG, "acx: bulkout ep: 0x%X\n", adev->bulkoutep);
903 log(L_DEBUG, "acx: bulkin ep: 0x%X\n", adev->bulkinep);
905 /* already done by memset: adev->rxtruncsize = 0; */
906 log(L_DEBUG, "acx: TXBUFSIZE=%d RXBUFSIZE=%d\n",
907 (int)TXBUFSIZE, (int)RXBUFSIZE);
909 /* Allocate the RX/TX containers. */
910 adev->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX_TX_URB_CNT, GFP_KERNEL);
911 if (!adev->usb_tx) {
912 msg = "acx: no memory for tx container";
913 goto end_nomem;
915 adev->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX_RX_URB_CNT, GFP_KERNEL);
916 if (!adev->usb_rx) {
917 msg = "acx: no memory for rx container";
918 goto end_nomem;
921 /* Setup URBs for bulk-in/out messages */
922 for (i = 0; i < ACX_RX_URB_CNT; i++) {
923 adev->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
924 if (!adev->usb_rx[i].urb) {
925 msg = "acx: no memory for input URB\n";
926 goto end_nomem;
928 adev->usb_rx[i].urb->status = 0;
929 adev->usb_rx[i].adev = adev;
930 adev->usb_rx[i].busy = 0;
933 for (i = 0; i < ACX_TX_URB_CNT; i++) {
934 adev->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
935 if (!adev->usb_tx[i].urb) {
936 msg = "acx: no memory for output URB\n";
937 goto end_nomem;
939 adev->usb_tx[i].urb->status = 0;
940 adev->usb_tx[i].adev = adev;
941 adev->usb_tx[i].busy = 0;
943 adev->tx_free = ACX_TX_URB_CNT;
945 usb_set_intfdata(intf, adev);
946 SET_IEEE80211_DEV(ieee, &intf->dev);
948 /* TODO: move all of fw cmds to open()? But then we won't know our MAC addr
949 until ifup (it's available via reading ACX1xx_IE_DOT11_STATION_ID)... */
951 /* put acx out of sleep mode and initialize it */
952 acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
954 result = acx_s_init_mac(adev);
955 if (result)
956 goto end;
958 /* TODO: see similar code in pci.c */
959 acxusb_s_read_eeprom_version(adev);
960 acxusb_s_fill_configoption(adev);
961 acx_s_set_defaults(adev);
962 acx_s_get_firmware_version(adev);
963 acx_display_hardware_details(adev);
965 /* MAC_COPY(ndev->dev_addr, adev->dev_addr); */
967 err = acx_setup_modes(adev);
968 if (err) {
969 msg = "acx: can't register hwmode\n";
970 goto end_nomem;
973 /* Register the network device */
974 log(L_INIT, "acx: registering network device\n");
975 result = ieee80211_register_hw(adev->ieee);
976 if (result) {
977 msg = "acx: failed to register USB network device "
978 "(error %d)\n";
979 goto end_nomem;
982 acx_proc_register_entries(ieee);
985 printk("acx: USB module " ACX_RELEASE " loaded successfully\n");
987 acx_init_task_scheduler(adev);
989 #if CMD_DISCOVERY
990 great_inquisitor(adev);
991 #endif
993 /* Everything went OK, we are happy now */
994 result = OK;
995 goto end;
997 end_nomem:
998 printk(msg, result);
1000 if (ieee) {
1001 if (adev->usb_rx) {
1002 for (i = 0; i < ACX_RX_URB_CNT; i++)
1003 usb_free_urb(adev->usb_rx[i].urb);
1004 kfree(adev->usb_rx);
1006 if (adev->usb_tx) {
1007 for (i = 0; i < ACX_TX_URB_CNT; i++)
1008 usb_free_urb(adev->usb_tx[i].urb);
1009 kfree(adev->usb_tx);
1011 ieee80211_free_hw(ieee);
1014 result = -ENOMEM;
1015 goto end;
1017 end_nodev:
1018 /* no device we could handle, return error. */
1019 result = -EIO;
1021 end:
1022 FN_EXIT1(result);
1023 return result;
1027 /***********************************************************************
1028 ** acxusb_e_disconnect()
1030 ** This function is invoked whenever the user pulls the plug from the USB
1031 ** device or the module is removed from the kernel. In these cases, the
1032 ** network devices have to be taken down and all allocated memory has
1033 ** to be freed.
1035 void acxusb_e_disconnect(struct usb_interface *intf)
1037 unsigned long flags;
1038 int i;
1039 acx_device_t *adev = usb_get_intfdata(intf);
1041 FN_ENTER;
1043 /* No WLAN device... no sense */
1044 if (!adev)
1045 goto end;
1047 /* Unregister network device
1049 * If the interface is up, unregister_netdev() will take
1050 * care of calling our close() function, which takes
1051 * care of unlinking the urbs, sending the device to
1052 * sleep, etc...
1053 * This can't be called with sem or lock held because
1054 * _close() will try to grab it as well if it's called,
1055 * deadlocking the machine.
1057 acx_proc_unregister_entries(adev->ieee);
1058 ieee80211_unregister_hw(adev->ieee);
1060 acx_sem_lock(adev);
1061 acx_lock(adev, flags);
1062 /* This device exists no more */
1063 usb_set_intfdata(intf, NULL);
1066 * Here we only free them. _close() took care of
1067 * unlinking them.
1069 for (i = 0; i < ACX_RX_URB_CNT; ++i) {
1070 usb_free_urb(adev->usb_rx[i].urb);
1072 for (i = 0; i < ACX_TX_URB_CNT; ++i) {
1073 usb_free_urb(adev->usb_tx[i].urb);
1076 /* Freeing containers */
1077 kfree(adev->usb_rx);
1078 kfree(adev->usb_tx);
1080 acx_unlock(adev, flags);
1081 acx_sem_unlock(adev);
1083 ieee80211_free_hw(adev->ieee);
1084 end:
1085 FN_EXIT0;
1088 /***********************************************************************
1089 ** acxusb_e_open()
1090 ** This function is called when the user sets up the network interface.
1091 ** It initializes a management timer, sets up the USB card and starts
1092 ** the network tx queue and USB receive.
1094 int acxusb_e_open(struct ieee80211_hw *hw)
1096 acx_device_t *adev = ieee2adev(hw);
1097 unsigned long flags;
1098 int i;
1100 FN_ENTER;
1102 acx_sem_lock(adev);
1104 /* put the ACX100 out of sleep mode */
1105 // acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
1107 init_timer(&adev->mgmt_timer);
1108 adev->mgmt_timer.function = acx_i_timer;
1109 adev->mgmt_timer.data = (unsigned long)adev;
1111 /* acx_s_start needs it */
1112 SET_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1113 acx_s_start(adev);
1115 /* don't acx_start_queue() here, we need to associate first */
1117 acx_lock(adev, flags);
1118 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1119 adev->usb_rx[i].urb->status = 0;
1122 acxusb_l_poll_rx(adev, &adev->usb_rx[0]);
1124 ieee80211_start_queues(adev->ieee);
1125 acx_unlock(adev, flags);
1127 acx_sem_unlock(adev);
1129 FN_EXIT0;
1130 return 0;
1134 /***********************************************************************
1135 ** acxusb_e_close()
1137 ** This function stops the network functionality of the interface (invoked
1138 ** when the user calls ifconfig <wlan> down). The tx queue is halted and
1139 ** the device is marked as down. In case there were any pending USB bulk
1140 ** transfers, these are unlinked (asynchronously). The module in-use count
1141 ** is also decreased in this function.
1143 static void acxusb_e_close(struct ieee80211_hw *hw)
1145 acx_device_t *adev = ieee2adev(hw);
1146 unsigned long flags;
1147 int i;
1149 FN_ENTER;
1151 acx_sem_lock(adev);
1152 if (adev->dev_state_mask & ACX_STATE_IFACE_UP)
1154 // acxusb_e_down(adev);
1155 CLEAR_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1158 /* Code below is remarkably similar to acxpci_s_down(). Maybe we can merge them? */
1160 acx_free_modes(adev);
1162 /* Make sure we don't get any more rx requests */
1163 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_RX, NULL, 0);
1164 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_TX, NULL, 0);
1167 * We must do FLUSH *without* holding sem to avoid a deadlock.
1168 * See pci.c:acxpci_s_down() for deails.
1170 acx_sem_unlock(adev);
1171 flush_scheduled_work();
1172 acx_sem_lock(adev);
1174 /* Power down the device */
1175 acx_s_issue_cmd(adev, ACX1xx_CMD_SLEEP, NULL, 0);
1177 /* Stop the transmit queue, mark the device as DOWN */
1178 acx_lock(adev, flags);
1179 // acx_stop_queue(ndev, "on ifdown");
1180 // acx_set_status(adev, ACX_STATUS_0_STOPPED);
1181 /* stop pending rx/tx urb transfers */
1182 for (i = 0; i < ACX_TX_URB_CNT; i++) {
1183 acxusb_unlink_urb(adev->usb_tx[i].urb);
1184 adev->usb_tx[i].busy = 0;
1186 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1187 acxusb_unlink_urb(adev->usb_rx[i].urb);
1188 adev->usb_rx[i].busy = 0;
1190 adev->tx_free = ACX_TX_URB_CNT;
1191 acx_unlock(adev, flags);
1193 /* Must do this outside of lock */
1194 del_timer_sync(&adev->mgmt_timer);
1196 acx_sem_unlock(adev);
1198 FN_EXIT0;
1202 /***********************************************************************
1203 ** acxusb_l_poll_rx
1204 ** This function (re)initiates a bulk-in USB transfer on a given urb
1206 void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx)
1208 struct usb_device *usbdev;
1209 struct urb *rxurb;
1210 int errcode, rxnum;
1211 unsigned int inpipe;
1213 FN_ENTER;
1215 rxurb = rx->urb;
1216 usbdev = adev->usbdev;
1218 rxnum = rx - adev->usb_rx;
1220 inpipe = usb_rcvbulkpipe(usbdev, adev->bulkinep);
1221 if (unlikely(rxurb->status == -EINPROGRESS)) {
1222 printk(KERN_ERR
1223 "acx: error, rx triggered while rx urb in progress\n");
1224 /* FIXME: this is nasty, receive is being cancelled by this code
1225 * on the other hand, this should not happen anyway...
1227 usb_unlink_urb(rxurb);
1228 } else if (unlikely(rxurb->status == -ECONNRESET)) {
1229 log(L_USBRXTX, "acx: _poll_rx: connection reset\n");
1230 goto end;
1232 rxurb->actual_length = 0;
1233 usb_fill_bulk_urb(rxurb, usbdev, inpipe, &rx->bulkin, /* dataptr */
1234 RXBUFSIZE, /* size */
1235 acxusb_i_complete_rx, /* handler */
1236 rx /* handler param */
1238 rxurb->transfer_flags = URB_ASYNC_UNLINK;
1240 /* ATOMIC: we may be called from complete_rx() usb callback */
1241 errcode = usb_submit_urb(rxurb, GFP_ATOMIC);
1242 /* FIXME: evaluate the error code! */
1243 log(L_USBRXTX,
1244 "acx: SUBMIT RX (%d) inpipe=0x%X size=%d errcode=%d\n",
1245 rxnum, inpipe, (int)RXBUFSIZE, errcode);
1246 end:
1247 FN_EXIT0;
1251 /***********************************************************************
1252 ** acxusb_i_complete_rx()
1253 ** Inputs:
1254 ** urb -> pointer to USB request block
1255 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1257 ** This function is invoked by USB subsystem whenever a bulk receive
1258 ** request returns.
1259 ** The received data is then committed to the network stack and the next
1260 ** USB receive is triggered.
1262 void acxusb_i_complete_rx(struct urb *urb)
1264 acx_device_t *adev;
1265 rxbuffer_t *ptr;
1266 rxbuffer_t *inbuf;
1267 usb_rx_t *rx;
1268 unsigned long flags;
1269 int size, remsize, packetsize, rxnum;
1271 FN_ENTER;
1273 BUG_ON(!urb->context);
1275 rx = (usb_rx_t *) urb->context;
1276 adev = rx->adev;
1278 acx_lock(adev, flags);
1281 * Happens on disconnect or close. Don't play with the urb.
1282 * Don't resubmit it. It will get unlinked by close()
1284 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1285 log(L_USBRXTX,
1286 "acx: rx: device is down, not doing anything\n");
1287 goto end_unlock;
1290 inbuf = &rx->bulkin;
1291 size = urb->actual_length;
1292 remsize = size;
1293 rxnum = rx - adev->usb_rx;
1295 log(L_USBRXTX, "acx: RETURN RX (%d) status=%d size=%d\n",
1296 rxnum, urb->status, size);
1298 /* Send the URB that's waiting. */
1299 log(L_USBRXTX, "acx: rxnum=%d, sending=%d\n",
1300 rxnum, rxnum ^ 1);
1301 acxusb_l_poll_rx(adev, &adev->usb_rx[rxnum ^ 1]);
1303 if (unlikely(size > sizeof(rxbuffer_t)))
1304 printk("acx_usb: rx too large: %d, please report\n", size);
1306 /* check if the transfer was aborted */
1307 switch (urb->status) {
1308 case 0: /* No error */
1309 break;
1310 case -EOVERFLOW:
1311 printk(KERN_ERR "acx: rx data overrun\n");
1312 adev->rxtruncsize = 0; /* Not valid anymore. */
1313 goto end_unlock;
1314 case -ECONNRESET:
1315 adev->rxtruncsize = 0;
1316 goto end_unlock;
1317 case -ESHUTDOWN: /* rmmod */
1318 adev->rxtruncsize = 0;
1319 goto end_unlock;
1320 default:
1321 adev->rxtruncsize = 0;
1322 adev->stats.rx_errors++;
1323 printk("acx: rx error (urb status=%d)\n", urb->status);
1324 goto end_unlock;
1327 if (unlikely(!size))
1328 printk("acx: warning, encountered zerolength rx packet\n");
1330 if (urb->transfer_buffer != inbuf)
1331 goto end_unlock;
1333 /* check if previous frame was truncated
1334 ** FIXME: this code can only handle truncation
1335 ** of consecutive packets!
1337 ptr = inbuf;
1338 if (adev->rxtruncsize) {
1339 int tail_size;
1340 ptr = &adev->rxtruncbuf;
1341 packetsize = RXBUF_BYTES_USED(ptr);
1342 log(L_ANY,
1343 "acx: handling truncated frame (truncsize=%d size=%d "
1344 "packetsize(from trunc)=%d)\n",
1345 adev->rxtruncsize, size, packetsize);
1347 ptr = &adev->rxtruncbuf;
1348 packetsize = RXBUF_BYTES_USED(ptr);
1349 if (acx_debug & L_USBRXTX) {
1350 printk("acx: handling truncated frame (truncsize=%d size=%d "
1351 "packetsize(from trunc)=%d)\n",
1352 adev->rxtruncsize, size, packetsize);
1353 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1354 acx_dump_bytes(inbuf, RXBUF_HDRSIZE);
1357 /* bytes needed for rxtruncbuf completion: */
1358 tail_size = packetsize - adev->rxtruncsize;
1360 if (size < tail_size) {
1361 /* there is not enough data to complete this packet,
1362 ** simply append the stuff to the truncation buffer
1364 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf, size);
1365 adev->rxtruncsize += size;
1366 remsize = 0;
1367 } else {
1368 /* ok, this data completes the previously
1369 ** truncated packet. copy it into a descriptor
1370 ** and give it to the rest of the stack */
1372 /* append tail to previously truncated part
1373 ** NB: adev->rxtruncbuf (pointed to by ptr) can't
1374 ** overflow because this is already checked before
1375 ** truncation buffer was filled. See below,
1376 ** "if (packetsize > sizeof(rxbuffer_t))..." code */
1377 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf,
1378 tail_size);
1380 if (acx_debug & L_USBRXTX) {
1381 printk("acx: full trailing packet + 12 bytes:\n");
1382 acx_dump_bytes(inbuf,
1383 tail_size + RXBUF_HDRSIZE);
1385 acx_l_process_rxbuf(adev, ptr);
1386 adev->rxtruncsize = 0;
1387 ptr = (rxbuffer_t *) (((char *)inbuf) + tail_size);
1388 remsize -= tail_size;
1390 printk("acx: post-merge size=%d remsize=%d\n",
1391 size, remsize);
1394 /* size = USB data block size
1395 ** remsize = unprocessed USB bytes left
1396 ** ptr = current pos in USB data block
1398 while (remsize) {
1399 if (remsize < RXBUF_HDRSIZE) {
1400 printk("acx: truncated rx header (%d bytes)!\n",
1401 remsize);
1402 if (ACX_DEBUG)
1403 acx_dump_bytes(ptr, remsize);
1404 break;
1407 packetsize = RXBUF_BYTES_USED(ptr);
1408 log(L_USBRXTX,
1409 "acx: packet with packetsize=%d\n", packetsize);
1411 if (RXBUF_IS_TXSTAT(ptr)) {
1412 /* do rate handling */
1413 usb_txstatus_t *stat = (void *)ptr;
1415 log(L_USBRXTX,
1416 "acx: tx: stat: mac_cnt_rcvd:%04X "
1417 "queue_index:%02X mac_status:%02X "
1418 "hostdata:%08X rate:%u ack_failures:%02X "
1419 "rts_failures:%02X rts_ok:%02X\n",
1420 stat->mac_cnt_rcvd, stat->queue_index,
1421 stat->mac_status, stat->hostdata, stat->rate,
1422 stat->ack_failures, stat->rts_failures,
1423 stat->rts_ok);
1425 if (adev->rate_auto && client_no < VEC_SIZE(adev->sta_list)) {
1426 client_t *clt = &adev->sta_list[client_no];
1427 u16 cur = stat->hostdata >> 16;
1429 if (clt && clt->rate_cur == cur) {
1430 acx_l_handle_txrate_auto(adev, clt,
1431 cur, // intended rate
1432 stat->rate, 0, // actually used rate
1433 stat->mac_status, // error?
1434 ACX_TX_URB_CNT - adev->tx_free);
1437 */ goto next;
1440 if (packetsize > sizeof(rxbuffer_t)) {
1441 printk("acx: packet exceeds max wlan "
1442 "frame size (%d > %d). size=%d\n",
1443 packetsize, (int)sizeof(rxbuffer_t), size);
1444 if (ACX_DEBUG)
1445 acx_dump_bytes(ptr, 16);
1446 /* FIXME: put some real error-handling in here! */
1447 break;
1450 if (packetsize > remsize) {
1451 /* frame truncation handling */
1452 if (acx_debug & L_USBRXTX) {
1453 printk("acx: need to truncate packet, "
1454 "packetsize=%d remsize=%d "
1455 "size=%d bytes:",
1456 packetsize, remsize, size);
1457 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1459 memcpy(&adev->rxtruncbuf, ptr, remsize);
1460 adev->rxtruncsize = remsize;
1461 break;
1464 /* packetsize <= remsize */
1465 /* now handle the received data */
1466 acx_l_process_rxbuf(adev, ptr);
1467 next:
1468 ptr = (rxbuffer_t *) (((char *)ptr) + packetsize);
1469 remsize -= packetsize;
1470 if ((acx_debug & L_USBRXTX) && remsize) {
1471 printk("acx: more than one packet in buffer, "
1472 "second packet hdr:");
1473 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1477 end_unlock:
1478 acx_unlock(adev, flags);
1479 /* end: */
1480 FN_EXIT0;
1484 /***********************************************************************
1485 ** acxusb_i_complete_tx()
1486 ** Inputs:
1487 ** urb -> pointer to USB request block
1488 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1490 ** This function is invoked upon termination of a USB transfer.
1492 void acxusb_i_complete_tx(struct urb *urb)
1494 acx_device_t *adev;
1495 usb_tx_t *tx;
1496 unsigned long flags;
1497 int txnum;
1499 FN_ENTER;
1501 BUG_ON(!urb->context);
1503 tx = (usb_tx_t *) urb->context;
1504 adev = tx->adev;
1506 txnum = tx - adev->usb_tx;
1508 acx_lock(adev, flags);
1511 * If the iface isn't up, we don't have any right
1512 * to play with them. The urb may get unlinked.
1514 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1515 printk("acx: tx: device is down, not doing anything\n");
1516 goto end_unlock;
1519 printk("acx: RETURN TX (%d): status=%d size=%d\n",
1520 txnum, urb->status, urb->actual_length);
1522 /* handle USB transfer errors */
1523 switch (urb->status) {
1524 case 0: /* No error */
1525 break;
1526 case -ESHUTDOWN:
1527 goto end_unlock;
1528 break;
1529 case -ECONNRESET:
1530 goto end_unlock;
1531 break;
1532 /* FIXME: real error-handling code here please */
1533 default:
1534 printk(KERN_ERR "acx: tx error, urb status=%d\n", urb->status);
1535 /* FIXME: real error-handling code here please */
1538 /* free the URB and check for more data */
1539 tx->busy = 0;
1540 adev->tx_free++;
1541 if ((adev->tx_free >= TX_START_QUEUE)
1542 && (adev->status == ACX_STATUS_4_ASSOCIATED)
1543 /* && (acx_queue_stopped(adev->ndev)*/) {
1544 log(L_BUF,
1545 "acx: tx: wake queue (%u free txbufs)\n", adev->tx_free);
1546 /* acx_wake_queue(adev->ndev, NULL); */
1549 end_unlock:
1550 acx_unlock(adev, flags);
1551 /* end: */
1552 FN_EXIT0;
1556 /***************************************************************
1557 ** acxusb_l_alloc_tx
1558 ** Actually returns a usb_tx_t* ptr
1560 tx_t *acxusb_l_alloc_tx(acx_device_t * adev)
1562 usb_tx_t *tx;
1563 unsigned head;
1565 FN_ENTER;
1567 head = adev->tx_head;
1568 do {
1569 head = (head + 1) % ACX_TX_URB_CNT;
1570 if (!adev->usb_tx[head].busy) {
1571 log(L_USBRXTX,
1572 "acx: allocated tx %d\n", head);
1573 tx = &adev->usb_tx[head];
1574 tx->busy = 1;
1575 adev->tx_free--;
1576 /* Keep a few free descs between head and tail of tx ring.
1577 ** It is not absolutely needed, just feels safer */
1578 if (adev->tx_free < TX_STOP_QUEUE) {
1579 log(L_BUF, "acx: tx: stop queue "
1580 "(%u free txbufs)\n", adev->tx_free);
1581 /* acx_stop_queue(adev->ndev, NULL); */
1583 goto end;
1585 } while (likely(head != adev->tx_head));
1586 tx = NULL;
1587 printk_ratelimited("acx: tx buffers full\n");
1588 end:
1589 adev->tx_head = head;
1590 FN_EXIT0;
1591 return (tx_t *) tx;
1595 /***************************************************************
1596 ** Used if alloc_tx()'ed buffer needs to be cancelled without doing tx
1598 void acxusb_l_dealloc_tx(tx_t * tx_opaque)
1600 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1601 tx->busy = 0;
1605 /***************************************************************
1607 void *acxusb_l_get_txbuf(acx_device_t * adev, tx_t * tx_opaque)
1609 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1610 return &tx->bulkout.data;
1614 /***************************************************************
1615 ** acxusb_l_tx_data
1617 ** Can be called from IRQ (rx -> (AP bridging or mgmt response) -> tx).
1618 ** Can be called from acx_i_start_xmit (data frames from net core).
1620 void acxusb_l_tx_data(acx_device_t * adev, tx_t * tx_opaque, int wlanpkt_len, struct ieee80211_tx_control *ctl,
1621 struct sk_buff* skb)
1623 struct usb_device *usbdev;
1624 struct urb *txurb;
1625 usb_tx_t *tx;
1626 usb_txbuffer_t *txbuf;
1627 // client_t *clt;
1628 struct ieee80211_hdr *whdr;
1629 unsigned int outpipe;
1630 int ucode, txnum;
1632 FN_ENTER;
1634 tx = ((usb_tx_t *) tx_opaque);
1635 txurb = tx->urb;
1636 txbuf = &tx->bulkout;
1637 whdr = (struct ieee80211_hdr *) txbuf->data;
1638 txnum = tx - adev->usb_tx;
1640 log(L_DEBUG, "acx: using buf#%d free=%d len=%d\n",
1641 txnum, adev->tx_free, wlanpkt_len);
1643 switch (adev->mode) {
1644 case ACX_MODE_0_ADHOC:
1645 case ACX_MODE_3_AP:
1646 clt = acx_l_sta_list_get(adev, whdr->a1);
1647 break;
1648 case ACX_MODE_2_STA:
1649 // clt = adev->ap_client;
1650 break;
1651 default:
1652 clt = NULL;
1653 break;
1655 if (unlikely(clt && !clt->rate_cur)) {
1656 printk("acx: driver bug! bad ratemask\n");
1657 goto end;
1661 /* fill the USB transfer header */
1662 txbuf->desc = cpu_to_le16(USB_TXBUF_TXDESC);
1663 txbuf->mpdu_len = cpu_to_le16(wlanpkt_len);
1664 txbuf->queue_index = 1;
1665 txbuf->rate = ctl->tx_rate; //clt->rate_100;
1666 // FIXME(); //This used to have | (clt - adev->ap_client)
1667 txbuf->hostdata = (ctl->tx_rate << 16);
1668 txbuf->ctrl1 = DESC_CTL_FIRSTFRAG;
1669 if (1 == adev->preamble_cur)
1670 SET_BIT(txbuf->ctrl1, DESC_CTL_SHORT_PREAMBLE);
1671 txbuf->ctrl2 = 0;
1672 txbuf->data_len = cpu_to_le16(wlanpkt_len);
1674 if (unlikely(acx_debug & L_DATA)) {
1675 printk("acx: dump of bulk out urb:\n");
1676 acx_dump_bytes(txbuf, wlanpkt_len + USB_TXBUF_HDRSIZE);
1679 if (unlikely(txurb->status == -EINPROGRESS)) {
1680 printk
1681 ("acx: trying to submit tx urb while already in progress\n");
1684 /* now schedule the USB transfer */
1685 usbdev = adev->usbdev;
1686 outpipe = usb_sndbulkpipe(usbdev, adev->bulkoutep);
1688 usb_fill_bulk_urb(txurb, usbdev, outpipe, txbuf, /* dataptr */
1689 wlanpkt_len + USB_TXBUF_HDRSIZE, /* size */
1690 acxusb_i_complete_tx, /* handler */
1691 tx /* handler param */
1694 txurb->transfer_flags = URB_ASYNC_UNLINK | URB_ZERO_PACKET;
1695 ucode = usb_submit_urb(txurb, GFP_ATOMIC);
1696 log(L_USBRXTX, "acx: SUBMIT TX (%d): outpipe=0x%X buf=%p txsize=%d "
1697 "rate=%u errcode=%d\n", txnum, outpipe, txbuf,
1698 wlanpkt_len + USB_TXBUF_HDRSIZE, txbuf->rate, ucode);
1700 if (unlikely(ucode)) {
1701 printk(KERN_ERR "acx: submit_urb() error=%d txsize=%d\n",
1702 ucode, wlanpkt_len + USB_TXBUF_HDRSIZE);
1704 /* on error, just mark the frame as done and update
1705 ** the statistics
1707 adev->stats.tx_errors++;
1708 tx->busy = 0;
1709 adev->tx_free++;
1710 /* needed? if (adev->tx_free > TX_START_QUEUE) acx_wake_queue(...) */
1712 FN_EXIT0;
1716 /***********************************************************************
1717 static void acxusb_i_set_rx_mode(struct net_device *ndev)
1723 /***********************************************************************
1725 #ifdef HAVE_TX_TIMEOUT
1727 void acxusb_i_tx_timeout(struct net_device *ndev)
1729 acx_device_t *adev = ndev2adev(ndev);
1730 unsigned long flags;
1731 int i;
1733 FN_ENTER;
1735 acx_lock(adev, flags);
1736 */ /* unlink the URBs */
1737 /* for (i = 0; i < ACX_TX_URB_CNT; i++) {
1738 acxusb_unlink_urb(adev->usb_tx[i].urb);
1739 adev->usb_tx[i].busy = 0;
1741 adev->tx_free = ACX_TX_URB_CNT;
1742 */ /* TODO: stats update */
1743 /* acx_unlock(adev, flags);
1745 FN_EXIT0;
1748 #endif
1751 /***********************************************************************
1752 ** init_module()
1754 ** This function is invoked upon loading of the kernel module.
1755 ** It registers itself at the kernel's USB subsystem.
1757 ** Returns: Errorcode on failure, 0 on success
1759 int __init acxusb_e_init_module(void)
1761 log(L_INIT, "acx: USB module " ACX_RELEASE " initialized, "
1762 "probing for devices...\n");
1763 return usb_register(&acxusb_driver);
1768 /***********************************************************************
1769 ** cleanup_module()
1771 ** This function is invoked as last step of the module unloading. It simply
1772 ** deregisters this module at the kernel's USB subsystem.
1774 void __exit acxusb_e_cleanup_module(void)
1776 usb_deregister(&acxusb_driver);
1777 log(L_INIT, "acx: USB module " ACX_RELEASE " unloaded\n");
1781 /***********************************************************************
1782 ** DEBUG STUFF
1784 #if ACX_DEBUG
1786 #ifdef UNUSED
1787 static void dump_device(struct usb_device *usbdev)
1789 int i;
1790 struct usb_config_descriptor *cd;
1792 printk("acx: device dump:\n");
1793 printk("acx: devnum: %d\n", usbdev->devnum);
1794 printk("acx: speed: %d\n", usbdev->speed);
1795 printk("acx: tt: 0x%X\n", (unsigned int)(usbdev->tt));
1796 printk("acx: ttport: %d\n", (unsigned int)(usbdev->ttport));
1797 printk("acx: toggle[0]: 0x%X toggle[1]: 0x%X\n",
1798 (unsigned int)(usbdev->toggle[0]),
1799 (unsigned int)(usbdev->toggle[1]));
1800 /* This saw a change after 2.6.10 */
1801 printk("acx: ep_in wMaxPacketSize: ");
1802 for (i = 0; i < 16; ++i)
1803 if (usbdev->ep_in[i] != NULL)
1804 printk("%d:%d ", i,
1805 usbdev->ep_in[i]->desc.wMaxPacketSize);
1806 printk("\n");
1807 printk("acx: ep_out wMaxPacketSize: ");
1808 for (i = 0; i < ARRAY_SIZE(usbdev->ep_out); ++i)
1809 if (usbdev->ep_out[i] != NULL)
1810 printk("%d:%d ", i,
1811 usbdev->ep_out[i]->desc.wMaxPacketSize);
1812 printk("\n");
1813 printk("acx: parent: 0x%X\n", (unsigned int)usbdev->parent);
1814 printk("acx: bus: 0x%X\n", (unsigned int)usbdev->bus);
1815 #ifdef NO_DATATYPE
1816 printk(" configs: ");
1817 for (i = 0; i < usbdev->descriptor.bNumConfigurations; i++)
1818 printk("0x%X ", usbdev->config[i]);
1819 printk("\n");
1820 #endif
1821 printk("acx: actconfig: %p\n", usbdev->actconfig);
1822 dump_device_descriptor(&usbdev->descriptor);
1824 cd = &usbdev->config->desc;
1825 dump_config_descriptor(cd);
1829 /***********************************************************************
1831 static void dump_config_descriptor(struct usb_config_descriptor *cd)
1833 printk("acx: Configuration Descriptor:\n");
1834 if (!cd) {
1835 printk("acx: NULL\n");
1836 return;
1838 printk("acx: bLength: %d (0x%X)\n", cd->bLength, cd->bLength);
1839 printk("acx: bDescriptorType: %d (0x%X)\n", cd->bDescriptorType,
1840 cd->bDescriptorType);
1841 printk("acx: bNumInterfaces: %d (0x%X)\n", cd->bNumInterfaces,
1842 cd->bNumInterfaces);
1843 printk("acx: bConfigurationValue: %d (0x%X)\n", cd->bConfigurationValue,
1844 cd->bConfigurationValue);
1845 printk("acx: iConfiguration: %d (0x%X)\n", cd->iConfiguration,
1846 cd->iConfiguration);
1847 printk("acx: bmAttributes: %d (0x%X)\n", cd->bmAttributes,
1848 cd->bmAttributes);
1849 /* printk("acx: MaxPower: %d (0x%X)\n", cd->bMaxPower, cd->bMaxPower); */
1853 static void dump_device_descriptor(struct usb_device_descriptor *dd)
1855 printk("Device Descriptor:\n");
1856 if (!dd) {
1857 printk("NULL\n");
1858 return;
1860 printk("acx: bLength: %d (0x%X)\n", dd->bLength, dd->bLength);
1861 printk("acx: bDescriptortype: %d (0x%X)\n", dd->bDescriptorType,
1862 dd->bDescriptorType);
1863 printk("acx: bcdUSB: %d (0x%X)\n", dd->bcdUSB, dd->bcdUSB);
1864 printk("acx: bDeviceClass: %d (0x%X)\n", dd->bDeviceClass,
1865 dd->bDeviceClass);
1866 printk("acx: bDeviceSubClass: %d (0x%X)\n", dd->bDeviceSubClass,
1867 dd->bDeviceSubClass);
1868 printk("acx: bDeviceProtocol: %d (0x%X)\n", dd->bDeviceProtocol,
1869 dd->bDeviceProtocol);
1870 printk("acx: bMaxPacketSize0: %d (0x%X)\n", dd->bMaxPacketSize0,
1871 dd->bMaxPacketSize0);
1872 printk("acx: idVendor: %d (0x%X)\n", dd->idVendor, dd->idVendor);
1873 printk("acx: idProduct: %d (0x%X)\n", dd->idProduct, dd->idProduct);
1874 printk("acx: bcdDevice: %d (0x%X)\n", dd->bcdDevice, dd->bcdDevice);
1875 printk("acx: iManufacturer: %d (0x%X)\n", dd->iManufacturer,
1876 dd->iManufacturer);
1877 printk("acx: iProduct: %d (0x%X)\n", dd->iProduct, dd->iProduct);
1878 printk("acx: iSerialNumber: %d (0x%X)\n", dd->iSerialNumber,
1879 dd->iSerialNumber);
1880 printk("acx: bNumConfigurations: %d (0x%X)\n", dd->bNumConfigurations,
1881 dd->bNumConfigurations);
1883 #endif /* UNUSED */
1885 #endif /* ACX_DEBUG */