if_spi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /*
  2. * linux/drivers/net/wireless/libertas/if_spi.c
  3. *
  4. * Driver for Marvell SPI WLAN cards.
  5. *
  6. * Copyright 2008 Analog Devices Inc.
  7. *
  8. * Authors:
  9. * Andrey Yurovsky <andrey@cozybit.com>
  10. * Colin McCabe <colin@cozybit.com>
  11. *
  12. * Inspired by if_sdio.c, Copyright 2007-2008 Pierre Ossman
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/hardirq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/firmware.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/list.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/slab.h>
  28. #include <linux/spi/libertas_spi.h>
  29. #include <linux/spi/spi.h>
  30. #include "host.h"
  31. #include "decl.h"
  32. #include "defs.h"
  33. #include "dev.h"
  34. #include "if_spi.h"
  35. struct if_spi_packet {
  36. struct list_head list;
  37. u16 blen;
  38. u8 buffer[0] __attribute__((aligned(4)));
  39. };
  40. struct if_spi_card {
  41. struct spi_device *spi;
  42. struct lbs_private *priv;
  43. struct libertas_spi_platform_data *pdata;
  44. /* The card ID and card revision, as reported by the hardware. */
  45. u16 card_id;
  46. u8 card_rev;
  47. /* The last time that we initiated an SPU operation */
  48. unsigned long prev_xfer_time;
  49. int use_dummy_writes;
  50. unsigned long spu_port_delay;
  51. unsigned long spu_reg_delay;
  52. /* Handles all SPI communication (except for FW load) */
  53. struct workqueue_struct *workqueue;
  54. struct work_struct packet_work;
  55. struct work_struct resume_work;
  56. u8 cmd_buffer[IF_SPI_CMD_BUF_SIZE];
  57. /* A buffer of incoming packets from libertas core.
  58. * Since we can't sleep in hw_host_to_card, we have to buffer
  59. * them. */
  60. struct list_head cmd_packet_list;
  61. struct list_head data_packet_list;
  62. /* Protects cmd_packet_list and data_packet_list */
  63. spinlock_t buffer_lock;
  64. /* True is card suspended */
  65. u8 suspended;
  66. };
  67. static void free_if_spi_card(struct if_spi_card *card)
  68. {
  69. struct list_head *cursor, *next;
  70. struct if_spi_packet *packet;
  71. list_for_each_safe(cursor, next, &card->cmd_packet_list) {
  72. packet = container_of(cursor, struct if_spi_packet, list);
  73. list_del(&packet->list);
  74. kfree(packet);
  75. }
  76. list_for_each_safe(cursor, next, &card->data_packet_list) {
  77. packet = container_of(cursor, struct if_spi_packet, list);
  78. list_del(&packet->list);
  79. kfree(packet);
  80. }
  81. spi_set_drvdata(card->spi, NULL);
  82. kfree(card);
  83. }
  84. #define MODEL_8385 0x04
  85. #define MODEL_8686 0x0b
  86. #define MODEL_8688 0x10
  87. static const struct lbs_fw_table fw_table[] = {
  88. { MODEL_8385, "libertas/gspi8385_helper.bin", "libertas/gspi8385.bin" },
  89. { MODEL_8385, "libertas/gspi8385_hlp.bin", "libertas/gspi8385.bin" },
  90. { MODEL_8686, "libertas/gspi8686_v9_helper.bin", "libertas/gspi8686_v9.bin" },
  91. { MODEL_8686, "libertas/gspi8686_hlp.bin", "libertas/gspi8686.bin" },
  92. { MODEL_8688, "libertas/gspi8688_helper.bin", "libertas/gspi8688.bin" },
  93. { 0, NULL, NULL }
  94. };
  95. MODULE_FIRMWARE("libertas/gspi8385_helper.bin");
  96. MODULE_FIRMWARE("libertas/gspi8385_hlp.bin");
  97. MODULE_FIRMWARE("libertas/gspi8385.bin");
  98. MODULE_FIRMWARE("libertas/gspi8686_v9_helper.bin");
  99. MODULE_FIRMWARE("libertas/gspi8686_v9.bin");
  100. MODULE_FIRMWARE("libertas/gspi8686_hlp.bin");
  101. MODULE_FIRMWARE("libertas/gspi8686.bin");
  102. MODULE_FIRMWARE("libertas/gspi8688_helper.bin");
  103. MODULE_FIRMWARE("libertas/gspi8688.bin");
  104. /*
  105. * SPI Interface Unit Routines
  106. *
  107. * The SPU sits between the host and the WLAN module.
  108. * All communication with the firmware is through SPU transactions.
  109. *
  110. * First we have to put a SPU register name on the bus. Then we can
  111. * either read from or write to that register.
  112. *
  113. */
  114. static void spu_transaction_init(struct if_spi_card *card)
  115. {
  116. if (!time_after(jiffies, card->prev_xfer_time + 1)) {
  117. /* Unfortunately, the SPU requires a delay between successive
  118. * transactions. If our last transaction was more than a jiffy
  119. * ago, we have obviously already delayed enough.
  120. * If not, we have to busy-wait to be on the safe side. */
  121. ndelay(400);
  122. }
  123. }
  124. static void spu_transaction_finish(struct if_spi_card *card)
  125. {
  126. card->prev_xfer_time = jiffies;
  127. }
  128. /*
  129. * Write out a byte buffer to an SPI register,
  130. * using a series of 16-bit transfers.
  131. */
  132. static int spu_write(struct if_spi_card *card, u16 reg, const u8 *buf, int len)
  133. {
  134. int err = 0;
  135. __le16 reg_out = cpu_to_le16(reg | IF_SPI_WRITE_OPERATION_MASK);
  136. struct spi_message m;
  137. struct spi_transfer reg_trans;
  138. struct spi_transfer data_trans;
  139. spi_message_init(&m);
  140. memset(&reg_trans, 0, sizeof(reg_trans));
  141. memset(&data_trans, 0, sizeof(data_trans));
  142. /* You must give an even number of bytes to the SPU, even if it
  143. * doesn't care about the last one. */
  144. BUG_ON(len & 0x1);
  145. spu_transaction_init(card);
  146. /* write SPU register index */
  147. reg_trans.tx_buf = &reg_out;
  148. reg_trans.len = sizeof(reg_out);
  149. data_trans.tx_buf = buf;
  150. data_trans.len = len;
  151. spi_message_add_tail(&reg_trans, &m);
  152. spi_message_add_tail(&data_trans, &m);
  153. err = spi_sync(card->spi, &m);
  154. spu_transaction_finish(card);
  155. return err;
  156. }
  157. static inline int spu_write_u16(struct if_spi_card *card, u16 reg, u16 val)
  158. {
  159. __le16 buff;
  160. buff = cpu_to_le16(val);
  161. return spu_write(card, reg, (u8 *)&buff, sizeof(u16));
  162. }
  163. static inline int spu_reg_is_port_reg(u16 reg)
  164. {
  165. switch (reg) {
  166. case IF_SPI_IO_RDWRPORT_REG:
  167. case IF_SPI_CMD_RDWRPORT_REG:
  168. case IF_SPI_DATA_RDWRPORT_REG:
  169. return 1;
  170. default:
  171. return 0;
  172. }
  173. }
  174. static int spu_read(struct if_spi_card *card, u16 reg, u8 *buf, int len)
  175. {
  176. unsigned int delay;
  177. int err = 0;
  178. __le16 reg_out = cpu_to_le16(reg | IF_SPI_READ_OPERATION_MASK);
  179. struct spi_message m;
  180. struct spi_transfer reg_trans;
  181. struct spi_transfer dummy_trans;
  182. struct spi_transfer data_trans;
  183. /*
  184. * You must take an even number of bytes from the SPU, even if you
  185. * don't care about the last one.
  186. */
  187. BUG_ON(len & 0x1);
  188. spu_transaction_init(card);
  189. spi_message_init(&m);
  190. memset(&reg_trans, 0, sizeof(reg_trans));
  191. memset(&dummy_trans, 0, sizeof(dummy_trans));
  192. memset(&data_trans, 0, sizeof(data_trans));
  193. /* write SPU register index */
  194. reg_trans.tx_buf = &reg_out;
  195. reg_trans.len = sizeof(reg_out);
  196. spi_message_add_tail(&reg_trans, &m);
  197. delay = spu_reg_is_port_reg(reg) ? card->spu_port_delay :
  198. card->spu_reg_delay;
  199. if (card->use_dummy_writes) {
  200. /* Clock in dummy cycles while the SPU fills the FIFO */
  201. dummy_trans.len = delay / 8;
  202. spi_message_add_tail(&dummy_trans, &m);
  203. } else {
  204. /* Busy-wait while the SPU fills the FIFO */
  205. reg_trans.delay_usecs =
  206. DIV_ROUND_UP((100 + (delay * 10)), 1000);
  207. }
  208. /* read in data */
  209. data_trans.rx_buf = buf;
  210. data_trans.len = len;
  211. spi_message_add_tail(&data_trans, &m);
  212. err = spi_sync(card->spi, &m);
  213. spu_transaction_finish(card);
  214. return err;
  215. }
  216. /* Read 16 bits from an SPI register */
  217. static inline int spu_read_u16(struct if_spi_card *card, u16 reg, u16 *val)
  218. {
  219. __le16 buf;
  220. int ret;
  221. ret = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
  222. if (ret == 0)
  223. *val = le16_to_cpup(&buf);
  224. return ret;
  225. }
  226. /*
  227. * Read 32 bits from an SPI register.
  228. * The low 16 bits are read first.
  229. */
  230. static int spu_read_u32(struct if_spi_card *card, u16 reg, u32 *val)
  231. {
  232. __le32 buf;
  233. int err;
  234. err = spu_read(card, reg, (u8 *)&buf, sizeof(buf));
  235. if (!err)
  236. *val = le32_to_cpup(&buf);
  237. return err;
  238. }
  239. /*
  240. * Keep reading 16 bits from an SPI register until you get the correct result.
  241. *
  242. * If mask = 0, the correct result is any non-zero number.
  243. * If mask != 0, the correct result is any number where
  244. * number & target_mask == target
  245. *
  246. * Returns -ETIMEDOUT if a second passes without the correct result.
  247. */
  248. static int spu_wait_for_u16(struct if_spi_card *card, u16 reg,
  249. u16 target_mask, u16 target)
  250. {
  251. int err;
  252. unsigned long timeout = jiffies + 5*HZ;
  253. while (1) {
  254. u16 val;
  255. err = spu_read_u16(card, reg, &val);
  256. if (err)
  257. return err;
  258. if (target_mask) {
  259. if ((val & target_mask) == target)
  260. return 0;
  261. } else {
  262. if (val)
  263. return 0;
  264. }
  265. udelay(100);
  266. if (time_after(jiffies, timeout)) {
  267. pr_err("%s: timeout with val=%02x, target_mask=%02x, target=%02x\n",
  268. __func__, val, target_mask, target);
  269. return -ETIMEDOUT;
  270. }
  271. }
  272. }
  273. /*
  274. * Read 16 bits from an SPI register until you receive a specific value.
  275. * Returns -ETIMEDOUT if a 4 tries pass without success.
  276. */
  277. static int spu_wait_for_u32(struct if_spi_card *card, u32 reg, u32 target)
  278. {
  279. int err, try;
  280. for (try = 0; try < 4; ++try) {
  281. u32 val = 0;
  282. err = spu_read_u32(card, reg, &val);
  283. if (err)
  284. return err;
  285. if (val == target)
  286. return 0;
  287. mdelay(100);
  288. }
  289. return -ETIMEDOUT;
  290. }
  291. static int spu_set_interrupt_mode(struct if_spi_card *card,
  292. int suppress_host_int,
  293. int auto_int)
  294. {
  295. int err = 0;
  296. /*
  297. * We can suppress a host interrupt by clearing the appropriate
  298. * bit in the "host interrupt status mask" register
  299. */
  300. if (suppress_host_int) {
  301. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
  302. if (err)
  303. return err;
  304. } else {
  305. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG,
  306. IF_SPI_HISM_TX_DOWNLOAD_RDY |
  307. IF_SPI_HISM_RX_UPLOAD_RDY |
  308. IF_SPI_HISM_CMD_DOWNLOAD_RDY |
  309. IF_SPI_HISM_CARDEVENT |
  310. IF_SPI_HISM_CMD_UPLOAD_RDY);
  311. if (err)
  312. return err;
  313. }
  314. /*
  315. * If auto-interrupts are on, the completion of certain transactions
  316. * will trigger an interrupt automatically. If auto-interrupts
  317. * are off, we need to set the "Card Interrupt Cause" register to
  318. * trigger a card interrupt.
  319. */
  320. if (auto_int) {
  321. err = spu_write_u16(card, IF_SPI_HOST_INT_CTRL_REG,
  322. IF_SPI_HICT_TX_DOWNLOAD_OVER_AUTO |
  323. IF_SPI_HICT_RX_UPLOAD_OVER_AUTO |
  324. IF_SPI_HICT_CMD_DOWNLOAD_OVER_AUTO |
  325. IF_SPI_HICT_CMD_UPLOAD_OVER_AUTO);
  326. if (err)
  327. return err;
  328. } else {
  329. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_MASK_REG, 0);
  330. if (err)
  331. return err;
  332. }
  333. return err;
  334. }
  335. static int spu_get_chip_revision(struct if_spi_card *card,
  336. u16 *card_id, u8 *card_rev)
  337. {
  338. int err = 0;
  339. u32 dev_ctrl;
  340. err = spu_read_u32(card, IF_SPI_DEVICEID_CTRL_REG, &dev_ctrl);
  341. if (err)
  342. return err;
  343. *card_id = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_ID(dev_ctrl);
  344. *card_rev = IF_SPI_DEVICEID_CTRL_REG_TO_CARD_REV(dev_ctrl);
  345. return err;
  346. }
  347. static int spu_set_bus_mode(struct if_spi_card *card, u16 mode)
  348. {
  349. int err = 0;
  350. u16 rval;
  351. /* set bus mode */
  352. err = spu_write_u16(card, IF_SPI_SPU_BUS_MODE_REG, mode);
  353. if (err)
  354. return err;
  355. /* Check that we were able to read back what we just wrote. */
  356. err = spu_read_u16(card, IF_SPI_SPU_BUS_MODE_REG, &rval);
  357. if (err)
  358. return err;
  359. if ((rval & 0xF) != mode) {
  360. pr_err("Can't read bus mode register\n");
  361. return -EIO;
  362. }
  363. return 0;
  364. }
  365. static int spu_init(struct if_spi_card *card, int use_dummy_writes)
  366. {
  367. int err = 0;
  368. u32 delay;
  369. /*
  370. * We have to start up in timed delay mode so that we can safely
  371. * read the Delay Read Register.
  372. */
  373. card->use_dummy_writes = 0;
  374. err = spu_set_bus_mode(card,
  375. IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
  376. IF_SPI_BUS_MODE_DELAY_METHOD_TIMED |
  377. IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
  378. if (err)
  379. return err;
  380. card->spu_port_delay = 1000;
  381. card->spu_reg_delay = 1000;
  382. err = spu_read_u32(card, IF_SPI_DELAY_READ_REG, &delay);
  383. if (err)
  384. return err;
  385. card->spu_port_delay = delay & 0x0000ffff;
  386. card->spu_reg_delay = (delay & 0xffff0000) >> 16;
  387. /* If dummy clock delay mode has been requested, switch to it now */
  388. if (use_dummy_writes) {
  389. card->use_dummy_writes = 1;
  390. err = spu_set_bus_mode(card,
  391. IF_SPI_BUS_MODE_SPI_CLOCK_PHASE_RISING |
  392. IF_SPI_BUS_MODE_DELAY_METHOD_DUMMY_CLOCK |
  393. IF_SPI_BUS_MODE_16_BIT_ADDRESS_16_BIT_DATA);
  394. if (err)
  395. return err;
  396. }
  397. lbs_deb_spi("Initialized SPU unit. "
  398. "spu_port_delay=0x%04lx, spu_reg_delay=0x%04lx\n",
  399. card->spu_port_delay, card->spu_reg_delay);
  400. return err;
  401. }
  402. /*
  403. * Firmware Loading
  404. */
  405. static int if_spi_prog_helper_firmware(struct if_spi_card *card,
  406. const struct firmware *firmware)
  407. {
  408. int err = 0;
  409. int bytes_remaining;
  410. const u8 *fw;
  411. u8 temp[HELPER_FW_LOAD_CHUNK_SZ];
  412. lbs_deb_enter(LBS_DEB_SPI);
  413. err = spu_set_interrupt_mode(card, 1, 0);
  414. if (err)
  415. goto out;
  416. bytes_remaining = firmware->size;
  417. fw = firmware->data;
  418. /* Load helper firmware image */
  419. while (bytes_remaining > 0) {
  420. /*
  421. * Scratch pad 1 should contain the number of bytes we
  422. * want to download to the firmware
  423. */
  424. err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG,
  425. HELPER_FW_LOAD_CHUNK_SZ);
  426. if (err)
  427. goto out;
  428. err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  429. IF_SPI_HIST_CMD_DOWNLOAD_RDY,
  430. IF_SPI_HIST_CMD_DOWNLOAD_RDY);
  431. if (err)
  432. goto out;
  433. /*
  434. * Feed the data into the command read/write port reg
  435. * in chunks of 64 bytes
  436. */
  437. memset(temp, 0, sizeof(temp));
  438. memcpy(temp, fw,
  439. min(bytes_remaining, HELPER_FW_LOAD_CHUNK_SZ));
  440. mdelay(10);
  441. err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
  442. temp, HELPER_FW_LOAD_CHUNK_SZ);
  443. if (err)
  444. goto out;
  445. /* Interrupt the boot code */
  446. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  447. if (err)
  448. goto out;
  449. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
  450. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  451. if (err)
  452. goto out;
  453. bytes_remaining -= HELPER_FW_LOAD_CHUNK_SZ;
  454. fw += HELPER_FW_LOAD_CHUNK_SZ;
  455. }
  456. /*
  457. * Once the helper / single stage firmware download is complete,
  458. * write 0 to scratch pad 1 and interrupt the
  459. * bootloader. This completes the helper download.
  460. */
  461. err = spu_write_u16(card, IF_SPI_SCRATCH_1_REG, FIRMWARE_DNLD_OK);
  462. if (err)
  463. goto out;
  464. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  465. if (err)
  466. goto out;
  467. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG,
  468. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  469. goto out;
  470. lbs_deb_spi("waiting for helper to boot...\n");
  471. out:
  472. if (err)
  473. pr_err("failed to load helper firmware (err=%d)\n", err);
  474. lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
  475. return err;
  476. }
  477. /*
  478. * Returns the length of the next packet the firmware expects us to send.
  479. * Sets crc_err if the previous transfer had a CRC error.
  480. */
  481. static int if_spi_prog_main_firmware_check_len(struct if_spi_card *card,
  482. int *crc_err)
  483. {
  484. u16 len;
  485. int err = 0;
  486. /*
  487. * wait until the host interrupt status register indicates
  488. * that we are ready to download
  489. */
  490. err = spu_wait_for_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  491. IF_SPI_HIST_CMD_DOWNLOAD_RDY,
  492. IF_SPI_HIST_CMD_DOWNLOAD_RDY);
  493. if (err) {
  494. pr_err("timed out waiting for host_int_status\n");
  495. return err;
  496. }
  497. /* Ask the device how many bytes of firmware it wants. */
  498. err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
  499. if (err)
  500. return err;
  501. if (len > IF_SPI_CMD_BUF_SIZE) {
  502. pr_err("firmware load device requested a larger transfer than we are prepared to handle (len = %d)\n",
  503. len);
  504. return -EIO;
  505. }
  506. if (len & 0x1) {
  507. lbs_deb_spi("%s: crc error\n", __func__);
  508. len &= ~0x1;
  509. *crc_err = 1;
  510. } else
  511. *crc_err = 0;
  512. return len;
  513. }
  514. static int if_spi_prog_main_firmware(struct if_spi_card *card,
  515. const struct firmware *firmware)
  516. {
  517. struct lbs_private *priv = card->priv;
  518. int len, prev_len;
  519. int bytes, crc_err = 0, err = 0;
  520. const u8 *fw;
  521. u16 num_crc_errs;
  522. lbs_deb_enter(LBS_DEB_SPI);
  523. err = spu_set_interrupt_mode(card, 1, 0);
  524. if (err)
  525. goto out;
  526. err = spu_wait_for_u16(card, IF_SPI_SCRATCH_1_REG, 0, 0);
  527. if (err) {
  528. netdev_err(priv->dev,
  529. "%s: timed out waiting for initial scratch reg = 0\n",
  530. __func__);
  531. goto out;
  532. }
  533. num_crc_errs = 0;
  534. prev_len = 0;
  535. bytes = firmware->size;
  536. fw = firmware->data;
  537. while ((len = if_spi_prog_main_firmware_check_len(card, &crc_err))) {
  538. if (len < 0) {
  539. err = len;
  540. goto out;
  541. }
  542. if (bytes < 0) {
  543. /*
  544. * If there are no more bytes left, we would normally
  545. * expect to have terminated with len = 0
  546. */
  547. netdev_err(priv->dev,
  548. "Firmware load wants more bytes than we have to offer.\n");
  549. break;
  550. }
  551. if (crc_err) {
  552. /* Previous transfer failed. */
  553. if (++num_crc_errs > MAX_MAIN_FW_LOAD_CRC_ERR) {
  554. pr_err("Too many CRC errors encountered in firmware load.\n");
  555. err = -EIO;
  556. goto out;
  557. }
  558. } else {
  559. /* Previous transfer succeeded. Advance counters. */
  560. bytes -= prev_len;
  561. fw += prev_len;
  562. }
  563. if (bytes < len) {
  564. memset(card->cmd_buffer, 0, len);
  565. memcpy(card->cmd_buffer, fw, bytes);
  566. } else
  567. memcpy(card->cmd_buffer, fw, len);
  568. err = spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG, 0);
  569. if (err)
  570. goto out;
  571. err = spu_write(card, IF_SPI_CMD_RDWRPORT_REG,
  572. card->cmd_buffer, len);
  573. if (err)
  574. goto out;
  575. err = spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG ,
  576. IF_SPI_CIC_CMD_DOWNLOAD_OVER);
  577. if (err)
  578. goto out;
  579. prev_len = len;
  580. }
  581. if (bytes > prev_len) {
  582. pr_err("firmware load wants fewer bytes than we have to offer\n");
  583. }
  584. /* Confirm firmware download */
  585. err = spu_wait_for_u32(card, IF_SPI_SCRATCH_4_REG,
  586. SUCCESSFUL_FW_DOWNLOAD_MAGIC);
  587. if (err) {
  588. pr_err("failed to confirm the firmware download\n");
  589. goto out;
  590. }
  591. out:
  592. if (err)
  593. pr_err("failed to load firmware (err=%d)\n", err);
  594. lbs_deb_leave_args(LBS_DEB_SPI, "err %d", err);
  595. return err;
  596. }
  597. /*
  598. * SPI Transfer Thread
  599. *
  600. * The SPI worker handles all SPI transfers, so there is no need for a lock.
  601. */
  602. /* Move a command from the card to the host */
  603. static int if_spi_c2h_cmd(struct if_spi_card *card)
  604. {
  605. struct lbs_private *priv = card->priv;
  606. unsigned long flags;
  607. int err = 0;
  608. u16 len;
  609. u8 i;
  610. /*
  611. * We need a buffer big enough to handle whatever people send to
  612. * hw_host_to_card
  613. */
  614. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_CMD_BUFFER_SIZE);
  615. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE < LBS_UPLD_SIZE);
  616. /*
  617. * It's just annoying if the buffer size isn't a multiple of 4, because
  618. * then we might have len < IF_SPI_CMD_BUF_SIZE but
  619. * ALIGN(len, 4) > IF_SPI_CMD_BUF_SIZE
  620. */
  621. BUILD_BUG_ON(IF_SPI_CMD_BUF_SIZE % 4 != 0);
  622. lbs_deb_enter(LBS_DEB_SPI);
  623. /* How many bytes are there to read? */
  624. err = spu_read_u16(card, IF_SPI_SCRATCH_2_REG, &len);
  625. if (err)
  626. goto out;
  627. if (!len) {
  628. netdev_err(priv->dev, "%s: error: card has no data for host\n",
  629. __func__);
  630. err = -EINVAL;
  631. goto out;
  632. } else if (len > IF_SPI_CMD_BUF_SIZE) {
  633. netdev_err(priv->dev,
  634. "%s: error: response packet too large: %d bytes, but maximum is %d\n",
  635. __func__, len, IF_SPI_CMD_BUF_SIZE);
  636. err = -EINVAL;
  637. goto out;
  638. }
  639. /* Read the data from the WLAN module into our command buffer */
  640. err = spu_read(card, IF_SPI_CMD_RDWRPORT_REG,
  641. card->cmd_buffer, ALIGN(len, 4));
  642. if (err)
  643. goto out;
  644. spin_lock_irqsave(&priv->driver_lock, flags);
  645. i = (priv->resp_idx == 0) ? 1 : 0;
  646. BUG_ON(priv->resp_len[i]);
  647. priv->resp_len[i] = len;
  648. memcpy(priv->resp_buf[i], card->cmd_buffer, len);
  649. lbs_notify_command_response(priv, i);
  650. spin_unlock_irqrestore(&priv->driver_lock, flags);
  651. out:
  652. if (err)
  653. netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
  654. lbs_deb_leave(LBS_DEB_SPI);
  655. return err;
  656. }
  657. /* Move data from the card to the host */
  658. static int if_spi_c2h_data(struct if_spi_card *card)
  659. {
  660. struct lbs_private *priv = card->priv;
  661. struct sk_buff *skb;
  662. char *data;
  663. u16 len;
  664. int err = 0;
  665. lbs_deb_enter(LBS_DEB_SPI);
  666. /* How many bytes are there to read? */
  667. err = spu_read_u16(card, IF_SPI_SCRATCH_1_REG, &len);
  668. if (err)
  669. goto out;
  670. if (!len) {
  671. netdev_err(priv->dev, "%s: error: card has no data for host\n",
  672. __func__);
  673. err = -EINVAL;
  674. goto out;
  675. } else if (len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
  676. netdev_err(priv->dev,
  677. "%s: error: card has %d bytes of data, but our maximum skb size is %zu\n",
  678. __func__, len, MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
  679. err = -EINVAL;
  680. goto out;
  681. }
  682. /* TODO: should we allocate a smaller skb if we have less data? */
  683. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
  684. if (!skb) {
  685. err = -ENOBUFS;
  686. goto out;
  687. }
  688. skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
  689. data = skb_put(skb, len);
  690. /* Read the data from the WLAN module into our skb... */
  691. err = spu_read(card, IF_SPI_DATA_RDWRPORT_REG, data, ALIGN(len, 4));
  692. if (err)
  693. goto free_skb;
  694. /* pass the SKB to libertas */
  695. err = lbs_process_rxed_packet(card->priv, skb);
  696. if (err)
  697. goto free_skb;
  698. /* success */
  699. goto out;
  700. free_skb:
  701. dev_kfree_skb(skb);
  702. out:
  703. if (err)
  704. netdev_err(priv->dev, "%s: err=%d\n", __func__, err);
  705. lbs_deb_leave(LBS_DEB_SPI);
  706. return err;
  707. }
  708. /* Move data or a command from the host to the card. */
  709. static void if_spi_h2c(struct if_spi_card *card,
  710. struct if_spi_packet *packet, int type)
  711. {
  712. struct lbs_private *priv = card->priv;
  713. int err = 0;
  714. u16 int_type, port_reg;
  715. switch (type) {
  716. case MVMS_DAT:
  717. int_type = IF_SPI_CIC_TX_DOWNLOAD_OVER;
  718. port_reg = IF_SPI_DATA_RDWRPORT_REG;
  719. break;
  720. case MVMS_CMD:
  721. int_type = IF_SPI_CIC_CMD_DOWNLOAD_OVER;
  722. port_reg = IF_SPI_CMD_RDWRPORT_REG;
  723. break;
  724. default:
  725. netdev_err(priv->dev, "can't transfer buffer of type %d\n",
  726. type);
  727. err = -EINVAL;
  728. goto out;
  729. }
  730. /* Write the data to the card */
  731. err = spu_write(card, port_reg, packet->buffer, packet->blen);
  732. if (err)
  733. goto out;
  734. out:
  735. kfree(packet);
  736. if (err)
  737. netdev_err(priv->dev, "%s: error %d\n", __func__, err);
  738. }
  739. /* Inform the host about a card event */
  740. static void if_spi_e2h(struct if_spi_card *card)
  741. {
  742. int err = 0;
  743. u32 cause;
  744. struct lbs_private *priv = card->priv;
  745. err = spu_read_u32(card, IF_SPI_SCRATCH_3_REG, &cause);
  746. if (err)
  747. goto out;
  748. /* re-enable the card event interrupt */
  749. spu_write_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  750. ~IF_SPI_HICU_CARD_EVENT);
  751. /* generate a card interrupt */
  752. spu_write_u16(card, IF_SPI_CARD_INT_CAUSE_REG, IF_SPI_CIC_HOST_EVENT);
  753. lbs_queue_event(priv, cause & 0xff);
  754. out:
  755. if (err)
  756. netdev_err(priv->dev, "%s: error %d\n", __func__, err);
  757. }
  758. static void if_spi_host_to_card_worker(struct work_struct *work)
  759. {
  760. int err;
  761. struct if_spi_card *card;
  762. u16 hiStatus;
  763. unsigned long flags;
  764. struct if_spi_packet *packet;
  765. struct lbs_private *priv;
  766. card = container_of(work, struct if_spi_card, packet_work);
  767. priv = card->priv;
  768. lbs_deb_enter(LBS_DEB_SPI);
  769. /*
  770. * Read the host interrupt status register to see what we
  771. * can do.
  772. */
  773. err = spu_read_u16(card, IF_SPI_HOST_INT_STATUS_REG,
  774. &hiStatus);
  775. if (err) {
  776. netdev_err(priv->dev, "I/O error\n");
  777. goto err;
  778. }
  779. if (hiStatus & IF_SPI_HIST_CMD_UPLOAD_RDY) {
  780. err = if_spi_c2h_cmd(card);
  781. if (err)
  782. goto err;
  783. }
  784. if (hiStatus & IF_SPI_HIST_RX_UPLOAD_RDY) {
  785. err = if_spi_c2h_data(card);
  786. if (err)
  787. goto err;
  788. }
  789. /*
  790. * workaround: in PS mode, the card does not set the Command
  791. * Download Ready bit, but it sets TX Download Ready.
  792. */
  793. if (hiStatus & IF_SPI_HIST_CMD_DOWNLOAD_RDY ||
  794. (card->priv->psstate != PS_STATE_FULL_POWER &&
  795. (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY))) {
  796. /*
  797. * This means two things. First of all,
  798. * if there was a previous command sent, the card has
  799. * successfully received it.
  800. * Secondly, it is now ready to download another
  801. * command.
  802. */
  803. lbs_host_to_card_done(card->priv);
  804. /* Do we have any command packets from the host to send? */
  805. packet = NULL;
  806. spin_lock_irqsave(&card->buffer_lock, flags);
  807. if (!list_empty(&card->cmd_packet_list)) {
  808. packet = (struct if_spi_packet *)(card->
  809. cmd_packet_list.next);
  810. list_del(&packet->list);
  811. }
  812. spin_unlock_irqrestore(&card->buffer_lock, flags);
  813. if (packet)
  814. if_spi_h2c(card, packet, MVMS_CMD);
  815. }
  816. if (hiStatus & IF_SPI_HIST_TX_DOWNLOAD_RDY) {
  817. /* Do we have any data packets from the host to send? */
  818. packet = NULL;
  819. spin_lock_irqsave(&card->buffer_lock, flags);
  820. if (!list_empty(&card->data_packet_list)) {
  821. packet = (struct if_spi_packet *)(card->
  822. data_packet_list.next);
  823. list_del(&packet->list);
  824. }
  825. spin_unlock_irqrestore(&card->buffer_lock, flags);
  826. if (packet)
  827. if_spi_h2c(card, packet, MVMS_DAT);
  828. }
  829. if (hiStatus & IF_SPI_HIST_CARD_EVENT)
  830. if_spi_e2h(card);
  831. err:
  832. if (err)
  833. netdev_err(priv->dev, "%s: got error %d\n", __func__, err);
  834. lbs_deb_leave(LBS_DEB_SPI);
  835. }
  836. /*
  837. * Host to Card
  838. *
  839. * Called from Libertas to transfer some data to the WLAN device
  840. * We can't sleep here.
  841. */
  842. static int if_spi_host_to_card(struct lbs_private *priv,
  843. u8 type, u8 *buf, u16 nb)
  844. {
  845. int err = 0;
  846. unsigned long flags;
  847. struct if_spi_card *card = priv->card;
  848. struct if_spi_packet *packet;
  849. u16 blen;
  850. lbs_deb_enter_args(LBS_DEB_SPI, "type %d, bytes %d", type, nb);
  851. if (nb == 0) {
  852. netdev_err(priv->dev, "%s: invalid size requested: %d\n",
  853. __func__, nb);
  854. err = -EINVAL;
  855. goto out;
  856. }
  857. blen = ALIGN(nb, 4);
  858. packet = kzalloc(sizeof(struct if_spi_packet) + blen, GFP_ATOMIC);
  859. if (!packet) {
  860. err = -ENOMEM;
  861. goto out;
  862. }
  863. packet->blen = blen;
  864. memcpy(packet->buffer, buf, nb);
  865. memset(packet->buffer + nb, 0, blen - nb);
  866. switch (type) {
  867. case MVMS_CMD:
  868. priv->dnld_sent = DNLD_CMD_SENT;
  869. spin_lock_irqsave(&card->buffer_lock, flags);
  870. list_add_tail(&packet->list, &card->cmd_packet_list);
  871. spin_unlock_irqrestore(&card->buffer_lock, flags);
  872. break;
  873. case MVMS_DAT:
  874. priv->dnld_sent = DNLD_DATA_SENT;
  875. spin_lock_irqsave(&card->buffer_lock, flags);
  876. list_add_tail(&packet->list, &card->data_packet_list);
  877. spin_unlock_irqrestore(&card->buffer_lock, flags);
  878. break;
  879. default:
  880. netdev_err(priv->dev, "can't transfer buffer of type %d\n",
  881. type);
  882. err = -EINVAL;
  883. break;
  884. }
  885. /* Queue spi xfer work */
  886. queue_work(card->workqueue, &card->packet_work);
  887. out:
  888. lbs_deb_leave_args(LBS_DEB_SPI, "err=%d", err);
  889. return err;
  890. }
  891. /*
  892. * Host Interrupts
  893. *
  894. * Service incoming interrupts from the WLAN device. We can't sleep here, so
  895. * don't try to talk on the SPI bus, just queue the SPI xfer work.
  896. */
  897. static irqreturn_t if_spi_host_interrupt(int irq, void *dev_id)
  898. {
  899. struct if_spi_card *card = dev_id;
  900. queue_work(card->workqueue, &card->packet_work);
  901. return IRQ_HANDLED;
  902. }
  903. /*
  904. * SPI callbacks
  905. */
  906. static int if_spi_init_card(struct if_spi_card *card)
  907. {
  908. struct lbs_private *priv = card->priv;
  909. int err, i;
  910. u32 scratch;
  911. const struct firmware *helper = NULL;
  912. const struct firmware *mainfw = NULL;
  913. lbs_deb_enter(LBS_DEB_SPI);
  914. err = spu_init(card, card->pdata->use_dummy_writes);
  915. if (err)
  916. goto out;
  917. err = spu_get_chip_revision(card, &card->card_id, &card->card_rev);
  918. if (err)
  919. goto out;
  920. err = spu_read_u32(card, IF_SPI_SCRATCH_4_REG, &scratch);
  921. if (err)
  922. goto out;
  923. if (scratch == SUCCESSFUL_FW_DOWNLOAD_MAGIC)
  924. lbs_deb_spi("Firmware is already loaded for "
  925. "Marvell WLAN 802.11 adapter\n");
  926. else {
  927. /* Check if we support this card */
  928. for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
  929. if (card->card_id == fw_table[i].model)
  930. break;
  931. }
  932. if (i == ARRAY_SIZE(fw_table)) {
  933. netdev_err(priv->dev, "Unsupported chip_id: 0x%02x\n",
  934. card->card_id);
  935. err = -ENODEV;
  936. goto out;
  937. }
  938. err = lbs_get_firmware(&card->spi->dev, NULL, NULL,
  939. card->card_id, &fw_table[0], &helper,
  940. &mainfw);
  941. if (err) {
  942. netdev_err(priv->dev, "failed to find firmware (%d)\n",
  943. err);
  944. goto out;
  945. }
  946. lbs_deb_spi("Initializing FW for Marvell WLAN 802.11 adapter "
  947. "(chip_id = 0x%04x, chip_rev = 0x%02x) "
  948. "attached to SPI bus_num %d, chip_select %d. "
  949. "spi->max_speed_hz=%d\n",
  950. card->card_id, card->card_rev,
  951. card->spi->master->bus_num,
  952. card->spi->chip_select,
  953. card->spi->max_speed_hz);
  954. err = if_spi_prog_helper_firmware(card, helper);
  955. if (err)
  956. goto out;
  957. err = if_spi_prog_main_firmware(card, mainfw);
  958. if (err)
  959. goto out;
  960. lbs_deb_spi("loaded FW for Marvell WLAN 802.11 adapter\n");
  961. }
  962. err = spu_set_interrupt_mode(card, 0, 1);
  963. if (err)
  964. goto out;
  965. out:
  966. if (helper)
  967. release_firmware(helper);
  968. if (mainfw)
  969. release_firmware(mainfw);
  970. lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
  971. return err;
  972. }
  973. static void if_spi_resume_worker(struct work_struct *work)
  974. {
  975. struct if_spi_card *card;
  976. card = container_of(work, struct if_spi_card, resume_work);
  977. if (card->suspended) {
  978. if (card->pdata->setup)
  979. card->pdata->setup(card->spi);
  980. /* Init card ... */
  981. if_spi_init_card(card);
  982. enable_irq(card->spi->irq);
  983. /* And resume it ... */
  984. lbs_resume(card->priv);
  985. card->suspended = 0;
  986. }
  987. }
  988. static int __devinit if_spi_probe(struct spi_device *spi)
  989. {
  990. struct if_spi_card *card;
  991. struct lbs_private *priv = NULL;
  992. struct libertas_spi_platform_data *pdata = spi->dev.platform_data;
  993. int err = 0;
  994. lbs_deb_enter(LBS_DEB_SPI);
  995. if (!pdata) {
  996. err = -EINVAL;
  997. goto out;
  998. }
  999. if (pdata->setup) {
  1000. err = pdata->setup(spi);
  1001. if (err)
  1002. goto out;
  1003. }
  1004. /* Allocate card structure to represent this specific device */
  1005. card = kzalloc(sizeof(struct if_spi_card), GFP_KERNEL);
  1006. if (!card) {
  1007. err = -ENOMEM;
  1008. goto teardown;
  1009. }
  1010. spi_set_drvdata(spi, card);
  1011. card->pdata = pdata;
  1012. card->spi = spi;
  1013. card->prev_xfer_time = jiffies;
  1014. INIT_LIST_HEAD(&card->cmd_packet_list);
  1015. INIT_LIST_HEAD(&card->data_packet_list);
  1016. spin_lock_init(&card->buffer_lock);
  1017. /* Initialize the SPI Interface Unit */
  1018. /* Firmware load */
  1019. err = if_spi_init_card(card);
  1020. if (err)
  1021. goto free_card;
  1022. /*
  1023. * Register our card with libertas.
  1024. * This will call alloc_etherdev.
  1025. */
  1026. priv = lbs_add_card(card, &spi->dev);
  1027. if (!priv) {
  1028. err = -ENOMEM;
  1029. goto free_card;
  1030. }
  1031. card->priv = priv;
  1032. priv->setup_fw_on_resume = 1;
  1033. priv->card = card;
  1034. priv->hw_host_to_card = if_spi_host_to_card;
  1035. priv->enter_deep_sleep = NULL;
  1036. priv->exit_deep_sleep = NULL;
  1037. priv->reset_deep_sleep_wakeup = NULL;
  1038. priv->fw_ready = 1;
  1039. /* Initialize interrupt handling stuff. */
  1040. card->workqueue = create_workqueue("libertas_spi");
  1041. INIT_WORK(&card->packet_work, if_spi_host_to_card_worker);
  1042. INIT_WORK(&card->resume_work, if_spi_resume_worker);
  1043. err = request_irq(spi->irq, if_spi_host_interrupt,
  1044. IRQF_TRIGGER_FALLING, "libertas_spi", card);
  1045. if (err) {
  1046. pr_err("can't get host irq line-- request_irq failed\n");
  1047. goto terminate_workqueue;
  1048. }
  1049. /*
  1050. * Start the card.
  1051. * This will call register_netdev, and we'll start
  1052. * getting interrupts...
  1053. */
  1054. err = lbs_start_card(priv);
  1055. if (err)
  1056. goto release_irq;
  1057. lbs_deb_spi("Finished initializing WLAN module.\n");
  1058. /* successful exit */
  1059. goto out;
  1060. release_irq:
  1061. free_irq(spi->irq, card);
  1062. terminate_workqueue:
  1063. flush_workqueue(card->workqueue);
  1064. destroy_workqueue(card->workqueue);
  1065. lbs_remove_card(priv); /* will call free_netdev */
  1066. free_card:
  1067. free_if_spi_card(card);
  1068. teardown:
  1069. if (pdata->teardown)
  1070. pdata->teardown(spi);
  1071. out:
  1072. lbs_deb_leave_args(LBS_DEB_SPI, "err %d\n", err);
  1073. return err;
  1074. }
  1075. static int __devexit libertas_spi_remove(struct spi_device *spi)
  1076. {
  1077. struct if_spi_card *card = spi_get_drvdata(spi);
  1078. struct lbs_private *priv = card->priv;
  1079. lbs_deb_spi("libertas_spi_remove\n");
  1080. lbs_deb_enter(LBS_DEB_SPI);
  1081. cancel_work_sync(&card->resume_work);
  1082. lbs_stop_card(priv);
  1083. lbs_remove_card(priv); /* will call free_netdev */
  1084. free_irq(spi->irq, card);
  1085. flush_workqueue(card->workqueue);
  1086. destroy_workqueue(card->workqueue);
  1087. if (card->pdata->teardown)
  1088. card->pdata->teardown(spi);
  1089. free_if_spi_card(card);
  1090. lbs_deb_leave(LBS_DEB_SPI);
  1091. return 0;
  1092. }
  1093. static int if_spi_suspend(struct device *dev)
  1094. {
  1095. struct spi_device *spi = to_spi_device(dev);
  1096. struct if_spi_card *card = spi_get_drvdata(spi);
  1097. if (!card->suspended) {
  1098. lbs_suspend(card->priv);
  1099. flush_workqueue(card->workqueue);
  1100. disable_irq(spi->irq);
  1101. if (card->pdata->teardown)
  1102. card->pdata->teardown(spi);
  1103. card->suspended = 1;
  1104. }
  1105. return 0;
  1106. }
  1107. static int if_spi_resume(struct device *dev)
  1108. {
  1109. struct spi_device *spi = to_spi_device(dev);
  1110. struct if_spi_card *card = spi_get_drvdata(spi);
  1111. /* Schedule delayed work */
  1112. schedule_work(&card->resume_work);
  1113. return 0;
  1114. }
  1115. static const struct dev_pm_ops if_spi_pm_ops = {
  1116. .suspend = if_spi_suspend,
  1117. .resume = if_spi_resume,
  1118. };
  1119. static struct spi_driver libertas_spi_driver = {
  1120. .probe = if_spi_probe,
  1121. .remove = __devexit_p(libertas_spi_remove),
  1122. .driver = {
  1123. .name = "libertas_spi",
  1124. .bus = &spi_bus_type,
  1125. .owner = THIS_MODULE,
  1126. .pm = &if_spi_pm_ops,
  1127. },
  1128. };
  1129. /*
  1130. * Module functions
  1131. */
  1132. static int __init if_spi_init_module(void)
  1133. {
  1134. int ret = 0;
  1135. lbs_deb_enter(LBS_DEB_SPI);
  1136. printk(KERN_INFO "libertas_spi: Libertas SPI driver\n");
  1137. ret = spi_register_driver(&libertas_spi_driver);
  1138. lbs_deb_leave(LBS_DEB_SPI);
  1139. return ret;
  1140. }
  1141. static void __exit if_spi_exit_module(void)
  1142. {
  1143. lbs_deb_enter(LBS_DEB_SPI);
  1144. spi_unregister_driver(&libertas_spi_driver);
  1145. lbs_deb_leave(LBS_DEB_SPI);
  1146. }
  1147. module_init(if_spi_init_module);
  1148. module_exit(if_spi_exit_module);
  1149. MODULE_DESCRIPTION("Libertas SPI WLAN Driver");
  1150. MODULE_AUTHOR("Andrey Yurovsky <andrey@cozybit.com>, "
  1151. "Colin McCabe <colin@cozybit.com>");
  1152. MODULE_LICENSE("GPL");
  1153. MODULE_ALIAS("spi:libertas_spi");