46 #define DRIVER_NAME "orinoco_usb"
47 #define PFX DRIVER_NAME ": "
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/signal.h>
53 #include <linux/errno.h>
54 #include <linux/poll.h>
56 #include <linux/slab.h>
57 #include <linux/fcntl.h>
59 #include <linux/list.h>
63 #include <linux/netdevice.h>
64 #include <linux/if_arp.h>
66 #include <linux/wireless.h>
72 #ifndef URB_ASYNC_UNLINK
73 #define URB_ASYNC_UNLINK 0
77 static const u8 encaps_hdr[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
78 #define ENCAPS_OVERHEAD (sizeof(encaps_hdr) + 2)
104 #ifdef CONFIG_USB_DEBUG
105 static int debug = 1;
112 #define dbg(format, arg...) \
113 do { if (debug) printk(KERN_DEBUG PFX "%s: " format "\n", \
114 __func__ , ## arg); } while (0)
116 #define err(format, arg...) \
117 do { printk(KERN_ERR PFX format "\n", ## arg); } while (0)
135 #define USB_COMPAQ_VENDOR_ID 0x049f
136 #define USB_COMPAQ_WL215_ID 0x001f
137 #define USB_COMPAQ_W200_ID 0x0076
138 #define USB_HP_WL215_ID 0x0082
140 #define USB_MELCO_VENDOR_ID 0x0411
141 #define USB_BUFFALO_L11_ID 0x0006
142 #define USB_BUFFALO_L11G_WR_ID 0x000B
143 #define USB_BUFFALO_L11G_ID 0x000D
145 #define USB_LUCENT_VENDOR_ID 0x047E
146 #define USB_LUCENT_ORINOCO_ID 0x0300
148 #define USB_AVAYA8_VENDOR_ID 0x0D98
149 #define USB_AVAYAE_VENDOR_ID 0x0D9E
150 #define USB_AVAYA_WIRELESS_ID 0x0300
152 #define USB_AGERE_VENDOR_ID 0x0D4E
153 #define USB_AGERE_MODEL0801_ID 0x1000
154 #define USB_AGERE_MODEL0802_ID 0x1001
155 #define USB_AGERE_REBRANDED_ID 0x047A
157 #define USB_ELSA_VENDOR_ID 0x05CC
158 #define USB_ELSA_AIRLANCER_ID 0x3100
160 #define USB_LEGEND_VENDOR_ID 0x0E7C
161 #define USB_LEGEND_JOYNET_ID 0x0300
163 #define USB_SAMSUNG_VENDOR_ID 0x04E8
164 #define USB_SAMSUNG_SEW2001U1_ID 0x5002
165 #define USB_SAMSUNG_SEW2001U2_ID 0x5B11
166 #define USB_SAMSUNG_SEW2003U_ID 0x7011
168 #define USB_IGATE_VENDOR_ID 0x0681
169 #define USB_IGATE_IGATE_11M_ID 0x0012
171 #define USB_FUJITSU_VENDOR_ID 0x0BF8
172 #define USB_FUJITSU_E1100_ID 0x1002
174 #define USB_2WIRE_VENDOR_ID 0x1630
175 #define USB_2WIRE_WIRELESS_ID 0xff81
178 #define EZUSB_REQUEST_FW_TRANS 0xA0
179 #define EZUSB_REQUEST_TRIGER 0xAA
180 #define EZUSB_REQUEST_TRIG_AC 0xAC
181 #define EZUSB_CPUCS_REG 0x7F92
183 #define EZUSB_RID_TX 0x0700
184 #define EZUSB_RID_RX 0x0701
185 #define EZUSB_RID_INIT1 0x0702
186 #define EZUSB_RID_ACK 0x0710
187 #define EZUSB_RID_READ_PDA 0x0800
188 #define EZUSB_RID_PROG_INIT 0x0852
189 #define EZUSB_RID_PROG_SET_ADDR 0x0853
190 #define EZUSB_RID_PROG_BYTES 0x0854
191 #define EZUSB_RID_PROG_END 0x0855
192 #define EZUSB_RID_DOCMD 0x0860
195 #define EZUSB_IS_INFO(id) ((id >= 0xF000) && (id <= 0xF2FF))
197 #define EZUSB_MAGIC 0x0210
199 #define EZUSB_FRAME_DATA 1
200 #define EZUSB_FRAME_CONTROL 2
202 #define DEF_TIMEOUT (3 * HZ)
204 #define BULK_BUF_SIZE 2048
206 #define MAX_DL_SIZE (BULK_BUF_SIZE - sizeof(struct ezusb_packet))
208 #define FW_BUF_SIZE 64
209 #define FW_VAR_OFFSET_PTR 0x359
210 #define FW_VAR_VALUE 0
211 #define FW_HOLE_START 0x100
212 #define FW_HOLE_END 0x300
303 static void ezusb_req_queue_run(
struct ezusb_priv *upriv);
304 static void ezusb_bulk_in_callback(
struct urb *
urb);
306 static inline u8 ezusb_reply_inc(
u8 count)
327 static inline void ezusb_mod_timer(
struct ezusb_priv *upriv,
329 unsigned long expire)
336 static void ezusb_request_timerfn(
u_long _ctx)
345 dbg(
"couldn't unlink");
348 ezusb_ctx_complete(ctx);
349 ezusb_request_context_put(ctx);
380 init_completion(&ctx->
done);
383 ctx->
timer.function = ezusb_request_timerfn;
391 static inline void ezusb_complete_all(
struct completion *comp)
406 list_del_init(&ctx->
list);
408 spin_unlock_irqrestore(&upriv->
req_lock, flags);
409 ezusb_req_queue_run(upriv);
413 switch (ctx->
state) {
419 spin_unlock_irqrestore(&upriv->
req_lock, flags);
431 netif_wake_queue(dev);
433 ezusb_complete_all(&ctx->
done);
434 ezusb_request_context_put(ctx);
438 spin_unlock_irqrestore(&upriv->
req_lock, flags);
442 err(
"Called, CTX not terminating, but device gone");
443 ezusb_complete_all(&ctx->
done);
444 ezusb_request_context_put(ctx);
448 err(
"Called, CTX not in terminating state.");
465 static void ezusb_req_queue_run(
struct ezusb_priv *upriv)
483 if (!ctx->
upriv->udev)
495 spin_unlock_irqrestore(&upriv->
req_lock, flags);
497 err(
"Fatal, failed to submit command urb."
498 " error=%d\n", result);
500 ezusb_ctx_complete(ctx);
501 ezusb_request_context_put(ctx);
511 spin_unlock_irqrestore(&upriv->
req_lock, flags);
517 static void ezusb_req_enqueue_run(
struct ezusb_priv *upriv,
524 if (!ctx->
upriv->udev) {
525 spin_unlock_irqrestore(&upriv->
req_lock, flags);
530 spin_unlock_irqrestore(&upriv->
req_lock, flags);
533 ezusb_req_queue_run(upriv);
539 static void ezusb_request_out_callback(
struct urb *
urb)
551 spin_unlock_irqrestore(&upriv->
req_lock, flags);
558 if (urb->status == 0) {
564 ezusb_mod_timer(upriv, &ctx->
timer,
566 spin_unlock_irqrestore(&upriv->
req_lock,
574 spin_unlock_irqrestore(&upriv->
req_lock, flags);
575 ezusb_ctx_complete(ctx);
579 spin_unlock_irqrestore(&upriv->
req_lock, flags);
580 err(
"Unexpected state(0x%x, %d) in OUT URB",
596 spin_unlock_irqrestore(&upriv->
req_lock, flags);
598 ezusb_ctx_complete(ctx);
602 spin_unlock_irqrestore(&upriv->
req_lock, flags);
604 err(
"Unexpected state(0x%x, %d) in OUT URB",
610 ezusb_request_context_put(ctx);
613 static void ezusb_request_in_callback(
struct ezusb_priv *upriv,
632 ezusb_reply_inc(c->
buf->req_reply_count);
638 dbg(
"Skipped (0x%x/0x%x) (%d/%d)",
645 spin_unlock_irqrestore(&upriv->
req_lock, flags);
646 err(
"%s: got unexpected RID: 0x%04X", __func__,
648 ezusb_req_queue_run(upriv);
653 urb->transfer_buffer = ctx->
buf;
654 ctx->
buf = (
void *) ans;
666 spin_unlock_irqrestore(&upriv->
req_lock, flags);
680 spin_unlock_irqrestore(&upriv->
req_lock, flags);
683 ezusb_ctx_complete(ctx);
687 spin_unlock_irqrestore(&upriv->
req_lock, flags);
689 pr_warning(
"Matched IN URB, unexpected context state(0x%x)",
695 ezusb_req_queue_run(upriv);
701 static void ezusb_req_ctx_wait(
struct ezusb_priv *upriv,
704 switch (ctx->
state) {
714 while (!ctx->
done.done && msecs--)
733 for (i = 0; i < 8; i++)
734 crc = (crc << 1) + bytes[
i];
766 static int ezusb_submit_in_urb(
struct ezusb_priv *upriv)
769 void *cur_buf = upriv->
read_urb->transfer_buffer;
772 dbg(
"urb busy, not resubmiting");
778 ezusb_bulk_in_callback, upriv);
779 upriv->
read_urb->transfer_flags = 0;
782 err(
"%s submit failed %d", __func__, retval);
788 static inline int ezusb_8051_cpucs(
struct ezusb_priv *upriv,
int reset)
793 err(
"%s: !upriv->udev", __func__);
797 usb_sndctrlpipe(upriv->
udev, 0),
804 static int ezusb_firmware_download(
struct ezusb_priv *upriv,
818 if (variant_offset >= fw->
size) {
820 "0x%04x\n", variant_offset);
825 retval = ezusb_8051_cpucs(upriv, 1);
835 if (variant_offset >= addr &&
837 dbg(
"Patching card_variant byte at 0x%04X",
842 usb_sndctrlpipe(upriv->
udev, 0),
853 retval = ezusb_8051_cpucs(upriv, 0);
865 static int ezusb_access_ltv(
struct ezusb_priv *upriv,
867 u16 length,
const void *data,
u16 frame_type,
868 void *ans_buff,
int ans_size,
u16 *ans_length)
877 dbg(
"Device disconnected");
882 err(
"%s: in urb not pending", __func__);
886 req_size = ezusb_fill_req(ctx->
buf, length, ctx->
out_rid, data,
890 ezusb_request_out_callback, ctx);
895 ezusb_req_enqueue_run(upriv, ctx);
900 ezusb_req_ctx_wait(upriv, ctx);
905 retval = ctx->
outurb->status;
913 err(
"%s: Unexpected context state %d", __func__,
944 err(
"%s: length mismatch for RID 0x%04x: "
945 "expected %d, got %d", __func__,
953 min_t(
int, exp_len, ans_size));
958 ezusb_request_context_put(ctx);
962 static int ezusb_write_ltv(
struct hermes *
hw,
int bap,
u16 rid,
963 u16 length,
const void *data)
988 return ezusb_access_ltv(upriv, ctx, length, data, frame_type,
992 static int ezusb_read_ltv(
struct hermes *hw,
int bap,
u16 rid,
998 if ((bufsize < 0) || (bufsize % 2))
1001 ctx = ezusb_alloc_ctx(upriv, rid, rid);
1006 buf, bufsize, length);
1021 dbg(
"0x%04X, parm0 0x%04X, parm1 0x%04X, parm2 0x%04X",
1022 cmd, parm0, parm1, parm2);
1027 return ezusb_access_ltv(upriv, ctx,
sizeof(data), &data,
1031 static int ezusb_docmd_wait(
struct hermes *hw,
u16 cmd,
u16 parm0,
1043 dbg(
"0x%04X, parm0 0x%04X", cmd, parm0);
1048 return ezusb_access_ltv(upriv, ctx,
sizeof(data), &data,
1052 static int ezusb_bap_pread(
struct hermes *hw,
int bap,
1057 int actual_length = upriv->
read_urb->actual_length;
1060 if ((
sizeof(*ans) + offset + len) > actual_length) {
1071 if ((
sizeof(*ans) + offset + len - 4) > actual_length) {
1086 u32 pda_addr,
u16 pda_len)
1105 return ezusb_access_ltv(upriv, ctx,
sizeof(data), &data,
1110 static int ezusb_program_init(
struct hermes *hw,
u32 entry_point)
1120 return ezusb_access_ltv(upriv, ctx,
sizeof(data), &data,
1124 static int ezusb_program_end(
struct hermes *hw)
1133 return ezusb_access_ltv(upriv, ctx, 0,
NULL,
1137 static int ezusb_program_bytes(
struct hermes *hw,
const char *buf,
1149 err = ezusb_access_ltv(upriv, ctx,
sizeof(data), &data,
1158 return ezusb_access_ltv(upriv, ctx, len, buf,
1162 static int ezusb_program(
struct hermes *hw,
const char *buf,
1176 while (ch_addr < (addr + len)) {
1177 pr_debug(
"Programming subblock of length %d "
1178 "to address 0x%08x. Data @ %p\n",
1179 ch_len, ch_addr, &buf[ch_addr - addr]);
1181 err = ezusb_program_bytes(hw, &buf[ch_addr - addr],
1202 unsigned long flags;
1207 if (!netif_running(dev)) {
1213 if (netif_queue_stopped(dev)) {
1219 if (orinoco_lock(priv, &flags) != 0) {
1221 "%s: ezusb_xmit() called while hw_unavailable\n",
1226 if (!netif_carrier_ok(dev) ||
1243 buf = ctx->
buf->data;
1255 buf +=
sizeof(*tx_cntl);
1273 netif_stop_queue(dev);
1276 tx_size =
ALIGN(buf - ctx->
buf->data, 2);
1278 err = ezusb_access_ltv(upriv, ctx, tx_size,
NULL,
1282 netif_start_queue(dev);
1298 orinoco_unlock(priv, &flags);
1303 orinoco_unlock(priv, &flags);
1317 int retval = ezusb_8051_cpucs(upriv, 1);
1320 err(
"Failed to reset");
1324 retval = ezusb_8051_cpucs(upriv, 0);
1326 err(
"Failed to unreset");
1330 dbg(
"sending control message");
1332 usb_sndctrlpipe(upriv->
udev, 0),
1338 err(
"EZUSB_REQUEST_TRIGER failed retval %d", retval);
1342 dbg(
"Sending EZUSB_REQUEST_TRIG_AC");
1344 usb_sndctrlpipe(upriv->
udev, 0),
1350 err(
"EZUSB_REQUEST_TRIG_AC failed retval %d", retval);
1359 static int ezusb_init(
struct hermes *hw)
1374 ezusb_submit_in_urb(upriv);
1392 static void ezusb_bulk_in_callback(
struct urb *urb)
1400 dbg(
"disconnected");
1408 pr_warning(
"%s: urb timed out, not resubmiting", __func__);
1412 pr_warning(
"%s: connection abort, resubmiting urb",
1416 if ((urb->status == -
EILSEQ)
1417 || (urb->status == -
ENOENT)
1419 dbg(
"status %d, not resubmiting", urb->status);
1423 dbg(
"status: %d length: %d",
1424 urb->status, urb->actual_length);
1425 if (urb->actual_length <
sizeof(*ans)) {
1426 err(
"%s: short read, ignoring", __func__);
1429 crc = build_crc(ans);
1431 err(
"CRC error, ignoring packet");
1437 ezusb_request_in_callback(upriv, urb);
1438 }
else if (upriv->
dev) {
1454 ezusb_submit_in_urb(upriv);
1457 static inline void ezusb_delete(
struct ezusb_priv *upriv)
1462 unsigned long flags;
1485 spin_unlock_irqrestore(&upriv->
req_lock, flags);
1492 if (!list_empty(&ctx->
list))
1493 ezusb_ctx_complete(ctx);
1495 ezusb_request_context_put(ctx);
1498 spin_unlock_irqrestore(&upriv->
req_lock, flags);
1509 if (upriv->read_urb) {
1530 spin_unlock_bh(
lock);
1540 spin_unlock_bh(
lock);
1545 .cmd_wait = ezusb_docmd_wait,
1546 .init_cmd_wait = ezusb_doicmd_wait,
1547 .allocate = ezusb_allocate,
1548 .read_ltv = ezusb_read_ltv,
1549 .write_ltv = ezusb_write_ltv,
1550 .bap_pread = ezusb_bap_pread,
1551 .read_pda = ezusb_read_pda,
1552 .program_init = ezusb_program_init,
1553 .program_end = ezusb_program_end,
1554 .program = ezusb_program,
1555 .lock_irqsave = ezusb_lock_irqsave,
1556 .unlock_irqrestore = ezusb_unlock_irqrestore,
1557 .lock_irq = ezusb_lock_irq,
1558 .unlock_irq = ezusb_unlock_irq,
1564 .ndo_start_xmit = ezusb_xmit,
1576 struct usb_device *
udev = interface_to_usbdev(interface);
1582 const struct firmware *fw_entry;
1587 ezusb_hard_reset,
NULL);
1589 err(
"Couldn't allocate orinocodev");
1609 hw->
ops = &ezusb_ops;
1614 iface_desc = &interface->altsetting[0].desc;
1616 ep = &interface->altsetting[0].endpoint[
i].desc;
1624 pr_warning(
"Found a second bulk in ep, ignored");
1630 err(
"No free urbs available");
1638 upriv->
read_pipe = usb_rcvbulkpipe(udev,
1643 if (!upriv->
read_urb->transfer_buffer) {
1644 err(
"Couldn't allocate IN buffer");
1655 pr_warning(
"Found a second bulk out ep, ignored");
1660 pr_warning(
"bulk out: wMaxPacketSize != 64");
1669 err(
"Couldn't allocate bulk_out_buffer");
1675 err(
"Didn't find the required bulk endpoints");
1680 &interface->dev) == 0) {
1684 if (firmware.
size && firmware.
code) {
1685 ezusb_firmware_download(upriv, &firmware);
1687 err(
"No firmware to download");
1691 if (ezusb_hard_reset(priv) < 0) {
1692 err(
"Cannot reset the device");
1700 if (ezusb_init(hw) < 0) {
1701 err(
"Couldn't initialize the device");
1702 err(
"Firmware may not be downloaded or may be wrong.");
1708 err(
"orinoco_init() failed\n");
1714 err(
"%s: orinoco_if_add() failed", __func__);
1722 ezusb_delete(upriv);
1735 usb_set_intfdata(interface, upriv);
1742 struct ezusb_priv *upriv = usb_get_intfdata(intf);
1743 usb_set_intfdata(intf,
NULL);
1744 ezusb_delete(upriv);
1750 static struct usb_driver orinoco_driver = {
1752 .probe = ezusb_probe,
1753 .disconnect = ezusb_disconnect,
1754 .id_table = ezusb_table,
1755 .disable_hub_initiated_lpm = 1,