Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / wireless / intersil / p54 / p54usb.c
blob39cfabf968d499b94f55ecbaccfde63a84a42108
2 /*
3 * Linux device driver for USB based Prism54
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
7 * Based on the islsm (softmac prism54) driver, which is:
8 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/usb.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/firmware.h>
19 #include <linux/etherdevice.h>
20 #include <linux/delay.h>
21 #include <linux/crc32.h>
22 #include <linux/module.h>
23 #include <net/mac80211.h>
25 #include "p54.h"
26 #include "lmac.h"
27 #include "p54usb.h"
29 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
30 MODULE_DESCRIPTION("Prism54 USB wireless driver");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("prism54usb");
33 MODULE_FIRMWARE("isl3886usb");
34 MODULE_FIRMWARE("isl3887usb");
36 static struct usb_driver p54u_driver;
39 * Note:
41 * Always update our wiki's device list (located at:
42 * http://wireless.kernel.org/en/users/Drivers/p54/devices ),
43 * whenever you add a new device.
46 static const struct usb_device_id p54u_table[] = {
47 /* Version 1 devices (pci chip + net2280) */
48 {USB_DEVICE(0x0411, 0x0050)}, /* Buffalo WLI2-USB2-G54 */
49 {USB_DEVICE(0x045e, 0x00c2)}, /* Microsoft MN-710 */
50 {USB_DEVICE(0x0506, 0x0a11)}, /* 3COM 3CRWE254G72 */
51 {USB_DEVICE(0x0675, 0x0530)}, /* DrayTek Vigor 530 */
52 {USB_DEVICE(0x06b9, 0x0120)}, /* Thomson SpeedTouch 120g */
53 {USB_DEVICE(0x0707, 0xee06)}, /* SMC 2862W-G */
54 {USB_DEVICE(0x07aa, 0x001c)}, /* Corega CG-WLUSB2GT */
55 {USB_DEVICE(0x083a, 0x4501)}, /* Accton 802.11g WN4501 USB */
56 {USB_DEVICE(0x083a, 0x4502)}, /* Siemens Gigaset USB Adapter */
57 {USB_DEVICE(0x083a, 0x5501)}, /* Phillips CPWUA054 */
58 {USB_DEVICE(0x0846, 0x4200)}, /* Netgear WG121 */
59 {USB_DEVICE(0x0846, 0x4210)}, /* Netgear WG121 the second ? */
60 {USB_DEVICE(0x0846, 0x4220)}, /* Netgear WG111 */
61 {USB_DEVICE(0x09aa, 0x1000)}, /* Spinnaker Proto board */
62 {USB_DEVICE(0x0bf8, 0x1007)}, /* Fujitsu E-5400 USB */
63 {USB_DEVICE(0x0cde, 0x0006)}, /* Medion 40900, Roper Europe */
64 {USB_DEVICE(0x0db0, 0x6826)}, /* MSI UB54G (MS-6826) */
65 {USB_DEVICE(0x107b, 0x55f2)}, /* Gateway WGU-210 (Gemtek) */
66 {USB_DEVICE(0x124a, 0x4023)}, /* Shuttle PN15, Airvast WM168g, IOGear GWU513 */
67 {USB_DEVICE(0x124a, 0x4026)}, /* AirVasT USB wireless device */
68 {USB_DEVICE(0x1435, 0x0210)}, /* Inventel UR054G */
69 {USB_DEVICE(0x15a9, 0x0002)}, /* Gemtek WUBI-100GW 802.11g */
70 {USB_DEVICE(0x1630, 0x0005)}, /* 2Wire 802.11g USB (v1) / Z-Com */
71 {USB_DEVICE(0x182d, 0x096b)}, /* Sitecom WL-107 */
72 {USB_DEVICE(0x1915, 0x2234)}, /* Linksys WUSB54G OEM */
73 {USB_DEVICE(0x1915, 0x2235)}, /* Linksys WUSB54G Portable OEM */
74 {USB_DEVICE(0x2001, 0x3701)}, /* DLink DWL-G120 Spinnaker */
75 {USB_DEVICE(0x2001, 0x3703)}, /* DLink DWL-G122 */
76 {USB_DEVICE(0x2001, 0x3762)}, /* Conceptronic C54U */
77 {USB_DEVICE(0x5041, 0x2234)}, /* Linksys WUSB54G */
78 {USB_DEVICE(0x5041, 0x2235)}, /* Linksys WUSB54G Portable */
80 /* Version 2 devices (3887) */
81 {USB_DEVICE(0x0471, 0x1230)}, /* Philips CPWUA054/00 */
82 {USB_DEVICE(0x050d, 0x7050)}, /* Belkin F5D7050 ver 1000 */
83 {USB_DEVICE(0x0572, 0x2000)}, /* Cohiba Proto board */
84 {USB_DEVICE(0x0572, 0x2002)}, /* Cohiba Proto board */
85 {USB_DEVICE(0x06a9, 0x000e)}, /* Westell 802.11g USB (A90-211WG-01) */
86 {USB_DEVICE(0x06b9, 0x0121)}, /* Thomson SpeedTouch 121g */
87 {USB_DEVICE(0x0707, 0xee13)}, /* SMC 2862W-G version 2 */
88 {USB_DEVICE(0x07aa, 0x0020)}, /* Corega WLUSB2GTST USB */
89 {USB_DEVICE(0x0803, 0x4310)}, /* Zoom 4410a */
90 {USB_DEVICE(0x083a, 0x4521)}, /* Siemens Gigaset USB Adapter 54 version 2 */
91 {USB_DEVICE(0x083a, 0x4531)}, /* T-Com Sinus 154 data II */
92 {USB_DEVICE(0x083a, 0xc501)}, /* Zoom Wireless-G 4410 */
93 {USB_DEVICE(0x083a, 0xf503)}, /* Accton FD7050E ver 1010ec */
94 {USB_DEVICE(0x0846, 0x4240)}, /* Netgear WG111 (v2) */
95 {USB_DEVICE(0x0915, 0x2000)}, /* Cohiba Proto board */
96 {USB_DEVICE(0x0915, 0x2002)}, /* Cohiba Proto board */
97 {USB_DEVICE(0x0baf, 0x0118)}, /* U.S. Robotics U5 802.11g Adapter*/
98 {USB_DEVICE(0x0bf8, 0x1009)}, /* FUJITSU E-5400 USB D1700*/
99 /* {USB_DEVICE(0x0cde, 0x0006)}, * Medion MD40900 already listed above,
100 * just noting it here for clarity */
101 {USB_DEVICE(0x0cde, 0x0008)}, /* Sagem XG703A */
102 {USB_DEVICE(0x0cde, 0x0015)}, /* Zcomax XG-705A */
103 {USB_DEVICE(0x0d8e, 0x3762)}, /* DLink DWL-G120 Cohiba */
104 {USB_DEVICE(0x124a, 0x4025)}, /* IOGear GWU513 (GW3887IK chip) */
105 {USB_DEVICE(0x1260, 0xee22)}, /* SMC 2862W-G version 2 */
106 {USB_DEVICE(0x13b1, 0x000a)}, /* Linksys WUSB54G ver 2 */
107 {USB_DEVICE(0x13B1, 0x000C)}, /* Linksys WUSB54AG */
108 {USB_DEVICE(0x1413, 0x5400)}, /* Telsey 802.11g USB2.0 Adapter */
109 {USB_DEVICE(0x1435, 0x0427)}, /* Inventel UR054G */
110 /* {USB_DEVICE(0x15a9, 0x0002)}, * Also SparkLAN WL-682 with 3887 */
111 {USB_DEVICE(0x1668, 0x1050)}, /* Actiontec 802UIG-1 */
112 {USB_DEVICE(0x1740, 0x1000)}, /* Senao NUB-350 */
113 {USB_DEVICE(0x2001, 0x3704)}, /* DLink DWL-G122 rev A2 */
114 {USB_DEVICE(0x2001, 0x3705)}, /* D-Link DWL-G120 rev C1 */
115 {USB_DEVICE(0x413c, 0x5513)}, /* Dell WLA3310 USB Wireless Adapter */
116 {USB_DEVICE(0x413c, 0x8102)}, /* Spinnaker DUT */
117 {USB_DEVICE(0x413c, 0x8104)}, /* Cohiba Proto board */
121 MODULE_DEVICE_TABLE(usb, p54u_table);
123 static const struct {
124 u32 intf;
125 enum p54u_hw_type type;
126 const char *fw;
127 char hw[20];
128 } p54u_fwlist[__NUM_P54U_HWTYPES] = {
130 .type = P54U_NET2280,
131 .intf = FW_LM86,
132 .fw = "isl3886usb",
133 .hw = "ISL3886 + net2280",
136 .type = P54U_3887,
137 .intf = FW_LM87,
138 .fw = "isl3887usb",
139 .hw = "ISL3887",
143 static void p54u_rx_cb(struct urb *urb)
145 struct sk_buff *skb = (struct sk_buff *) urb->context;
146 struct p54u_rx_info *info = (struct p54u_rx_info *)skb->cb;
147 struct ieee80211_hw *dev = info->dev;
148 struct p54u_priv *priv = dev->priv;
150 skb_unlink(skb, &priv->rx_queue);
152 if (unlikely(urb->status)) {
153 dev_kfree_skb_irq(skb);
154 return;
157 skb_put(skb, urb->actual_length);
159 if (priv->hw_type == P54U_NET2280)
160 skb_pull(skb, priv->common.tx_hdr_len);
161 if (priv->common.fw_interface == FW_LM87) {
162 skb_pull(skb, 4);
163 skb_put(skb, 4);
166 if (p54_rx(dev, skb)) {
167 skb = dev_alloc_skb(priv->common.rx_mtu + 32);
168 if (unlikely(!skb)) {
169 /* TODO check rx queue length and refill *somewhere* */
170 return;
173 info = (struct p54u_rx_info *) skb->cb;
174 info->urb = urb;
175 info->dev = dev;
176 urb->transfer_buffer = skb_tail_pointer(skb);
177 urb->context = skb;
178 } else {
179 if (priv->hw_type == P54U_NET2280)
180 skb_push(skb, priv->common.tx_hdr_len);
181 if (priv->common.fw_interface == FW_LM87) {
182 skb_push(skb, 4);
183 skb_put(skb, 4);
185 skb_reset_tail_pointer(skb);
186 skb_trim(skb, 0);
187 urb->transfer_buffer = skb_tail_pointer(skb);
189 skb_queue_tail(&priv->rx_queue, skb);
190 usb_anchor_urb(urb, &priv->submitted);
191 if (usb_submit_urb(urb, GFP_ATOMIC)) {
192 skb_unlink(skb, &priv->rx_queue);
193 usb_unanchor_urb(urb);
194 dev_kfree_skb_irq(skb);
198 static void p54u_tx_cb(struct urb *urb)
200 struct sk_buff *skb = urb->context;
201 struct ieee80211_hw *dev =
202 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
204 p54_free_skb(dev, skb);
207 static void p54u_tx_dummy_cb(struct urb *urb) { }
209 static void p54u_free_urbs(struct ieee80211_hw *dev)
211 struct p54u_priv *priv = dev->priv;
212 usb_kill_anchored_urbs(&priv->submitted);
215 static void p54u_stop(struct ieee80211_hw *dev)
218 * TODO: figure out how to reliably stop the 3887 and net2280 so
219 * the hardware is still usable next time we want to start it.
220 * until then, we just stop listening to the hardware..
222 p54u_free_urbs(dev);
225 static int p54u_init_urbs(struct ieee80211_hw *dev)
227 struct p54u_priv *priv = dev->priv;
228 struct urb *entry = NULL;
229 struct sk_buff *skb;
230 struct p54u_rx_info *info;
231 int ret = 0;
233 while (skb_queue_len(&priv->rx_queue) < 32) {
234 skb = __dev_alloc_skb(priv->common.rx_mtu + 32, GFP_KERNEL);
235 if (!skb) {
236 ret = -ENOMEM;
237 goto err;
239 entry = usb_alloc_urb(0, GFP_KERNEL);
240 if (!entry) {
241 ret = -ENOMEM;
242 goto err;
245 usb_fill_bulk_urb(entry, priv->udev,
246 usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA),
247 skb_tail_pointer(skb),
248 priv->common.rx_mtu + 32, p54u_rx_cb, skb);
249 info = (struct p54u_rx_info *) skb->cb;
250 info->urb = entry;
251 info->dev = dev;
252 skb_queue_tail(&priv->rx_queue, skb);
254 usb_anchor_urb(entry, &priv->submitted);
255 ret = usb_submit_urb(entry, GFP_KERNEL);
256 if (ret) {
257 skb_unlink(skb, &priv->rx_queue);
258 usb_unanchor_urb(entry);
259 goto err;
261 usb_free_urb(entry);
262 entry = NULL;
265 return 0;
267 err:
268 usb_free_urb(entry);
269 kfree_skb(skb);
270 p54u_free_urbs(dev);
271 return ret;
274 static int p54u_open(struct ieee80211_hw *dev)
277 * TODO: Because we don't know how to reliably stop the 3887 and
278 * the isl3886+net2280, other than brutally cut off all
279 * communications. We have to reinitialize the urbs on every start.
281 return p54u_init_urbs(dev);
284 static __le32 p54u_lm87_chksum(const __le32 *data, size_t length)
286 u32 chk = 0;
288 length >>= 2;
289 while (length--) {
290 chk ^= le32_to_cpu(*data++);
291 chk = (chk >> 5) ^ (chk << 3);
294 return cpu_to_le32(chk);
297 static void p54u_tx_lm87(struct ieee80211_hw *dev, struct sk_buff *skb)
299 struct p54u_priv *priv = dev->priv;
300 struct urb *data_urb;
301 struct lm87_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
303 data_urb = usb_alloc_urb(0, GFP_ATOMIC);
304 if (!data_urb) {
305 p54_free_skb(dev, skb);
306 return;
309 hdr->chksum = p54u_lm87_chksum((__le32 *)skb->data, skb->len);
310 hdr->device_addr = ((struct p54_hdr *)skb->data)->req_id;
312 usb_fill_bulk_urb(data_urb, priv->udev,
313 usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
314 hdr, skb->len + sizeof(*hdr), FREE_AFTER_TX(skb) ?
315 p54u_tx_cb : p54u_tx_dummy_cb, skb);
316 data_urb->transfer_flags |= URB_ZERO_PACKET;
318 usb_anchor_urb(data_urb, &priv->submitted);
319 if (usb_submit_urb(data_urb, GFP_ATOMIC)) {
320 usb_unanchor_urb(data_urb);
321 p54_free_skb(dev, skb);
323 usb_free_urb(data_urb);
326 static void p54u_tx_net2280(struct ieee80211_hw *dev, struct sk_buff *skb)
328 struct p54u_priv *priv = dev->priv;
329 struct urb *int_urb = NULL, *data_urb = NULL;
330 struct net2280_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
331 struct net2280_reg_write *reg = NULL;
332 int err = -ENOMEM;
334 reg = kmalloc(sizeof(*reg), GFP_ATOMIC);
335 if (!reg)
336 goto out;
338 int_urb = usb_alloc_urb(0, GFP_ATOMIC);
339 if (!int_urb)
340 goto out;
342 data_urb = usb_alloc_urb(0, GFP_ATOMIC);
343 if (!data_urb)
344 goto out;
346 reg->port = cpu_to_le16(NET2280_DEV_U32);
347 reg->addr = cpu_to_le32(P54U_DEV_BASE);
348 reg->val = cpu_to_le32(ISL38XX_DEV_INT_DATA);
350 memset(hdr, 0, sizeof(*hdr));
351 hdr->len = cpu_to_le16(skb->len);
352 hdr->device_addr = ((struct p54_hdr *) skb->data)->req_id;
354 usb_fill_bulk_urb(int_urb, priv->udev,
355 usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV), reg, sizeof(*reg),
356 p54u_tx_dummy_cb, dev);
359 * URB_FREE_BUFFER triggers a code path in the USB subsystem that will
360 * free what is inside the transfer_buffer after the last reference to
361 * the int_urb is dropped.
363 int_urb->transfer_flags |= URB_FREE_BUFFER | URB_ZERO_PACKET;
364 reg = NULL;
366 usb_fill_bulk_urb(data_urb, priv->udev,
367 usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
368 hdr, skb->len + sizeof(*hdr), FREE_AFTER_TX(skb) ?
369 p54u_tx_cb : p54u_tx_dummy_cb, skb);
370 data_urb->transfer_flags |= URB_ZERO_PACKET;
372 usb_anchor_urb(int_urb, &priv->submitted);
373 err = usb_submit_urb(int_urb, GFP_ATOMIC);
374 if (err) {
375 usb_unanchor_urb(int_urb);
376 goto out;
379 usb_anchor_urb(data_urb, &priv->submitted);
380 err = usb_submit_urb(data_urb, GFP_ATOMIC);
381 if (err) {
382 usb_unanchor_urb(data_urb);
383 goto out;
385 out:
386 usb_free_urb(int_urb);
387 usb_free_urb(data_urb);
389 if (err) {
390 kfree(reg);
391 p54_free_skb(dev, skb);
395 static int p54u_write(struct p54u_priv *priv,
396 struct net2280_reg_write *buf,
397 enum net2280_op_type type,
398 __le32 addr, __le32 val)
400 unsigned int ep;
401 int alen;
403 if (type & 0x0800)
404 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV);
405 else
406 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_BRG);
408 buf->port = cpu_to_le16(type);
409 buf->addr = addr;
410 buf->val = val;
412 return usb_bulk_msg(priv->udev, ep, buf, sizeof(*buf), &alen, 1000);
415 static int p54u_read(struct p54u_priv *priv, void *buf,
416 enum net2280_op_type type,
417 __le32 addr, __le32 *val)
419 struct net2280_reg_read *read = buf;
420 __le32 *reg = buf;
421 unsigned int ep;
422 int alen, err;
424 if (type & 0x0800)
425 ep = P54U_PIPE_DEV;
426 else
427 ep = P54U_PIPE_BRG;
429 read->port = cpu_to_le16(type);
430 read->addr = addr;
432 err = usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
433 read, sizeof(*read), &alen, 1000);
434 if (err)
435 return err;
437 err = usb_bulk_msg(priv->udev, usb_rcvbulkpipe(priv->udev, ep),
438 reg, sizeof(*reg), &alen, 1000);
439 if (err)
440 return err;
442 *val = *reg;
443 return 0;
446 static int p54u_bulk_msg(struct p54u_priv *priv, unsigned int ep,
447 void *data, size_t len)
449 int alen;
450 return usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
451 data, len, &alen, 2000);
454 static int p54u_device_reset(struct ieee80211_hw *dev)
456 struct p54u_priv *priv = dev->priv;
457 int ret, lock = (priv->intf->condition != USB_INTERFACE_BINDING);
459 if (lock) {
460 ret = usb_lock_device_for_reset(priv->udev, priv->intf);
461 if (ret < 0) {
462 dev_err(&priv->udev->dev, "(p54usb) unable to lock "
463 "device for reset (%d)!\n", ret);
464 return ret;
468 ret = usb_reset_device(priv->udev);
469 if (lock)
470 usb_unlock_device(priv->udev);
472 if (ret)
473 dev_err(&priv->udev->dev, "(p54usb) unable to reset "
474 "device (%d)!\n", ret);
476 return ret;
479 static const char p54u_romboot_3887[] = "~~~~";
480 static int p54u_firmware_reset_3887(struct ieee80211_hw *dev)
482 struct p54u_priv *priv = dev->priv;
483 u8 *buf;
484 int ret;
486 buf = kmemdup(p54u_romboot_3887, 4, GFP_KERNEL);
487 if (!buf)
488 return -ENOMEM;
489 ret = p54u_bulk_msg(priv, P54U_PIPE_DATA,
490 buf, 4);
491 kfree(buf);
492 if (ret)
493 dev_err(&priv->udev->dev, "(p54usb) unable to jump to "
494 "boot ROM (%d)!\n", ret);
496 return ret;
499 static const char p54u_firmware_upload_3887[] = "<\r";
500 static int p54u_upload_firmware_3887(struct ieee80211_hw *dev)
502 struct p54u_priv *priv = dev->priv;
503 int err, alen;
504 u8 carry = 0;
505 u8 *buf, *tmp;
506 const u8 *data;
507 unsigned int left, remains, block_size;
508 struct x2_header *hdr;
509 unsigned long timeout;
511 err = p54u_firmware_reset_3887(dev);
512 if (err)
513 return err;
515 tmp = buf = kmalloc(P54U_FW_BLOCK, GFP_KERNEL);
516 if (!buf)
517 return -ENOMEM;
519 left = block_size = min_t(size_t, P54U_FW_BLOCK, priv->fw->size);
520 strcpy(buf, p54u_firmware_upload_3887);
521 left -= strlen(p54u_firmware_upload_3887);
522 tmp += strlen(p54u_firmware_upload_3887);
524 data = priv->fw->data;
525 remains = priv->fw->size;
527 hdr = (struct x2_header *)(buf + strlen(p54u_firmware_upload_3887));
528 memcpy(hdr->signature, X2_SIGNATURE, X2_SIGNATURE_SIZE);
529 hdr->fw_load_addr = cpu_to_le32(ISL38XX_DEV_FIRMWARE_ADDR);
530 hdr->fw_length = cpu_to_le32(priv->fw->size);
531 hdr->crc = cpu_to_le32(~crc32_le(~0, (void *)&hdr->fw_load_addr,
532 sizeof(u32)*2));
533 left -= sizeof(*hdr);
534 tmp += sizeof(*hdr);
536 while (remains) {
537 while (left--) {
538 if (carry) {
539 *tmp++ = carry;
540 carry = 0;
541 remains--;
542 continue;
544 switch (*data) {
545 case '~':
546 *tmp++ = '}';
547 carry = '^';
548 break;
549 case '}':
550 *tmp++ = '}';
551 carry = ']';
552 break;
553 default:
554 *tmp++ = *data;
555 remains--;
556 break;
558 data++;
561 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, block_size);
562 if (err) {
563 dev_err(&priv->udev->dev, "(p54usb) firmware "
564 "upload failed!\n");
565 goto err_upload_failed;
568 tmp = buf;
569 left = block_size = min((unsigned int)P54U_FW_BLOCK, remains);
572 *((__le32 *)buf) = cpu_to_le32(~crc32_le(~0, priv->fw->data,
573 priv->fw->size));
574 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, sizeof(u32));
575 if (err) {
576 dev_err(&priv->udev->dev, "(p54usb) firmware upload failed!\n");
577 goto err_upload_failed;
579 timeout = jiffies + msecs_to_jiffies(1000);
580 while (!(err = usb_bulk_msg(priv->udev,
581 usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA), buf, 128, &alen, 1000))) {
582 if (alen > 2 && !memcmp(buf, "OK", 2))
583 break;
585 if (alen > 5 && !memcmp(buf, "ERROR", 5)) {
586 err = -EINVAL;
587 break;
590 if (time_after(jiffies, timeout)) {
591 dev_err(&priv->udev->dev, "(p54usb) firmware boot "
592 "timed out!\n");
593 err = -ETIMEDOUT;
594 break;
597 if (err) {
598 dev_err(&priv->udev->dev, "(p54usb) firmware upload failed!\n");
599 goto err_upload_failed;
602 buf[0] = 'g';
603 buf[1] = '\r';
604 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, 2);
605 if (err) {
606 dev_err(&priv->udev->dev, "(p54usb) firmware boot failed!\n");
607 goto err_upload_failed;
610 timeout = jiffies + msecs_to_jiffies(1000);
611 while (!(err = usb_bulk_msg(priv->udev,
612 usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA), buf, 128, &alen, 1000))) {
613 if (alen > 0 && buf[0] == 'g')
614 break;
616 if (time_after(jiffies, timeout)) {
617 err = -ETIMEDOUT;
618 break;
621 if (err)
622 goto err_upload_failed;
624 err_upload_failed:
625 kfree(buf);
626 return err;
629 static int p54u_upload_firmware_net2280(struct ieee80211_hw *dev)
631 struct p54u_priv *priv = dev->priv;
632 const struct p54p_csr *devreg = (const struct p54p_csr *) P54U_DEV_BASE;
633 int err, alen;
634 void *buf;
635 __le32 reg;
636 unsigned int remains, offset;
637 const u8 *data;
639 buf = kmalloc(512, GFP_KERNEL);
640 if (!buf)
641 return -ENOMEM;
643 #define P54U_WRITE(type, addr, data) \
644 do {\
645 err = p54u_write(priv, buf, type,\
646 cpu_to_le32((u32)(unsigned long)addr), data);\
647 if (err) \
648 goto fail;\
649 } while (0)
651 #define P54U_READ(type, addr) \
652 do {\
653 err = p54u_read(priv, buf, type,\
654 cpu_to_le32((u32)(unsigned long)addr), &reg);\
655 if (err)\
656 goto fail;\
657 } while (0)
659 /* power down net2280 bridge */
660 P54U_READ(NET2280_BRG_U32, NET2280_GPIOCTL);
661 reg |= cpu_to_le32(P54U_BRG_POWER_DOWN);
662 reg &= cpu_to_le32(~P54U_BRG_POWER_UP);
663 P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
665 mdelay(100);
667 /* power up bridge */
668 reg |= cpu_to_le32(P54U_BRG_POWER_UP);
669 reg &= cpu_to_le32(~P54U_BRG_POWER_DOWN);
670 P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
672 mdelay(100);
674 P54U_WRITE(NET2280_BRG_U32, NET2280_DEVINIT,
675 cpu_to_le32(NET2280_CLK_30Mhz |
676 NET2280_PCI_ENABLE |
677 NET2280_PCI_SOFT_RESET));
679 mdelay(20);
681 P54U_WRITE(NET2280_BRG_CFG_U16, PCI_COMMAND,
682 cpu_to_le32(PCI_COMMAND_MEMORY |
683 PCI_COMMAND_MASTER));
685 P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_0,
686 cpu_to_le32(NET2280_BASE));
688 P54U_READ(NET2280_BRG_CFG_U16, PCI_STATUS);
689 reg |= cpu_to_le32(PCI_STATUS_REC_MASTER_ABORT);
690 P54U_WRITE(NET2280_BRG_CFG_U16, PCI_STATUS, reg);
692 // TODO: we really need this?
693 P54U_READ(NET2280_BRG_U32, NET2280_RELNUM);
695 P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_RSP,
696 cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
697 P54U_WRITE(NET2280_BRG_U32, NET2280_EPC_RSP,
698 cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
700 P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_2,
701 cpu_to_le32(NET2280_BASE2));
703 /* finally done setting up the bridge */
705 P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | PCI_COMMAND,
706 cpu_to_le32(PCI_COMMAND_MEMORY |
707 PCI_COMMAND_MASTER));
709 P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | 0x40 /* TRDY timeout */, 0);
710 P54U_WRITE(NET2280_DEV_CFG_U32, 0x10000 | PCI_BASE_ADDRESS_0,
711 cpu_to_le32(P54U_DEV_BASE));
713 P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
714 P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
715 cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
717 /* do romboot */
718 P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable, 0);
720 P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
721 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
722 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
723 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
724 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
726 mdelay(20);
728 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
729 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
731 mdelay(20);
733 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
734 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
736 mdelay(100);
738 P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
739 P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
741 /* finally, we can upload firmware now! */
742 remains = priv->fw->size;
743 data = priv->fw->data;
744 offset = ISL38XX_DEV_FIRMWARE_ADDR;
746 while (remains) {
747 unsigned int block_len = min(remains, (unsigned int)512);
748 memcpy(buf, data, block_len);
750 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, block_len);
751 if (err) {
752 dev_err(&priv->udev->dev, "(p54usb) firmware block "
753 "upload failed\n");
754 goto fail;
757 P54U_WRITE(NET2280_DEV_U32, &devreg->direct_mem_base,
758 cpu_to_le32(0xc0000f00));
760 P54U_WRITE(NET2280_DEV_U32,
761 0x0020 | (unsigned long)&devreg->direct_mem_win, 0);
762 P54U_WRITE(NET2280_DEV_U32,
763 0x0020 | (unsigned long)&devreg->direct_mem_win,
764 cpu_to_le32(1));
766 P54U_WRITE(NET2280_DEV_U32,
767 0x0024 | (unsigned long)&devreg->direct_mem_win,
768 cpu_to_le32(block_len));
769 P54U_WRITE(NET2280_DEV_U32,
770 0x0028 | (unsigned long)&devreg->direct_mem_win,
771 cpu_to_le32(offset));
773 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_addr,
774 cpu_to_le32(NET2280_EPA_FIFO_PCI_ADDR));
775 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_len,
776 cpu_to_le32(block_len >> 2));
777 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_ctrl,
778 cpu_to_le32(ISL38XX_DMA_MASTER_CONTROL_TRIGGER));
780 mdelay(10);
782 P54U_READ(NET2280_DEV_U32,
783 0x002C | (unsigned long)&devreg->direct_mem_win);
784 if (!(reg & cpu_to_le32(ISL38XX_DMA_STATUS_DONE)) ||
785 !(reg & cpu_to_le32(ISL38XX_DMA_STATUS_READY))) {
786 dev_err(&priv->udev->dev, "(p54usb) firmware DMA "
787 "transfer failed\n");
788 goto fail;
791 P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_STAT,
792 cpu_to_le32(NET2280_FIFO_FLUSH));
794 remains -= block_len;
795 data += block_len;
796 offset += block_len;
799 /* do ramboot */
800 P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
801 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
802 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
803 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
804 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
806 mdelay(20);
808 reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
809 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
811 reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
812 P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
814 mdelay(100);
816 P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
817 P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
819 /* start up the firmware */
820 P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable,
821 cpu_to_le32(ISL38XX_INT_IDENT_INIT));
823 P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
824 cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
826 P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1,
827 cpu_to_le32(NET2280_PCI_INTA_INTERRUPT_ENABLE |
828 NET2280_USB_INTERRUPT_ENABLE));
830 P54U_WRITE(NET2280_DEV_U32, &devreg->dev_int,
831 cpu_to_le32(ISL38XX_DEV_INT_RESET));
833 err = usb_interrupt_msg(priv->udev,
834 usb_rcvbulkpipe(priv->udev, P54U_PIPE_INT),
835 buf, sizeof(__le32), &alen, 1000);
836 if (err || alen != sizeof(__le32))
837 goto fail;
839 P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
840 P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
842 if (!(reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT)))
843 err = -EINVAL;
845 P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
846 P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
847 cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
849 #undef P54U_WRITE
850 #undef P54U_READ
852 fail:
853 kfree(buf);
854 return err;
857 static int p54_find_type(struct p54u_priv *priv)
859 int i;
861 for (i = 0; i < __NUM_P54U_HWTYPES; i++)
862 if (p54u_fwlist[i].type == priv->hw_type)
863 break;
864 if (i == __NUM_P54U_HWTYPES)
865 return -EOPNOTSUPP;
867 return i;
870 static int p54u_start_ops(struct p54u_priv *priv)
872 struct ieee80211_hw *dev = priv->common.hw;
873 int ret;
875 ret = p54_parse_firmware(dev, priv->fw);
876 if (ret)
877 goto err_out;
879 ret = p54_find_type(priv);
880 if (ret < 0)
881 goto err_out;
883 if (priv->common.fw_interface != p54u_fwlist[ret].intf) {
884 dev_err(&priv->udev->dev, "wrong firmware, please get "
885 "a firmware for \"%s\" and try again.\n",
886 p54u_fwlist[ret].hw);
887 ret = -ENODEV;
888 goto err_out;
891 ret = priv->upload_fw(dev);
892 if (ret)
893 goto err_out;
895 ret = p54u_open(dev);
896 if (ret)
897 goto err_out;
899 ret = p54_read_eeprom(dev);
900 if (ret)
901 goto err_stop;
903 p54u_stop(dev);
905 ret = p54_register_common(dev, &priv->udev->dev);
906 if (ret)
907 goto err_stop;
909 return 0;
911 err_stop:
912 p54u_stop(dev);
914 err_out:
916 * p54u_disconnect will do the rest of the
917 * cleanup
919 return ret;
922 static void p54u_load_firmware_cb(const struct firmware *firmware,
923 void *context)
925 struct p54u_priv *priv = context;
926 struct usb_device *udev = priv->udev;
927 struct usb_interface *intf = priv->intf;
928 int err;
930 if (firmware) {
931 priv->fw = firmware;
932 err = p54u_start_ops(priv);
933 } else {
934 err = -ENOENT;
935 dev_err(&udev->dev, "Firmware not found.\n");
938 complete(&priv->fw_wait_load);
940 * At this point p54u_disconnect may have already freed
941 * the "priv" context. Do not use it anymore!
943 priv = NULL;
945 if (err) {
946 dev_err(&intf->dev, "failed to initialize device (%d)\n", err);
948 usb_lock_device(udev);
949 usb_driver_release_interface(&p54u_driver, intf);
950 usb_unlock_device(udev);
953 usb_put_intf(intf);
956 static int p54u_load_firmware(struct ieee80211_hw *dev,
957 struct usb_interface *intf)
959 struct usb_device *udev = interface_to_usbdev(intf);
960 struct p54u_priv *priv = dev->priv;
961 struct device *device = &udev->dev;
962 int err, i;
964 BUILD_BUG_ON(ARRAY_SIZE(p54u_fwlist) != __NUM_P54U_HWTYPES);
966 init_completion(&priv->fw_wait_load);
967 i = p54_find_type(priv);
968 if (i < 0)
969 return i;
971 dev_info(&priv->udev->dev, "Loading firmware file %s\n",
972 p54u_fwlist[i].fw);
974 usb_get_intf(intf);
975 err = request_firmware_nowait(THIS_MODULE, 1, p54u_fwlist[i].fw,
976 device, GFP_KERNEL, priv,
977 p54u_load_firmware_cb);
978 if (err) {
979 dev_err(&priv->udev->dev, "(p54usb) cannot load firmware %s "
980 "(%d)!\n", p54u_fwlist[i].fw, err);
981 usb_put_intf(intf);
984 return err;
987 static int p54u_probe(struct usb_interface *intf,
988 const struct usb_device_id *id)
990 struct usb_device *udev = interface_to_usbdev(intf);
991 struct ieee80211_hw *dev;
992 struct p54u_priv *priv;
993 int err;
994 unsigned int i, recognized_pipes;
996 dev = p54_init_common(sizeof(*priv));
998 if (!dev) {
999 dev_err(&udev->dev, "(p54usb) ieee80211 alloc failed\n");
1000 return -ENOMEM;
1003 priv = dev->priv;
1004 priv->hw_type = P54U_INVALID_HW;
1006 SET_IEEE80211_DEV(dev, &intf->dev);
1007 usb_set_intfdata(intf, dev);
1008 priv->udev = udev;
1009 priv->intf = intf;
1010 skb_queue_head_init(&priv->rx_queue);
1011 init_usb_anchor(&priv->submitted);
1013 /* really lazy and simple way of figuring out if we're a 3887 */
1014 /* TODO: should just stick the identification in the device table */
1015 i = intf->altsetting->desc.bNumEndpoints;
1016 recognized_pipes = 0;
1017 while (i--) {
1018 switch (intf->altsetting->endpoint[i].desc.bEndpointAddress) {
1019 case P54U_PIPE_DATA:
1020 case P54U_PIPE_MGMT:
1021 case P54U_PIPE_BRG:
1022 case P54U_PIPE_DEV:
1023 case P54U_PIPE_DATA | USB_DIR_IN:
1024 case P54U_PIPE_MGMT | USB_DIR_IN:
1025 case P54U_PIPE_BRG | USB_DIR_IN:
1026 case P54U_PIPE_DEV | USB_DIR_IN:
1027 case P54U_PIPE_INT | USB_DIR_IN:
1028 recognized_pipes++;
1031 priv->common.open = p54u_open;
1032 priv->common.stop = p54u_stop;
1033 if (recognized_pipes < P54U_PIPE_NUMBER) {
1034 #ifdef CONFIG_PM
1035 /* ISL3887 needs a full reset on resume */
1036 udev->reset_resume = 1;
1037 #endif /* CONFIG_PM */
1038 err = p54u_device_reset(dev);
1040 priv->hw_type = P54U_3887;
1041 dev->extra_tx_headroom += sizeof(struct lm87_tx_hdr);
1042 priv->common.tx_hdr_len = sizeof(struct lm87_tx_hdr);
1043 priv->common.tx = p54u_tx_lm87;
1044 priv->upload_fw = p54u_upload_firmware_3887;
1045 } else {
1046 priv->hw_type = P54U_NET2280;
1047 dev->extra_tx_headroom += sizeof(struct net2280_tx_hdr);
1048 priv->common.tx_hdr_len = sizeof(struct net2280_tx_hdr);
1049 priv->common.tx = p54u_tx_net2280;
1050 priv->upload_fw = p54u_upload_firmware_net2280;
1052 err = p54u_load_firmware(dev, intf);
1053 if (err)
1054 p54_free_common(dev);
1055 return err;
1058 static void p54u_disconnect(struct usb_interface *intf)
1060 struct ieee80211_hw *dev = usb_get_intfdata(intf);
1061 struct p54u_priv *priv;
1063 if (!dev)
1064 return;
1066 priv = dev->priv;
1067 wait_for_completion(&priv->fw_wait_load);
1068 p54_unregister_common(dev);
1070 release_firmware(priv->fw);
1071 p54_free_common(dev);
1074 static int p54u_pre_reset(struct usb_interface *intf)
1076 struct ieee80211_hw *dev = usb_get_intfdata(intf);
1078 if (!dev)
1079 return -ENODEV;
1081 p54u_stop(dev);
1082 return 0;
1085 static int p54u_resume(struct usb_interface *intf)
1087 struct ieee80211_hw *dev = usb_get_intfdata(intf);
1088 struct p54u_priv *priv;
1090 if (!dev)
1091 return -ENODEV;
1093 priv = dev->priv;
1094 if (unlikely(!(priv->upload_fw && priv->fw)))
1095 return 0;
1097 return priv->upload_fw(dev);
1100 static int p54u_post_reset(struct usb_interface *intf)
1102 struct ieee80211_hw *dev = usb_get_intfdata(intf);
1103 struct p54u_priv *priv;
1104 int err;
1106 err = p54u_resume(intf);
1107 if (err)
1108 return err;
1110 /* reinitialize old device state */
1111 priv = dev->priv;
1112 if (priv->common.mode != NL80211_IFTYPE_UNSPECIFIED)
1113 ieee80211_restart_hw(dev);
1115 return 0;
1118 #ifdef CONFIG_PM
1120 static int p54u_suspend(struct usb_interface *intf, pm_message_t message)
1122 return p54u_pre_reset(intf);
1125 #endif /* CONFIG_PM */
1127 static struct usb_driver p54u_driver = {
1128 .name = "p54usb",
1129 .id_table = p54u_table,
1130 .probe = p54u_probe,
1131 .disconnect = p54u_disconnect,
1132 .pre_reset = p54u_pre_reset,
1133 .post_reset = p54u_post_reset,
1134 #ifdef CONFIG_PM
1135 .suspend = p54u_suspend,
1136 .resume = p54u_resume,
1137 .reset_resume = p54u_resume,
1138 #endif /* CONFIG_PM */
1139 .soft_unbind = 1,
1140 .disable_hub_initiated_lpm = 1,
1143 module_usb_driver(p54u_driver);