again some little changes (diff between my and matteo's code)
[acx-mac80211.git] / usb.c
blob73c04396f85cfaecb1c11ff2e17b77385248d984
1 /**** (legal) claimer in README
2 ** Copyright (C) 2003 ACX100 Open Source Project
3 */
5 /***********************************************************************
6 ** USB support for TI ACX100 based devices. Many parts are taken from
7 ** the PCI driver.
8 **
9 ** Authors:
10 ** Martin Wawro <martin.wawro AT uni-dortmund.de>
11 ** Andreas Mohr <andi AT lisas.de>
13 ** LOCKING
14 ** callback functions called by USB core are running in interrupt context
15 ** and thus have names with _i_.
17 #define ACX_MAC80211_USB 1
19 #include <linux/version.h>
20 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
21 #include <linux/config.h>
22 #endif
23 #include <linux/types.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/usb.h>
28 #include <linux/netdevice.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/etherdevice.h>
31 #include <linux/wireless.h>
32 #include <net/iw_handler.h>
33 #include <linux/vmalloc.h>
34 #include <linux/ethtool.h>
35 #include <linux/workqueue.h>
37 #include "acx.h"
40 /***********************************************************************
42 /* number of endpoints of an interface */
43 #define NUM_EP(intf) (intf)->altsetting[0].desc.bNumEndpoints
44 #define EP(intf, nr) (intf)->altsetting[0].endpoint[(nr)].desc
45 #define GET_DEV(udev) usb_get_dev((udev))
46 #define PUT_DEV(udev) usb_put_dev((udev))
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
49 /* removed in 2.6.14. We will use fake value for now */
50 #define URB_ASYNC_UNLINK 0
51 #endif
54 /***********************************************************************
56 /* ACX100 (TNETW1100) USB device: D-Link DWL-120+ */
57 #define ACX100_VENDOR_ID 0x2001
58 #define ACX100_PRODUCT_ID_UNBOOTED 0x3B01
59 #define ACX100_PRODUCT_ID_BOOTED 0x3B00
61 /* TNETW1450 USB devices */
62 #define VENDOR_ID_DLINK 0x07b8 /* D-Link Corp. */
63 #define PRODUCT_ID_WUG2400 0xb21a /* AboCom WUG2400 or SafeCom SWLUT-54125 */
64 #define VENDOR_ID_AVM_GMBH 0x057c
65 #define PRODUCT_ID_AVM_WLAN_USB 0x5601
66 #define PRODUCT_ID_AVM_WLAN_USB_si 0x6201 /* "self install" named Version:
67 * driver kills kernel on inbound scans from fritz box ?? */
68 #define VENDOR_ID_ZCOM 0x0cde
69 #define PRODUCT_ID_ZCOM_XG750 0x0017 /* not tested yet */
70 #define VENDOR_ID_TI 0x0451
71 #define PRODUCT_ID_TI_UNKNOWN 0x60c5 /* not tested yet */
73 #define ACX_USB_CTRL_TIMEOUT 5500 /* steps in ms */
75 /* Buffer size for fw upload, same for both ACX100 USB and TNETW1450 */
76 #define USB_RWMEM_MAXLEN 2048
78 /* The number of bulk URBs to use */
79 #define ACX_TX_URB_CNT 8
80 #define ACX_RX_URB_CNT 2
82 /* Should be sent to the bulkout endpoint */
83 #define ACX_USB_REQ_UPLOAD_FW 0x10
84 #define ACX_USB_REQ_ACK_CS 0x11
85 #define ACX_USB_REQ_CMD 0x12
87 /***********************************************************************
88 ** Prototypes
90 static int acxusb_e_probe(struct usb_interface *, const struct usb_device_id *);
91 static void acxusb_e_disconnect(struct usb_interface *);
92 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
93 static void acxusb_i_complete_tx(struct urb *);
94 static void acxusb_i_complete_rx(struct urb *);
95 #else
96 static void acxusb_i_complete_tx(struct urb *, struct pt_regs *);
97 static void acxusb_i_complete_rx(struct urb *, struct pt_regs *);
98 #endif
99 static int acxusb_e_open(struct ieee80211_hw *);
100 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
101 static int acxusb_e_close(struct ieee80211_hw *);
102 #else
103 static void acxusb_e_close(struct ieee80211_hw *);
104 #endif
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 void acxusb_put_devname(acx_device_t *adev, struct ethtool_drvinfo *info)
153 usb_make_path(adev->usbdev, info->bus_info, sizeof info->bus_info);
155 /***********************************************************************
156 ** USB helper
158 ** ldd3 ch13 says:
159 ** When the function is usb_kill_urb, the urb lifecycle is stopped. This
160 ** function is usually used when the device is disconnected from the system,
161 ** in the disconnect callback. For some drivers, the usb_unlink_urb function
162 ** should be used to tell the USB core to stop an urb. This function does not
163 ** wait for the urb to be fully stopped before returning to the caller.
164 ** This is useful for stoppingthe urb while in an interrupt handler or when
165 ** a spinlock is held, as waiting for a urb to fully stop requires the ability
166 ** for the USB core to put the calling process to sleep. This function requires
167 ** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
168 ** to be stopped in order to work properly.
170 ** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
171 ** asynchronous while usb_kill_urb is synchronous and should be called
172 ** directly (drivers/usb/core/urb.c))
174 ** In light of this, timeout is just for paranoid reasons...
176 ** Actually, it's useful for debugging. If we reach timeout, we're doing
177 ** something wrong with the urbs.
179 static void acxusb_unlink_urb(struct urb *urb)
181 if (!urb)
182 return;
184 if (urb->status == -EINPROGRESS) {
185 int timeout = 10;
187 usb_unlink_urb(urb);
188 while (--timeout && urb->status == -EINPROGRESS) {
189 mdelay(1);
191 if (!timeout) {
192 printk(KERN_ERR "acx_usb: urb unlink timeout!\n");
198 /***********************************************************************
199 ** EEPROM and PHY read/write helpers
201 /***********************************************************************
202 ** acxusb_s_read_phy_reg
204 int acxusb_s_read_phy_reg(acx_device_t * adev, u32 reg, u8 * charbuf)
206 /* mem_read_write_t mem; */
208 FN_ENTER;
210 printk("%s doesn't seem to work yet, disabled.\n", __func__);
213 mem.addr = cpu_to_le16(reg);
214 mem.type = cpu_to_le16(0x82);
215 mem.len = cpu_to_le32(4);
216 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
217 *charbuf = mem.data;
218 log(L_DEBUG, "read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
221 FN_EXIT1(OK);
222 return OK;
226 /***********************************************************************
228 int acxusb_s_write_phy_reg(acx_device_t * adev, u32 reg, u8 value)
230 mem_read_write_t mem;
232 FN_ENTER;
234 mem.addr = cpu_to_le16(reg);
235 mem.type = cpu_to_le16(0x82);
236 mem.len = cpu_to_le32(4);
237 mem.data = value;
238 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
239 log(L_DEBUG, "write radio PHY[0x%04X]=0x%02X\n", reg, value);
241 FN_EXIT1(OK);
242 return OK;
246 /***********************************************************************
247 ** acxusb_s_issue_cmd_timeo
248 ** Excecutes a command in the command mailbox
250 ** buffer = a pointer to the data.
251 ** The data must not include 4 byte command header
254 /* TODO: ideally we shall always know how much we need
255 ** and this shall be 0 */
256 #define BOGUS_SAFETY_PADDING 0x40
258 #undef FUNC
259 #define FUNC "issue_cmd"
261 #if !ACX_DEBUG
263 acxusb_s_issue_cmd_timeo(acx_device_t * adev,
264 unsigned cmd,
265 void *buffer, unsigned buflen, unsigned timeout)
267 #else
269 acxusb_s_issue_cmd_timeo_debug(acx_device_t * adev,
270 unsigned cmd,
271 void *buffer,
272 unsigned buflen,
273 unsigned timeout, const char *cmdstr)
275 #endif
276 /* USB ignores timeout param */
278 struct usb_device *usbdev;
279 struct {
280 u16 cmd;
281 u16 status;
282 u8 data[1];
283 } ACX_PACKED *loc;
284 const char *devname;
285 int acklen, blocklen, inpipe, outpipe;
286 int cmd_status;
287 int result;
289 FN_ENTER;
291 devname = wiphy_name(adev->ieee->wiphy);
292 /* no "wlan%%d: ..." please */
293 if (!devname || !devname[0] || devname[4] == '%')
294 devname = "acx";
296 log(L_CTL, FUNC "(cmd:%s,buflen:%u,type:0x%04X)\n",
297 cmdstr, buflen,
298 buffer ? le16_to_cpu(((acx_ie_generic_t *) buffer)->type) : -1);
300 loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
301 if (!loc) {
302 printk("%s: " FUNC "(): no memory for data buffer\n", devname);
303 goto bad;
306 /* get context from acx_device */
307 usbdev = adev->usbdev;
309 /* check which kind of command was issued */
310 loc->cmd = cpu_to_le16(cmd);
311 loc->status = 0;
313 /* NB: buflen == frmlen + 4
315 ** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
316 ** read (cmd,status,rid,frmlen,data[frmlen]) back
318 ** Configure: write (cmd,status,rid,frmlen,data[frmlen])
320 ** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
323 /* now write the parameters of the command if needed */
324 acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
325 blocklen = buflen;
326 if (buffer && buflen) {
327 /* if it's an INTERROGATE command, just pass the length
328 * of parameters to read, as data */
329 if (cmd == ACX1xx_CMD_INTERROGATE) {
330 blocklen = 4;
331 acklen = buflen + 4;
333 memcpy(loc->data, buffer, blocklen);
335 blocklen += 4; /* account for cmd,status */
337 /* obtain the I/O pipes */
338 outpipe = usb_sndctrlpipe(usbdev, 0);
339 inpipe = usb_rcvctrlpipe(usbdev, 0);
340 log(L_CTL, "ctrl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
341 log(L_CTL, "sending USB control msg (out) (blocklen=%d)\n", blocklen);
342 if (acx_debug & L_DATA)
343 acx_dump_bytes(loc, blocklen);
345 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_CMD, /* request */
346 USB_TYPE_VENDOR | USB_DIR_OUT, /* requesttype */
347 0, /* value */
348 0, /* index */
349 loc, /* dataptr */
350 blocklen, /* size */
351 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
354 if (result == -ENODEV) {
355 log(L_CTL, "no device present (unplug?)\n");
356 goto good;
359 log(L_CTL, "wrote %d bytes\n", result);
360 if (result < 0) {
361 goto bad;
364 /* check for device acknowledge */
365 log(L_CTL, "sending USB control msg (in) (acklen=%d)\n", acklen);
366 loc->status = 0; /* delete old status flag -> set to IDLE */
367 /* shall we zero out the rest? */
368 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_CMD, /* request */
369 USB_TYPE_VENDOR | USB_DIR_IN, /* requesttype */
370 0, /* value */
371 0, /* index */
372 loc, /* dataptr */
373 acklen, /* size */
374 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
376 if (result < 0) {
377 printk("%s: " FUNC "(): USB read error %d\n", devname, result);
378 goto bad;
380 if (acx_debug & L_CTL) {
381 printk("read %d bytes: ", result);
382 acx_dump_bytes(loc, result);
386 check for result==buflen+4? Was seen:
388 interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
389 issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
390 ctrl inpipe=0x80000280 outpipe=0x80000200
391 sending USB control msg (out) (blocklen=8)
392 01 00 00 00 0F 10 04 00
393 wrote 8 bytes
394 sending USB control msg (in) (acklen=12) sizeof(loc->data
395 read 4 bytes <==== MUST BE 12!!
398 cmd_status = le16_to_cpu(loc->status);
399 if (cmd_status != 1) {
400 printk("%s: " FUNC "(): cmd_status is not SUCCESS: %d (%s)\n",
401 devname, cmd_status, acx_cmd_status_str(cmd_status));
402 /* TODO: goto bad; ? */
404 if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
405 memcpy(buffer, loc->data, buflen);
406 log(L_CTL, "response frame: cmd=0x%04X status=%d\n",
407 le16_to_cpu(loc->cmd), cmd_status);
409 good:
410 kfree(loc);
411 FN_EXIT1(OK);
412 return OK;
413 bad:
414 /* Give enough info so that callers can avoid
415 ** printing their own diagnostic messages */
416 #if ACX_DEBUG
417 printk("%s: " FUNC "(cmd:%s) FAILED\n", devname, cmdstr);
418 #else
419 printk("%s: " FUNC "(cmd:0x%04X) FAILED\n", devname, cmd);
420 #endif
421 dump_stack();
422 kfree(loc);
423 FN_EXIT1(NOT_OK);
424 return NOT_OK;
428 /***********************************************************************
429 ** acxusb_boot()
430 ** Inputs:
431 ** usbdev -> Pointer to kernel's usb_device structure
433 ** Returns:
434 ** (int) Errorcode or 0 on success
436 ** This function triggers the loading of the firmware image from harddisk
437 ** and then uploads the firmware to the USB device. After uploading the
438 ** firmware and transmitting the checksum, the device resets and appears
439 ** as a new device on the USB bus (the device we can finally deal with)
441 static inline int
442 acxusb_fw_needs_padding(firmware_image_t *fw_image, unsigned int usb_maxlen)
444 unsigned int num_xfers = ((fw_image->size - 1) / usb_maxlen) + 1;
446 return ((num_xfers % 2) == 0);
449 static int
450 acxusb_boot(struct usb_device *usbdev, int is_tnetw1450, int *radio_type)
452 char filename[sizeof("tiacx1NNusbcRR")];
454 firmware_image_t *fw_image = NULL;
455 char *usbbuf;
456 unsigned int offset;
457 unsigned int blk_len, inpipe, outpipe;
458 u32 num_processed;
459 u32 img_checksum, sum;
460 u32 file_size;
461 int result = -EIO;
462 int i;
464 FN_ENTER;
466 /* dump_device(usbdev); */
468 usbbuf = kmalloc(USB_RWMEM_MAXLEN, GFP_KERNEL);
469 if (!usbbuf) {
470 printk(KERN_ERR
471 "acx: no memory for USB transfer buffer (%d bytes)\n",
472 USB_RWMEM_MAXLEN);
473 result = -ENOMEM;
474 goto end;
476 if (is_tnetw1450) {
477 /* Obtain the I/O pipes */
478 outpipe = usb_sndbulkpipe(usbdev, 1);
479 inpipe = usb_rcvbulkpipe(usbdev, 2);
481 printk(KERN_DEBUG "wait for device ready\n");
482 for (i = 0; i <= 2; i++) {
483 result = usb_bulk_msg(usbdev, inpipe,
484 usbbuf,
485 USB_RWMEM_MAXLEN,
486 &num_processed, 2000);
488 if ((*(u32 *) & usbbuf[4] == 0x40000001)
489 && (*(u16 *) & usbbuf[2] == 0x1)
490 && ((*(u16 *) usbbuf & 0x3fff) == 0)
491 && ((*(u16 *) usbbuf & 0xc000) == 0xc000))
492 break;
493 acx_s_mwait(10);
495 if (i == 2)
496 goto fw_end;
498 *radio_type = usbbuf[8];
499 } else {
500 /* Obtain the I/O pipes */
501 outpipe = usb_sndctrlpipe(usbdev, 0);
502 inpipe = usb_rcvctrlpipe(usbdev, 0);
504 /* FIXME: shouldn't be hardcoded */
505 *radio_type = RADIO_MAXIM_0D;
508 snprintf(filename, sizeof(filename), "tiacx1%02dusbc%02X",
509 is_tnetw1450 * 11, *radio_type);
511 fw_image = acx_s_read_fw(&usbdev->dev, filename, &file_size);
512 if (!fw_image) {
513 result = -EIO;
514 goto end;
516 log(L_INIT, "firmware size: %d bytes\n", file_size);
518 img_checksum = le32_to_cpu(fw_image->chksum);
520 if (is_tnetw1450) {
521 u8 cmdbuf[20];
522 const u8 *p;
523 u8 need_padding;
524 u32 tmplen, val;
526 memset(cmdbuf, 0, 16);
528 need_padding =
529 acxusb_fw_needs_padding(fw_image, USB_RWMEM_MAXLEN);
530 tmplen = need_padding ? file_size - 4 : file_size - 8;
531 *(u16 *) & cmdbuf[0] = 0xc000;
532 *(u16 *) & cmdbuf[2] = 0x000b;
533 *(u32 *) & cmdbuf[4] = tmplen;
534 *(u32 *) & cmdbuf[8] = file_size - 8;
535 *(u32 *) & cmdbuf[12] = img_checksum;
537 result =
538 usb_bulk_msg(usbdev, outpipe, cmdbuf, 16, &num_processed,
539 HZ);
540 if (result < 0)
541 goto fw_end;
543 p = (const u8 *)&fw_image->size;
545 /* first calculate checksum for image size part */
546 sum = p[0] + p[1] + p[2] + p[3];
547 p += 4;
549 /* now continue checksum for firmware data part */
550 tmplen = le32_to_cpu(fw_image->size);
551 for (i = 0; i < tmplen /* image size */ ; i++) {
552 sum += *p++;
555 if (sum != le32_to_cpu(fw_image->chksum)) {
556 printk("acx: FATAL: firmware upload: "
557 "checksums don't match! "
558 "(0x%08x vs. 0x%08x)\n", sum, fw_image->chksum);
559 goto fw_end;
562 offset = 8;
563 while (offset < file_size) {
564 blk_len = file_size - offset;
565 if (blk_len > USB_RWMEM_MAXLEN) {
566 blk_len = USB_RWMEM_MAXLEN;
569 log(L_INIT,
570 "uploading firmware (%d bytes, offset=%d)\n",
571 blk_len, offset);
572 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
574 p = usbbuf;
575 for (i = 0; i < blk_len; i += 4) {
576 *(u32 *) p = be32_to_cpu(*(u32 *) p);
577 p += 4;
580 result =
581 usb_bulk_msg(usbdev, outpipe, usbbuf, blk_len,
582 &num_processed, HZ);
583 if ((result < 0) || (num_processed != blk_len))
584 goto fw_end;
585 offset += blk_len;
587 if (need_padding) {
588 printk(KERN_DEBUG "send padding\n");
589 memset(usbbuf, 0, 4);
590 result =
591 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
592 &num_processed, HZ);
593 if ((result < 0) || (num_processed != 4))
594 goto fw_end;
596 printk(KERN_DEBUG "read firmware upload result\n");
597 memset(cmdbuf, 0, 20); /* additional memset */
598 result =
599 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed,
600 2000);
601 if (result < 0)
602 goto fw_end;
603 if (*(u32 *) & cmdbuf[4] == 0x40000003)
604 goto fw_end;
605 if (*(u32 *) & cmdbuf[4])
606 goto fw_end;
607 if (*(u16 *) & cmdbuf[16] != 1)
608 goto fw_end;
610 val = *(u32 *) & cmdbuf[0];
611 if ((val & 0x3fff)
612 || ((val & 0xc000) != 0xc000))
613 goto fw_end;
615 val = *(u32 *) & cmdbuf[8];
616 if (val & 2) {
617 result =
618 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20,
619 &num_processed, 2000);
620 if (result < 0)
621 goto fw_end;
622 val = *(u32 *) & cmdbuf[8];
624 /* yup, no "else" here! */
625 if (val & 1) {
626 memset(usbbuf, 0, 4);
627 result =
628 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
629 &num_processed, HZ);
630 if ((result < 0) || (!num_processed))
631 goto fw_end;
634 printk("TNETW1450 firmware upload successful!\n");
635 result = 0;
636 goto end;
637 fw_end:
638 result = -EIO;
639 goto end;
640 } else {
641 /* ACX100 USB */
643 /* now upload the firmware, slice the data into blocks */
644 offset = 8;
645 while (offset < file_size) {
646 blk_len = file_size - offset;
647 if (blk_len > USB_RWMEM_MAXLEN) {
648 blk_len = USB_RWMEM_MAXLEN;
650 log(L_INIT,
651 "uploading firmware (%d bytes, offset=%d)\n",
652 blk_len, offset);
653 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
654 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, (file_size - 8) & 0xffff, /* value */
655 (file_size - 8) >> 16, /* index */
656 usbbuf, /* dataptr */
657 blk_len, /* size */
658 3000 /* timeout in ms */
660 offset += blk_len;
661 if (result < 0) {
662 printk(KERN_ERR "acx: error %d during upload "
663 "of firmware, aborting\n", result);
664 goto end;
668 /* finally, send the checksum and reboot the device */
669 /* does this trigger the reboot? */
670 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, img_checksum & 0xffff, /* value */
671 img_checksum >> 16, /* index */
672 NULL, /* dataptr */
673 0, /* size */
674 3000 /* timeout in ms */
676 if (result < 0) {
677 printk(KERN_ERR "acx: error %d during tx of checksum, "
678 "aborting\n", result);
679 goto end;
681 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_ACK_CS, USB_TYPE_VENDOR | USB_DIR_IN, img_checksum & 0xffff, /* value */
682 img_checksum >> 16, /* index */
683 usbbuf, /* dataptr */
684 8, /* size */
685 3000 /* timeout in ms */
687 if (result < 0) {
688 printk(KERN_ERR "acx: error %d during ACK of checksum, "
689 "aborting\n", result);
690 goto end;
692 if (*usbbuf != 0x10) {
693 printk(KERN_ERR "acx: invalid checksum?\n");
694 result = -EINVAL;
695 goto end;
697 result = 0;
700 end:
701 vfree(fw_image);
702 kfree(usbbuf);
704 FN_EXIT1(result);
705 return result;
709 /* FIXME: maybe merge it with usual eeprom reading, into common code? */
710 static void acxusb_s_read_eeprom_version(acx_device_t * adev)
712 u8 eeprom_ver[0x8];
714 memset(eeprom_ver, 0, sizeof(eeprom_ver));
715 acx_s_interrogate(adev, &eeprom_ver, ACX1FF_IE_EEPROM_VER);
717 /* FIXME: which one of those values to take? */
718 adev->eeprom_version = eeprom_ver[5];
723 * temporary helper function to at least fill important cfgopt members with
724 * useful replacement values until we figure out how one manages to fetch
725 * the configoption struct in the USB device case...
727 static int acxusb_s_fill_configoption(acx_device_t * adev)
729 adev->cfgopt_probe_delay = 200;
730 adev->cfgopt_dot11CCAModes = 4;
731 adev->cfgopt_dot11Diversity = 1;
732 adev->cfgopt_dot11ShortPreambleOption = 1;
733 adev->cfgopt_dot11PBCCOption = 1;
734 adev->cfgopt_dot11ChannelAgility = 0;
735 adev->cfgopt_dot11PhyType = 5;
736 adev->cfgopt_dot11TempType = 1;
737 return OK;
740 static const struct ieee80211_ops acxusb_hw_ops = {
741 .tx = acx_i_start_xmit,
742 .conf_tx = acx_net_conf_tx,
743 .add_interface = acx_add_interface,
744 .remove_interface = acx_remove_interface,
745 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
746 .open = acxusb_e_open,
747 .reset = acx_net_reset,
748 .set_multicast_list = acx_i_set_multicast_list,
749 #else
750 .start = acxusb_e_open,
751 .configure_filter = acx_i_set_multicast_list,
752 #endif
753 .stop = acxusb_e_close,
754 .config = acx_net_config,
755 .config_interface = acx_config_interface,
756 .set_key = acx_net_set_key,
757 .get_stats = acx_e_get_stats,
758 .get_tx_stats = acx_net_get_tx_stats,
761 /***********************************************************************
762 ** acxusb_e_probe()
764 ** This function is invoked by the kernel's USB core whenever a new device is
765 ** attached to the system or the module is loaded. It is presented a usb_device
766 ** structure from which information regarding the device is obtained and evaluated.
767 ** In case this driver is able to handle one of the offered devices, it returns
768 ** a non-null pointer to a driver context and thereby claims the device.
772 static int
773 acxusb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
775 struct usb_device *usbdev = interface_to_usbdev(intf);
776 acx_device_t *adev = NULL;
777 struct usb_config_descriptor *config;
778 struct usb_endpoint_descriptor *epdesc;
779 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
780 struct usb_host_endpoint *ep;
781 #endif
782 struct usb_interface_descriptor *ifdesc;
783 const char *msg;
784 int numconfigs, numfaces, numep;
785 int result = OK;
786 int i;
787 int radio_type;
788 /* this one needs to be more precise in case there appears
789 * a TNETW1450 from the same vendor */
790 int is_tnetw1450 = (usbdev->descriptor.idVendor != ACX100_VENDOR_ID);
791 struct ieee80211_hw *ieee;
793 FN_ENTER;
795 if (is_tnetw1450) {
796 /* Boot the device (i.e. upload the firmware) */
797 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
799 /* TNETW1450-based cards will continue right away with
800 * the same USB ID after booting */
801 } else {
802 /* First check if this is the "unbooted" hardware */
803 if (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED) {
805 /* Boot the device (i.e. upload the firmware) */
806 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
808 /* DWL-120+ will first boot the firmware,
809 * then later have a *separate* probe() run
810 * since its USB ID will have changed after
811 * firmware boot!
812 * Since the first probe() run has no
813 * other purpose than booting the firmware,
814 * simply return immediately.
816 log(L_INIT,
817 "finished booting, returning from probe()\n");
818 result = OK; /* success */
819 goto end;
820 } else {
821 if (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)
822 /* device not unbooted, but invalid USB ID!? */
823 goto end_nodev;
827 /* Ok, so it's our device and it has already booted */
829 /* Allocate memory for a network device */
831 ieee = ieee80211_alloc_hw(sizeof(*adev), &acxusb_hw_ops);
832 if (!ieee) {
833 msg = "acx: no memory for ieee80211_dev\n";
834 goto end_nomem;
838 ieee->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
839 /* TODO: mainline doesn't support the following flags yet */
841 ~IEEE80211_HW_MONITOR_DURING_OPER &
842 ~IEEE80211_HW_WEP_INCLUDE_IV;
844 ieee->queues = 1;
846 /* Register the callbacks for the network device functions */
849 /* Setup private driver context */
851 adev = ieee2adev(ieee);
852 adev->ieee = ieee;
854 adev->dev_type = DEVTYPE_USB;
855 adev->radio_type = radio_type;
856 if (is_tnetw1450) {
857 /* well, actually it's a TNETW1450, but since it
858 * seems to be sufficiently similar to TNETW1130,
859 * I don't want to change large amounts of code now */
860 adev->chip_type = CHIPTYPE_ACX111;
861 } else {
862 adev->chip_type = CHIPTYPE_ACX100;
865 adev->usbdev = usbdev;
866 spin_lock_init(&adev->lock); /* initial state: unlocked */
867 mutex_init(&adev->mutex);
869 /* Check that this is really the hardware we know about.
870 ** If not sure, at least notify the user that he
871 ** may be in trouble...
873 numconfigs = (int)usbdev->descriptor.bNumConfigurations;
874 if (numconfigs != 1)
875 printk("acx: number of configurations is %d, "
876 "this driver only knows how to handle 1, "
877 "be prepared for surprises\n", numconfigs);
879 config = &usbdev->config->desc;
880 numfaces = config->bNumInterfaces;
881 if (numfaces != 1)
882 printk("acx: number of interfaces is %d, "
883 "this driver only knows how to handle 1, "
884 "be prepared for surprises\n", numfaces);
886 ifdesc = &intf->altsetting->desc;
887 numep = ifdesc->bNumEndpoints;
888 log(L_DEBUG, "# of endpoints: %d\n", numep);
890 if (is_tnetw1450) {
891 adev->bulkoutep = 1;
892 adev->bulkinep = 2;
893 } else {
894 /* obtain information about the endpoint
895 ** addresses, begin with some default values
897 adev->bulkoutep = 1;
898 adev->bulkinep = 1;
899 for (i = 0; i < numep; i++) {
900 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
901 ep = usbdev->ep_in[i];
902 if (!ep)
903 continue;
904 epdesc = &ep->desc;
905 #else
906 epdesc = usb_epnum_to_ep_desc(usbdev, i);
907 if (!epdesc)
908 continue;
909 #endif
910 if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
911 if (epdesc->bEndpointAddress & 0x80)
912 adev->bulkinep =
913 epdesc->bEndpointAddress & 0xF;
914 else
915 adev->bulkoutep =
916 epdesc->bEndpointAddress & 0xF;
920 log(L_DEBUG, "bulkout ep: 0x%X\n", adev->bulkoutep);
921 log(L_DEBUG, "bulkin ep: 0x%X\n", adev->bulkinep);
923 /* already done by memset: adev->rxtruncsize = 0; */
924 log(L_DEBUG, "TXBUFSIZE=%d RXBUFSIZE=%d\n",
925 (int)TXBUFSIZE, (int)RXBUFSIZE);
927 /* Allocate the RX/TX containers. */
928 adev->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX_TX_URB_CNT, GFP_KERNEL);
929 if (!adev->usb_tx) {
930 msg = "acx: no memory for tx container";
931 goto end_nomem;
933 adev->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX_RX_URB_CNT, GFP_KERNEL);
934 if (!adev->usb_rx) {
935 msg = "acx: no memory for rx container";
936 goto end_nomem;
939 /* Setup URBs for bulk-in/out messages */
940 for (i = 0; i < ACX_RX_URB_CNT; i++) {
941 adev->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
942 if (!adev->usb_rx[i].urb) {
943 msg = "acx: no memory for input URB\n";
944 goto end_nomem;
946 adev->usb_rx[i].urb->status = 0;
947 adev->usb_rx[i].adev = adev;
948 adev->usb_rx[i].busy = 0;
951 for (i = 0; i < ACX_TX_URB_CNT; i++) {
952 adev->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
953 if (!adev->usb_tx[i].urb) {
954 msg = "acx: no memory for output URB\n";
955 goto end_nomem;
957 adev->usb_tx[i].urb->status = 0;
958 adev->usb_tx[i].adev = adev;
959 adev->usb_tx[i].busy = 0;
961 adev->tx_free = ACX_TX_URB_CNT;
963 usb_set_intfdata(intf, adev);
964 SET_IEEE80211_DEV(ieee, &intf->dev);
966 /* TODO: move all of fw cmds to open()? But then we won't know our MAC addr
967 until ifup (it's available via reading ACX1xx_IE_DOT11_STATION_ID)... */
969 /* put acx out of sleep mode and initialize it */
970 acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
972 result = acx_s_init_mac(adev);
973 if (result)
974 goto end;
976 /* TODO: see similar code in pci.c */
977 acxusb_s_read_eeprom_version(adev);
978 acxusb_s_fill_configoption(adev);
979 acx_s_set_defaults(adev);
980 acx_s_get_firmware_version(adev);
981 acx_display_hardware_details(adev);
983 /* MAC_COPY(ndev->dev_addr, adev->dev_addr); */
985 /* Register the network device */
986 log(L_INIT, "registering network device\n");
987 result = ieee80211_register_hw(adev->ieee);
988 if (result) {
989 msg = "acx: failed to register USB network device "
990 "(error %d)\n";
991 goto end_nomem;
994 acx_proc_register_entries(ieee);
997 printk("acx: USB module " ACX_RELEASE " loaded successfully\n");
999 acx_init_task_scheduler(adev);
1001 #if CMD_DISCOVERY
1002 great_inquisitor(adev);
1003 #endif
1005 /* Everything went OK, we are happy now */
1006 result = OK;
1007 goto end;
1009 end_nomem:
1010 printk(msg, result);
1012 if (ieee) {
1013 if (adev->usb_rx) {
1014 for (i = 0; i < ACX_RX_URB_CNT; i++)
1015 usb_free_urb(adev->usb_rx[i].urb);
1016 kfree(adev->usb_rx);
1018 if (adev->usb_tx) {
1019 for (i = 0; i < ACX_TX_URB_CNT; i++)
1020 usb_free_urb(adev->usb_tx[i].urb);
1021 kfree(adev->usb_tx);
1023 ieee80211_free_hw(ieee);
1026 result = -ENOMEM;
1027 goto end;
1029 end_nodev:
1030 /* no device we could handle, return error. */
1031 result = -EIO;
1033 end:
1034 FN_EXIT1(result);
1035 return result;
1039 /***********************************************************************
1040 ** acxusb_e_disconnect()
1042 ** This function is invoked whenever the user pulls the plug from the USB
1043 ** device or the module is removed from the kernel. In these cases, the
1044 ** network devices have to be taken down and all allocated memory has
1045 ** to be freed.
1047 void acxusb_e_disconnect(struct usb_interface *intf)
1049 unsigned long flags;
1050 int i;
1051 acx_device_t *adev = usb_get_intfdata(intf);
1053 FN_ENTER;
1055 /* No WLAN device... no sense */
1056 if (!adev)
1057 goto end;
1059 /* Unregister network device
1061 * If the interface is up, unregister_netdev() will take
1062 * care of calling our close() function, which takes
1063 * care of unlinking the urbs, sending the device to
1064 * sleep, etc...
1065 * This can't be called with sem or lock held because
1066 * _close() will try to grab it as well if it's called,
1067 * deadlocking the machine.
1069 acx_proc_unregister_entries(adev->ieee);
1070 ieee80211_unregister_hw(adev->ieee);
1072 acx_sem_lock(adev);
1073 acx_lock(adev, flags);
1074 /* This device exists no more */
1075 usb_set_intfdata(intf, NULL);
1078 * Here we only free them. _close() took care of
1079 * unlinking them.
1081 for (i = 0; i < ACX_RX_URB_CNT; ++i) {
1082 usb_free_urb(adev->usb_rx[i].urb);
1084 for (i = 0; i < ACX_TX_URB_CNT; ++i) {
1085 usb_free_urb(adev->usb_tx[i].urb);
1088 /* Freeing containers */
1089 kfree(adev->usb_rx);
1090 kfree(adev->usb_tx);
1092 acx_unlock(adev, flags);
1093 acx_sem_unlock(adev);
1095 ieee80211_free_hw(adev->ieee);
1096 end:
1097 FN_EXIT0;
1100 /***********************************************************************
1101 ** acxusb_e_open()
1102 ** This function is called when the user sets up the network interface.
1103 ** It initializes a management timer, sets up the USB card and starts
1104 ** the network tx queue and USB receive.
1106 int acxusb_e_open(struct ieee80211_hw *hw)
1108 acx_device_t *adev = ieee2adev(hw);
1109 unsigned long flags;
1110 int i;
1112 FN_ENTER;
1114 acx_sem_lock(adev);
1116 /* put the ACX100 out of sleep mode */
1117 // acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
1119 init_timer(&adev->mgmt_timer);
1120 adev->mgmt_timer.function = acx_i_timer;
1121 adev->mgmt_timer.data = (unsigned long)adev;
1123 /* acx_s_start needs it */
1124 SET_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1125 acx_s_start(adev);
1127 /* don't acx_start_queue() here, we need to associate first */
1129 acx_lock(adev, flags);
1130 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1131 adev->usb_rx[i].urb->status = 0;
1134 acxusb_l_poll_rx(adev, &adev->usb_rx[0]);
1136 acx_setup_modes(adev);
1137 ieee80211_start_queues(adev->ieee);
1138 acx_unlock(adev, flags);
1140 acx_sem_unlock(adev);
1142 FN_EXIT0;
1143 return 0;
1147 /***********************************************************************
1148 ** acxusb_e_close()
1150 ** This function stops the network functionality of the interface (invoked
1151 ** when the user calls ifconfig <wlan> down). The tx queue is halted and
1152 ** the device is marked as down. In case there were any pending USB bulk
1153 ** transfers, these are unlinked (asynchronously). The module in-use count
1154 ** is also decreased in this function.
1156 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1157 int acxusb_e_close(struct ieee80211_hw *hw)
1158 #else
1159 static void acxusb_e_close(struct ieee80211_hw *hw)
1160 #endif
1162 acx_device_t *adev = ieee2adev(hw);
1163 unsigned long flags;
1164 int i;
1166 FN_ENTER;
1168 acx_sem_lock(adev);
1169 if (adev->dev_state_mask & ACX_STATE_IFACE_UP)
1171 // acxusb_e_down(adev);
1172 CLEAR_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1175 /* Code below is remarkably similar to acxpci_s_down(). Maybe we can merge them? */
1177 acx_free_modes(adev);
1179 /* Make sure we don't get any more rx requests */
1180 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_RX, NULL, 0);
1181 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_TX, NULL, 0);
1184 * We must do FLUSH *without* holding sem to avoid a deadlock.
1185 * See pci.c:acxpci_s_down() for deails.
1187 acx_sem_unlock(adev);
1188 flush_scheduled_work();
1189 acx_sem_lock(adev);
1191 /* Power down the device */
1192 acx_s_issue_cmd(adev, ACX1xx_CMD_SLEEP, NULL, 0);
1194 /* Stop the transmit queue, mark the device as DOWN */
1195 acx_lock(adev, flags);
1196 // acx_stop_queue(ndev, "on ifdown");
1197 // acx_set_status(adev, ACX_STATUS_0_STOPPED);
1198 /* stop pending rx/tx urb transfers */
1199 for (i = 0; i < ACX_TX_URB_CNT; i++) {
1200 acxusb_unlink_urb(adev->usb_tx[i].urb);
1201 adev->usb_tx[i].busy = 0;
1203 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1204 acxusb_unlink_urb(adev->usb_rx[i].urb);
1205 adev->usb_rx[i].busy = 0;
1207 adev->tx_free = ACX_TX_URB_CNT;
1208 acx_unlock(adev, flags);
1210 /* Must do this outside of lock */
1211 del_timer_sync(&adev->mgmt_timer);
1213 acx_sem_unlock(adev);
1215 FN_EXIT0;
1216 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1217 return 0;
1218 #endif
1222 /***********************************************************************
1223 ** acxusb_l_poll_rx
1224 ** This function (re)initiates a bulk-in USB transfer on a given urb
1226 void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx)
1228 struct usb_device *usbdev;
1229 struct urb *rxurb;
1230 int errcode, rxnum;
1231 unsigned int inpipe;
1233 FN_ENTER;
1235 rxurb = rx->urb;
1236 usbdev = adev->usbdev;
1238 rxnum = rx - adev->usb_rx;
1240 inpipe = usb_rcvbulkpipe(usbdev, adev->bulkinep);
1241 if (unlikely(rxurb->status == -EINPROGRESS)) {
1242 printk(KERN_ERR
1243 "acx: error, rx triggered while rx urb in progress\n");
1244 /* FIXME: this is nasty, receive is being cancelled by this code
1245 * on the other hand, this should not happen anyway...
1247 usb_unlink_urb(rxurb);
1248 } else if (unlikely(rxurb->status == -ECONNRESET)) {
1249 log(L_USBRXTX, "acx_usb: _poll_rx: connection reset\n");
1250 goto end;
1252 rxurb->actual_length = 0;
1253 usb_fill_bulk_urb(rxurb, usbdev, inpipe, &rx->bulkin, /* dataptr */
1254 RXBUFSIZE, /* size */
1255 acxusb_i_complete_rx, /* handler */
1256 rx /* handler param */
1258 rxurb->transfer_flags = URB_ASYNC_UNLINK;
1260 /* ATOMIC: we may be called from complete_rx() usb callback */
1261 errcode = usb_submit_urb(rxurb, GFP_ATOMIC);
1262 /* FIXME: evaluate the error code! */
1263 log(L_USBRXTX, "SUBMIT RX (%d) inpipe=0x%X size=%d errcode=%d\n",
1264 rxnum, inpipe, (int)RXBUFSIZE, errcode);
1265 end:
1266 FN_EXIT0;
1270 /***********************************************************************
1271 ** acxusb_i_complete_rx()
1272 ** Inputs:
1273 ** urb -> pointer to USB request block
1274 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1276 ** This function is invoked by USB subsystem whenever a bulk receive
1277 ** request returns.
1278 ** The received data is then committed to the network stack and the next
1279 ** USB receive is triggered.
1281 void acxusb_i_complete_rx(struct urb *urb)
1283 acx_device_t *adev;
1284 rxbuffer_t *ptr;
1285 rxbuffer_t *inbuf;
1286 usb_rx_t *rx;
1287 unsigned long flags;
1288 int size, remsize, packetsize, rxnum;
1290 FN_ENTER;
1292 BUG_ON(!urb->context);
1294 rx = (usb_rx_t *) urb->context;
1295 adev = rx->adev;
1297 acx_lock(adev, flags);
1300 * Happens on disconnect or close. Don't play with the urb.
1301 * Don't resubmit it. It will get unlinked by close()
1303 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1304 log(L_USBRXTX, "rx: device is down, not doing anything\n");
1305 goto end_unlock;
1308 inbuf = &rx->bulkin;
1309 size = urb->actual_length;
1310 remsize = size;
1311 rxnum = rx - adev->usb_rx;
1313 log(L_USBRXTX, "RETURN RX (%d) status=%d size=%d\n",
1314 rxnum, urb->status, size);
1316 /* Send the URB that's waiting. */
1317 log(L_USBRXTX, "rxnum=%d, sending=%d\n", rxnum, rxnum ^ 1);
1318 acxusb_l_poll_rx(adev, &adev->usb_rx[rxnum ^ 1]);
1320 if (unlikely(size > sizeof(rxbuffer_t)))
1321 printk("acx_usb: rx too large: %d, please report\n", size);
1323 /* check if the transfer was aborted */
1324 switch (urb->status) {
1325 case 0: /* No error */
1326 break;
1327 case -EOVERFLOW:
1328 printk(KERN_ERR "acx: rx data overrun\n");
1329 adev->rxtruncsize = 0; /* Not valid anymore. */
1330 goto end_unlock;
1331 case -ECONNRESET:
1332 adev->rxtruncsize = 0;
1333 goto end_unlock;
1334 case -ESHUTDOWN: /* rmmod */
1335 adev->rxtruncsize = 0;
1336 goto end_unlock;
1337 default:
1338 adev->rxtruncsize = 0;
1339 adev->stats.rx_errors++;
1340 printk("acx: rx error (urb status=%d)\n", urb->status);
1341 goto end_unlock;
1344 if (unlikely(!size))
1345 printk("acx: warning, encountered zerolength rx packet\n");
1347 if (urb->transfer_buffer != inbuf)
1348 goto end_unlock;
1350 /* check if previous frame was truncated
1351 ** FIXME: this code can only handle truncation
1352 ** of consecutive packets!
1354 ptr = inbuf;
1355 if (adev->rxtruncsize) {
1356 int tail_size;
1358 ptr = &adev->rxtruncbuf;
1359 packetsize = RXBUF_BYTES_USED(ptr);
1360 if (acx_debug & L_USBRXTX) {
1361 printk("handling truncated frame (truncsize=%d size=%d "
1362 "packetsize(from trunc)=%d)\n",
1363 adev->rxtruncsize, size, packetsize);
1364 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1365 acx_dump_bytes(inbuf, RXBUF_HDRSIZE);
1368 /* bytes needed for rxtruncbuf completion: */
1369 tail_size = packetsize - adev->rxtruncsize;
1371 if (size < tail_size) {
1372 /* there is not enough data to complete this packet,
1373 ** simply append the stuff to the truncation buffer
1375 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf, size);
1376 adev->rxtruncsize += size;
1377 remsize = 0;
1378 } else {
1379 /* ok, this data completes the previously
1380 ** truncated packet. copy it into a descriptor
1381 ** and give it to the rest of the stack */
1383 /* append tail to previously truncated part
1384 ** NB: adev->rxtruncbuf (pointed to by ptr) can't
1385 ** overflow because this is already checked before
1386 ** truncation buffer was filled. See below,
1387 ** "if (packetsize > sizeof(rxbuffer_t))..." code */
1388 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf,
1389 tail_size);
1391 if (acx_debug & L_USBRXTX) {
1392 printk("full trailing packet + 12 bytes:\n");
1393 acx_dump_bytes(inbuf,
1394 tail_size + RXBUF_HDRSIZE);
1396 acx_l_process_rxbuf(adev, ptr);
1397 adev->rxtruncsize = 0;
1398 ptr = (rxbuffer_t *) (((char *)inbuf) + tail_size);
1399 remsize -= tail_size;
1401 log(L_USBRXTX, "post-merge size=%d remsize=%d\n",
1402 size, remsize);
1405 /* size = USB data block size
1406 ** remsize = unprocessed USB bytes left
1407 ** ptr = current pos in USB data block
1409 while (remsize) {
1410 if (remsize < RXBUF_HDRSIZE) {
1411 printk("acx: truncated rx header (%d bytes)!\n",
1412 remsize);
1413 if (ACX_DEBUG)
1414 acx_dump_bytes(ptr, remsize);
1415 break;
1418 packetsize = RXBUF_BYTES_USED(ptr);
1419 log(L_USBRXTX, "packet with packetsize=%d\n", packetsize);
1421 if (RXBUF_IS_TXSTAT(ptr)) {
1422 /* do rate handling */
1423 usb_txstatus_t *stat = (void *)ptr;
1425 log(L_USBRXTX, "tx: stat: mac_cnt_rcvd:%04X "
1426 "queue_index:%02X mac_status:%02X hostdata:%08X "
1427 "rate:%u ack_failures:%02X rts_failures:%02X "
1428 "rts_ok:%02X\n",
1429 stat->mac_cnt_rcvd,
1430 stat->queue_index, stat->mac_status, stat->hostdata,
1431 stat->rate, stat->ack_failures, stat->rts_failures,
1432 stat->rts_ok);
1434 if (adev->rate_auto && client_no < VEC_SIZE(adev->sta_list)) {
1435 client_t *clt = &adev->sta_list[client_no];
1436 u16 cur = stat->hostdata >> 16;
1438 if (clt && clt->rate_cur == cur) {
1439 acx_l_handle_txrate_auto(adev, clt,
1440 cur, // intended rate
1441 stat->rate, 0, // actually used rate
1442 stat->mac_status, // error?
1443 ACX_TX_URB_CNT - adev->tx_free);
1446 */ goto next;
1449 if (packetsize > sizeof(rxbuffer_t)) {
1450 printk("acx: packet exceeds max wlan "
1451 "frame size (%d > %d). size=%d\n",
1452 packetsize, (int)sizeof(rxbuffer_t), size);
1453 if (ACX_DEBUG)
1454 acx_dump_bytes(ptr, 16);
1455 /* FIXME: put some real error-handling in here! */
1456 break;
1459 if (packetsize > remsize) {
1460 /* frame truncation handling */
1461 if (acx_debug & L_USBRXTX) {
1462 printk("need to truncate packet, "
1463 "packetsize=%d remsize=%d "
1464 "size=%d bytes:",
1465 packetsize, remsize, size);
1466 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1468 memcpy(&adev->rxtruncbuf, ptr, remsize);
1469 adev->rxtruncsize = remsize;
1470 break;
1473 /* packetsize <= remsize */
1474 /* now handle the received data */
1475 acx_l_process_rxbuf(adev, ptr);
1476 next:
1477 ptr = (rxbuffer_t *) (((char *)ptr) + packetsize);
1478 remsize -= packetsize;
1479 if ((acx_debug & L_USBRXTX) && remsize) {
1480 printk("more than one packet in buffer, "
1481 "second packet hdr:");
1482 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1486 end_unlock:
1487 acx_unlock(adev, flags);
1488 /* end: */
1489 FN_EXIT0;
1493 /***********************************************************************
1494 ** acxusb_i_complete_tx()
1495 ** Inputs:
1496 ** urb -> pointer to USB request block
1497 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1499 ** This function is invoked upon termination of a USB transfer.
1501 void acxusb_i_complete_tx(struct urb *urb)
1503 acx_device_t *adev;
1504 usb_tx_t *tx;
1505 unsigned long flags;
1506 int txnum;
1508 FN_ENTER;
1510 BUG_ON(!urb->context);
1512 tx = (usb_tx_t *) urb->context;
1513 adev = tx->adev;
1515 txnum = tx - adev->usb_tx;
1517 acx_lock(adev, flags);
1520 * If the iface isn't up, we don't have any right
1521 * to play with them. The urb may get unlinked.
1523 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1524 log(L_USBRXTX, "tx: device is down, not doing anything\n");
1525 goto end_unlock;
1528 log(L_USBRXTX, "RETURN TX (%d): status=%d size=%d\n",
1529 txnum, urb->status, urb->actual_length);
1531 /* handle USB transfer errors */
1532 switch (urb->status) {
1533 case 0: /* No error */
1534 break;
1535 case -ESHUTDOWN:
1536 goto end_unlock;
1537 break;
1538 case -ECONNRESET:
1539 goto end_unlock;
1540 break;
1541 /* FIXME: real error-handling code here please */
1542 default:
1543 printk(KERN_ERR "acx: tx error, urb status=%d\n", urb->status);
1544 /* FIXME: real error-handling code here please */
1547 /* free the URB and check for more data */
1548 tx->busy = 0;
1549 adev->tx_free++;
1550 if ((adev->tx_free >= TX_START_QUEUE)
1551 && (adev->status == ACX_STATUS_4_ASSOCIATED)
1552 /* && (acx_queue_stopped(adev->ndev)*/) {
1553 log(L_BUF, "tx: wake queue (%u free txbufs)\n", adev->tx_free);
1554 /* acx_wake_queue(adev->ndev, NULL); */
1557 end_unlock:
1558 acx_unlock(adev, flags);
1559 /* end: */
1560 FN_EXIT0;
1564 /***************************************************************
1565 ** acxusb_l_alloc_tx
1566 ** Actually returns a usb_tx_t* ptr
1568 tx_t *acxusb_l_alloc_tx(acx_device_t * adev)
1570 usb_tx_t *tx;
1571 unsigned head;
1573 FN_ENTER;
1575 head = adev->tx_head;
1576 do {
1577 head = (head + 1) % ACX_TX_URB_CNT;
1578 if (!adev->usb_tx[head].busy) {
1579 log(L_USBRXTX, "allocated tx %d\n", head);
1580 tx = &adev->usb_tx[head];
1581 tx->busy = 1;
1582 adev->tx_free--;
1583 /* Keep a few free descs between head and tail of tx ring.
1584 ** It is not absolutely needed, just feels safer */
1585 if (adev->tx_free < TX_STOP_QUEUE) {
1586 log(L_BUF, "tx: stop queue "
1587 "(%u free txbufs)\n", adev->tx_free);
1588 /* acx_stop_queue(adev->ndev, NULL); */
1590 goto end;
1592 } while (likely(head != adev->tx_head));
1593 tx = NULL;
1594 printk_ratelimited("acx: tx buffers full\n");
1595 end:
1596 adev->tx_head = head;
1597 FN_EXIT0;
1598 return (tx_t *) tx;
1602 /***************************************************************
1603 ** Used if alloc_tx()'ed buffer needs to be cancelled without doing tx
1605 void acxusb_l_dealloc_tx(tx_t * tx_opaque)
1607 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1608 tx->busy = 0;
1612 /***************************************************************
1614 void *acxusb_l_get_txbuf(acx_device_t * adev, tx_t * tx_opaque)
1616 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1617 return &tx->bulkout.data;
1621 /***************************************************************
1622 ** acxusb_l_tx_data
1624 ** Can be called from IRQ (rx -> (AP bridging or mgmt response) -> tx).
1625 ** Can be called from acx_i_start_xmit (data frames from net core).
1627 void acxusb_l_tx_data(acx_device_t * adev, tx_t * tx_opaque, int wlanpkt_len, struct ieee80211_tx_control *ctl,
1628 struct sk_buff* skb)
1630 struct usb_device *usbdev;
1631 struct urb *txurb;
1632 usb_tx_t *tx;
1633 usb_txbuffer_t *txbuf;
1634 // client_t *clt;
1635 struct ieee80211_hdr *whdr;
1636 unsigned int outpipe;
1637 int ucode, txnum;
1639 FN_ENTER;
1641 tx = ((usb_tx_t *) tx_opaque);
1642 txurb = tx->urb;
1643 txbuf = &tx->bulkout;
1644 whdr = (struct ieee80211_hdr *) txbuf->data;
1645 txnum = tx - adev->usb_tx;
1647 log(L_DEBUG, "using buf#%d free=%d len=%d\n",
1648 txnum, adev->tx_free, wlanpkt_len);
1650 switch (adev->mode) {
1651 case ACX_MODE_0_ADHOC:
1652 case ACX_MODE_3_AP:
1653 clt = acx_l_sta_list_get(adev, whdr->a1);
1654 break;
1655 case ACX_MODE_2_STA:
1656 // clt = adev->ap_client;
1657 break;
1658 default:
1659 clt = NULL;
1660 break;
1662 if (unlikely(clt && !clt->rate_cur)) {
1663 printk("acx: driver bug! bad ratemask\n");
1664 goto end;
1668 /* fill the USB transfer header */
1669 txbuf->desc = cpu_to_le16(USB_TXBUF_TXDESC);
1670 txbuf->mpdu_len = cpu_to_le16(wlanpkt_len);
1671 txbuf->queue_index = 1;
1672 txbuf->rate = ctl->tx_rate; //clt->rate_100;
1673 // FIXME(); //This used to have | (clt - adev->ap_client)
1674 txbuf->hostdata = (ctl->tx_rate << 16);
1675 txbuf->ctrl1 = DESC_CTL_FIRSTFRAG;
1676 if (1 == adev->preamble_cur)
1677 SET_BIT(txbuf->ctrl1, DESC_CTL_SHORT_PREAMBLE);
1678 txbuf->ctrl2 = 0;
1679 txbuf->data_len = cpu_to_le16(wlanpkt_len);
1681 if (unlikely(acx_debug & L_DATA)) {
1682 printk("dump of bulk out urb:\n");
1683 acx_dump_bytes(txbuf, wlanpkt_len + USB_TXBUF_HDRSIZE);
1686 if (unlikely(txurb->status == -EINPROGRESS)) {
1687 printk
1688 ("acx: trying to submit tx urb while already in progress\n");
1691 /* now schedule the USB transfer */
1692 usbdev = adev->usbdev;
1693 outpipe = usb_sndbulkpipe(usbdev, adev->bulkoutep);
1695 usb_fill_bulk_urb(txurb, usbdev, outpipe, txbuf, /* dataptr */
1696 wlanpkt_len + USB_TXBUF_HDRSIZE, /* size */
1697 acxusb_i_complete_tx, /* handler */
1698 tx /* handler param */
1701 txurb->transfer_flags = URB_ASYNC_UNLINK | URB_ZERO_PACKET;
1702 ucode = usb_submit_urb(txurb, GFP_ATOMIC);
1703 log(L_USBRXTX, "SUBMIT TX (%d): outpipe=0x%X buf=%p txsize=%d "
1704 "rate=%u errcode=%d\n", txnum, outpipe, txbuf,
1705 wlanpkt_len + USB_TXBUF_HDRSIZE, txbuf->rate, ucode);
1707 if (unlikely(ucode)) {
1708 printk(KERN_ERR "acx: submit_urb() error=%d txsize=%d\n",
1709 ucode, wlanpkt_len + USB_TXBUF_HDRSIZE);
1711 /* on error, just mark the frame as done and update
1712 ** the statistics
1714 adev->stats.tx_errors++;
1715 tx->busy = 0;
1716 adev->tx_free++;
1717 /* needed? if (adev->tx_free > TX_START_QUEUE) acx_wake_queue(...) */
1719 FN_EXIT0;
1723 /***********************************************************************
1724 static void acxusb_i_set_rx_mode(struct net_device *ndev)
1730 /***********************************************************************
1732 #ifdef HAVE_TX_TIMEOUT
1734 void acxusb_i_tx_timeout(struct net_device *ndev)
1736 acx_device_t *adev = ndev2adev(ndev);
1737 unsigned long flags;
1738 int i;
1740 FN_ENTER;
1742 acx_lock(adev, flags);
1743 */ /* unlink the URBs */
1744 /* for (i = 0; i < ACX_TX_URB_CNT; i++) {
1745 acxusb_unlink_urb(adev->usb_tx[i].urb);
1746 adev->usb_tx[i].busy = 0;
1748 adev->tx_free = ACX_TX_URB_CNT;
1749 */ /* TODO: stats update */
1750 /* acx_unlock(adev, flags);
1752 FN_EXIT0;
1755 #endif
1758 /***********************************************************************
1759 ** init_module()
1761 ** This function is invoked upon loading of the kernel module.
1762 ** It registers itself at the kernel's USB subsystem.
1764 ** Returns: Errorcode on failure, 0 on success
1766 int __init acxusb_e_init_module(void)
1768 log(L_INIT, "USB module " ACX_RELEASE " initialized, "
1769 "probing for devices...\n");
1770 return usb_register(&acxusb_driver);
1775 /***********************************************************************
1776 ** cleanup_module()
1778 ** This function is invoked as last step of the module unloading. It simply
1779 ** deregisters this module at the kernel's USB subsystem.
1781 void __exit acxusb_e_cleanup_module(void)
1783 usb_deregister(&acxusb_driver);
1784 log(L_INIT, "USB module " ACX_RELEASE " unloaded\n");
1788 /***********************************************************************
1789 ** DEBUG STUFF
1791 #if ACX_DEBUG
1793 #ifdef UNUSED
1794 static void dump_device(struct usb_device *usbdev)
1796 int i;
1797 struct usb_config_descriptor *cd;
1799 printk("acx device dump:\n");
1800 printk(" devnum: %d\n", usbdev->devnum);
1801 printk(" speed: %d\n", usbdev->speed);
1802 printk(" tt: 0x%X\n", (unsigned int)(usbdev->tt));
1803 printk(" ttport: %d\n", (unsigned int)(usbdev->ttport));
1804 printk(" toggle[0]: 0x%X toggle[1]: 0x%X\n",
1805 (unsigned int)(usbdev->toggle[0]),
1806 (unsigned int)(usbdev->toggle[1]));
1807 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1808 /* This saw a change after 2.6.10 */
1809 printk(" ep_in wMaxPacketSize: ");
1810 for (i = 0; i < 16; ++i)
1811 if (usbdev->ep_in[i] != NULL)
1812 printk("%d:%d ", i,
1813 usbdev->ep_in[i]->desc.wMaxPacketSize);
1814 printk("\n");
1815 printk(" ep_out wMaxPacketSize: ");
1816 for (i = 0; i < ARRAY_SIZE(usbdev->ep_out); ++i)
1817 if (usbdev->ep_out[i] != NULL)
1818 printk("%d:%d ", i,
1819 usbdev->ep_out[i]->desc.wMaxPacketSize);
1820 printk("\n");
1821 #else
1822 printk(" epmaxpacketin: ");
1823 for (i = 0; i < 16; i++)
1824 printk("%d ", usbdev->epmaxpacketin[i]);
1825 printk("\n");
1826 printk(" epmaxpacketout: ");
1827 for (i = 0; i < 16; i++)
1828 printk("%d ", usbdev->epmaxpacketout[i]);
1829 printk("\n");
1830 #endif
1831 printk(" parent: 0x%X\n", (unsigned int)usbdev->parent);
1832 printk(" bus: 0x%X\n", (unsigned int)usbdev->bus);
1833 #ifdef NO_DATATYPE
1834 printk(" configs: ");
1835 for (i = 0; i < usbdev->descriptor.bNumConfigurations; i++)
1836 printk("0x%X ", usbdev->config[i]);
1837 printk("\n");
1838 #endif
1839 printk(" actconfig: %p\n", usbdev->actconfig);
1840 dump_device_descriptor(&usbdev->descriptor);
1842 cd = &usbdev->config->desc;
1843 dump_config_descriptor(cd);
1847 /***********************************************************************
1849 static void dump_config_descriptor(struct usb_config_descriptor *cd)
1851 printk("Configuration Descriptor:\n");
1852 if (!cd) {
1853 printk("NULL\n");
1854 return;
1856 printk(" bLength: %d (0x%X)\n", cd->bLength, cd->bLength);
1857 printk(" bDescriptorType: %d (0x%X)\n", cd->bDescriptorType,
1858 cd->bDescriptorType);
1859 printk(" bNumInterfaces: %d (0x%X)\n", cd->bNumInterfaces,
1860 cd->bNumInterfaces);
1861 printk(" bConfigurationValue: %d (0x%X)\n", cd->bConfigurationValue,
1862 cd->bConfigurationValue);
1863 printk(" iConfiguration: %d (0x%X)\n", cd->iConfiguration,
1864 cd->iConfiguration);
1865 printk(" bmAttributes: %d (0x%X)\n", cd->bmAttributes,
1866 cd->bmAttributes);
1867 /* printk(" MaxPower: %d (0x%X)\n", cd->bMaxPower, cd->bMaxPower); */
1871 static void dump_device_descriptor(struct usb_device_descriptor *dd)
1873 printk("Device Descriptor:\n");
1874 if (!dd) {
1875 printk("NULL\n");
1876 return;
1878 printk(" bLength: %d (0x%X)\n", dd->bLength, dd->bLength);
1879 printk(" bDescriptortype: %d (0x%X)\n", dd->bDescriptorType,
1880 dd->bDescriptorType);
1881 printk(" bcdUSB: %d (0x%X)\n", dd->bcdUSB, dd->bcdUSB);
1882 printk(" bDeviceClass: %d (0x%X)\n", dd->bDeviceClass,
1883 dd->bDeviceClass);
1884 printk(" bDeviceSubClass: %d (0x%X)\n", dd->bDeviceSubClass,
1885 dd->bDeviceSubClass);
1886 printk(" bDeviceProtocol: %d (0x%X)\n", dd->bDeviceProtocol,
1887 dd->bDeviceProtocol);
1888 printk(" bMaxPacketSize0: %d (0x%X)\n", dd->bMaxPacketSize0,
1889 dd->bMaxPacketSize0);
1890 printk(" idVendor: %d (0x%X)\n", dd->idVendor, dd->idVendor);
1891 printk(" idProduct: %d (0x%X)\n", dd->idProduct, dd->idProduct);
1892 printk(" bcdDevice: %d (0x%X)\n", dd->bcdDevice, dd->bcdDevice);
1893 printk(" iManufacturer: %d (0x%X)\n", dd->iManufacturer,
1894 dd->iManufacturer);
1895 printk(" iProduct: %d (0x%X)\n", dd->iProduct, dd->iProduct);
1896 printk(" iSerialNumber: %d (0x%X)\n", dd->iSerialNumber,
1897 dd->iSerialNumber);
1898 printk(" bNumConfigurations: %d (0x%X)\n", dd->bNumConfigurations,
1899 dd->bNumConfigurations);
1901 #endif /* UNUSED */
1903 #endif /* ACX_DEBUG */