Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-2.6
[linux-2.6] / drivers / net / wireless / p54 / p54spi.c
1 /*
2  * Copyright (C) 2008 Christian Lamparter <chunkeey@web.de>
3  * Copyright 2008       Johannes Berg <johannes@sipsolutions.net>
4  *
5  * This driver is a port from stlc45xx:
6  *      Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/interrupt.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/irq.h>
29 #include <linux/spi/spi.h>
30 #include <linux/etherdevice.h>
31 #include <linux/gpio.h>
32
33 #include "p54spi.h"
34 #include "p54spi_eeprom.h"
35 #include "p54.h"
36
37 #include "p54common.h"
38
39 MODULE_FIRMWARE("3826.arm");
40 MODULE_ALIAS("stlc45xx");
41
42 /*
43  * gpios should be handled in board files and provided via platform data,
44  * but because it's currently impossible for p54spi to have a header file
45  * in include/linux, let's use module paramaters for now
46  */
47
48 static int p54spi_gpio_power = 97;
49 module_param(p54spi_gpio_power, int, 0444);
50 MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
51
52 static int p54spi_gpio_irq = 87;
53 module_param(p54spi_gpio_irq, int, 0444);
54 MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
55
56 static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
57                               void *buf, size_t len)
58 {
59         struct spi_transfer t[2];
60         struct spi_message m;
61         __le16 addr;
62
63         /* We first push the address */
64         addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
65
66         spi_message_init(&m);
67         memset(t, 0, sizeof(t));
68
69         t[0].tx_buf = &addr;
70         t[0].len = sizeof(addr);
71         spi_message_add_tail(&t[0], &m);
72
73         t[1].rx_buf = buf;
74         t[1].len = len;
75         spi_message_add_tail(&t[1], &m);
76
77         spi_sync(priv->spi, &m);
78 }
79
80
81 static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
82                              const void *buf, size_t len)
83 {
84         struct spi_transfer t[3];
85         struct spi_message m;
86         __le16 addr;
87
88         /* We first push the address */
89         addr = cpu_to_le16(address << 8);
90
91         spi_message_init(&m);
92         memset(t, 0, sizeof(t));
93
94         t[0].tx_buf = &addr;
95         t[0].len = sizeof(addr);
96         spi_message_add_tail(&t[0], &m);
97
98         t[1].tx_buf = buf;
99         t[1].len = len;
100         spi_message_add_tail(&t[1], &m);
101
102         if (len % 2) {
103                 __le16 last_word;
104                 last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
105
106                 t[2].tx_buf = &last_word;
107                 t[2].len = sizeof(last_word);
108                 spi_message_add_tail(&t[2], &m);
109         }
110
111         spi_sync(priv->spi, &m);
112 }
113
114 static u16 p54spi_read16(struct p54s_priv *priv, u8 addr)
115 {
116         __le16 val;
117
118         p54spi_spi_read(priv, addr, &val, sizeof(val));
119
120         return le16_to_cpu(val);
121 }
122
123 static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
124 {
125         __le32 val;
126
127         p54spi_spi_read(priv, addr, &val, sizeof(val));
128
129         return le32_to_cpu(val);
130 }
131
132 static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
133 {
134         p54spi_spi_write(priv, addr, &val, sizeof(val));
135 }
136
137 static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
138 {
139         p54spi_spi_write(priv, addr, &val, sizeof(val));
140 }
141
142 struct p54spi_spi_reg {
143         u16 address;            /* __le16 ? */
144         u16 length;
145         char *name;
146 };
147
148 static const struct p54spi_spi_reg p54spi_registers_array[] =
149 {
150         { SPI_ADRS_ARM_INTERRUPTS,      32, "ARM_INT     " },
151         { SPI_ADRS_ARM_INT_EN,          32, "ARM_INT_ENA " },
152         { SPI_ADRS_HOST_INTERRUPTS,     32, "HOST_INT    " },
153         { SPI_ADRS_HOST_INT_EN,         32, "HOST_INT_ENA" },
154         { SPI_ADRS_HOST_INT_ACK,        32, "HOST_INT_ACK" },
155         { SPI_ADRS_GEN_PURP_1,          32, "GP1_COMM    " },
156         { SPI_ADRS_GEN_PURP_2,          32, "GP2_COMM    " },
157         { SPI_ADRS_DEV_CTRL_STAT,       32, "DEV_CTRL_STA" },
158         { SPI_ADRS_DMA_DATA,            16, "DMA_DATA    " },
159         { SPI_ADRS_DMA_WRITE_CTRL,      16, "DMA_WR_CTRL " },
160         { SPI_ADRS_DMA_WRITE_LEN,       16, "DMA_WR_LEN  " },
161         { SPI_ADRS_DMA_WRITE_BASE,      32, "DMA_WR_BASE " },
162         { SPI_ADRS_DMA_READ_CTRL,       16, "DMA_RD_CTRL " },
163         { SPI_ADRS_DMA_READ_LEN,        16, "DMA_RD_LEN  " },
164         { SPI_ADRS_DMA_WRITE_BASE,      32, "DMA_RD_BASE " }
165 };
166
167 static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, __le32 bits)
168 {
169         int i;
170         __le32 buffer;
171
172         for (i = 0; i < 2000; i++) {
173                 p54spi_spi_read(priv, reg, &buffer, sizeof(buffer));
174                 if (buffer == bits)
175                         return 1;
176
177                 msleep(1);
178         }
179         return 0;
180 }
181
182 static int p54spi_request_firmware(struct ieee80211_hw *dev)
183 {
184         struct p54s_priv *priv = dev->priv;
185         int ret;
186
187         /* FIXME: should driver use it's own struct device? */
188         ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
189
190         if (ret < 0) {
191                 dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
192                 return ret;
193         }
194
195         ret = p54_parse_firmware(dev, priv->firmware);
196         if (ret) {
197                 release_firmware(priv->firmware);
198                 return ret;
199         }
200
201         return 0;
202 }
203
204 static int p54spi_request_eeprom(struct ieee80211_hw *dev)
205 {
206         struct p54s_priv *priv = dev->priv;
207         const struct firmware *eeprom;
208         int ret;
209
210         /*
211          * allow users to customize their eeprom.
212          */
213
214         ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev);
215         if (ret < 0) {
216                 dev_info(&priv->spi->dev, "loading default eeprom...\n");
217                 ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
218                                        sizeof(p54spi_eeprom));
219         } else {
220                 dev_info(&priv->spi->dev, "loading user eeprom...\n");
221                 ret = p54_parse_eeprom(dev, (void *) eeprom->data,
222                                        (int)eeprom->size);
223                 release_firmware(eeprom);
224         }
225         return ret;
226 }
227
228 static int p54spi_upload_firmware(struct ieee80211_hw *dev)
229 {
230         struct p54s_priv *priv = dev->priv;
231         unsigned long fw_len, fw_addr;
232         long _fw_len;
233
234         /* stop the device */
235         p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
236                        SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
237                        SPI_CTRL_STAT_START_HALTED));
238
239         msleep(TARGET_BOOT_SLEEP);
240
241         p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
242                        SPI_CTRL_STAT_HOST_OVERRIDE |
243                        SPI_CTRL_STAT_START_HALTED));
244
245         msleep(TARGET_BOOT_SLEEP);
246
247         fw_addr = ISL38XX_DEV_FIRMWARE_ADDR;
248         fw_len = priv->firmware->size;
249
250         while (fw_len > 0) {
251                 _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
252
253                 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
254                                cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
255
256                 if (p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL,
257                                     cpu_to_le32(HOST_ALLOWED)) == 0) {
258                         dev_err(&priv->spi->dev, "fw_upload not allowed "
259                                 "to DMA write.");
260                         return -EAGAIN;
261                 }
262
263                 p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN,
264                                cpu_to_le16(_fw_len));
265                 p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE,
266                                cpu_to_le32(fw_addr));
267
268                 p54spi_spi_write(priv, SPI_ADRS_DMA_DATA,
269                                  &priv->firmware->data, _fw_len);
270
271                 fw_len -= _fw_len;
272                 fw_addr += _fw_len;
273
274                 /* FIXME: I think this doesn't work if firmware is large,
275                  * this loop goes to second round. fw->data is not
276                  * increased at all! */
277         }
278
279         BUG_ON(fw_len != 0);
280
281         /* enable host interrupts */
282         p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
283                        cpu_to_le32(SPI_HOST_INTS_DEFAULT));
284
285         /* boot the device */
286         p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
287                        SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
288                        SPI_CTRL_STAT_RAM_BOOT));
289
290         msleep(TARGET_BOOT_SLEEP);
291
292         p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
293                        SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
294         msleep(TARGET_BOOT_SLEEP);
295         return 0;
296 }
297
298 static void p54spi_power_off(struct p54s_priv *priv)
299 {
300         disable_irq(gpio_to_irq(p54spi_gpio_irq));
301         gpio_set_value(p54spi_gpio_power, 0);
302 }
303
304 static void p54spi_power_on(struct p54s_priv *priv)
305 {
306         gpio_set_value(p54spi_gpio_power, 1);
307         enable_irq(gpio_to_irq(p54spi_gpio_irq));
308
309         /*
310          * need to wait a while before device can be accessed, the lenght
311          * is just a guess
312          */
313         msleep(10);
314 }
315
316 static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
317 {
318         p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
319 }
320
321 static void p54spi_wakeup(struct p54s_priv *priv)
322 {
323         unsigned long timeout;
324         u32 ints;
325
326         /* wake the chip */
327         p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
328                        cpu_to_le32(SPI_TARGET_INT_WAKEUP));
329
330         /* And wait for the READY interrupt */
331         timeout = jiffies + HZ;
332
333         ints =  p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
334         while (!(ints & SPI_HOST_INT_READY)) {
335                 if (time_after(jiffies, timeout))
336                                 goto out;
337                 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
338         }
339
340         p54spi_int_ack(priv, SPI_HOST_INT_READY);
341
342 out:
343         return;
344 }
345
346 static inline void p54spi_sleep(struct p54s_priv *priv)
347 {
348         p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
349                        cpu_to_le32(SPI_TARGET_INT_SLEEP));
350 }
351
352 static void p54spi_int_ready(struct p54s_priv *priv)
353 {
354         p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
355                        SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
356
357         switch (priv->fw_state) {
358         case FW_STATE_BOOTING:
359                 priv->fw_state = FW_STATE_READY;
360                 complete(&priv->fw_comp);
361                 break;
362         case FW_STATE_RESETTING:
363                 priv->fw_state = FW_STATE_READY;
364                 /* TODO: reinitialize state */
365                 break;
366         default:
367                 break;
368         }
369 }
370
371 static int p54spi_rx(struct p54s_priv *priv)
372 {
373         struct sk_buff *skb;
374         u16 len;
375
376         p54spi_wakeup(priv);
377
378         /* dummy read to flush SPI DMA controller bug */
379         p54spi_read16(priv, SPI_ADRS_GEN_PURP_1);
380
381         len = p54spi_read16(priv, SPI_ADRS_DMA_DATA);
382
383         if (len == 0) {
384                 dev_err(&priv->spi->dev, "rx request of zero bytes");
385                 return 0;
386         }
387
388         skb = dev_alloc_skb(len);
389         if (!skb) {
390                 dev_err(&priv->spi->dev, "could not alloc skb");
391                 return 0;
392         }
393
394         p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, skb_put(skb, len), len);
395         p54spi_sleep(priv);
396
397         if (p54_rx(priv->hw, skb) == 0)
398                 dev_kfree_skb(skb);
399
400         return 0;
401 }
402
403
404 static irqreturn_t p54spi_interrupt(int irq, void *config)
405 {
406         struct spi_device *spi = config;
407         struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
408
409         queue_work(priv->hw->workqueue, &priv->work);
410
411         return IRQ_HANDLED;
412 }
413
414 static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
415 {
416         struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
417         struct p54s_dma_regs dma_regs;
418         unsigned long timeout;
419         int ret = 0;
420         u32 ints;
421
422         p54spi_wakeup(priv);
423
424         dma_regs.cmd = cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE);
425         dma_regs.len = cpu_to_le16(skb->len);
426         dma_regs.addr = hdr->req_id;
427
428         p54spi_spi_write(priv, SPI_ADRS_DMA_WRITE_CTRL, &dma_regs,
429                            sizeof(dma_regs));
430
431         p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, skb->data, skb->len);
432
433         timeout = jiffies + 2 * HZ;
434         ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
435         while (!(ints & SPI_HOST_INT_WR_READY)) {
436                 if (time_after(jiffies, timeout)) {
437                         dev_err(&priv->spi->dev, "WR_READY timeout");
438                         ret = -1;
439                         goto out;
440                 }
441                 ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
442         }
443
444         p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
445         p54spi_sleep(priv);
446
447 out:
448         if (FREE_AFTER_TX(skb))
449                 p54_free_skb(priv->hw, skb);
450         return ret;
451 }
452
453 static int p54spi_wq_tx(struct p54s_priv *priv)
454 {
455         struct p54s_tx_info *entry;
456         struct sk_buff *skb;
457         struct ieee80211_tx_info *info;
458         struct p54_tx_info *minfo;
459         struct p54s_tx_info *dinfo;
460         unsigned long flags;
461         int ret = 0;
462
463         spin_lock_irqsave(&priv->tx_lock, flags);
464
465         while (!list_empty(&priv->tx_pending)) {
466                 entry = list_entry(priv->tx_pending.next,
467                                    struct p54s_tx_info, tx_list);
468
469                 list_del_init(&entry->tx_list);
470
471                 spin_unlock_irqrestore(&priv->tx_lock, flags);
472
473                 dinfo = container_of((void *) entry, struct p54s_tx_info,
474                                      tx_list);
475                 minfo = container_of((void *) dinfo, struct p54_tx_info,
476                                      data);
477                 info = container_of((void *) minfo, struct ieee80211_tx_info,
478                                     rate_driver_data);
479                 skb = container_of((void *) info, struct sk_buff, cb);
480
481                 ret = p54spi_tx_frame(priv, skb);
482
483                 if (ret < 0) {
484                         p54_free_skb(priv->hw, skb);
485                         return ret;
486                 }
487
488                 spin_lock_irqsave(&priv->tx_lock, flags);
489         }
490         spin_unlock_irqrestore(&priv->tx_lock, flags);
491         return ret;
492 }
493
494 static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
495 {
496         struct p54s_priv *priv = dev->priv;
497         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
498         struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
499         struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
500         unsigned long flags;
501
502         BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
503
504         spin_lock_irqsave(&priv->tx_lock, flags);
505         list_add_tail(&di->tx_list, &priv->tx_pending);
506         spin_unlock_irqrestore(&priv->tx_lock, flags);
507
508         queue_work(priv->hw->workqueue, &priv->work);
509 }
510
511 static void p54spi_work(struct work_struct *work)
512 {
513         struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
514         u32 ints;
515         int ret;
516
517         mutex_lock(&priv->mutex);
518
519         if (priv->fw_state == FW_STATE_OFF &&
520             priv->fw_state == FW_STATE_RESET)
521                 goto out;
522
523         ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
524
525         if (ints & SPI_HOST_INT_READY) {
526                 p54spi_int_ready(priv);
527                 p54spi_int_ack(priv, SPI_HOST_INT_READY);
528         }
529
530         if (priv->fw_state != FW_STATE_READY)
531                 goto out;
532
533         if (ints & SPI_HOST_INT_UPDATE) {
534                 p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
535                 ret = p54spi_rx(priv);
536                 if (ret < 0)
537                         goto out;
538         }
539         if (ints & SPI_HOST_INT_SW_UPDATE) {
540                 p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
541                 ret = p54spi_rx(priv);
542                 if (ret < 0)
543                         goto out;
544         }
545
546         ret = p54spi_wq_tx(priv);
547         if (ret < 0)
548                 goto out;
549
550         ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
551
552 out:
553         mutex_unlock(&priv->mutex);
554 }
555
556 static int p54spi_op_start(struct ieee80211_hw *dev)
557 {
558         struct p54s_priv *priv = dev->priv;
559         unsigned long timeout;
560         int ret = 0;
561
562         if (mutex_lock_interruptible(&priv->mutex)) {
563                 ret = -EINTR;
564                 goto out;
565         }
566
567         priv->fw_state = FW_STATE_BOOTING;
568
569         p54spi_power_on(priv);
570
571         ret = p54spi_upload_firmware(dev);
572         if (ret < 0) {
573                 p54spi_power_off(priv);
574                 goto out_unlock;
575         }
576
577         mutex_unlock(&priv->mutex);
578
579         timeout = msecs_to_jiffies(2000);
580         timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
581                                                             timeout);
582         if (!timeout) {
583                 dev_err(&priv->spi->dev, "firmware boot failed");
584                 p54spi_power_off(priv);
585                 ret = -1;
586                 goto out;
587         }
588
589         if (mutex_lock_interruptible(&priv->mutex)) {
590                 ret = -EINTR;
591                 p54spi_power_off(priv);
592                 goto out;
593         }
594
595         WARN_ON(priv->fw_state != FW_STATE_READY);
596
597 out_unlock:
598         mutex_unlock(&priv->mutex);
599
600 out:
601         return ret;
602 }
603
604 static void p54spi_op_stop(struct ieee80211_hw *dev)
605 {
606         struct p54s_priv *priv = dev->priv;
607         unsigned long flags;
608
609         if (mutex_lock_interruptible(&priv->mutex)) {
610                 /* FIXME: how to handle this error? */
611                 return;
612         }
613
614         WARN_ON(priv->fw_state != FW_STATE_READY);
615
616         cancel_work_sync(&priv->work);
617
618         p54spi_power_off(priv);
619         spin_lock_irqsave(&priv->tx_lock, flags);
620         INIT_LIST_HEAD(&priv->tx_pending);
621         spin_unlock_irqrestore(&priv->tx_lock, flags);
622
623         priv->fw_state = FW_STATE_OFF;
624         mutex_unlock(&priv->mutex);
625 }
626
627 static int __devinit p54spi_probe(struct spi_device *spi)
628 {
629         struct p54s_priv *priv = NULL;
630         struct ieee80211_hw *hw;
631         int ret = -EINVAL;
632
633         hw = p54_init_common(sizeof(*priv));
634         if (!hw) {
635                 dev_err(&priv->spi->dev, "could not alloc ieee80211_hw");
636                 return -ENOMEM;
637         }
638
639         priv = hw->priv;
640         priv->hw = hw;
641         dev_set_drvdata(&spi->dev, priv);
642         priv->spi = spi;
643
644         spi->bits_per_word = 16;
645         spi->max_speed_hz = 24000000;
646
647         ret = spi_setup(spi);
648         if (ret < 0) {
649                 dev_err(&priv->spi->dev, "spi_setup failed");
650                 goto err_free_common;
651         }
652
653         ret = gpio_request(p54spi_gpio_power, "p54spi power");
654         if (ret < 0) {
655                 dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
656                 goto err_free_common;
657         }
658
659         ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
660         if (ret < 0) {
661                 dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
662                 goto err_free_common;
663         }
664
665         gpio_direction_output(p54spi_gpio_power, 0);
666         gpio_direction_input(p54spi_gpio_irq);
667
668         ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
669                           p54spi_interrupt, IRQF_DISABLED, "p54spi",
670                           priv->spi);
671         if (ret < 0) {
672                 dev_err(&priv->spi->dev, "request_irq() failed");
673                 goto err_free_common;
674         }
675
676         set_irq_type(gpio_to_irq(p54spi_gpio_irq),
677                      IRQ_TYPE_EDGE_RISING);
678
679         disable_irq(gpio_to_irq(p54spi_gpio_irq));
680
681         INIT_WORK(&priv->work, p54spi_work);
682         init_completion(&priv->fw_comp);
683         INIT_LIST_HEAD(&priv->tx_pending);
684         mutex_init(&priv->mutex);
685         SET_IEEE80211_DEV(hw, &spi->dev);
686         priv->common.open = p54spi_op_start;
687         priv->common.stop = p54spi_op_stop;
688         priv->common.tx = p54spi_op_tx;
689
690         ret = p54spi_request_firmware(hw);
691         if (ret < 0)
692                 goto err_free_common;
693
694         ret = p54spi_request_eeprom(hw);
695         if (ret)
696                 goto err_free_common;
697
698         ret = p54_register_common(hw, &priv->spi->dev);
699         if (ret)
700                 goto err_free_common;
701
702         return 0;
703
704 err_free_common:
705         p54_free_common(priv->hw);
706         return ret;
707 }
708
709 static int __devexit p54spi_remove(struct spi_device *spi)
710 {
711         struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
712
713         ieee80211_unregister_hw(priv->hw);
714
715         free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
716
717         gpio_free(p54spi_gpio_power);
718         gpio_free(p54spi_gpio_irq);
719         release_firmware(priv->firmware);
720
721         mutex_destroy(&priv->mutex);
722
723         p54_free_common(priv->hw);
724         ieee80211_free_hw(priv->hw);
725
726         return 0;
727 }
728
729
730 static struct spi_driver p54spi_driver = {
731         .driver = {
732                 /* use cx3110x name because board-n800.c uses that for the
733                  * SPI port */
734                 .name           = "cx3110x",
735                 .bus            = &spi_bus_type,
736                 .owner          = THIS_MODULE,
737         },
738
739         .probe          = p54spi_probe,
740         .remove         = __devexit_p(p54spi_remove),
741 };
742
743 static int __init p54spi_init(void)
744 {
745         int ret;
746
747         ret = spi_register_driver(&p54spi_driver);
748         if (ret < 0) {
749                 printk(KERN_ERR "failed to register SPI driver: %d", ret);
750                 goto out;
751         }
752
753 out:
754         return ret;
755 }
756
757 static void __exit p54spi_exit(void)
758 {
759         spi_unregister_driver(&p54spi_driver);
760 }
761
762 module_init(p54spi_init);
763 module_exit(p54spi_exit);
764
765 MODULE_LICENSE("GPL");
766 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");