Original 20051017 tarball
[acx-mac80211.git] / usb.c
blob4719b37a843d68795bbf2644ee83ddcd73f16053
1 /***********************************************************************
2 ** Copyright (C) 2003 ACX100 Open Source Project
3 **
4 ** The contents of this file are subject to the Mozilla Public
5 ** License Version 1.1 (the "License"); you may not use this file
6 ** except in compliance with the License. You may obtain a copy of
7 ** the License at http://www.mozilla.org/MPL/
8 **
9 ** Software distributed under the License is distributed on an "AS
10 ** IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 ** implied. See the License for the specific language governing
12 ** rights and limitations under the License.
14 ** Alternatively, the contents of this file may be used under the
15 ** terms of the GNU Public License version 2 (the "GPL"), in which
16 ** case the provisions of the GPL are applicable instead of the
17 ** above. If you wish to allow the use of your version of this file
18 ** only under the terms of the GPL and not to allow others to use
19 ** your version of this file under the MPL, indicate your decision
20 ** by deleting the provisions above and replace them with the notice
21 ** and other provisions required by the GPL. If you do not delete
22 ** the provisions above, a recipient may use your version of this
23 ** file under either the MPL or the GPL.
24 ** ---------------------------------------------------------------------
25 ** Inquiries regarding the ACX100 Open Source Project can be
26 ** made directly to:
28 ** acx100-users@lists.sf.net
29 ** http://acx100.sf.net
30 ** ---------------------------------------------------------------------
33 /***********************************************************************
34 ** USB support for TI ACX100 based devices. Many parts are taken from
35 ** the PCI driver.
37 ** Authors:
38 ** Martin Wawro <martin.wawro AT uni-dortmund.de>
39 ** Andreas Mohr <andi AT lisas.de>
41 ** Issues:
42 ** - Note that this driver relies on a native little-endian byteformat
43 ** at some points
45 ** LOCKING
46 ** callback functions called by USB core are running in interrupt context
47 ** and thus have names with _i_.
49 #define ACX_USB 1
51 #include <linux/version.h>
52 #include <linux/config.h>
53 #include <linux/types.h>
54 #include <linux/module.h>
55 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)
56 #include <linux/moduleparam.h>
57 #endif
58 #include <linux/kernel.h>
59 #include <linux/usb.h>
60 #include <linux/netdevice.h>
61 #include <linux/rtnetlink.h>
62 #include <linux/etherdevice.h>
63 #include <linux/wireless.h>
64 #if WIRELESS_EXT >= 13
65 #include <net/iw_handler.h>
66 #endif
68 #include "acx.h"
71 /***********************************************************************
73 /* number of endpoints of an interface */
74 #define NUM_EP(intf) (intf)->altsetting[0].desc.bNumEndpoints
75 #define EP(intf, nr) (intf)->altsetting[0].endpoint[(nr)].desc
76 #define GET_DEV(udev) usb_get_dev((udev))
77 #define PUT_DEV(udev) usb_put_dev((udev))
78 #define SET_NETDEV_OWNER(ndev, owner) /* not needed anymore ??? */
80 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
81 /* removed in 2.6.14. We will use fake value for now */
82 #define URB_ASYNC_UNLINK 0
83 #endif
86 /***********************************************************************
88 #define ACX100_VENDOR_ID 0x2001
89 #define ACX100_PRODUCT_ID_UNBOOTED 0x3B01
90 #define ACX100_PRODUCT_ID_BOOTED 0x3B00
92 /* RX-Timeout: NONE (request waits forever) */
93 #define ACX100_USB_RX_TIMEOUT (0)
95 #define ACX100_USB_TX_TIMEOUT (4*HZ)
97 #define USB_CTRL_HARD_TIMEOUT 5500 /* steps in ms */
100 /***********************************************************************
101 ** Prototypes
103 static int acx100usb_e_probe(struct usb_interface *, const struct usb_device_id *);
104 static void acx100usb_e_disconnect(struct usb_interface *);
105 static void acx100usb_i_complete_tx(struct urb *, struct pt_regs *);
106 static void acx100usb_i_complete_rx(struct urb *, struct pt_regs *);
107 static int acx100usb_e_open(struct net_device *);
108 static int acx100usb_e_close(struct net_device *);
109 static void acx100usb_i_set_rx_mode(struct net_device *);
110 static int acx100usb_e_init_network_device(struct net_device *);
111 static int acx100usb_boot(struct usb_device *);
113 static void acx100usb_l_poll_rx(wlandevice_t *, usb_rx_t* rx);
115 static void acx100usb_i_tx_timeout(struct net_device *);
117 /* static void dump_device(struct usb_device *); */
118 /* static void dump_device_descriptor(struct usb_device_descriptor *); */
119 /* static void dump_config_descriptor(struct usb_config_descriptor *); */
121 /***********************************************************************
122 ** Module Data
124 #define TXBUFSIZE sizeof(usb_txbuffer_t)
125 //// Bogus! We CANNOT pretend that rxbuffer_t is larger than it is.
126 /* make it a multiply of 64 */
127 /* #define RXBUFSIZE ((sizeof(rxbuffer_t)+63) & ~63) */
128 #define RXBUFSIZE sizeof(rxbuffer_t)
130 static const struct usb_device_id
131 acx100usb_ids[] = {
132 { USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_BOOTED) },
133 { USB_DEVICE(ACX100_VENDOR_ID, ACX100_PRODUCT_ID_UNBOOTED) },
138 /* USB driver data structure as required by the kernel's USB core */
139 static struct usb_driver
140 acx100usb_driver = {
141 .name = "acx_usb",
142 .owner = THIS_MODULE,
143 .probe = acx100usb_e_probe,
144 .disconnect = acx100usb_e_disconnect,
145 .id_table = acx100usb_ids
149 /***********************************************************************
150 ** USB helper
152 ** ldd3 ch13 says:
153 ** When the function is usb_kill_urb, the urb lifecycle is stopped. This
154 ** function is usually used when the device is disconnected from the system,
155 ** in the disconnect callback. For some drivers, the usb_unlink_urb function
156 ** should be used to tell the USB core to stop an urb. This function does not
157 ** wait for the urb to be fully stopped before returning to the caller.
158 ** This is useful for stoppingthe urb while in an interrupt handler or when
159 ** a spinlock is held, as waiting for a urb to fully stop requires the ability
160 ** for the USB core to put the calling process to sleep. This function requires
161 ** that the URB_ASYNC_UNLINK flag value be set in the urb that is being asked
162 ** to be stopped in order to work properly.
164 ** (URB_ASYNC_UNLINK is obsolete, usb_unlink_urb will always be
165 ** asynchronous while usb_kill_urb is synchronous and should be called
166 ** directly (drivers/usb/core/urb.c))
168 ** In light of this, timeout is just for paranoid reasons...
170 * Actually, it's useful for debugging. If we reach timeout, we're doing
171 * something wrong with the urbs.
173 static void
174 acxusb_unlink_urb(struct urb* urb)
176 if (!urb)
177 return;
179 if (urb->status == -EINPROGRESS) {
180 int timeout = 10;
182 usb_unlink_urb(urb);
183 while (--timeout && urb->status == -EINPROGRESS) {
184 mdelay(1);
186 if (!timeout) {
187 printk("acx_usb: urb unlink timeout!\n");
193 /***********************************************************************
194 ** EEPROM and PHY read/write helpers
196 /***********************************************************************
197 ** acxusb_s_read_phy_reg
200 acxusb_s_read_phy_reg(wlandevice_t *priv, u32 reg, u8 *charbuf)
202 mem_read_write_t mem;
204 FN_ENTER;
206 mem.addr = cpu_to_le16(reg);
207 mem.type = cpu_to_le16(0x82);
208 mem.len = cpu_to_le32(4);
209 acx_s_issue_cmd(priv, ACX1xx_CMD_MEM_READ, &mem, sizeof(mem));
210 *charbuf = mem.data;
211 acxlog(L_DEBUG, "read radio PHY[0x%04X]=0x%02X\n", reg, *charbuf);
213 FN_EXIT1(OK);
214 return OK;
218 /***********************************************************************
221 acxusb_s_write_phy_reg(wlandevice_t *priv, u32 reg, u8 value)
223 mem_read_write_t mem;
225 FN_ENTER;
227 mem.addr = cpu_to_le16(reg);
228 mem.type = cpu_to_le16(0x82);
229 mem.len = cpu_to_le32(4);
230 mem.data = value;
231 acx_s_issue_cmd(priv, ACX1xx_CMD_MEM_WRITE, &mem, sizeof(mem));
232 acxlog(L_DEBUG, "write radio PHY[0x%04X]=0x%02X\n", reg, value);
234 FN_EXIT1(OK);
235 return OK;
239 /***********************************************************************
240 ** acx_s_issue_cmd_timeo
241 ** Excecutes a command in the command mailbox
243 ** buffer = a pointer to the data.
244 ** The data must not include 4 byte command header
247 /* TODO: ideally we shall always know how much we need
248 ** and this shall be 0 */
249 #define BOGUS_SAFETY_PADDING 0x40
251 #undef FUNC
252 #define FUNC "issue_cmd"
254 #if !ACX_DEBUG
256 acxusb_s_issue_cmd_timeo(
257 wlandevice_t *priv,
258 unsigned cmd,
259 void *buffer,
260 unsigned buflen,
261 unsigned timeout)
263 #else
265 acxusb_s_issue_cmd_timeo_debug(
266 wlandevice_t *priv,
267 unsigned cmd,
268 void *buffer,
269 unsigned buflen,
270 unsigned timeout,
271 const char* cmdstr)
273 #endif
274 /* USB ignores timeout param */
276 struct usb_device *usbdev;
277 struct {
278 u16 cmd ACX_PACKED;
279 u16 status ACX_PACKED;
280 u8 data[1] ACX_PACKED;
281 } *loc;
282 const char *devname;
283 int acklen, blocklen, inpipe, outpipe;
284 int cmd_status;
285 int result;
287 FN_ENTER;
289 devname = priv->netdev->name;
290 if (!devname || !devname[0])
291 devname = "acx";
293 acxlog(L_CTL, FUNC"(cmd:%s,buflen:%u,type:0x%04X)\n",
294 cmdstr, buflen,
295 buffer ? le16_to_cpu(((acx_ie_generic_t *)buffer)->type) : -1);
297 loc = kmalloc(buflen + 4 + BOGUS_SAFETY_PADDING, GFP_KERNEL);
298 if (!loc) {
299 printk("%s: "FUNC"(): no memory for data buffer\n", devname);
300 goto bad;
303 /* get context from wlandevice */
304 usbdev = priv->usbdev;
306 /* check which kind of command was issued */
307 loc->cmd = cpu_to_le16(cmd);
308 loc->status = 0;
310 /* NB: buflen == frmlen + 4
312 ** Interrogate: write 8 bytes: (cmd,status,rid,frmlen), then
313 ** read (cmd,status,rid,frmlen,data[frmlen]) back
315 ** Configure: write (cmd,status,rid,frmlen,data[frmlen])
317 ** Possibly bogus special handling of ACX1xx_IE_SCAN_STATUS removed
320 /* now write the parameters of the command if needed */
321 acklen = buflen + 4 + BOGUS_SAFETY_PADDING;
322 blocklen = buflen;
323 if (buffer && buflen) {
324 /* if it's an INTERROGATE command, just pass the length
325 * of parameters to read, as data */
326 if (cmd == ACX1xx_CMD_INTERROGATE) {
327 blocklen = 4;
328 acklen = buflen + 4;
330 memcpy(loc->data, buffer, blocklen);
332 blocklen += 4; /* account for cmd,status */
334 /* obtain the I/O pipes */
335 outpipe = usb_sndctrlpipe(usbdev, 0);
336 inpipe = usb_rcvctrlpipe(usbdev, 0);
337 acxlog(L_CTL, "ctrl inpipe=0x%X outpipe=0x%X\n", inpipe, outpipe);
338 acxlog(L_CTL, "sending USB control msg (out) (blocklen=%d)\n", blocklen);
339 if (acx_debug & L_DATA)
340 acx_dump_bytes(loc, blocklen);
342 result = usb_control_msg(usbdev, outpipe,
343 ACX_USB_REQ_CMD, /* request */
344 USB_TYPE_VENDOR|USB_DIR_OUT, /* requesttype */
345 0, /* value */
346 0, /* index */
347 loc, /* dataptr */
348 blocklen, /* size */
349 USB_CTRL_HARD_TIMEOUT /* timeout in ms */
352 if (result == -ENODEV) {
353 acxlog(L_CTL, "no device present (unplug?)\n");
354 goto good;
357 acxlog(L_CTL, "wrote %d bytes\n", result);
358 if (result < 0) {
359 goto bad;
362 /* check for device acknowledge */
363 acxlog(L_CTL, "sending USB control msg (in) (acklen=%d)\n", acklen);
364 loc->status = 0; /* delete old status flag -> set to IDLE */
365 //shall we zero out the rest?
366 result = usb_control_msg(usbdev, inpipe,
367 ACX_USB_REQ_CMD, /* request */
368 USB_TYPE_VENDOR|USB_DIR_IN, /* requesttype */
369 0, /* value */
370 0, /* index */
371 loc, /* dataptr */
372 acklen, /* size */
373 USB_CTRL_HARD_TIMEOUT /* timeout in ms */
375 if (result < 0) {
376 printk("%s: "FUNC"(): USB read error %d\n", devname, result);
377 goto bad;
379 if (acx_debug & L_CTL) {
380 printk("read %d bytes: ", result);
381 acx_dump_bytes(loc, result);
384 //check for result==buflen+4? Was seen:
386 interrogate(type:ACX100_IE_DOT11_ED_THRESHOLD,len:4)
387 issue_cmd(cmd:ACX1xx_CMD_INTERROGATE,buflen:8,type:4111)
388 ctrl inpipe=0x80000280 outpipe=0x80000200
389 sending USB control msg (out) (blocklen=8)
390 01 00 00 00 0F 10 04 00
391 wrote 8 bytes
392 sending USB control msg (in) (acklen=12) sizeof(loc->data
393 read 4 bytes <==== MUST BE 12!!
396 cmd_status = le16_to_cpu(loc->status);
397 if (cmd_status != 1) {
398 printk("%s: "FUNC"(): cmd_status is not SUCCESS: %d (%s)\n",
399 devname, cmd_status, acx_cmd_status_str(cmd_status));
400 /* TODO: goto bad; ? */
402 if ((cmd == ACX1xx_CMD_INTERROGATE) && buffer && buflen) {
403 memcpy(buffer, loc->data, buflen);
404 acxlog(L_CTL, "response frame: cmd=0x%04X status=%d\n",
405 le16_to_cpu(loc->cmd),
406 cmd_status);
408 good:
409 kfree(loc);
410 FN_EXIT1(OK);
411 return OK;
412 bad:
413 /* Give enough info so that callers can avoid
414 ** printing their own diagnostic messages */
415 #if ACX_DEBUG
416 printk("%s: "FUNC"(cmd:%s) FAILED\n", devname, cmdstr);
417 #else
418 printk("%s: "FUNC"(cmd:0x%04X) FAILED\n", devname, cmd);
419 #endif
420 dump_stack();
421 kfree(loc);
422 FN_EXIT1(NOT_OK);
423 return NOT_OK;
427 /***********************************************************************
428 ** acx100usb_e_probe()
430 ** Inputs:
431 ** dev -> Pointer to usb_device structure that may or may not be claimed
432 ** ifNum -> Interface number
433 ** devID -> Device ID (vendor and product specific stuff)
434 ************************************************************************
435 ** Returns:
436 ** (void *) Pointer to (custom) driver context or NULL if we are not interested
437 ** or unable to handle the offered device.
439 ** Description:
440 ** This function is invoked by the kernel's USB core whenever a new device is
441 ** attached to the system or the module is loaded. It is presented a usb_device
442 ** structure from which information regarding the device is obtained and evaluated.
443 ** In case this driver is able to handle one of the offered devices, it returns
444 ** a non-null pointer to a driver context and thereby claims the device.
446 static void
447 dummy_netdev_init(struct net_device *dev) {}
449 static int
450 acx100usb_e_probe(struct usb_interface *intf, const struct usb_device_id *devID)
452 struct usb_device *usbdev = interface_to_usbdev(intf);
453 wlandevice_t *priv = NULL;
454 struct net_device *dev = NULL;
455 struct usb_config_descriptor *config;
456 struct usb_endpoint_descriptor *epdesc;
457 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
458 struct usb_host_endpoint *ep;
459 #endif
460 struct usb_interface_descriptor *ifdesc;
461 const char* msg;
462 int numconfigs, numfaces, numep;
463 int result = OK;
464 int i;
466 FN_ENTER;
468 /* First check if this is the "unbooted" hardware */
469 if ((usbdev->descriptor.idVendor == ACX100_VENDOR_ID)
470 && (usbdev->descriptor.idProduct == ACX100_PRODUCT_ID_UNBOOTED)) {
471 /* Boot the device (i.e. upload the firmware) */
472 acx100usb_boot(usbdev);
474 /* OK, we are done with booting. Normally, the
475 ** ID for the unbooted device should disappear
476 ** and it will not need a driver anyway...so
477 ** return a NULL
479 acxlog(L_INIT, "finished booting, returning from probe()\n");
480 result = OK; /* success */
481 goto end;
484 if ((usbdev->descriptor.idVendor != ACX100_VENDOR_ID)
485 || (usbdev->descriptor.idProduct != ACX100_PRODUCT_ID_BOOTED)) {
486 goto end_nodev;
489 /* Ok, so it's our device and it is already booted */
491 /* Allocate memory for a network device */
492 dev = alloc_netdev(sizeof(wlandevice_t), "wlan%d", dummy_netdev_init);
493 /* (NB: memsets to 0 entire area) */
494 if (!dev) {
495 msg = "acx: no memory for netdev\n";
496 goto end_nomem;
498 dev->init = (void *)&acx100usb_e_init_network_device;
500 /* Setup private driver context */
501 priv = netdev_priv(dev);
502 priv->netdev = dev;
503 priv->dev_type = DEVTYPE_USB;
504 priv->chip_type = CHIPTYPE_ACX100;
505 /* FIXME: should be read from register (via firmware) using standard ACX code */
506 priv->radio_type = RADIO_MAXIM_0D;
507 priv->usbdev = usbdev;
509 spin_lock_init(&priv->lock); /* initial state: unlocked */
510 sema_init(&priv->sem, 1); /* initial state: 1 (upped) */
512 /* Initialize the device context and also check
513 ** if this is really the hardware we know about.
514 ** If not sure, at least notify the user that he
515 ** may be in trouble...
517 numconfigs = (int)usbdev->descriptor.bNumConfigurations;
518 if (numconfigs != 1)
519 printk("acx: number of configurations is %d, "
520 "this driver only knows how to handle 1, "
521 "be prepared for surprises\n", numconfigs);
523 config = &usbdev->config->desc;
524 numfaces = config->bNumInterfaces;
525 if (numfaces != 1)
526 printk("acx: number of interfaces is %d, "
527 "this driver only knows how to handle 1, "
528 "be prepared for surprises\n", numfaces);
530 ifdesc = &intf->altsetting->desc;
531 numep = ifdesc->bNumEndpoints;
532 acxlog(L_DEBUG, "# of endpoints: %d\n", numep);
534 /* obtain information about the endpoint
535 ** addresses, begin with some default values
537 priv->bulkoutep = 1;
538 priv->bulkinep = 1;
539 for (i = 0; i < numep; i++) {
540 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
541 ep = usbdev->ep_in[i];
542 if (!ep)
543 continue;
544 epdesc = &ep->desc;
545 #else
546 epdesc = usb_epnum_to_ep_desc(usbdev, i);
547 if (!epdesc)
548 continue;
549 #endif
550 if (epdesc->bmAttributes & USB_ENDPOINT_XFER_BULK) {
551 if (epdesc->bEndpointAddress & 0x80)
552 priv->bulkinep = epdesc->bEndpointAddress & 0xF;
553 else
554 priv->bulkoutep = epdesc->bEndpointAddress & 0xF;
557 acxlog(L_DEBUG, "bulkout ep: 0x%X\n", priv->bulkoutep);
558 acxlog(L_DEBUG, "bulkin ep: 0x%X\n", priv->bulkinep);
560 /* Set the packet-size equivalent to the buffer size */
561 /* already done by memset: priv->rxtruncsize = 0; */
562 acxlog(L_DEBUG, "TXBUFSIZE=%d RXBUFSIZE=%d\n",
563 (int) TXBUFSIZE, (int) RXBUFSIZE);
565 priv->tx_free = ACX100_USB_NUM_BULK_URBS;
567 /* Allocate the RX/TX containers. */
568 priv->usb_tx = kmalloc(sizeof(usb_tx_t) * ACX100_USB_NUM_BULK_URBS,
569 GFP_KERNEL);
570 if (!priv->usb_tx) {
571 msg = "acx: no memory for tx container";
572 goto end_nomem;
574 priv->usb_rx = kmalloc(sizeof(usb_rx_t) * ACX100_USB_NUM_BULK_URBS,
575 GFP_KERNEL);
576 if (!priv->usb_rx) {
577 msg = "acx: no memory for rx container";
578 goto end_nomem;
581 /* Setup URBs for bulk-in/out messages */
582 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++) {
583 priv->usb_rx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
584 if (!priv->usb_rx[i].urb) {
585 msg = "acx: no memory for input URB\n";
586 goto end_nomem;
588 priv->usb_rx[i].urb->status = 0;
589 priv->usb_rx[i].priv = priv;
591 priv->usb_tx[i].urb = usb_alloc_urb(0, GFP_KERNEL);
592 if (!priv->usb_tx[i].urb) {
593 msg = "acx: no memory for output URB\n";
594 goto end_nomem;
596 priv->usb_tx[i].urb->status = 0;
597 priv->usb_tx[i].priv = priv;
600 usb_set_intfdata(intf, priv);
601 SET_NETDEV_DEV(dev, &intf->dev);
603 /* Register the network device */
604 acxlog(L_INIT, "registering network device\n");
605 result = register_netdev(dev);
606 if (result != 0) {
607 msg = "acx: failed to register network device "
608 "for USB WLAN (errcode=%d)\n";
609 goto end_nomem;
612 if (OK != acx_proc_register_entries(dev)) {
613 printk("acx: /proc registration failed\n");
616 printk("acx: USB module " WLAN_RELEASE " loaded successfully\n");
618 #if CMD_DISCOVERY
619 great_inquisitor(priv);
620 #endif
622 /* Everything went OK, we are happy now */
623 result = OK;
624 goto end;
626 end_nomem:
627 printk(msg, result);
629 if (dev) {
630 if (priv->usb_rx) {
631 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++)
632 usb_free_urb(priv->usb_rx[i].urb);
633 kfree(priv->usb_rx);
635 if (priv->usb_tx) {
636 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++)
637 usb_free_urb(priv->usb_tx[i].urb);
638 kfree(priv->usb_tx);
640 free_netdev(dev);
643 result = -ENOMEM;
644 goto end;
646 end_nodev:
648 /* no device we could handle, return error. */
649 result = -EIO;
651 end:
652 FN_EXIT1(result);
653 return result;
657 /***********************************************************************
658 ** acx100usb_e_disconnect()
660 ** This function is invoked whenever the user pulls the plug from the USB
661 ** device or the module is removed from the kernel. In these cases, the
662 ** network devices have to be taken down and all allocated memory has
663 ** to be freed.
665 static void
666 acx100usb_e_disconnect(struct usb_interface *intf)
668 wlandevice_t *priv = usb_get_intfdata(intf);
669 int i;
670 unsigned long flags;
672 FN_ENTER;
674 /* No WLAN device... no sense */
675 if (!priv)
676 goto end;
678 /* Unregister network device
680 * If the interface is up, unregister_netdev() will take
681 * care of calling our close() function, which takes
682 * care of unlinking the urbs, sending the device to
683 * sleep, etc...
684 * This can't be called with sem or lock held because
685 * _close() will try to grab it as well if it's called,
686 * deadlocking the machine.
688 unregister_netdev(priv->netdev);
690 acx_sem_lock(priv);
691 acx_lock(priv, flags);
692 /* This device exists no more */
693 usb_set_intfdata(intf, NULL);
694 acx_proc_unregister_entries(priv->netdev);
697 * Here we only free them. _close() takes care of
698 * unlinking them.
700 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; ++i) {
701 usb_free_urb(priv->usb_rx[i].urb);
702 usb_free_urb(priv->usb_tx[i].urb);
705 /* The the containers. */
706 kfree(priv->usb_rx);
707 kfree(priv->usb_tx);
709 acx_unlock(priv, flags);
710 acx_sem_unlock(priv);
712 free_netdev(priv->netdev);
713 end:
714 FN_EXIT0;
718 /***********************************************************************
719 ** acx100usb_boot():
720 ** Inputs:
721 ** usbdev -> Pointer to kernel's usb_device structure
722 ** endpoint -> Address of the endpoint for control transfers
723 ************************************************************************
724 ** Returns:
725 ** (int) Errorcode or 0 on success
727 ** Description:
728 ** This function triggers the loading of the firmware image from harddisk
729 ** and then uploads the firmware to the USB device. After uploading the
730 ** firmware and transmitting the checksum, the device resets and appears
731 ** as a new device on the USB bus (the device we can finally deal with)
733 static int
734 acx100usb_boot(struct usb_device *usbdev)
736 static const char filename[] = "tiacx100usb";
738 char *firmware = NULL;
739 char *usbbuf;
740 unsigned int offset;
741 unsigned int len, inpipe, outpipe;
742 u32 checksum;
743 u32 size;
744 int result;
746 FN_ENTER;
748 usbbuf = kmalloc(ACX100_USB_RWMEM_MAXLEN, GFP_KERNEL);
749 if (!usbbuf) {
750 printk(KERN_ERR "acx: no memory for USB transfer buffer ("
751 STRING(ACX100_USB_RWMEM_MAXLEN)" bytes)\n");
752 result = -ENOMEM;
753 goto end;
755 firmware = (char *)acx_s_read_fw(&usbdev->dev, filename, &size);
756 if (!firmware) {
757 result = -EIO;
758 goto end;
760 acxlog(L_INIT, "firmware size: %d bytes\n", size);
762 /* Obtain the I/O pipes */
763 outpipe = usb_sndctrlpipe(usbdev, 0);
764 inpipe = usb_rcvctrlpipe(usbdev, 0);
766 /* now upload the firmware, slice the data into blocks */
767 offset = 8;
768 while (offset < size) {
769 len = size - offset;
770 if (len >= ACX100_USB_RWMEM_MAXLEN) {
771 len = ACX100_USB_RWMEM_MAXLEN;
773 acxlog(L_INIT, "uploading firmware (%d bytes, offset=%d)\n",
774 len, offset);
775 result = 0;
776 memcpy(usbbuf, firmware + offset, len);
777 result = usb_control_msg(usbdev, outpipe,
778 ACX_USB_REQ_UPLOAD_FW,
779 USB_TYPE_VENDOR|USB_DIR_OUT,
780 size - 8, /* value */
781 0, /* index */
782 usbbuf, /* dataptr */
783 len, /* size */
784 3000 /* timeout in ms */
786 offset += len;
787 if (result < 0) {
788 printk(KERN_ERR "acx: error %d during upload "
789 "of firmware, aborting\n", result);
790 goto end;
794 /* finally, send the checksum and reboot the device */
795 checksum = le32_to_cpu(*(u32 *)firmware);
796 /* is this triggers the reboot? */
797 result = usb_control_msg(usbdev, outpipe,
798 ACX_USB_REQ_UPLOAD_FW,
799 USB_TYPE_VENDOR|USB_DIR_OUT,
800 checksum & 0xffff, /* value */
801 checksum >> 16, /* index */
802 NULL, /* dataptr */
803 0, /* size */
804 3000 /* timeout in ms */
806 if (result < 0) {
807 printk(KERN_ERR "acx: error %d during tx of checksum, "
808 "aborting\n", result);
809 goto end;
811 result = usb_control_msg(usbdev, inpipe,
812 ACX_USB_REQ_ACK_CS,
813 USB_TYPE_VENDOR|USB_DIR_IN,
814 checksum & 0xffff, /* value */
815 checksum >> 16, /* index */
816 usbbuf, /* dataptr */
817 8, /* size */
818 3000 /* timeout in ms */
820 if (result < 0) {
821 printk(KERN_ERR "acx: error %d during ACK of checksum, "
822 "aborting\n", result);
823 goto end;
825 if (*usbbuf != 0x10) {
826 kfree(usbbuf);
827 printk(KERN_ERR "acx: invalid checksum?\n");
828 result = -EINVAL;
829 goto end;
831 result = 0;
832 end:
833 vfree(firmware);
834 kfree(usbbuf);
836 FN_EXIT1(result);
837 return result;
841 /***********************************************************************
842 ** acx100usb_e_init_network_device():
843 ** Inputs:
844 ** dev -> Pointer to network device
845 ************************************************************************
846 ** Description:
847 ** Basic setup of a network device for use with the WLAN device.
849 static int
850 acx100usb_e_init_network_device(struct net_device *dev)
852 wlandevice_t *priv;
853 int result = 0;
855 FN_ENTER;
857 /* Setup the device and stop the queue */
858 ether_setup(dev);
859 acx_stop_queue(dev, "on init");
861 priv = netdev_priv(dev);
863 acx_sem_lock(priv);
865 /* put the ACX100 out of sleep mode */
866 acx_s_issue_cmd(priv, ACX1xx_CMD_WAKE, NULL, 0);
868 /* Register the callbacks for the network device functions */
869 dev->open = &acx100usb_e_open;
870 dev->stop = &acx100usb_e_close;
871 dev->hard_start_xmit = (void *)&acx_i_start_xmit;
872 dev->get_stats = (void *)&acx_e_get_stats;
873 dev->get_wireless_stats = (void *)&acx_e_get_wireless_stats;
874 #if WIRELESS_EXT >= 13
875 dev->wireless_handlers = (struct iw_handler_def *)&acx_ioctl_handler_def;
876 #else
877 dev->do_ioctl = (void *)&acx_e_ioctl_old;
878 #endif
879 dev->set_multicast_list = (void *)&acx100usb_i_set_rx_mode;
880 #ifdef HAVE_TX_TIMEOUT
881 dev->tx_timeout = &acx100usb_i_tx_timeout;
882 dev->watchdog_timeo = 4 * HZ;
883 #endif
884 dev->change_mtu = &acx_e_change_mtu;
885 result = acx_s_init_mac(dev);
886 if (OK != result)
887 goto end;
888 result = acx_s_set_defaults(priv);
889 if (OK != result) {
890 printk("%s: acx_set_defaults FAILED\n", dev->name);
891 goto end;
894 SET_MODULE_OWNER(dev);
895 end:
896 acx_sem_unlock(priv);
898 FN_EXIT1(result);
899 return result;
903 /***********************************************************************
904 ** acx100usb_e_open
905 ** This function is called when the user sets up the network interface.
906 ** It initializes a management timer, sets up the USB card and starts
907 ** the network tx queue and USB receive.
909 static int
910 acx100usb_e_open(struct net_device *dev)
912 wlandevice_t *priv = netdev_priv(dev);
913 unsigned long flags;
914 int i;
916 FN_ENTER;
918 acx_sem_lock(priv);
920 /* put the ACX100 out of sleep mode */
921 acx_s_issue_cmd(priv, ACX1xx_CMD_WAKE, NULL, 0);
923 acx_init_task_scheduler(priv);
925 init_timer(&priv->mgmt_timer);
926 priv->mgmt_timer.function = acx_i_timer;
927 priv->mgmt_timer.data = (unsigned long)priv;
929 /* acx_s_start needs it */
930 SET_BIT(priv->dev_state_mask, ACX_STATE_IFACE_UP);
931 acx_s_start(priv);
933 acx_start_queue(dev, "on open");
935 acx_lock(priv, flags);
936 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++) {
937 acx100usb_l_poll_rx(priv, &priv->usb_rx[i]);
939 acx_unlock(priv, flags);
941 WLAN_MOD_INC_USE_COUNT;
943 acx_sem_unlock(priv);
945 FN_EXIT0;
946 return 0;
950 /***********************************************************************
951 ** acx100usb_l_poll_rx
952 ** This function (re)initiates a bulk-in USB transfer on given urb
954 static void
955 acx100usb_l_poll_rx(wlandevice_t *priv, usb_rx_t* rx)
957 struct usb_device *usbdev;
958 struct urb *rxurb;
959 int errcode, rxnum;
960 unsigned int inpipe;
962 FN_ENTER;
964 rxurb = rx->urb;
965 usbdev = priv->usbdev;
967 rxnum = rx - priv->usb_rx;
969 inpipe = usb_rcvbulkpipe(usbdev, priv->bulkinep);
970 if (rxurb->status == -EINPROGRESS) {
971 printk(KERN_ERR "acx: error, rx triggered while rx urb in progress\n");
972 /* FIXME: this is nasty, receive is being cancelled by this code
973 * on the other hand, this should not happen anyway...
975 usb_unlink_urb(rxurb);
976 } else if (rxurb->status == -ECONNRESET) {
977 acxlog(L_USBRXTX, "acx_usb: _poll_rx: connection reset\n");
978 goto end;
980 rxurb->actual_length = 0;
981 usb_fill_bulk_urb(rxurb, usbdev, inpipe,
982 &rx->bulkin, /* dataptr */
983 RXBUFSIZE, /* size */
984 acx100usb_i_complete_rx, /* handler */
985 rx /* handler param */
987 rxurb->transfer_flags = URB_ASYNC_UNLINK;
989 /* ATOMIC: we may be called from complete_rx() usb callback */
990 errcode = usb_submit_urb(rxurb, GFP_ATOMIC);
991 /* FIXME: evaluate the error code! */
992 acxlog(L_USBRXTX, "SUBMIT RX (%d) inpipe=0x%X size=%d errcode=%d\n",
993 rxnum, inpipe, (int) RXBUFSIZE, errcode);
994 end:
995 FN_EXIT0;
999 /***********************************************************************
1000 ** acx100usb_i_complete_rx():
1001 ** Inputs:
1002 ** urb -> Pointer to USB request block
1003 ** regs -> Pointer to register-buffer for syscalls (see asm/ptrace.h)
1004 ************************************************************************
1005 ** Description:
1006 ** This function is invoked by USB subsystem whenever a bulk receive
1007 ** request returns.
1008 ** The received data is then committed to the network stack and the next
1009 ** USB receive is triggered.
1011 static void
1012 acx100usb_i_complete_rx(struct urb *urb, struct pt_regs *regs)
1014 wlandevice_t *priv;
1015 rxbuffer_t *ptr;
1016 rxbuffer_t *inbuf;
1017 usb_rx_t *rx;
1018 unsigned long flags;
1019 int size, remsize, packetsize, rxnum;
1021 FN_ENTER;
1023 BUG_ON(!urb->context);
1025 rx = (usb_rx_t *)urb->context;
1026 priv = rx->priv;
1028 acx_lock(priv, flags);
1031 * Happens on disconnect or close. Don't play with the urb.
1032 * Don't resubmit it. It will get unlinked by close()
1034 if (!(priv->dev_state_mask & ACX_STATE_IFACE_UP)) {
1035 acxlog(L_USBRXTX, "not doing anything.\n");
1036 goto end_unlock;
1039 inbuf = &rx->bulkin;
1040 size = urb->actual_length;
1041 remsize = size;
1042 rxnum = rx - priv->usb_rx;
1044 acxlog(L_USBRXTX, "RETURN RX (%d) status=%d size=%d\n",
1045 rxnum, urb->status, size);
1047 if(size > sizeof(rxbuffer_t))
1048 printk("acx_usb: rx too large: %d bytes, lease report\n", size);
1050 /* check if the transfer was aborted */
1051 switch (urb->status) {
1052 case 0: /* No error */
1053 break;
1054 case -EOVERFLOW:
1055 printk(KERN_ERR "acx: error in rx, data overrun -> emergency stop\n");
1056 /* LOCKING BUG! acx100usb_e_close(priv->netdev); */
1057 goto end_unlock;
1058 case -ECONNRESET:
1059 goto end_unlock;
1060 case -ESHUTDOWN: /* rmmod */
1061 goto end_unlock;
1062 default:
1063 priv->stats.rx_errors++;
1064 printk("acx: rx error (urb status=%d)\n", urb->status);
1065 goto do_poll_rx;
1068 if (!size)
1069 printk("acx: warning, encountered zerolength rx packet\n");
1071 if (urb->transfer_buffer != inbuf)
1072 goto do_poll_rx;
1074 /* check if previous frame was truncated
1075 ** FIXME: this code can only handle truncation
1076 ** of consecutive packets!
1078 ptr = inbuf;
1079 if (priv->rxtruncsize) {
1080 int tail_size;
1082 ptr = &priv->rxtruncbuf;
1083 packetsize = RXBUF_BYTES_USED(ptr);
1084 if (acx_debug & L_USBRXTX) {
1085 printk("handling truncated frame (truncsize=%d size=%d "
1086 "packetsize(from trunc)=%d)\n",
1087 priv->rxtruncsize, size, packetsize);
1088 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1089 acx_dump_bytes(inbuf, RXBUF_HDRSIZE);
1092 /* bytes needed for rxtruncbuf completion: */
1093 tail_size = packetsize - priv->rxtruncsize;
1095 if (size < tail_size) {
1096 /* there is not enough data to complete this packet,
1097 ** simply append the stuff to the truncation buffer
1099 memcpy(((char *)ptr) + priv->rxtruncsize, inbuf, size);
1100 priv->rxtruncsize += size;
1101 remsize = 0;
1102 } else {
1103 /* ok, this data completes the previously
1104 ** truncated packet. copy it into a descriptor
1105 ** and give it to the rest of the stack */
1107 /* append tail to previously truncated part
1108 ** NB: priv->rxtruncbuf (pointed to by ptr) can't
1109 ** overflow because this is already checked before
1110 ** truncation buffer was filled. See below,
1111 ** "if (packetsize > sizeof(rxbuffer_t))..." code */
1112 memcpy(((char *)ptr) + priv->rxtruncsize, inbuf, tail_size);
1114 if (acx_debug & L_USBRXTX) {
1115 printk("full trailing packet + 12 bytes:\n");
1116 acx_dump_bytes(inbuf, tail_size + RXBUF_HDRSIZE);
1118 acx_l_process_rxbuf(priv, ptr);
1119 priv->rxtruncsize = 0;
1120 ptr = (rxbuffer_t *) (((char *)inbuf) + tail_size);
1121 remsize -= tail_size;
1123 acxlog(L_USBRXTX, "post-merge size=%d remsize=%d\n",
1124 size, remsize);
1127 /* size = USB data block size
1128 ** remsize = unprocessed USB bytes left
1129 ** ptr = current pos in USB data block
1131 while (remsize) {
1132 if (remsize < RXBUF_HDRSIZE) {
1133 printk("acx: truncated rx header (%d bytes)!\n",
1134 remsize);
1135 break;
1137 packetsize = RXBUF_BYTES_USED(ptr);
1138 acxlog(L_USBRXTX, "packet with packetsize=%d\n", packetsize);
1139 if (packetsize > sizeof(rxbuffer_t)) {
1140 printk("acx: packet exceeds max wlan "
1141 "frame size (%d > %d). size=%d\n",
1142 packetsize, (int) sizeof(rxbuffer_t), size);
1143 /* FIXME: put some real error-handling in here! */
1144 break;
1147 /* skip null packets (does this really happen?!) */
1148 if (packetsize == RXBUF_HDRSIZE) {
1149 remsize -= RXBUF_HDRSIZE;
1150 if (acx_debug & L_USBRXTX) {
1151 printk("acx: null packet, new remsize=%d. "
1152 "header follows:\n", remsize);
1153 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1155 ptr = (rxbuffer_t *)(((char *)ptr) + RXBUF_HDRSIZE);
1156 continue;
1159 if (packetsize > remsize) {
1160 /* frame truncation handling */
1161 if (acx_debug & L_USBRXTX) {
1162 printk("need to truncate packet, "
1163 "packetsize=%d remsize=%d "
1164 "size=%d\n",
1165 packetsize, remsize, size);
1166 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1168 memcpy(&priv->rxtruncbuf, ptr, remsize);
1169 priv->rxtruncsize = remsize;
1170 break;
1171 } else { /* packetsize <= remsize */
1172 /* now handle the received data */
1173 acx_l_process_rxbuf(priv, ptr);
1175 ptr = (rxbuffer_t *)(((char *)ptr) + packetsize);
1176 remsize -= packetsize;
1177 if ((acx_debug & L_USBRXTX) && remsize) {
1178 printk("more than one packet in buffer, "
1179 "second packet hdr follows\n");
1180 acx_dump_bytes(ptr, RXBUF_HDRSIZE);
1185 do_poll_rx:
1186 /* receive of frame completed, now look for the next one */
1187 acx100usb_l_poll_rx(priv, rx);
1188 end_unlock:
1189 acx_unlock(priv, flags);
1190 /* end: */
1191 FN_EXIT0;
1195 /***********************************************************************
1196 ** acx100usb_i_complete_tx():
1197 ** Inputs:
1198 ** urb -> Pointer to USB request block
1199 ** regs -> Pointer to register-buffer for syscalls (see asm/ptrace.h)
1200 ************************************************************************
1201 ** Description:
1202 ** This function is invoked upon termination of a USB transfer. As the
1203 ** USB device is only capable of sending a limited amount of bytes per
1204 ** transfer to the bulk-out endpoint, this routine checks if there are
1205 ** more bytes to send and triggers subsequent transfers. In case the
1206 ** transfer size exactly matches the maximum bulk-out size, it triggers
1207 ** a transfer of a null-frame, telling the card that this is it. Upon
1208 ** completion of a frame, it checks whether the Tx ringbuffer contains
1209 ** more data to send and invokes the Tx routines if this is the case.
1210 ** If there are no more occupied Tx descriptors, the Tx Mutex is unlocked
1211 ** and the network queue is switched back to life again.
1213 ** FIXME: unlike PCI code, we do not analyze tx rate used, retries, etc...
1214 ** Thus we have no automatic rate control in USB!
1216 static void
1217 acx100usb_i_complete_tx(struct urb *urb, struct pt_regs *regs)
1219 wlandevice_t *priv;
1220 usb_tx_t *tx;
1221 unsigned long flags;
1222 int txnum;
1224 FN_ENTER;
1226 BUG_ON(!urb->context);
1228 tx = (usb_tx_t *)urb->context;
1229 priv = tx->priv;
1231 txnum = tx - priv->usb_tx;
1233 acx_lock(priv, flags);
1236 * If the iface isn't up, we don't have any right
1237 * to play with them. The urb may get unlinked.
1239 if (!(priv->dev_state_mask & ACX_STATE_IFACE_UP)) {
1240 acxlog(L_USBRXTX, "not doing anything\n");
1241 goto end_unlock;
1242 /*FIXME: Do the priv->tx_free++? */
1245 acxlog(L_USBRXTX, "RETURN TX (%d): status=%d size=%d\n",
1246 txnum, urb->status, urb->actual_length);
1248 /* handle USB transfer errors */
1249 switch (urb->status) {
1250 case 0: /* No error */
1251 break;
1252 case -ESHUTDOWN:
1253 goto end_unlock;
1254 break;
1255 case -ECONNRESET:
1256 goto end_unlock;
1257 break;
1258 /* FIXME: real error-handling code here please */
1259 default:
1260 printk(KERN_ERR "acx: tx error, urb status=%d\n", urb->status);
1261 /* FIXME: real error-handling code here please */
1264 /* free the URB and check for more data */
1265 priv->tx_free++;
1266 tx->busy = 0;
1268 end_unlock:
1269 acx_unlock(priv, flags);
1270 /* end: */
1271 FN_EXIT0;
1275 /***********************************************************************
1276 ** acx100usb_e_close()
1278 ** This function stops the network functionality of the interface (invoked
1279 ** when the user calls ifconfig <wlan> down). The tx queue is halted and
1280 ** the device is marked as down. In case there were any pending USB bulk
1281 ** transfers, these are unlinked (asynchronously). The module in-use count
1282 ** is also decreased in this function.
1284 static int
1285 acx100usb_e_close(struct net_device *dev)
1287 wlandevice_t *priv = netdev_priv(dev);
1288 unsigned long flags;
1289 int i;
1291 FN_ENTER;
1293 #ifdef WE_STILL_DONT_CARE_ABOUT_IT
1294 /* Transmit a disassociate frame */
1295 lock
1296 acx_l_transmit_disassoc(priv, &client);
1297 unlock
1298 #endif
1300 acx_sem_lock(priv);
1302 /* Make sure we don't get any more rx requests */
1303 acx_s_issue_cmd(priv, ACX1xx_CMD_DISABLE_RX, NULL, 0);
1304 acx_s_issue_cmd(priv, ACX1xx_CMD_DISABLE_TX, NULL, 0);
1307 * We must do FLUSH *without* holding sem to avoid a deadlock.
1308 * See pci.c:acxpci_s_down() for deails.
1310 acx_sem_unlock(priv);
1311 FLUSH_SCHEDULED_WORK();
1312 acx_sem_lock(priv);
1314 /* Power down the device */
1315 acx_s_issue_cmd(priv, ACX1xx_CMD_SLEEP, NULL, 0);
1317 /* Stop the transmit queue, mark the device as DOWN */
1318 acx_lock(priv, flags);
1319 acx_stop_queue(dev, "on iface stop");
1320 CLEAR_BIT(priv->dev_state_mask, ACX_STATE_IFACE_UP);
1321 /* stop pending rx/tx urb transfers */
1322 /* Make sure you don't free them! */
1323 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++) {
1324 acxusb_unlink_urb(priv->usb_rx[i].urb);
1325 acxusb_unlink_urb(priv->usb_tx[i].urb);
1328 del_timer_sync(&priv->mgmt_timer);
1330 priv->tx_free = ACX100_USB_NUM_BULK_URBS;
1332 for (i = 0; i< ACX100_USB_NUM_BULK_URBS; ++i) {
1333 priv->usb_tx[i].busy = 0;
1334 priv->usb_rx[i].busy = 0;
1337 acx_unlock(priv, flags);
1339 acx_sem_unlock(priv);
1341 /* Decrease module-in-use count (if necessary) */
1343 WLAN_MOD_DEC_USE_COUNT;
1345 FN_EXIT0;
1346 return 0;
1350 /***************************************************************
1351 ** acxusb_l_alloc_tx
1352 ** Actually returns a usb_tx_t* ptr
1354 tx_t*
1355 acxusb_l_alloc_tx(wlandevice_t* priv)
1357 int i;
1358 usb_tx_t *tx = NULL;
1360 FN_ENTER;
1362 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++) {
1363 if (!priv->usb_tx[i].busy) {
1364 acxlog(L_USBRXTX, "allocated tx %d\n", i);
1365 tx = &priv->usb_tx[i];
1366 tx->busy = 1;
1367 break;
1370 if (i >= ACX100_USB_NUM_BULK_URBS) {
1371 printk_ratelimited("acx: tx buffers full\n");
1374 FN_EXIT0;
1376 return (tx_t*)tx;
1380 /***************************************************************
1382 void*
1383 acxusb_l_get_txbuf(wlandevice_t *priv, tx_t* tx_opaque)
1385 usb_tx_t* tx = (usb_tx_t*)tx_opaque;
1386 return &tx->bulkout.data;
1390 /***************************************************************
1391 ** acxusb_l_tx_data
1393 ** Can be called from IRQ (rx -> (AP bridging or mgmt response) -> tx).
1394 ** Can be called from acx_i_start_xmit (data frames from net core).
1396 void
1397 acxusb_l_tx_data(wlandevice_t *priv, tx_t* tx_opaque, int wlanpkt_len)
1399 struct usb_device *usbdev;
1400 struct urb* txurb;
1401 usb_tx_t* tx;
1402 usb_txbuffer_t* txbuf;
1403 client_t *clt;
1404 wlan_hdr_t* whdr;
1405 unsigned int outpipe;
1406 int ucode;
1407 u8 rate100;
1409 FN_ENTER;
1411 tx = ((usb_tx_t *)tx_opaque);
1412 txurb = tx->urb;
1413 txbuf = &tx->bulkout;
1414 whdr = (wlan_hdr_t *)txbuf->data;
1416 priv->tx_free--;
1417 acxlog(L_DEBUG, "using buf#%d free=%d len=%d\n",
1418 (int)(tx - priv->usb_tx),
1419 priv->tx_free, wlanpkt_len);
1421 switch (priv->mode) {
1422 case ACX_MODE_0_ADHOC:
1423 case ACX_MODE_3_AP:
1424 clt = acx_l_sta_list_get(priv, whdr->a1);
1425 break;
1426 case ACX_MODE_2_STA:
1427 clt = priv->ap_client;
1428 break;
1429 default: /* ACX_MODE_OFF, ACX_MODE_MONITOR */
1430 clt = NULL;
1431 break;
1434 if (unlikely(clt && !clt->rate_cur)) {
1435 printk("acx: driver bug! bad ratemask\n");
1436 goto end;
1439 /* used in tx cleanup routine for auto rate and accounting: */
1440 //TODO: currently unused - fix that
1441 tx->txc = clt;
1443 rate100 = clt ? clt->rate_100 : priv->rate_bcast100;
1445 /* fill the USB transfer header */
1446 txbuf->desc = cpu_to_le16(USB_TXBUF_TXDESC);
1447 txbuf->MPDUlen = cpu_to_le16(wlanpkt_len);
1448 txbuf->ctrl1 = 0;
1449 txbuf->ctrl2 = 0;
1450 txbuf->hostData = cpu_to_le32(wlanpkt_len | (rate100 << 24));
1451 if (1 == priv->preamble_cur)
1452 SET_BIT(txbuf->ctrl1, DESC_CTL_SHORT_PREAMBLE);
1453 SET_BIT(txbuf->ctrl1, DESC_CTL_FIRSTFRAG);
1454 txbuf->txRate = rate100;
1455 txbuf->index = 1;
1456 txbuf->dataLength = cpu_to_le16(wlanpkt_len);
1458 if ( (WF_FC_FTYPEi & whdr->fc) == WF_FTYPE_DATAi )
1459 SET_BIT(txbuf->hostData, cpu_to_le32(USB_TXBUF_HD_ISDATA));
1460 if (mac_is_directed(whdr->a1))
1461 SET_BIT(txbuf->hostData, cpu_to_le32(USB_TXBUF_HD_DIRECTED));
1462 else if (mac_is_bcast(whdr->a1))
1463 SET_BIT(txbuf->hostData, cpu_to_le32(USB_TXBUF_HD_BROADCAST));
1465 if (acx_debug & L_DATA) {
1466 printk("dump of bulk out urb:\n");
1467 acx_dump_bytes(txbuf, wlanpkt_len + USB_TXBUF_HDRSIZE);
1470 if (txurb->status == -EINPROGRESS) {
1471 printk("acx: trying to submit tx urb while already in progress\n");
1474 /* now schedule the USB transfer */
1475 usbdev = priv->usbdev;
1476 outpipe = usb_sndbulkpipe(usbdev, priv->bulkoutep);
1478 usb_fill_bulk_urb(txurb, usbdev, outpipe,
1479 txbuf, /* dataptr */
1480 wlanpkt_len + USB_TXBUF_HDRSIZE, /* size */
1481 acx100usb_i_complete_tx, /* handler */
1482 tx /* handler param */
1485 txurb->transfer_flags = URB_ASYNC_UNLINK|URB_ZERO_PACKET;
1486 ucode = usb_submit_urb(txurb, GFP_ATOMIC);
1487 acxlog(L_USBRXTX, "SUBMIT TX (%p): outpipe=0x%X buf=%p txsize=%d "
1488 "errcode=%d\n", tx, outpipe, txbuf,
1489 wlanpkt_len + USB_TXBUF_HDRSIZE, ucode);
1491 if (ucode) {
1492 printk(KERN_ERR "acx: submit_urb() error=%d txsize=%d\n",
1493 ucode, wlanpkt_len + USB_TXBUF_HDRSIZE);
1495 /* on error, just mark the frame as done and update
1496 ** the statistics
1498 priv->stats.tx_errors++;
1499 tx->busy = 0;
1500 priv->tx_free++;
1502 end:
1503 FN_EXIT0;
1507 /***********************************************************************
1509 static void
1510 acx100usb_i_set_rx_mode(struct net_device *dev)
1515 /***********************************************************************
1517 #ifdef HAVE_TX_TIMEOUT
1518 static void
1519 acx100usb_i_tx_timeout(struct net_device *dev)
1521 wlandevice_t *priv = netdev_priv(dev);
1522 unsigned long flags;
1523 int i;
1525 FN_ENTER;
1527 acx_lock(priv, flags);
1528 /* unlink the URBs */
1529 for (i = 0; i < ACX100_USB_NUM_BULK_URBS; i++) {
1530 acxusb_unlink_urb(priv->usb_tx[i].urb);
1532 /* TODO: stats update */
1533 acx_unlock(priv, flags);
1535 FN_EXIT0;
1537 #endif
1540 /***********************************************************************
1541 ** init_module():
1543 ** This function is invoked upon loading of the kernel module.
1544 ** It registers itself at the kernel's USB subsystem.
1546 ** Returns: Errorcode on failure, 0 on success
1548 int __init
1549 acxusb_e_init_module(void)
1551 acxlog(L_INIT, "USB module " WLAN_RELEASE " initialized, "
1552 "probing for devices...\n");
1553 return usb_register(&acx100usb_driver);
1558 /***********************************************************************
1559 ** cleanup_module():
1561 ** This function is invoked as last step of the module unloading. It simply
1562 ** deregisters this module at the kernel's USB subsystem.
1564 void __exit
1565 acxusb_e_cleanup_module()
1567 usb_deregister(&acx100usb_driver);
1571 /***********************************************************************
1572 ** DEBUG STUFF
1574 #if ACX_DEBUG
1576 #ifdef UNUSED
1577 static void
1578 dump_device(struct usb_device *usbdev)
1580 int i;
1581 struct usb_config_descriptor *cd;
1583 printk("acx device dump:\n");
1584 printk(" devnum: %d\n", usbdev->devnum);
1585 printk(" speed: %d\n", usbdev->speed);
1586 printk(" tt: 0x%X\n", (unsigned int)(usbdev->tt));
1587 printk(" ttport: %d\n", (unsigned int)(usbdev->ttport));
1588 printk(" toggle[0]: 0x%X toggle[1]: 0x%X\n", (unsigned int)(usbdev->toggle[0]), (unsigned int)(usbdev->toggle[1]));
1589 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 8)
1590 /* halted removed in 2.6.9-rc1 */
1591 /* DOH, Canbreak... err... Mandrake decided to do their very own very
1592 * special version "2.6.8.1" which already includes this change, so we
1593 * need to blacklist that version already (i.e. 2.6.8) */
1594 printk(" halted[0]: 0x%X halted[1]: 0x%X\n", usbdev->halted[0], usbdev->halted[1]);
1595 #endif
1596 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 11)
1597 /* This saw a change after 2.6.10 */
1598 printk(" ep_in wMaxPacketSize: ");
1599 for (i = 0; i < 16; ++i)
1600 printk("%d ", usbdev->ep_in[i]->desc.wMaxPacketSize);
1601 printk("\n");
1602 printk(" ep_out wMaxPacketSize: ");
1603 for (i = 0; i < 15; ++i)
1604 printk("%d ", usbdev->ep_out[i]->desc.wMaxPacketSize);
1605 printk("\n");
1606 #else
1607 printk(" epmaxpacketin: ");
1608 for (i = 0; i < 16; i++)
1609 printk("%d ", usbdev->epmaxpacketin[i]);
1610 printk("\n");
1611 printk(" epmaxpacketout: ");
1612 for (i = 0; i < 16; i++)
1613 printk("%d ", usbdev->epmaxpacketout[i]);
1614 printk("\n");
1615 #endif
1616 printk(" parent: 0x%X\n", (unsigned int)usbdev->parent);
1617 printk(" bus: 0x%X\n", (unsigned int)usbdev->bus);
1618 #if NO_DATATYPE
1619 printk(" configs: ");
1620 for (i = 0; i < usbdev->descriptor.bNumConfigurations; i++)
1621 printk("0x%X ", usbdev->config[i]);
1622 printk("\n");
1623 #endif
1624 printk(" actconfig: %p\n", usbdev->actconfig);
1625 dump_device_descriptor(&usbdev->descriptor);
1627 cd = &usbdev->config->desc;
1628 dump_config_descriptor(cd);
1632 /***********************************************************************
1634 static void
1635 dump_config_descriptor(struct usb_config_descriptor *cd)
1637 printk("Configuration Descriptor:\n");
1638 if (!cd) {
1639 printk("NULL\n");
1640 return;
1642 printk(" bLength: %d (0x%X)\n", cd->bLength, cd->bLength);
1643 printk(" bDescriptorType: %d (0x%X)\n", cd->bDescriptorType, cd->bDescriptorType);
1644 printk(" bNumInterfaces: %d (0x%X)\n", cd->bNumInterfaces, cd->bNumInterfaces);
1645 printk(" bConfigurationValue: %d (0x%X)\n", cd->bConfigurationValue, cd->bConfigurationValue);
1646 printk(" iConfiguration: %d (0x%X)\n", cd->iConfiguration, cd->iConfiguration);
1647 printk(" bmAttributes: %d (0x%X)\n", cd->bmAttributes, cd->bmAttributes);
1648 /* printk(" MaxPower: %d (0x%X)\n", cd->bMaxPower, cd->bMaxPower); */
1652 static void
1653 dump_device_descriptor(struct usb_device_descriptor *dd)
1655 printk("Device Descriptor:\n");
1656 if (!dd) {
1657 printk("NULL\n");
1658 return;
1660 printk(" bLength: %d (0x%X)\n", dd->bLength, dd->bLength);
1661 printk(" bDescriptortype: %d (0x%X)\n", dd->bDescriptorType, dd->bDescriptorType);
1662 printk(" bcdUSB: %d (0x%X)\n", dd->bcdUSB, dd->bcdUSB);
1663 printk(" bDeviceClass: %d (0x%X)\n", dd->bDeviceClass, dd->bDeviceClass);
1664 printk(" bDeviceSubClass: %d (0x%X)\n", dd->bDeviceSubClass, dd->bDeviceSubClass);
1665 printk(" bDeviceProtocol: %d (0x%X)\n", dd->bDeviceProtocol, dd->bDeviceProtocol);
1666 printk(" bMaxPacketSize0: %d (0x%X)\n", dd->bMaxPacketSize0, dd->bMaxPacketSize0);
1667 printk(" idVendor: %d (0x%X)\n", dd->idVendor, dd->idVendor);
1668 printk(" idProduct: %d (0x%X)\n", dd->idProduct, dd->idProduct);
1669 printk(" bcdDevice: %d (0x%X)\n", dd->bcdDevice, dd->bcdDevice);
1670 printk(" iManufacturer: %d (0x%X)\n", dd->iManufacturer, dd->iManufacturer);
1671 printk(" iProduct: %d (0x%X)\n", dd->iProduct, dd->iProduct);
1672 printk(" iSerialNumber: %d (0x%X)\n", dd->iSerialNumber, dd->iSerialNumber);
1673 printk(" bNumConfigurations: %d (0x%X)\n", dd->bNumConfigurations, dd->bNumConfigurations);
1675 #endif /* UNUSED */
1677 #endif /* ACX_DEBUG */