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