p54spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/firmware.h>
  26. #include <linux/delay.h>
  27. #include <linux/irq.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/gpio.h>
  31. #include "p54spi.h"
  32. #include "p54spi_eeprom.h"
  33. #include "p54.h"
  34. #include "p54common.h"
  35. MODULE_FIRMWARE("3826.arm");
  36. MODULE_ALIAS("stlc45xx");
  37. /*
  38. * gpios should be handled in board files and provided via platform data,
  39. * but because it's currently impossible for p54spi to have a header file
  40. * in include/linux, let's use module paramaters for now
  41. */
  42. static int p54spi_gpio_power = 97;
  43. module_param(p54spi_gpio_power, int, 0444);
  44. MODULE_PARM_DESC(p54spi_gpio_power, "gpio number for power line");
  45. static int p54spi_gpio_irq = 87;
  46. module_param(p54spi_gpio_irq, int, 0444);
  47. MODULE_PARM_DESC(p54spi_gpio_irq, "gpio number for irq line");
  48. static void p54spi_spi_read(struct p54s_priv *priv, u8 address,
  49. void *buf, size_t len)
  50. {
  51. struct spi_transfer t[2];
  52. struct spi_message m;
  53. __le16 addr;
  54. /* We first push the address */
  55. addr = cpu_to_le16(address << 8 | SPI_ADRS_READ_BIT_15);
  56. spi_message_init(&m);
  57. memset(t, 0, sizeof(t));
  58. t[0].tx_buf = &addr;
  59. t[0].len = sizeof(addr);
  60. spi_message_add_tail(&t[0], &m);
  61. t[1].rx_buf = buf;
  62. t[1].len = len;
  63. spi_message_add_tail(&t[1], &m);
  64. spi_sync(priv->spi, &m);
  65. }
  66. static void p54spi_spi_write(struct p54s_priv *priv, u8 address,
  67. const void *buf, size_t len)
  68. {
  69. struct spi_transfer t[3];
  70. struct spi_message m;
  71. __le16 addr;
  72. /* We first push the address */
  73. addr = cpu_to_le16(address << 8);
  74. spi_message_init(&m);
  75. memset(t, 0, sizeof(t));
  76. t[0].tx_buf = &addr;
  77. t[0].len = sizeof(addr);
  78. spi_message_add_tail(&t[0], &m);
  79. t[1].tx_buf = buf;
  80. t[1].len = len & ~1;
  81. spi_message_add_tail(&t[1], &m);
  82. if (len % 2) {
  83. __le16 last_word;
  84. last_word = cpu_to_le16(((u8 *)buf)[len - 1]);
  85. t[2].tx_buf = &last_word;
  86. t[2].len = sizeof(last_word);
  87. spi_message_add_tail(&t[2], &m);
  88. }
  89. spi_sync(priv->spi, &m);
  90. }
  91. static u16 p54spi_read16(struct p54s_priv *priv, u8 addr)
  92. {
  93. __le16 val;
  94. p54spi_spi_read(priv, addr, &val, sizeof(val));
  95. return le16_to_cpu(val);
  96. }
  97. static u32 p54spi_read32(struct p54s_priv *priv, u8 addr)
  98. {
  99. __le32 val;
  100. p54spi_spi_read(priv, addr, &val, sizeof(val));
  101. return le32_to_cpu(val);
  102. }
  103. static inline void p54spi_write16(struct p54s_priv *priv, u8 addr, __le16 val)
  104. {
  105. p54spi_spi_write(priv, addr, &val, sizeof(val));
  106. }
  107. static inline void p54spi_write32(struct p54s_priv *priv, u8 addr, __le32 val)
  108. {
  109. p54spi_spi_write(priv, addr, &val, sizeof(val));
  110. }
  111. struct p54spi_spi_reg {
  112. u16 address; /* __le16 ? */
  113. u16 length;
  114. char *name;
  115. };
  116. static const struct p54spi_spi_reg p54spi_registers_array[] =
  117. {
  118. { SPI_ADRS_ARM_INTERRUPTS, 32, "ARM_INT " },
  119. { SPI_ADRS_ARM_INT_EN, 32, "ARM_INT_ENA " },
  120. { SPI_ADRS_HOST_INTERRUPTS, 32, "HOST_INT " },
  121. { SPI_ADRS_HOST_INT_EN, 32, "HOST_INT_ENA" },
  122. { SPI_ADRS_HOST_INT_ACK, 32, "HOST_INT_ACK" },
  123. { SPI_ADRS_GEN_PURP_1, 32, "GP1_COMM " },
  124. { SPI_ADRS_GEN_PURP_2, 32, "GP2_COMM " },
  125. { SPI_ADRS_DEV_CTRL_STAT, 32, "DEV_CTRL_STA" },
  126. { SPI_ADRS_DMA_DATA, 16, "DMA_DATA " },
  127. { SPI_ADRS_DMA_WRITE_CTRL, 16, "DMA_WR_CTRL " },
  128. { SPI_ADRS_DMA_WRITE_LEN, 16, "DMA_WR_LEN " },
  129. { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_WR_BASE " },
  130. { SPI_ADRS_DMA_READ_CTRL, 16, "DMA_RD_CTRL " },
  131. { SPI_ADRS_DMA_READ_LEN, 16, "DMA_RD_LEN " },
  132. { SPI_ADRS_DMA_WRITE_BASE, 32, "DMA_RD_BASE " }
  133. };
  134. static int p54spi_wait_bit(struct p54s_priv *priv, u16 reg, __le32 bits)
  135. {
  136. int i;
  137. for (i = 0; i < 2000; i++) {
  138. __le32 buffer = p54spi_read32(priv, reg);
  139. if ((buffer & bits) == bits)
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. static int p54spi_spi_write_dma(struct p54s_priv *priv, __le32 base,
  145. const void *buf, size_t len)
  146. {
  147. if (!p54spi_wait_bit(priv, SPI_ADRS_DMA_WRITE_CTRL,
  148. cpu_to_le32(HOST_ALLOWED))) {
  149. dev_err(&priv->spi->dev, "spi_write_dma not allowed "
  150. "to DMA write.\n");
  151. return -EAGAIN;
  152. }
  153. p54spi_write16(priv, SPI_ADRS_DMA_WRITE_CTRL,
  154. cpu_to_le16(SPI_DMA_WRITE_CTRL_ENABLE));
  155. p54spi_write16(priv, SPI_ADRS_DMA_WRITE_LEN, cpu_to_le16(len));
  156. p54spi_write32(priv, SPI_ADRS_DMA_WRITE_BASE, base);
  157. p54spi_spi_write(priv, SPI_ADRS_DMA_DATA, buf, len);
  158. return 0;
  159. }
  160. static int p54spi_request_firmware(struct ieee80211_hw *dev)
  161. {
  162. struct p54s_priv *priv = dev->priv;
  163. int ret;
  164. /* FIXME: should driver use it's own struct device? */
  165. ret = request_firmware(&priv->firmware, "3826.arm", &priv->spi->dev);
  166. if (ret < 0) {
  167. dev_err(&priv->spi->dev, "request_firmware() failed: %d", ret);
  168. return ret;
  169. }
  170. ret = p54_parse_firmware(dev, priv->firmware);
  171. if (ret) {
  172. release_firmware(priv->firmware);
  173. return ret;
  174. }
  175. return 0;
  176. }
  177. static int p54spi_request_eeprom(struct ieee80211_hw *dev)
  178. {
  179. struct p54s_priv *priv = dev->priv;
  180. const struct firmware *eeprom;
  181. int ret;
  182. /*
  183. * allow users to customize their eeprom.
  184. */
  185. ret = request_firmware(&eeprom, "3826.eeprom", &priv->spi->dev);
  186. if (ret < 0) {
  187. dev_info(&priv->spi->dev, "loading default eeprom...\n");
  188. ret = p54_parse_eeprom(dev, (void *) p54spi_eeprom,
  189. sizeof(p54spi_eeprom));
  190. } else {
  191. dev_info(&priv->spi->dev, "loading user eeprom...\n");
  192. ret = p54_parse_eeprom(dev, (void *) eeprom->data,
  193. (int)eeprom->size);
  194. release_firmware(eeprom);
  195. }
  196. return ret;
  197. }
  198. static int p54spi_upload_firmware(struct ieee80211_hw *dev)
  199. {
  200. struct p54s_priv *priv = dev->priv;
  201. unsigned long fw_len, _fw_len;
  202. unsigned int offset = 0;
  203. int err = 0;
  204. u8 *fw;
  205. fw_len = priv->firmware->size;
  206. fw = kmemdup(priv->firmware->data, fw_len, GFP_KERNEL);
  207. if (!fw)
  208. return -ENOMEM;
  209. /* stop the device */
  210. p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
  211. SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
  212. SPI_CTRL_STAT_START_HALTED));
  213. msleep(TARGET_BOOT_SLEEP);
  214. p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
  215. SPI_CTRL_STAT_HOST_OVERRIDE |
  216. SPI_CTRL_STAT_START_HALTED));
  217. msleep(TARGET_BOOT_SLEEP);
  218. while (fw_len > 0) {
  219. _fw_len = min_t(long, fw_len, SPI_MAX_PACKET_SIZE);
  220. err = p54spi_spi_write_dma(priv, cpu_to_le32(
  221. ISL38XX_DEV_FIRMWARE_ADDR + offset),
  222. (fw + offset), _fw_len);
  223. if (err < 0)
  224. goto out;
  225. fw_len -= _fw_len;
  226. offset += _fw_len;
  227. }
  228. BUG_ON(fw_len != 0);
  229. /* enable host interrupts */
  230. p54spi_write32(priv, SPI_ADRS_HOST_INT_EN,
  231. cpu_to_le32(SPI_HOST_INTS_DEFAULT));
  232. /* boot the device */
  233. p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
  234. SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_HOST_RESET |
  235. SPI_CTRL_STAT_RAM_BOOT));
  236. msleep(TARGET_BOOT_SLEEP);
  237. p54spi_write16(priv, SPI_ADRS_DEV_CTRL_STAT, cpu_to_le16(
  238. SPI_CTRL_STAT_HOST_OVERRIDE | SPI_CTRL_STAT_RAM_BOOT));
  239. msleep(TARGET_BOOT_SLEEP);
  240. out:
  241. kfree(fw);
  242. return err;
  243. }
  244. static void p54spi_power_off(struct p54s_priv *priv)
  245. {
  246. disable_irq(gpio_to_irq(p54spi_gpio_irq));
  247. gpio_set_value(p54spi_gpio_power, 0);
  248. }
  249. static void p54spi_power_on(struct p54s_priv *priv)
  250. {
  251. gpio_set_value(p54spi_gpio_power, 1);
  252. enable_irq(gpio_to_irq(p54spi_gpio_irq));
  253. /*
  254. * need to wait a while before device can be accessed, the lenght
  255. * is just a guess
  256. */
  257. msleep(10);
  258. }
  259. static inline void p54spi_int_ack(struct p54s_priv *priv, u32 val)
  260. {
  261. p54spi_write32(priv, SPI_ADRS_HOST_INT_ACK, cpu_to_le32(val));
  262. }
  263. static int p54spi_wakeup(struct p54s_priv *priv)
  264. {
  265. /* wake the chip */
  266. p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
  267. cpu_to_le32(SPI_TARGET_INT_WAKEUP));
  268. /* And wait for the READY interrupt */
  269. if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
  270. cpu_to_le32(SPI_HOST_INT_READY))) {
  271. dev_err(&priv->spi->dev, "INT_READY timeout\n");
  272. return -EBUSY;
  273. }
  274. p54spi_int_ack(priv, SPI_HOST_INT_READY);
  275. return 0;
  276. }
  277. static inline void p54spi_sleep(struct p54s_priv *priv)
  278. {
  279. p54spi_write32(priv, SPI_ADRS_ARM_INTERRUPTS,
  280. cpu_to_le32(SPI_TARGET_INT_SLEEP));
  281. }
  282. static void p54spi_int_ready(struct p54s_priv *priv)
  283. {
  284. p54spi_write32(priv, SPI_ADRS_HOST_INT_EN, cpu_to_le32(
  285. SPI_HOST_INT_UPDATE | SPI_HOST_INT_SW_UPDATE));
  286. switch (priv->fw_state) {
  287. case FW_STATE_BOOTING:
  288. priv->fw_state = FW_STATE_READY;
  289. complete(&priv->fw_comp);
  290. break;
  291. case FW_STATE_RESETTING:
  292. priv->fw_state = FW_STATE_READY;
  293. /* TODO: reinitialize state */
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. static int p54spi_rx(struct p54s_priv *priv)
  300. {
  301. struct sk_buff *skb;
  302. u16 len;
  303. u16 rx_head[2];
  304. #define READAHEAD_SZ (sizeof(rx_head)-sizeof(u16))
  305. if (p54spi_wakeup(priv) < 0)
  306. return -EBUSY;
  307. /* Read data size and first data word in one SPI transaction
  308. * This is workaround for firmware/DMA bug,
  309. * when first data word gets lost under high load.
  310. */
  311. p54spi_spi_read(priv, SPI_ADRS_DMA_DATA, rx_head, sizeof(rx_head));
  312. len = rx_head[0];
  313. if (len == 0) {
  314. p54spi_sleep(priv);
  315. dev_err(&priv->spi->dev, "rx request of zero bytes\n");
  316. return 0;
  317. }
  318. /* Firmware may insert up to 4 padding bytes after the lmac header,
  319. * but it does not amend the size of SPI data transfer.
  320. * Such packets has correct data size in header, thus referencing
  321. * past the end of allocated skb. Reserve extra 4 bytes for this case */
  322. skb = dev_alloc_skb(len + 4);
  323. if (!skb) {
  324. p54spi_sleep(priv);
  325. dev_err(&priv->spi->dev, "could not alloc skb");
  326. return -ENOMEM;
  327. }
  328. if (len <= READAHEAD_SZ) {
  329. memcpy(skb_put(skb, len), rx_head + 1, len);
  330. } else {
  331. memcpy(skb_put(skb, READAHEAD_SZ), rx_head + 1, READAHEAD_SZ);
  332. p54spi_spi_read(priv, SPI_ADRS_DMA_DATA,
  333. skb_put(skb, len - READAHEAD_SZ),
  334. len - READAHEAD_SZ);
  335. }
  336. p54spi_sleep(priv);
  337. /* Put additional bytes to compensate for the possible
  338. * alignment-caused truncation */
  339. skb_put(skb, 4);
  340. if (p54_rx(priv->hw, skb) == 0)
  341. dev_kfree_skb(skb);
  342. return 0;
  343. }
  344. static irqreturn_t p54spi_interrupt(int irq, void *config)
  345. {
  346. struct spi_device *spi = config;
  347. struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
  348. queue_work(priv->hw->workqueue, &priv->work);
  349. return IRQ_HANDLED;
  350. }
  351. static int p54spi_tx_frame(struct p54s_priv *priv, struct sk_buff *skb)
  352. {
  353. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  354. int ret = 0;
  355. if (p54spi_wakeup(priv) < 0)
  356. return -EBUSY;
  357. ret = p54spi_spi_write_dma(priv, hdr->req_id, skb->data, skb->len);
  358. if (ret < 0)
  359. goto out;
  360. if (!p54spi_wait_bit(priv, SPI_ADRS_HOST_INTERRUPTS,
  361. cpu_to_le32(SPI_HOST_INT_WR_READY))) {
  362. dev_err(&priv->spi->dev, "WR_READY timeout\n");
  363. ret = -EAGAIN;
  364. goto out;
  365. }
  366. p54spi_int_ack(priv, SPI_HOST_INT_WR_READY);
  367. if (FREE_AFTER_TX(skb))
  368. p54_free_skb(priv->hw, skb);
  369. out:
  370. p54spi_sleep(priv);
  371. return ret;
  372. }
  373. static int p54spi_wq_tx(struct p54s_priv *priv)
  374. {
  375. struct p54s_tx_info *entry;
  376. struct sk_buff *skb;
  377. struct ieee80211_tx_info *info;
  378. struct p54_tx_info *minfo;
  379. struct p54s_tx_info *dinfo;
  380. unsigned long flags;
  381. int ret = 0;
  382. spin_lock_irqsave(&priv->tx_lock, flags);
  383. while (!list_empty(&priv->tx_pending)) {
  384. entry = list_entry(priv->tx_pending.next,
  385. struct p54s_tx_info, tx_list);
  386. list_del_init(&entry->tx_list);
  387. spin_unlock_irqrestore(&priv->tx_lock, flags);
  388. dinfo = container_of((void *) entry, struct p54s_tx_info,
  389. tx_list);
  390. minfo = container_of((void *) dinfo, struct p54_tx_info,
  391. data);
  392. info = container_of((void *) minfo, struct ieee80211_tx_info,
  393. rate_driver_data);
  394. skb = container_of((void *) info, struct sk_buff, cb);
  395. ret = p54spi_tx_frame(priv, skb);
  396. if (ret < 0) {
  397. p54_free_skb(priv->hw, skb);
  398. return ret;
  399. }
  400. spin_lock_irqsave(&priv->tx_lock, flags);
  401. }
  402. spin_unlock_irqrestore(&priv->tx_lock, flags);
  403. return ret;
  404. }
  405. static void p54spi_op_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
  406. {
  407. struct p54s_priv *priv = dev->priv;
  408. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  409. struct p54_tx_info *mi = (struct p54_tx_info *) info->rate_driver_data;
  410. struct p54s_tx_info *di = (struct p54s_tx_info *) mi->data;
  411. unsigned long flags;
  412. BUILD_BUG_ON(sizeof(*di) > sizeof((mi->data)));
  413. spin_lock_irqsave(&priv->tx_lock, flags);
  414. list_add_tail(&di->tx_list, &priv->tx_pending);
  415. spin_unlock_irqrestore(&priv->tx_lock, flags);
  416. queue_work(priv->hw->workqueue, &priv->work);
  417. }
  418. static void p54spi_work(struct work_struct *work)
  419. {
  420. struct p54s_priv *priv = container_of(work, struct p54s_priv, work);
  421. u32 ints;
  422. int ret;
  423. mutex_lock(&priv->mutex);
  424. if (priv->fw_state == FW_STATE_OFF)
  425. goto out;
  426. ints = p54spi_read32(priv, SPI_ADRS_HOST_INTERRUPTS);
  427. if (ints & SPI_HOST_INT_READY) {
  428. p54spi_int_ready(priv);
  429. p54spi_int_ack(priv, SPI_HOST_INT_READY);
  430. }
  431. if (priv->fw_state != FW_STATE_READY)
  432. goto out;
  433. if (ints & SPI_HOST_INT_UPDATE) {
  434. p54spi_int_ack(priv, SPI_HOST_INT_UPDATE);
  435. ret = p54spi_rx(priv);
  436. if (ret < 0)
  437. goto out;
  438. }
  439. if (ints & SPI_HOST_INT_SW_UPDATE) {
  440. p54spi_int_ack(priv, SPI_HOST_INT_SW_UPDATE);
  441. ret = p54spi_rx(priv);
  442. if (ret < 0)
  443. goto out;
  444. }
  445. ret = p54spi_wq_tx(priv);
  446. out:
  447. mutex_unlock(&priv->mutex);
  448. }
  449. static int p54spi_op_start(struct ieee80211_hw *dev)
  450. {
  451. struct p54s_priv *priv = dev->priv;
  452. unsigned long timeout;
  453. int ret = 0;
  454. if (mutex_lock_interruptible(&priv->mutex)) {
  455. ret = -EINTR;
  456. goto out;
  457. }
  458. priv->fw_state = FW_STATE_BOOTING;
  459. p54spi_power_on(priv);
  460. ret = p54spi_upload_firmware(dev);
  461. if (ret < 0) {
  462. p54spi_power_off(priv);
  463. goto out_unlock;
  464. }
  465. mutex_unlock(&priv->mutex);
  466. timeout = msecs_to_jiffies(2000);
  467. timeout = wait_for_completion_interruptible_timeout(&priv->fw_comp,
  468. timeout);
  469. if (!timeout) {
  470. dev_err(&priv->spi->dev, "firmware boot failed");
  471. p54spi_power_off(priv);
  472. ret = -1;
  473. goto out;
  474. }
  475. if (mutex_lock_interruptible(&priv->mutex)) {
  476. ret = -EINTR;
  477. p54spi_power_off(priv);
  478. goto out;
  479. }
  480. WARN_ON(priv->fw_state != FW_STATE_READY);
  481. out_unlock:
  482. mutex_unlock(&priv->mutex);
  483. out:
  484. return ret;
  485. }
  486. static void p54spi_op_stop(struct ieee80211_hw *dev)
  487. {
  488. struct p54s_priv *priv = dev->priv;
  489. unsigned long flags;
  490. if (mutex_lock_interruptible(&priv->mutex)) {
  491. /* FIXME: how to handle this error? */
  492. return;
  493. }
  494. WARN_ON(priv->fw_state != FW_STATE_READY);
  495. cancel_work_sync(&priv->work);
  496. p54spi_power_off(priv);
  497. spin_lock_irqsave(&priv->tx_lock, flags);
  498. INIT_LIST_HEAD(&priv->tx_pending);
  499. spin_unlock_irqrestore(&priv->tx_lock, flags);
  500. priv->fw_state = FW_STATE_OFF;
  501. mutex_unlock(&priv->mutex);
  502. }
  503. static int __devinit p54spi_probe(struct spi_device *spi)
  504. {
  505. struct p54s_priv *priv = NULL;
  506. struct ieee80211_hw *hw;
  507. int ret = -EINVAL;
  508. hw = p54_init_common(sizeof(*priv));
  509. if (!hw) {
  510. dev_err(&priv->spi->dev, "could not alloc ieee80211_hw");
  511. return -ENOMEM;
  512. }
  513. priv = hw->priv;
  514. priv->hw = hw;
  515. dev_set_drvdata(&spi->dev, priv);
  516. priv->spi = spi;
  517. spi->bits_per_word = 16;
  518. spi->max_speed_hz = 24000000;
  519. ret = spi_setup(spi);
  520. if (ret < 0) {
  521. dev_err(&priv->spi->dev, "spi_setup failed");
  522. goto err_free_common;
  523. }
  524. ret = gpio_request(p54spi_gpio_power, "p54spi power");
  525. if (ret < 0) {
  526. dev_err(&priv->spi->dev, "power GPIO request failed: %d", ret);
  527. goto err_free_common;
  528. }
  529. ret = gpio_request(p54spi_gpio_irq, "p54spi irq");
  530. if (ret < 0) {
  531. dev_err(&priv->spi->dev, "irq GPIO request failed: %d", ret);
  532. goto err_free_common;
  533. }
  534. gpio_direction_output(p54spi_gpio_power, 0);
  535. gpio_direction_input(p54spi_gpio_irq);
  536. ret = request_irq(gpio_to_irq(p54spi_gpio_irq),
  537. p54spi_interrupt, IRQF_DISABLED, "p54spi",
  538. priv->spi);
  539. if (ret < 0) {
  540. dev_err(&priv->spi->dev, "request_irq() failed");
  541. goto err_free_common;
  542. }
  543. set_irq_type(gpio_to_irq(p54spi_gpio_irq),
  544. IRQ_TYPE_EDGE_RISING);
  545. disable_irq(gpio_to_irq(p54spi_gpio_irq));
  546. INIT_WORK(&priv->work, p54spi_work);
  547. init_completion(&priv->fw_comp);
  548. INIT_LIST_HEAD(&priv->tx_pending);
  549. mutex_init(&priv->mutex);
  550. SET_IEEE80211_DEV(hw, &spi->dev);
  551. priv->common.open = p54spi_op_start;
  552. priv->common.stop = p54spi_op_stop;
  553. priv->common.tx = p54spi_op_tx;
  554. ret = p54spi_request_firmware(hw);
  555. if (ret < 0)
  556. goto err_free_common;
  557. ret = p54spi_request_eeprom(hw);
  558. if (ret)
  559. goto err_free_common;
  560. ret = p54_register_common(hw, &priv->spi->dev);
  561. if (ret)
  562. goto err_free_common;
  563. return 0;
  564. err_free_common:
  565. p54_free_common(priv->hw);
  566. return ret;
  567. }
  568. static int __devexit p54spi_remove(struct spi_device *spi)
  569. {
  570. struct p54s_priv *priv = dev_get_drvdata(&spi->dev);
  571. ieee80211_unregister_hw(priv->hw);
  572. free_irq(gpio_to_irq(p54spi_gpio_irq), spi);
  573. gpio_free(p54spi_gpio_power);
  574. gpio_free(p54spi_gpio_irq);
  575. release_firmware(priv->firmware);
  576. mutex_destroy(&priv->mutex);
  577. p54_free_common(priv->hw);
  578. ieee80211_free_hw(priv->hw);
  579. return 0;
  580. }
  581. static struct spi_driver p54spi_driver = {
  582. .driver = {
  583. /* use cx3110x name because board-n800.c uses that for the
  584. * SPI port */
  585. .name = "cx3110x",
  586. .bus = &spi_bus_type,
  587. .owner = THIS_MODULE,
  588. },
  589. .probe = p54spi_probe,
  590. .remove = __devexit_p(p54spi_remove),
  591. };
  592. static int __init p54spi_init(void)
  593. {
  594. int ret;
  595. ret = spi_register_driver(&p54spi_driver);
  596. if (ret < 0) {
  597. printk(KERN_ERR "failed to register SPI driver: %d", ret);
  598. goto out;
  599. }
  600. out:
  601. return ret;
  602. }
  603. static void __exit p54spi_exit(void)
  604. {
  605. spi_unregister_driver(&p54spi_driver);
  606. }
  607. module_init(p54spi_init);
  608. module_exit(p54spi_exit);
  609. MODULE_LICENSE("GPL");
  610. MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");