Merge branch 'for-rmk' of git://git.kernel.org/pub/scm/linux/kernel/git/khilman/linux...
[linux-2.6] / 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/firmware.h>
19 #include <linux/etherdevice.h>
20 #include <linux/delay.h>
21 #include <linux/crc32.h>
22 #include <net/mac80211.h>
23
24 #include "p54.h"
25 #include "p54usb.h"
26
27 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
28 MODULE_DESCRIPTION("Prism54 USB wireless driver");
29 MODULE_LICENSE("GPL");
30 MODULE_ALIAS("prism54usb");
31 MODULE_FIRMWARE("isl3886usb");
32 MODULE_FIRMWARE("isl3887usb");
33
34 static struct usb_device_id p54u_table[] __devinitdata = {
35         /* Version 1 devices (pci chip + net2280) */
36         {USB_DEVICE(0x0506, 0x0a11)},   /* 3COM 3CRWE254G72 */
37         {USB_DEVICE(0x0707, 0xee06)},   /* SMC 2862W-G */
38         {USB_DEVICE(0x083a, 0x4501)},   /* Accton 802.11g WN4501 USB */
39         {USB_DEVICE(0x083a, 0x4502)},   /* Siemens Gigaset USB Adapter */
40         {USB_DEVICE(0x083a, 0x5501)},   /* Phillips CPWUA054 */
41         {USB_DEVICE(0x0846, 0x4200)},   /* Netgear WG121 */
42         {USB_DEVICE(0x0846, 0x4210)},   /* Netgear WG121 the second ? */
43         {USB_DEVICE(0x0846, 0x4220)},   /* Netgear WG111 */
44         {USB_DEVICE(0x09aa, 0x1000)},   /* Spinnaker Proto board */
45         {USB_DEVICE(0x0cde, 0x0006)},   /* Medion 40900, Roper Europe */
46         {USB_DEVICE(0x124a, 0x4023)},   /* Shuttle PN15, Airvast WM168g, IOGear GWU513 */
47         {USB_DEVICE(0x1915, 0x2234)},   /* Linksys WUSB54G OEM */
48         {USB_DEVICE(0x1915, 0x2235)},   /* Linksys WUSB54G Portable OEM */
49         {USB_DEVICE(0x2001, 0x3701)},   /* DLink DWL-G120 Spinnaker */
50         {USB_DEVICE(0x2001, 0x3703)},   /* DLink DWL-G122 */
51         {USB_DEVICE(0x5041, 0x2234)},   /* Linksys WUSB54G */
52         {USB_DEVICE(0x5041, 0x2235)},   /* Linksys WUSB54G Portable */
53
54         /* Version 2 devices (3887) */
55         {USB_DEVICE(0x0471, 0x1230)},   /* Philips CPWUA054/00 */
56         {USB_DEVICE(0x050d, 0x7050)},   /* Belkin F5D7050 ver 1000 */
57         {USB_DEVICE(0x0572, 0x2000)},   /* Cohiba Proto board */
58         {USB_DEVICE(0x0572, 0x2002)},   /* Cohiba Proto board */
59         {USB_DEVICE(0x06b9, 0x0121)},   /* Thomson SpeedTouch 121g */
60         {USB_DEVICE(0x0707, 0xee13)},   /* SMC 2862W-G version 2 */
61         {USB_DEVICE(0x083a, 0x4521)},   /* Siemens Gigaset USB Adapter 54 version 2 */
62         {USB_DEVICE(0x0846, 0x4240)},   /* Netgear WG111 (v2) */
63         {USB_DEVICE(0x0915, 0x2000)},   /* Cohiba Proto board */
64         {USB_DEVICE(0x0915, 0x2002)},   /* Cohiba Proto board */
65         {USB_DEVICE(0x0baf, 0x0118)},   /* U.S. Robotics U5 802.11g Adapter*/
66         {USB_DEVICE(0x0bf8, 0x1009)},   /* FUJITSU E-5400 USB D1700*/
67         {USB_DEVICE(0x0cde, 0x0006)},   /* Medion MD40900 */
68         {USB_DEVICE(0x0cde, 0x0008)},   /* Sagem XG703A */
69         {USB_DEVICE(0x0d8e, 0x3762)},   /* DLink DWL-G120 Cohiba */
70         {USB_DEVICE(0x124a, 0x4025)},   /* IOGear GWU513 (GW3887IK chip) */
71         {USB_DEVICE(0x1260, 0xee22)},   /* SMC 2862W-G version 2 */
72         {USB_DEVICE(0x13b1, 0x000a)},   /* Linksys WUSB54G ver 2 */
73         {USB_DEVICE(0x13B1, 0x000C)},   /* Linksys WUSB54AG */
74         {USB_DEVICE(0x1413, 0x5400)},   /* Telsey 802.11g USB2.0 Adapter */
75         {USB_DEVICE(0x1435, 0x0427)},   /* Inventel UR054G */
76         {USB_DEVICE(0x2001, 0x3704)},   /* DLink DWL-G122 rev A2 */
77         {USB_DEVICE(0x413c, 0x8102)},   /* Spinnaker DUT */
78         {USB_DEVICE(0x413c, 0x8104)},   /* Cohiba Proto board */
79         {}
80 };
81
82 MODULE_DEVICE_TABLE(usb, p54u_table);
83
84 static void p54u_rx_cb(struct urb *urb)
85 {
86         struct sk_buff *skb = (struct sk_buff *) urb->context;
87         struct p54u_rx_info *info = (struct p54u_rx_info *)skb->cb;
88         struct ieee80211_hw *dev = info->dev;
89         struct p54u_priv *priv = dev->priv;
90
91         skb_unlink(skb, &priv->rx_queue);
92
93         if (unlikely(urb->status)) {
94                 dev_kfree_skb_irq(skb);
95                 return;
96         }
97
98         skb_put(skb, urb->actual_length);
99
100         if (priv->hw_type == P54U_NET2280)
101                 skb_pull(skb, priv->common.tx_hdr_len);
102         if (priv->common.fw_interface == FW_LM87) {
103                 skb_pull(skb, 4);
104                 skb_put(skb, 4);
105         }
106
107         if (p54_rx(dev, skb)) {
108                 skb = dev_alloc_skb(priv->common.rx_mtu + 32);
109                 if (unlikely(!skb)) {
110                         /* TODO check rx queue length and refill *somewhere* */
111                         return;
112                 }
113
114                 info = (struct p54u_rx_info *) skb->cb;
115                 info->urb = urb;
116                 info->dev = dev;
117                 urb->transfer_buffer = skb_tail_pointer(skb);
118                 urb->context = skb;
119         } else {
120                 if (priv->hw_type == P54U_NET2280)
121                         skb_push(skb, priv->common.tx_hdr_len);
122                 if (priv->common.fw_interface == FW_LM87) {
123                         skb_push(skb, 4);
124                         skb_put(skb, 4);
125                 }
126                 skb_reset_tail_pointer(skb);
127                 skb_trim(skb, 0);
128                 if (urb->transfer_buffer != skb_tail_pointer(skb)) {
129                         /* this should not happen */
130                         WARN_ON(1);
131                         urb->transfer_buffer = skb_tail_pointer(skb);
132                 }
133         }
134         skb_queue_tail(&priv->rx_queue, skb);
135         usb_anchor_urb(urb, &priv->submitted);
136         if (usb_submit_urb(urb, GFP_ATOMIC)) {
137                 skb_unlink(skb, &priv->rx_queue);
138                 usb_unanchor_urb(urb);
139                 dev_kfree_skb_irq(skb);
140         }
141 }
142
143 static void p54u_tx_cb(struct urb *urb)
144 {
145         struct sk_buff *skb = urb->context;
146         struct ieee80211_hw *dev = (struct ieee80211_hw *)
147                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
148
149         p54_free_skb(dev, skb);
150 }
151
152 static void p54u_tx_dummy_cb(struct urb *urb) { }
153
154 static void p54u_free_urbs(struct ieee80211_hw *dev)
155 {
156         struct p54u_priv *priv = dev->priv;
157         usb_kill_anchored_urbs(&priv->submitted);
158 }
159
160 static int p54u_init_urbs(struct ieee80211_hw *dev)
161 {
162         struct p54u_priv *priv = dev->priv;
163         struct urb *entry = NULL;
164         struct sk_buff *skb;
165         struct p54u_rx_info *info;
166         int ret = 0;
167
168         while (skb_queue_len(&priv->rx_queue) < 32) {
169                 skb = __dev_alloc_skb(priv->common.rx_mtu + 32, GFP_KERNEL);
170                 if (!skb) {
171                         ret = -ENOMEM;
172                         goto err;
173                 }
174                 entry = usb_alloc_urb(0, GFP_KERNEL);
175                 if (!entry) {
176                         ret = -ENOMEM;
177                         goto err;
178                 }
179
180                 usb_fill_bulk_urb(entry, priv->udev,
181                                   usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA),
182                                   skb_tail_pointer(skb),
183                                   priv->common.rx_mtu + 32, p54u_rx_cb, skb);
184                 info = (struct p54u_rx_info *) skb->cb;
185                 info->urb = entry;
186                 info->dev = dev;
187                 skb_queue_tail(&priv->rx_queue, skb);
188
189                 usb_anchor_urb(entry, &priv->submitted);
190                 ret = usb_submit_urb(entry, GFP_KERNEL);
191                 if (ret) {
192                         skb_unlink(skb, &priv->rx_queue);
193                         usb_unanchor_urb(entry);
194                         goto err;
195                 }
196                 usb_free_urb(entry);
197                 entry = NULL;
198         }
199
200         return 0;
201
202  err:
203         usb_free_urb(entry);
204         kfree_skb(skb);
205         p54u_free_urbs(dev);
206         return ret;
207 }
208
209 static void p54u_tx_3887(struct ieee80211_hw *dev, struct sk_buff *skb)
210 {
211         struct p54u_priv *priv = dev->priv;
212         struct urb *addr_urb, *data_urb;
213         int err = 0;
214
215         addr_urb = usb_alloc_urb(0, GFP_ATOMIC);
216         if (!addr_urb)
217                 return;
218
219         data_urb = usb_alloc_urb(0, GFP_ATOMIC);
220         if (!data_urb) {
221                 usb_free_urb(addr_urb);
222                 return;
223         }
224
225         usb_fill_bulk_urb(addr_urb, priv->udev,
226                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
227                           &((struct p54_hdr *)skb->data)->req_id, 4,
228                           p54u_tx_dummy_cb, dev);
229         usb_fill_bulk_urb(data_urb, priv->udev,
230                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
231                           skb->data, skb->len, FREE_AFTER_TX(skb) ?
232                           p54u_tx_cb : p54u_tx_dummy_cb, skb);
233         addr_urb->transfer_flags |= URB_ZERO_PACKET;
234         data_urb->transfer_flags |= URB_ZERO_PACKET;
235
236         usb_anchor_urb(addr_urb, &priv->submitted);
237         err = usb_submit_urb(addr_urb, GFP_ATOMIC);
238         if (err) {
239                 usb_unanchor_urb(addr_urb);
240                 goto out;
241         }
242
243         usb_anchor_urb(data_urb, &priv->submitted);
244         err = usb_submit_urb(data_urb, GFP_ATOMIC);
245         if (err)
246                 usb_unanchor_urb(data_urb);
247
248  out:
249         usb_free_urb(addr_urb);
250         usb_free_urb(data_urb);
251
252         if (err)
253                 p54_free_skb(dev, skb);
254 }
255
256 static __le32 p54u_lm87_chksum(const __le32 *data, size_t length)
257 {
258         u32 chk = 0;
259
260         length >>= 2;
261         while (length--) {
262                 chk ^= le32_to_cpu(*data++);
263                 chk = (chk >> 5) ^ (chk << 3);
264         }
265
266         return cpu_to_le32(chk);
267 }
268
269 static void p54u_tx_lm87(struct ieee80211_hw *dev, struct sk_buff *skb)
270 {
271         struct p54u_priv *priv = dev->priv;
272         struct urb *data_urb;
273         struct lm87_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
274
275         data_urb = usb_alloc_urb(0, GFP_ATOMIC);
276         if (!data_urb)
277                 return;
278
279         hdr->chksum = p54u_lm87_chksum((__le32 *)skb->data, skb->len);
280         hdr->device_addr = ((struct p54_hdr *)skb->data)->req_id;
281
282         usb_fill_bulk_urb(data_urb, priv->udev,
283                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
284                           hdr, skb->len + sizeof(*hdr),  FREE_AFTER_TX(skb) ?
285                           p54u_tx_cb : p54u_tx_dummy_cb, skb);
286         data_urb->transfer_flags |= URB_ZERO_PACKET;
287
288         usb_anchor_urb(data_urb, &priv->submitted);
289         if (usb_submit_urb(data_urb, GFP_ATOMIC)) {
290                 usb_unanchor_urb(data_urb);
291                 p54_free_skb(dev, skb);
292         }
293         usb_free_urb(data_urb);
294 }
295
296 static void p54u_tx_net2280(struct ieee80211_hw *dev, struct sk_buff *skb)
297 {
298         struct p54u_priv *priv = dev->priv;
299         struct urb *int_urb, *data_urb;
300         struct net2280_tx_hdr *hdr = (void *)skb->data - sizeof(*hdr);
301         struct net2280_reg_write *reg;
302         int err = 0;
303
304         reg = kmalloc(sizeof(*reg), GFP_ATOMIC);
305         if (!reg)
306                 return;
307
308         int_urb = usb_alloc_urb(0, GFP_ATOMIC);
309         if (!int_urb) {
310                 kfree(reg);
311                 return;
312         }
313
314         data_urb = usb_alloc_urb(0, GFP_ATOMIC);
315         if (!data_urb) {
316                 kfree(reg);
317                 usb_free_urb(int_urb);
318                 return;
319         }
320
321         reg->port = cpu_to_le16(NET2280_DEV_U32);
322         reg->addr = cpu_to_le32(P54U_DEV_BASE);
323         reg->val = cpu_to_le32(ISL38XX_DEV_INT_DATA);
324
325         memset(hdr, 0, sizeof(*hdr));
326         hdr->len = cpu_to_le16(skb->len);
327         hdr->device_addr = ((struct p54_hdr *) skb->data)->req_id;
328
329         usb_fill_bulk_urb(int_urb, priv->udev,
330                 usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV), reg, sizeof(*reg),
331                 p54u_tx_dummy_cb, dev);
332
333         /*
334          * This flag triggers a code path in the USB subsystem that will
335          * free what's inside the transfer_buffer after the callback routine
336          * has completed.
337          */
338         int_urb->transfer_flags |= URB_FREE_BUFFER | URB_ZERO_PACKET;
339
340         usb_fill_bulk_urb(data_urb, priv->udev,
341                           usb_sndbulkpipe(priv->udev, P54U_PIPE_DATA),
342                           hdr, skb->len + sizeof(*hdr), FREE_AFTER_TX(skb) ?
343                           p54u_tx_cb : p54u_tx_dummy_cb, skb);
344         data_urb->transfer_flags |= URB_ZERO_PACKET;
345
346         usb_anchor_urb(int_urb, &priv->submitted);
347         err = usb_submit_urb(int_urb, GFP_ATOMIC);
348         if (err) {
349                 usb_unanchor_urb(int_urb);
350                 goto out;
351         }
352
353         usb_anchor_urb(data_urb, &priv->submitted);
354         err = usb_submit_urb(data_urb, GFP_ATOMIC);
355         if (err) {
356                 usb_unanchor_urb(data_urb);
357                 goto out;
358         }
359  out:
360         usb_free_urb(int_urb);
361         usb_free_urb(data_urb);
362
363         if (err) {
364                 skb_pull(skb, sizeof(*hdr));
365                 p54_free_skb(dev, skb);
366         }
367 }
368
369 static int p54u_write(struct p54u_priv *priv,
370                       struct net2280_reg_write *buf,
371                       enum net2280_op_type type,
372                       __le32 addr, __le32 val)
373 {
374         unsigned int ep;
375         int alen;
376
377         if (type & 0x0800)
378                 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_DEV);
379         else
380                 ep = usb_sndbulkpipe(priv->udev, P54U_PIPE_BRG);
381
382         buf->port = cpu_to_le16(type);
383         buf->addr = addr;
384         buf->val = val;
385
386         return usb_bulk_msg(priv->udev, ep, buf, sizeof(*buf), &alen, 1000);
387 }
388
389 static int p54u_read(struct p54u_priv *priv, void *buf,
390                      enum net2280_op_type type,
391                      __le32 addr, __le32 *val)
392 {
393         struct net2280_reg_read *read = buf;
394         __le32 *reg = buf;
395         unsigned int ep;
396         int alen, err;
397
398         if (type & 0x0800)
399                 ep = P54U_PIPE_DEV;
400         else
401                 ep = P54U_PIPE_BRG;
402
403         read->port = cpu_to_le16(type);
404         read->addr = addr;
405
406         err = usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
407                            read, sizeof(*read), &alen, 1000);
408         if (err)
409                 return err;
410
411         err = usb_bulk_msg(priv->udev, usb_rcvbulkpipe(priv->udev, ep),
412                            reg, sizeof(*reg), &alen, 1000);
413         if (err)
414                 return err;
415
416         *val = *reg;
417         return 0;
418 }
419
420 static int p54u_bulk_msg(struct p54u_priv *priv, unsigned int ep,
421                          void *data, size_t len)
422 {
423         int alen;
424         return usb_bulk_msg(priv->udev, usb_sndbulkpipe(priv->udev, ep),
425                             data, len, &alen, 2000);
426 }
427
428 static const char p54u_romboot_3887[] = "~~~~";
429 static const char p54u_firmware_upload_3887[] = "<\r";
430
431 static int p54u_device_reset_3887(struct ieee80211_hw *dev)
432 {
433         struct p54u_priv *priv = dev->priv;
434         int ret, lock = (priv->intf->condition != USB_INTERFACE_BINDING);
435         u8 buf[4];
436
437         if (lock) {
438                 ret = usb_lock_device_for_reset(priv->udev, priv->intf);
439                 if (ret < 0) {
440                         dev_err(&priv->udev->dev, "(p54usb) unable to lock "
441                                 " device for reset: %d\n", ret);
442                         return ret;
443                 }
444         }
445
446         ret = usb_reset_device(priv->udev);
447         if (lock)
448                 usb_unlock_device(priv->udev);
449
450         if (ret) {
451                 dev_err(&priv->udev->dev, "(p54usb) unable to reset "
452                         "device: %d\n", ret);
453                 return ret;
454         }
455
456         memcpy(&buf, p54u_romboot_3887, sizeof(buf));
457         ret = p54u_bulk_msg(priv, P54U_PIPE_DATA,
458                             buf, sizeof(buf));
459         if (ret)
460                 dev_err(&priv->udev->dev, "(p54usb) unable to jump to "
461                         "boot ROM: %d\n", ret);
462
463         return ret;
464 }
465
466 static int p54u_upload_firmware_3887(struct ieee80211_hw *dev)
467 {
468         struct p54u_priv *priv = dev->priv;
469         const struct firmware *fw_entry = NULL;
470         int err, alen;
471         u8 carry = 0;
472         u8 *buf, *tmp;
473         const u8 *data;
474         unsigned int left, remains, block_size;
475         struct x2_header *hdr;
476         unsigned long timeout;
477
478         tmp = buf = kmalloc(P54U_FW_BLOCK, GFP_KERNEL);
479         if (!buf) {
480                 dev_err(&priv->udev->dev, "(p54usb) cannot allocate firmware"
481                                           "upload buffer!\n");
482                 err = -ENOMEM;
483                 goto err_bufalloc;
484         }
485
486         err = p54u_device_reset_3887(dev);
487         if (err)
488                 goto err_reset;
489
490         err = request_firmware(&fw_entry, "isl3887usb", &priv->udev->dev);
491         if (err) {
492                 dev_err(&priv->udev->dev, "p54usb: cannot find firmware "
493                                           "(isl3887usb)\n");
494                 err = request_firmware(&fw_entry, "isl3887usb_bare",
495                         &priv->udev->dev);
496                 if (err)
497                         goto err_req_fw_failed;
498         }
499
500         err = p54_parse_firmware(dev, fw_entry);
501         if (err)
502                 goto err_upload_failed;
503
504         if (priv->common.fw_interface != FW_LM87) {
505                 dev_err(&priv->udev->dev, "wrong firmware, "
506                         "please get a LM87 firmware and try again.\n");
507                 err = -EINVAL;
508                 goto err_upload_failed;
509         }
510
511         left = block_size = min((size_t)P54U_FW_BLOCK, fw_entry->size);
512         strcpy(buf, p54u_firmware_upload_3887);
513         left -= strlen(p54u_firmware_upload_3887);
514         tmp += strlen(p54u_firmware_upload_3887);
515
516         data = fw_entry->data;
517         remains = fw_entry->size;
518
519         hdr = (struct x2_header *)(buf + strlen(p54u_firmware_upload_3887));
520         memcpy(hdr->signature, X2_SIGNATURE, X2_SIGNATURE_SIZE);
521         hdr->fw_load_addr = cpu_to_le32(ISL38XX_DEV_FIRMWARE_ADDR);
522         hdr->fw_length = cpu_to_le32(fw_entry->size);
523         hdr->crc = cpu_to_le32(~crc32_le(~0, (void *)&hdr->fw_load_addr,
524                                          sizeof(u32)*2));
525         left -= sizeof(*hdr);
526         tmp += sizeof(*hdr);
527
528         while (remains) {
529                 while (left--) {
530                         if (carry) {
531                                 *tmp++ = carry;
532                                 carry = 0;
533                                 remains--;
534                                 continue;
535                         }
536                         switch (*data) {
537                         case '~':
538                                 *tmp++ = '}';
539                                 carry = '^';
540                                 break;
541                         case '}':
542                                 *tmp++ = '}';
543                                 carry = ']';
544                                 break;
545                         default:
546                                 *tmp++ = *data;
547                                 remains--;
548                                 break;
549                         }
550                         data++;
551                 }
552
553                 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, block_size);
554                 if (err) {
555                         dev_err(&priv->udev->dev, "(p54usb) firmware "
556                                                   "upload failed!\n");
557                         goto err_upload_failed;
558                 }
559
560                 tmp = buf;
561                 left = block_size = min((unsigned int)P54U_FW_BLOCK, remains);
562         }
563
564         *((__le32 *)buf) = cpu_to_le32(~crc32_le(~0, fw_entry->data, fw_entry->size));
565         err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, sizeof(u32));
566         if (err) {
567                 dev_err(&priv->udev->dev, "(p54usb) firmware upload failed!\n");
568                 goto err_upload_failed;
569         }
570         timeout = jiffies + msecs_to_jiffies(1000);
571         while (!(err = usb_bulk_msg(priv->udev,
572                 usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA), buf, 128, &alen, 1000))) {
573                 if (alen > 2 && !memcmp(buf, "OK", 2))
574                         break;
575
576                 if (alen > 5 && !memcmp(buf, "ERROR", 5)) {
577                         err = -EINVAL;
578                         break;
579                 }
580
581                 if (time_after(jiffies, timeout)) {
582                         dev_err(&priv->udev->dev, "(p54usb) firmware boot "
583                                                   "timed out!\n");
584                         err = -ETIMEDOUT;
585                         break;
586                 }
587         }
588         if (err) {
589                 dev_err(&priv->udev->dev, "(p54usb) firmware upload failed!\n");
590                 goto err_upload_failed;
591         }
592
593         buf[0] = 'g';
594         buf[1] = '\r';
595         err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, 2);
596         if (err) {
597                 dev_err(&priv->udev->dev, "(p54usb) firmware boot failed!\n");
598                 goto err_upload_failed;
599         }
600
601         timeout = jiffies + msecs_to_jiffies(1000);
602         while (!(err = usb_bulk_msg(priv->udev,
603                 usb_rcvbulkpipe(priv->udev, P54U_PIPE_DATA), buf, 128, &alen, 1000))) {
604                 if (alen > 0 && buf[0] == 'g')
605                         break;
606
607                 if (time_after(jiffies, timeout)) {
608                         err = -ETIMEDOUT;
609                         break;
610                 }
611         }
612         if (err)
613                 goto err_upload_failed;
614
615   err_upload_failed:
616         release_firmware(fw_entry);
617   err_req_fw_failed:
618   err_reset:
619         kfree(buf);
620   err_bufalloc:
621         return err;
622 }
623
624 static int p54u_upload_firmware_net2280(struct ieee80211_hw *dev)
625 {
626         struct p54u_priv *priv = dev->priv;
627         const struct firmware *fw_entry = NULL;
628         const struct p54p_csr *devreg = (const struct p54p_csr *) P54U_DEV_BASE;
629         int err, alen;
630         void *buf;
631         __le32 reg;
632         unsigned int remains, offset;
633         const u8 *data;
634
635         buf = kmalloc(512, GFP_KERNEL);
636         if (!buf) {
637                 dev_err(&priv->udev->dev, "(p54usb) firmware buffer "
638                                           "alloc failed!\n");
639                 return -ENOMEM;
640         }
641
642         err = request_firmware(&fw_entry, "isl3886usb", &priv->udev->dev);
643         if (err) {
644                 dev_err(&priv->udev->dev, "(p54usb) cannot find firmware "
645                                           "(isl3886usb)\n");
646                 err = request_firmware(&fw_entry, "isl3890usb",
647                         &priv->udev->dev);
648                 if (err) {
649                         kfree(buf);
650                         return err;
651                         }
652         }
653
654         err = p54_parse_firmware(dev, fw_entry);
655         if (err) {
656                 kfree(buf);
657                 release_firmware(fw_entry);
658                 return err;
659         }
660
661         if (priv->common.fw_interface != FW_LM86) {
662                 dev_err(&priv->udev->dev, "wrong firmware, "
663                         "please get a LM86(USB) firmware and try again.\n");
664                 kfree(buf);
665                 release_firmware(fw_entry);
666                 return -EINVAL;
667         }
668
669 #define P54U_WRITE(type, addr, data) \
670         do {\
671                 err = p54u_write(priv, buf, type,\
672                                  cpu_to_le32((u32)(unsigned long)addr), data);\
673                 if (err) \
674                         goto fail;\
675         } while (0)
676
677 #define P54U_READ(type, addr) \
678         do {\
679                 err = p54u_read(priv, buf, type,\
680                                 cpu_to_le32((u32)(unsigned long)addr), &reg);\
681                 if (err)\
682                         goto fail;\
683         } while (0)
684
685         /* power down net2280 bridge */
686         P54U_READ(NET2280_BRG_U32, NET2280_GPIOCTL);
687         reg |= cpu_to_le32(P54U_BRG_POWER_DOWN);
688         reg &= cpu_to_le32(~P54U_BRG_POWER_UP);
689         P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
690
691         mdelay(100);
692
693         /* power up bridge */
694         reg |= cpu_to_le32(P54U_BRG_POWER_UP);
695         reg &= cpu_to_le32(~P54U_BRG_POWER_DOWN);
696         P54U_WRITE(NET2280_BRG_U32, NET2280_GPIOCTL, reg);
697
698         mdelay(100);
699
700         P54U_WRITE(NET2280_BRG_U32, NET2280_DEVINIT,
701                    cpu_to_le32(NET2280_CLK_30Mhz |
702                                NET2280_PCI_ENABLE |
703                                NET2280_PCI_SOFT_RESET));
704
705         mdelay(20);
706
707         P54U_WRITE(NET2280_BRG_CFG_U16, PCI_COMMAND,
708                    cpu_to_le32(PCI_COMMAND_MEMORY |
709                                PCI_COMMAND_MASTER));
710
711         P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_0,
712                    cpu_to_le32(NET2280_BASE));
713
714         P54U_READ(NET2280_BRG_CFG_U16, PCI_STATUS);
715         reg |= cpu_to_le32(PCI_STATUS_REC_MASTER_ABORT);
716         P54U_WRITE(NET2280_BRG_CFG_U16, PCI_STATUS, reg);
717
718         // TODO: we really need this?
719         P54U_READ(NET2280_BRG_U32, NET2280_RELNUM);
720
721         P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_RSP,
722                    cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
723         P54U_WRITE(NET2280_BRG_U32, NET2280_EPC_RSP,
724                    cpu_to_le32(NET2280_CLEAR_NAK_OUT_PACKETS_MODE));
725
726         P54U_WRITE(NET2280_BRG_CFG_U32, PCI_BASE_ADDRESS_2,
727                    cpu_to_le32(NET2280_BASE2));
728
729         /* finally done setting up the bridge */
730
731         P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | PCI_COMMAND,
732                    cpu_to_le32(PCI_COMMAND_MEMORY |
733                                PCI_COMMAND_MASTER));
734
735         P54U_WRITE(NET2280_DEV_CFG_U16, 0x10000 | 0x40 /* TRDY timeout */, 0);
736         P54U_WRITE(NET2280_DEV_CFG_U32, 0x10000 | PCI_BASE_ADDRESS_0,
737                    cpu_to_le32(P54U_DEV_BASE));
738
739         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
740         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
741                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
742
743         /* do romboot */
744         P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable, 0);
745
746         P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
747         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
748         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RAMBOOT);
749         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
750         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
751
752         mdelay(20);
753
754         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
755         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
756
757         mdelay(20);
758
759         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
760         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
761
762         mdelay(100);
763
764         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
765         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
766
767         /* finally, we can upload firmware now! */
768         remains = fw_entry->size;
769         data = fw_entry->data;
770         offset = ISL38XX_DEV_FIRMWARE_ADDR;
771
772         while (remains) {
773                 unsigned int block_len = min(remains, (unsigned int)512);
774                 memcpy(buf, data, block_len);
775
776                 err = p54u_bulk_msg(priv, P54U_PIPE_DATA, buf, block_len);
777                 if (err) {
778                         dev_err(&priv->udev->dev, "(p54usb) firmware block "
779                                                   "upload failed\n");
780                         goto fail;
781                 }
782
783                 P54U_WRITE(NET2280_DEV_U32, &devreg->direct_mem_base,
784                            cpu_to_le32(0xc0000f00));
785
786                 P54U_WRITE(NET2280_DEV_U32,
787                            0x0020 | (unsigned long)&devreg->direct_mem_win, 0);
788                 P54U_WRITE(NET2280_DEV_U32,
789                            0x0020 | (unsigned long)&devreg->direct_mem_win,
790                            cpu_to_le32(1));
791
792                 P54U_WRITE(NET2280_DEV_U32,
793                            0x0024 | (unsigned long)&devreg->direct_mem_win,
794                            cpu_to_le32(block_len));
795                 P54U_WRITE(NET2280_DEV_U32,
796                            0x0028 | (unsigned long)&devreg->direct_mem_win,
797                            cpu_to_le32(offset));
798
799                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_addr,
800                            cpu_to_le32(NET2280_EPA_FIFO_PCI_ADDR));
801                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_len,
802                            cpu_to_le32(block_len >> 2));
803                 P54U_WRITE(NET2280_DEV_U32, &devreg->dma_ctrl,
804                            cpu_to_le32(ISL38XX_DMA_MASTER_CONTROL_TRIGGER));
805
806                 mdelay(10);
807
808                 P54U_READ(NET2280_DEV_U32,
809                           0x002C | (unsigned long)&devreg->direct_mem_win);
810                 if (!(reg & cpu_to_le32(ISL38XX_DMA_STATUS_DONE)) ||
811                     !(reg & cpu_to_le32(ISL38XX_DMA_STATUS_READY))) {
812                         dev_err(&priv->udev->dev, "(p54usb) firmware DMA "
813                                                   "transfer failed\n");
814                         goto fail;
815                 }
816
817                 P54U_WRITE(NET2280_BRG_U32, NET2280_EPA_STAT,
818                            cpu_to_le32(NET2280_FIFO_FLUSH));
819
820                 remains -= block_len;
821                 data += block_len;
822                 offset += block_len;
823         }
824
825         /* do ramboot */
826         P54U_READ(NET2280_DEV_U32, &devreg->ctrl_stat);
827         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
828         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_CLKRUN);
829         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RAMBOOT);
830         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
831
832         mdelay(20);
833
834         reg |= cpu_to_le32(ISL38XX_CTRL_STAT_RESET);
835         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
836
837         reg &= cpu_to_le32(~ISL38XX_CTRL_STAT_RESET);
838         P54U_WRITE(NET2280_DEV_U32, &devreg->ctrl_stat, reg);
839
840         mdelay(100);
841
842         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
843         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
844
845         /* start up the firmware */
846         P54U_WRITE(NET2280_DEV_U32, &devreg->int_enable,
847                    cpu_to_le32(ISL38XX_INT_IDENT_INIT));
848
849         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
850                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
851
852         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1,
853                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT_ENABLE |
854                                NET2280_USB_INTERRUPT_ENABLE));
855
856         P54U_WRITE(NET2280_DEV_U32, &devreg->dev_int,
857                    cpu_to_le32(ISL38XX_DEV_INT_RESET));
858
859         err = usb_interrupt_msg(priv->udev,
860                                 usb_rcvbulkpipe(priv->udev, P54U_PIPE_INT),
861                                 buf, sizeof(__le32), &alen, 1000);
862         if (err || alen != sizeof(__le32))
863                 goto fail;
864
865         P54U_READ(NET2280_DEV_U32, &devreg->int_ident);
866         P54U_WRITE(NET2280_DEV_U32, &devreg->int_ack, reg);
867
868         if (!(reg & cpu_to_le32(ISL38XX_INT_IDENT_INIT)))
869                 err = -EINVAL;
870
871         P54U_WRITE(NET2280_BRG_U32, NET2280_USBIRQENB1, 0);
872         P54U_WRITE(NET2280_BRG_U32, NET2280_IRQSTAT1,
873                    cpu_to_le32(NET2280_PCI_INTA_INTERRUPT));
874
875 #undef P54U_WRITE
876 #undef P54U_READ
877
878  fail:
879         release_firmware(fw_entry);
880         kfree(buf);
881         return err;
882 }
883
884 static int p54u_open(struct ieee80211_hw *dev)
885 {
886         struct p54u_priv *priv = dev->priv;
887         int err;
888
889         err = p54u_init_urbs(dev);
890         if (err) {
891                 return err;
892         }
893
894         priv->common.open = p54u_init_urbs;
895
896         return 0;
897 }
898
899 static void p54u_stop(struct ieee80211_hw *dev)
900 {
901         /* TODO: figure out how to reliably stop the 3887 and net2280 so
902            the hardware is still usable next time we want to start it.
903            until then, we just stop listening to the hardware.. */
904         p54u_free_urbs(dev);
905         return;
906 }
907
908 static int __devinit p54u_probe(struct usb_interface *intf,
909                                 const struct usb_device_id *id)
910 {
911         struct usb_device *udev = interface_to_usbdev(intf);
912         struct ieee80211_hw *dev;
913         struct p54u_priv *priv;
914         int err;
915         unsigned int i, recognized_pipes;
916
917         dev = p54_init_common(sizeof(*priv));
918
919         if (!dev) {
920                 dev_err(&udev->dev, "(p54usb) ieee80211 alloc failed\n");
921                 return -ENOMEM;
922         }
923
924         priv = dev->priv;
925
926         SET_IEEE80211_DEV(dev, &intf->dev);
927         usb_set_intfdata(intf, dev);
928         priv->udev = udev;
929         priv->intf = intf;
930         skb_queue_head_init(&priv->rx_queue);
931         init_usb_anchor(&priv->submitted);
932
933         usb_get_dev(udev);
934
935         /* really lazy and simple way of figuring out if we're a 3887 */
936         /* TODO: should just stick the identification in the device table */
937         i = intf->altsetting->desc.bNumEndpoints;
938         recognized_pipes = 0;
939         while (i--) {
940                 switch (intf->altsetting->endpoint[i].desc.bEndpointAddress) {
941                 case P54U_PIPE_DATA:
942                 case P54U_PIPE_MGMT:
943                 case P54U_PIPE_BRG:
944                 case P54U_PIPE_DEV:
945                 case P54U_PIPE_DATA | USB_DIR_IN:
946                 case P54U_PIPE_MGMT | USB_DIR_IN:
947                 case P54U_PIPE_BRG | USB_DIR_IN:
948                 case P54U_PIPE_DEV | USB_DIR_IN:
949                 case P54U_PIPE_INT | USB_DIR_IN:
950                         recognized_pipes++;
951                 }
952         }
953         priv->common.open = p54u_open;
954         priv->common.stop = p54u_stop;
955         if (recognized_pipes < P54U_PIPE_NUMBER) {
956                 priv->hw_type = P54U_3887;
957                 err = p54u_upload_firmware_3887(dev);
958                 if (priv->common.fw_interface == FW_LM87) {
959                         dev->extra_tx_headroom += sizeof(struct lm87_tx_hdr);
960                         priv->common.tx_hdr_len = sizeof(struct lm87_tx_hdr);
961                         priv->common.tx = p54u_tx_lm87;
962                 } else
963                         priv->common.tx = p54u_tx_3887;
964         } else {
965                 priv->hw_type = P54U_NET2280;
966                 dev->extra_tx_headroom += sizeof(struct net2280_tx_hdr);
967                 priv->common.tx_hdr_len = sizeof(struct net2280_tx_hdr);
968                 priv->common.tx = p54u_tx_net2280;
969                 err = p54u_upload_firmware_net2280(dev);
970         }
971         if (err)
972                 goto err_free_dev;
973
974         p54u_open(dev);
975         err = p54_read_eeprom(dev);
976         p54u_stop(dev);
977         if (err)
978                 goto err_free_dev;
979
980         err = p54_register_common(dev, &udev->dev);
981         if (err)
982                 goto err_free_dev;
983
984         return 0;
985
986  err_free_dev:
987         ieee80211_free_hw(dev);
988         usb_set_intfdata(intf, NULL);
989         usb_put_dev(udev);
990         return err;
991 }
992
993 static void __devexit p54u_disconnect(struct usb_interface *intf)
994 {
995         struct ieee80211_hw *dev = usb_get_intfdata(intf);
996         struct p54u_priv *priv;
997
998         if (!dev)
999                 return;
1000
1001         ieee80211_unregister_hw(dev);
1002
1003         priv = dev->priv;
1004         usb_put_dev(interface_to_usbdev(intf));
1005         p54_free_common(dev);
1006         ieee80211_free_hw(dev);
1007 }
1008
1009 static int p54u_pre_reset(struct usb_interface *intf)
1010 {
1011         return 0;
1012 }
1013
1014 static int p54u_post_reset(struct usb_interface *intf)
1015 {
1016         return 0;
1017 }
1018
1019 static struct usb_driver p54u_driver = {
1020         .name   = "p54usb",
1021         .id_table = p54u_table,
1022         .probe = p54u_probe,
1023         .disconnect = p54u_disconnect,
1024         .pre_reset = p54u_pre_reset,
1025         .post_reset = p54u_post_reset,
1026         .soft_unbind = 1,
1027 };
1028
1029 static int __init p54u_init(void)
1030 {
1031         return usb_register(&p54u_driver);
1032 }
1033
1034 static void __exit p54u_exit(void)
1035 {
1036         usb_deregister(&p54u_driver);
1037 }
1038
1039 module_init(p54u_init);
1040 module_exit(p54u_exit);