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