linux/drivers/net/caif/caif_spi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) ST-Ericsson AB 2010
   4 * Author:  Daniel Martensson
   5 */
   6
   7#include <linux/init.h>
   8#include <linux/module.h>
   9#include <linux/device.h>
  10#include <linux/platform_device.h>
  11#include <linux/string.h>
  12#include <linux/workqueue.h>
  13#include <linux/completion.h>
  14#include <linux/list.h>
  15#include <linux/interrupt.h>
  16#include <linux/dma-mapping.h>
  17#include <linux/delay.h>
  18#include <linux/sched.h>
  19#include <linux/debugfs.h>
  20#include <linux/if_arp.h>
  21#include <net/caif/caif_layer.h>
  22#include <net/caif/caif_spi.h>
  23
  24#ifndef CONFIG_CAIF_SPI_SYNC
  25#define FLAVOR "Flavour: Vanilla.\n"
  26#else
  27#define FLAVOR "Flavour: Master CMD&LEN at start.\n"
  28#endif /* CONFIG_CAIF_SPI_SYNC */
  29
  30MODULE_LICENSE("GPL");
  31MODULE_AUTHOR("Daniel Martensson");
  32MODULE_DESCRIPTION("CAIF SPI driver");
  33
  34/* Returns the number of padding bytes for alignment. */
  35#define PAD_POW2(x, pow) ((((x)&((pow)-1))==0) ? 0 : (((pow)-((x)&((pow)-1)))))
  36
  37static bool spi_loop;
  38module_param(spi_loop, bool, 0444);
  39MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode.");
  40
  41/* SPI frame alignment. */
  42module_param(spi_frm_align, int, 0444);
  43MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment.");
  44
  45/*
  46 * SPI padding options.
  47 * Warning: must be a base of 2 (& operation used) and can not be zero !
  48 */
  49module_param(spi_up_head_align, int, 0444);
  50MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment.");
  51
  52module_param(spi_up_tail_align, int, 0444);
  53MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment.");
  54
  55module_param(spi_down_head_align, int, 0444);
  56MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment.");
  57
  58module_param(spi_down_tail_align, int, 0444);
  59MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment.");
  60
  61#ifdef CONFIG_ARM
  62#define BYTE_HEX_FMT "%02X"
  63#else
  64#define BYTE_HEX_FMT "%02hhX"
  65#endif
  66
  67#define SPI_MAX_PAYLOAD_SIZE 4096
  68/*
  69 * Threshold values for the SPI packet queue. Flowcontrol will be asserted
  70 * when the number of packets exceeds HIGH_WATER_MARK. It will not be
  71 * deasserted before the number of packets drops below LOW_WATER_MARK.
  72 */
  73#define LOW_WATER_MARK   100
  74#define HIGH_WATER_MARK  (LOW_WATER_MARK*5)
  75
  76#ifndef CONFIG_HAS_DMA
  77
  78/*
  79 * We sometimes use UML for debugging, but it cannot handle
  80 * dma_alloc_coherent so we have to wrap it.
  81 */
  82static inline void *dma_alloc(struct cfspi *cfspi, dma_addr_t *daddr)
  83{
  84        return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL);
  85}
  86
  87static inline void dma_free(struct cfspi *cfspi, void *cpu_addr,
  88                dma_addr_t handle)
  89{
  90        kfree(cpu_addr);
  91}
  92
  93#else
  94
  95static inline void *dma_alloc(struct cfspi *cfspi, dma_addr_t *daddr)
  96{
  97        return dma_alloc_coherent(&cfspi->pdev->dev, SPI_DMA_BUF_LEN, daddr,
  98                                GFP_KERNEL);
  99}
 100
 101static inline void dma_free(struct cfspi *cfspi, void *cpu_addr,
 102                dma_addr_t handle)
 103{
 104        dma_free_coherent(&cfspi->pdev->dev, SPI_DMA_BUF_LEN, cpu_addr, handle);
 105}
 106#endif  /* CONFIG_HAS_DMA */
 107
 108#ifdef CONFIG_DEBUG_FS
 109
 110#define DEBUGFS_BUF_SIZE        4096
 111
 112static struct dentry *dbgfs_root;
 113
 114static inline void driver_debugfs_create(void)
 115{
 116        dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL);
 117}
 118
 119static inline void driver_debugfs_remove(void)
 120{
 121        debugfs_remove(dbgfs_root);
 122}
 123
 124static inline void dev_debugfs_rem(struct cfspi *cfspi)
 125{
 126        debugfs_remove(cfspi->dbgfs_frame);
 127        debugfs_remove(cfspi->dbgfs_state);
 128        debugfs_remove(cfspi->dbgfs_dir);
 129}
 130
 131static ssize_t dbgfs_state(struct file *file, char __user *user_buf,
 132                           size_t count, loff_t *ppos)
 133{
 134        char *buf;
 135        int len = 0;
 136        ssize_t size;
 137        struct cfspi *cfspi = file->private_data;
 138
 139        buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
 140        if (!buf)
 141                return 0;
 142
 143        /* Print out debug information. */
 144        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 145                         "CAIF SPI debug information:\n");
 146
 147        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR);
 148
 149        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 150                         "STATE: %d\n", cfspi->dbg_state);
 151        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 152                         "Previous CMD: 0x%x\n", cfspi->pcmd);
 153        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 154                         "Current CMD: 0x%x\n", cfspi->cmd);
 155        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 156                         "Previous TX len: %d\n", cfspi->tx_ppck_len);
 157        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 158                         "Previous RX len: %d\n", cfspi->rx_ppck_len);
 159        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 160                         "Current TX len: %d\n", cfspi->tx_cpck_len);
 161        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 162                         "Current RX len: %d\n", cfspi->rx_cpck_len);
 163        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 164                         "Next TX len: %d\n", cfspi->tx_npck_len);
 165        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 166                         "Next RX len: %d\n", cfspi->rx_npck_len);
 167
 168        if (len > DEBUGFS_BUF_SIZE)
 169                len = DEBUGFS_BUF_SIZE;
 170
 171        size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
 172        kfree(buf);
 173
 174        return size;
 175}
 176
 177static ssize_t print_frame(char *buf, size_t size, char *frm,
 178                           size_t count, size_t cut)
 179{
 180        int len = 0;
 181        int i;
 182        for (i = 0; i < count; i++) {
 183                len += scnprintf((buf + len), (size - len),
 184                                        "[0x" BYTE_HEX_FMT "]",
 185                                        frm[i]);
 186                if ((i == cut) && (count > (cut * 2))) {
 187                        /* Fast forward. */
 188                        i = count - cut;
 189                        len += scnprintf((buf + len), (size - len),
 190                                         "--- %zu bytes skipped ---\n",
 191                                         count - (cut * 2));
 192                }
 193
 194                if ((!(i % 10)) && i) {
 195                        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 196                                         "\n");
 197                }
 198        }
 199        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n");
 200        return len;
 201}
 202
 203static ssize_t dbgfs_frame(struct file *file, char __user *user_buf,
 204                           size_t count, loff_t *ppos)
 205{
 206        char *buf;
 207        int len = 0;
 208        ssize_t size;
 209        struct cfspi *cfspi;
 210
 211        cfspi = file->private_data;
 212        buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL);
 213        if (!buf)
 214                return 0;
 215
 216        /* Print out debug information. */
 217        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 218                         "Current frame:\n");
 219
 220        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 221                         "Tx data (Len: %d):\n", cfspi->tx_cpck_len);
 222
 223        len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
 224                           cfspi->xfer.va_tx[0],
 225                           (cfspi->tx_cpck_len + SPI_CMD_SZ), 100);
 226
 227        len += scnprintf((buf + len), (DEBUGFS_BUF_SIZE - len),
 228                         "Rx data (Len: %d):\n", cfspi->rx_cpck_len);
 229
 230        len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len),
 231                           cfspi->xfer.va_rx,
 232                           (cfspi->rx_cpck_len + SPI_CMD_SZ), 100);
 233
 234        size = simple_read_from_buffer(user_buf, count, ppos, buf, len);
 235        kfree(buf);
 236
 237        return size;
 238}
 239
 240static const struct file_operations dbgfs_state_fops = {
 241        .open = simple_open,
 242        .read = dbgfs_state,
 243        .owner = THIS_MODULE
 244};
 245
 246static const struct file_operations dbgfs_frame_fops = {
 247        .open = simple_open,
 248        .read = dbgfs_frame,
 249        .owner = THIS_MODULE
 250};
 251
 252static inline void dev_debugfs_add(struct cfspi *cfspi)
 253{
 254        cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root);
 255        cfspi->dbgfs_state = debugfs_create_file("state", 0444,
 256                                                 cfspi->dbgfs_dir, cfspi,
 257                                                 &dbgfs_state_fops);
 258        cfspi->dbgfs_frame = debugfs_create_file("frame", 0444,
 259                                                 cfspi->dbgfs_dir, cfspi,
 260                                                 &dbgfs_frame_fops);
 261}
 262
 263inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
 264{
 265        cfspi->dbg_state = state;
 266};
 267#else
 268
 269static inline void driver_debugfs_create(void)
 270{
 271}
 272
 273static inline void driver_debugfs_remove(void)
 274{
 275}
 276
 277static inline void dev_debugfs_add(struct cfspi *cfspi)
 278{
 279}
 280
 281static inline void dev_debugfs_rem(struct cfspi *cfspi)
 282{
 283}
 284
 285inline void cfspi_dbg_state(struct cfspi *cfspi, int state)
 286{
 287}
 288#endif                          /* CONFIG_DEBUG_FS */
 289
 290static LIST_HEAD(cfspi_list);
 291static spinlock_t cfspi_list_lock;
 292
 293/* SPI uplink head alignment. */
 294static ssize_t up_head_align_show(struct device_driver *driver, char *buf)
 295{
 296        return sprintf(buf, "%d\n", spi_up_head_align);
 297}
 298
 299static DRIVER_ATTR_RO(up_head_align);
 300
 301/* SPI uplink tail alignment. */
 302static ssize_t up_tail_align_show(struct device_driver *driver, char *buf)
 303{
 304        return sprintf(buf, "%d\n", spi_up_tail_align);
 305}
 306
 307static DRIVER_ATTR_RO(up_tail_align);
 308
 309/* SPI downlink head alignment. */
 310static ssize_t down_head_align_show(struct device_driver *driver, char *buf)
 311{
 312        return sprintf(buf, "%d\n", spi_down_head_align);
 313}
 314
 315static DRIVER_ATTR_RO(down_head_align);
 316
 317/* SPI downlink tail alignment. */
 318static ssize_t down_tail_align_show(struct device_driver *driver, char *buf)
 319{
 320        return sprintf(buf, "%d\n", spi_down_tail_align);
 321}
 322
 323static DRIVER_ATTR_RO(down_tail_align);
 324
 325/* SPI frame alignment. */
 326static ssize_t frame_align_show(struct device_driver *driver, char *buf)
 327{
 328        return sprintf(buf, "%d\n", spi_frm_align);
 329}
 330
 331static DRIVER_ATTR_RO(frame_align);
 332
 333int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len)
 334{
 335        u8 *dst = buf;
 336        caif_assert(buf);
 337
 338        if (cfspi->slave && !cfspi->slave_talked)
 339                cfspi->slave_talked = true;
 340
 341        do {
 342                struct sk_buff *skb;
 343                struct caif_payload_info *info;
 344                int spad = 0;
 345                int epad;
 346
 347                skb = skb_dequeue(&cfspi->chead);
 348                if (!skb)
 349                        break;
 350
 351                /*
 352                 * Calculate length of frame including SPI padding.
 353                 * The payload position is found in the control buffer.
 354                 */
 355                info = (struct caif_payload_info *)&skb->cb;
 356
 357                /*
 358                 * Compute head offset i.e. number of bytes to add to
 359                 * get the start of the payload aligned.
 360                 */
 361                if (spi_up_head_align > 1) {
 362                        spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
 363                        *dst = (u8)(spad - 1);
 364                        dst += spad;
 365                }
 366
 367                /* Copy in CAIF frame. */
 368                skb_copy_bits(skb, 0, dst, skb->len);
 369                dst += skb->len;
 370                cfspi->ndev->stats.tx_packets++;
 371                cfspi->ndev->stats.tx_bytes += skb->len;
 372
 373                /*
 374                 * Compute tail offset i.e. number of bytes to add to
 375                 * get the complete CAIF frame aligned.
 376                 */
 377                epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
 378                dst += epad;
 379
 380                dev_kfree_skb(skb);
 381
 382        } while ((dst - buf) < len);
 383
 384        return dst - buf;
 385}
 386
 387int cfspi_xmitlen(struct cfspi *cfspi)
 388{
 389        struct sk_buff *skb = NULL;
 390        int frm_len = 0;
 391        int pkts = 0;
 392
 393        /*
 394         * Decommit previously committed frames.
 395         * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead)
 396         */
 397        while (skb_peek(&cfspi->chead)) {
 398                skb = skb_dequeue_tail(&cfspi->chead);
 399                skb_queue_head(&cfspi->qhead, skb);
 400        }
 401
 402        do {
 403                struct caif_payload_info *info = NULL;
 404                int spad = 0;
 405                int epad = 0;
 406
 407                skb = skb_dequeue(&cfspi->qhead);
 408                if (!skb)
 409                        break;
 410
 411                /*
 412                 * Calculate length of frame including SPI padding.
 413                 * The payload position is found in the control buffer.
 414                 */
 415                info = (struct caif_payload_info *)&skb->cb;
 416
 417                /*
 418                 * Compute head offset i.e. number of bytes to add to
 419                 * get the start of the payload aligned.
 420                 */
 421                if (spi_up_head_align > 1)
 422                        spad = 1 + PAD_POW2((info->hdr_len + 1), spi_up_head_align);
 423
 424                /*
 425                 * Compute tail offset i.e. number of bytes to add to
 426                 * get the complete CAIF frame aligned.
 427                 */
 428                epad = PAD_POW2((skb->len + spad), spi_up_tail_align);
 429
 430                if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) {
 431                        skb_queue_tail(&cfspi->chead, skb);
 432                        pkts++;
 433                        frm_len += skb->len + spad + epad;
 434                } else {
 435                        /* Put back packet. */
 436                        skb_queue_head(&cfspi->qhead, skb);
 437                        break;
 438                }
 439        } while (pkts <= CAIF_MAX_SPI_PKTS);
 440
 441        /*
 442         * Send flow on if previously sent flow off
 443         * and now go below the low water mark
 444         */
 445        if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark &&
 446                cfspi->cfdev.flowctrl) {
 447                cfspi->flow_off_sent = 0;
 448                cfspi->cfdev.flowctrl(cfspi->ndev, 1);
 449        }
 450
 451        return frm_len;
 452}
 453
 454static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc)
 455{
 456        struct cfspi *cfspi = (struct cfspi *)ifc->priv;
 457
 458        /*
 459         * The slave device is the master on the link. Interrupts before the
 460         * slave has transmitted are considered spurious.
 461         */
 462        if (cfspi->slave && !cfspi->slave_talked) {
 463                printk(KERN_WARNING "CFSPI: Spurious SS interrupt.\n");
 464                return;
 465        }
 466
 467        if (!in_interrupt())
 468                spin_lock(&cfspi->lock);
 469        if (assert) {
 470                set_bit(SPI_SS_ON, &cfspi->state);
 471                set_bit(SPI_XFER, &cfspi->state);
 472        } else {
 473                set_bit(SPI_SS_OFF, &cfspi->state);
 474        }
 475        if (!in_interrupt())
 476                spin_unlock(&cfspi->lock);
 477
 478        /* Wake up the xfer thread. */
 479        if (assert)
 480                wake_up_interruptible(&cfspi->wait);
 481}
 482
 483static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc)
 484{
 485        struct cfspi *cfspi = (struct cfspi *)ifc->priv;
 486
 487        /* Transfer done, complete work queue */
 488        complete(&cfspi->comp);
 489}
 490
 491static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev)
 492{
 493        struct cfspi *cfspi = NULL;
 494        unsigned long flags;
 495        if (!dev)
 496                return -EINVAL;
 497
 498        cfspi = netdev_priv(dev);
 499
 500        skb_queue_tail(&cfspi->qhead, skb);
 501
 502        spin_lock_irqsave(&cfspi->lock, flags);
 503        if (!test_and_set_bit(SPI_XFER, &cfspi->state)) {
 504                /* Wake up xfer thread. */
 505                wake_up_interruptible(&cfspi->wait);
 506        }
 507        spin_unlock_irqrestore(&cfspi->lock, flags);
 508
 509        /* Send flow off if number of bytes is above high water mark */
 510        if (!cfspi->flow_off_sent &&
 511                cfspi->qhead.qlen > cfspi->qd_high_mark &&
 512                cfspi->cfdev.flowctrl) {
 513                cfspi->flow_off_sent = 1;
 514                cfspi->cfdev.flowctrl(cfspi->ndev, 0);
 515        }
 516
 517        return 0;
 518}
 519
 520int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len)
 521{
 522        u8 *src = buf;
 523
 524        caif_assert(buf != NULL);
 525
 526        do {
 527                int res;
 528                struct sk_buff *skb = NULL;
 529                int spad = 0;
 530                int epad = 0;
 531                int pkt_len = 0;
 532
 533                /*
 534                 * Compute head offset i.e. number of bytes added to
 535                 * get the start of the payload aligned.
 536                 */
 537                if (spi_down_head_align > 1) {
 538                        spad = 1 + *src;
 539                        src += spad;
 540                }
 541
 542                /* Read length of CAIF frame (little endian). */
 543                pkt_len = *src;
 544                pkt_len |= ((*(src+1)) << 8) & 0xFF00;
 545                pkt_len += 2;   /* Add FCS fields. */
 546
 547                /* Get a suitable caif packet and copy in data. */
 548
 549                skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1);
 550                caif_assert(skb != NULL);
 551
 552                skb_put_data(skb, src, pkt_len);
 553                src += pkt_len;
 554
 555                skb->protocol = htons(ETH_P_CAIF);
 556                skb_reset_mac_header(skb);
 557
 558                /*
 559                 * Push received packet up the stack.
 560                 */
 561                if (!spi_loop)
 562                        res = netif_rx_ni(skb);
 563                else
 564                        res = cfspi_xmit(skb, cfspi->ndev);
 565
 566                if (!res) {
 567                        cfspi->ndev->stats.rx_packets++;
 568                        cfspi->ndev->stats.rx_bytes += pkt_len;
 569                } else
 570                        cfspi->ndev->stats.rx_dropped++;
 571
 572                /*
 573                 * Compute tail offset i.e. number of bytes added to
 574                 * get the complete CAIF frame aligned.
 575                 */
 576                epad = PAD_POW2((pkt_len + spad), spi_down_tail_align);
 577                src += epad;
 578        } while ((src - buf) < len);
 579
 580        return src - buf;
 581}
 582
 583static int cfspi_open(struct net_device *dev)
 584{
 585        netif_wake_queue(dev);
 586        return 0;
 587}
 588
 589static int cfspi_close(struct net_device *dev)
 590{
 591        netif_stop_queue(dev);
 592        return 0;
 593}
 594
 595static int cfspi_init(struct net_device *dev)
 596{
 597        int res = 0;
 598        struct cfspi *cfspi = netdev_priv(dev);
 599
 600        /* Set flow info. */
 601        cfspi->flow_off_sent = 0;
 602        cfspi->qd_low_mark = LOW_WATER_MARK;
 603        cfspi->qd_high_mark = HIGH_WATER_MARK;
 604
 605        /* Set slave info. */
 606        if (!strncmp(cfspi_spi_driver.driver.name, "cfspi_sspi", 10)) {
 607                cfspi->slave = true;
 608                cfspi->slave_talked = false;
 609        } else {
 610                cfspi->slave = false;
 611                cfspi->slave_talked = false;
 612        }
 613
 614        /* Allocate DMA buffers. */
 615        cfspi->xfer.va_tx[0] = dma_alloc(cfspi, &cfspi->xfer.pa_tx[0]);
 616        if (!cfspi->xfer.va_tx[0]) {
 617                res = -ENODEV;
 618                goto err_dma_alloc_tx_0;
 619        }
 620
 621        cfspi->xfer.va_rx = dma_alloc(cfspi, &cfspi->xfer.pa_rx);
 622
 623        if (!cfspi->xfer.va_rx) {
 624                res = -ENODEV;
 625                goto err_dma_alloc_rx;
 626        }
 627
 628        /* Initialize the work queue. */
 629        INIT_WORK(&cfspi->work, cfspi_xfer);
 630
 631        /* Initialize spin locks. */
 632        spin_lock_init(&cfspi->lock);
 633
 634        /* Initialize flow control state. */
 635        cfspi->flow_stop = false;
 636
 637        /* Initialize wait queue. */
 638        init_waitqueue_head(&cfspi->wait);
 639
 640        /* Create work thread. */
 641        cfspi->wq = create_singlethread_workqueue(dev->name);
 642        if (!cfspi->wq) {
 643                printk(KERN_WARNING "CFSPI: failed to create work queue.\n");
 644                res = -ENODEV;
 645                goto err_create_wq;
 646        }
 647
 648        /* Initialize work queue. */
 649        init_completion(&cfspi->comp);
 650
 651        /* Create debugfs entries. */
 652        dev_debugfs_add(cfspi);
 653
 654        /* Set up the ifc. */
 655        cfspi->ifc.ss_cb = cfspi_ss_cb;
 656        cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb;
 657        cfspi->ifc.priv = cfspi;
 658
 659        /* Add CAIF SPI device to list. */
 660        spin_lock(&cfspi_list_lock);
 661        list_add_tail(&cfspi->list, &cfspi_list);
 662        spin_unlock(&cfspi_list_lock);
 663
 664        /* Schedule the work queue. */
 665        queue_work(cfspi->wq, &cfspi->work);
 666
 667        return 0;
 668
 669 err_create_wq:
 670        dma_free(cfspi, cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
 671 err_dma_alloc_rx:
 672        dma_free(cfspi, cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
 673 err_dma_alloc_tx_0:
 674        return res;
 675}
 676
 677static void cfspi_uninit(struct net_device *dev)
 678{
 679        struct cfspi *cfspi = netdev_priv(dev);
 680
 681        /* Remove from list. */
 682        spin_lock(&cfspi_list_lock);
 683        list_del(&cfspi->list);
 684        spin_unlock(&cfspi_list_lock);
 685
 686        cfspi->ndev = NULL;
 687        /* Free DMA buffers. */
 688        dma_free(cfspi, cfspi->xfer.va_rx, cfspi->xfer.pa_rx);
 689        dma_free(cfspi, cfspi->xfer.va_tx[0], cfspi->xfer.pa_tx[0]);
 690        set_bit(SPI_TERMINATE, &cfspi->state);
 691        wake_up_interruptible(&cfspi->wait);
 692        destroy_workqueue(cfspi->wq);
 693        /* Destroy debugfs directory and files. */
 694        dev_debugfs_rem(cfspi);
 695        return;
 696}
 697
 698static const struct net_device_ops cfspi_ops = {
 699        .ndo_open = cfspi_open,
 700        .ndo_stop = cfspi_close,
 701        .ndo_init = cfspi_init,
 702        .ndo_uninit = cfspi_uninit,
 703        .ndo_start_xmit = cfspi_xmit
 704};
 705
 706static void cfspi_setup(struct net_device *dev)
 707{
 708        struct cfspi *cfspi = netdev_priv(dev);
 709        dev->features = 0;
 710        dev->netdev_ops = &cfspi_ops;
 711        dev->type = ARPHRD_CAIF;
 712        dev->flags = IFF_NOARP | IFF_POINTOPOINT;
 713        dev->priv_flags |= IFF_NO_QUEUE;
 714        dev->mtu = SPI_MAX_PAYLOAD_SIZE;
 715        dev->needs_free_netdev = true;
 716        skb_queue_head_init(&cfspi->qhead);
 717        skb_queue_head_init(&cfspi->chead);
 718        cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
 719        cfspi->cfdev.use_frag = false;
 720        cfspi->cfdev.use_stx = false;
 721        cfspi->cfdev.use_fcs = false;
 722        cfspi->ndev = dev;
 723}
 724
 725int cfspi_spi_probe(struct platform_device *pdev)
 726{
 727        struct cfspi *cfspi = NULL;
 728        struct net_device *ndev;
 729        struct cfspi_dev *dev;
 730        int res;
 731        dev = (struct cfspi_dev *)pdev->dev.platform_data;
 732
 733        if (!dev)
 734                return -ENODEV;
 735
 736        ndev = alloc_netdev(sizeof(struct cfspi), "cfspi%d",
 737                            NET_NAME_UNKNOWN, cfspi_setup);
 738        if (!ndev)
 739                return -ENOMEM;
 740
 741        cfspi = netdev_priv(ndev);
 742        netif_stop_queue(ndev);
 743        cfspi->ndev = ndev;
 744        cfspi->pdev = pdev;
 745
 746        /* Assign the SPI device. */
 747        cfspi->dev = dev;
 748        /* Assign the device ifc to this SPI interface. */
 749        dev->ifc = &cfspi->ifc;
 750
 751        /* Register network device. */
 752        res = register_netdev(ndev);
 753        if (res) {
 754                printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res);
 755                goto err_net_reg;
 756        }
 757        return res;
 758
 759 err_net_reg:
 760        free_netdev(ndev);
 761
 762        return res;
 763}
 764
 765int cfspi_spi_remove(struct platform_device *pdev)
 766{
 767        /* Everything is done in cfspi_uninit(). */
 768        return 0;
 769}
 770
 771static void __exit cfspi_exit_module(void)
 772{
 773        struct list_head *list_node;
 774        struct list_head *n;
 775        struct cfspi *cfspi = NULL;
 776
 777        list_for_each_safe(list_node, n, &cfspi_list) {
 778                cfspi = list_entry(list_node, struct cfspi, list);
 779                unregister_netdev(cfspi->ndev);
 780        }
 781
 782        /* Destroy sysfs files. */
 783        driver_remove_file(&cfspi_spi_driver.driver,
 784                           &driver_attr_up_head_align);
 785        driver_remove_file(&cfspi_spi_driver.driver,
 786                           &driver_attr_up_tail_align);
 787        driver_remove_file(&cfspi_spi_driver.driver,
 788                           &driver_attr_down_head_align);
 789        driver_remove_file(&cfspi_spi_driver.driver,
 790                           &driver_attr_down_tail_align);
 791        driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align);
 792        /* Unregister platform driver. */
 793        platform_driver_unregister(&cfspi_spi_driver);
 794        /* Destroy debugfs root directory. */
 795        driver_debugfs_remove();
 796}
 797
 798static int __init cfspi_init_module(void)
 799{
 800        int result;
 801
 802        /* Initialize spin lock. */
 803        spin_lock_init(&cfspi_list_lock);
 804
 805        /* Register platform driver. */
 806        result = platform_driver_register(&cfspi_spi_driver);
 807        if (result) {
 808                printk(KERN_ERR "Could not register platform SPI driver.\n");
 809                goto err_dev_register;
 810        }
 811
 812        /* Create sysfs files. */
 813        result =
 814            driver_create_file(&cfspi_spi_driver.driver,
 815                               &driver_attr_up_head_align);
 816        if (result) {
 817                printk(KERN_ERR "Sysfs creation failed 1.\n");
 818                goto err_create_up_head_align;
 819        }
 820
 821        result =
 822            driver_create_file(&cfspi_spi_driver.driver,
 823                               &driver_attr_up_tail_align);
 824        if (result) {
 825                printk(KERN_ERR "Sysfs creation failed 2.\n");
 826                goto err_create_up_tail_align;
 827        }
 828
 829        result =
 830            driver_create_file(&cfspi_spi_driver.driver,
 831                               &driver_attr_down_head_align);
 832        if (result) {
 833                printk(KERN_ERR "Sysfs creation failed 3.\n");
 834                goto err_create_down_head_align;
 835        }
 836
 837        result =
 838            driver_create_file(&cfspi_spi_driver.driver,
 839                               &driver_attr_down_tail_align);
 840        if (result) {
 841                printk(KERN_ERR "Sysfs creation failed 4.\n");
 842                goto err_create_down_tail_align;
 843        }
 844
 845        result =
 846            driver_create_file(&cfspi_spi_driver.driver,
 847                               &driver_attr_frame_align);
 848        if (result) {
 849                printk(KERN_ERR "Sysfs creation failed 5.\n");
 850                goto err_create_frame_align;
 851        }
 852        driver_debugfs_create();
 853        return result;
 854
 855 err_create_frame_align:
 856        driver_remove_file(&cfspi_spi_driver.driver,
 857                           &driver_attr_down_tail_align);
 858 err_create_down_tail_align:
 859        driver_remove_file(&cfspi_spi_driver.driver,
 860                           &driver_attr_down_head_align);
 861 err_create_down_head_align:
 862        driver_remove_file(&cfspi_spi_driver.driver,
 863                           &driver_attr_up_tail_align);
 864 err_create_up_tail_align:
 865        driver_remove_file(&cfspi_spi_driver.driver,
 866                           &driver_attr_up_head_align);
 867 err_create_up_head_align:
 868        platform_driver_unregister(&cfspi_spi_driver);
 869 err_dev_register:
 870        return result;
 871}
 872
 873module_init(cfspi_init_module);
 874module_exit(cfspi_exit_module);
 875