]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/phy/phy_device.c
Merge branch 'linus' into x86/kconfig
[linux-2.6-omap-h63xx.git] / drivers / net / phy / phy_device.c
1 /*
2  * drivers/net/phy/phy_device.c
3  *
4  * Framework for finding and configuring PHYs.
5  * Also contains generic PHY driver
6  *
7  * Author: Andy Fleming
8  *
9  * Copyright (c) 2004 Freescale Semiconductor, Inc.
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  *
16  */
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
33
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/uaccess.h>
37
38 MODULE_DESCRIPTION("PHY library");
39 MODULE_AUTHOR("Andy Fleming");
40 MODULE_LICENSE("GPL");
41
42 static struct phy_driver genphy_driver;
43 extern int mdio_bus_init(void);
44 extern void mdio_bus_exit(void);
45
46 void phy_device_free(struct phy_device *phydev)
47 {
48         kfree(phydev);
49 }
50
51 static void phy_device_release(struct device *dev)
52 {
53         phy_device_free(to_phy_device(dev));
54 }
55
56 static LIST_HEAD(phy_fixup_list);
57 static DEFINE_MUTEX(phy_fixup_lock);
58
59 /*
60  * Creates a new phy_fixup and adds it to the list
61  * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
62  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
63  *      It can also be PHY_ANY_UID
64  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
65  *      comparison
66  * @run: The actual code to be run when a matching PHY is found
67  */
68 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
69                 int (*run)(struct phy_device *))
70 {
71         struct phy_fixup *fixup;
72
73         fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
74         if (!fixup)
75                 return -ENOMEM;
76
77         strncpy(fixup->bus_id, bus_id, BUS_ID_SIZE);
78         fixup->phy_uid = phy_uid;
79         fixup->phy_uid_mask = phy_uid_mask;
80         fixup->run = run;
81
82         mutex_lock(&phy_fixup_lock);
83         list_add_tail(&fixup->list, &phy_fixup_list);
84         mutex_unlock(&phy_fixup_lock);
85
86         return 0;
87 }
88 EXPORT_SYMBOL(phy_register_fixup);
89
90 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
91 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
92                 int (*run)(struct phy_device *))
93 {
94         return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
95 }
96 EXPORT_SYMBOL(phy_register_fixup_for_uid);
97
98 /* Registers a fixup to be run on the PHY with id string bus_id */
99 int phy_register_fixup_for_id(const char *bus_id,
100                 int (*run)(struct phy_device *))
101 {
102         return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
103 }
104 EXPORT_SYMBOL(phy_register_fixup_for_id);
105
106 /*
107  * Returns 1 if fixup matches phydev in bus_id and phy_uid.
108  * Fixups can be set to match any in one or more fields.
109  */
110 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
111 {
112         if (strcmp(fixup->bus_id, phydev->dev.bus_id) != 0)
113                 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
114                         return 0;
115
116         if ((fixup->phy_uid & fixup->phy_uid_mask) !=
117                         (phydev->phy_id & fixup->phy_uid_mask))
118                 if (fixup->phy_uid != PHY_ANY_UID)
119                         return 0;
120
121         return 1;
122 }
123
124 /* Runs any matching fixups for this phydev */
125 int phy_scan_fixups(struct phy_device *phydev)
126 {
127         struct phy_fixup *fixup;
128
129         mutex_lock(&phy_fixup_lock);
130         list_for_each_entry(fixup, &phy_fixup_list, list) {
131                 if (phy_needs_fixup(phydev, fixup)) {
132                         int err;
133
134                         err = fixup->run(phydev);
135
136                         if (err < 0)
137                                 return err;
138                 }
139         }
140         mutex_unlock(&phy_fixup_lock);
141
142         return 0;
143 }
144 EXPORT_SYMBOL(phy_scan_fixups);
145
146 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
147 {
148         struct phy_device *dev;
149         /* We allocate the device, and initialize the
150          * default values */
151         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
152
153         if (NULL == dev)
154                 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
155
156         dev->dev.release = phy_device_release;
157
158         dev->speed = 0;
159         dev->duplex = -1;
160         dev->pause = dev->asym_pause = 0;
161         dev->link = 1;
162         dev->interface = PHY_INTERFACE_MODE_GMII;
163
164         dev->autoneg = AUTONEG_ENABLE;
165
166         dev->addr = addr;
167         dev->phy_id = phy_id;
168         dev->bus = bus;
169
170         dev->state = PHY_DOWN;
171
172         mutex_init(&dev->lock);
173
174         return dev;
175 }
176 EXPORT_SYMBOL(phy_device_create);
177
178 /**
179  * get_phy_id - reads the specified addr for its ID.
180  * @bus: the target MII bus
181  * @addr: PHY address on the MII bus
182  * @phy_id: where to store the ID retrieved.
183  *
184  * Description: Reads the ID registers of the PHY at @addr on the
185  *   @bus, stores it in @phy_id and returns zero on success.
186  */
187 int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
188 {
189         int phy_reg;
190
191         /* Grab the bits from PHYIR1, and put them
192          * in the upper half */
193         phy_reg = bus->read(bus, addr, MII_PHYSID1);
194
195         if (phy_reg < 0)
196                 return -EIO;
197
198         *phy_id = (phy_reg & 0xffff) << 16;
199
200         /* Grab the bits from PHYIR2, and put them in the lower half */
201         phy_reg = bus->read(bus, addr, MII_PHYSID2);
202
203         if (phy_reg < 0)
204                 return -EIO;
205
206         *phy_id |= (phy_reg & 0xffff);
207
208         return 0;
209 }
210 EXPORT_SYMBOL(get_phy_id);
211
212 /**
213  * get_phy_device - reads the specified PHY device and returns its @phy_device struct
214  * @bus: the target MII bus
215  * @addr: PHY address on the MII bus
216  *
217  * Description: Reads the ID registers of the PHY at @addr on the
218  *   @bus, then allocates and returns the phy_device to represent it.
219  */
220 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
221 {
222         struct phy_device *dev = NULL;
223         u32 phy_id;
224         int r;
225
226         r = get_phy_id(bus, addr, &phy_id);
227         if (r)
228                 return ERR_PTR(r);
229
230         /* If the phy_id is all Fs, there is no device there */
231         if (0xffffffff == phy_id)
232                 return NULL;
233
234         dev = phy_device_create(bus, addr, phy_id);
235
236         return dev;
237 }
238
239 /**
240  * phy_prepare_link - prepares the PHY layer to monitor link status
241  * @phydev: target phy_device struct
242  * @handler: callback function for link status change notifications
243  *
244  * Description: Tells the PHY infrastructure to handle the
245  *   gory details on monitoring link status (whether through
246  *   polling or an interrupt), and to call back to the
247  *   connected device driver when the link status changes.
248  *   If you want to monitor your own link state, don't call
249  *   this function.
250  */
251 void phy_prepare_link(struct phy_device *phydev,
252                 void (*handler)(struct net_device *))
253 {
254         phydev->adjust_link = handler;
255 }
256
257 /**
258  * phy_connect - connect an ethernet device to a PHY device
259  * @dev: the network device to connect
260  * @bus_id: the id string of the PHY device to connect
261  * @handler: callback function for state change notifications
262  * @flags: PHY device's dev_flags
263  * @interface: PHY device's interface
264  *
265  * Description: Convenience function for connecting ethernet
266  *   devices to PHY devices.  The default behavior is for
267  *   the PHY infrastructure to handle everything, and only notify
268  *   the connected driver when the link status changes.  If you
269  *   don't want, or can't use the provided functionality, you may
270  *   choose to call only the subset of functions which provide
271  *   the desired functionality.
272  */
273 struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
274                 void (*handler)(struct net_device *), u32 flags,
275                 phy_interface_t interface)
276 {
277         struct phy_device *phydev;
278
279         phydev = phy_attach(dev, bus_id, flags, interface);
280
281         if (IS_ERR(phydev))
282                 return phydev;
283
284         phy_prepare_link(phydev, handler);
285
286         phy_start_machine(phydev, NULL);
287
288         if (phydev->irq > 0)
289                 phy_start_interrupts(phydev);
290
291         return phydev;
292 }
293 EXPORT_SYMBOL(phy_connect);
294
295 /**
296  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
297  * @phydev: target phy_device struct
298  */
299 void phy_disconnect(struct phy_device *phydev)
300 {
301         if (phydev->irq > 0)
302                 phy_stop_interrupts(phydev);
303
304         phy_stop_machine(phydev);
305         
306         phydev->adjust_link = NULL;
307
308         phy_detach(phydev);
309 }
310 EXPORT_SYMBOL(phy_disconnect);
311
312 static int phy_compare_id(struct device *dev, void *data)
313 {
314         return strcmp((char *)data, dev->bus_id) ? 0 : 1;
315 }
316
317 /**
318  * phy_attach - attach a network device to a particular PHY device
319  * @dev: network device to attach
320  * @bus_id: PHY device to attach
321  * @flags: PHY device's dev_flags
322  * @interface: PHY device's interface
323  *
324  * Description: Called by drivers to attach to a particular PHY
325  *     device. The phy_device is found, and properly hooked up
326  *     to the phy_driver.  If no driver is attached, then the
327  *     genphy_driver is used.  The phy_device is given a ptr to
328  *     the attaching device, and given a callback for link status
329  *     change.  The phy_device is returned to the attaching driver.
330  */
331 struct phy_device *phy_attach(struct net_device *dev,
332                 const char *bus_id, u32 flags, phy_interface_t interface)
333 {
334         struct bus_type *bus = &mdio_bus_type;
335         struct phy_device *phydev;
336         struct device *d;
337
338         /* Search the list of PHY devices on the mdio bus for the
339          * PHY with the requested name */
340         d = bus_find_device(bus, NULL, (void *)bus_id, phy_compare_id);
341
342         if (d) {
343                 phydev = to_phy_device(d);
344         } else {
345                 printk(KERN_ERR "%s not found\n", bus_id);
346                 return ERR_PTR(-ENODEV);
347         }
348
349         /* Assume that if there is no driver, that it doesn't
350          * exist, and we should use the genphy driver. */
351         if (NULL == d->driver) {
352                 int err;
353                 d->driver = &genphy_driver.driver;
354
355                 err = d->driver->probe(d);
356                 if (err >= 0)
357                         err = device_bind_driver(d);
358
359                 if (err)
360                         return ERR_PTR(err);
361         }
362
363         if (phydev->attached_dev) {
364                 printk(KERN_ERR "%s: %s already attached\n",
365                                 dev->name, bus_id);
366                 return ERR_PTR(-EBUSY);
367         }
368
369         phydev->attached_dev = dev;
370
371         phydev->dev_flags = flags;
372
373         phydev->interface = interface;
374
375         /* Do initial configuration here, now that
376          * we have certain key parameters
377          * (dev_flags and interface) */
378         if (phydev->drv->config_init) {
379                 int err;
380
381                 err = phy_scan_fixups(phydev);
382
383                 if (err < 0)
384                         return ERR_PTR(err);
385
386                 err = phydev->drv->config_init(phydev);
387
388                 if (err < 0)
389                         return ERR_PTR(err);
390         }
391
392         return phydev;
393 }
394 EXPORT_SYMBOL(phy_attach);
395
396 /**
397  * phy_detach - detach a PHY device from its network device
398  * @phydev: target phy_device struct
399  */
400 void phy_detach(struct phy_device *phydev)
401 {
402         phydev->attached_dev = NULL;
403
404         /* If the device had no specific driver before (i.e. - it
405          * was using the generic driver), we unbind the device
406          * from the generic driver so that there's a chance a
407          * real driver could be loaded */
408         if (phydev->dev.driver == &genphy_driver.driver)
409                 device_release_driver(&phydev->dev);
410 }
411 EXPORT_SYMBOL(phy_detach);
412
413
414 /* Generic PHY support and helper functions */
415
416 /**
417  * genphy_config_advert - sanitize and advertise auto-negotation parameters
418  * @phydev: target phy_device struct
419  *
420  * Description: Writes MII_ADVERTISE with the appropriate values,
421  *   after sanitizing the values to make sure we only advertise
422  *   what is supported.
423  */
424 int genphy_config_advert(struct phy_device *phydev)
425 {
426         u32 advertise;
427         int adv;
428         int err;
429
430         /* Only allow advertising what
431          * this PHY supports */
432         phydev->advertising &= phydev->supported;
433         advertise = phydev->advertising;
434
435         /* Setup standard advertisement */
436         adv = phy_read(phydev, MII_ADVERTISE);
437
438         if (adv < 0)
439                 return adv;
440
441         adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | 
442                  ADVERTISE_PAUSE_ASYM);
443         if (advertise & ADVERTISED_10baseT_Half)
444                 adv |= ADVERTISE_10HALF;
445         if (advertise & ADVERTISED_10baseT_Full)
446                 adv |= ADVERTISE_10FULL;
447         if (advertise & ADVERTISED_100baseT_Half)
448                 adv |= ADVERTISE_100HALF;
449         if (advertise & ADVERTISED_100baseT_Full)
450                 adv |= ADVERTISE_100FULL;
451         if (advertise & ADVERTISED_Pause)
452                 adv |= ADVERTISE_PAUSE_CAP;
453         if (advertise & ADVERTISED_Asym_Pause)
454                 adv |= ADVERTISE_PAUSE_ASYM;
455
456         err = phy_write(phydev, MII_ADVERTISE, adv);
457
458         if (err < 0)
459                 return err;
460
461         /* Configure gigabit if it's supported */
462         if (phydev->supported & (SUPPORTED_1000baseT_Half |
463                                 SUPPORTED_1000baseT_Full)) {
464                 adv = phy_read(phydev, MII_CTRL1000);
465
466                 if (adv < 0)
467                         return adv;
468
469                 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
470                 if (advertise & SUPPORTED_1000baseT_Half)
471                         adv |= ADVERTISE_1000HALF;
472                 if (advertise & SUPPORTED_1000baseT_Full)
473                         adv |= ADVERTISE_1000FULL;
474                 err = phy_write(phydev, MII_CTRL1000, adv);
475
476                 if (err < 0)
477                         return err;
478         }
479
480         return adv;
481 }
482 EXPORT_SYMBOL(genphy_config_advert);
483
484 /**
485  * genphy_setup_forced - configures/forces speed/duplex from @phydev
486  * @phydev: target phy_device struct
487  *
488  * Description: Configures MII_BMCR to force speed/duplex
489  *   to the values in phydev. Assumes that the values are valid.
490  *   Please see phy_sanitize_settings().
491  */
492 int genphy_setup_forced(struct phy_device *phydev)
493 {
494         int err;
495         int ctl = 0;
496
497         phydev->pause = phydev->asym_pause = 0;
498
499         if (SPEED_1000 == phydev->speed)
500                 ctl |= BMCR_SPEED1000;
501         else if (SPEED_100 == phydev->speed)
502                 ctl |= BMCR_SPEED100;
503
504         if (DUPLEX_FULL == phydev->duplex)
505                 ctl |= BMCR_FULLDPLX;
506         
507         err = phy_write(phydev, MII_BMCR, ctl);
508
509         if (err < 0)
510                 return err;
511
512         /*
513          * Run the fixups on this PHY, just in case the
514          * board code needs to change something after a reset
515          */
516         err = phy_scan_fixups(phydev);
517
518         if (err < 0)
519                 return err;
520
521         /* We just reset the device, so we'd better configure any
522          * settings the PHY requires to operate */
523         if (phydev->drv->config_init)
524                 err = phydev->drv->config_init(phydev);
525
526         return err;
527 }
528
529
530 /**
531  * genphy_restart_aneg - Enable and Restart Autonegotiation
532  * @phydev: target phy_device struct
533  */
534 int genphy_restart_aneg(struct phy_device *phydev)
535 {
536         int ctl;
537
538         ctl = phy_read(phydev, MII_BMCR);
539
540         if (ctl < 0)
541                 return ctl;
542
543         ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
544
545         /* Don't isolate the PHY if we're negotiating */
546         ctl &= ~(BMCR_ISOLATE);
547
548         ctl = phy_write(phydev, MII_BMCR, ctl);
549
550         return ctl;
551 }
552
553
554 /**
555  * genphy_config_aneg - restart auto-negotiation or write BMCR
556  * @phydev: target phy_device struct
557  *
558  * Description: If auto-negotiation is enabled, we configure the
559  *   advertising, and then restart auto-negotiation.  If it is not
560  *   enabled, then we write the BMCR.
561  */
562 int genphy_config_aneg(struct phy_device *phydev)
563 {
564         int err = 0;
565
566         if (AUTONEG_ENABLE == phydev->autoneg) {
567                 err = genphy_config_advert(phydev);
568
569                 if (err < 0)
570                         return err;
571
572                 err = genphy_restart_aneg(phydev);
573         } else
574                 err = genphy_setup_forced(phydev);
575
576         return err;
577 }
578 EXPORT_SYMBOL(genphy_config_aneg);
579
580 /**
581  * genphy_update_link - update link status in @phydev
582  * @phydev: target phy_device struct
583  *
584  * Description: Update the value in phydev->link to reflect the
585  *   current link value.  In order to do this, we need to read
586  *   the status register twice, keeping the second value.
587  */
588 int genphy_update_link(struct phy_device *phydev)
589 {
590         int status;
591
592         /* Do a fake read */
593         status = phy_read(phydev, MII_BMSR);
594
595         if (status < 0)
596                 return status;
597
598         /* Read link and autonegotiation status */
599         status = phy_read(phydev, MII_BMSR);
600
601         if (status < 0)
602                 return status;
603
604         if ((status & BMSR_LSTATUS) == 0)
605                 phydev->link = 0;
606         else
607                 phydev->link = 1;
608
609         return 0;
610 }
611 EXPORT_SYMBOL(genphy_update_link);
612
613 /**
614  * genphy_read_status - check the link status and update current link state
615  * @phydev: target phy_device struct
616  *
617  * Description: Check the link, then figure out the current state
618  *   by comparing what we advertise with what the link partner
619  *   advertises.  Start by checking the gigabit possibilities,
620  *   then move on to 10/100.
621  */
622 int genphy_read_status(struct phy_device *phydev)
623 {
624         int adv;
625         int err;
626         int lpa;
627         int lpagb = 0;
628
629         /* Update the link, but return if there
630          * was an error */
631         err = genphy_update_link(phydev);
632         if (err)
633                 return err;
634
635         if (AUTONEG_ENABLE == phydev->autoneg) {
636                 if (phydev->supported & (SUPPORTED_1000baseT_Half
637                                         | SUPPORTED_1000baseT_Full)) {
638                         lpagb = phy_read(phydev, MII_STAT1000);
639
640                         if (lpagb < 0)
641                                 return lpagb;
642
643                         adv = phy_read(phydev, MII_CTRL1000);
644
645                         if (adv < 0)
646                                 return adv;
647
648                         lpagb &= adv << 2;
649                 }
650
651                 lpa = phy_read(phydev, MII_LPA);
652
653                 if (lpa < 0)
654                         return lpa;
655
656                 adv = phy_read(phydev, MII_ADVERTISE);
657
658                 if (adv < 0)
659                         return adv;
660
661                 lpa &= adv;
662
663                 phydev->speed = SPEED_10;
664                 phydev->duplex = DUPLEX_HALF;
665                 phydev->pause = phydev->asym_pause = 0;
666
667                 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
668                         phydev->speed = SPEED_1000;
669
670                         if (lpagb & LPA_1000FULL)
671                                 phydev->duplex = DUPLEX_FULL;
672                 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
673                         phydev->speed = SPEED_100;
674                         
675                         if (lpa & LPA_100FULL)
676                                 phydev->duplex = DUPLEX_FULL;
677                 } else
678                         if (lpa & LPA_10FULL)
679                                 phydev->duplex = DUPLEX_FULL;
680
681                 if (phydev->duplex == DUPLEX_FULL){
682                         phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
683                         phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
684                 }
685         } else {
686                 int bmcr = phy_read(phydev, MII_BMCR);
687                 if (bmcr < 0)
688                         return bmcr;
689
690                 if (bmcr & BMCR_FULLDPLX)
691                         phydev->duplex = DUPLEX_FULL;
692                 else
693                         phydev->duplex = DUPLEX_HALF;
694
695                 if (bmcr & BMCR_SPEED1000)
696                         phydev->speed = SPEED_1000;
697                 else if (bmcr & BMCR_SPEED100)
698                         phydev->speed = SPEED_100;
699                 else
700                         phydev->speed = SPEED_10;
701
702                 phydev->pause = phydev->asym_pause = 0;
703         }
704
705         return 0;
706 }
707 EXPORT_SYMBOL(genphy_read_status);
708
709 static int genphy_config_init(struct phy_device *phydev)
710 {
711         int val;
712         u32 features;
713
714         /* For now, I'll claim that the generic driver supports
715          * all possible port types */
716         features = (SUPPORTED_TP | SUPPORTED_MII
717                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
718                         SUPPORTED_BNC);
719
720         /* Do we support autonegotiation? */
721         val = phy_read(phydev, MII_BMSR);
722
723         if (val < 0)
724                 return val;
725
726         if (val & BMSR_ANEGCAPABLE)
727                 features |= SUPPORTED_Autoneg;
728
729         if (val & BMSR_100FULL)
730                 features |= SUPPORTED_100baseT_Full;
731         if (val & BMSR_100HALF)
732                 features |= SUPPORTED_100baseT_Half;
733         if (val & BMSR_10FULL)
734                 features |= SUPPORTED_10baseT_Full;
735         if (val & BMSR_10HALF)
736                 features |= SUPPORTED_10baseT_Half;
737
738         if (val & BMSR_ESTATEN) {
739                 val = phy_read(phydev, MII_ESTATUS);
740
741                 if (val < 0)
742                         return val;
743
744                 if (val & ESTATUS_1000_TFULL)
745                         features |= SUPPORTED_1000baseT_Full;
746                 if (val & ESTATUS_1000_THALF)
747                         features |= SUPPORTED_1000baseT_Half;
748         }
749
750         phydev->supported = features;
751         phydev->advertising = features;
752
753         return 0;
754 }
755
756
757 /**
758  * phy_probe - probe and init a PHY device
759  * @dev: device to probe and init
760  *
761  * Description: Take care of setting up the phy_device structure,
762  *   set the state to READY (the driver's init function should
763  *   set it to STARTING if needed).
764  */
765 static int phy_probe(struct device *dev)
766 {
767         struct phy_device *phydev;
768         struct phy_driver *phydrv;
769         struct device_driver *drv;
770         int err = 0;
771
772         phydev = to_phy_device(dev);
773
774         /* Make sure the driver is held.
775          * XXX -- Is this correct? */
776         drv = get_driver(phydev->dev.driver);
777         phydrv = to_phy_driver(drv);
778         phydev->drv = phydrv;
779
780         /* Disable the interrupt if the PHY doesn't support it */
781         if (!(phydrv->flags & PHY_HAS_INTERRUPT))
782                 phydev->irq = PHY_POLL;
783
784         mutex_lock(&phydev->lock);
785
786         /* Start out supporting everything. Eventually,
787          * a controller will attach, and may modify one
788          * or both of these values */
789         phydev->supported = phydrv->features;
790         phydev->advertising = phydrv->features;
791
792         /* Set the state to READY by default */
793         phydev->state = PHY_READY;
794
795         if (phydev->drv->probe)
796                 err = phydev->drv->probe(phydev);
797
798         mutex_unlock(&phydev->lock);
799
800         return err;
801
802 }
803
804 static int phy_remove(struct device *dev)
805 {
806         struct phy_device *phydev;
807
808         phydev = to_phy_device(dev);
809
810         mutex_lock(&phydev->lock);
811         phydev->state = PHY_DOWN;
812         mutex_unlock(&phydev->lock);
813
814         if (phydev->drv->remove)
815                 phydev->drv->remove(phydev);
816
817         put_driver(dev->driver);
818         phydev->drv = NULL;
819
820         return 0;
821 }
822
823 /**
824  * phy_driver_register - register a phy_driver with the PHY layer
825  * @new_driver: new phy_driver to register
826  */
827 int phy_driver_register(struct phy_driver *new_driver)
828 {
829         int retval;
830
831         memset(&new_driver->driver, 0, sizeof(new_driver->driver));
832         new_driver->driver.name = new_driver->name;
833         new_driver->driver.bus = &mdio_bus_type;
834         new_driver->driver.probe = phy_probe;
835         new_driver->driver.remove = phy_remove;
836
837         retval = driver_register(&new_driver->driver);
838
839         if (retval) {
840                 printk(KERN_ERR "%s: Error %d in registering driver\n",
841                                 new_driver->name, retval);
842
843                 return retval;
844         }
845
846         pr_debug("%s: Registered new driver\n", new_driver->name);
847
848         return 0;
849 }
850 EXPORT_SYMBOL(phy_driver_register);
851
852 void phy_driver_unregister(struct phy_driver *drv)
853 {
854         driver_unregister(&drv->driver);
855 }
856 EXPORT_SYMBOL(phy_driver_unregister);
857
858 static struct phy_driver genphy_driver = {
859         .phy_id         = 0xffffffff,
860         .phy_id_mask    = 0xffffffff,
861         .name           = "Generic PHY",
862         .config_init    = genphy_config_init,
863         .features       = 0,
864         .config_aneg    = genphy_config_aneg,
865         .read_status    = genphy_read_status,
866         .driver         = {.owner= THIS_MODULE, },
867 };
868
869 static int __init phy_init(void)
870 {
871         int rc;
872
873         rc = mdio_bus_init();
874         if (rc)
875                 return rc;
876
877         rc = phy_driver_register(&genphy_driver);
878         if (rc)
879                 mdio_bus_exit();
880
881         return rc;
882 }
883
884 static void __exit phy_exit(void)
885 {
886         phy_driver_unregister(&genphy_driver);
887         mdio_bus_exit();
888 }
889
890 subsys_initcall(phy_init);
891 module_exit(phy_exit);