]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/p54/p54usb.c
Merge remote-tracking branch 'regulator/topic/max8997' into regulator-next
[karo-tx-linux.git] / drivers / net / wireless / p54 / p54usb.c
1
2 /*
3  * Linux device driver for USB based Prism54
4  *
5  * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6  *
7  * Based on the islsm (softmac prism54) driver, which is:
8  * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
9  *
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.
13  */
14
15 #include <linux/init.h>
16 #include <linux/usb.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/firmware.h>
20 #include <linux/etherdevice.h>
21 #include <linux/delay.h>
22 #include <linux/crc32.h>
23 #include <linux/module.h>
24 #include <net/mac80211.h>
25
26 #include "p54.h"
27 #include "lmac.h"
28 #include "p54usb.h"
29
30 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
31 MODULE_DESCRIPTION("Prism54 USB wireless driver");
32 MODULE_LICENSE("GPL");
33 MODULE_ALIAS("prism54usb");
34 MODULE_FIRMWARE("isl3886usb");
35 MODULE_FIRMWARE("isl3887usb");
36
37 /*
38  * Note:
39  *
40  * Always update our wiki's device list (located at:
41  * http://wireless.kernel.org/en/users/Drivers/p54/devices ),
42  * whenever you add a new device.
43  */
44
45 static struct usb_device_id p54u_table[] = {
46         /* Version 1 devices (pci chip + net2280) */
47         {USB_DEVICE(0x0411, 0x0050)},   /* Buffalo WLI2-USB2-G54 */
48         {USB_DEVICE(0x045e, 0x00c2)},   /* Microsoft MN-710 */
49         {USB_DEVICE(0x0506, 0x0a11)},   /* 3COM 3CRWE254G72 */
50         {USB_DEVICE(0x0675, 0x0530)},   /* DrayTek Vigor 530 */
51         {USB_DEVICE(0x06b9, 0x0120)},   /* Thomson SpeedTouch 120g */
52         {USB_DEVICE(0x0707, 0xee06)},   /* SMC 2862W-G */
53         {USB_DEVICE(0x07aa, 0x001c)},   /* Corega CG-WLUSB2GT */
54         {USB_DEVICE(0x083a, 0x4501)},   /* Accton 802.11g WN4501 USB */
55         {USB_DEVICE(0x083a, 0x4502)},   /* Siemens Gigaset USB Adapter */
56         {USB_DEVICE(0x083a, 0x5501)},   /* Phillips CPWUA054 */
57         {USB_DEVICE(0x0846, 0x4200)},   /* Netgear WG121 */
58         {USB_DEVICE(0x0846, 0x4210)},   /* Netgear WG121 the second ? */
59         {USB_DEVICE(0x0846, 0x4220)},   /* Netgear WG111 */
60         {USB_DEVICE(0x09aa, 0x1000)},   /* Spinnaker Proto board */
61         {USB_DEVICE(0x0bf8, 0x1007)},   /* Fujitsu E-5400 USB */
62         {USB_DEVICE(0x0cde, 0x0006)},   /* Medion 40900, Roper Europe */
63         {USB_DEVICE(0x0db0, 0x6826)},   /* MSI UB54G (MS-6826) */
64         {USB_DEVICE(0x107b, 0x55f2)},   /* Gateway WGU-210 (Gemtek) */
65         {USB_DEVICE(0x124a, 0x4023)},   /* Shuttle PN15, Airvast WM168g, IOGear GWU513 */
66         {USB_DEVICE(0x1435, 0x0210)},   /* Inventel UR054G */
67         {USB_DEVICE(0x15a9, 0x0002)},   /* Gemtek WUBI-100GW 802.11g */
68         {USB_DEVICE(0x1630, 0x0005)},   /* 2Wire 802.11g USB (v1) / Z-Com */
69         {USB_DEVICE(0x182d, 0x096b)},   /* Sitecom WL-107 */
70         {USB_DEVICE(0x1915, 0x2234)},   /* Linksys WUSB54G OEM */
71         {USB_DEVICE(0x1915, 0x2235)},   /* Linksys WUSB54G Portable OEM */
72         {USB_DEVICE(0x2001, 0x3701)},   /* DLink DWL-G120 Spinnaker */
73         {USB_DEVICE(0x2001, 0x3703)},   /* DLink DWL-G122 */
74         {USB_DEVICE(0x2001, 0x3762)},   /* Conceptronic C54U */
75         {USB_DEVICE(0x5041, 0x2234)},   /* Linksys WUSB54G */
76         {USB_DEVICE(0x5041, 0x2235)},   /* Linksys WUSB54G Portable */
77
78         /* Version 2 devices (3887) */
79         {USB_DEVICE(0x0471, 0x1230)},   /* Philips CPWUA054/00 */
80         {USB_DEVICE(0x050d, 0x7050)},   /* Belkin F5D7050 ver 1000 */
81         {USB_DEVICE(0x0572, 0x2000)},   /* Cohiba Proto board */
82         {USB_DEVICE(0x0572, 0x2002)},   /* Cohiba Proto board */
83         {USB_DEVICE(0x06a9, 0x000e)},   /* Westell 802.11g USB (A90-211WG-01) */
84         {USB_DEVICE(0x06b9, 0x0121)},   /* Thomson SpeedTouch 121g */
85         {USB_DEVICE(0x0707, 0xee13)},   /* SMC 2862W-G version 2 */
86         {USB_DEVICE(0x0803, 0x4310)},   /* Zoom 4410a */
87         {USB_DEVICE(0x083a, 0x4503)},   /* T-Com Sinus 154 data II */
88         {USB_DEVICE(0x083a, 0x4521)},   /* Siemens Gigaset USB Adapter 54 version 2 */
89         {USB_DEVICE(0x083a, 0xc501)},   /* Zoom Wireless-G 4410 */
90         {USB_DEVICE(0x083a, 0xf503)},   /* Accton FD7050E ver 1010ec  */
91         {USB_DEVICE(0x0846, 0x4240)},   /* Netgear WG111 (v2) */
92         {USB_DEVICE(0x0915, 0x2000)},   /* Cohiba Proto board */
93         {USB_DEVICE(0x0915, 0x2002)},   /* Cohiba Proto board */
94         {USB_DEVICE(0x0baf, 0x0118)},   /* U.S. Robotics U5 802.11g Adapter*/
95         {USB_DEVICE(0x0bf8, 0x1009)},   /* FUJITSU E-5400 USB D1700*/
96         /* {USB_DEVICE(0x0cde, 0x0006)}, * Medion MD40900 already listed above,
97                                          * just noting it here for clarity */
98         {USB_DEVICE(0x0cde, 0x0008)},   /* Sagem XG703A */
99         {USB_DEVICE(0x0cde, 0x0015)},   /* Zcomax XG-705A */
100         {USB_DEVICE(0x0d8e, 0x3762)},   /* DLink DWL-G120 Cohiba */
101         {USB_DEVICE(0x124a, 0x4025)},   /* IOGear GWU513 (GW3887IK chip) */
102         {USB_DEVICE(0x1260, 0xee22)},   /* SMC 2862W-G version 2 */
103         {USB_DEVICE(0x13b1, 0x000a)},   /* Linksys WUSB54G ver 2 */
104         {USB_DEVICE(0x13B1, 0x000C)},   /* Linksys WUSB54AG */
105         {USB_DEVICE(0x1413, 0x5400)},   /* Telsey 802.11g USB2.0 Adapter */
106         {USB_DEVICE(0x1435, 0x0427)},   /* Inventel UR054G */
107         /* {USB_DEVICE(0x15a9, 0x0002)}, * Also SparkLAN WL-682 with 3887 */
108         {USB_DEVICE(0x1668, 0x1050)},   /* Actiontec 802UIG-1 */
109         {USB_DEVICE(0x1740, 0x1000)},   /* Senao NUB-350 */
110         {USB_DEVICE(0x2001, 0x3704)},   /* DLink DWL-G122 rev A2 */
111         {USB_DEVICE(0x2001, 0x3705)},   /* D-Link DWL-G120 rev C1 */
112         {USB_DEVICE(0x413c, 0x5513)},   /* Dell WLA3310 USB Wireless Adapter */
113         {USB_DEVICE(0x413c, 0x8102)},   /* Spinnaker DUT */
114         {USB_DEVICE(0x413c, 0x8104)},   /* Cohiba Proto board */
115         {}
116 };
117
118 MODULE_DEVICE_TABLE(usb, p54u_table);
119
120 static const struct {
121         u32 intf;
122         enum p54u_hw_type type;
123         const char *fw;
124         char hw[20];
125 } p54u_fwlist[__NUM_P54U_HWTYPES] = {
126         {
127                 .type = P54U_NET2280,
128                 .intf = FW_LM86,
129                 .fw = "isl3886usb",
130                 .hw = "ISL3886 + net2280",
131         },
132         {
133                 .type = P54U_3887,
134                 .intf = FW_LM87,
135                 .fw = "isl3887usb",
136                 .hw = "ISL3887",
137         },
138 };
139
140 static void p54u_rx_cb(struct urb *urb)
141 {
142         struct sk_buff *skb = (struct sk_buff *) urb->context;
143         struct p54u_rx_info *info = (struct p54u_rx_info *)skb->cb;
144         struct ieee80211_hw *dev = info->dev;
145         struct p54u_priv *priv = dev->priv;
146
147         skb_unlink(skb, &priv->rx_queue);
148
149         if (unlikely(urb->status)) {
150                 dev_kfree_skb_irq(skb);
151                 return;
152         }
153
154         skb_put(skb, urb->actual_length);
155
156         if (priv->hw_type == P54U_NET2280)
157                 skb_pull(skb, priv->common.tx_hdr_len);
158         if (priv->common.fw_interface == FW_LM87) {
159                 skb_pull(skb, 4);
160                 skb_put(skb, 4);
161         }
162
163         if (p54_rx(dev, skb)) {
164                 skb = dev_alloc_skb(priv->common.rx_mtu + 32);
165                 if (unlikely(!skb)) {
166                         /* TODO check rx queue length and refill *somewhere* */
167                         return;
168                 }
169
170                 info = (struct p54u_rx_info *) skb->cb;
171                 info->urb = urb;
172                 info->dev = dev;
173                 urb->transfer_buffer = skb_tail_pointer(skb);
174                 urb->context = skb;
175         } else {
176                 if (priv->hw_type == P54U_NET2280)
177                         skb_push(skb, priv->common.tx_hdr_len);
178                 if (priv->common.fw_interface == FW_LM87) {
179                         skb_push(skb, 4);
180                         skb_put(skb, 4);
181                 }
182                 skb_reset_tail_pointer(skb);
183                 skb_trim(skb, 0);
184                 urb->transfer_buffer = skb_tail_pointer(skb);
185         }
186         skb_queue_tail(&priv->rx_queue, skb);
187         usb_anchor_urb(urb, &priv->submitted);
188         if (usb_submit_urb(urb, GFP_ATOMIC)) {
189                 skb_unlink(skb, &priv->rx_queue);
190                 usb_unanchor_urb(urb);
191                 dev_kfree_skb_irq(skb);
192         }
193 }
194
195 static void p54u_tx_cb(struct urb *urb)
196 {
197         struct sk_buff *skb = urb->context;
198         struct ieee80211_hw *dev =
199                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
200
201         p54_free_skb(dev, skb);
202 }
203
204 static void p54u_tx_dummy_cb(struct urb *urb) { }
205
206 static void p54u_free_urbs(struct ieee80211_hw *dev)
207 {
208         struct p54u_priv *priv = dev->priv;
209         usb_kill_anchored_urbs(&priv->submitted);
210 }
211
212 static void p54u_stop(struct ieee80211_hw *dev)
213 {
214         /*
215          * TODO: figure out how to reliably stop the 3887 and net2280 so
216          * the hardware is still usable next time we want to start it.
217          * until then, we just stop listening to the hardware..
218          */
219         p54u_free_urbs(dev);
220 }
221
222 static int p54u_init_urbs(struct ieee80211_hw *dev)
223 {
224         struct p54u_priv *priv = dev->priv;
225         struct urb *entry = NULL;
226         struct sk_buff *skb;
227         struct p54u_rx_info *info;
228         int ret = 0;
229
230         while (skb_queue_len(&priv->rx_queue) < 32) {
231                 skb = __dev_alloc_skb(priv->common.rx_mtu + 32, GFP_KERNEL);
232                 if (!skb) {
233                         ret = -ENOMEM;
234                         goto err;
235                 }
236                 entry = usb_alloc_urb(0, GFP_KERNEL);
237                 if (!entry) {
238                         ret = -ENOMEM;
239                         goto err;
240                 }
241
242                 usb_fill_bulk_urb(entry, priv->udev,
243                                   usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA),
244                                   skb_tail_pointer(skb),
245                                   priv->common.rx_mtu + 32, p54u_rx_cb, skb);
246                 info = (struct p54u_rx_info *) skb->cb;
247                 info->urb = entry;
248                 info->dev = dev;
249                 skb_queue_tail(&priv->rx_queue, skb);
250
251                 usb_anchor_urb(entry, &priv->submitted);
252                 ret = usb_submit_urb(entry, GFP_KERNEL);
253                 if (ret) {
254                         skb_unlink(skb, &priv->rx_queue);
255                         usb_unanchor_urb(entry);
256                         goto err;
257                 }
258                 usb_free_urb(entry);
259                 entry = NULL;
260         }
261
262         return 0;
263
264  err:
265         usb_free_urb(entry);
266         kfree_skb(skb);
267         p54u_free_urbs(dev);
268         return ret;
269 }
270
271 static int p54u_open(struct ieee80211_hw *dev)
272 {
273         /*
274          * TODO: Because we don't know how to reliably stop the 3887 and
275          * the isl3886+net2280, other than brutally cut off all
276          * communications. We have to reinitialize the urbs on every start.
277          */
278         return p54u_init_urbs(dev);
279 }
280
281 static __le32 p54u_lm87_chksum(const __le32 *data, size_t length)
282 {
283         u32 chk = 0;
284
285         length >>= 2;
286         while (length--) {
287                 chk ^= le32_to_cpu(*data++);
288                 chk = (chk >> 5) ^ (chk << 3);
289         }
290
291         return cpu_to_le32(chk);
292 }
293
294 static void p54u_tx_lm87(struct ieee80211_hw *dev, struct sk_buff *skb)
295 {
296         struct p54u_priv *priv = dev->priv;
297         struct urb *data_urb;
298         struct lm87_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
299
300         data_urb = usb_alloc_urb(0, GFP_ATOMIC);
301         if (!data_urb) {
302                 p54_free_skb(dev, skb);
303                 return;
304         }
305
306         hdr->chksum = p54u_lm87_chksum((__le32 *)skb->data, skb->len);
307         hdr->device_addr = ((struct p54_hdr *)skb->data)->req_id;
308
309         usb_fill_bulk_urb(data_urb, priv->udev,
310                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
311                           hdr, skb->len + sizeof(*hdr),  FREE_AFTER_TX(skb) ?
312                           p54u_tx_cb : p54u_tx_dummy_cb, skb);
313         data_urb->transfer_flags |= URB_ZERO_PACKET;
314
315         usb_anchor_urb(data_urb, &priv->submitted);
316         if (usb_submit_urb(data_urb, GFP_ATOMIC)) {
317                 usb_unanchor_urb(data_urb);
318                 p54_free_skb(dev, skb);
319         }
320         usb_free_urb(data_urb);
321 }
322
323 static void p54u_tx_net2280(struct ieee80211_hw *dev, struct sk_buff *skb)
324 {
325         struct p54u_priv *priv = dev->priv;
326         struct urb *int_urb = NULL, *data_urb = NULL;
327         struct net2280_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
328         struct net2280_reg_write *reg = NULL;
329         int err = -ENOMEM;
330
331         reg = kmalloc(sizeof(*reg), GFP_ATOMIC);
332         if (!reg)
333                 goto out;
334
335         int_urb = usb_alloc_urb(0, GFP_ATOMIC);
336         if (!int_urb)
337                 goto out;
338
339         data_urb = usb_alloc_urb(0, GFP_ATOMIC);
340         if (!data_urb)
341                 goto out;
342
343         reg->port = cpu_to_le16(NET2280_DEV_U32);
344         reg->addr = cpu_to_le32(P54U_DEV_BASE);
345         reg->val = cpu_to_le32(ISL38XX_DEV_INT_DATA);
346
347         memset(hdr, 0, sizeof(*hdr));
348         hdr->len = cpu_to_le16(skb->len);
349         hdr->device_addr = ((struct p54_hdr *) skb->data)->req_id;
350
351         usb_fill_bulk_urb(int_urb, priv->udev,
352                 usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV), reg, sizeof(*reg),
353                 p54u_tx_dummy_cb, dev);
354
355         /*
356          * URB_FREE_BUFFER triggers a code path in the USB subsystem that will
357          * free what is inside the transfer_buffer after the last reference to
358          * the int_urb is dropped.
359          */
360         int_urb->transfer_flags |= URB_FREE_BUFFER | URB_ZERO_PACKET;
361         reg = NULL;
362
363         usb_fill_bulk_urb(data_urb, priv->udev,
364                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
365                           hdr, skb->len + sizeof(*hdr), FREE_AFTER_TX(skb) ?
366                           p54u_tx_cb : p54u_tx_dummy_cb, skb);
367         data_urb->transfer_flags |= URB_ZERO_PACKET;
368
369         usb_anchor_urb(int_urb, &priv->submitted);
370         err = usb_submit_urb(int_urb, GFP_ATOMIC);
371         if (err) {
372                 usb_unanchor_urb(int_urb);
373                 goto out;
374         }
375
376         usb_anchor_urb(data_urb, &priv->submitted);
377         err = usb_submit_urb(data_urb, GFP_ATOMIC);
378         if (err) {
379                 usb_unanchor_urb(data_urb);
380                 goto out;
381         }
382 out:
383         usb_free_urb(int_urb);
384         usb_free_urb(data_urb);
385
386         if (err) {
387                 kfree(reg);
388                 p54_free_skb(dev, skb);
389         }
390 }
391
392 static int p54u_write(struct p54u_priv *priv,
393                       struct net2280_reg_write *buf,
394                       enum net2280_op_type type,
395                       __le32 addr, __le32 val)
396 {
397         unsigned int ep;
398         int alen;
399
400         if (type & 0x0800)
401                 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV);
402         else
403                 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_BRG);
404
405         buf->port = cpu_to_le16(type);
406         buf->addr = addr;
407         buf->val = val;
408
409         return usb_bulk_msg(priv->udev, ep, buf, sizeof(*buf), &alen, 1000);
410 }
411
412 static int p54u_read(struct p54u_priv *priv, void *buf,
413                      enum net2280_op_type type,
414                      __le32 addr, __le32 *val)
415 {
416         struct net2280_reg_read *read = buf;
417         __le32 *reg = buf;
418         unsigned int ep;
419         int alen, err;
420
421         if (type & 0x0800)
422                 ep = P54U_PIPE_DEV;
423         else
424                 ep = P54U_PIPE_BRG;
425
426         read->port = cpu_to_le16(type);
427         read->addr = addr;
428
429         err = usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
430                            read, sizeof(*read), &alen, 1000);
431         if (err)
432                 return err;
433
434         err = usb_bulk_msg(priv->udev, usb_rcvbulkpipe(priv->udev, ep),
435                            reg, sizeof(*reg), &alen, 1000);
436         if (err)
437                 return err;
438
439         *val = *reg;
440         return 0;
441 }
442
443 static int p54u_bulk_msg(struct p54u_priv *priv, unsigned int ep,
444                          void *data, size_t len)
445 {
446         int alen;
447         return usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
448                             data, len, &alen, 2000);
449 }
450
451 static int p54u_device_reset(struct ieee80211_hw *dev)
452 {
453         struct p54u_priv *priv = dev->priv;
454         int ret, lock = (priv->intf->condition != USB_INTERFACE_BINDING);
455
456         if (lock) {
457                 ret = usb_lock_device_for_reset(priv->udev, priv->intf);
458                 if (ret < 0) {
459                         dev_err(&priv->udev->dev, "(p54usb) unable to lock "
460                                 "device for reset (%d)!\n", ret);
461                         return ret;
462                 }
463         }
464
465         ret = usb_reset_device(priv->udev);
466         if (lock)
467                 usb_unlock_device(priv->udev);
468
469         if (ret)
470                 dev_err(&priv->udev->dev, "(p54usb) unable to reset "
471                         "device (%d)!\n", ret);
472
473         return ret;
474 }
475
476 static const char p54u_romboot_3887[] = "~~~~";
477 static int p54u_firmware_reset_3887(struct ieee80211_hw *dev)
478 {
479         struct p54u_priv *priv = dev->priv;
480         u8 *buf;
481         int ret;
482
483         buf = kmemdup(p54u_romboot_3887, 4, GFP_KERNEL);
484         if (!buf)
485                 return -ENOMEM;
486         ret = p54u_bulk_msg(priv, P54U_PIPE_DATA,
487                             buf, 4);
488         kfree(buf);
489         if (ret)
490                 dev_err(&priv->udev->dev, "(p54usb) unable to jump to "
491                         "boot ROM (%d)!\n", ret);
492
493         return ret;
494 }
495
496 static const char p54u_firmware_upload_3887[] = "<\r";
497 static int p54u_upload_firmware_3887(struct ieee80211_hw *dev)
498 {
499         struct p54u_priv *priv = dev->priv;
500         int err, alen;
501         u8 carry = 0;
502         u8 *buf, *tmp;
503         const u8 *data;
504         unsigned int left, remains, block_size;
505         struct x2_header *hdr;
506         unsigned long timeout;
507
508         err = p54u_firmware_reset_3887(dev);
509         if (err)
510                 return err;
511
512         tmp = buf = kmalloc(P54U_FW_BLOCK, GFP_KERNEL);
513         if (!buf) {
514                 dev_err(&priv->udev->dev, "(p54usb) cannot allocate firmware"
515                                           "upload buffer!\n");
516                 return -ENOMEM;
517         }
518
519         left = block_size = min((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);
523
524         data = priv->fw->data;
525         remains = priv->fw->size;
526
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);
535
536         while (remains) {
537                 while (left--) {
538                         if (carry) {
539                                 *tmp++ = carry;
540                                 carry = 0;
541                                 remains--;
542                                 continue;
543                         }
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;
557                         }
558                         data++;
559                 }
560
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;
566                 }
567
568                 tmp = buf;
569                 left = block_size = min((unsigned int)P54U_FW_BLOCK, remains);
570         }
571
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;
578         }
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;
584
585                 if (alen > 5 && !memcmp(buf, "ERROR", 5)) {
586                         err = -EINVAL;
587                         break;
588                 }
589
590                 if (time_after(jiffies, timeout)) {
591                         dev_err(&priv->udev->dev, "(p54usb) firmware boot "
592                                                   "timed out!\n");
593                         err = -ETIMEDOUT;
594                         break;
595                 }
596         }
597         if (err) {
598                 dev_err(&priv->udev->dev, "(p54usb) firmware upload failed!\n");
599                 goto err_upload_failed;
600         }
601
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;
608         }
609
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;
615
616                 if (time_after(jiffies, timeout)) {
617                         err = -ETIMEDOUT;
618                         break;
619                 }
620         }
621         if (err)
622                 goto err_upload_failed;
623
624 err_upload_failed:
625         kfree(buf);
626         return err;
627 }
628
629 static int p54u_upload_firmware_net2280(struct ieee80211_hw *dev)
630 {
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;
638
639         buf = kmalloc(512, GFP_KERNEL);
640         if (!buf) {
641                 dev_err(&priv->udev->dev, "(p54usb) firmware buffer "
642                                           "alloc failed!\n");
643                 return -ENOMEM;
644         }
645
646 #define P54U_WRITE(type, addr, data) \
647         do {\
648                 err = p54u_write(priv, buf, type,\
649                                  cpu_to_le32((u32)(unsigned long)addr), data);\
650                 if (err) \
651                         goto fail;\
652         } while (0)
653
654 #define P54U_READ(type, addr) \
655         do {\
656                 err = p54u_read(priv, buf, type,\
657                                 cpu_to_le32((u32)(unsigned long)addr), &reg);\
658                 if (err)\
659                         goto fail;\
660         } while (0)
661
662         /* power down net2280 bridge */
663         P54U_READ(NET2280_BRG_U32, NET2280_GPIOCTL);
664         reg |= cpu_to_le32(P54U_BRG_POWER_DOWN);
665         reg &= cpu_to_le32(~P54U_BRG_POWER_UP);
666         P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
667
668         mdelay(100);
669
670         /* power up bridge */
671         reg |= cpu_to_le32(P54U_BRG_POWER_UP);
672         reg &= cpu_to_le32(~P54U_BRG_POWER_DOWN);
673         P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
674
675         mdelay(100);
676
677         P54U_WRITE(NET2280_BRG_U32, NET2280_DEVINIT,
678                    cpu_to_le32(NET2280_CLK_30Mhz |
679                                NET2280_PCI_ENABLE |
680                                NET2280_PCI_SOFT_RESET));
681
682         mdelay(20);
683
684         P54U_WRITE(NET2280_BRG_CFG_U16, PCI_COMMAND,
685                    cpu_to_le32(PCI_COMMAND_MEMORY |
686                                PCI_COMMAND_MASTER));
687
688         P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_0,
689                    cpu_to_le32(NET2280_BASE));
690
691         P54U_READ(NET2280_BRG_CFG_U16, PCI_STATUS);
692         reg |= cpu_to_le32(PCI_STATUS_REC_MASTER_ABORT);
693         P54U_WRITE(NET2280_BRG_CFG_U16, PCI_STATUS, reg);
694
695         // TODO: we really need this?
696         P54U_READ(NET2280_BRG_U32, NET2280_RELNUM);
697
698         P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_RSP,
699                    cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
700         P54U_WRITE(NET2280_BRG_U32, NET2280_EPC_RSP,
701                    cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
702
703         P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_2,
704                    cpu_to_le32(NET2280_BASE2));
705
706         /* finally done setting up the bridge */
707
708         P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | PCI_COMMAND,
709                    cpu_to_le32(PCI_COMMAND_MEMORY |
710                                PCI_COMMAND_MASTER));
711
712         P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | 0x40 /* TRDY timeout */, 0);
713         P54U_WRITE(NET2280_DEV_CFG_U32, 0x10000 | PCI_BASE_ADDRESS_0,
714                    cpu_to_le32(P54U_DEV_BASE));
715
716         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
717         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
718                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
719
720         /* do romboot */
721         P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable, 0);
722
723         P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
724         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
725         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
726         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
727         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
728
729         mdelay(20);
730
731         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
732         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
733
734         mdelay(20);
735
736         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
737         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
738
739         mdelay(100);
740
741         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
742         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
743
744         /* finally, we can upload firmware now! */
745         remains = priv->fw->size;
746         data = priv->fw->data;
747         offset = ISL38XX_DEV_FIRMWARE_ADDR;
748
749         while (remains) {
750                 unsigned int block_len = min(remains, (unsigned int)512);
751                 memcpy(buf, data, block_len);
752
753                 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, block_len);
754                 if (err) {
755                         dev_err(&priv->udev->dev, "(p54usb) firmware block "
756                                                   "upload failed\n");
757                         goto fail;
758                 }
759
760                 P54U_WRITE(NET2280_DEV_U32, &devreg->direct_mem_base,
761                            cpu_to_le32(0xc0000f00));
762
763                 P54U_WRITE(NET2280_DEV_U32,
764                            0x0020 | (unsigned long)&devreg->direct_mem_win, 0);
765                 P54U_WRITE(NET2280_DEV_U32,
766                            0x0020 | (unsigned long)&devreg->direct_mem_win,
767                            cpu_to_le32(1));
768
769                 P54U_WRITE(NET2280_DEV_U32,
770                            0x0024 | (unsigned long)&devreg->direct_mem_win,
771                            cpu_to_le32(block_len));
772                 P54U_WRITE(NET2280_DEV_U32,
773                            0x0028 | (unsigned long)&devreg->direct_mem_win,
774                            cpu_to_le32(offset));
775
776                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_addr,
777                            cpu_to_le32(NET2280_EPA_FIFO_PCI_ADDR));
778                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_len,
779                            cpu_to_le32(block_len >> 2));
780                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_ctrl,
781                            cpu_to_le32(ISL38XX_DMA_MASTER_CONTROL_TRIGGER));
782
783                 mdelay(10);
784
785                 P54U_READ(NET2280_DEV_U32,
786                           0x002C | (unsigned long)&devreg->direct_mem_win);
787                 if (!(reg & cpu_to_le32(ISL38XX_DMA_STATUS_DONE)) ||
788                     !(reg & cpu_to_le32(ISL38XX_DMA_STATUS_READY))) {
789                         dev_err(&priv->udev->dev, "(p54usb) firmware DMA "
790                                                   "transfer failed\n");
791                         goto fail;
792                 }
793
794                 P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_STAT,
795                            cpu_to_le32(NET2280_FIFO_FLUSH));
796
797                 remains -= block_len;
798                 data += block_len;
799                 offset += block_len;
800         }
801
802         /* do ramboot */
803         P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
804         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
805         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
806         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
807         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
808
809         mdelay(20);
810
811         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
812         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
813
814         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
815         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
816
817         mdelay(100);
818
819         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
820         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
821
822         /* start up the firmware */
823         P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable,
824                    cpu_to_le32(ISL38XX_INT_IDENT_INIT));
825
826         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
827                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
828
829         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1,
830                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT_ENABLE |
831                                NET2280_USB_INTERRUPT_ENABLE));
832
833         P54U_WRITE(NET2280_DEV_U32, &devreg->dev_int,
834                    cpu_to_le32(ISL38XX_DEV_INT_RESET));
835
836         err = usb_interrupt_msg(priv->udev,
837                                 usb_rcvbulkpipe(priv->udev, P54U_PIPE_INT),
838                                 buf, sizeof(__le32), &alen, 1000);
839         if (err || alen != sizeof(__le32))
840                 goto fail;
841
842         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
843         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
844
845         if (!(reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT)))
846                 err = -EINVAL;
847
848         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
849         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
850                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
851
852 #undef P54U_WRITE
853 #undef P54U_READ
854
855 fail:
856         kfree(buf);
857         return err;
858 }
859
860 static int p54_find_type(struct p54u_priv *priv)
861 {
862         int i;
863
864         for (i = 0; i < __NUM_P54U_HWTYPES; i++)
865                 if (p54u_fwlist[i].type == priv->hw_type)
866                         break;
867         if (i == __NUM_P54U_HWTYPES)
868                 return -EOPNOTSUPP;
869
870         return i;
871 }
872
873 static int p54u_start_ops(struct p54u_priv *priv)
874 {
875         struct ieee80211_hw *dev = priv->common.hw;
876         int ret;
877
878         ret = p54_parse_firmware(dev, priv->fw);
879         if (ret)
880                 goto err_out;
881
882         ret = p54_find_type(priv);
883         if (ret < 0)
884                 goto err_out;
885
886         if (priv->common.fw_interface != p54u_fwlist[ret].intf) {
887                 dev_err(&priv->udev->dev, "wrong firmware, please get "
888                         "a firmware for \"%s\" and try again.\n",
889                         p54u_fwlist[ret].hw);
890                 ret = -ENODEV;
891                 goto err_out;
892         }
893
894         ret = priv->upload_fw(dev);
895         if (ret)
896                 goto err_out;
897
898         ret = p54u_open(dev);
899         if (ret)
900                 goto err_out;
901
902         ret = p54_read_eeprom(dev);
903         if (ret)
904                 goto err_stop;
905
906         p54u_stop(dev);
907
908         ret = p54_register_common(dev, &priv->udev->dev);
909         if (ret)
910                 goto err_stop;
911
912         return 0;
913
914 err_stop:
915         p54u_stop(dev);
916
917 err_out:
918         /*
919          * p54u_disconnect will do the rest of the
920          * cleanup
921          */
922         return ret;
923 }
924
925 static void p54u_load_firmware_cb(const struct firmware *firmware,
926                                   void *context)
927 {
928         struct p54u_priv *priv = context;
929         struct usb_device *udev = priv->udev;
930         int err;
931
932         complete(&priv->fw_wait_load);
933         if (firmware) {
934                 priv->fw = firmware;
935                 err = p54u_start_ops(priv);
936         } else {
937                 err = -ENOENT;
938                 dev_err(&udev->dev, "Firmware not found.\n");
939         }
940
941         if (err) {
942                 struct device *parent = priv->udev->dev.parent;
943
944                 dev_err(&udev->dev, "failed to initialize device (%d)\n", err);
945
946                 if (parent)
947                         device_lock(parent);
948
949                 device_release_driver(&udev->dev);
950                 /*
951                  * At this point p54u_disconnect has already freed
952                  * the "priv" context. Do not use it anymore!
953                  */
954                 priv = NULL;
955
956                 if (parent)
957                         device_unlock(parent);
958         }
959
960         usb_put_dev(udev);
961 }
962
963 static int p54u_load_firmware(struct ieee80211_hw *dev,
964                               struct usb_interface *intf)
965 {
966         struct usb_device *udev = interface_to_usbdev(intf);
967         struct p54u_priv *priv = dev->priv;
968         struct device *device = &udev->dev;
969         int err, i;
970
971         BUILD_BUG_ON(ARRAY_SIZE(p54u_fwlist) != __NUM_P54U_HWTYPES);
972
973         init_completion(&priv->fw_wait_load);
974         i = p54_find_type(priv);
975         if (i < 0)
976                 return i;
977
978         dev_info(&priv->udev->dev, "Loading firmware file %s\n",
979                p54u_fwlist[i].fw);
980
981         usb_get_dev(udev);
982         err = request_firmware_nowait(THIS_MODULE, 1, p54u_fwlist[i].fw,
983                                       device, GFP_KERNEL, priv,
984                                       p54u_load_firmware_cb);
985         if (err) {
986                 dev_err(&priv->udev->dev, "(p54usb) cannot load firmware %s "
987                                           "(%d)!\n", p54u_fwlist[i].fw, err);
988         }
989
990         return err;
991 }
992
993 static int p54u_probe(struct usb_interface *intf,
994                                 const struct usb_device_id *id)
995 {
996         struct usb_device *udev = interface_to_usbdev(intf);
997         struct ieee80211_hw *dev;
998         struct p54u_priv *priv;
999         int err;
1000         unsigned int i, recognized_pipes;
1001
1002         dev = p54_init_common(sizeof(*priv));
1003
1004         if (!dev) {
1005                 dev_err(&udev->dev, "(p54usb) ieee80211 alloc failed\n");
1006                 return -ENOMEM;
1007         }
1008
1009         priv = dev->priv;
1010         priv->hw_type = P54U_INVALID_HW;
1011
1012         SET_IEEE80211_DEV(dev, &intf->dev);
1013         usb_set_intfdata(intf, dev);
1014         priv->udev = udev;
1015         priv->intf = intf;
1016         skb_queue_head_init(&priv->rx_queue);
1017         init_usb_anchor(&priv->submitted);
1018
1019         usb_get_dev(udev);
1020
1021         /* really lazy and simple way of figuring out if we're a 3887 */
1022         /* TODO: should just stick the identification in the device table */
1023         i = intf->altsetting->desc.bNumEndpoints;
1024         recognized_pipes = 0;
1025         while (i--) {
1026                 switch (intf->altsetting->endpoint[i].desc.bEndpointAddress) {
1027                 case P54U_PIPE_DATA:
1028                 case P54U_PIPE_MGMT:
1029                 case P54U_PIPE_BRG:
1030                 case P54U_PIPE_DEV:
1031                 case P54U_PIPE_DATA | USB_DIR_IN:
1032                 case P54U_PIPE_MGMT | USB_DIR_IN:
1033                 case P54U_PIPE_BRG | USB_DIR_IN:
1034                 case P54U_PIPE_DEV | USB_DIR_IN:
1035                 case P54U_PIPE_INT | USB_DIR_IN:
1036                         recognized_pipes++;
1037                 }
1038         }
1039         priv->common.open = p54u_open;
1040         priv->common.stop = p54u_stop;
1041         if (recognized_pipes < P54U_PIPE_NUMBER) {
1042 #ifdef CONFIG_PM
1043                 /* ISL3887 needs a full reset on resume */
1044                 udev->reset_resume = 1;
1045 #endif /* CONFIG_PM */
1046                 err = p54u_device_reset(dev);
1047
1048                 priv->hw_type = P54U_3887;
1049                 dev->extra_tx_headroom += sizeof(struct lm87_tx_hdr);
1050                 priv->common.tx_hdr_len = sizeof(struct lm87_tx_hdr);
1051                 priv->common.tx = p54u_tx_lm87;
1052                 priv->upload_fw = p54u_upload_firmware_3887;
1053         } else {
1054                 priv->hw_type = P54U_NET2280;
1055                 dev->extra_tx_headroom += sizeof(struct net2280_tx_hdr);
1056                 priv->common.tx_hdr_len = sizeof(struct net2280_tx_hdr);
1057                 priv->common.tx = p54u_tx_net2280;
1058                 priv->upload_fw = p54u_upload_firmware_net2280;
1059         }
1060         err = p54u_load_firmware(dev, intf);
1061         return err;
1062 }
1063
1064 static void p54u_disconnect(struct usb_interface *intf)
1065 {
1066         struct ieee80211_hw *dev = usb_get_intfdata(intf);
1067         struct p54u_priv *priv;
1068
1069         if (!dev)
1070                 return;
1071
1072         priv = dev->priv;
1073         wait_for_completion(&priv->fw_wait_load);
1074         p54_unregister_common(dev);
1075
1076         usb_put_dev(interface_to_usbdev(intf));
1077         release_firmware(priv->fw);
1078         p54_free_common(dev);
1079 }
1080
1081 static int p54u_pre_reset(struct usb_interface *intf)
1082 {
1083         struct ieee80211_hw *dev = usb_get_intfdata(intf);
1084
1085         if (!dev)
1086                 return -ENODEV;
1087
1088         p54u_stop(dev);
1089         return 0;
1090 }
1091
1092 static int p54u_resume(struct usb_interface *intf)
1093 {
1094         struct ieee80211_hw *dev = usb_get_intfdata(intf);
1095         struct p54u_priv *priv;
1096
1097         if (!dev)
1098                 return -ENODEV;
1099
1100         priv = dev->priv;
1101         if (unlikely(!(priv->upload_fw && priv->fw)))
1102                 return 0;
1103
1104         return priv->upload_fw(dev);
1105 }
1106
1107 static int p54u_post_reset(struct usb_interface *intf)
1108 {
1109         struct ieee80211_hw *dev = usb_get_intfdata(intf);
1110         struct p54u_priv *priv;
1111         int err;
1112
1113         err = p54u_resume(intf);
1114         if (err)
1115                 return err;
1116
1117         /* reinitialize old device state */
1118         priv = dev->priv;
1119         if (priv->common.mode != NL80211_IFTYPE_UNSPECIFIED)
1120                 ieee80211_restart_hw(dev);
1121
1122         return 0;
1123 }
1124
1125 #ifdef CONFIG_PM
1126
1127 static int p54u_suspend(struct usb_interface *intf, pm_message_t message)
1128 {
1129         return p54u_pre_reset(intf);
1130 }
1131
1132 #endif /* CONFIG_PM */
1133
1134 static struct usb_driver p54u_driver = {
1135         .name   = "p54usb",
1136         .id_table = p54u_table,
1137         .probe = p54u_probe,
1138         .disconnect = p54u_disconnect,
1139         .pre_reset = p54u_pre_reset,
1140         .post_reset = p54u_post_reset,
1141 #ifdef CONFIG_PM
1142         .suspend = p54u_suspend,
1143         .resume = p54u_resume,
1144         .reset_resume = p54u_resume,
1145 #endif /* CONFIG_PM */
1146         .soft_unbind = 1,
1147         .disable_hub_initiated_lpm = 1,
1148 };
1149
1150 module_usb_driver(p54u_driver);