Rest of the merge.
[linux-2.6/linux-mips.git] / drivers / usb / pegasus.c
blob1e752dc75ce05650e9737819e81c8fc8769d74b4
1 /*
2 **
3 ** Pegasus: USB 10/100Mbps/HomePNA (1Mbps) Controller
4 **
5 ** Copyleft (L) 1999 Petko Manolov - Petkan (petkan@spct.net)
6 **
7 ** Distribute under GPL version 2 or later.
8 */
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/malloc.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
20 #include "usb.h"
22 #if LINUX_VERSION_CODE<0x2032d || !defined(__KERNEL__) || !defined(__OPTIMIZE__)
23 #error You can not compile this driver on this kernel with this C options!
24 #endif
27 #define ADMTEK_VENDOR_ID 0x07a6
28 #define ADMTEK_HPNA_PEGASUS 0x0986
30 #define HPNA_MTU 1500
31 #define MAX_MTU 1536
33 #define TX_TIMEOUT (HZ*5)
34 #define SOMETHING (jiffies + TX_TIMEOUT)
37 static const char version[] = "pegasus.c: v0.2.27 2000/02/29 Written by Petko Manolov (petkan@spct.net)\n";
40 typedef struct usb_hpna
42 struct usb_device *usb_dev;
43 struct net_device *net_dev;
44 int present;
45 int active;
46 void *irq_handler;
47 struct list_head list;
48 struct net_device_stats stats;
49 spinlock_t hpna_lock;
50 struct timer_list timer;
52 unsigned int rx_pipe;
53 unsigned char * rx_buff;
54 urb_t rx_urb;
56 unsigned int tx_pipe;
57 unsigned char * tx_buff;
58 urb_t tx_urb;
59 struct sk_buff * tx_skbuff;
61 __u8 intr_ival;
62 unsigned int intr_pipe;
63 unsigned char intr_buff[8];
64 urb_t intr_urb;
65 } usb_hpna_t;
68 usb_hpna_t usb_dev_hpna;
69 static int loopback = 0;
70 int multicast_filter_limit = 32;
71 static LIST_HEAD(hpna_list);
74 MODULE_AUTHOR("Petko Manolov <petkan@spct.net>");
75 MODULE_DESCRIPTION("ADMtek \"Pegasus\" USB Ethernet driver");
76 MODULE_PARM(loopback, "i");
80 /*** vendor specific commands ***/
81 static __inline__ int hpna_get_registers( struct usb_device *dev, __u16 indx, __u16 size, void *data )
83 return usb_control_msg(dev, usb_rcvctrlpipe(dev,0), 0xf0, 0xc0, 0,
84 indx, data, size, HZ);
88 static __inline__ int hpna_set_register( struct usb_device *dev, __u16 indx, __u8 value )
90 __u8 data = value;
91 return usb_control_msg(dev, usb_sndctrlpipe(dev,0), 0xf1, 0x40,
92 data, indx, &data, 1, HZ);
96 static __inline__ int hpna_set_registers( struct usb_device *dev, __u16 indx, __u16 size, void *data )
98 return usb_control_msg(dev, usb_sndctrlpipe(dev,0), 0xf1, 0x40, 0,
99 indx, data, size, HZ);
103 static int read_phy_word( struct usb_device *dev, __u8 index, __u16 *regdata )
105 int i;
106 __u8 data[4];
108 data[0] = 1;
109 data[1] = 0;
110 data[2] = 0;
111 data[3] = 0x40 + index;
112 hpna_set_registers( dev, 0x25, 4, data );
113 for ( i=0; i<100; i++ ) {
114 hpna_get_registers( dev, 0x25, 4, data );
115 if ( data[3] & 0x80 ) {
116 *regdata = *(__u16 *)(data+1);
117 return 0;
119 udelay(100);
121 warn("read_phy_word() failed");
122 return 1;
126 static int write_phy_word( struct usb_device *dev, __u8 index, __u16 regdata )
128 int i;
129 __u8 data[4];
131 data[0] = 1;
132 data[1] = regdata;
133 data[2] = regdata >> 8;
134 data[3] = 0x20 + index;
135 hpna_set_registers( dev, 0x25, 4, data );
136 for ( i=0; i<100; i++ ) {
137 hpna_get_registers( dev, 0x28, 1, data );
138 if ( data[0] & 0x80 ) {
139 return 0;
141 udelay(100);
143 warn("write_phy_word() failed");
144 return 1;
148 int read_srom_word( struct usb_device *dev, __u8 index, __u16 *retdata)
150 int i;
151 __u8 data[4];
153 data[0] = index;
154 data[1] = data[2] = 0;
155 data[3] = 0x02;
156 hpna_set_registers(dev, 0x20, 4, data);
157 for ( i=0; i<100; i++ ) {
158 hpna_get_registers(dev, 0x23, 1, data);
159 if ( data[0] & 4 ) {
160 hpna_get_registers(dev, 0x21, 2, data);
161 *retdata = *(__u16 *)data;
162 return 0;
165 warn("read_srom_word() failed");
166 return 1;
168 /*** end ***/
173 int get_node_id( struct usb_device *dev, __u8 *id )
175 int i;
177 for ( i=0; i<3; i++ ) {
178 if ( read_srom_word(dev, i, (__u16 *)&id[i*2] ) )
179 return 1;
181 return 0;
185 static int reset_mac( struct usb_device *dev )
187 __u8 data = 0x8;
188 int i;
190 hpna_set_register( dev, 1, 0x08 );
191 for ( i=0; i<100; i++ ) {
192 hpna_get_registers( dev, 1, 1, &data);
193 if ( !(data & 0x08) ) {
194 if ( loopback & 1 )
195 return 0;
196 else if ( loopback & 2 ) {
197 write_phy_word( dev, 0, 0x4000 );
198 /*return 0;*/
200 hpna_set_register( dev, 0x7e, 0x24 );
201 hpna_set_register( dev, 0x7e, 0x27 );
202 return 0;
205 return 1;
209 int start_net( struct net_device *dev, struct usb_device *usb_dev )
211 __u16 partmedia, temp;
212 __u8 node_id[6];
213 __u8 data[4];
215 if ( get_node_id(usb_dev, node_id) )
216 return 1;
217 hpna_set_registers(usb_dev, 0x10, 6, node_id);
218 memcpy(dev->dev_addr, node_id, 6);
219 if ( read_phy_word(usb_dev, 1, &temp) )
220 return 2;
221 if ( !(temp & 4) ) {
222 if ( loopback )
223 goto ok;
224 err("link NOT established - %x", temp);
225 return 3;
228 if ( read_phy_word(usb_dev, 5, &partmedia) )
229 return 4;
230 temp = partmedia;
231 partmedia &= 0x1f;
232 if ( partmedia != 1 ) {
233 err("party FAIL %x", temp);
234 return 5;
236 partmedia = temp;
237 if ( partmedia & 0x100 )
238 data[1] = 0x30;
239 else {
240 if ( partmedia & 0x80 )
241 data[1] = 0x10;
242 else
243 data[1] = 0;
246 data[0] = 0xc9;
247 data[2] = (loopback & 1) ? 0x08 : 0x00;
249 hpna_set_registers(usb_dev, 0, 3, data);
251 return 0;
255 static void hpna_read_irq( purb_t urb )
257 struct net_device *net_dev = urb->context;
258 usb_hpna_t *hpna = net_dev->priv;
259 int count = urb->actual_length, res;
260 int rx_status = *(int *)(hpna->rx_buff + count - 4);
263 if ( urb->status ) {
264 info( "%s: RX status %d\n", net_dev->name, urb->status );
265 goto goon;
268 if ( !count )
269 goto goon;
270 /* if ( rx_status & 0x00010000 )
271 goto goon;
273 if ( rx_status & 0x000e0000 ) {
274 dbg("%s: error receiving packet %x",
275 net_dev->name, rx_status & 0xe0000);
276 hpna->stats.rx_errors++;
277 if(rx_status & 0x060000) hpna->stats.rx_length_errors++;
278 if(rx_status & 0x080000) hpna->stats.rx_crc_errors++;
279 if(rx_status & 0x100000) hpna->stats.rx_frame_errors++;
280 } else {
281 struct sk_buff *skb;
282 __u16 pkt_len = (rx_status & 0xfff) - 8;
285 if((skb = dev_alloc_skb(pkt_len+2)) != NULL ) {
286 skb->dev = net_dev;
287 skb_reserve(skb, 2);
288 eth_copy_and_sum(skb, hpna->rx_buff, pkt_len, 0);
289 skb_put(skb, pkt_len);
290 } else
291 goto goon;
292 skb->protocol = eth_type_trans(skb, net_dev);
293 netif_rx(skb);
294 hpna->stats.rx_packets++;
295 hpna->stats.rx_bytes += pkt_len;
297 goon:
298 if ( (res = usb_submit_urb( &hpna->rx_urb )) )
299 warn("failed rx_urb %d", res);
303 static void hpna_irq( urb_t *urb)
305 if( urb->status ) {
306 __u8 *d = urb->transfer_buffer;
307 printk("txst0 %x, txst1 %x, rxst %x, rxlst0 %x, rxlst1 %x, wakest %x",
308 d[0], d[1], d[2], d[3], d[4], d[5] );
313 static void hpna_write_irq( purb_t urb )
315 struct net_device *net_dev = urb->context;
316 usb_hpna_t *hpna = net_dev->priv;
319 spin_lock( &hpna->hpna_lock );
321 if ( urb->status )
322 info("%s: TX status %d\n", net_dev->name, urb->status);
323 netif_wake_queue( net_dev );
325 spin_unlock( &hpna->hpna_lock );
329 static void tx_timeout( struct net_device *dev )
331 usb_hpna_t *hpna = dev->priv;
333 warn( "%s: Tx timed out. Reseting...", dev->name );
334 hpna->stats.tx_errors++;
335 dev->trans_start = jiffies;
336 netif_wake_queue( dev );
340 static int hpna_start_xmit( struct sk_buff *skb, struct net_device *net_dev )
342 usb_hpna_t *hpna = (usb_hpna_t *)net_dev->priv;
343 int count = skb->len+2 % 64 ? skb->len+2 : skb->len+3;
344 int res;
346 spin_lock( &hpna->hpna_lock );
348 netif_stop_queue( net_dev );
349 ((__u16 *)hpna->tx_buff)[0] = skb->len;
350 memcpy(hpna->tx_buff+2, skb->data, skb->len);
351 (&hpna->tx_urb)->transfer_buffer_length = count;
352 if ( (res = usb_submit_urb( &hpna->tx_urb )) ) {
353 warn("failed tx_urb %d", res);
354 hpna->stats.tx_errors++;
355 netif_start_queue( net_dev );
356 } else {
357 hpna->stats.tx_packets++;
358 hpna->stats.tx_bytes += skb->len;
359 net_dev->trans_start = jiffies;
361 dev_kfree_skb( skb );
362 spin_unlock( &hpna->hpna_lock );
363 return 0;
367 static struct net_device_stats *hpna_netdev_stats( struct net_device *dev )
369 return &((usb_hpna_t *)dev->priv)->stats;
372 static int hpna_open( struct net_device *net_dev )
374 usb_hpna_t *hpna = (usb_hpna_t *)net_dev->priv;
375 int res;
377 if ( hpna->active )
378 return -EBUSY;
379 else
380 hpna->active = 1;
382 if ( start_net(net_dev, hpna->usb_dev) ) {
383 err("can't start_net()");
384 return -EIO;
387 if ( (res = usb_submit_urb( &hpna->rx_urb )) )
388 warn("failed rx_urb %d", res);
390 /* usb_submit_urb( &hpna->intr_urb );*/
391 netif_start_queue( net_dev );
393 MOD_INC_USE_COUNT;
395 return 0;
399 static int hpna_close( struct net_device *net_dev )
401 usb_hpna_t *hpna = net_dev->priv;
404 netif_stop_queue( net_dev );
406 usb_unlink_urb( &hpna->rx_urb );
407 usb_unlink_urb( &hpna->tx_urb );
408 /* usb_unlink_urb( hpna->intr_urb );*/
410 hpna->active = 0;
412 MOD_DEC_USE_COUNT;
414 return 0;
418 static int hpna_ioctl( struct net_device *dev, struct ifreq *rq, int cmd )
420 __u16 *data = (__u16 *)&rq->ifr_data;
421 usb_hpna_t *hpna = dev->priv;
423 switch( cmd ) {
424 case SIOCDEVPRIVATE:
425 data[0] = 1;
426 case SIOCDEVPRIVATE+1:
427 read_phy_word(hpna->usb_dev, data[1] & 0x1f, &data[3]);
428 return 0;
429 case SIOCDEVPRIVATE+2:
430 if ( !capable(CAP_NET_ADMIN) )
431 return -EPERM;
432 write_phy_word(hpna->usb_dev, data[1] & 0x1f, data[2]);
433 return 0;
434 default:
435 return -EOPNOTSUPP;
440 static void set_rx_mode( struct net_device *net_dev )
442 usb_hpna_t *hpna=net_dev->priv;
444 netif_stop_queue( net_dev );
446 if ( net_dev->flags & IFF_PROMISC ) {
447 info("%s: Promiscuous mode enabled", net_dev->name);
448 hpna_set_register( hpna->usb_dev, 2, 0x04 );
449 } else if ((net_dev->mc_count > multicast_filter_limit) ||
450 (net_dev->flags & IFF_ALLMULTI)) {
451 hpna_set_register(hpna->usb_dev, 0, 0xfa);
452 hpna_set_register(hpna->usb_dev, 2, 0);
453 } else {
454 dbg("%s: set Rx mode", net_dev->name);
457 netif_wake_queue( net_dev );
461 static void * usb_hpna_probe( struct usb_device *dev, unsigned int ifnum )
463 struct net_device *net_dev;
464 usb_hpna_t *hpna = &usb_dev_hpna;
468 if ( dev->descriptor.idVendor != ADMTEK_VENDOR_ID ||
469 dev->descriptor.idProduct != ADMTEK_HPNA_PEGASUS ) {
470 return NULL;
473 printk("USB HPNA Pegasus found\n");
475 if ( usb_set_configuration(dev, dev->config[0].bConfigurationValue)) {
476 err("usb_set_configuration() failed");
477 return NULL;
480 hpna->usb_dev = dev;
482 hpna->rx_pipe = usb_rcvbulkpipe(hpna->usb_dev, 1);
483 hpna->tx_pipe = usb_sndbulkpipe(hpna->usb_dev, 2);
484 hpna->intr_pipe = usb_rcvintpipe(hpna->usb_dev, 0);
486 if ( reset_mac(dev) ) {
487 err("can't reset MAC");
490 hpna->present = 1;
492 if(!(hpna->rx_buff=kmalloc(MAX_MTU, GFP_KERNEL))) {
493 err("not enough mem for out buff");
494 return NULL;
496 if(!(hpna->tx_buff=kmalloc(MAX_MTU, GFP_KERNEL))) {
497 kfree_s(hpna->rx_buff, MAX_MTU);
498 err("not enough mem for out buff");
499 return NULL;
502 net_dev = init_etherdev( 0, 0 );
503 hpna->net_dev = net_dev;
504 net_dev->priv = hpna;
505 net_dev->open = hpna_open;
506 net_dev->stop = hpna_close;
507 net_dev->watchdog_timeo = TX_TIMEOUT;
508 net_dev->tx_timeout = tx_timeout;
509 net_dev->do_ioctl = hpna_ioctl;
510 net_dev->hard_start_xmit = hpna_start_xmit;
511 net_dev->set_multicast_list = set_rx_mode;
512 net_dev->get_stats = hpna_netdev_stats;
513 net_dev->mtu = HPNA_MTU;
514 hpna->hpna_lock = SPIN_LOCK_UNLOCKED;
516 FILL_BULK_URB( &hpna->rx_urb, hpna->usb_dev, hpna->rx_pipe,
517 hpna->rx_buff, MAX_MTU, hpna_read_irq, net_dev );
518 FILL_BULK_URB( &hpna->tx_urb, hpna->usb_dev, hpna->tx_pipe,
519 hpna->tx_buff, MAX_MTU, hpna_write_irq, net_dev );
520 FILL_INT_URB( &hpna->intr_urb, hpna->usb_dev, hpna->intr_pipe,
521 hpna->intr_buff, 8, hpna_irq, net_dev, 250 );
523 /* list_add( &hpna->list, &hpna_list );*/
525 return net_dev;
529 static void usb_hpna_disconnect( struct usb_device *dev, void *ptr )
531 struct net_device *net_dev = ptr;
532 struct usb_hpna *hpna = net_dev->priv;
535 if ( net_dev->flags & IFF_UP )
536 dev_close(net_dev);
538 unregister_netdev( net_dev );
540 if ( !hpna ) /* should never happen */
541 return;
543 usb_unlink_urb( &hpna->rx_urb );
544 usb_unlink_urb( &hpna->tx_urb );
545 /* usb_unlink_urb( &hpna->intr_urb );*/
546 kfree_s(hpna->rx_buff, MAX_MTU);
547 kfree_s(hpna->tx_buff, MAX_MTU);
549 hpna->usb_dev = NULL;
550 hpna->present = 0;
552 printk("USB HPNA disconnected\n");
556 static struct usb_driver usb_hpna_driver = {
557 "ADMtek \"Pegasus\" USB Ethernet",
558 usb_hpna_probe,
559 usb_hpna_disconnect,
560 {NULL, NULL}
565 static int __init start_hpna( void )
567 printk( version );
568 return usb_register( &usb_hpna_driver );
572 static void __exit stop_hpna( void )
574 usb_deregister( &usb_hpna_driver );
578 module_init( start_hpna );
579 module_exit( stop_hpna );