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"
22 void p54u_mdelay(int ms
)
24 int t
= (ms
* HZ
+ 500) / 1000;
29 set_current_state(TASK_UNINTERRUPTIBLE
);
31 //printk("t: %i\n", t);
32 t
= schedule_timeout(t
);
39 void p54u_data_debug(struct net_device
*netdev
, unsigned int ep
, void *_data
, int len
)
41 struct p54u
*p54u
= netdev_priv(netdev
);
42 struct usb_device
*usbdev
= p54u
->usbdev
;
43 unsigned char *data
= _data
;
45 unsigned long int time
= jiffies
;
49 for(k
= 0; k
< len
; k
+= 16) {
51 printk(KERN_CRIT
"[%s >%-6lld<%-6lld+%-6ld] %02x %s 00000000:", in
?"IN ":"OUT", 0LL, 0LL, time
, ep
, in
?"<-":"->");
53 printk(KERN_CRIT
" -> %08x:", k
);
55 for (l
= 0; (l
< 16) && (k
+ l
< len
); ++l
) {
56 printk(" %02x", ((unsigned char *)data
)[k
+ l
]);
63 int p54u_bulk_msg(struct net_device
*netdev
, unsigned int ep
, void *_data
, int len
)
65 struct p54u
*p54u
= netdev_priv(netdev
);
66 struct usb_device
*usbdev
= p54u
->usbdev
;
67 unsigned char *data
= _data
;
71 p54u_data_debug(netdev
, ep
, data
, len
);
73 pipe
= usb_sndbulkpipe(usbdev
, ep
);
74 err
= usb_bulk_msg(usbdev
, pipe
, data
, len
, &alen
, 2 * HZ
);
76 p54u_info("bulk submit failed: %i\n", err
);
82 u32
p54u_reg_rw(struct net_device
*netdev
, int write
, int ep
, int port
, u32 addr
, u32 val
)
84 struct p54u
*p54u
= netdev_priv(netdev
);
85 struct usb_device
*usbdev
= p54u
->usbdev
;
92 reg
.port
= cpu_to_le16(port
);
93 reg
.addr
= cpu_to_le32(addr
);
94 reg
.val
= ((port
& P54U_PORT_U32
) == P54U_PORT_U32
) ? cpu_to_le32(val
) : cpu_to_le32(val
&0xffff);
95 len
= write
? P54U_REG_WSIZE
: P54U_REG_RSIZE
;
96 pipe
= usb_sndbulkpipe(usbdev
, ep
& USB_ENDPOINT_NUMBER_MASK
);
99 err
= usb_bulk_msg(usbdev
, pipe
, ®
, len
, &retlen
, HZ
);
103 p54u_data_debug(netdev
, ep
, ®
, len
);
106 p54u_err("%s %02x %04x %08x %08x: failed: %i", write
? "Write": "Read", ep
, port
, addr
, val
, err
);
108 if(write
&& (ep
== P54U_PIPE_DEV
) && (len
!= retlen
)) {
109 /* something bad happened, but we know how to cope with this case. We should do it for reads, too */
110 p54u_err("expected to write %i but wrote %i on dev pipe, resetting",len
,retlen
);
112 usb_clear_halt(usbdev
,pipe
);
113 /* resubmit only once */
114 err
= usb_bulk_msg(usbdev
, pipe
, ®
, len
, &retlen
, HZ
);
116 p54u_err("%s %02x %04x %08x %08x: failed: %i", write
? "Write": "Read", ep
, port
, addr
, val
, err
);
127 // udelay(ISL38XX_WRITEIO_DELAY);
131 pipe
= usb_rcvbulkpipe(usbdev
, ep
& USB_ENDPOINT_NUMBER_MASK
);
132 len
= sizeof(reg
.val
);
135 err
= usb_bulk_msg(usbdev
, pipe
, ®
.val
, len
, &len
, HZ
);
140 p54u_err("Register read %02x %04x %08x %08x: failed: %i", ep
, port
, addr
, val
, err
);
145 p54u_data_debug(netdev
, ep
| USB_DIR_IN
, ®
.val
, len
);
147 return ((port
& P54U_PORT_U32
) == P54U_PORT_U32
) ? le32_to_cpu(reg
.val
) : (le32_to_cpu(reg
.val
)&0xffff);
150 static void p54u_wait_ctrl(struct urb
*urb
, struct pt_regs
*regs
) {
151 struct completion
*comp
= (struct completion
*) urb
->context
;
156 int p54u_annouced_msg(struct net_device
*netdev
, unsigned int pipe
, void *data
, int data_len
)
158 struct p54u
*p54u
= netdev_priv(netdev
);
159 struct usb_device
*usbdev
= p54u
->usbdev
;
160 struct p54u_reg
*reg
= 0;
162 unsigned int ctrlpipe
;
163 struct completion done
;
171 init_completion(&done
);
173 reg
= (struct p54u_reg
*) kmalloc(sizeof(struct p54u_reg
),GFP_KERNEL
);
177 urb
= usb_alloc_urb(0,GFP_KERNEL
);
184 ctrlpipe
= usb_sndbulkpipe(usbdev
, P54U_PIPE_DEV
& USB_ENDPOINT_NUMBER_MASK
);
186 usb_fill_bulk_urb(urb
, usbdev
, ctrlpipe
, (char *) reg
, P54U_REG_WSIZE
,
187 p54u_wait_ctrl
, &done
);
189 if (pipe
== P54U_PIPE_DATA
)
191 val
= ISL38XX_DEV_INT_DATA
;
193 else if (pipe
== P54U_PIPE_MGMT
)
195 val
= ISL38XX_DEV_INT_MGMT
;
198 reg
->port
= cpu_to_le16(P54U_PORT_DEV_U32
);
199 reg
->addr
= cpu_to_le32(P54U_DEV_BASE
| ISL38XX_DEV_INT_REG
);
200 reg
->val
= ((P54U_PORT_DEV_U32
& P54U_PORT_U32
) == P54U_PORT_U32
) ? cpu_to_le32(val
) : cpu_to_le32(val
&0xffff);
202 /* remap data buffer if non-dmable */
203 // if (!virt_addr_valid(remap_buf)) {
206 remap_buf
= kmalloc(data_len
, GFP_KERNEL
);
209 memcpy(remap_buf
, data
, data_len
);
211 /* Send first one asynchronously and wait for last one */
212 p54u
->err
= usb_submit_urb(urb
, GFP_KERNEL
);
215 p54u_data_debug(netdev
, P54U_PIPE_DEV
, reg
, P54U_REG_WSIZE
);
216 p54u
->err
= p54u_bulk_msg(netdev
, pipe
, remap_buf
,data_len
);
217 wait_for_completion(&done
);
229 static void timeout_kill(unsigned long data
) {
230 struct p54u_pipe
*pipe
= (struct p54u_pipe
*) data
;
231 struct completion
*comp
= &pipe
->comp
;
232 // p54u->err=-ETIMEDOUT; we can put the value back in the pipe
234 printk("timeout waiting");
238 static int p54u_wait_timeout(struct p54u_pipe
*pipe
, unsigned int delay
)
240 struct timer_list timer
;
243 timer
.expires
= jiffies
+ HZ
*delay
;
244 timer
.data
= (unsigned long)(pipe
);
245 timer
.function
= timeout_kill
;
248 wait_for_completion(&pipe
->comp
);
249 del_timer_sync(&timer
);
254 int p54u_wait_int(struct net_device
*netdev
)
256 struct p54u
*p54u
= netdev_priv(netdev
);
257 struct usb_device
*usbdev
= p54u
->usbdev
;
259 p54u_wait_timeout(&p54u
->int_rx
,1);
261 return p54u
->pending
;
264 int p54u_wait_mgmt_response(struct net_device
*netdev
)
266 struct p54u
*p54u
= netdev_priv(netdev
);
267 struct usb_device
*usbdev
= p54u
->usbdev
;
269 p54u_wait_timeout(&p54u
->mgmt_rx
,1);
271 return p54u
->pending
;
274 int p54u_wait_data(struct net_device
*netdev
)
276 struct p54u
*p54u
= netdev_priv(netdev
);
277 struct usb_device
*usbdev
= p54u
->usbdev
;
279 p54u_wait_timeout(&p54u
->data_rx
,1);
281 return p54u
->pending
;
284 void p54u_int_ack(void *data
)
286 struct net_device
*netdev
= (struct net_device
*) data
;
287 struct p54u
*p54u
= netdev_priv(netdev
);
288 u32 pending
= p54u
->pending
;
291 /* disable interrupt in ISL */
292 p54u_dev_writel(netdev
, ISL38XX_INT_EN_REG
, 0);
295 /* ack the interrupts */
296 reg
= p54u_dev_readl(netdev
, ISL38XX_INT_IDENT_REG
);
297 p54u_info("isl int vector: %08x\n", reg
);
298 if (reg
& ~(0x80000000))
299 p54u_dev_writel(netdev
, ISL38XX_INT_ACK_REG
, reg
);
301 while(reg
& ~(0x80000000));
303 /* clear the interrupt at net2280 level
304 the net2280 is in host mode */
305 p54u_brg_writel(netdev
, NET2280_IRQSTAT1
, NET2280_PCI_INTA_INTERRUPT
);
307 /* reenable interrupts in ISL */
308 p54u_dev_writel(netdev
, ISL38XX_INT_EN_REG
, 0x00004004);
310 /* resubmit URB, only if we're not stopping */
311 if (p54u
->state
!= P54U_SHUTDOWN
)
313 p54u
->err
= usb_submit_urb(p54u
->int_rx
.urb
[0], GFP_KERNEL
);
315 p54u_dbg("%s: Error submit int 0: %i\n", netdev
->name
, p54u
->err
);
321 void p54u_int_rx_cb(struct urb
*urb
, struct pt_regs
*p
)
323 struct net_device
*netdev
= urb
->context
;
324 struct p54u
*p54u
= netdev_priv(netdev
);
325 struct usb_device
*usbdev
= p54u
->usbdev
;
330 if (urb
->status
!= 0)
332 p54u_dbg("interrupt handler abnormal termination");
333 /* This is bound to happen upon urb handback */
337 pending
= *(int *)urb
->transfer_buffer
;
338 p54u_data_debug(netdev
, 0x8f, urb
->transfer_buffer
, urb
->actual_length
);
339 p54u
->pending
= le32_to_cpu(pending
);
340 if(p54u
->pending
& NET2280_PCI_INTA_INTERRUPT
)
341 p54u_dbg("interrupt from prism chip");
343 if(p54u
->pending
& ~NET2280_PCI_INTA_INTERRUPT
)
344 p54u_dbg("interrupt from unknown source");
346 // only complete on return to zero
347 if(p54u
->pending
== 0)
349 p54u_dbg("interrupt returning to inactive, scheduling work");
350 schedule_work(&p54u
->int_ack
);
351 complete(&p54u
->int_rx
.comp
);
353 p54u_dbg("interrupts pending, scheduling work");
354 schedule_work(&p54u
->int_ack
);
357 // in the end only complete when == 0;
362 void p54u_data_rx_cb(struct urb
*urb
, struct pt_regs
*p
)
364 struct net_device
*netdev
= urb
->context
;
365 struct p54u
*p54u
= netdev_priv(netdev
);
366 struct usb_device
*usbdev
= p54u
->usbdev
;
369 if (urb
->status
!= 0)
371 p54u_dbg("data rx abnormal termination");
375 atomic_dec(&p54u
->data_rx
.busy
);
376 complete(&p54u
->data_rx
.comp
);
379 /* this will resubmit the urb */
380 schedule_work(&p54u
->data_bh
);
384 void p54u_data_rx(void *data
)
386 struct net_device
*netdev
= (struct net_device
*) data
;
387 struct p54u
*p54u
= netdev_priv(netdev
);
389 /* for now will be the only thing we do with it : display */
391 p54u_data_debug(netdev
, 0x81, p54u
->data_rx
.buf
[0], p54u
->data_rx
.urb
[0]->actual_length
);
392 p54u_data_rx_submit(netdev
);
395 void p54u_mgmt_rx_cb(struct urb
*urb
, struct pt_regs
*p
)
397 struct net_device
*netdev
= urb
->context
;
398 struct p54u
*p54u
= netdev_priv(netdev
);
399 struct usb_device
*usbdev
= p54u
->usbdev
;
401 if (urb
->status
!= 0)
403 p54u_dbg("mgmt rx abnormal termination");
407 atomic_dec(&p54u
->mgmt_rx
.busy
);
408 complete(&p54u
->mgmt_rx
.comp
);
409 p54u_data_debug(netdev
, 0x82, urb
->transfer_buffer
, urb
->actual_length
);
413 int p54u_mgmt_rx_submit(struct net_device
*netdev
)
416 struct p54u
*p54u
= netdev_priv(netdev
);
417 if (p54u
->state
!= P54U_SHUTDOWN
)
419 err
= usb_submit_urb(p54u
->mgmt_rx
.urb
[0], GFP_KERNEL
);
421 p54u_info("mgmt submit failed %i\n", err
);
424 atomic_inc(&p54u
->mgmt_rx
.busy
);
425 p54u_dbg("%s: Submit mgmt ok.\n", netdev
->name
);
432 int p54u_data_rx_submit(struct net_device
*netdev
)
435 struct p54u
*p54u
= netdev_priv(netdev
);
437 if (p54u
->state
!= P54U_SHUTDOWN
)
440 err
= usb_submit_urb(p54u
->data_rx
.urb
[0], GFP_KERNEL
);
442 p54u_info("data submit failed %i\n", err
);
445 atomic_inc(&p54u
->data_rx
.busy
);
446 p54u_dbg("%s: Submit data ok.\n", netdev
->name
);