2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/ethernet.h>
31 #include <ipxe/usbnet.h>
36 * Davicom DM96xx USB Ethernet driver
40 /******************************************************************************
44 ******************************************************************************
50 * @v dm96xx DM96xx device
51 * @ret rc Return status code
53 static int dm96xx_reset ( struct dm96xx_device *dm96xx ) {
58 if ( ( rc = dm96xx_write_register ( dm96xx, DM96XX_NCR,
59 DM96XX_NCR_RST ) ) != 0 ) {
60 DBGC ( dm96xx, "DM96XX %p could not reset: %s\n",
61 dm96xx, strerror ( rc ) );
65 /* Wait for reset to complete */
66 udelay ( DM96XX_RESET_DELAY_US );
68 /* Check that reset has completed */
69 ncr = dm96xx_read_register ( dm96xx, DM96XX_NCR );
72 DBGC ( dm96xx, "DM96XX %p failed to reset: %s\n",
73 dm96xx, strerror ( rc ) );
76 if ( ncr & DM96XX_NCR_RST ) {
77 DBGC ( dm96xx, "DM96XX %p failed to reset (NCR=%#02x)\n",
88 * @v dm96xx DM96xx device
89 * @v mac MAC address to fill in
90 * @ret rc Return status code
92 static int dm96xx_read_mac ( struct dm96xx_device *dm96xx, uint8_t *mac ) {
95 /* Read MAC address */
96 if ( ( rc = dm96xx_read_registers ( dm96xx, DM96XX_PAR, mac,
98 DBGC ( dm96xx, "DM96XX %p could not read MAC address: %s\n",
99 dm96xx, strerror ( rc ) );
109 * @v dm96xx DM96xx device
111 * @ret rc Return status code
113 static int dm96xx_write_mac ( struct dm96xx_device *dm96xx, uint8_t *mac ) {
116 /* Write MAC address */
117 if ( ( rc = dm96xx_write_registers ( dm96xx, DM96XX_PAR, mac,
118 ETH_ALEN ) ) != 0 ) {
119 DBGC ( dm96xx, "DM96XX %p could not write MAC address: %s\n",
120 dm96xx, strerror ( rc ) );
128 * Update link status based on network status register
130 * @v dm96xx DM96xx device
131 * @v nsr Network status register
133 static void dm96xx_link_nsr ( struct dm96xx_device *dm96xx, unsigned int nsr ) {
134 struct net_device *netdev = dm96xx->netdev;
136 if ( nsr & DM96XX_NSR_LINKST ) {
137 if ( ! netdev_link_ok ( netdev ) )
138 netdev_link_up ( netdev );
140 if ( netdev_link_ok ( netdev ) )
141 netdev_link_down ( netdev );
148 * @v dm96xx DM96xx device
149 * @ret rc Return status code
151 static int dm96xx_check_link ( struct dm96xx_device *dm96xx ) {
155 /* Read network status register */
156 nsr = dm96xx_read_register ( dm96xx, DM96XX_NSR );
159 DBGC ( dm96xx, "DM96XX %p could not read network status: %s\n",
160 dm96xx, strerror ( rc ) );
164 /* Update link status */
165 dm96xx_link_nsr ( dm96xx, nsr );
171 * Set DM9601-compatible RX header mode
173 * @v dm96xx DM96xx device
174 * @ret rc Return status code
176 static int dm96xx_rx_mode ( struct dm96xx_device *dm96xx ) {
181 /* Get chip revision */
182 chipr = dm96xx_read_register ( dm96xx, DM96XX_CHIPR );
185 DBGC ( dm96xx, "DM96XX %p could not read chip revision: %s\n",
186 dm96xx, strerror ( rc ) );
190 /* Do nothing if device is a DM9601 anyway */
191 if ( chipr == DM96XX_CHIPR_9601 )
194 /* Read current mode control */
195 mode_ctl = dm96xx_read_register ( dm96xx, DM96XX_MODE_CTL );
196 if ( mode_ctl < 0 ) {
198 DBGC ( dm96xx, "DM96XX %p could not read mode control: %s\n",
199 dm96xx, strerror ( rc ) );
203 /* Write mode control */
204 mode_ctl &= ~DM96XX_MODE_CTL_MODE;
205 if ( ( rc = dm96xx_write_register ( dm96xx, DM96XX_MODE_CTL,
206 mode_ctl ) ) != 0 ) {
207 DBGC ( dm96xx, "DM96XX %p could not write mode control: %s\n",
208 dm96xx, strerror ( rc ) );
215 /******************************************************************************
217 * Endpoint operations
219 ******************************************************************************
223 * Complete interrupt transfer
226 * @v iobuf I/O buffer
227 * @v rc Completion status code
229 static void dm96xx_intr_complete ( struct usb_endpoint *ep,
230 struct io_buffer *iobuf, int rc ) {
231 struct dm96xx_device *dm96xx = container_of ( ep, struct dm96xx_device,
233 struct net_device *netdev = dm96xx->netdev;
234 struct dm96xx_interrupt *intr;
235 size_t len = iob_len ( iobuf );
237 /* Ignore packets cancelled when the endpoint closes */
241 /* Record USB errors against the network device */
243 DBGC ( dm96xx, "DM96XX %p interrupt failed: %s\n",
244 dm96xx, strerror ( rc ) );
245 DBGC_HDA ( dm96xx, 0, iobuf->data, iob_len ( iobuf ) );
246 netdev_rx_err ( netdev, NULL, rc );
250 /* Extract message header */
251 if ( len < sizeof ( *intr ) ) {
252 DBGC ( dm96xx, "DM96XX %p underlength interrupt:\n", dm96xx );
253 DBGC_HDA ( dm96xx, 0, iobuf->data, iob_len ( iobuf ) );
254 netdev_rx_err ( netdev, NULL, -EINVAL );
259 /* Update link status */
260 dm96xx_link_nsr ( dm96xx, intr->nsr );
263 /* Free I/O buffer */
267 /** Interrupt endpoint operations */
268 static struct usb_endpoint_driver_operations dm96xx_intr_operations = {
269 .complete = dm96xx_intr_complete,
273 * Complete bulk IN transfer
276 * @v iobuf I/O buffer
277 * @v rc Completion status code
279 static void dm96xx_in_complete ( struct usb_endpoint *ep,
280 struct io_buffer *iobuf, int rc ) {
281 struct dm96xx_device *dm96xx = container_of ( ep, struct dm96xx_device,
283 struct net_device *netdev = dm96xx->netdev;
284 struct dm96xx_rx_header *header;
286 /* Ignore packets cancelled when the endpoint closes */
292 /* Record USB errors against the network device */
294 DBGC ( dm96xx, "DM96XX %p bulk IN failed: %s\n",
295 dm96xx, strerror ( rc ) );
300 if ( iob_len ( iobuf ) < ( sizeof ( *header ) + 4 /* CRC */ ) ) {
301 DBGC ( dm96xx, "DM96XX %p underlength bulk IN\n", dm96xx );
302 DBGC_HDA ( dm96xx, 0, iobuf->data, iob_len ( iobuf ) );
307 /* Strip header and CRC */
308 header = iobuf->data;
309 iob_pull ( iobuf, sizeof ( *header ) );
310 iob_unput ( iobuf, 4 /* CRC */ );
313 if ( header->rsr & ~DM96XX_RSR_MF ) {
314 DBGC ( dm96xx, "DM96XX %p receive error %02x:\n",
315 dm96xx, header->rsr );
316 DBGC_HDA ( dm96xx, 0, iobuf->data, iob_len ( iobuf ) );
321 /* Hand off to network stack */
322 netdev_rx ( netdev, iob_disown ( iobuf ) );
326 /* Hand off to network stack */
327 netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
330 /** Bulk IN endpoint operations */
331 static struct usb_endpoint_driver_operations dm96xx_in_operations = {
332 .complete = dm96xx_in_complete,
338 * @v dm96xx DM96xx device
339 * @v iobuf I/O buffer
340 * @ret rc Return status code
342 static int dm96xx_out_transmit ( struct dm96xx_device *dm96xx,
343 struct io_buffer *iobuf ) {
344 struct dm96xx_tx_header *header;
345 size_t len = iob_len ( iobuf );
349 if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
351 header = iob_push ( iobuf, sizeof ( *header ) );
352 header->len = cpu_to_le16 ( len );
354 /* Enqueue I/O buffer */
355 if ( ( rc = usb_stream ( &dm96xx->usbnet.out, iobuf, 0 ) ) != 0 )
362 * Complete bulk OUT transfer
365 * @v iobuf I/O buffer
366 * @v rc Completion status code
368 static void dm96xx_out_complete ( struct usb_endpoint *ep,
369 struct io_buffer *iobuf, int rc ) {
370 struct dm96xx_device *dm96xx = container_of ( ep, struct dm96xx_device,
372 struct net_device *netdev = dm96xx->netdev;
374 /* Report TX completion */
375 netdev_tx_complete_err ( netdev, iobuf, rc );
378 /** Bulk OUT endpoint operations */
379 static struct usb_endpoint_driver_operations dm96xx_out_operations = {
380 .complete = dm96xx_out_complete,
383 /******************************************************************************
385 * Network device interface
387 ******************************************************************************
391 * Open network device
393 * @v netdev Network device
394 * @ret rc Return status code
396 static int dm96xx_open ( struct net_device *netdev ) {
397 struct dm96xx_device *dm96xx = netdev->priv;
401 /* Set DM9601-compatible RX header mode */
402 if ( ( rc = dm96xx_rx_mode ( dm96xx ) ) != 0 )
405 /* Write MAC address */
406 if ( ( rc = dm96xx_write_mac ( dm96xx, netdev->ll_addr ) ) != 0 )
409 /* Open USB network device */
410 if ( ( rc = usbnet_open ( &dm96xx->usbnet ) ) != 0 ) {
411 DBGC ( dm96xx, "DM96XX %p could not open: %s\n",
412 dm96xx, strerror ( rc ) );
416 /* Set receive filters */
417 rcr = ( DM96XX_RCR_ALL | DM96XX_RCR_RUNT | DM96XX_RCR_PRMSC |
419 if ( ( rc = dm96xx_write_register ( dm96xx, DM96XX_RCR, rcr ) ) != 0 ) {
420 DBGC ( dm96xx, "DM96XX %p could not write receive filters: "
421 "%s\n", dm96xx, strerror ( rc ) );
425 /* Update link status */
426 if ( ( rc = dm96xx_check_link ( dm96xx ) ) != 0 )
433 usbnet_close ( &dm96xx->usbnet );
441 * Close network device
443 * @v netdev Network device
445 static void dm96xx_close ( struct net_device *netdev ) {
446 struct dm96xx_device *dm96xx = netdev->priv;
448 /* Close USB network device */
449 usbnet_close ( &dm96xx->usbnet );
452 dm96xx_reset ( dm96xx );
458 * @v netdev Network device
459 * @v iobuf I/O buffer
460 * @ret rc Return status code
462 static int dm96xx_transmit ( struct net_device *netdev,
463 struct io_buffer *iobuf ) {
464 struct dm96xx_device *dm96xx = netdev->priv;
467 /* Transmit packet */
468 if ( ( rc = dm96xx_out_transmit ( dm96xx, iobuf ) ) != 0 )
475 * Poll for completed and received packets
477 * @v netdev Network device
479 static void dm96xx_poll ( struct net_device *netdev ) {
480 struct dm96xx_device *dm96xx = netdev->priv;
484 usb_poll ( dm96xx->bus );
486 /* Refill endpoints */
487 if ( ( rc = usbnet_refill ( &dm96xx->usbnet ) ) != 0 )
488 netdev_rx_err ( netdev, NULL, rc );
491 /** DM96xx network device operations */
492 static struct net_device_operations dm96xx_operations = {
494 .close = dm96xx_close,
495 .transmit = dm96xx_transmit,
499 /******************************************************************************
503 ******************************************************************************
509 * @v func USB function
510 * @v config Configuration descriptor
511 * @ret rc Return status code
513 static int dm96xx_probe ( struct usb_function *func,
514 struct usb_configuration_descriptor *config ) {
515 struct usb_device *usb = func->usb;
516 struct net_device *netdev;
517 struct dm96xx_device *dm96xx;
520 /* Allocate and initialise structure */
521 netdev = alloc_etherdev ( sizeof ( *dm96xx ) );
526 netdev_init ( netdev, &dm96xx_operations );
527 netdev->dev = &func->dev;
528 dm96xx = netdev->priv;
529 memset ( dm96xx, 0, sizeof ( *dm96xx ) );
531 dm96xx->bus = usb->port->hub->bus;
532 dm96xx->netdev = netdev;
533 usbnet_init ( &dm96xx->usbnet, func, &dm96xx_intr_operations,
534 &dm96xx_in_operations, &dm96xx_out_operations );
535 usb_refill_init ( &dm96xx->usbnet.intr, 0, DM96XX_INTR_MAX_FILL );
536 usb_refill_init ( &dm96xx->usbnet.in, DM96XX_IN_MTU,
537 DM96XX_IN_MAX_FILL );
538 DBGC ( dm96xx, "DM96XX %p on %s\n", dm96xx, func->name );
540 /* Describe USB network device */
541 if ( ( rc = usbnet_describe ( &dm96xx->usbnet, config ) ) != 0 ) {
542 DBGC ( dm96xx, "DM96XX %p could not describe: %s\n",
543 dm96xx, strerror ( rc ) );
548 if ( ( rc = dm96xx_reset ( dm96xx ) ) != 0 )
551 /* Read MAC address */
552 if ( ( rc = dm96xx_read_mac ( dm96xx, netdev->hw_addr ) ) != 0 )
555 /* Get initial link status */
556 if ( ( rc = dm96xx_check_link ( dm96xx ) ) != 0 )
559 /* Register network device */
560 if ( ( rc = register_netdev ( netdev ) ) != 0 )
563 usb_func_set_drvdata ( func, netdev );
566 unregister_netdev ( netdev );
572 netdev_nullify ( netdev );
573 netdev_put ( netdev );
581 * @v func USB function
583 static void dm96xx_remove ( struct usb_function *func ) {
584 struct net_device *netdev = usb_func_get_drvdata ( func );
586 unregister_netdev ( netdev );
587 netdev_nullify ( netdev );
588 netdev_put ( netdev );
591 /** DM96xx device IDs */
592 static struct usb_device_id dm96xx_ids[] = {
594 .name = "dm9601-corega",
619 .name = "dm9601-hirose",
624 .name = "dm9601-8101",
629 .name = "dm9601-9700",
665 /** Davicom DM96xx driver */
666 struct usb_driver dm96xx_driver __usb_driver = {
668 .id_count = ( sizeof ( dm96xx_ids ) / sizeof ( dm96xx_ids[0] ) ),
669 .probe = dm96xx_probe,
670 .remove = dm96xx_remove,