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