Initial import from the website's tarball, on the 18th of october 2004
[islsm.git] / usb_init.c
blob76ab8dc9214b01f84fb2dfbea5d9d94d5c28ab58
1 /*
2 * Prism54 USB driver
3 */
5 #include <linux/config.h>
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/smp_lock.h>
12 #include <linux/completion.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/usb.h>
16 #include <linux/pci.h>
17 #include <linux/firmware.h>
18 #include <asm/uaccess.h>
19 #include "prism54_usb.h"
20 #include "isl_38xx.h"
21 #include "sent_data_ok.h"
22 #include "modes.h"
23 #include "channels.h"
25 #ifdef CONFIG_USB_DEBUG
26 int debug = 2;
27 #else
28 int debug;
29 #endif
31 int load_fw;
32 static const char driver_name[] = "prism54_usb";
34 static const struct usb_device_id p54u_table[] = {
35 {USB_DEVICE(0x5041, 0x2234)}, /* Linksys WUSB54G */
36 {USB_DEVICE(0x5041, 0x2235)}, /* Linksys WUSB54G Portable */
37 {USB_DEVICE(0x1915, 0x2234)}, /* Linksys WUSB54G OEM */
38 {USB_DEVICE(0x1915, 0x2235)}, /* Linksys WUSB54G Portable OEM */
39 {USB_DEVICE(0x0506, 0x0a11)}, /* 3COM 3CRWE254G72 */
40 {USB_DEVICE(0x2001, 0x3701)}, /* DLink G120 */
41 {USB_DEVICE(0x0cde, 0x0006)}, /* Medion 40900 */
42 {USB_DEVICE(0x0846, 0x4200)}, /* Netgear WG121 */
43 {USB_DEVICE(0x0846, 0x4210)}, /* Netgear WG121 the second ? */
44 {USB_DEVICE(0x0846, 0x4220)}, /* Netgear WG111 */
45 {USB_DEVICE(0x0707, 0xee06)}, /* SMC 2862W-G */
46 {USB_DEVICE(0x124a, 0x4023)}, /* Shuttle PN15 (Airvast WM168g) */
50 static const struct p54u_pipe_desc p54u_pipes[] = {
51 { P54U_PIPE_DATA, USB_ENDPOINT_XFER_BULK },
52 { P54U_PIPE_MGMT, USB_ENDPOINT_XFER_BULK },
53 { P54U_PIPE_BRG, USB_ENDPOINT_XFER_BULK },
54 { P54U_PIPE_DEV, USB_ENDPOINT_XFER_BULK },
55 { P54U_PIPE_DATA | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
56 { P54U_PIPE_MGMT | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
57 { P54U_PIPE_3 | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
58 { P54U_PIPE_4 | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
59 { P54U_PIPE_BRG | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
60 { P54U_PIPE_DEV | USB_DIR_IN, USB_ENDPOINT_XFER_BULK },
61 { P54U_PIPE_INT | USB_DIR_IN, USB_ENDPOINT_XFER_INT },
65 MODULE_DESCRIPTION("Prism54 USB Driver");
66 MODULE_AUTHOR("Feyd <feyd@seznam.cz>");
67 MODULE_LICENSE("GPL");
68 MODULE_DEVICE_TABLE(usb, p54u_table);
69 MODULE_PARM(debug, "i");
70 MODULE_PARM(load_fw, "i");
71 MODULE_PARM_DESC(debug, "Debug enabled or not");
72 MODULE_PARM_DESC(load_fw, "Load firmware on probe (1) or open (0)");
74 static int p54u_reset_usb(struct net_device *netdev)
76 struct p54u *p54u = netdev_priv(netdev);
77 struct usb_device *usbdev = p54u->usbdev;
78 int err = 0;
79 u32 reg;
81 // return 0;
83 p54u_info("%s: Reset USB device.\n", netdev->name);
84 err = usb_reset_device(usbdev);
85 if(err) {
86 p54u_err("%s: Reset USB device failed.\n", netdev->name);
87 return err;
89 // p54u_mdelay(200);
90 p54u_info("%s: Reset USB device done.\n", netdev->name);
92 return 0;
94 do_err:
95 p54u_err("%s: Reset USB device failed.\n", netdev->name);
96 return err;
99 static void p54u_free_pipe(struct net_device *netdev, struct p54u_pipe *pipe)
101 struct p54u *p54u = netdev_priv(netdev);
102 struct usb_device *usbdev = p54u->usbdev;
103 int i;
105 p54u_info("Freeing pipe.\n");
107 /* first unlink all submitted urbs
108 We have to do this before freeing
109 resources that are accessed by the
110 completion callbacks, such as completion
111 variables */
113 if(atomic_read(&pipe->busy)) {
114 p54u_info("pipe->busy is set to %i",atomic_read(&pipe->busy));
115 // this will do for the current situation
116 usb_unlink_urb(pipe->urb[0]);
119 if(!pipe->buf) {
120 p54u_info("pipe->buf == NULL\n");
121 goto do_free_urb;
124 if(!pipe->urb) {
125 p54u_info("pipe->urb == NULL\n");
126 goto do_free_size;
129 if(!pipe->size) {
130 p54u_info("pipe->size == NULL\n");
131 goto do_clear;
134 for(i = 0; i < pipe->len; i++) {
135 if(pipe->urb[i]) {
136 if(pipe->buf[i]) {
137 usb_buffer_free(usbdev, pipe->size[i], pipe->buf[i], pipe->urb[i]->transfer_dma);
138 } else {
139 p54u_info("pipe->buf[%i] == NULL\n", i);
142 usb_free_urb(pipe->urb[i]);
143 } else {
144 p54u_info("pipe->urb[%i] == NULL\n", i);
148 kfree(pipe->buf);
150 do_free_urb:
151 if(pipe->urb) {
152 kfree(pipe->urb);
153 } else {
154 p54u_info("pipe->urb == NULL\n");
157 do_free_size:
158 if(pipe->size) {
159 kfree(pipe->size);
160 } else {
161 p54u_info("pipe->size == NULL\n");
164 do_clear:
165 p54u_info("Clear the memory.\n");
167 memset(pipe, 0, sizeof(*pipe));
169 p54u_info("Freeing pipe done.\n");
171 return;
174 static void p54u_free_buffers(struct net_device *netdev)
176 struct p54u *p54u = netdev_priv(netdev);
177 struct usb_device *usbdev = p54u->usbdev;
179 p54u_free_pipe(netdev, &p54u->data_tx);
180 p54u_free_pipe(netdev, &p54u->data_rx);
181 p54u_free_pipe(netdev, &p54u->mgmt_tx);
182 p54u_free_pipe(netdev, &p54u->mgmt_rx);
183 p54u_free_pipe(netdev, &p54u->int_rx);
185 return;
188 static int p54u_reset_pipe(struct usb_device *usbdev, int addr)
190 int err = 0;
192 p54u_dbg("resetting pipe %02x", addr);
193 switch(addr) {
194 case P54U_PIPE_DATA:
195 case P54U_PIPE_MGMT:
196 case P54U_PIPE_BRG :
197 err = usb_clear_halt(usbdev, usb_sndbulkpipe(usbdev,addr) );
198 break;
199 case P54U_PIPE_BRG | USB_DIR_IN :
200 case P54U_PIPE_DATA | USB_DIR_IN:
201 case P54U_PIPE_MGMT | USB_DIR_IN:
202 err = usb_clear_halt(usbdev, usb_rcvbulkpipe(usbdev,addr) );
203 break;
204 case P54U_PIPE_INT | USB_DIR_IN:
205 err = usb_clear_halt(usbdev, usb_rcvintpipe(usbdev,addr) );
206 break;
207 default:
208 break;
210 return err;
213 static int p54u_reset_pipes(struct net_device *netdev)
215 struct p54u *p54u = netdev_priv(netdev);
216 struct usb_device *usbdev = p54u->usbdev;
218 (void) p54u_reset_pipe(usbdev, P54U_PIPE_DEV | USB_DIR_IN );
219 (void) p54u_reset_pipe(usbdev, P54U_PIPE_DEV);
220 (void) p54u_reset_pipe(usbdev, P54U_PIPE_BRG | USB_DIR_IN );
221 (void) p54u_reset_pipe(usbdev, P54U_PIPE_BRG);
223 (void) p54u_reset_pipe(usbdev, P54U_PIPE_INT | USB_DIR_IN );
225 (void) p54u_reset_pipe(usbdev, P54U_PIPE_DATA | USB_DIR_IN );
226 (void) p54u_reset_pipe(usbdev, P54U_PIPE_DATA);
227 (void) p54u_reset_pipe(usbdev, P54U_PIPE_MGMT | USB_DIR_IN );
228 (void) p54u_reset_pipe(usbdev, P54U_PIPE_MGMT);
230 return 0;
233 static int p54u_alloc_pipe(struct net_device *netdev, struct usb_endpoint_descriptor *desc, struct p54u_pipe *pipe, usb_complete_t callback)
235 struct p54u *p54u = netdev_priv(netdev);
236 struct usb_device *usbdev = p54u->usbdev;
237 int err, i, p, e;
239 if(!pipe)
240 return 0;
242 p54u_info("%s: Allocate pipe %02x, max pkt size is %02x\n", netdev->name, desc->bEndpointAddress, desc->wMaxPacketSize);
244 pipe->pkt_size = desc->wMaxPacketSize;
245 pipe->type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
246 pipe->addr = desc->bEndpointAddress;
247 pipe->len = P54U_QUEUE_LEN;
249 pipe->size = kmalloc(sizeof(*pipe->size) * pipe->len, GFP_KERNEL);
250 if(!pipe->size) {
251 p54u_info("Failed to allocate pipe->size\n");
252 goto do_enomem;
253 } else {
254 p54u_info("pipe->size == %p\n", pipe->size);
256 memset(pipe->size, 0, sizeof(*pipe->size) * pipe->len);
259 pipe->urb = kmalloc(sizeof(*pipe->urb) * pipe->len, GFP_KERNEL);
260 if(!pipe->urb) {
261 p54u_info("Failed to allocate pipe->urb\n");
262 goto do_enomem;
263 } else {
264 p54u_info("pipe->urb == %p\n", pipe->urb);
266 memset(pipe->urb, 0, sizeof(*pipe->urb) * pipe->len);
268 pipe->buf = kmalloc(sizeof(*pipe->buf) * pipe->len, GFP_KERNEL);
269 if(!pipe->buf) {
270 p54u_info("Failed to allocate pipe->buf\n");
271 goto do_enomem;
272 } else {
273 p54u_info("pipe->buf == %p\n", pipe->buf);
275 memset(pipe->buf, 0, sizeof(*pipe->buf) * pipe->len);
277 init_completion(&pipe->comp);
278 atomic_set(&pipe->busy, 0);
280 p54u_info("%s: Allocate buffers.\n", netdev->name);
282 for(i = 0; i < pipe->len; i++) {
283 pipe->size[i] = P54U_MAX_FRAME_SIZE;
284 if(pipe->type == USB_ENDPOINT_XFER_INT) {
285 pipe->size[i] = 4;
288 p54u_info("pipe->size [%i] = %i\n", i, pipe->size[i]);
290 pipe->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
291 if(!pipe->urb[i]) {
292 p54u_info("Failed to allocate pipe->urb[%i]\n", i);
293 goto do_enomem;
294 } else {
295 p54u_info("pipe->urb[%i] == %p\n", i, pipe->urb[i]);
297 pipe->urb[i]->transfer_flags = (URB_NO_TRANSFER_DMA_MAP | URB_ASYNC_UNLINK);
299 pipe->buf[i] = usb_buffer_alloc(usbdev, pipe->size[i], GFP_KERNEL, &pipe->urb[i]->transfer_dma);
300 if(!pipe->buf[i]) {
301 p54u_info("Failed to allocate pipe->buf[%i]\n", i);
302 goto do_enomem;
303 } else {
304 p54u_info("pipe->buf[%i] == %p\n", i, pipe->buf[i]);
307 e = pipe->addr & USB_ENDPOINT_NUMBER_MASK;
308 p54u_info("pipe->addr = %i\n", e);
309 // e = pipe->addr;
310 switch(pipe->type) {
311 case USB_ENDPOINT_XFER_BULK:
312 p = pipe->addr & USB_DIR_IN ? usb_rcvbulkpipe(usbdev, e) : usb_sndbulkpipe(usbdev, e);
313 usb_fill_bulk_urb(pipe->urb[i], usbdev, p, pipe->buf[i], pipe->size[i], callback, netdev);
314 case USB_ENDPOINT_XFER_INT:
315 p = usb_rcvintpipe(usbdev, e);
316 usb_fill_int_urb(pipe->urb[i], usbdev, p, pipe->buf[i], pipe->size[i], callback, netdev, 6);
317 // usb_fill_int_urb(pipe->urb[i], usbdev, p, pipe->buf[i], pipe->size[i], callback, netdev, HZ / 4);
318 // p = pipe->addr & USB_DIR_IN ? usb_rcvbulkpipe(usbdev, e) : usb_sndbulkpipe(usbdev, e);
319 // usb_fill_bulk_urb(pipe->urb[i], usbdev, p, pipe->buf[i], pipe->size[i], callback, netdev);
323 p54u_info("%s: Allocate pipe done.\n", netdev->name);
324 return 0;
326 do_enomem:
327 p54u_info("%s: Not enough memory.\n", netdev->name);
328 err = -ENOMEM;
329 do_err:
330 p54u_info("%s: Allocate pipe failed.\n", netdev->name);
331 return err;
334 static int p54u_alloc_buffers(struct net_device *netdev)
336 struct p54u *p54u = netdev_priv(netdev);
337 struct usb_device *usbdev = p54u->usbdev;
338 struct usb_interface *interface = p54u->interface;
339 struct usb_host_interface *iface_desc = &interface->altsetting[0];
340 struct usb_endpoint_descriptor *desc;
341 struct p54u_pipe *pipe;
342 int err = 0, i;
344 p54u_info("%s: Setup USB structures.\n", netdev->name);
346 for(i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
347 desc = &iface_desc->endpoint[i].desc;
348 switch(desc->bEndpointAddress) {
349 case P54U_PIPE_DATA:
350 err = p54u_alloc_pipe(netdev, desc, &p54u->data_tx, NULL);
351 break;
352 case P54U_PIPE_MGMT:
353 err = p54u_alloc_pipe(netdev, desc, &p54u->mgmt_tx, NULL);
354 break;
355 case P54U_PIPE_DATA | USB_DIR_IN:
356 err = p54u_alloc_pipe(netdev, desc, &p54u->data_rx, p54u_data_rx_cb);
357 break;
358 case P54U_PIPE_MGMT | USB_DIR_IN:
359 err = p54u_alloc_pipe(netdev, desc, &p54u->mgmt_rx, p54u_mgmt_rx_cb);
360 break;
361 case P54U_PIPE_INT | USB_DIR_IN:
362 err = p54u_alloc_pipe(netdev, desc , &p54u->int_rx, p54u_int_rx_cb);
363 break;
364 default:
365 break;
367 if(err)
368 goto do_err;
370 p54u_info("%s: Setup USB structures successful.\n", netdev->name);
372 return 0;
374 do_err:
375 p54u_info("%s: Setup USB structures failed: %i\n", netdev->name, err);
376 p54u_free_buffers(netdev);
378 return err;
381 static int p54u_reset_dev(struct net_device *netdev)
383 struct p54u *p54u = netdev_priv(netdev);
384 struct usb_device *usbdev = p54u->usbdev;
385 u32 reg, revision;
386 int err;
388 p54u_info("%s: Reset device.\n", netdev->name);
390 /* Bridge */
391 /* Reset the usb<->pci bridge */
392 p54u_dbg("%s: Reset bridge\n", netdev->name);
393 reg = p54u_brg_readl(netdev, NET2280_GPIOCTL);
394 //reg = 0x3e;
395 //reg |= 0x3e;
396 p54u_brg_writel(netdev, NET2280_GPIOCTL, (reg | P54U_BRG_POWER_DOWN) & ~P54U_BRG_POWER_UP);
397 if(p54u->err)
398 goto do_err;
399 p54u_mdelay(100);
400 p54u_brg_writel(netdev, NET2280_GPIOCTL, (reg | P54U_BRG_POWER_UP) & ~P54U_BRG_POWER_DOWN);
401 if(p54u->err)
402 goto do_err;
403 p54u_dbg("%s: Reset bridge done\n", netdev->name);
404 p54u_mdelay(100);
406 /* See 11.5.1 on net2280 doc */
407 p54u_dbg("%s: Magic 1\n", netdev->name);
408 p54u_brg_writel(netdev, NET2280_DEVINIT, NET2280_CLK_30Mhz | NET2280_PCI_ENABLE | NET2280_PCI_SOFT_RESET);
409 if(p54u->err)
410 goto do_err;
411 p54u_dbg("%s: Magic 1 done\n", netdev->name);
412 p54u_mdelay(20);
414 /* Enable mmio and busmaster on the bridge */
415 p54u_dbg("%s: Setup bridge pci resources\n", netdev->name);
416 p54u_pcicfg_brg_writew(netdev, PCI_COMMAND, (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
418 /* Set base address 0 */
419 p54u_pcicfg_brg_writel(netdev, PCI_BASE_ADDRESS_0, NET2280_BASE);
421 /* Set PCI_STATUS_REC_MASTER_ABORT (why?) */
422 reg = p54u_pcicfg_brg_readw(netdev, PCI_STATUS);
423 p54u_pcicfg_brg_writew(netdev, PCI_STATUS, (reg | PCI_STATUS_REC_MASTER_ABORT));
425 /* Read revision? */
426 revision = p54u_brg_readl(netdev, NET2280_RELNUM);
427 p54u_dbg("%s: Setup bridge pci resources done\n", netdev->name);
429 /* Magic */
430 p54u_dbg("%s: Magic 2\n", netdev->name);
431 p54u_brg_writel(netdev, NET2280_EPA_RSP, NET2280_CLEAR_NAK_OUT_PACKETS_MODE);
432 p54u_brg_writel(netdev, NET2280_EPC_RSP, NET2280_CLEAR_NAK_OUT_PACKETS_MODE);
433 p54u_dbg("%s: Magic 2 done\n", netdev->name);
435 /* Set base address 2 for bridge : where the endpoint fifos are */
436 p54u_dbg("%s: Setup bridge base addr 2\n", netdev->name);
437 p54u_pcicfg_brg_writel(netdev, PCI_BASE_ADDRESS_2, NET2280_BASE2);
438 p54u_dbg("%s: Setup bridge base addr 2 done\n", netdev->name);
440 /* Device */
441 /* Enable mmio and busmaster on the device */
442 p54u_dbg("%s: Setup device pci resources\n", netdev->name);
443 p54u_pcicfg_dev_writew(netdev, PCI_COMMAND | 0x10000, (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
444 // p54u_pcicfg_dev_writew(netdev, PCI_COMMAND, (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
446 /* Set TRDY_TIMEOUT and RETRY_TIMEOUT to 0 */
447 p54u_pcicfg_dev_writew(netdev, P54U_TRDY_TIMEOUT | 0x10000, 0);
448 // p54u_pcicfg_dev_writew(netdev, P54U_TRDY_TIMEOUT, 0);
450 /* Set base address 0 */
451 p54u_pcicfg_dev_writel(netdev, PCI_BASE_ADDRESS_0 | 0x10000, P54U_DEV_BASE);
452 // p54u_pcicfg_dev_writel(netdev, PCI_BASE_ADDRESS_0, P54U_DEV_BASE);
453 p54u_dbg("%s: Setup device pci resources done\n", netdev->name);
455 /* See 7.6.5.5 and chapter 9 of net2280 doc */
456 p54u_dbg("%s: Magic 3\n", netdev->name);
457 p54u_brg_writel(netdev, NET2280_USBIRQENB1, 0);
458 // p54u_brg_writel(netdev, NET2280_USBIRQENB1, NET2280_PCI_INTA_INTERRUPT_ENABLE | NET2280_USB_INTERRUPT_ENABLE);
459 p54u_brg_writel(netdev, NET2280_IRQSTAT1, NET2280_PCI_INTA_INTERRUPT);
460 p54u_dbg("%s: Magic 3 done\n", netdev->name);
462 /* Assert wakeup */
463 p54u_dbg("%s: Assert device\n", netdev->name);
464 p54u_dev_writel(netdev, ISL38XX_DEV_INT_REG, ISL38XX_DEV_INT_WAKEUP);
465 if(p54u->err)
466 goto do_err;
467 p54u_mdelay(20);
469 /* (see old driver for INT_ABORT definition) */
470 p54u_dev_writel(netdev, ISL38XX_DEV_INT_REG, ISL38XX_DEV_INT_ABORT);
471 if(p54u->err)
472 goto do_err;
473 p54u_dbg("%s: Assert device done\n", netdev->name);
474 p54u_mdelay(20);
476 /* Reset the device */
477 p54u_dbg("%s: Reset device\n", netdev->name);
479 /* clear the RAMBoot, the CLKRUN and the Reset bit */
480 reg = p54u_dev_readl(netdev, ISL38XX_CTRL_STAT_REG);
481 // reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT);
482 reg &= ~(ISL38XX_CTRL_STAT_RESET | ISL38XX_CTRL_STAT_RAMBOOT | ISL38XX_CTRL_STAT_CLKRUN);
483 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg);
484 p54u_mdelay(20); /* WRITEIO_DELAY */
486 /* set the Reset bit without reading the register */
487 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg | ISL38XX_CTRL_STAT_RESET);
488 p54u_mdelay(20); /* WRITEIO_DELAY */
489 /* clear the Reset bit */
490 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg);
492 if(p54u->err)
493 goto do_err;
494 p54u_dbg("%s: Reset device done\n", netdev->name);
495 /* wait for the device to reboot */
496 p54u_mdelay(100);
497 p54u_dbg("%s: Disable irq\n", netdev->name);
498 p54u_dev_writel(netdev, ISL38XX_INT_EN_REG, 0);
499 // p54u_dev_writel(netdev, ISL38XX_INT_EN_REG, 0x00004004);
500 p54u_dbg("%s: Disable irq done\n", netdev->name);
502 // p54u_mdelay(50);
504 /* Ack the irqs */
505 p54u_dbg("%s: Ack irq\n", netdev->name);
506 reg = p54u_dev_readl(netdev, ISL38XX_INT_IDENT_REG);
507 p54u_dev_writel(netdev, ISL38XX_INT_ACK_REG, reg);
508 if(p54u->err)
509 goto do_err;
510 p54u_dbg("%s: Ack done\n", netdev->name);
512 /* Magic */
513 p54u_dbg("%s: Magic 4\n", netdev->name);
514 p54u_brg_writel(netdev, NET2280_EPA_STAT, NET2280_FIFO_FLUSH);
515 p54u_brg_writel(netdev, NET2280_EPB_STAT, NET2280_FIFO_FLUSH);
516 p54u_brg_writel(netdev, NET2280_EPC_STAT, NET2280_FIFO_FLUSH);
517 p54u_brg_writel(netdev, NET2280_EPD_STAT, NET2280_FIFO_FLUSH);
518 /* why this again ? */
519 p54u_brg_writel(netdev, NET2280_EPA_STAT, NET2280_FIFO_FLUSH);
520 p54u_dbg("%s: Magic 4 done\n", netdev->name);
522 p54u_info("%s: Reset device done.\n", netdev->name);
524 return 0;
526 do_err:
527 p54u_err("%s: Reset error: %i\n", netdev->name, p54u->err);
528 err = p54u->err;
529 p54u->err = 0;
530 return err;
533 static int p54u_load_firmware(struct net_device *netdev)
535 struct p54u *p54u = netdev_priv(netdev);
536 struct usb_device *usbdev = p54u->usbdev;
537 unsigned int pipe;
538 u32 reg;
539 void *data;
540 void *kmptr;
541 size_t len,retlen;
542 int err, i, j, k, l;
543 u64 t, t1, t2;
545 char *rcv;
547 p54u_info("%s: Load firmware.\n", netdev->name);
549 /* Firmware */
551 pipe = usb_sndbulkpipe(usbdev, P54U_PIPE_DATA);
552 data = p54u->fw_entry->data;
553 i = p54u->fw_entry->size;
554 j = 0;
555 kmptr = kmalloc(P54U_FW_BLOCK, GFP_KERNEL);
556 if ( !kmptr ) {
557 p54u->err = -ENOMEM;
558 goto do_err;
561 while(i > 0) {
562 len = i > P54U_FW_BLOCK ? P54U_FW_BLOCK : i;
563 memcpy(kmptr, data, len);
564 t1 = p54u_time();
565 p54u->err = usb_bulk_msg(usbdev, pipe, kmptr, len, &retlen, 10*HZ);
566 t2 = p54u_time();
567 t = t2 - t1;
569 p54u_dbg("Actually written %i of %i", retlen, len);
570 p54u_data_debug(netdev, P54U_PIPE_DATA, data, len);
572 if(p54u->err) {
573 p54u_dbg("%s: Error writing firmware block: writen %i of %i.\n", netdev->name, j, p54u->fw_entry->size);
574 goto do_release;
577 data += P54U_FW_BLOCK;
578 i -= P54U_FW_BLOCK;
580 /* Magic */
581 p54u_dev_writel(netdev, ISL38XX_DIR_MEM_BASE_REG, 0xc0000f00);
582 p54u_dev_writel(netdev, 0x1020, 0);
583 p54u_dev_writel(netdev, 0x1020, 1);
584 p54u_dev_writel(netdev, 0x1024, len);
585 p54u_dev_writel(netdev, 0x1028, j | 0x00020000);
586 p54u_dev_writel(netdev, 0x0060, 0x20000000);
587 p54u_dev_writel(netdev, 0x0064, len/4);
588 p54u_dev_writel(netdev, 0x0068, 4);
589 reg = p54u_dev_readl(netdev, 0x102c);
590 if(i > 0)
591 p54u_brg_writel(netdev, NET2280_EPA_STAT, NET2280_FIFO_FLUSH);
592 if(p54u->err) {
593 p54u_dbg("%s: Error updating registers: writen %i of %i.\n", netdev->name, j, p54u->fw_entry->size);
594 goto do_release;
596 j += P54U_FW_BLOCK;
600 reg = p54u_dev_readl(netdev, ISL38XX_CTRL_STAT_REG);
601 // reg |= ISL38XX_CTRL_STAT_RAMBOOT | ISL38XX_CTRL_STAT_CLKRUN;
602 reg |= ISL38XX_CTRL_STAT_RAMBOOT;
603 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg);
604 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg | ISL38XX_CTRL_STAT_RESET);
605 p54u_dev_writel(netdev, ISL38XX_CTRL_STAT_REG, reg);
607 if(p54u->err) {
608 p54u_dbg("%s: Error latching reset: %i\n", netdev->name, p54u->err);
611 if ( kmptr ) {
612 kfree(kmptr);
614 p54u_info("%s: Load firmware done.\n", netdev->name);
615 return 0;
617 do_release:
618 do_err:
619 if ( kmptr ) {
620 kfree(kmptr);
622 p54u_err("%s: Load error: %i\n", netdev->name, p54u->err);
623 err = p54u->err;
624 p54u->err = 0;
625 return err;
628 int p54u_setup_dev(struct net_device *netdev)
630 struct p54u *p54u = netdev_priv(netdev);
631 struct usb_device *usbdev = p54u->usbdev;
632 u32 reg, rst = 0;
633 int i, j, k, pending, err;
635 p54u->err = usb_submit_urb(p54u->int_rx.urb[0], GFP_KERNEL);
636 if(p54u->err) {
637 p54u_dbg("%s: Error submit int 0: %i\n", netdev->name, p54u->err);
638 } else {
639 p54u_dbg("%s: Submit int 0 ok.\n", netdev->name);
642 p54u->err = p54u_data_rx_submit(netdev);
643 p54u->err = p54u_mgmt_rx_submit(netdev);
646 //p54u_dev_writel(netdev, ISL38XX_INT_EN_REG, 0x80004004);
647 p54u_dev_writel(netdev, ISL38XX_INT_EN_REG, 0x00004004);
648 p54u_brg_writel(netdev, NET2280_IRQSTAT1, NET2280_PCI_INTA_INTERRUPT);
649 p54u_brg_writel(netdev, NET2280_USBIRQENB1, NET2280_PCI_INTA_INTERRUPT_ENABLE | NET2280_USB_INTERRUPT_ENABLE);
650 p54u_dev_writel(netdev, ISL38XX_DEV_INT_REG, ISL38XX_DEV_INT_RESET);
652 p54u_wait_int(netdev);
653 return p54u->err;
656 int p54u_mgmt_readback(struct net_device *netdev)
658 struct p54u *p54u = netdev_priv(netdev);
659 struct usb_device *usbdev = p54u->usbdev;
660 struct p54u_mgmt_tx1 *tx1;
661 struct p54u_mgmt_tx2 *tx2;
663 int i, j, len, err;
665 memset(p54u->mgmt_tx.buf[0], 0, p54u->mgmt_tx.size[0]);
666 memset(p54u->mgmt_tx.buf[1], 0, p54u->mgmt_tx.size[1]);
668 tx1 = p54u->mgmt_tx.buf[0];
669 tx2 = p54u->mgmt_tx.buf[1];
671 tx1->magic1 = cpu_to_le32(0x02066c);
672 tx1->magic2 = cpu_to_le16(0x2);
673 tx1->padding1 = 0;
674 tx1->padding2 = 0;
676 tx2->magic1 = cpu_to_le16(0x8000);
677 /* The low bits of magic2 varies, i dont really know where the
678 values comes from */
679 // tx2->magic3 = cpu_to_le32(0xc);
680 tx2->magic3 = cpu_to_le32(0xc);
682 i = P54U_MGMT_MEM_SIZE;
683 j = 0;
685 /* Each iteration will trigger a 0x82 pipe response */
686 while (i > 0) {
687 /* those alternate between addresses of buffers... used by the windows driver, only. with ndiswrapper, this alternates between three values.
688 We can put here a value that will not be overwritten... and given bakc in the response packet */
689 tx2->magic2 = cpu_to_le32(0);
690 j++;
692 len = i > P54U_INIT_BLOCK ? P54U_INIT_BLOCK : i;
694 /* announces to the device what the full length
695 of the next mgmt frame will be */
696 tx1->len = cpu_to_le16(len + sizeof(*tx2) - sizeof(void*));
697 p54u_bulk_msg(netdev, P54U_PIPE_MGMT, tx1, 16);
699 /* The +4 is for the length + pos field in p54u_mgmt_tx struct */
700 tx2->len = cpu_to_le16(len + 4);
701 tx2->sub_len = cpu_to_le16(len);
702 tx2->sub_pos = cpu_to_le16(P54U_MGMT_MEM_SIZE - i);
704 /* The following two writes can be done concurrently */
705 p54u_mgmt_msg(netdev, tx2, len + sizeof(*tx2) -sizeof(void*));
707 p54u_wait_mgmt_response(netdev);
709 if (p54u->err)
710 break;
712 if (j==1) {
713 /* maybe check size ? Hum ? */
714 struct p54u_rx * rxd = p54u->mgmt_rx.urb[0]->transfer_buffer;
715 struct p54u_mgmt_tx2 *txd = (struct p54u_mgmt_tx2 *) &rxd->data;
716 struct p54u_mgmt_rx_header *mgmt_h = (struct p54u_mgmt_rx_header *) &txd->data;
717 struct p54u_mgmt_rx_frame *mgmt;
718 int frame_offset;
720 p54u_info("Storing mgm frame");
722 frame_offset = le16_to_cpu(mgmt_h->preamble_length);
723 mgmt = (struct p54u_mgmt_rx_frame *) &(((u8 *)&(mgmt_h->data))[frame_offset]);
724 memcpy(&p54u->mgmt_frame, (char*) mgmt, sizeof(p54u->mgmt_frame));
725 memcpy(netdev->dev_addr, &mgmt->mac_addr, ETH_ALEN);
728 p54u_mgmt_rx_submit(netdev);
730 i -= P54U_INIT_BLOCK;
733 return p54u->err;
736 int p54u_readback_conf(struct net_device *netdev)
738 struct p54u *p54u = netdev_priv(netdev);
739 int a;
741 a = p54u_brg_readl(netdev,NET2280_FIFOCTL);
742 p54u_info("FIFOCTL : %08x",a);
744 a = p54u_brg_readl(netdev,NET2280_EPA_CFG);
745 p54u_info("EPA : %08x",a);
747 a = p54u_brg_readl(netdev,NET2280_EPB_CFG);
748 p54u_info("EPB : %08x",a);
750 a = p54u_brg_readl(netdev,NET2280_EPC_CFG);
751 p54u_info("EPC : %08x",a);
753 a = p54u_brg_readl(netdev,NET2280_EPD_CFG);
754 p54u_info("EPD : %08x",a);
756 return p54u->err;
759 inline void make_rx_filter_packet(struct net_device *netdev, char* data)
761 struct p54u_tx *tx = (struct p54u_tx *) data;
762 struct p54u_tx_control *txc = (struct p54u_tx_control *) &tx->data;
763 struct p54u_tx_control_filter *txcf = (struct p54u_tx_control_filter *) &txc->data;
764 unsigned int i ;
766 memcpy(data,p54u_data_filter,FULL_TX_CONTROL_FILTER);
767 printk("origin %p, int0 %p, int1 %p, int2 %p, destination %p", data, tx, txc, txcf, &txcf->destination[0]);
769 /* fill with ff:ff:ff:ff:ff:ff address */
770 for( i = 0 ; i < ETH_ALEN ; i++)
771 txcf->source[i] = 0xff;
772 /* my mac address */
773 memcpy(&txcf->destination[0],netdev->dev_addr,ETH_ALEN);
775 return;
778 static int p54u_set_filter(struct net_device *netdev)
780 struct p54u *p54u = netdev_priv(netdev);
781 char* filter_packet;
783 filter_packet = kmalloc(FULL_TX_CONTROL_FILTER, GFP_KERNEL);
784 if (!filter_packet)
785 return -ENOMEM;
787 make_rx_filter_packet(netdev, filter_packet);
789 p54u_control_msg(netdev, p54u_mode_send, sizeof(p54u_mode_send));
790 p54u_control_msg(netdev, filter_packet, FULL_TX_CONTROL_FILTER);
791 p54u_control_msg(netdev, p54u_mode_monitor, sizeof(p54u_mode_monitor));
793 return p54u->err;
796 int p54u_data_send(struct net_device *netdev)
798 struct p54u *p54u = netdev_priv(netdev);
799 struct usb_device *usbdev = p54u->usbdev;
800 char *channel_packet = 0;
801 int i;
803 channel_packet=kmalloc(FULL_TX_CONTROL_CHANNEL,GFP_KERNEL);
804 if (!channel_packet)
805 return -ENOMEM;
807 /* submit initial data on data pipe */
808 p54u_control_msg(netdev, p54u_data_packet1, sizeof(p54u_data_packet1));
809 /* p54u_control_msg(netdev, p54u_mode_send, sizeof(p54u_mode_send)); */
810 /* //p54u_control_msg(netdev, p54u_data_pkt_crc, sizeof(p54u_data_pkt_crc)); */
811 /* p54u_control_msg(netdev, p54u_data_filter, sizeof(p54u_data_filter)); */
812 /* p54u_control_msg(netdev, p54u_mode_monitor, sizeof(p54u_mode_monitor)); */
813 p54u_set_filter(netdev);
815 /* cycle through the channels */
816 /* for ( i = 1 ; i <= P54U_NR_CHANNELS ; i++) */
817 /* { */
818 /* make_tx_control_channel(p54u, channel_packet, i); */
819 /* p54u_control_msg(netdev, p54u_freqs[i-1], FULL_TX_CONTROL_CHANNEL); */
820 /* p54u_wait_data(netdev); */
821 /* } */
823 return p54u->err;
826 int p54u_boot(struct net_device *netdev)
828 struct p54u *p54u = netdev_priv(netdev);
829 int err;
831 p54u->state = P54U_BOOT;
833 p54u_info("%s: Boot the device.\n", netdev->name);
835 p54u_dbg("%s: Request firmware.\n", netdev->name);
837 err = request_firmware(&p54u->fw_entry, P54U_IMAGE_FILE, netdev->class_dev.dev);
838 if(err) {
839 p54u_err("%s: Request firmware failed: %i\n", netdev->name, err);
840 return err;
843 p54u_dbg("%s: Request firmware done: len = %i.\n", netdev->name, p54u->fw_entry->size);
845 p54u->time = get_jiffies_64();
847 INIT_WORK(&p54u->int_ack, p54u_int_ack, netdev);
848 INIT_WORK(&p54u->data_bh, p54u_data_rx, netdev);
850 /* will be overwritten in mgmt_readback */
851 memcpy(netdev->dev_addr, dummy_mac, ETH_ALEN);
853 err = p54u_reset_usb(netdev);
855 if(!err)
856 err = p54u_alloc_buffers(netdev);
857 if(!err)
858 err = p54u_reset_dev(netdev);
859 if(!err)
860 err = p54u_load_firmware(netdev);
861 if(!err)
862 err = p54u_setup_dev(netdev);
863 if(!err)
864 err = p54u_mgmt_readback(netdev);
865 //if(!err)
866 // err = p54u_readback_conf(netdev);
867 /* dont do this yet, first generate properly channel freq change paquets */
869 if(!err)
870 err = p54u_data_send(netdev);
872 // if(!err)
873 // err = p54u_reset_pipes(netdev);
875 /* err = p54u_reset_usb(netdev); */
877 // if(!err)
878 // err = p54u_reset_dev(netdev);
879 /* if(!err) */
880 /* err = p54u_load_firmware(netdev); */
881 /* if(!err) */
882 /* err = p54u_setup_dev(netdev); */
883 /* if(!err) */
884 /* err = p54u_mgmt_readback(netdev); */
886 /* if(!err) */
887 /* err = p54u_data_send(netdev); */
890 release_firmware(p54u->fw_entry);
892 p54u->running = 1;
893 p54u->state = P54U_RUN;
895 if(err) {
896 p54u_err("%s: Boot the device failed: %i\n", netdev->name, err);
897 return err;
900 p54u_info("%s: Boot the device done.\n", netdev->name);
901 return 0;
904 int p54u_shutdown(struct net_device *netdev)
906 struct p54u *p54u = netdev_priv(netdev);
907 struct usb_device *usbdev = p54u->usbdev;
908 u32 reg;
910 p54u_dbg("%s: Shutting down the device.\n", netdev->name);
912 /* so that the bottom halves wont resubmit urbs */
913 p54u->state = P54U_SHUTDOWN;
915 p54u_free_buffers(netdev);
916 /* now there's no way work will be scheduled again, so we can wait on the bh's. Wait is sufficient for now*/
917 p54u_mdelay(100);
919 p54u->running = 0;
921 // reg = p54u_brg_readl(netdev, P54U_BRG_CTRL_REG);
922 // p54u_brg_writel(netdev, P54U_BRG_CTRL_REG, (reg | P54U_BRG_POWER_DOWN) & ~P54U_BRG_POWER_UP);
923 // p54u_brg_writel(netdev, P54U_BRG_CTRL_REG, 0x3e);
926 p54u_dbg("%s: Shutdown complete.\n", netdev->name);
928 return 0;
931 static int p54u_validate_device(struct usb_interface *interface, const struct usb_device_id *id)
933 /* See if the device offered us matches what we can accept */
935 return 0;
938 static int p54u_probe(struct usb_interface *interface, const struct usb_device_id *id)
940 struct usb_device *usbdev = interface_to_usbdev(interface);
941 struct net_device *netdev;
942 struct p54u *p54u;
945 p54u_dbg("Prism54 USB Device Probe (Device number:%d): 0x%4.4x:0x%4.4x:0x%4.4x", usbdev->devnum, (int) usbdev->descriptor.idVendor, (int) usbdev->descriptor.idProduct, (int) usbdev->descriptor.bcdDevice);
946 p54u_dbg("Device at %p", usbdev);
947 p54u_dbg("Descriptor length: %x type: %x", (int) usbdev->descriptor.bLength, (int) usbdev->descriptor.bDescriptorType);
949 if(p54u_validate_device(interface, id))
950 return -ENODEV;
952 netdev = alloc_etherdev(sizeof(*p54u));
953 if (!netdev) {
954 p54u_err("Failed to allocate netdevice.");
955 return -ENOMEM;
958 SET_MODULE_OWNER(netdev);
959 SET_NETDEV_DEV(netdev, &interface->dev);
961 usb_set_intfdata(interface, netdev);
963 p54u = netdev_priv(netdev);
964 p54u->interface = interface;
965 p54u->usbdev = usbdev;
966 p54u->netdev = netdev;
968 init_MUTEX (&p54u->disconnect_sem);
969 memcpy(netdev->dev_addr, dummy_mac, ETH_ALEN);
971 if(load_fw)
972 /* Dont error if it fails, give it chance on open. */
973 p54u_boot(netdev);
975 if(p54u_setup_net(netdev)) {
976 p54u_err("Failed to setup netdevice.");
977 goto do_cleanup;
980 if(register_netdev(netdev)) {
981 p54u_err("Failed to register netdevice.");
982 goto do_cleanup;
985 p54u_info("Prism54 USB device now attached to %s", netdev->name);
986 return 0;
988 do_cleanup:
989 p54u_free_buffers(netdev);
990 usb_set_intfdata(interface, NULL);
991 free_netdev(netdev);
993 return -EIO;
996 static void p54u_disconnect(struct usb_interface *interface)
998 struct net_device *netdev = usb_get_intfdata(interface);
999 struct p54u *p54u = netdev_priv(netdev);
1000 struct usb_device *usbdev = p54u->usbdev;
1002 p54u_info("Prism54 USB device %s disconnecing\n", netdev->name);
1004 p54u_dbg("sem down\n");
1005 down(&p54u->disconnect_sem);
1006 p54u_dbg("sem down done\n");
1008 p54u->running = 0;
1010 p54u_dbg("Running = 0.\n");
1012 usb_set_intfdata(interface, NULL);
1014 p54u_dbg("Cleared interface data.\n");
1016 unregister_netdev(netdev);
1018 p54u_dbg("Unregistered netdevice.\n");
1020 p54u_free_buffers(netdev);
1022 p54u_dbg("Freed buffers.\n");
1024 free_netdev(netdev);
1026 p54u_dbg("Freed netdevice.\n");
1028 p54u_info("Disconnect complete.\n");
1030 p54u_dbg("sem up\n");
1031 up(&p54u->disconnect_sem);
1032 p54u_dbg("sem up done\n");
1035 static struct usb_driver p54u_driver = {
1036 .owner = THIS_MODULE,
1037 .name = driver_name,
1038 .probe = p54u_probe,
1039 .disconnect = p54u_disconnect,
1040 .id_table = p54u_table,
1043 static int __init p54u_init(void)
1045 int result;
1047 /* register this driver with the USB subsystem */
1048 result = usb_register(&p54u_driver);
1049 if (result) {
1050 p54u_err("usb_register failed. Error number %d", result);
1051 return result;
1054 return 0;
1057 static void __exit p54u_exit(void)
1059 /* deregister this driver with the USB subsystem */
1060 usb_deregister(&p54u_driver);
1063 module_init(p54u_init);
1064 module_exit(p54u_exit);