thanks to Matthew "mentor" Bell for finding a big bug in the code
[acx-mac80211.git] / usb.c
bloba6e335f80da87c70e8a234d8000e732c8b898d82
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 void acxusb_put_devname(acx_device_t *adev, struct ethtool_drvinfo *info)
145 usb_make_path(adev->usbdev, info->bus_info, sizeof info->bus_info);
147 /***********************************************************************
148 ** USB helper
150 ** ldd3 ch13 says:
151 ** When the function is usb_kill_urb, the urb lifecycle is stopped. This
152 ** function is usually used when the device is disconnected from the system,
153 ** in the disconnect callback. For some drivers, the usb_unlink_urb function
154 ** should be used to tell the USB core to stop an urb. This function does not
155 ** wait for the urb to be fully stopped before returning to the caller.
156 ** This is useful for stoppingthe urb while in an interrupt handler or when
157 ** a spinlock is held, as waiting for a urb to fully stop requires the ability
158 ** for the USB core to put the calling process to sleep. This function requires
159 ** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
160 ** to be stopped in order to work properly.
162 ** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
163 ** asynchronous while usb_kill_urb is synchronous and should be called
164 ** directly (drivers/usb/core/urb.c))
166 ** In light of this, timeout is just for paranoid reasons...
168 ** Actually, it's useful for debugging. If we reach timeout, we're doing
169 ** something wrong with the urbs.
171 static void acxusb_unlink_urb(struct urb *urb)
173 if (!urb)
174 return;
176 if (urb->status == -EINPROGRESS) {
177 int timeout = 10;
179 usb_unlink_urb(urb);
180 while (--timeout && urb->status == -EINPROGRESS) {
181 mdelay(1);
183 if (!timeout) {
184 printk(KERN_ERR "acx_usb: urb unlink timeout!\n");
190 /***********************************************************************
191 ** EEPROM and PHY read/write helpers
193 /***********************************************************************
194 ** acxusb_s_read_phy_reg
196 int acxusb_s_read_phy_reg(acx_device_t * adev, u32 reg, u8 * charbuf)
198 /* mem_read_write_t mem; */
200 FN_ENTER;
202 printk("%s doesn't seem to work yet, disabled.\n", __func__);
205 mem.addr = cpu_to_le16(reg);
206 mem.type = cpu_to_le16(0x82);
207 mem.len = cpu_to_le32(4);
208 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
209 *charbuf = mem.data;
210 log(L_DEBUG, "read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
213 FN_EXIT1(OK);
214 return OK;
218 /***********************************************************************
220 int acxusb_s_write_phy_reg(acx_device_t * adev, u32 reg, u8 value)
222 mem_read_write_t mem;
224 FN_ENTER;
226 mem.addr = cpu_to_le16(reg);
227 mem.type = cpu_to_le16(0x82);
228 mem.len = cpu_to_le32(4);
229 mem.data = value;
230 acx_s_issue_cmd(adev, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
231 log(L_DEBUG, "write radio PHY[0x%04X]=0x%02X\n", reg, value);
233 FN_EXIT1(OK);
234 return OK;
238 /***********************************************************************
239 ** acxusb_s_issue_cmd_timeo
240 ** Excecutes a command in the command mailbox
242 ** buffer = a pointer to the data.
243 ** The data must not include 4 byte command header
246 /* TODO: ideally we shall always know how much we need
247 ** and this shall be 0 */
248 #define BOGUS_SAFETY_PADDING 0x40
250 #undef FUNC
251 #define FUNC "issue_cmd"
253 #if !ACX_DEBUG
255 acxusb_s_issue_cmd_timeo(acx_device_t * adev,
256 unsigned cmd,
257 void *buffer, unsigned buflen, unsigned timeout)
259 #else
261 acxusb_s_issue_cmd_timeo_debug(acx_device_t * adev,
262 unsigned cmd,
263 void *buffer,
264 unsigned buflen,
265 unsigned timeout, const char *cmdstr)
267 #endif
268 /* USB ignores timeout param */
270 struct usb_device *usbdev;
271 struct {
272 u16 cmd;
273 u16 status;
274 u8 data[1];
275 } ACX_PACKED *loc;
276 const char *devname;
277 int acklen, blocklen, inpipe, outpipe;
278 int cmd_status;
279 int result;
281 FN_ENTER;
283 devname = wiphy_name(adev->ieee->wiphy);
284 /* no "wlan%%d: ..." please */
285 if (!devname || !devname[0] || devname[4] == '%')
286 devname = "acx";
288 log(L_CTL, FUNC "(cmd:%s,buflen:%u,type:0x%04X)\n",
289 cmdstr, buflen,
290 buffer ? le16_to_cpu(((acx_ie_generic_t *) buffer)->type) : -1);
292 loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
293 if (!loc) {
294 printk("%s: " FUNC "(): no memory for data buffer\n", devname);
295 goto bad;
298 /* get context from acx_device */
299 usbdev = adev->usbdev;
301 /* check which kind of command was issued */
302 loc->cmd = cpu_to_le16(cmd);
303 loc->status = 0;
305 /* NB: buflen == frmlen + 4
307 ** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
308 ** read (cmd,status,rid,frmlen,data[frmlen]) back
310 ** Configure: write (cmd,status,rid,frmlen,data[frmlen])
312 ** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
315 /* now write the parameters of the command if needed */
316 acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
317 blocklen = buflen;
318 if (buffer && buflen) {
319 /* if it's an INTERROGATE command, just pass the length
320 * of parameters to read, as data */
321 if (cmd == ACX1xx_CMD_INTERROGATE) {
322 blocklen = 4;
323 acklen = buflen + 4;
325 memcpy(loc->data, buffer, blocklen);
327 blocklen += 4; /* account for cmd,status */
329 /* obtain the I/O pipes */
330 outpipe = usb_sndctrlpipe(usbdev, 0);
331 inpipe = usb_rcvctrlpipe(usbdev, 0);
332 log(L_CTL, "ctrl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
333 log(L_CTL, "sending USB control msg (out) (blocklen=%d)\n", blocklen);
334 if (acx_debug & L_DATA)
335 acx_dump_bytes(loc, blocklen);
337 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_CMD, /* request */
338 USB_TYPE_VENDOR | USB_DIR_OUT, /* requesttype */
339 0, /* value */
340 0, /* index */
341 loc, /* dataptr */
342 blocklen, /* size */
343 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
346 if (result == -ENODEV) {
347 log(L_CTL, "no device present (unplug?)\n");
348 goto good;
351 log(L_CTL, "wrote %d bytes\n", result);
352 if (result < 0) {
353 goto bad;
356 /* check for device acknowledge */
357 log(L_CTL, "sending USB control msg (in) (acklen=%d)\n", acklen);
358 loc->status = 0; /* delete old status flag -> set to IDLE */
359 /* shall we zero out the rest? */
360 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_CMD, /* request */
361 USB_TYPE_VENDOR | USB_DIR_IN, /* requesttype */
362 0, /* value */
363 0, /* index */
364 loc, /* dataptr */
365 acklen, /* size */
366 ACX_USB_CTRL_TIMEOUT /* timeout in ms */
368 if (result < 0) {
369 printk("%s: " FUNC "(): USB read error %d\n", devname, result);
370 goto bad;
372 if (acx_debug & L_CTL) {
373 printk("read %d bytes: ", result);
374 acx_dump_bytes(loc, result);
378 check for result==buflen+4? Was seen:
380 interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
381 issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
382 ctrl inpipe=0x80000280 outpipe=0x80000200
383 sending USB control msg (out) (blocklen=8)
384 01 00 00 00 0F 10 04 00
385 wrote 8 bytes
386 sending USB control msg (in) (acklen=12) sizeof(loc->data
387 read 4 bytes <==== MUST BE 12!!
390 cmd_status = le16_to_cpu(loc->status);
391 if (cmd_status != 1) {
392 printk("%s: " FUNC "(): cmd_status is not SUCCESS: %d (%s)\n",
393 devname, cmd_status, acx_cmd_status_str(cmd_status));
394 /* TODO: goto bad; ? */
396 if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
397 memcpy(buffer, loc->data, buflen);
398 log(L_CTL, "response frame: cmd=0x%04X status=%d\n",
399 le16_to_cpu(loc->cmd), cmd_status);
401 good:
402 kfree(loc);
403 FN_EXIT1(OK);
404 return OK;
405 bad:
406 /* Give enough info so that callers can avoid
407 ** printing their own diagnostic messages */
408 #if ACX_DEBUG
409 printk("%s: " FUNC "(cmd:%s) FAILED\n", devname, cmdstr);
410 #else
411 printk("%s: " FUNC "(cmd:0x%04X) FAILED\n", devname, cmd);
412 #endif
413 dump_stack();
414 kfree(loc);
415 FN_EXIT1(NOT_OK);
416 return NOT_OK;
420 /***********************************************************************
421 ** acxusb_boot()
422 ** Inputs:
423 ** usbdev -> Pointer to kernel's usb_device structure
425 ** Returns:
426 ** (int) Errorcode or 0 on success
428 ** This function triggers the loading of the firmware image from harddisk
429 ** and then uploads the firmware to the USB device. After uploading the
430 ** firmware and transmitting the checksum, the device resets and appears
431 ** as a new device on the USB bus (the device we can finally deal with)
433 static inline int
434 acxusb_fw_needs_padding(firmware_image_t *fw_image, unsigned int usb_maxlen)
436 unsigned int num_xfers = ((fw_image->size - 1) / usb_maxlen) + 1;
438 return ((num_xfers % 2) == 0);
441 static int
442 acxusb_boot(struct usb_device *usbdev, int is_tnetw1450, int *radio_type)
444 char filename[sizeof("tiacx1NNusbcRR")];
446 firmware_image_t *fw_image = NULL;
447 char *usbbuf;
448 unsigned int offset;
449 unsigned int blk_len, inpipe, outpipe;
450 u32 num_processed;
451 u32 img_checksum, sum;
452 u32 file_size;
453 int result = -EIO;
454 int i;
456 FN_ENTER;
458 /* dump_device(usbdev); */
460 usbbuf = kmalloc(USB_RWMEM_MAXLEN, GFP_KERNEL);
461 if (!usbbuf) {
462 printk(KERN_ERR
463 "acx: no memory for USB transfer buffer (%d bytes)\n",
464 USB_RWMEM_MAXLEN);
465 result = -ENOMEM;
466 goto end;
468 if (is_tnetw1450) {
469 /* Obtain the I/O pipes */
470 outpipe = usb_sndbulkpipe(usbdev, 1);
471 inpipe = usb_rcvbulkpipe(usbdev, 2);
473 printk(KERN_DEBUG "wait for device ready\n");
474 for (i = 0; i <= 2; i++) {
475 result = usb_bulk_msg(usbdev, inpipe,
476 usbbuf,
477 USB_RWMEM_MAXLEN,
478 &num_processed, 2000);
480 if ((*(u32 *) & usbbuf[4] == 0x40000001)
481 && (*(u16 *) & usbbuf[2] == 0x1)
482 && ((*(u16 *) usbbuf & 0x3fff) == 0)
483 && ((*(u16 *) usbbuf & 0xc000) == 0xc000))
484 break;
485 acx_s_mwait(10);
487 if (i == 2)
488 goto fw_end;
490 *radio_type = usbbuf[8];
491 } else {
492 /* Obtain the I/O pipes */
493 outpipe = usb_sndctrlpipe(usbdev, 0);
494 inpipe = usb_rcvctrlpipe(usbdev, 0);
496 /* FIXME: shouldn't be hardcoded */
497 *radio_type = RADIO_MAXIM_0D;
500 snprintf(filename, sizeof(filename), "tiacx1%02dusbc%02X",
501 is_tnetw1450 * 11, *radio_type);
503 fw_image = acx_s_read_fw(&usbdev->dev, filename, &file_size);
504 if (!fw_image) {
505 result = -EIO;
506 goto end;
508 log(L_INIT, "firmware size: %d bytes\n", file_size);
510 img_checksum = le32_to_cpu(fw_image->chksum);
512 if (is_tnetw1450) {
513 u8 cmdbuf[20];
514 const u8 *p;
515 u8 need_padding;
516 u32 tmplen, val;
518 memset(cmdbuf, 0, 16);
520 need_padding =
521 acxusb_fw_needs_padding(fw_image, USB_RWMEM_MAXLEN);
522 tmplen = need_padding ? file_size - 4 : file_size - 8;
523 *(u16 *) & cmdbuf[0] = 0xc000;
524 *(u16 *) & cmdbuf[2] = 0x000b;
525 *(u32 *) & cmdbuf[4] = tmplen;
526 *(u32 *) & cmdbuf[8] = file_size - 8;
527 *(u32 *) & cmdbuf[12] = img_checksum;
529 result =
530 usb_bulk_msg(usbdev, outpipe, cmdbuf, 16, &num_processed,
531 HZ);
532 if (result < 0)
533 goto fw_end;
535 p = (const u8 *)&fw_image->size;
537 /* first calculate checksum for image size part */
538 sum = p[0] + p[1] + p[2] + p[3];
539 p += 4;
541 /* now continue checksum for firmware data part */
542 tmplen = le32_to_cpu(fw_image->size);
543 for (i = 0; i < tmplen /* image size */ ; i++) {
544 sum += *p++;
547 if (sum != le32_to_cpu(fw_image->chksum)) {
548 printk("acx: FATAL: firmware upload: "
549 "checksums don't match! "
550 "(0x%08x vs. 0x%08x)\n", sum, fw_image->chksum);
551 goto fw_end;
554 offset = 8;
555 while (offset < file_size) {
556 blk_len = file_size - offset;
557 if (blk_len > USB_RWMEM_MAXLEN) {
558 blk_len = USB_RWMEM_MAXLEN;
561 log(L_INIT,
562 "uploading firmware (%d bytes, offset=%d)\n",
563 blk_len, offset);
564 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
566 p = usbbuf;
567 for (i = 0; i < blk_len; i += 4) {
568 *(u32 *) p = be32_to_cpu(*(u32 *) p);
569 p += 4;
572 result =
573 usb_bulk_msg(usbdev, outpipe, usbbuf, blk_len,
574 &num_processed, HZ);
575 if ((result < 0) || (num_processed != blk_len))
576 goto fw_end;
577 offset += blk_len;
579 if (need_padding) {
580 printk(KERN_DEBUG "send padding\n");
581 memset(usbbuf, 0, 4);
582 result =
583 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
584 &num_processed, HZ);
585 if ((result < 0) || (num_processed != 4))
586 goto fw_end;
588 printk(KERN_DEBUG "read firmware upload result\n");
589 memset(cmdbuf, 0, 20); /* additional memset */
590 result =
591 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20, &num_processed,
592 2000);
593 if (result < 0)
594 goto fw_end;
595 if (*(u32 *) & cmdbuf[4] == 0x40000003)
596 goto fw_end;
597 if (*(u32 *) & cmdbuf[4])
598 goto fw_end;
599 if (*(u16 *) & cmdbuf[16] != 1)
600 goto fw_end;
602 val = *(u32 *) & cmdbuf[0];
603 if ((val & 0x3fff)
604 || ((val & 0xc000) != 0xc000))
605 goto fw_end;
607 val = *(u32 *) & cmdbuf[8];
608 if (val & 2) {
609 result =
610 usb_bulk_msg(usbdev, inpipe, cmdbuf, 20,
611 &num_processed, 2000);
612 if (result < 0)
613 goto fw_end;
614 val = *(u32 *) & cmdbuf[8];
616 /* yup, no "else" here! */
617 if (val & 1) {
618 memset(usbbuf, 0, 4);
619 result =
620 usb_bulk_msg(usbdev, outpipe, usbbuf, 4,
621 &num_processed, HZ);
622 if ((result < 0) || (!num_processed))
623 goto fw_end;
626 printk("TNETW1450 firmware upload successful!\n");
627 result = 0;
628 goto end;
629 fw_end:
630 result = -EIO;
631 goto end;
632 } else {
633 /* ACX100 USB */
635 /* now upload the firmware, slice the data into blocks */
636 offset = 8;
637 while (offset < file_size) {
638 blk_len = file_size - offset;
639 if (blk_len > USB_RWMEM_MAXLEN) {
640 blk_len = USB_RWMEM_MAXLEN;
642 log(L_INIT,
643 "uploading firmware (%d bytes, offset=%d)\n",
644 blk_len, offset);
645 memcpy(usbbuf, ((u8 *) fw_image) + offset, blk_len);
646 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, (file_size - 8) & 0xffff, /* value */
647 (file_size - 8) >> 16, /* index */
648 usbbuf, /* dataptr */
649 blk_len, /* size */
650 3000 /* timeout in ms */
652 offset += blk_len;
653 if (result < 0) {
654 printk(KERN_ERR "acx: error %d during upload "
655 "of firmware, aborting\n", result);
656 goto end;
660 /* finally, send the checksum and reboot the device */
661 /* does this trigger the reboot? */
662 result = usb_control_msg(usbdev, outpipe, ACX_USB_REQ_UPLOAD_FW, USB_TYPE_VENDOR | USB_DIR_OUT, img_checksum & 0xffff, /* value */
663 img_checksum >> 16, /* index */
664 NULL, /* dataptr */
665 0, /* size */
666 3000 /* timeout in ms */
668 if (result < 0) {
669 printk(KERN_ERR "acx: error %d during tx of checksum, "
670 "aborting\n", result);
671 goto end;
673 result = usb_control_msg(usbdev, inpipe, ACX_USB_REQ_ACK_CS, USB_TYPE_VENDOR | USB_DIR_IN, img_checksum & 0xffff, /* value */
674 img_checksum >> 16, /* index */
675 usbbuf, /* dataptr */
676 8, /* size */
677 3000 /* timeout in ms */
679 if (result < 0) {
680 printk(KERN_ERR "acx: error %d during ACK of checksum, "
681 "aborting\n", result);
682 goto end;
684 if (*usbbuf != 0x10) {
685 printk(KERN_ERR "acx: invalid checksum?\n");
686 result = -EINVAL;
687 goto end;
689 result = 0;
692 end:
693 vfree(fw_image);
694 kfree(usbbuf);
696 FN_EXIT1(result);
697 return result;
701 /* FIXME: maybe merge it with usual eeprom reading, into common code? */
702 static void acxusb_s_read_eeprom_version(acx_device_t * adev)
704 u8 eeprom_ver[0x8];
706 memset(eeprom_ver, 0, sizeof(eeprom_ver));
707 acx_s_interrogate(adev, &eeprom_ver, ACX1FF_IE_EEPROM_VER);
709 /* FIXME: which one of those values to take? */
710 adev->eeprom_version = eeprom_ver[5];
715 * temporary helper function to at least fill important cfgopt members with
716 * useful replacement values until we figure out how one manages to fetch
717 * the configoption struct in the USB device case...
719 static int acxusb_s_fill_configoption(acx_device_t * adev)
721 adev->cfgopt_probe_delay = 200;
722 adev->cfgopt_dot11CCAModes = 4;
723 adev->cfgopt_dot11Diversity = 1;
724 adev->cfgopt_dot11ShortPreambleOption = 1;
725 adev->cfgopt_dot11PBCCOption = 1;
726 adev->cfgopt_dot11ChannelAgility = 0;
727 adev->cfgopt_dot11PhyType = 5;
728 adev->cfgopt_dot11TempType = 1;
729 return OK;
732 static const struct ieee80211_ops acxusb_hw_ops = {
733 .tx = acx_i_start_xmit,
734 .conf_tx = acx_net_conf_tx,
735 .add_interface = acx_add_interface,
736 .remove_interface = acx_remove_interface,
737 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
738 .open = acxusb_e_open,
739 .reset = acx_net_reset,
740 .set_multicast_list = acx_i_set_multicast_list,
741 #else
742 .start = acxusb_e_open,
743 .configure_filter = acx_i_set_multicast_list,
744 #endif
745 .stop = acxusb_e_close,
746 .config = acx_net_config,
747 .config_interface = acx_config_interface,
748 .set_key = acx_net_set_key,
749 .get_stats = acx_e_get_stats,
750 .get_tx_stats = acx_net_get_tx_stats,
753 /***********************************************************************
754 ** acxusb_e_probe()
756 ** This function is invoked by the kernel's USB core whenever a new device is
757 ** attached to the system or the module is loaded. It is presented a usb_device
758 ** structure from which information regarding the device is obtained and evaluated.
759 ** In case this driver is able to handle one of the offered devices, it returns
760 ** a non-null pointer to a driver context and thereby claims the device.
764 static int
765 acxusb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
767 struct usb_device *usbdev = interface_to_usbdev(intf);
768 acx_device_t *adev = NULL;
769 struct usb_config_descriptor *config;
770 struct usb_endpoint_descriptor *epdesc;
771 struct usb_host_endpoint *ep;
772 struct usb_interface_descriptor *ifdesc;
773 const char *msg;
774 int numconfigs, numfaces, numep;
775 int result = OK;
776 int i;
777 int radio_type;
778 /* this one needs to be more precise in case there appears
779 * a TNETW1450 from the same vendor */
780 int is_tnetw1450 = (usbdev->descriptor.idVendor != ACX100_VENDOR_ID);
781 struct ieee80211_hw *ieee;
783 FN_ENTER;
785 if (is_tnetw1450) {
786 /* Boot the device (i.e. upload the firmware) */
787 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
789 /* TNETW1450-based cards will continue right away with
790 * the same USB ID after booting */
791 } else {
792 /* First check if this is the "unbooted" hardware */
793 if (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED) {
795 /* Boot the device (i.e. upload the firmware) */
796 acxusb_boot(usbdev, is_tnetw1450, &radio_type);
798 /* DWL-120+ will first boot the firmware,
799 * then later have a *separate* probe() run
800 * since its USB ID will have changed after
801 * firmware boot!
802 * Since the first probe() run has no
803 * other purpose than booting the firmware,
804 * simply return immediately.
806 log(L_INIT,
807 "finished booting, returning from probe()\n");
808 result = OK; /* success */
809 goto end;
810 } else {
811 if (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)
812 /* device not unbooted, but invalid USB ID!? */
813 goto end_nodev;
817 /* Ok, so it's our device and it has already booted */
819 /* Allocate memory for a network device */
821 ieee = ieee80211_alloc_hw(sizeof(*adev), &acxusb_hw_ops);
822 if (!ieee) {
823 msg = "acx: no memory for ieee80211_dev\n";
824 goto end_nomem;
828 ieee->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
829 /* TODO: mainline doesn't support the following flags yet */
831 ~IEEE80211_HW_MONITOR_DURING_OPER &
832 ~IEEE80211_HW_WEP_INCLUDE_IV;
834 ieee->queues = 1;
836 /* Register the callbacks for the network device functions */
839 /* Setup private driver context */
841 adev = ieee2adev(ieee);
842 adev->ieee = ieee;
844 adev->dev_type = DEVTYPE_USB;
845 adev->radio_type = radio_type;
846 if (is_tnetw1450) {
847 /* well, actually it's a TNETW1450, but since it
848 * seems to be sufficiently similar to TNETW1130,
849 * I don't want to change large amounts of code now */
850 adev->chip_type = CHIPTYPE_ACX111;
851 } else {
852 adev->chip_type = CHIPTYPE_ACX100;
855 adev->usbdev = usbdev;
856 spin_lock_init(&adev->spinlock); /* initial state: unlocked */
857 mutex_init(&adev->mutex);
859 /* Check that this is really the hardware we know about.
860 ** If not sure, at least notify the user that he
861 ** may be in trouble...
863 numconfigs = (int)usbdev->descriptor.bNumConfigurations;
864 if (numconfigs != 1)
865 printk("acx: number of configurations is %d, "
866 "this driver only knows how to handle 1, "
867 "be prepared for surprises\n", numconfigs);
869 config = &usbdev->config->desc;
870 numfaces = config->bNumInterfaces;
871 if (numfaces != 1)
872 printk("acx: number of interfaces is %d, "
873 "this driver only knows how to handle 1, "
874 "be prepared for surprises\n", numfaces);
876 ifdesc = &intf->altsetting->desc;
877 numep = ifdesc->bNumEndpoints;
878 log(L_DEBUG, "# of endpoints: %d\n", numep);
880 if (is_tnetw1450) {
881 adev->bulkoutep = 1;
882 adev->bulkinep = 2;
883 } else {
884 /* obtain information about the endpoint
885 ** addresses, begin with some default values
887 adev->bulkoutep = 1;
888 adev->bulkinep = 1;
889 for (i = 0; i < numep; i++) {
890 ep = usbdev->ep_in[i];
891 if (!ep)
892 continue;
893 epdesc = &ep->desc;
894 if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
895 if (epdesc->bEndpointAddress & 0x80)
896 adev->bulkinep =
897 epdesc->bEndpointAddress & 0xF;
898 else
899 adev->bulkoutep =
900 epdesc->bEndpointAddress & 0xF;
904 log(L_DEBUG, "bulkout ep: 0x%X\n", adev->bulkoutep);
905 log(L_DEBUG, "bulkin ep: 0x%X\n", adev->bulkinep);
907 /* already done by memset: adev->rxtruncsize = 0; */
908 log(L_DEBUG, "TXBUFSIZE=%d RXBUFSIZE=%d\n",
909 (int)TXBUFSIZE, (int)RXBUFSIZE);
911 /* Allocate the RX/TX containers. */
912 adev->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX_TX_URB_CNT, GFP_KERNEL);
913 if (!adev->usb_tx) {
914 msg = "acx: no memory for tx container";
915 goto end_nomem;
917 adev->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX_RX_URB_CNT, GFP_KERNEL);
918 if (!adev->usb_rx) {
919 msg = "acx: no memory for rx container";
920 goto end_nomem;
923 /* Setup URBs for bulk-in/out messages */
924 for (i = 0; i < ACX_RX_URB_CNT; i++) {
925 adev->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
926 if (!adev->usb_rx[i].urb) {
927 msg = "acx: no memory for input URB\n";
928 goto end_nomem;
930 adev->usb_rx[i].urb->status = 0;
931 adev->usb_rx[i].adev = adev;
932 adev->usb_rx[i].busy = 0;
935 for (i = 0; i < ACX_TX_URB_CNT; i++) {
936 adev->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
937 if (!adev->usb_tx[i].urb) {
938 msg = "acx: no memory for output URB\n";
939 goto end_nomem;
941 adev->usb_tx[i].urb->status = 0;
942 adev->usb_tx[i].adev = adev;
943 adev->usb_tx[i].busy = 0;
945 adev->tx_free = ACX_TX_URB_CNT;
947 usb_set_intfdata(intf, adev);
948 SET_IEEE80211_DEV(ieee, &intf->dev);
950 /* TODO: move all of fw cmds to open()? But then we won't know our MAC addr
951 until ifup (it's available via reading ACX1xx_IE_DOT11_STATION_ID)... */
953 /* put acx out of sleep mode and initialize it */
954 acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
956 result = acx_s_init_mac(adev);
957 if (result)
958 goto end;
960 /* TODO: see similar code in pci.c */
961 acxusb_s_read_eeprom_version(adev);
962 acxusb_s_fill_configoption(adev);
963 acx_s_set_defaults(adev);
964 acx_s_get_firmware_version(adev);
965 acx_display_hardware_details(adev);
967 /* MAC_COPY(ndev->dev_addr, adev->dev_addr); */
969 err = acx_setup_modes(adev);
970 if (err) {
971 printk("can't register hwmode\n");
972 goto fail_register_netdev;
975 /* Register the network device */
976 log(L_INIT, "registering network device\n");
977 result = ieee80211_register_hw(adev->ieee);
978 if (result) {
979 msg = "acx: failed to register USB network device "
980 "(error %d)\n";
981 goto end_nomem;
984 acx_proc_register_entries(ieee);
987 printk("acx: USB module " ACX_RELEASE " loaded successfully\n");
989 acx_init_task_scheduler(adev);
991 #if CMD_DISCOVERY
992 great_inquisitor(adev);
993 #endif
995 /* Everything went OK, we are happy now */
996 result = OK;
997 goto end;
999 end_nomem:
1000 printk(msg, result);
1002 if (ieee) {
1003 if (adev->usb_rx) {
1004 for (i = 0; i < ACX_RX_URB_CNT; i++)
1005 usb_free_urb(adev->usb_rx[i].urb);
1006 kfree(adev->usb_rx);
1008 if (adev->usb_tx) {
1009 for (i = 0; i < ACX_TX_URB_CNT; i++)
1010 usb_free_urb(adev->usb_tx[i].urb);
1011 kfree(adev->usb_tx);
1013 ieee80211_free_hw(ieee);
1016 result = -ENOMEM;
1017 goto end;
1019 end_nodev:
1020 /* no device we could handle, return error. */
1021 result = -EIO;
1023 end:
1024 FN_EXIT1(result);
1025 return result;
1029 /***********************************************************************
1030 ** acxusb_e_disconnect()
1032 ** This function is invoked whenever the user pulls the plug from the USB
1033 ** device or the module is removed from the kernel. In these cases, the
1034 ** network devices have to be taken down and all allocated memory has
1035 ** to be freed.
1037 void acxusb_e_disconnect(struct usb_interface *intf)
1039 unsigned long flags;
1040 int i;
1041 acx_device_t *adev = usb_get_intfdata(intf);
1043 FN_ENTER;
1045 /* No WLAN device... no sense */
1046 if (!adev)
1047 goto end;
1049 /* Unregister network device
1051 * If the interface is up, unregister_netdev() will take
1052 * care of calling our close() function, which takes
1053 * care of unlinking the urbs, sending the device to
1054 * sleep, etc...
1055 * This can't be called with sem or lock held because
1056 * _close() will try to grab it as well if it's called,
1057 * deadlocking the machine.
1059 acx_proc_unregister_entries(adev->ieee);
1060 ieee80211_unregister_hw(adev->ieee);
1062 acx_sem_lock(adev);
1063 acx_lock(adev, flags);
1064 /* This device exists no more */
1065 usb_set_intfdata(intf, NULL);
1068 * Here we only free them. _close() took care of
1069 * unlinking them.
1071 for (i = 0; i < ACX_RX_URB_CNT; ++i) {
1072 usb_free_urb(adev->usb_rx[i].urb);
1074 for (i = 0; i < ACX_TX_URB_CNT; ++i) {
1075 usb_free_urb(adev->usb_tx[i].urb);
1078 /* Freeing containers */
1079 kfree(adev->usb_rx);
1080 kfree(adev->usb_tx);
1082 acx_unlock(adev, flags);
1083 acx_sem_unlock(adev);
1085 ieee80211_free_hw(adev->ieee);
1086 end:
1087 FN_EXIT0;
1090 /***********************************************************************
1091 ** acxusb_e_open()
1092 ** This function is called when the user sets up the network interface.
1093 ** It initializes a management timer, sets up the USB card and starts
1094 ** the network tx queue and USB receive.
1096 int acxusb_e_open(struct ieee80211_hw *hw)
1098 acx_device_t *adev = ieee2adev(hw);
1099 unsigned long flags;
1100 int i;
1102 FN_ENTER;
1104 acx_sem_lock(adev);
1106 /* put the ACX100 out of sleep mode */
1107 // acx_s_issue_cmd(adev, ACX1xx_CMD_WAKE, NULL, 0);
1109 init_timer(&adev->mgmt_timer);
1110 adev->mgmt_timer.function = acx_i_timer;
1111 adev->mgmt_timer.data = (unsigned long)adev;
1113 /* acx_s_start needs it */
1114 SET_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1115 acx_s_start(adev);
1117 /* don't acx_start_queue() here, we need to associate first */
1119 acx_lock(adev, flags);
1120 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1121 adev->usb_rx[i].urb->status = 0;
1124 acxusb_l_poll_rx(adev, &adev->usb_rx[0]);
1126 ieee80211_start_queues(adev->ieee);
1127 acx_unlock(adev, flags);
1129 acx_sem_unlock(adev);
1131 FN_EXIT0;
1132 return 0;
1136 /***********************************************************************
1137 ** acxusb_e_close()
1139 ** This function stops the network functionality of the interface (invoked
1140 ** when the user calls ifconfig <wlan> down). The tx queue is halted and
1141 ** the device is marked as down. In case there were any pending USB bulk
1142 ** transfers, these are unlinked (asynchronously). The module in-use count
1143 ** is also decreased in this function.
1145 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1146 int acxusb_e_close(struct ieee80211_hw *hw)
1147 #else
1148 static void acxusb_e_close(struct ieee80211_hw *hw)
1149 #endif
1151 acx_device_t *adev = ieee2adev(hw);
1152 unsigned long flags;
1153 int i;
1155 FN_ENTER;
1157 acx_sem_lock(adev);
1158 if (adev->dev_state_mask & ACX_STATE_IFACE_UP)
1160 // acxusb_e_down(adev);
1161 CLEAR_BIT(adev->dev_state_mask, ACX_STATE_IFACE_UP);
1164 /* Code below is remarkably similar to acxpci_s_down(). Maybe we can merge them? */
1166 acx_free_modes(adev);
1168 /* Make sure we don't get any more rx requests */
1169 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_RX, NULL, 0);
1170 acx_s_issue_cmd(adev, ACX1xx_CMD_DISABLE_TX, NULL, 0);
1173 * We must do FLUSH *without* holding sem to avoid a deadlock.
1174 * See pci.c:acxpci_s_down() for deails.
1176 acx_sem_unlock(adev);
1177 flush_scheduled_work();
1178 acx_sem_lock(adev);
1180 /* Power down the device */
1181 acx_s_issue_cmd(adev, ACX1xx_CMD_SLEEP, NULL, 0);
1183 /* Stop the transmit queue, mark the device as DOWN */
1184 acx_lock(adev, flags);
1185 // acx_stop_queue(ndev, "on ifdown");
1186 // acx_set_status(adev, ACX_STATUS_0_STOPPED);
1187 /* stop pending rx/tx urb transfers */
1188 for (i = 0; i < ACX_TX_URB_CNT; i++) {
1189 acxusb_unlink_urb(adev->usb_tx[i].urb);
1190 adev->usb_tx[i].busy = 0;
1192 for (i = 0; i < ACX_RX_URB_CNT; i++) {
1193 acxusb_unlink_urb(adev->usb_rx[i].urb);
1194 adev->usb_rx[i].busy = 0;
1196 adev->tx_free = ACX_TX_URB_CNT;
1197 acx_unlock(adev, flags);
1199 /* Must do this outside of lock */
1200 del_timer_sync(&adev->mgmt_timer);
1202 acx_sem_unlock(adev);
1204 FN_EXIT0;
1205 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
1206 return 0;
1207 #endif
1211 /***********************************************************************
1212 ** acxusb_l_poll_rx
1213 ** This function (re)initiates a bulk-in USB transfer on a given urb
1215 void acxusb_l_poll_rx(acx_device_t * adev, usb_rx_t * rx)
1217 struct usb_device *usbdev;
1218 struct urb *rxurb;
1219 int errcode, rxnum;
1220 unsigned int inpipe;
1222 FN_ENTER;
1224 rxurb = rx->urb;
1225 usbdev = adev->usbdev;
1227 rxnum = rx - adev->usb_rx;
1229 inpipe = usb_rcvbulkpipe(usbdev, adev->bulkinep);
1230 if (unlikely(rxurb->status == -EINPROGRESS)) {
1231 printk(KERN_ERR
1232 "acx: error, rx triggered while rx urb in progress\n");
1233 /* FIXME: this is nasty, receive is being cancelled by this code
1234 * on the other hand, this should not happen anyway...
1236 usb_unlink_urb(rxurb);
1237 } else if (unlikely(rxurb->status == -ECONNRESET)) {
1238 log(L_USBRXTX, "acx_usb: _poll_rx: connection reset\n");
1239 goto end;
1241 rxurb->actual_length = 0;
1242 usb_fill_bulk_urb(rxurb, usbdev, inpipe, &rx->bulkin, /* dataptr */
1243 RXBUFSIZE, /* size */
1244 acxusb_i_complete_rx, /* handler */
1245 rx /* handler param */
1247 rxurb->transfer_flags = URB_ASYNC_UNLINK;
1249 /* ATOMIC: we may be called from complete_rx() usb callback */
1250 errcode = usb_submit_urb(rxurb, GFP_ATOMIC);
1251 /* FIXME: evaluate the error code! */
1252 log(L_USBRXTX, "SUBMIT RX (%d) inpipe=0x%X size=%d errcode=%d\n",
1253 rxnum, inpipe, (int)RXBUFSIZE, errcode);
1254 end:
1255 FN_EXIT0;
1259 /***********************************************************************
1260 ** acxusb_i_complete_rx()
1261 ** Inputs:
1262 ** urb -> pointer to USB request block
1263 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1265 ** This function is invoked by USB subsystem whenever a bulk receive
1266 ** request returns.
1267 ** The received data is then committed to the network stack and the next
1268 ** USB receive is triggered.
1270 void acxusb_i_complete_rx(struct urb *urb)
1272 acx_device_t *adev;
1273 rxbuffer_t *ptr;
1274 rxbuffer_t *inbuf;
1275 usb_rx_t *rx;
1276 unsigned long flags;
1277 int size, remsize, packetsize, rxnum;
1279 FN_ENTER;
1281 BUG_ON(!urb->context);
1283 rx = (usb_rx_t *) urb->context;
1284 adev = rx->adev;
1286 acx_lock(adev, flags);
1289 * Happens on disconnect or close. Don't play with the urb.
1290 * Don't resubmit it. It will get unlinked by close()
1292 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1293 log(L_USBRXTX, "rx: device is down, not doing anything\n");
1294 goto end_unlock;
1297 inbuf = &rx->bulkin;
1298 size = urb->actual_length;
1299 remsize = size;
1300 rxnum = rx - adev->usb_rx;
1302 log(L_USBRXTX, "RETURN RX (%d) status=%d size=%d\n",
1303 rxnum, urb->status, size);
1305 /* Send the URB that's waiting. */
1306 log(L_USBRXTX, "rxnum=%d, sending=%d\n", rxnum, rxnum ^ 1);
1307 acxusb_l_poll_rx(adev, &adev->usb_rx[rxnum ^ 1]);
1309 if (unlikely(size > sizeof(rxbuffer_t)))
1310 printk("acx_usb: rx too large: %d, please report\n", size);
1312 /* check if the transfer was aborted */
1313 switch (urb->status) {
1314 case 0: /* No error */
1315 break;
1316 case -EOVERFLOW:
1317 printk(KERN_ERR "acx: rx data overrun\n");
1318 adev->rxtruncsize = 0; /* Not valid anymore. */
1319 goto end_unlock;
1320 case -ECONNRESET:
1321 adev->rxtruncsize = 0;
1322 goto end_unlock;
1323 case -ESHUTDOWN: /* rmmod */
1324 adev->rxtruncsize = 0;
1325 goto end_unlock;
1326 default:
1327 adev->rxtruncsize = 0;
1328 adev->stats.rx_errors++;
1329 printk("acx: rx error (urb status=%d)\n", urb->status);
1330 goto end_unlock;
1333 if (unlikely(!size))
1334 printk("acx: warning, encountered zerolength rx packet\n");
1336 if (urb->transfer_buffer != inbuf)
1337 goto end_unlock;
1339 /* check if previous frame was truncated
1340 ** FIXME: this code can only handle truncation
1341 ** of consecutive packets!
1343 ptr = inbuf;
1344 if (adev->rxtruncsize) {
1345 int tail_size;
1347 ptr = &adev->rxtruncbuf;
1348 packetsize = RXBUF_BYTES_USED(ptr);
1349 if (acx_debug & L_USBRXTX) {
1350 printk("handling truncated frame (truncsize=%d size=%d "
1351 "packetsize(from trunc)=%d)\n",
1352 adev->rxtruncsize, size, packetsize);
1353 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1354 acx_dump_bytes(inbuf, RXBUF_HDRSIZE);
1357 /* bytes needed for rxtruncbuf completion: */
1358 tail_size = packetsize - adev->rxtruncsize;
1360 if (size < tail_size) {
1361 /* there is not enough data to complete this packet,
1362 ** simply append the stuff to the truncation buffer
1364 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf, size);
1365 adev->rxtruncsize += size;
1366 remsize = 0;
1367 } else {
1368 /* ok, this data completes the previously
1369 ** truncated packet. copy it into a descriptor
1370 ** and give it to the rest of the stack */
1372 /* append tail to previously truncated part
1373 ** NB: adev->rxtruncbuf (pointed to by ptr) can't
1374 ** overflow because this is already checked before
1375 ** truncation buffer was filled. See below,
1376 ** "if (packetsize > sizeof(rxbuffer_t))..." code */
1377 memcpy(((char *)ptr) + adev->rxtruncsize, inbuf,
1378 tail_size);
1380 if (acx_debug & L_USBRXTX) {
1381 printk("full trailing packet + 12 bytes:\n");
1382 acx_dump_bytes(inbuf,
1383 tail_size + RXBUF_HDRSIZE);
1385 acx_l_process_rxbuf(adev, ptr);
1386 adev->rxtruncsize = 0;
1387 ptr = (rxbuffer_t *) (((char *)inbuf) + tail_size);
1388 remsize -= tail_size;
1390 log(L_USBRXTX, "post-merge size=%d remsize=%d\n",
1391 size, remsize);
1394 /* size = USB data block size
1395 ** remsize = unprocessed USB bytes left
1396 ** ptr = current pos in USB data block
1398 while (remsize) {
1399 if (remsize < RXBUF_HDRSIZE) {
1400 printk("acx: truncated rx header (%d bytes)!\n",
1401 remsize);
1402 if (ACX_DEBUG)
1403 acx_dump_bytes(ptr, remsize);
1404 break;
1407 packetsize = RXBUF_BYTES_USED(ptr);
1408 log(L_USBRXTX, "packet with packetsize=%d\n", packetsize);
1410 if (RXBUF_IS_TXSTAT(ptr)) {
1411 /* do rate handling */
1412 usb_txstatus_t *stat = (void *)ptr;
1414 log(L_USBRXTX, "tx: stat: mac_cnt_rcvd:%04X "
1415 "queue_index:%02X mac_status:%02X hostdata:%08X "
1416 "rate:%u ack_failures:%02X rts_failures:%02X "
1417 "rts_ok:%02X\n",
1418 stat->mac_cnt_rcvd,
1419 stat->queue_index, stat->mac_status, stat->hostdata,
1420 stat->rate, stat->ack_failures, stat->rts_failures,
1421 stat->rts_ok);
1423 if (adev->rate_auto && client_no < VEC_SIZE(adev->sta_list)) {
1424 client_t *clt = &adev->sta_list[client_no];
1425 u16 cur = stat->hostdata >> 16;
1427 if (clt && clt->rate_cur == cur) {
1428 acx_l_handle_txrate_auto(adev, clt,
1429 cur, // intended rate
1430 stat->rate, 0, // actually used rate
1431 stat->mac_status, // error?
1432 ACX_TX_URB_CNT - adev->tx_free);
1435 */ goto next;
1438 if (packetsize > sizeof(rxbuffer_t)) {
1439 printk("acx: packet exceeds max wlan "
1440 "frame size (%d > %d). size=%d\n",
1441 packetsize, (int)sizeof(rxbuffer_t), size);
1442 if (ACX_DEBUG)
1443 acx_dump_bytes(ptr, 16);
1444 /* FIXME: put some real error-handling in here! */
1445 break;
1448 if (packetsize > remsize) {
1449 /* frame truncation handling */
1450 if (acx_debug & L_USBRXTX) {
1451 printk("need to truncate packet, "
1452 "packetsize=%d remsize=%d "
1453 "size=%d bytes:",
1454 packetsize, remsize, size);
1455 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1457 memcpy(&adev->rxtruncbuf, ptr, remsize);
1458 adev->rxtruncsize = remsize;
1459 break;
1462 /* packetsize <= remsize */
1463 /* now handle the received data */
1464 acx_l_process_rxbuf(adev, ptr);
1465 next:
1466 ptr = (rxbuffer_t *) (((char *)ptr) + packetsize);
1467 remsize -= packetsize;
1468 if ((acx_debug & L_USBRXTX) && remsize) {
1469 printk("more than one packet in buffer, "
1470 "second packet hdr:");
1471 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1475 end_unlock:
1476 acx_unlock(adev, flags);
1477 /* end: */
1478 FN_EXIT0;
1482 /***********************************************************************
1483 ** acxusb_i_complete_tx()
1484 ** Inputs:
1485 ** urb -> pointer to USB request block
1486 ** regs -> pointer to register-buffer for syscalls (see asm/ptrace.h)
1488 ** This function is invoked upon termination of a USB transfer.
1490 void acxusb_i_complete_tx(struct urb *urb)
1492 acx_device_t *adev;
1493 usb_tx_t *tx;
1494 unsigned long flags;
1495 int txnum;
1497 FN_ENTER;
1499 BUG_ON(!urb->context);
1501 tx = (usb_tx_t *) urb->context;
1502 adev = tx->adev;
1504 txnum = tx - adev->usb_tx;
1506 acx_lock(adev, flags);
1509 * If the iface isn't up, we don't have any right
1510 * to play with them. The urb may get unlinked.
1512 if (unlikely(!(adev->dev_state_mask & ACX_STATE_IFACE_UP))) {
1513 log(L_USBRXTX, "tx: device is down, not doing anything\n");
1514 goto end_unlock;
1517 log(L_USBRXTX, "RETURN TX (%d): status=%d size=%d\n",
1518 txnum, urb->status, urb->actual_length);
1520 /* handle USB transfer errors */
1521 switch (urb->status) {
1522 case 0: /* No error */
1523 break;
1524 case -ESHUTDOWN:
1525 goto end_unlock;
1526 break;
1527 case -ECONNRESET:
1528 goto end_unlock;
1529 break;
1530 /* FIXME: real error-handling code here please */
1531 default:
1532 printk(KERN_ERR "acx: tx error, urb status=%d\n", urb->status);
1533 /* FIXME: real error-handling code here please */
1536 /* free the URB and check for more data */
1537 tx->busy = 0;
1538 adev->tx_free++;
1539 if ((adev->tx_free >= TX_START_QUEUE)
1540 && (adev->status == ACX_STATUS_4_ASSOCIATED)
1541 /* && (acx_queue_stopped(adev->ndev)*/) {
1542 log(L_BUF, "tx: wake queue (%u free txbufs)\n", adev->tx_free);
1543 /* acx_wake_queue(adev->ndev, NULL); */
1546 end_unlock:
1547 acx_unlock(adev, flags);
1548 /* end: */
1549 FN_EXIT0;
1553 /***************************************************************
1554 ** acxusb_l_alloc_tx
1555 ** Actually returns a usb_tx_t* ptr
1557 tx_t *acxusb_l_alloc_tx(acx_device_t * adev)
1559 usb_tx_t *tx;
1560 unsigned head;
1562 FN_ENTER;
1564 head = adev->tx_head;
1565 do {
1566 head = (head + 1) % ACX_TX_URB_CNT;
1567 if (!adev->usb_tx[head].busy) {
1568 log(L_USBRXTX, "allocated tx %d\n", head);
1569 tx = &adev->usb_tx[head];
1570 tx->busy = 1;
1571 adev->tx_free--;
1572 /* Keep a few free descs between head and tail of tx ring.
1573 ** It is not absolutely needed, just feels safer */
1574 if (adev->tx_free < TX_STOP_QUEUE) {
1575 log(L_BUF, "tx: stop queue "
1576 "(%u free txbufs)\n", adev->tx_free);
1577 /* acx_stop_queue(adev->ndev, NULL); */
1579 goto end;
1581 } while (likely(head != adev->tx_head));
1582 tx = NULL;
1583 printk_ratelimited("acx: tx buffers full\n");
1584 end:
1585 adev->tx_head = head;
1586 FN_EXIT0;
1587 return (tx_t *) tx;
1591 /***************************************************************
1592 ** Used if alloc_tx()'ed buffer needs to be cancelled without doing tx
1594 void acxusb_l_dealloc_tx(tx_t * tx_opaque)
1596 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1597 tx->busy = 0;
1601 /***************************************************************
1603 void *acxusb_l_get_txbuf(acx_device_t * adev, tx_t * tx_opaque)
1605 usb_tx_t *tx = (usb_tx_t *) tx_opaque;
1606 return &tx->bulkout.data;
1610 /***************************************************************
1611 ** acxusb_l_tx_data
1613 ** Can be called from IRQ (rx -> (AP bridging or mgmt response) -> tx).
1614 ** Can be called from acx_i_start_xmit (data frames from net core).
1616 void acxusb_l_tx_data(acx_device_t * adev, tx_t * tx_opaque, int wlanpkt_len, struct ieee80211_tx_control *ctl,
1617 struct sk_buff* skb)
1619 struct usb_device *usbdev;
1620 struct urb *txurb;
1621 usb_tx_t *tx;
1622 usb_txbuffer_t *txbuf;
1623 // client_t *clt;
1624 struct ieee80211_hdr *whdr;
1625 unsigned int outpipe;
1626 int ucode, txnum;
1628 FN_ENTER;
1630 tx = ((usb_tx_t *) tx_opaque);
1631 txurb = tx->urb;
1632 txbuf = &tx->bulkout;
1633 whdr = (struct ieee80211_hdr *) txbuf->data;
1634 txnum = tx - adev->usb_tx;
1636 log(L_DEBUG, "using buf#%d free=%d len=%d\n",
1637 txnum, adev->tx_free, wlanpkt_len);
1639 switch (adev->mode) {
1640 case ACX_MODE_0_ADHOC:
1641 case ACX_MODE_3_AP:
1642 clt = acx_l_sta_list_get(adev, whdr->a1);
1643 break;
1644 case ACX_MODE_2_STA:
1645 // clt = adev->ap_client;
1646 break;
1647 default:
1648 clt = NULL;
1649 break;
1651 if (unlikely(clt && !clt->rate_cur)) {
1652 printk("acx: driver bug! bad ratemask\n");
1653 goto end;
1657 /* fill the USB transfer header */
1658 txbuf->desc = cpu_to_le16(USB_TXBUF_TXDESC);
1659 txbuf->mpdu_len = cpu_to_le16(wlanpkt_len);
1660 txbuf->queue_index = 1;
1661 txbuf->rate = ctl->tx_rate; //clt->rate_100;
1662 // FIXME(); //This used to have | (clt - adev->ap_client)
1663 txbuf->hostdata = (ctl->tx_rate << 16);
1664 txbuf->ctrl1 = DESC_CTL_FIRSTFRAG;
1665 if (1 == adev->preamble_cur)
1666 SET_BIT(txbuf->ctrl1, DESC_CTL_SHORT_PREAMBLE);
1667 txbuf->ctrl2 = 0;
1668 txbuf->data_len = cpu_to_le16(wlanpkt_len);
1670 if (unlikely(acx_debug & L_DATA)) {
1671 printk("dump of bulk out urb:\n");
1672 acx_dump_bytes(txbuf, wlanpkt_len + USB_TXBUF_HDRSIZE);
1675 if (unlikely(txurb->status == -EINPROGRESS)) {
1676 printk
1677 ("acx: trying to submit tx urb while already in progress\n");
1680 /* now schedule the USB transfer */
1681 usbdev = adev->usbdev;
1682 outpipe = usb_sndbulkpipe(usbdev, adev->bulkoutep);
1684 usb_fill_bulk_urb(txurb, usbdev, outpipe, txbuf, /* dataptr */
1685 wlanpkt_len + USB_TXBUF_HDRSIZE, /* size */
1686 acxusb_i_complete_tx, /* handler */
1687 tx /* handler param */
1690 txurb->transfer_flags = URB_ASYNC_UNLINK | URB_ZERO_PACKET;
1691 ucode = usb_submit_urb(txurb, GFP_ATOMIC);
1692 log(L_USBRXTX, "SUBMIT TX (%d): outpipe=0x%X buf=%p txsize=%d "
1693 "rate=%u errcode=%d\n", txnum, outpipe, txbuf,
1694 wlanpkt_len + USB_TXBUF_HDRSIZE, txbuf->rate, ucode);
1696 if (unlikely(ucode)) {
1697 printk(KERN_ERR "acx: submit_urb() error=%d txsize=%d\n",
1698 ucode, wlanpkt_len + USB_TXBUF_HDRSIZE);
1700 /* on error, just mark the frame as done and update
1701 ** the statistics
1703 adev->stats.tx_errors++;
1704 tx->busy = 0;
1705 adev->tx_free++;
1706 /* needed? if (adev->tx_free > TX_START_QUEUE) acx_wake_queue(...) */
1708 FN_EXIT0;
1712 /***********************************************************************
1713 static void acxusb_i_set_rx_mode(struct net_device *ndev)
1719 /***********************************************************************
1721 #ifdef HAVE_TX_TIMEOUT
1723 void acxusb_i_tx_timeout(struct net_device *ndev)
1725 acx_device_t *adev = ndev2adev(ndev);
1726 unsigned long flags;
1727 int i;
1729 FN_ENTER;
1731 acx_lock(adev, flags);
1732 */ /* unlink the URBs */
1733 /* for (i = 0; i < ACX_TX_URB_CNT; i++) {
1734 acxusb_unlink_urb(adev->usb_tx[i].urb);
1735 adev->usb_tx[i].busy = 0;
1737 adev->tx_free = ACX_TX_URB_CNT;
1738 */ /* TODO: stats update */
1739 /* acx_unlock(adev, flags);
1741 FN_EXIT0;
1744 #endif
1747 /***********************************************************************
1748 ** init_module()
1750 ** This function is invoked upon loading of the kernel module.
1751 ** It registers itself at the kernel's USB subsystem.
1753 ** Returns: Errorcode on failure, 0 on success
1755 int __init acxusb_e_init_module(void)
1757 log(L_INIT, "USB module " ACX_RELEASE " initialized, "
1758 "probing for devices...\n");
1759 return usb_register(&acxusb_driver);
1764 /***********************************************************************
1765 ** cleanup_module()
1767 ** This function is invoked as last step of the module unloading. It simply
1768 ** deregisters this module at the kernel's USB subsystem.
1770 void __exit acxusb_e_cleanup_module(void)
1772 usb_deregister(&acxusb_driver);
1773 log(L_INIT, "USB module " ACX_RELEASE " unloaded\n");
1777 /***********************************************************************
1778 ** DEBUG STUFF
1780 #if ACX_DEBUG
1782 #ifdef UNUSED
1783 static void dump_device(struct usb_device *usbdev)
1785 int i;
1786 struct usb_config_descriptor *cd;
1788 printk("acx device dump:\n");
1789 printk(" devnum: %d\n", usbdev->devnum);
1790 printk(" speed: %d\n", usbdev->speed);
1791 printk(" tt: 0x%X\n", (unsigned int)(usbdev->tt));
1792 printk(" ttport: %d\n", (unsigned int)(usbdev->ttport));
1793 printk(" toggle[0]: 0x%X toggle[1]: 0x%X\n",
1794 (unsigned int)(usbdev->toggle[0]),
1795 (unsigned int)(usbdev->toggle[1]));
1796 /* This saw a change after 2.6.10 */
1797 printk(" ep_in wMaxPacketSize: ");
1798 for (i = 0; i < 16; ++i)
1799 if (usbdev->ep_in[i] != NULL)
1800 printk("%d:%d ", i,
1801 usbdev->ep_in[i]->desc.wMaxPacketSize);
1802 printk("\n");
1803 printk(" ep_out wMaxPacketSize: ");
1804 for (i = 0; i < ARRAY_SIZE(usbdev->ep_out); ++i)
1805 if (usbdev->ep_out[i] != NULL)
1806 printk("%d:%d ", i,
1807 usbdev->ep_out[i]->desc.wMaxPacketSize);
1808 printk("\n");
1809 printk(" parent: 0x%X\n", (unsigned int)usbdev->parent);
1810 printk(" bus: 0x%X\n", (unsigned int)usbdev->bus);
1811 #ifdef NO_DATATYPE
1812 printk(" configs: ");
1813 for (i = 0; i < usbdev->descriptor.bNumConfigurations; i++)
1814 printk("0x%X ", usbdev->config[i]);
1815 printk("\n");
1816 #endif
1817 printk(" actconfig: %p\n", usbdev->actconfig);
1818 dump_device_descriptor(&usbdev->descriptor);
1820 cd = &usbdev->config->desc;
1821 dump_config_descriptor(cd);
1825 /***********************************************************************
1827 static void dump_config_descriptor(struct usb_config_descriptor *cd)
1829 printk("Configuration Descriptor:\n");
1830 if (!cd) {
1831 printk("NULL\n");
1832 return;
1834 printk(" bLength: %d (0x%X)\n", cd->bLength, cd->bLength);
1835 printk(" bDescriptorType: %d (0x%X)\n", cd->bDescriptorType,
1836 cd->bDescriptorType);
1837 printk(" bNumInterfaces: %d (0x%X)\n", cd->bNumInterfaces,
1838 cd->bNumInterfaces);
1839 printk(" bConfigurationValue: %d (0x%X)\n", cd->bConfigurationValue,
1840 cd->bConfigurationValue);
1841 printk(" iConfiguration: %d (0x%X)\n", cd->iConfiguration,
1842 cd->iConfiguration);
1843 printk(" bmAttributes: %d (0x%X)\n", cd->bmAttributes,
1844 cd->bmAttributes);
1845 /* printk(" MaxPower: %d (0x%X)\n", cd->bMaxPower, cd->bMaxPower); */
1849 static void dump_device_descriptor(struct usb_device_descriptor *dd)
1851 printk("Device Descriptor:\n");
1852 if (!dd) {
1853 printk("NULL\n");
1854 return;
1856 printk(" bLength: %d (0x%X)\n", dd->bLength, dd->bLength);
1857 printk(" bDescriptortype: %d (0x%X)\n", dd->bDescriptorType,
1858 dd->bDescriptorType);
1859 printk(" bcdUSB: %d (0x%X)\n", dd->bcdUSB, dd->bcdUSB);
1860 printk(" bDeviceClass: %d (0x%X)\n", dd->bDeviceClass,
1861 dd->bDeviceClass);
1862 printk(" bDeviceSubClass: %d (0x%X)\n", dd->bDeviceSubClass,
1863 dd->bDeviceSubClass);
1864 printk(" bDeviceProtocol: %d (0x%X)\n", dd->bDeviceProtocol,
1865 dd->bDeviceProtocol);
1866 printk(" bMaxPacketSize0: %d (0x%X)\n", dd->bMaxPacketSize0,
1867 dd->bMaxPacketSize0);
1868 printk(" idVendor: %d (0x%X)\n", dd->idVendor, dd->idVendor);
1869 printk(" idProduct: %d (0x%X)\n", dd->idProduct, dd->idProduct);
1870 printk(" bcdDevice: %d (0x%X)\n", dd->bcdDevice, dd->bcdDevice);
1871 printk(" iManufacturer: %d (0x%X)\n", dd->iManufacturer,
1872 dd->iManufacturer);
1873 printk(" iProduct: %d (0x%X)\n", dd->iProduct, dd->iProduct);
1874 printk(" iSerialNumber: %d (0x%X)\n", dd->iSerialNumber,
1875 dd->iSerialNumber);
1876 printk(" bNumConfigurations: %d (0x%X)\n", dd->bNumConfigurations,
1877 dd->bNumConfigurations);
1879 #endif /* UNUSED */
1881 #endif /* ACX_DEBUG */