remove unused function
[acx-mac80211.git] / usb.c
blobc502232b1980f141cc2d21eafa446e611d9ce3f1
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 #include <linux/types.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h>
24 #include <linux/usb.h>
25 #include <linux/netdevice.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/etherdevice.h>
28 #include <linux/wireless.h>
29 #include <net/iw_handler.h>
30 #include <linux/vmalloc.h>
31 #include <linux/ethtool.h>
32 #include <linux/workqueue.h>
34 #include "acx.h"
37 /***********************************************************************
39 /* number of endpoints of an interface */
40 #define NUM_EP(intf) (intf)->altsetting[0].desc.bNumEndpoints
41 #define EP(intf, nr) (intf)->altsetting[0].endpoint[(nr)].desc
42 #define GET_DEV(udev) usb_get_dev((udev))
43 #define PUT_DEV(udev) usb_put_dev((udev))
45 /* removed in 2.6.14. We will use fake value for now
46 * TODO: maybe we should just remove all lines that include
47 * URB_ASYNC_UNLINK somewhere?
49 #define URB_ASYNC_UNLINK 0
51 /***********************************************************************
53 /* ACX100 (TNETW1100) USB device: D-Link DWL-120+ */
54 #define ACX100_VENDOR_ID 0x2001
55 #define ACX100_PRODUCT_ID_UNBOOTED 0x3B01
56 #define ACX100_PRODUCT_ID_BOOTED 0x3B00
58 /* TNETW1450 USB devices */
59 #define VENDOR_ID_DLINK 0x07b8 /* D-Link Corp. */
60 #define PRODUCT_ID_WUG2400 0xb21a /* AboCom WUG2400 or SafeCom SWLUT-54125 */
61 #define VENDOR_ID_AVM_GMBH 0x057c
62 #define PRODUCT_ID_AVM_WLAN_USB 0x5601
63 #define PRODUCT_ID_AVM_WLAN_USB_si 0x6201 /* "self install" named Version:
64 * driver kills kernel on inbound scans from fritz box ?? */
65 #define VENDOR_ID_ZCOM 0x0cde
66 #define PRODUCT_ID_ZCOM_XG750 0x0017 /* not tested yet */
67 #define VENDOR_ID_TI 0x0451
68 #define PRODUCT_ID_TI_UNKNOWN 0x60c5 /* not tested yet */
70 #define ACX_USB_CTRL_TIMEOUT 5500 /* steps in ms */
72 /* Buffer size for fw upload, same for both ACX100 USB and TNETW1450 */
73 #define USB_RWMEM_MAXLEN 2048
75 /* The number of bulk URBs to use */
76 #define ACX_TX_URB_CNT 8
77 #define ACX_RX_URB_CNT 2
79 /* Should be sent to the bulkout endpoint */
80 #define ACX_USB_REQ_UPLOAD_FW 0x10
81 #define ACX_USB_REQ_ACK_CS 0x11
82 #define ACX_USB_REQ_CMD 0x12
84 /***********************************************************************
85 ** Prototypes
87 static int acxusb_e_probe(struct usb_interface *, const struct usb_device_id *);
88 static void acxusb_e_disconnect(struct usb_interface *);
89 static void acxusb_i_complete_tx(struct urb *);
90 static void acxusb_i_complete_rx(struct urb *);
91 static int acxusb_e_open(struct ieee80211_hw *);
92 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
93 static int acxusb_e_close(struct ieee80211_hw *);
94 #else
95 static void acxusb_e_close(struct ieee80211_hw *);
96 #endif
97 //static void acxusb_i_set_rx_mode(struct net_device *);
98 static int acxusb_boot(struct usb_device *, int is_tnetw1450, int *radio_type);
100 static void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx);
102 /*static void acxusb_i_tx_timeout(struct net_device *);*/
104 /* static void dump_device(struct usb_device *); */
105 /* static void dump_device_descriptor(struct usb_device_descriptor *); */
106 /* static void dump_config_descriptor(struct usb_config_descriptor *); */
108 /***********************************************************************
109 ** Module Data
111 #define TXBUFSIZE sizeof(usb_txbuffer_t)
113 * Now, this is just plain lying, but the device insists in giving us
114 * huge packets. We supply extra space after rxbuffer. Need to understand
115 * it better...
117 #define RXBUFSIZE (sizeof(rxbuffer_t) + \
118 (sizeof(usb_rx_t) - sizeof(struct usb_rx_plain)))
120 static const struct usb_device_id acxusb_ids[] = {
121 {USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_BOOTED)},
122 {USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_UNBOOTED)},
123 {USB_DEVICE(VENDOR_ID_DLINK, PRODUCT_ID_WUG2400)},
124 {USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB)},
125 {USB_DEVICE(VENDOR_ID_AVM_GMBH, PRODUCT_ID_AVM_WLAN_USB_si)},
126 {USB_DEVICE(VENDOR_ID_ZCOM, PRODUCT_ID_ZCOM_XG750)},
127 {USB_DEVICE(VENDOR_ID_TI, PRODUCT_ID_TI_UNKNOWN)},
131 MODULE_DEVICE_TABLE(usb, acxusb_ids);
133 /* USB driver data structure as required by the kernel's USB core */
134 static struct usb_driver
135 acxusb_driver = {
136 .name = "acx_usb",
137 .probe = acxusb_e_probe,
138 .disconnect = acxusb_e_disconnect,
139 .id_table = acxusb_ids
142 /***********************************************************************
143 ** USB helper
145 ** ldd3 ch13 says:
146 ** When the function is usb_kill_urb, the urb lifecycle is stopped. This
147 ** function is usually used when the device is disconnected from the system,
148 ** in the disconnect callback. For some drivers, the usb_unlink_urb function
149 ** should be used to tell the USB core to stop an urb. This function does not
150 ** wait for the urb to be fully stopped before returning to the caller.
151 ** This is useful for stoppingthe urb while in an interrupt handler or when
152 ** a spinlock is held, as waiting for a urb to fully stop requires the ability
153 ** for the USB core to put the calling process to sleep. This function requires
154 ** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
155 ** to be stopped in order to work properly.
157 ** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
158 ** asynchronous while usb_kill_urb is synchronous and should be called
159 ** directly (drivers/usb/core/urb.c))
161 ** In light of this, timeout is just for paranoid reasons...
163 ** Actually, it's useful for debugging. If we reach timeout, we're doing
164 ** something wrong with the urbs.
166 static void acxusb_unlink_urb(struct urb *urb)
168 if (!urb)
169 return;
171 if (urb->status == -EINPROGRESS) {
172 int timeout = 10;
174 usb_unlink_urb(urb);
175 while (--timeout && urb->status == -EINPROGRESS) {
176 mdelay(1);
178 if (!timeout) {
179 printk(KERN_ERR "acx_usb: urb unlink timeout!\n");
185 /***********************************************************************
186 ** EEPROM and PHY read/write helpers
188 /***********************************************************************
189 ** acxusb_s_read_phy_reg
191 int acxusb_s_read_phy_reg(acx_device_t * adev, u32 reg, u8 * charbuf)
193 /* mem_read_write_t mem; */
195 FN_ENTER;
197 printk("%s doesn't seem to work yet, disabled.\n", __func__);
200 mem.addr = cpu_to_le16(reg);
201 mem.type = cpu_to_le16(0x82);
202 mem.len = cpu_to_le32(4);
203 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
204 *charbuf = mem.data;
205 log(L_DEBUG, "read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
208 FN_EXIT1(OK);
209 return OK;
213 /***********************************************************************
215 int acxusb_s_write_phy_reg(acx_device_t * adev, u32 reg, u8 value)
217 mem_read_write_t mem;
219 FN_ENTER;
221 mem.addr = cpu_to_le16(reg);
222 mem.type = cpu_to_le16(0x82);
223 mem.len = cpu_to_le32(4);
224 mem.data = value;
225 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
226 log(L_DEBUG, "write radio PHY[0x%04X]=0x%02X\n", reg, value);
228 FN_EXIT1(OK);
229 return OK;
233 /***********************************************************************
234 ** acxusb_s_issue_cmd_timeo
235 ** Excecutes a command in the command mailbox
237 ** buffer = a pointer to the data.
238 ** The data must not include 4 byte command header
241 /* TODO: ideally we shall always know how much we need
242 ** and this shall be 0 */
243 #define BOGUS_SAFETY_PADDING 0x40
245 #undef FUNC
246 #define FUNC "issue_cmd"
248 #if !ACX_DEBUG
250 acxusb_s_issue_cmd_timeo(acx_device_t * adev,
251 unsigned cmd,
252 void *buffer, unsigned buflen, unsigned timeout)
254 #else
256 acxusb_s_issue_cmd_timeo_debug(acx_device_t * adev,
257 unsigned cmd,
258 void *buffer,
259 unsigned buflen,
260 unsigned timeout, const char *cmdstr)
262 #endif
263 /* USB ignores timeout param */
265 struct usb_device *usbdev;
266 struct {
267 u16 cmd;
268 u16 status;
269 u8 data[1];
270 } ACX_PACKED *loc;
271 const char *devname;
272 int acklen, blocklen, inpipe, outpipe;
273 int cmd_status;
274 int result;
276 FN_ENTER;
278 devname = wiphy_name(adev->ieee->wiphy);
279 /* no "wlan%%d: ..." please */
280 if (!devname || !devname[0] || devname[4] == '%')
281 devname = "acx";
283 log(L_CTL, FUNC "(cmd:%s,buflen:%u,type:0x%04X)\n",
284 cmdstr, buflen,
285 buffer ? le16_to_cpu(((acx_ie_generic_t *) buffer)->type) : -1);
287 loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
288 if (!loc) {
289 printk("%s: " FUNC "(): no memory for data buffer\n", devname);
290 goto bad;
293 /* get context from acx_device */
294 usbdev = adev->usbdev;
296 /* check which kind of command was issued */
297 loc->cmd = cpu_to_le16(cmd);
298 loc->status = 0;
300 /* NB: buflen == frmlen + 4
302 ** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
303 ** read (cmd,status,rid,frmlen,data[frmlen]) back
305 ** Configure: write (cmd,status,rid,frmlen,data[frmlen])
307 ** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
310 /* now write the parameters of the command if needed */
311 acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
312 blocklen = buflen;
313 if (buffer && buflen) {
314 /* if it's an INTERROGATE command, just pass the length
315 * of parameters to read, as data */
316 if (cmd == ACX1xx_CMD_INTERROGATE) {
317 blocklen = 4;
318 acklen = buflen + 4;
320 memcpy(loc->data, buffer, blocklen);
322 blocklen += 4; /* account for cmd,status */
324 /* obtain the I/O pipes */
325 outpipe = usb_sndctrlpipe(usbdev, 0);
326 inpipe = usb_rcvctrlpipe(usbdev, 0);
327 log(L_CTL, "ctrl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
328 log(L_CTL, "sending USB control msg (out) (blocklen=%d)\n", blocklen);
329 if (acx_debug & L_DATA)
330 acx_dump_bytes(loc, blocklen);
332 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_CMD, /* request */
333 USB_TYPE_VENDOR | USB_DIR_OUT, /* requesttype */
334 0, /* value */
335 0, /* index */
336 loc, /* dataptr */
337 blocklen, /* size */
338 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
341 if (result == -ENODEV) {
342 log(L_CTL, "no device present (unplug?)\n");
343 goto good;
346 log(L_CTL, "wrote %d bytes\n", result);
347 if (result < 0) {
348 goto bad;
351 /* check for device acknowledge */
352 log(L_CTL, "sending USB control msg (in) (acklen=%d)\n", acklen);
353 loc->status = 0; /* delete old status flag -> set to IDLE */
354 /* shall we zero out the rest? */
355 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_CMD, /* request */
356 USB_TYPE_VENDOR | USB_DIR_IN, /* requesttype */
357 0, /* value */
358 0, /* index */
359 loc, /* dataptr */
360 acklen, /* size */
361 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
363 if (result < 0) {
364 printk("%s: " FUNC "(): USB read error %d\n", devname, result);
365 goto bad;
367 if (acx_debug & L_CTL) {
368 printk("read %d bytes: ", result);
369 acx_dump_bytes(loc, result);
373 check for result==buflen+4? Was seen:
375 interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
376 issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
377 ctrl inpipe=0x80000280 outpipe=0x80000200
378 sending USB control msg (out) (blocklen=8)
379 01 00 00 00 0F 10 04 00
380 wrote 8 bytes
381 sending USB control msg (in) (acklen=12) sizeof(loc->data
382 read 4 bytes <==== MUST BE 12!!
385 cmd_status = le16_to_cpu(loc->status);
386 if (cmd_status != 1) {
387 printk("%s: " FUNC "(): cmd_status is not SUCCESS: %d (%s)\n",
388 devname, cmd_status, acx_cmd_status_str(cmd_status));
389 /* TODO: goto bad; ? */
391 if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
392 memcpy(buffer, loc->data, buflen);
393 log(L_CTL, "response frame: cmd=0x%04X status=%d\n",
394 le16_to_cpu(loc->cmd), cmd_status);
396 good:
397 kfree(loc);
398 FN_EXIT1(OK);
399 return OK;
400 bad:
401 /* Give enough info so that callers can avoid
402 ** printing their own diagnostic messages */
403 #if ACX_DEBUG
404 printk("%s: " FUNC "(cmd:%s) FAILED\n", devname, cmdstr);
405 #else
406 printk("%s: " FUNC "(cmd:0x%04X) FAILED\n", devname, cmd);
407 #endif
408 dump_stack();
409 kfree(loc);
410 FN_EXIT1(NOT_OK);
411 return NOT_OK;
415 /***********************************************************************
416 ** acxusb_boot()
417 ** Inputs:
418 ** usbdev -> Pointer to kernel's usb_device structure
420 ** Returns:
421 ** (int) Errorcode or 0 on success
423 ** This function triggers the loading of the firmware image from harddisk
424 ** and then uploads the firmware to the USB device. After uploading the
425 ** firmware and transmitting the checksum, the device resets and appears
426 ** as a new device on the USB bus (the device we can finally deal with)
428 static inline int
429 acxusb_fw_needs_padding(firmware_image_t *fw_image, unsigned int usb_maxlen)
431 unsigned int num_xfers = ((fw_image->size - 1) / usb_maxlen) + 1;
433 return ((num_xfers % 2) == 0);
436 static int
437 acxusb_boot(struct usb_device *usbdev, int is_tnetw1450, int *radio_type)
439 char filename[sizeof("tiacx1NNusbcRR")];
441 firmware_image_t *fw_image = NULL;
442 char *usbbuf;
443 unsigned int offset;
444 unsigned int blk_len, inpipe, outpipe;
445 u32 num_processed;
446 u32 img_checksum, sum;
447 u32 file_size;
448 int result = -EIO;
449 int i;
451 FN_ENTER;
453 /* dump_device(usbdev); */
455 usbbuf = kmalloc(USB_RWMEM_MAXLEN, GFP_KERNEL);
456 if (!usbbuf) {
457 printk(KERN_ERR
458 "acx: no memory for USB transfer buffer (%d bytes)\n",
459 USB_RWMEM_MAXLEN);
460 result = -ENOMEM;
461 goto end;
463 if (is_tnetw1450) {
464 /* Obtain the I/O pipes */
465 outpipe = usb_sndbulkpipe(usbdev, 1);
466 inpipe = usb_rcvbulkpipe(usbdev, 2);
468 printk(KERN_DEBUG "wait for device ready\n");
469 for (i = 0; i <= 2; i++) {
470 result = usb_bulk_msg(usbdev, inpipe,
471 usbbuf,
472 USB_RWMEM_MAXLEN,
473 &num_processed, 2000);
475 if ((*(u32 *) & usbbuf[4] == 0x40000001)
476 && (*(u16 *) & usbbuf[2] == 0x1)
477 && ((*(u16 *) usbbuf & 0x3fff) == 0)
478 && ((*(u16 *) usbbuf & 0xc000) == 0xc000))
479 break;
480 acx_s_mwait(10);
482 if (i == 2)
483 goto fw_end;
485 *radio_type = usbbuf[8];
486 } else {
487 /* Obtain the I/O pipes */
488 outpipe = usb_sndctrlpipe(usbdev, 0);
489 inpipe = usb_rcvctrlpipe(usbdev, 0);
491 /* FIXME: shouldn't be hardcoded */
492 *radio_type = RADIO_MAXIM_0D;
495 snprintf(filename, sizeof(filename), "tiacx1%02dusbc%02X",
496 is_tnetw1450 * 11, *radio_type);
498 fw_image = acx_s_read_fw(&usbdev->dev, filename, &file_size);
499 if (!fw_image) {
500 result = -EIO;
501 goto end;
503 log(L_INIT, "firmware size: %d bytes\n", file_size);
505 img_checksum = le32_to_cpu(fw_image->chksum);
507 if (is_tnetw1450) {
508 u8 cmdbuf[20];
509 const u8 *p;
510 u8 need_padding;
511 u32 tmplen, val;
513 memset(cmdbuf, 0, 16);
515 need_padding =
516 acxusb_fw_needs_padding(fw_image, USB_RWMEM_MAXLEN);
517 tmplen = need_padding ? file_size - 4 : file_size - 8;
518 *(u16 *) & cmdbuf[0] = 0xc000;
519 *(u16 *) & cmdbuf[2] = 0x000b;
520 *(u32 *) & cmdbuf[4] = tmplen;
521 *(u32 *) & cmdbuf[8] = file_size - 8;
522 *(u32 *) & cmdbuf[12] = img_checksum;
524 result =
525 usb_bulk_msg(usbdev, outpipe, cmdbuf, 16, &num_processed,
526 HZ);
527 if (result < 0)
528 goto fw_end;
530 p = (const u8 *)&fw_image->size;
532 /* first calculate checksum for image size part */
533 sum = p[0] + p[1] + p[2] + p[3];
534 p += 4;
536 /* now continue checksum for firmware data part */
537 tmplen = le32_to_cpu(fw_image->size);
538 for (i = 0; i < tmplen /* image size */ ; i++) {
539 sum += *p++;
542 if (sum != le32_to_cpu(fw_image->chksum)) {
543 printk("acx: FATAL: firmware upload: "
544 "checksums don't match! "
545 "(0x%08x vs. 0x%08x)\n", sum, fw_image->chksum);
546 goto fw_end;
549 offset = 8;
550 while (offset < file_size) {
551 blk_len = file_size - offset;
552 if (blk_len > USB_RWMEM_MAXLEN) {
553 blk_len = USB_RWMEM_MAXLEN;
556 log(L_INIT,
557 "uploading firmware (%d bytes, offset=%d)\n",
558 blk_len, offset);
559 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
561 p = usbbuf;
562 for (i = 0; i < blk_len; i += 4) {
563 *(u32 *) p = be32_to_cpu(*(u32 *) p);
564 p += 4;
567 result =
568 usb_bulk_msg(usbdev, outpipe, usbbuf, blk_len,
569 &num_processed, HZ);
570 if ((result < 0) || (num_processed != blk_len))
571 goto fw_end;
572 offset += blk_len;
574 if (need_padding) {
575 printk(KERN_DEBUG "send padding\n");
576 memset(usbbuf, 0, 4);
577 result =
578 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
579 &num_processed, HZ);
580 if ((result < 0) || (num_processed != 4))
581 goto fw_end;
583 printk(KERN_DEBUG "read firmware upload result\n");
584 memset(cmdbuf, 0, 20); /* additional memset */
585 result =
586 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed,
587 2000);
588 if (result < 0)
589 goto fw_end;
590 if (*(u32 *) & cmdbuf[4] == 0x40000003)
591 goto fw_end;
592 if (*(u32 *) & cmdbuf[4])
593 goto fw_end;
594 if (*(u16 *) & cmdbuf[16] != 1)
595 goto fw_end;
597 val = *(u32 *) & cmdbuf[0];
598 if ((val & 0x3fff)
599 || ((val & 0xc000) != 0xc000))
600 goto fw_end;
602 val = *(u32 *) & cmdbuf[8];
603 if (val & 2) {
604 result =
605 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20,
606 &num_processed, 2000);
607 if (result < 0)
608 goto fw_end;
609 val = *(u32 *) & cmdbuf[8];
611 /* yup, no "else" here! */
612 if (val & 1) {
613 memset(usbbuf, 0, 4);
614 result =
615 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
616 &num_processed, HZ);
617 if ((result < 0) || (!num_processed))
618 goto fw_end;
621 printk("TNETW1450 firmware upload successful!\n");
622 result = 0;
623 goto end;
624 fw_end:
625 result = -EIO;
626 goto end;
627 } else {
628 /* ACX100 USB */
630 /* now upload the firmware, slice the data into blocks */
631 offset = 8;
632 while (offset < file_size) {
633 blk_len = file_size - offset;
634 if (blk_len > USB_RWMEM_MAXLEN) {
635 blk_len = USB_RWMEM_MAXLEN;
637 log(L_INIT,
638 "uploading firmware (%d bytes, offset=%d)\n",
639 blk_len, offset);
640 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
641 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, (file_size - 8) & 0xffff, /* value */
642 (file_size - 8) >> 16, /* index */
643 usbbuf, /* dataptr */
644 blk_len, /* size */
645 3000 /* timeout in ms */
647 offset += blk_len;
648 if (result < 0) {
649 printk(KERN_ERR "acx: error %d during upload "
650 "of firmware, aborting\n", result);
651 goto end;
655 /* finally, send the checksum and reboot the device */
656 /* does this trigger the reboot? */
657 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, img_checksum & 0xffff, /* value */
658 img_checksum >> 16, /* index */
659 NULL, /* dataptr */
660 0, /* size */
661 3000 /* timeout in ms */
663 if (result < 0) {
664 printk(KERN_ERR "acx: error %d during tx of checksum, "
665 "aborting\n", result);
666 goto end;
668 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_ACK_CS, USB_TYPE_VENDOR | USB_DIR_IN, img_checksum & 0xffff, /* value */
669 img_checksum >> 16, /* index */
670 usbbuf, /* dataptr */
671 8, /* size */
672 3000 /* timeout in ms */
674 if (result < 0) {
675 printk(KERN_ERR "acx: error %d during ACK of checksum, "
676 "aborting\n", result);
677 goto end;
679 if (*usbbuf != 0x10) {
680 printk(KERN_ERR "acx: invalid checksum?\n");
681 result = -EINVAL;
682 goto end;
684 result = 0;
687 end:
688 vfree(fw_image);
689 kfree(usbbuf);
691 FN_EXIT1(result);
692 return result;
696 /* FIXME: maybe merge it with usual eeprom reading, into common code? */
697 static void acxusb_s_read_eeprom_version(acx_device_t * adev)
699 u8 eeprom_ver[0x8];
701 memset(eeprom_ver, 0, sizeof(eeprom_ver));
702 acx_s_interrogate(adev, &eeprom_ver, ACX1FF_IE_EEPROM_VER);
704 /* FIXME: which one of those values to take? */
705 adev->eeprom_version = eeprom_ver[5];
710 * temporary helper function to at least fill important cfgopt members with
711 * useful replacement values until we figure out how one manages to fetch
712 * the configoption struct in the USB device case...
714 static int acxusb_s_fill_configoption(acx_device_t * adev)
716 adev->cfgopt_probe_delay = 200;
717 adev->cfgopt_dot11CCAModes = 4;
718 adev->cfgopt_dot11Diversity = 1;
719 adev->cfgopt_dot11ShortPreambleOption = 1;
720 adev->cfgopt_dot11PBCCOption = 1;
721 adev->cfgopt_dot11ChannelAgility = 0;
722 adev->cfgopt_dot11PhyType = 5;
723 adev->cfgopt_dot11TempType = 1;
724 return OK;
727 static const struct ieee80211_ops acxusb_hw_ops = {
728 .tx = acx_i_start_xmit,
729 .conf_tx = acx_net_conf_tx,
730 .add_interface = acx_add_interface,
731 .remove_interface = acx_remove_interface,
732 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
733 .open = acxusb_e_open,
734 .reset = acx_net_reset,
735 .set_multicast_list = acx_i_set_multicast_list,
736 #else
737 .start = acxusb_e_open,
738 .configure_filter = acx_i_set_multicast_list,
739 #endif
740 .stop = acxusb_e_close,
741 .config = acx_net_config,
742 .config_interface = acx_config_interface,
743 .set_key = acx_net_set_key,
744 .get_stats = acx_e_get_stats,
745 .get_tx_stats = acx_net_get_tx_stats,
748 /***********************************************************************
749 ** acxusb_e_probe()
751 ** This function is invoked by the kernel's USB core whenever a new device is
752 ** attached to the system or the module is loaded. It is presented a usb_device
753 ** structure from which information regarding the device is obtained and evaluated.
754 ** In case this driver is able to handle one of the offered devices, it returns
755 ** a non-null pointer to a driver context and thereby claims the device.
759 static int
760 acxusb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
762 struct usb_device *usbdev = interface_to_usbdev(intf);
763 acx_device_t *adev = NULL;
764 struct usb_config_descriptor *config;
765 struct usb_endpoint_descriptor *epdesc;
766 struct usb_host_endpoint *ep;
767 struct usb_interface_descriptor *ifdesc;
768 const char *msg;
769 int numconfigs, numfaces, numep;
770 int result = OK;
771 int i;
772 int err;
773 int radio_type;
774 /* this one needs to be more precise in case there appears
775 * a TNETW1450 from the same vendor */
776 int is_tnetw1450 = (usbdev->descriptor.idVendor != ACX100_VENDOR_ID);
777 struct ieee80211_hw *ieee;
779 FN_ENTER;
781 if (is_tnetw1450) {
782 /* Boot the device (i.e. upload the firmware) */
783 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
785 /* TNETW1450-based cards will continue right away with
786 * the same USB ID after booting */
787 } else {
788 /* First check if this is the "unbooted" hardware */
789 if (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED) {
791 /* Boot the device (i.e. upload the firmware) */
792 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
794 /* DWL-120+ will first boot the firmware,
795 * then later have a *separate* probe() run
796 * since its USB ID will have changed after
797 * firmware boot!
798 * Since the first probe() run has no
799 * other purpose than booting the firmware,
800 * simply return immediately.
802 log(L_INIT,
803 "finished booting, returning from probe()\n");
804 result = OK; /* success */
805 goto end;
806 } else {
807 if (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)
808 /* device not unbooted, but invalid USB ID!? */
809 goto end_nodev;
813 /* Ok, so it's our device and it has already booted */
815 /* Allocate memory for a network device */
817 ieee = ieee80211_alloc_hw(sizeof(*adev), &acxusb_hw_ops);
818 if (!ieee) {
819 msg = "acx: no memory for ieee80211_dev\n";
820 goto end_nomem;
824 ieee->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
825 /* TODO: mainline doesn't support the following flags yet */
827 ~IEEE80211_HW_MONITOR_DURING_OPER &
828 ~IEEE80211_HW_WEP_INCLUDE_IV;
830 ieee->queues = 1;
832 /* Register the callbacks for the network device functions */
835 /* Setup private driver context */
837 adev = ieee2adev(ieee);
838 adev->ieee = ieee;
840 adev->dev_type = DEVTYPE_USB;
841 adev->radio_type = radio_type;
842 if (is_tnetw1450) {
843 /* well, actually it's a TNETW1450, but since it
844 * seems to be sufficiently similar to TNETW1130,
845 * I don't want to change large amounts of code now */
846 adev->chip_type = CHIPTYPE_ACX111;
847 } else {
848 adev->chip_type = CHIPTYPE_ACX100;
851 adev->usbdev = usbdev;
852 spin_lock_init(&adev->spinlock); /* initial state: unlocked */
853 mutex_init(&adev->mutex);
855 /* Check that this is really the hardware we know about.
856 ** If not sure, at least notify the user that he
857 ** may be in trouble...
859 numconfigs = (int)usbdev->descriptor.bNumConfigurations;
860 if (numconfigs != 1)
861 printk("acx: number of configurations is %d, "
862 "this driver only knows how to handle 1, "
863 "be prepared for surprises\n", numconfigs);
865 config = &usbdev->config->desc;
866 numfaces = config->bNumInterfaces;
867 if (numfaces != 1)
868 printk("acx: number of interfaces is %d, "
869 "this driver only knows how to handle 1, "
870 "be prepared for surprises\n", numfaces);
872 ifdesc = &intf->altsetting->desc;
873 numep = ifdesc->bNumEndpoints;
874 log(L_DEBUG, "# of endpoints: %d\n", numep);
876 if (is_tnetw1450) {
877 adev->bulkoutep = 1;
878 adev->bulkinep = 2;
879 } else {
880 /* obtain information about the endpoint
881 ** addresses, begin with some default values
883 adev->bulkoutep = 1;
884 adev->bulkinep = 1;
885 for (i = 0; i < numep; i++) {
886 ep = usbdev->ep_in[i];
887 if (!ep)
888 continue;
889 epdesc = &ep->desc;
890 if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
891 if (epdesc->bEndpointAddress & 0x80)
892 adev->bulkinep =
893 epdesc->bEndpointAddress & 0xF;
894 else
895 adev->bulkoutep =
896 epdesc->bEndpointAddress & 0xF;
900 log(L_DEBUG, "bulkout ep: 0x%X\n", adev->bulkoutep);
901 log(L_DEBUG, "bulkin ep: 0x%X\n", adev->bulkinep);
903 /* already done by memset: adev->rxtruncsize = 0; */
904 log(L_DEBUG, "TXBUFSIZE=%d RXBUFSIZE=%d\n",
905 (int)TXBUFSIZE, (int)RXBUFSIZE);
907 /* Allocate the RX/TX containers. */
908 adev->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX_TX_URB_CNT, GFP_KERNEL);
909 if (!adev->usb_tx) {
910 msg = "acx: no memory for tx container";
911 goto end_nomem;
913 adev->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX_RX_URB_CNT, GFP_KERNEL);
914 if (!adev->usb_rx) {
915 msg = "acx: no memory for rx container";
916 goto end_nomem;
919 /* Setup URBs for bulk-in/out messages */
920 for (i = 0; i < ACX_RX_URB_CNT; i++) {
921 adev->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
922 if (!adev->usb_rx[i].urb) {
923 msg = "acx: no memory for input URB\n";
924 goto end_nomem;
926 adev->usb_rx[i].urb->status = 0;
927 adev->usb_rx[i].adev = adev;
928 adev->usb_rx[i].busy = 0;
931 for (i = 0; i < ACX_TX_URB_CNT; i++) {
932 adev->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
933 if (!adev->usb_tx[i].urb) {
934 msg = "acx: no memory for output URB\n";
935 goto end_nomem;
937 adev->usb_tx[i].urb->status = 0;
938 adev->usb_tx[i].adev = adev;
939 adev->usb_tx[i].busy = 0;
941 adev->tx_free = ACX_TX_URB_CNT;
943 usb_set_intfdata(intf, adev);
944 SET_IEEE80211_DEV(ieee, &intf->dev);
946 /* TODO: move all of fw cmds to open()? But then we won't know our MAC addr
947 until ifup (it's available via reading ACX1xx_IE_DOT11_STATION_ID)... */
949 /* put acx out of sleep mode and initialize it */
950 acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
952 result = acx_s_init_mac(adev);
953 if (result)
954 goto end;
956 /* TODO: see similar code in pci.c */
957 acxusb_s_read_eeprom_version(adev);
958 acxusb_s_fill_configoption(adev);
959 acx_s_set_defaults(adev);
960 acx_s_get_firmware_version(adev);
961 acx_display_hardware_details(adev);
963 /* MAC_COPY(ndev->dev_addr, adev->dev_addr); */
965 err = acx_setup_modes(adev);
966 if (err) {
967 msg = "can't register hwmode\n";
968 goto end_nomem;
971 /* Register the network device */
972 log(L_INIT, "registering network device\n");
973 result = ieee80211_register_hw(adev->ieee);
974 if (result) {
975 msg = "acx: failed to register USB network device "
976 "(error %d)\n";
977 goto end_nomem;
980 acx_proc_register_entries(ieee);
983 printk("acx: USB module " ACX_RELEASE " loaded successfully\n");
985 acx_init_task_scheduler(adev);
987 #if CMD_DISCOVERY
988 great_inquisitor(adev);
989 #endif
991 /* Everything went OK, we are happy now */
992 result = OK;
993 goto end;
995 end_nomem:
996 printk(msg, result);
998 if (ieee) {
999 if (adev->usb_rx) {
1000 for (i = 0; i < ACX_RX_URB_CNT; i++)
1001 usb_free_urb(adev->usb_rx[i].urb);
1002 kfree(adev->usb_rx);
1004 if (adev->usb_tx) {
1005 for (i = 0; i < ACX_TX_URB_CNT; i++)
1006 usb_free_urb(adev->usb_tx[i].urb);
1007 kfree(adev->usb_tx);
1009 ieee80211_free_hw(ieee);
1012 result = -ENOMEM;
1013 goto end;
1015 end_nodev:
1016 /* no device we could handle, return error. */
1017 result = -EIO;
1019 end:
1020 FN_EXIT1(result);
1021 return result;
1025 /***********************************************************************
1026 ** acxusb_e_disconnect()
1028 ** This function is invoked whenever the user pulls the plug from the USB
1029 ** device or the module is removed from the kernel. In these cases, the
1030 ** network devices have to be taken down and all allocated memory has
1031 ** to be freed.
1033 void acxusb_e_disconnect(struct usb_interface *intf)
1035 unsigned long flags;
1036 int i;
1037 acx_device_t *adev = usb_get_intfdata(intf);
1039 FN_ENTER;
1041 /* No WLAN device... no sense */
1042 if (!adev)
1043 goto end;
1045 /* Unregister network device
1047 * If the interface is up, unregister_netdev() will take
1048 * care of calling our close() function, which takes
1049 * care of unlinking the urbs, sending the device to
1050 * sleep, etc...
1051 * This can't be called with sem or lock held because
1052 * _close() will try to grab it as well if it's called,
1053 * deadlocking the machine.
1055 acx_proc_unregister_entries(adev->ieee);
1056 ieee80211_unregister_hw(adev->ieee);
1058 acx_sem_lock(adev);
1059 acx_lock(adev, flags);
1060 /* This device exists no more */
1061 usb_set_intfdata(intf, NULL);
1064 * Here we only free them. _close() took care of
1065 * unlinking them.
1067 for (i = 0; i < ACX_RX_URB_CNT; ++i) {
1068 usb_free_urb(adev->usb_rx[i].urb);
1070 for (i = 0; i < ACX_TX_URB_CNT; ++i) {
1071 usb_free_urb(adev->usb_tx[i].urb);
1074 /* Freeing containers */
1075 kfree(adev->usb_rx);
1076 kfree(adev->usb_tx);
1078 acx_unlock(adev, flags);
1079 acx_sem_unlock(adev);
1081 ieee80211_free_hw(adev->ieee);
1082 end:
1083 FN_EXIT0;
1086 /***********************************************************************
1087 ** acxusb_e_open()
1088 ** This function is called when the user sets up the network interface.
1089 ** It initializes a management timer, sets up the USB card and starts
1090 ** the network tx queue and USB receive.
1092 int acxusb_e_open(struct ieee80211_hw *hw)
1094 acx_device_t *adev = ieee2adev(hw);
1095 unsigned long flags;
1096 int i;
1098 FN_ENTER;
1100 acx_sem_lock(adev);
1102 /* put the ACX100 out of sleep mode */
1103 // acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
1105 init_timer(&adev->mgmt_timer);
1106 adev->mgmt_timer.function = acx_i_timer;
1107 adev->mgmt_timer.data = (unsigned long)adev;
1109 /* acx_s_start needs it */
1110 SET_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1111 acx_s_start(adev);
1113 /* don't acx_start_queue() here, we need to associate first */
1115 acx_lock(adev, flags);
1116 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1117 adev->usb_rx[i].urb->status = 0;
1120 acxusb_l_poll_rx(adev, &adev->usb_rx[0]);
1122 ieee80211_start_queues(adev->ieee);
1123 acx_unlock(adev, flags);
1125 acx_sem_unlock(adev);
1127 FN_EXIT0;
1128 return 0;
1132 /***********************************************************************
1133 ** acxusb_e_close()
1135 ** This function stops the network functionality of the interface (invoked
1136 ** when the user calls ifconfig <wlan> down). The tx queue is halted and
1137 ** the device is marked as down. In case there were any pending USB bulk
1138 ** transfers, these are unlinked (asynchronously). The module in-use count
1139 ** is also decreased in this function.
1141 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1142 int acxusb_e_close(struct ieee80211_hw *hw)
1143 #else
1144 static void acxusb_e_close(struct ieee80211_hw *hw)
1145 #endif
1147 acx_device_t *adev = ieee2adev(hw);
1148 unsigned long flags;
1149 int i;
1151 FN_ENTER;
1153 acx_sem_lock(adev);
1154 if (adev->dev_state_mask & ACX_STATE_IFACE_UP)
1156 // acxusb_e_down(adev);
1157 CLEAR_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1160 /* Code below is remarkably similar to acxpci_s_down(). Maybe we can merge them? */
1162 acx_free_modes(adev);
1164 /* Make sure we don't get any more rx requests */
1165 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_RX, NULL, 0);
1166 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_TX, NULL, 0);
1169 * We must do FLUSH *without* holding sem to avoid a deadlock.
1170 * See pci.c:acxpci_s_down() for deails.
1172 acx_sem_unlock(adev);
1173 flush_scheduled_work();
1174 acx_sem_lock(adev);
1176 /* Power down the device */
1177 acx_s_issue_cmd(adev, ACX1xx_CMD_SLEEP, NULL, 0);
1179 /* Stop the transmit queue, mark the device as DOWN */
1180 acx_lock(adev, flags);
1181 // acx_stop_queue(ndev, "on ifdown");
1182 // acx_set_status(adev, ACX_STATUS_0_STOPPED);
1183 /* stop pending rx/tx urb transfers */
1184 for (i = 0; i < ACX_TX_URB_CNT; i++) {
1185 acxusb_unlink_urb(adev->usb_tx[i].urb);
1186 adev->usb_tx[i].busy = 0;
1188 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1189 acxusb_unlink_urb(adev->usb_rx[i].urb);
1190 adev->usb_rx[i].busy = 0;
1192 adev->tx_free = ACX_TX_URB_CNT;
1193 acx_unlock(adev, flags);
1195 /* Must do this outside of lock */
1196 del_timer_sync(&adev->mgmt_timer);
1198 acx_sem_unlock(adev);
1200 FN_EXIT0;
1201 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1202 return 0;
1203 #endif
1207 /***********************************************************************
1208 ** acxusb_l_poll_rx
1209 ** This function (re)initiates a bulk-in USB transfer on a given urb
1211 void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx)
1213 struct usb_device *usbdev;
1214 struct urb *rxurb;
1215 int errcode, rxnum;
1216 unsigned int inpipe;
1218 FN_ENTER;
1220 rxurb = rx->urb;
1221 usbdev = adev->usbdev;
1223 rxnum = rx - adev->usb_rx;
1225 inpipe = usb_rcvbulkpipe(usbdev, adev->bulkinep);
1226 if (unlikely(rxurb->status == -EINPROGRESS)) {
1227 printk(KERN_ERR
1228 "acx: error, rx triggered while rx urb in progress\n");
1229 /* FIXME: this is nasty, receive is being cancelled by this code
1230 * on the other hand, this should not happen anyway...
1232 usb_unlink_urb(rxurb);
1233 } else if (unlikely(rxurb->status == -ECONNRESET)) {
1234 log(L_USBRXTX, "acx_usb: _poll_rx: connection reset\n");
1235 goto end;
1237 rxurb->actual_length = 0;
1238 usb_fill_bulk_urb(rxurb, usbdev, inpipe, &rx->bulkin, /* dataptr */
1239 RXBUFSIZE, /* size */
1240 acxusb_i_complete_rx, /* handler */
1241 rx /* handler param */
1243 rxurb->transfer_flags = URB_ASYNC_UNLINK;
1245 /* ATOMIC: we may be called from complete_rx() usb callback */
1246 errcode = usb_submit_urb(rxurb, GFP_ATOMIC);
1247 /* FIXME: evaluate the error code! */
1248 log(L_USBRXTX, "SUBMIT RX (%d) inpipe=0x%X size=%d errcode=%d\n",
1249 rxnum, inpipe, (int)RXBUFSIZE, errcode);
1250 end:
1251 FN_EXIT0;
1255 /***********************************************************************
1256 ** acxusb_i_complete_rx()
1257 ** Inputs:
1258 ** urb -> pointer to USB request block
1259 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1261 ** This function is invoked by USB subsystem whenever a bulk receive
1262 ** request returns.
1263 ** The received data is then committed to the network stack and the next
1264 ** USB receive is triggered.
1266 void acxusb_i_complete_rx(struct urb *urb)
1268 acx_device_t *adev;
1269 rxbuffer_t *ptr;
1270 rxbuffer_t *inbuf;
1271 usb_rx_t *rx;
1272 unsigned long flags;
1273 int size, remsize, packetsize, rxnum;
1275 FN_ENTER;
1277 BUG_ON(!urb->context);
1279 rx = (usb_rx_t *) urb->context;
1280 adev = rx->adev;
1282 acx_lock(adev, flags);
1285 * Happens on disconnect or close. Don't play with the urb.
1286 * Don't resubmit it. It will get unlinked by close()
1288 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1289 log(L_USBRXTX, "rx: device is down, not doing anything\n");
1290 goto end_unlock;
1293 inbuf = &rx->bulkin;
1294 size = urb->actual_length;
1295 remsize = size;
1296 rxnum = rx - adev->usb_rx;
1298 log(L_USBRXTX, "RETURN RX (%d) status=%d size=%d\n",
1299 rxnum, urb->status, size);
1301 /* Send the URB that's waiting. */
1302 log(L_USBRXTX, "rxnum=%d, sending=%d\n", rxnum, rxnum ^ 1);
1303 acxusb_l_poll_rx(adev, &adev->usb_rx[rxnum ^ 1]);
1305 if (unlikely(size > sizeof(rxbuffer_t)))
1306 printk("acx_usb: rx too large: %d, please report\n", size);
1308 /* check if the transfer was aborted */
1309 switch (urb->status) {
1310 case 0: /* No error */
1311 break;
1312 case -EOVERFLOW:
1313 printk(KERN_ERR "acx: rx data overrun\n");
1314 adev->rxtruncsize = 0; /* Not valid anymore. */
1315 goto end_unlock;
1316 case -ECONNRESET:
1317 adev->rxtruncsize = 0;
1318 goto end_unlock;
1319 case -ESHUTDOWN: /* rmmod */
1320 adev->rxtruncsize = 0;
1321 goto end_unlock;
1322 default:
1323 adev->rxtruncsize = 0;
1324 adev->stats.rx_errors++;
1325 printk("acx: rx error (urb status=%d)\n", urb->status);
1326 goto end_unlock;
1329 if (unlikely(!size))
1330 printk("acx: warning, encountered zerolength rx packet\n");
1332 if (urb->transfer_buffer != inbuf)
1333 goto end_unlock;
1335 /* check if previous frame was truncated
1336 ** FIXME: this code can only handle truncation
1337 ** of consecutive packets!
1339 ptr = inbuf;
1340 if (adev->rxtruncsize) {
1341 int tail_size;
1343 ptr = &adev->rxtruncbuf;
1344 packetsize = RXBUF_BYTES_USED(ptr);
1345 if (acx_debug & L_USBRXTX) {
1346 printk("handling truncated frame (truncsize=%d size=%d "
1347 "packetsize(from trunc)=%d)\n",
1348 adev->rxtruncsize, size, packetsize);
1349 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1350 acx_dump_bytes(inbuf, RXBUF_HDRSIZE);
1353 /* bytes needed for rxtruncbuf completion: */
1354 tail_size = packetsize - adev->rxtruncsize;
1356 if (size < tail_size) {
1357 /* there is not enough data to complete this packet,
1358 ** simply append the stuff to the truncation buffer
1360 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf, size);
1361 adev->rxtruncsize += size;
1362 remsize = 0;
1363 } else {
1364 /* ok, this data completes the previously
1365 ** truncated packet. copy it into a descriptor
1366 ** and give it to the rest of the stack */
1368 /* append tail to previously truncated part
1369 ** NB: adev->rxtruncbuf (pointed to by ptr) can't
1370 ** overflow because this is already checked before
1371 ** truncation buffer was filled. See below,
1372 ** "if (packetsize > sizeof(rxbuffer_t))..." code */
1373 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf,
1374 tail_size);
1376 if (acx_debug & L_USBRXTX) {
1377 printk("full trailing packet + 12 bytes:\n");
1378 acx_dump_bytes(inbuf,
1379 tail_size + RXBUF_HDRSIZE);
1381 acx_l_process_rxbuf(adev, ptr);
1382 adev->rxtruncsize = 0;
1383 ptr = (rxbuffer_t *) (((char *)inbuf) + tail_size);
1384 remsize -= tail_size;
1386 log(L_USBRXTX, "post-merge size=%d remsize=%d\n",
1387 size, remsize);
1390 /* size = USB data block size
1391 ** remsize = unprocessed USB bytes left
1392 ** ptr = current pos in USB data block
1394 while (remsize) {
1395 if (remsize < RXBUF_HDRSIZE) {
1396 printk("acx: truncated rx header (%d bytes)!\n",
1397 remsize);
1398 if (ACX_DEBUG)
1399 acx_dump_bytes(ptr, remsize);
1400 break;
1403 packetsize = RXBUF_BYTES_USED(ptr);
1404 log(L_USBRXTX, "packet with packetsize=%d\n", packetsize);
1406 if (RXBUF_IS_TXSTAT(ptr)) {
1407 /* do rate handling */
1408 usb_txstatus_t *stat = (void *)ptr;
1410 log(L_USBRXTX, "tx: stat: mac_cnt_rcvd:%04X "
1411 "queue_index:%02X mac_status:%02X hostdata:%08X "
1412 "rate:%u ack_failures:%02X rts_failures:%02X "
1413 "rts_ok:%02X\n",
1414 stat->mac_cnt_rcvd,
1415 stat->queue_index, stat->mac_status, stat->hostdata,
1416 stat->rate, stat->ack_failures, stat->rts_failures,
1417 stat->rts_ok);
1419 if (adev->rate_auto && client_no < VEC_SIZE(adev->sta_list)) {
1420 client_t *clt = &adev->sta_list[client_no];
1421 u16 cur = stat->hostdata >> 16;
1423 if (clt && clt->rate_cur == cur) {
1424 acx_l_handle_txrate_auto(adev, clt,
1425 cur, // intended rate
1426 stat->rate, 0, // actually used rate
1427 stat->mac_status, // error?
1428 ACX_TX_URB_CNT - adev->tx_free);
1431 */ goto next;
1434 if (packetsize > sizeof(rxbuffer_t)) {
1435 printk("acx: packet exceeds max wlan "
1436 "frame size (%d > %d). size=%d\n",
1437 packetsize, (int)sizeof(rxbuffer_t), size);
1438 if (ACX_DEBUG)
1439 acx_dump_bytes(ptr, 16);
1440 /* FIXME: put some real error-handling in here! */
1441 break;
1444 if (packetsize > remsize) {
1445 /* frame truncation handling */
1446 if (acx_debug & L_USBRXTX) {
1447 printk("need to truncate packet, "
1448 "packetsize=%d remsize=%d "
1449 "size=%d bytes:",
1450 packetsize, remsize, size);
1451 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1453 memcpy(&adev->rxtruncbuf, ptr, remsize);
1454 adev->rxtruncsize = remsize;
1455 break;
1458 /* packetsize <= remsize */
1459 /* now handle the received data */
1460 acx_l_process_rxbuf(adev, ptr);
1461 next:
1462 ptr = (rxbuffer_t *) (((char *)ptr) + packetsize);
1463 remsize -= packetsize;
1464 if ((acx_debug & L_USBRXTX) && remsize) {
1465 printk("more than one packet in buffer, "
1466 "second packet hdr:");
1467 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1471 end_unlock:
1472 acx_unlock(adev, flags);
1473 /* end: */
1474 FN_EXIT0;
1478 /***********************************************************************
1479 ** acxusb_i_complete_tx()
1480 ** Inputs:
1481 ** urb -> pointer to USB request block
1482 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1484 ** This function is invoked upon termination of a USB transfer.
1486 void acxusb_i_complete_tx(struct urb *urb)
1488 acx_device_t *adev;
1489 usb_tx_t *tx;
1490 unsigned long flags;
1491 int txnum;
1493 FN_ENTER;
1495 BUG_ON(!urb->context);
1497 tx = (usb_tx_t *) urb->context;
1498 adev = tx->adev;
1500 txnum = tx - adev->usb_tx;
1502 acx_lock(adev, flags);
1505 * If the iface isn't up, we don't have any right
1506 * to play with them. The urb may get unlinked.
1508 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1509 log(L_USBRXTX, "tx: device is down, not doing anything\n");
1510 goto end_unlock;
1513 log(L_USBRXTX, "RETURN TX (%d): status=%d size=%d\n",
1514 txnum, urb->status, urb->actual_length);
1516 /* handle USB transfer errors */
1517 switch (urb->status) {
1518 case 0: /* No error */
1519 break;
1520 case -ESHUTDOWN:
1521 goto end_unlock;
1522 break;
1523 case -ECONNRESET:
1524 goto end_unlock;
1525 break;
1526 /* FIXME: real error-handling code here please */
1527 default:
1528 printk(KERN_ERR "acx: tx error, urb status=%d\n", urb->status);
1529 /* FIXME: real error-handling code here please */
1532 /* free the URB and check for more data */
1533 tx->busy = 0;
1534 adev->tx_free++;
1535 if ((adev->tx_free >= TX_START_QUEUE)
1536 && (adev->status == ACX_STATUS_4_ASSOCIATED)
1537 /* && (acx_queue_stopped(adev->ndev)*/) {
1538 log(L_BUF, "tx: wake queue (%u free txbufs)\n", adev->tx_free);
1539 /* acx_wake_queue(adev->ndev, NULL); */
1542 end_unlock:
1543 acx_unlock(adev, flags);
1544 /* end: */
1545 FN_EXIT0;
1549 /***************************************************************
1550 ** acxusb_l_alloc_tx
1551 ** Actually returns a usb_tx_t* ptr
1553 tx_t *acxusb_l_alloc_tx(acx_device_t * adev)
1555 usb_tx_t *tx;
1556 unsigned head;
1558 FN_ENTER;
1560 head = adev->tx_head;
1561 do {
1562 head = (head + 1) % ACX_TX_URB_CNT;
1563 if (!adev->usb_tx[head].busy) {
1564 log(L_USBRXTX, "allocated tx %d\n", head);
1565 tx = &adev->usb_tx[head];
1566 tx->busy = 1;
1567 adev->tx_free--;
1568 /* Keep a few free descs between head and tail of tx ring.
1569 ** It is not absolutely needed, just feels safer */
1570 if (adev->tx_free < TX_STOP_QUEUE) {
1571 log(L_BUF, "tx: stop queue "
1572 "(%u free txbufs)\n", adev->tx_free);
1573 /* acx_stop_queue(adev->ndev, NULL); */
1575 goto end;
1577 } while (likely(head != adev->tx_head));
1578 tx = NULL;
1579 printk_ratelimited("acx: tx buffers full\n");
1580 end:
1581 adev->tx_head = head;
1582 FN_EXIT0;
1583 return (tx_t *) tx;
1587 /***************************************************************
1588 ** Used if alloc_tx()'ed buffer needs to be cancelled without doing tx
1590 void acxusb_l_dealloc_tx(tx_t * tx_opaque)
1592 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1593 tx->busy = 0;
1597 /***************************************************************
1599 void *acxusb_l_get_txbuf(acx_device_t * adev, tx_t * tx_opaque)
1601 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1602 return &tx->bulkout.data;
1606 /***************************************************************
1607 ** acxusb_l_tx_data
1609 ** Can be called from IRQ (rx -> (AP bridging or mgmt response) -> tx).
1610 ** Can be called from acx_i_start_xmit (data frames from net core).
1612 void acxusb_l_tx_data(acx_device_t * adev, tx_t * tx_opaque, int wlanpkt_len, struct ieee80211_tx_control *ctl,
1613 struct sk_buff* skb)
1615 struct usb_device *usbdev;
1616 struct urb *txurb;
1617 usb_tx_t *tx;
1618 usb_txbuffer_t *txbuf;
1619 // client_t *clt;
1620 struct ieee80211_hdr *whdr;
1621 unsigned int outpipe;
1622 int ucode, txnum;
1624 FN_ENTER;
1626 tx = ((usb_tx_t *) tx_opaque);
1627 txurb = tx->urb;
1628 txbuf = &tx->bulkout;
1629 whdr = (struct ieee80211_hdr *) txbuf->data;
1630 txnum = tx - adev->usb_tx;
1632 log(L_DEBUG, "using buf#%d free=%d len=%d\n",
1633 txnum, adev->tx_free, wlanpkt_len);
1635 switch (adev->mode) {
1636 case ACX_MODE_0_ADHOC:
1637 case ACX_MODE_3_AP:
1638 clt = acx_l_sta_list_get(adev, whdr->a1);
1639 break;
1640 case ACX_MODE_2_STA:
1641 // clt = adev->ap_client;
1642 break;
1643 default:
1644 clt = NULL;
1645 break;
1647 if (unlikely(clt && !clt->rate_cur)) {
1648 printk("acx: driver bug! bad ratemask\n");
1649 goto end;
1653 /* fill the USB transfer header */
1654 txbuf->desc = cpu_to_le16(USB_TXBUF_TXDESC);
1655 txbuf->mpdu_len = cpu_to_le16(wlanpkt_len);
1656 txbuf->queue_index = 1;
1657 txbuf->rate = ctl->tx_rate; //clt->rate_100;
1658 // FIXME(); //This used to have | (clt - adev->ap_client)
1659 txbuf->hostdata = (ctl->tx_rate << 16);
1660 txbuf->ctrl1 = DESC_CTL_FIRSTFRAG;
1661 if (1 == adev->preamble_cur)
1662 SET_BIT(txbuf->ctrl1, DESC_CTL_SHORT_PREAMBLE);
1663 txbuf->ctrl2 = 0;
1664 txbuf->data_len = cpu_to_le16(wlanpkt_len);
1666 if (unlikely(acx_debug & L_DATA)) {
1667 printk("dump of bulk out urb:\n");
1668 acx_dump_bytes(txbuf, wlanpkt_len + USB_TXBUF_HDRSIZE);
1671 if (unlikely(txurb->status == -EINPROGRESS)) {
1672 printk
1673 ("acx: trying to submit tx urb while already in progress\n");
1676 /* now schedule the USB transfer */
1677 usbdev = adev->usbdev;
1678 outpipe = usb_sndbulkpipe(usbdev, adev->bulkoutep);
1680 usb_fill_bulk_urb(txurb, usbdev, outpipe, txbuf, /* dataptr */
1681 wlanpkt_len + USB_TXBUF_HDRSIZE, /* size */
1682 acxusb_i_complete_tx, /* handler */
1683 tx /* handler param */
1686 txurb->transfer_flags = URB_ASYNC_UNLINK | URB_ZERO_PACKET;
1687 ucode = usb_submit_urb(txurb, GFP_ATOMIC);
1688 log(L_USBRXTX, "SUBMIT TX (%d): outpipe=0x%X buf=%p txsize=%d "
1689 "rate=%u errcode=%d\n", txnum, outpipe, txbuf,
1690 wlanpkt_len + USB_TXBUF_HDRSIZE, txbuf->rate, ucode);
1692 if (unlikely(ucode)) {
1693 printk(KERN_ERR "acx: submit_urb() error=%d txsize=%d\n",
1694 ucode, wlanpkt_len + USB_TXBUF_HDRSIZE);
1696 /* on error, just mark the frame as done and update
1697 ** the statistics
1699 adev->stats.tx_errors++;
1700 tx->busy = 0;
1701 adev->tx_free++;
1702 /* needed? if (adev->tx_free > TX_START_QUEUE) acx_wake_queue(...) */
1704 FN_EXIT0;
1708 /***********************************************************************
1709 static void acxusb_i_set_rx_mode(struct net_device *ndev)
1715 /***********************************************************************
1717 #ifdef HAVE_TX_TIMEOUT
1719 void acxusb_i_tx_timeout(struct net_device *ndev)
1721 acx_device_t *adev = ndev2adev(ndev);
1722 unsigned long flags;
1723 int i;
1725 FN_ENTER;
1727 acx_lock(adev, flags);
1728 */ /* unlink the URBs */
1729 /* for (i = 0; i < ACX_TX_URB_CNT; i++) {
1730 acxusb_unlink_urb(adev->usb_tx[i].urb);
1731 adev->usb_tx[i].busy = 0;
1733 adev->tx_free = ACX_TX_URB_CNT;
1734 */ /* TODO: stats update */
1735 /* acx_unlock(adev, flags);
1737 FN_EXIT0;
1740 #endif
1743 /***********************************************************************
1744 ** init_module()
1746 ** This function is invoked upon loading of the kernel module.
1747 ** It registers itself at the kernel's USB subsystem.
1749 ** Returns: Errorcode on failure, 0 on success
1751 int __init acxusb_e_init_module(void)
1753 log(L_INIT, "USB module " ACX_RELEASE " initialized, "
1754 "probing for devices...\n");
1755 return usb_register(&acxusb_driver);
1760 /***********************************************************************
1761 ** cleanup_module()
1763 ** This function is invoked as last step of the module unloading. It simply
1764 ** deregisters this module at the kernel's USB subsystem.
1766 void __exit acxusb_e_cleanup_module(void)
1768 usb_deregister(&acxusb_driver);
1769 log(L_INIT, "USB module " ACX_RELEASE " unloaded\n");
1773 /***********************************************************************
1774 ** DEBUG STUFF
1776 #if ACX_DEBUG
1778 #ifdef UNUSED
1779 static void dump_device(struct usb_device *usbdev)
1781 int i;
1782 struct usb_config_descriptor *cd;
1784 printk("acx device dump:\n");
1785 printk(" devnum: %d\n", usbdev->devnum);
1786 printk(" speed: %d\n", usbdev->speed);
1787 printk(" tt: 0x%X\n", (unsigned int)(usbdev->tt));
1788 printk(" ttport: %d\n", (unsigned int)(usbdev->ttport));
1789 printk(" toggle[0]: 0x%X toggle[1]: 0x%X\n",
1790 (unsigned int)(usbdev->toggle[0]),
1791 (unsigned int)(usbdev->toggle[1]));
1792 /* This saw a change after 2.6.10 */
1793 printk(" ep_in wMaxPacketSize: ");
1794 for (i = 0; i < 16; ++i)
1795 if (usbdev->ep_in[i] != NULL)
1796 printk("%d:%d ", i,
1797 usbdev->ep_in[i]->desc.wMaxPacketSize);
1798 printk("\n");
1799 printk(" ep_out wMaxPacketSize: ");
1800 for (i = 0; i < ARRAY_SIZE(usbdev->ep_out); ++i)
1801 if (usbdev->ep_out[i] != NULL)
1802 printk("%d:%d ", i,
1803 usbdev->ep_out[i]->desc.wMaxPacketSize);
1804 printk("\n");
1805 printk(" parent: 0x%X\n", (unsigned int)usbdev->parent);
1806 printk(" bus: 0x%X\n", (unsigned int)usbdev->bus);
1807 #ifdef NO_DATATYPE
1808 printk(" configs: ");
1809 for (i = 0; i < usbdev->descriptor.bNumConfigurations; i++)
1810 printk("0x%X ", usbdev->config[i]);
1811 printk("\n");
1812 #endif
1813 printk(" actconfig: %p\n", usbdev->actconfig);
1814 dump_device_descriptor(&usbdev->descriptor);
1816 cd = &usbdev->config->desc;
1817 dump_config_descriptor(cd);
1821 /***********************************************************************
1823 static void dump_config_descriptor(struct usb_config_descriptor *cd)
1825 printk("Configuration Descriptor:\n");
1826 if (!cd) {
1827 printk("NULL\n");
1828 return;
1830 printk(" bLength: %d (0x%X)\n", cd->bLength, cd->bLength);
1831 printk(" bDescriptorType: %d (0x%X)\n", cd->bDescriptorType,
1832 cd->bDescriptorType);
1833 printk(" bNumInterfaces: %d (0x%X)\n", cd->bNumInterfaces,
1834 cd->bNumInterfaces);
1835 printk(" bConfigurationValue: %d (0x%X)\n", cd->bConfigurationValue,
1836 cd->bConfigurationValue);
1837 printk(" iConfiguration: %d (0x%X)\n", cd->iConfiguration,
1838 cd->iConfiguration);
1839 printk(" bmAttributes: %d (0x%X)\n", cd->bmAttributes,
1840 cd->bmAttributes);
1841 /* printk(" MaxPower: %d (0x%X)\n", cd->bMaxPower, cd->bMaxPower); */
1845 static void dump_device_descriptor(struct usb_device_descriptor *dd)
1847 printk("Device Descriptor:\n");
1848 if (!dd) {
1849 printk("NULL\n");
1850 return;
1852 printk(" bLength: %d (0x%X)\n", dd->bLength, dd->bLength);
1853 printk(" bDescriptortype: %d (0x%X)\n", dd->bDescriptorType,
1854 dd->bDescriptorType);
1855 printk(" bcdUSB: %d (0x%X)\n", dd->bcdUSB, dd->bcdUSB);
1856 printk(" bDeviceClass: %d (0x%X)\n", dd->bDeviceClass,
1857 dd->bDeviceClass);
1858 printk(" bDeviceSubClass: %d (0x%X)\n", dd->bDeviceSubClass,
1859 dd->bDeviceSubClass);
1860 printk(" bDeviceProtocol: %d (0x%X)\n", dd->bDeviceProtocol,
1861 dd->bDeviceProtocol);
1862 printk(" bMaxPacketSize0: %d (0x%X)\n", dd->bMaxPacketSize0,
1863 dd->bMaxPacketSize0);
1864 printk(" idVendor: %d (0x%X)\n", dd->idVendor, dd->idVendor);
1865 printk(" idProduct: %d (0x%X)\n", dd->idProduct, dd->idProduct);
1866 printk(" bcdDevice: %d (0x%X)\n", dd->bcdDevice, dd->bcdDevice);
1867 printk(" iManufacturer: %d (0x%X)\n", dd->iManufacturer,
1868 dd->iManufacturer);
1869 printk(" iProduct: %d (0x%X)\n", dd->iProduct, dd->iProduct);
1870 printk(" iSerialNumber: %d (0x%X)\n", dd->iSerialNumber,
1871 dd->iSerialNumber);
1872 printk(" bNumConfigurations: %d (0x%X)\n", dd->bNumConfigurations,
1873 dd->bNumConfigurations);
1875 #endif /* UNUSED */
1877 #endif /* ACX_DEBUG */