caif_hsi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com
  4. * Author: Daniel Martensson / daniel.martensson@stericsson.com
  5. * Dmitry.Tarnyagin / dmitry.tarnyagin@stericsson.com
  6. * License terms: GNU General Public License (GPL) version 2.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/string.h>
  14. #include <linux/list.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/timer.h>
  20. #include <linux/rtnetlink.h>
  21. #include <net/caif/caif_layer.h>
  22. #include <net/caif/caif_hsi.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>");
  25. MODULE_DESCRIPTION("CAIF HSI driver");
  26. /* Returns the number of padding bytes for alignment. */
  27. #define PAD_POW2(x, pow) ((((x)&((pow)-1)) == 0) ? 0 :\
  28. (((pow)-((x)&((pow)-1)))))
  29. static int inactivity_timeout = 1000;
  30. module_param(inactivity_timeout, int, S_IRUGO | S_IWUSR);
  31. MODULE_PARM_DESC(inactivity_timeout, "Inactivity timeout on HSI, ms.");
  32. /*
  33. * HSI padding options.
  34. * Warning: must be a base of 2 (& operation used) and can not be zero !
  35. */
  36. static int hsi_head_align = 4;
  37. module_param(hsi_head_align, int, S_IRUGO);
  38. MODULE_PARM_DESC(hsi_head_align, "HSI head alignment.");
  39. static int hsi_tail_align = 4;
  40. module_param(hsi_tail_align, int, S_IRUGO);
  41. MODULE_PARM_DESC(hsi_tail_align, "HSI tail alignment.");
  42. /*
  43. * HSI link layer flowcontrol thresholds.
  44. * Warning: A high threshold value migth increase throughput but it will at
  45. * the same time prevent channel prioritization and increase the risk of
  46. * flooding the modem. The high threshold should be above the low.
  47. */
  48. static int hsi_high_threshold = 100;
  49. module_param(hsi_high_threshold, int, S_IRUGO);
  50. MODULE_PARM_DESC(hsi_high_threshold, "HSI high threshold (FLOW OFF).");
  51. static int hsi_low_threshold = 50;
  52. module_param(hsi_low_threshold, int, S_IRUGO);
  53. MODULE_PARM_DESC(hsi_low_threshold, "HSI high threshold (FLOW ON).");
  54. #define ON 1
  55. #define OFF 0
  56. /*
  57. * Threshold values for the HSI packet queue. Flowcontrol will be asserted
  58. * when the number of packets exceeds HIGH_WATER_MARK. It will not be
  59. * de-asserted before the number of packets drops below LOW_WATER_MARK.
  60. */
  61. #define LOW_WATER_MARK hsi_low_threshold
  62. #define HIGH_WATER_MARK hsi_high_threshold
  63. static LIST_HEAD(cfhsi_list);
  64. static spinlock_t cfhsi_list_lock;
  65. static void cfhsi_inactivity_tout(unsigned long arg)
  66. {
  67. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  68. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  69. __func__);
  70. /* Schedule power down work queue. */
  71. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  72. queue_work(cfhsi->wq, &cfhsi->wake_down_work);
  73. }
  74. static void cfhsi_abort_tx(struct cfhsi *cfhsi)
  75. {
  76. struct sk_buff *skb;
  77. for (;;) {
  78. spin_lock_bh(&cfhsi->lock);
  79. skb = skb_dequeue(&cfhsi->qhead);
  80. if (!skb)
  81. break;
  82. cfhsi->ndev->stats.tx_errors++;
  83. cfhsi->ndev->stats.tx_dropped++;
  84. spin_unlock_bh(&cfhsi->lock);
  85. kfree_skb(skb);
  86. }
  87. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  88. if (!test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  89. mod_timer(&cfhsi->timer,
  90. jiffies + cfhsi->inactivity_timeout);
  91. spin_unlock_bh(&cfhsi->lock);
  92. }
  93. static int cfhsi_flush_fifo(struct cfhsi *cfhsi)
  94. {
  95. char buffer[32]; /* Any reasonable value */
  96. size_t fifo_occupancy;
  97. int ret;
  98. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  99. __func__);
  100. do {
  101. ret = cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  102. &fifo_occupancy);
  103. if (ret) {
  104. dev_warn(&cfhsi->ndev->dev,
  105. "%s: can't get FIFO occupancy: %d.\n",
  106. __func__, ret);
  107. break;
  108. } else if (!fifo_occupancy)
  109. /* No more data, exitting normally */
  110. break;
  111. fifo_occupancy = min(sizeof(buffer), fifo_occupancy);
  112. set_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  113. ret = cfhsi->dev->cfhsi_rx(buffer, fifo_occupancy,
  114. cfhsi->dev);
  115. if (ret) {
  116. clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits);
  117. dev_warn(&cfhsi->ndev->dev,
  118. "%s: can't read data: %d.\n",
  119. __func__, ret);
  120. break;
  121. }
  122. ret = 5 * HZ;
  123. ret = wait_event_interruptible_timeout(cfhsi->flush_fifo_wait,
  124. !test_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits), ret);
  125. if (ret < 0) {
  126. dev_warn(&cfhsi->ndev->dev,
  127. "%s: can't wait for flush complete: %d.\n",
  128. __func__, ret);
  129. break;
  130. } else if (!ret) {
  131. ret = -ETIMEDOUT;
  132. dev_warn(&cfhsi->ndev->dev,
  133. "%s: timeout waiting for flush complete.\n",
  134. __func__);
  135. break;
  136. }
  137. } while (1);
  138. return ret;
  139. }
  140. static int cfhsi_tx_frm(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  141. {
  142. int nfrms = 0;
  143. int pld_len = 0;
  144. struct sk_buff *skb;
  145. u8 *pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  146. skb = skb_dequeue(&cfhsi->qhead);
  147. if (!skb)
  148. return 0;
  149. /* Clear offset. */
  150. desc->offset = 0;
  151. /* Check if we can embed a CAIF frame. */
  152. if (skb->len < CFHSI_MAX_EMB_FRM_SZ) {
  153. struct caif_payload_info *info;
  154. int hpad = 0;
  155. int tpad = 0;
  156. /* Calculate needed head alignment and tail alignment. */
  157. info = (struct caif_payload_info *)&skb->cb;
  158. hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
  159. tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
  160. /* Check if frame still fits with added alignment. */
  161. if ((skb->len + hpad + tpad) <= CFHSI_MAX_EMB_FRM_SZ) {
  162. u8 *pemb = desc->emb_frm;
  163. desc->offset = CFHSI_DESC_SHORT_SZ;
  164. *pemb = (u8)(hpad - 1);
  165. pemb += hpad;
  166. /* Update network statistics. */
  167. cfhsi->ndev->stats.tx_packets++;
  168. cfhsi->ndev->stats.tx_bytes += skb->len;
  169. /* Copy in embedded CAIF frame. */
  170. skb_copy_bits(skb, 0, pemb, skb->len);
  171. consume_skb(skb);
  172. skb = NULL;
  173. }
  174. }
  175. /* Create payload CAIF frames. */
  176. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  177. while (nfrms < CFHSI_MAX_PKTS) {
  178. struct caif_payload_info *info;
  179. int hpad = 0;
  180. int tpad = 0;
  181. if (!skb)
  182. skb = skb_dequeue(&cfhsi->qhead);
  183. if (!skb)
  184. break;
  185. /* Calculate needed head alignment and tail alignment. */
  186. info = (struct caif_payload_info *)&skb->cb;
  187. hpad = 1 + PAD_POW2((info->hdr_len + 1), hsi_head_align);
  188. tpad = PAD_POW2((skb->len + hpad), hsi_tail_align);
  189. /* Fill in CAIF frame length in descriptor. */
  190. desc->cffrm_len[nfrms] = hpad + skb->len + tpad;
  191. /* Fill head padding information. */
  192. *pfrm = (u8)(hpad - 1);
  193. pfrm += hpad;
  194. /* Update network statistics. */
  195. cfhsi->ndev->stats.tx_packets++;
  196. cfhsi->ndev->stats.tx_bytes += skb->len;
  197. /* Copy in CAIF frame. */
  198. skb_copy_bits(skb, 0, pfrm, skb->len);
  199. /* Update payload length. */
  200. pld_len += desc->cffrm_len[nfrms];
  201. /* Update frame pointer. */
  202. pfrm += skb->len + tpad;
  203. consume_skb(skb);
  204. skb = NULL;
  205. /* Update number of frames. */
  206. nfrms++;
  207. }
  208. /* Unused length fields should be zero-filled (according to SPEC). */
  209. while (nfrms < CFHSI_MAX_PKTS) {
  210. desc->cffrm_len[nfrms] = 0x0000;
  211. nfrms++;
  212. }
  213. /* Check if we can piggy-back another descriptor. */
  214. skb = skb_peek(&cfhsi->qhead);
  215. if (skb)
  216. desc->header |= CFHSI_PIGGY_DESC;
  217. else
  218. desc->header &= ~CFHSI_PIGGY_DESC;
  219. return CFHSI_DESC_SZ + pld_len;
  220. }
  221. static void cfhsi_tx_done(struct cfhsi *cfhsi)
  222. {
  223. struct cfhsi_desc *desc = NULL;
  224. int len = 0;
  225. int res;
  226. dev_dbg(&cfhsi->ndev->dev, "%s.\n", __func__);
  227. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  228. return;
  229. desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  230. do {
  231. /*
  232. * Send flow on if flow off has been previously signalled
  233. * and number of packets is below low water mark.
  234. */
  235. spin_lock_bh(&cfhsi->lock);
  236. if (cfhsi->flow_off_sent &&
  237. cfhsi->qhead.qlen <= cfhsi->q_low_mark &&
  238. cfhsi->cfdev.flowctrl) {
  239. cfhsi->flow_off_sent = 0;
  240. cfhsi->cfdev.flowctrl(cfhsi->ndev, ON);
  241. }
  242. spin_unlock_bh(&cfhsi->lock);
  243. /* Create HSI frame. */
  244. do {
  245. len = cfhsi_tx_frm(desc, cfhsi);
  246. if (!len) {
  247. spin_lock_bh(&cfhsi->lock);
  248. if (unlikely(skb_peek(&cfhsi->qhead))) {
  249. spin_unlock_bh(&cfhsi->lock);
  250. continue;
  251. }
  252. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  253. /* Start inactivity timer. */
  254. mod_timer(&cfhsi->timer,
  255. jiffies + cfhsi->inactivity_timeout);
  256. spin_unlock_bh(&cfhsi->lock);
  257. goto done;
  258. }
  259. } while (!len);
  260. /* Set up new transfer. */
  261. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  262. if (WARN_ON(res < 0)) {
  263. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  264. __func__, res);
  265. }
  266. } while (res < 0);
  267. done:
  268. return;
  269. }
  270. static void cfhsi_tx_done_cb(struct cfhsi_drv *drv)
  271. {
  272. struct cfhsi *cfhsi;
  273. cfhsi = container_of(drv, struct cfhsi, drv);
  274. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  275. __func__);
  276. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  277. return;
  278. cfhsi_tx_done(cfhsi);
  279. }
  280. static int cfhsi_rx_desc(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  281. {
  282. int xfer_sz = 0;
  283. int nfrms = 0;
  284. u16 *plen = NULL;
  285. u8 *pfrm = NULL;
  286. if ((desc->header & ~CFHSI_PIGGY_DESC) ||
  287. (desc->offset > CFHSI_MAX_EMB_FRM_SZ)) {
  288. dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
  289. __func__);
  290. return -EPROTO;
  291. }
  292. /* Check for embedded CAIF frame. */
  293. if (desc->offset) {
  294. struct sk_buff *skb;
  295. u8 *dst = NULL;
  296. int len = 0;
  297. pfrm = ((u8 *)desc) + desc->offset;
  298. /* Remove offset padding. */
  299. pfrm += *pfrm + 1;
  300. /* Read length of CAIF frame (little endian). */
  301. len = *pfrm;
  302. len |= ((*(pfrm+1)) << 8) & 0xFF00;
  303. len += 2; /* Add FCS fields. */
  304. /* Sanity check length of CAIF frame. */
  305. if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
  306. dev_err(&cfhsi->ndev->dev, "%s: Invalid length.\n",
  307. __func__);
  308. return -EPROTO;
  309. }
  310. /* Allocate SKB (OK even in IRQ context). */
  311. skb = alloc_skb(len + 1, GFP_ATOMIC);
  312. if (!skb) {
  313. dev_err(&cfhsi->ndev->dev, "%s: Out of memory !\n",
  314. __func__);
  315. return -ENOMEM;
  316. }
  317. caif_assert(skb != NULL);
  318. dst = skb_put(skb, len);
  319. memcpy(dst, pfrm, len);
  320. skb->protocol = htons(ETH_P_CAIF);
  321. skb_reset_mac_header(skb);
  322. skb->dev = cfhsi->ndev;
  323. /*
  324. * We are called from a arch specific platform device.
  325. * Unfortunately we don't know what context we're
  326. * running in.
  327. */
  328. if (in_interrupt())
  329. netif_rx(skb);
  330. else
  331. netif_rx_ni(skb);
  332. /* Update network statistics. */
  333. cfhsi->ndev->stats.rx_packets++;
  334. cfhsi->ndev->stats.rx_bytes += len;
  335. }
  336. /* Calculate transfer length. */
  337. plen = desc->cffrm_len;
  338. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  339. xfer_sz += *plen;
  340. plen++;
  341. nfrms++;
  342. }
  343. /* Check for piggy-backed descriptor. */
  344. if (desc->header & CFHSI_PIGGY_DESC)
  345. xfer_sz += CFHSI_DESC_SZ;
  346. if ((xfer_sz % 4) || (xfer_sz > (CFHSI_BUF_SZ_RX - CFHSI_DESC_SZ))) {
  347. dev_err(&cfhsi->ndev->dev,
  348. "%s: Invalid payload len: %d, ignored.\n",
  349. __func__, xfer_sz);
  350. return -EPROTO;
  351. }
  352. return xfer_sz;
  353. }
  354. static int cfhsi_rx_pld(struct cfhsi_desc *desc, struct cfhsi *cfhsi)
  355. {
  356. int rx_sz = 0;
  357. int nfrms = 0;
  358. u16 *plen = NULL;
  359. u8 *pfrm = NULL;
  360. /* Sanity check header and offset. */
  361. if (WARN_ON((desc->header & ~CFHSI_PIGGY_DESC) ||
  362. (desc->offset > CFHSI_MAX_EMB_FRM_SZ))) {
  363. dev_err(&cfhsi->ndev->dev, "%s: Invalid descriptor.\n",
  364. __func__);
  365. return -EPROTO;
  366. }
  367. /* Set frame pointer to start of payload. */
  368. pfrm = desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ;
  369. plen = desc->cffrm_len;
  370. /* Skip already processed frames. */
  371. while (nfrms < cfhsi->rx_state.nfrms) {
  372. pfrm += *plen;
  373. rx_sz += *plen;
  374. plen++;
  375. nfrms++;
  376. }
  377. /* Parse payload. */
  378. while (nfrms < CFHSI_MAX_PKTS && *plen) {
  379. struct sk_buff *skb;
  380. u8 *dst = NULL;
  381. u8 *pcffrm = NULL;
  382. int len = 0;
  383. /* CAIF frame starts after head padding. */
  384. pcffrm = pfrm + *pfrm + 1;
  385. /* Read length of CAIF frame (little endian). */
  386. len = *pcffrm;
  387. len |= ((*(pcffrm + 1)) << 8) & 0xFF00;
  388. len += 2; /* Add FCS fields. */
  389. /* Sanity check length of CAIF frames. */
  390. if (unlikely(len > CFHSI_MAX_CAIF_FRAME_SZ)) {
  391. dev_err(&cfhsi->ndev->dev, "%s: Invalid length.\n",
  392. __func__);
  393. return -EPROTO;
  394. }
  395. /* Allocate SKB (OK even in IRQ context). */
  396. skb = alloc_skb(len + 1, GFP_ATOMIC);
  397. if (!skb) {
  398. dev_err(&cfhsi->ndev->dev, "%s: Out of memory !\n",
  399. __func__);
  400. cfhsi->rx_state.nfrms = nfrms;
  401. return -ENOMEM;
  402. }
  403. caif_assert(skb != NULL);
  404. dst = skb_put(skb, len);
  405. memcpy(dst, pcffrm, len);
  406. skb->protocol = htons(ETH_P_CAIF);
  407. skb_reset_mac_header(skb);
  408. skb->dev = cfhsi->ndev;
  409. /*
  410. * We're called from a platform device,
  411. * and don't know the context we're running in.
  412. */
  413. if (in_interrupt())
  414. netif_rx(skb);
  415. else
  416. netif_rx_ni(skb);
  417. /* Update network statistics. */
  418. cfhsi->ndev->stats.rx_packets++;
  419. cfhsi->ndev->stats.rx_bytes += len;
  420. pfrm += *plen;
  421. rx_sz += *plen;
  422. plen++;
  423. nfrms++;
  424. }
  425. return rx_sz;
  426. }
  427. static void cfhsi_rx_done(struct cfhsi *cfhsi)
  428. {
  429. int res;
  430. int desc_pld_len = 0;
  431. struct cfhsi_desc *desc = NULL;
  432. desc = (struct cfhsi_desc *)cfhsi->rx_buf;
  433. dev_dbg(&cfhsi->ndev->dev, "%s\n", __func__);
  434. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  435. return;
  436. /* Update inactivity timer if pending. */
  437. spin_lock_bh(&cfhsi->lock);
  438. mod_timer_pending(&cfhsi->timer,
  439. jiffies + cfhsi->inactivity_timeout);
  440. spin_unlock_bh(&cfhsi->lock);
  441. if (cfhsi->rx_state.state == CFHSI_RX_STATE_DESC) {
  442. desc_pld_len = cfhsi_rx_desc(desc, cfhsi);
  443. if (desc_pld_len == -ENOMEM)
  444. goto restart;
  445. if (desc_pld_len == -EPROTO)
  446. goto out_of_sync;
  447. } else {
  448. int pld_len;
  449. if (!cfhsi->rx_state.piggy_desc) {
  450. pld_len = cfhsi_rx_pld(desc, cfhsi);
  451. if (pld_len == -ENOMEM)
  452. goto restart;
  453. if (pld_len == -EPROTO)
  454. goto out_of_sync;
  455. cfhsi->rx_state.pld_len = pld_len;
  456. } else {
  457. pld_len = cfhsi->rx_state.pld_len;
  458. }
  459. if ((pld_len > 0) && (desc->header & CFHSI_PIGGY_DESC)) {
  460. struct cfhsi_desc *piggy_desc;
  461. piggy_desc = (struct cfhsi_desc *)
  462. (desc->emb_frm + CFHSI_MAX_EMB_FRM_SZ +
  463. pld_len);
  464. cfhsi->rx_state.piggy_desc = true;
  465. /* Extract piggy-backed descriptor. */
  466. desc_pld_len = cfhsi_rx_desc(piggy_desc, cfhsi);
  467. if (desc_pld_len == -ENOMEM)
  468. goto restart;
  469. /*
  470. * Copy needed information from the piggy-backed
  471. * descriptor to the descriptor in the start.
  472. */
  473. memcpy((u8 *)desc, (u8 *)piggy_desc,
  474. CFHSI_DESC_SHORT_SZ);
  475. if (desc_pld_len == -EPROTO)
  476. goto out_of_sync;
  477. }
  478. }
  479. memset(&cfhsi->rx_state, 0, sizeof(cfhsi->rx_state));
  480. if (desc_pld_len) {
  481. cfhsi->rx_state.state = CFHSI_RX_STATE_PAYLOAD;
  482. cfhsi->rx_ptr = cfhsi->rx_buf + CFHSI_DESC_SZ;
  483. cfhsi->rx_len = desc_pld_len;
  484. } else {
  485. cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
  486. cfhsi->rx_ptr = cfhsi->rx_buf;
  487. cfhsi->rx_len = CFHSI_DESC_SZ;
  488. }
  489. if (test_bit(CFHSI_AWAKE, &cfhsi->bits)) {
  490. /* Set up new transfer. */
  491. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n",
  492. __func__);
  493. res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len,
  494. cfhsi->dev);
  495. if (WARN_ON(res < 0)) {
  496. dev_err(&cfhsi->ndev->dev, "%s: RX error %d.\n",
  497. __func__, res);
  498. cfhsi->ndev->stats.rx_errors++;
  499. cfhsi->ndev->stats.rx_dropped++;
  500. }
  501. }
  502. return;
  503. restart:
  504. if (++cfhsi->rx_state.retries > CFHSI_MAX_RX_RETRIES) {
  505. dev_err(&cfhsi->ndev->dev, "%s: No memory available "
  506. "in %d iterations.\n",
  507. __func__, CFHSI_MAX_RX_RETRIES);
  508. BUG();
  509. }
  510. mod_timer(&cfhsi->rx_slowpath_timer, jiffies + 1);
  511. return;
  512. out_of_sync:
  513. dev_err(&cfhsi->ndev->dev, "%s: Out of sync.\n", __func__);
  514. print_hex_dump_bytes("--> ", DUMP_PREFIX_NONE,
  515. cfhsi->rx_buf, CFHSI_DESC_SZ);
  516. schedule_work(&cfhsi->out_of_sync_work);
  517. }
  518. static void cfhsi_rx_slowpath(unsigned long arg)
  519. {
  520. struct cfhsi *cfhsi = (struct cfhsi *)arg;
  521. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  522. __func__);
  523. cfhsi_rx_done(cfhsi);
  524. }
  525. static void cfhsi_rx_done_cb(struct cfhsi_drv *drv)
  526. {
  527. struct cfhsi *cfhsi;
  528. cfhsi = container_of(drv, struct cfhsi, drv);
  529. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  530. __func__);
  531. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  532. return;
  533. if (test_and_clear_bit(CFHSI_FLUSH_FIFO, &cfhsi->bits))
  534. wake_up_interruptible(&cfhsi->flush_fifo_wait);
  535. else
  536. cfhsi_rx_done(cfhsi);
  537. }
  538. static void cfhsi_wake_up(struct work_struct *work)
  539. {
  540. struct cfhsi *cfhsi = NULL;
  541. int res;
  542. int len;
  543. long ret;
  544. cfhsi = container_of(work, struct cfhsi, wake_up_work);
  545. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  546. return;
  547. if (unlikely(test_bit(CFHSI_AWAKE, &cfhsi->bits))) {
  548. /* It happenes when wakeup is requested by
  549. * both ends at the same time. */
  550. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  551. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  552. return;
  553. }
  554. /* Activate wake line. */
  555. cfhsi->dev->cfhsi_wake_up(cfhsi->dev);
  556. dev_dbg(&cfhsi->ndev->dev, "%s: Start waiting.\n",
  557. __func__);
  558. /* Wait for acknowledge. */
  559. ret = CFHSI_WAKE_TOUT;
  560. ret = wait_event_interruptible_timeout(cfhsi->wake_up_wait,
  561. test_and_clear_bit(CFHSI_WAKE_UP_ACK,
  562. &cfhsi->bits), ret);
  563. if (unlikely(ret < 0)) {
  564. /* Interrupted by signal. */
  565. dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  566. __func__, ret);
  567. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  568. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  569. return;
  570. } else if (!ret) {
  571. bool ca_wake = false;
  572. size_t fifo_occupancy = 0;
  573. /* Wakeup timeout */
  574. dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n",
  575. __func__);
  576. /* Check FIFO to check if modem has sent something. */
  577. WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  578. &fifo_occupancy));
  579. dev_err(&cfhsi->ndev->dev, "%s: Bytes in FIFO: %u.\n",
  580. __func__, (unsigned) fifo_occupancy);
  581. /* Check if we misssed the interrupt. */
  582. WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
  583. &ca_wake));
  584. if (ca_wake) {
  585. dev_err(&cfhsi->ndev->dev, "%s: CA Wake missed !.\n",
  586. __func__);
  587. /* Clear the CFHSI_WAKE_UP_ACK bit to prevent race. */
  588. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  589. /* Continue execution. */
  590. goto wake_ack;
  591. }
  592. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  593. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  594. return;
  595. }
  596. wake_ack:
  597. dev_dbg(&cfhsi->ndev->dev, "%s: Woken.\n",
  598. __func__);
  599. /* Clear power up bit. */
  600. set_bit(CFHSI_AWAKE, &cfhsi->bits);
  601. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  602. /* Resume read operation. */
  603. dev_dbg(&cfhsi->ndev->dev, "%s: Start RX.\n", __func__);
  604. res = cfhsi->dev->cfhsi_rx(cfhsi->rx_ptr, cfhsi->rx_len, cfhsi->dev);
  605. if (WARN_ON(res < 0))
  606. dev_err(&cfhsi->ndev->dev, "%s: RX err %d.\n", __func__, res);
  607. /* Clear power up acknowledment. */
  608. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  609. spin_lock_bh(&cfhsi->lock);
  610. /* Resume transmit if queue is not empty. */
  611. if (!skb_peek(&cfhsi->qhead)) {
  612. dev_dbg(&cfhsi->ndev->dev, "%s: Peer wake, start timer.\n",
  613. __func__);
  614. /* Start inactivity timer. */
  615. mod_timer(&cfhsi->timer,
  616. jiffies + cfhsi->inactivity_timeout);
  617. spin_unlock_bh(&cfhsi->lock);
  618. return;
  619. }
  620. dev_dbg(&cfhsi->ndev->dev, "%s: Host wake.\n",
  621. __func__);
  622. spin_unlock_bh(&cfhsi->lock);
  623. /* Create HSI frame. */
  624. len = cfhsi_tx_frm((struct cfhsi_desc *)cfhsi->tx_buf, cfhsi);
  625. if (likely(len > 0)) {
  626. /* Set up new transfer. */
  627. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  628. if (WARN_ON(res < 0)) {
  629. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  630. __func__, res);
  631. cfhsi_abort_tx(cfhsi);
  632. }
  633. } else {
  634. dev_err(&cfhsi->ndev->dev,
  635. "%s: Failed to create HSI frame: %d.\n",
  636. __func__, len);
  637. }
  638. }
  639. static void cfhsi_wake_down(struct work_struct *work)
  640. {
  641. long ret;
  642. struct cfhsi *cfhsi = NULL;
  643. size_t fifo_occupancy = 0;
  644. int retry = CFHSI_WAKE_TOUT;
  645. cfhsi = container_of(work, struct cfhsi, wake_down_work);
  646. dev_dbg(&cfhsi->ndev->dev, "%s.\n", __func__);
  647. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  648. return;
  649. /* Deactivate wake line. */
  650. cfhsi->dev->cfhsi_wake_down(cfhsi->dev);
  651. /* Wait for acknowledge. */
  652. ret = CFHSI_WAKE_TOUT;
  653. ret = wait_event_interruptible_timeout(cfhsi->wake_down_wait,
  654. test_and_clear_bit(CFHSI_WAKE_DOWN_ACK,
  655. &cfhsi->bits), ret);
  656. if (ret < 0) {
  657. /* Interrupted by signal. */
  658. dev_err(&cfhsi->ndev->dev, "%s: Signalled: %ld.\n",
  659. __func__, ret);
  660. return;
  661. } else if (!ret) {
  662. bool ca_wake = true;
  663. /* Timeout */
  664. dev_err(&cfhsi->ndev->dev, "%s: Timeout.\n", __func__);
  665. /* Check if we misssed the interrupt. */
  666. WARN_ON(cfhsi->dev->cfhsi_get_peer_wake(cfhsi->dev,
  667. &ca_wake));
  668. if (!ca_wake)
  669. dev_err(&cfhsi->ndev->dev, "%s: CA Wake missed !.\n",
  670. __func__);
  671. }
  672. /* Check FIFO occupancy. */
  673. while (retry) {
  674. WARN_ON(cfhsi->dev->cfhsi_fifo_occupancy(cfhsi->dev,
  675. &fifo_occupancy));
  676. if (!fifo_occupancy)
  677. break;
  678. set_current_state(TASK_INTERRUPTIBLE);
  679. schedule_timeout(1);
  680. retry--;
  681. }
  682. if (!retry)
  683. dev_err(&cfhsi->ndev->dev, "%s: FIFO Timeout.\n", __func__);
  684. /* Clear AWAKE condition. */
  685. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  686. /* Cancel pending RX requests. */
  687. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  688. }
  689. static void cfhsi_out_of_sync(struct work_struct *work)
  690. {
  691. struct cfhsi *cfhsi = NULL;
  692. cfhsi = container_of(work, struct cfhsi, out_of_sync_work);
  693. rtnl_lock();
  694. dev_close(cfhsi->ndev);
  695. rtnl_unlock();
  696. }
  697. static void cfhsi_wake_up_cb(struct cfhsi_drv *drv)
  698. {
  699. struct cfhsi *cfhsi = NULL;
  700. cfhsi = container_of(drv, struct cfhsi, drv);
  701. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  702. __func__);
  703. set_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  704. wake_up_interruptible(&cfhsi->wake_up_wait);
  705. if (test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))
  706. return;
  707. /* Schedule wake up work queue if the peer initiates. */
  708. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  709. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  710. }
  711. static void cfhsi_wake_down_cb(struct cfhsi_drv *drv)
  712. {
  713. struct cfhsi *cfhsi = NULL;
  714. cfhsi = container_of(drv, struct cfhsi, drv);
  715. dev_dbg(&cfhsi->ndev->dev, "%s.\n",
  716. __func__);
  717. /* Initiating low power is only permitted by the host (us). */
  718. set_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  719. wake_up_interruptible(&cfhsi->wake_down_wait);
  720. }
  721. static int cfhsi_xmit(struct sk_buff *skb, struct net_device *dev)
  722. {
  723. struct cfhsi *cfhsi = NULL;
  724. int start_xfer = 0;
  725. int timer_active;
  726. if (!dev)
  727. return -EINVAL;
  728. cfhsi = netdev_priv(dev);
  729. spin_lock_bh(&cfhsi->lock);
  730. skb_queue_tail(&cfhsi->qhead, skb);
  731. /* Sanity check; xmit should not be called after unregister_netdev */
  732. if (WARN_ON(test_bit(CFHSI_SHUTDOWN, &cfhsi->bits))) {
  733. spin_unlock_bh(&cfhsi->lock);
  734. cfhsi_abort_tx(cfhsi);
  735. return -EINVAL;
  736. }
  737. /* Send flow off if number of packets is above high water mark. */
  738. if (!cfhsi->flow_off_sent &&
  739. cfhsi->qhead.qlen > cfhsi->q_high_mark &&
  740. cfhsi->cfdev.flowctrl) {
  741. cfhsi->flow_off_sent = 1;
  742. cfhsi->cfdev.flowctrl(cfhsi->ndev, OFF);
  743. }
  744. if (cfhsi->tx_state == CFHSI_TX_STATE_IDLE) {
  745. cfhsi->tx_state = CFHSI_TX_STATE_XFER;
  746. start_xfer = 1;
  747. }
  748. if (!start_xfer) {
  749. spin_unlock_bh(&cfhsi->lock);
  750. return 0;
  751. }
  752. /* Delete inactivity timer if started. */
  753. timer_active = del_timer_sync(&cfhsi->timer);
  754. spin_unlock_bh(&cfhsi->lock);
  755. if (timer_active) {
  756. struct cfhsi_desc *desc = (struct cfhsi_desc *)cfhsi->tx_buf;
  757. int len;
  758. int res;
  759. /* Create HSI frame. */
  760. len = cfhsi_tx_frm(desc, cfhsi);
  761. WARN_ON(!len);
  762. /* Set up new transfer. */
  763. res = cfhsi->dev->cfhsi_tx(cfhsi->tx_buf, len, cfhsi->dev);
  764. if (WARN_ON(res < 0)) {
  765. dev_err(&cfhsi->ndev->dev, "%s: TX error %d.\n",
  766. __func__, res);
  767. cfhsi_abort_tx(cfhsi);
  768. }
  769. } else {
  770. /* Schedule wake up work queue if the we initiate. */
  771. if (!test_and_set_bit(CFHSI_WAKE_UP, &cfhsi->bits))
  772. queue_work(cfhsi->wq, &cfhsi->wake_up_work);
  773. }
  774. return 0;
  775. }
  776. static int cfhsi_open(struct net_device *dev)
  777. {
  778. netif_wake_queue(dev);
  779. return 0;
  780. }
  781. static int cfhsi_close(struct net_device *dev)
  782. {
  783. netif_stop_queue(dev);
  784. return 0;
  785. }
  786. static const struct net_device_ops cfhsi_ops = {
  787. .ndo_open = cfhsi_open,
  788. .ndo_stop = cfhsi_close,
  789. .ndo_start_xmit = cfhsi_xmit
  790. };
  791. static void cfhsi_setup(struct net_device *dev)
  792. {
  793. struct cfhsi *cfhsi = netdev_priv(dev);
  794. dev->features = 0;
  795. dev->netdev_ops = &cfhsi_ops;
  796. dev->type = ARPHRD_CAIF;
  797. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  798. dev->mtu = CFHSI_MAX_CAIF_FRAME_SZ;
  799. dev->tx_queue_len = 0;
  800. dev->destructor = free_netdev;
  801. skb_queue_head_init(&cfhsi->qhead);
  802. cfhsi->cfdev.link_select = CAIF_LINK_HIGH_BANDW;
  803. cfhsi->cfdev.use_frag = false;
  804. cfhsi->cfdev.use_stx = false;
  805. cfhsi->cfdev.use_fcs = false;
  806. cfhsi->ndev = dev;
  807. }
  808. int cfhsi_probe(struct platform_device *pdev)
  809. {
  810. struct cfhsi *cfhsi = NULL;
  811. struct net_device *ndev;
  812. struct cfhsi_dev *dev;
  813. int res;
  814. ndev = alloc_netdev(sizeof(struct cfhsi), "cfhsi%d", cfhsi_setup);
  815. if (!ndev)
  816. return -ENODEV;
  817. cfhsi = netdev_priv(ndev);
  818. cfhsi->ndev = ndev;
  819. cfhsi->pdev = pdev;
  820. /* Initialize state vaiables. */
  821. cfhsi->tx_state = CFHSI_TX_STATE_IDLE;
  822. cfhsi->rx_state.state = CFHSI_RX_STATE_DESC;
  823. /* Set flow info */
  824. cfhsi->flow_off_sent = 0;
  825. cfhsi->q_low_mark = LOW_WATER_MARK;
  826. cfhsi->q_high_mark = HIGH_WATER_MARK;
  827. /* Assign the HSI device. */
  828. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  829. cfhsi->dev = dev;
  830. /* Assign the driver to this HSI device. */
  831. dev->drv = &cfhsi->drv;
  832. /*
  833. * Allocate a TX buffer with the size of a HSI packet descriptors
  834. * and the necessary room for CAIF payload frames.
  835. */
  836. cfhsi->tx_buf = kzalloc(CFHSI_BUF_SZ_TX, GFP_KERNEL);
  837. if (!cfhsi->tx_buf) {
  838. res = -ENODEV;
  839. goto err_alloc_tx;
  840. }
  841. /*
  842. * Allocate a RX buffer with the size of two HSI packet descriptors and
  843. * the necessary room for CAIF payload frames.
  844. */
  845. cfhsi->rx_buf = kzalloc(CFHSI_BUF_SZ_RX, GFP_KERNEL);
  846. if (!cfhsi->rx_buf) {
  847. res = -ENODEV;
  848. goto err_alloc_rx;
  849. }
  850. /* Pre-calculate inactivity timeout. */
  851. if (inactivity_timeout != -1) {
  852. cfhsi->inactivity_timeout =
  853. inactivity_timeout * HZ / 1000;
  854. if (!cfhsi->inactivity_timeout)
  855. cfhsi->inactivity_timeout = 1;
  856. else if (cfhsi->inactivity_timeout > NEXT_TIMER_MAX_DELTA)
  857. cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
  858. } else {
  859. cfhsi->inactivity_timeout = NEXT_TIMER_MAX_DELTA;
  860. }
  861. /* Initialize recieve vaiables. */
  862. cfhsi->rx_ptr = cfhsi->rx_buf;
  863. cfhsi->rx_len = CFHSI_DESC_SZ;
  864. /* Initialize spin locks. */
  865. spin_lock_init(&cfhsi->lock);
  866. /* Set up the driver. */
  867. cfhsi->drv.tx_done_cb = cfhsi_tx_done_cb;
  868. cfhsi->drv.rx_done_cb = cfhsi_rx_done_cb;
  869. cfhsi->drv.wake_up_cb = cfhsi_wake_up_cb;
  870. cfhsi->drv.wake_down_cb = cfhsi_wake_down_cb;
  871. /* Initialize the work queues. */
  872. INIT_WORK(&cfhsi->wake_up_work, cfhsi_wake_up);
  873. INIT_WORK(&cfhsi->wake_down_work, cfhsi_wake_down);
  874. INIT_WORK(&cfhsi->out_of_sync_work, cfhsi_out_of_sync);
  875. /* Clear all bit fields. */
  876. clear_bit(CFHSI_WAKE_UP_ACK, &cfhsi->bits);
  877. clear_bit(CFHSI_WAKE_DOWN_ACK, &cfhsi->bits);
  878. clear_bit(CFHSI_WAKE_UP, &cfhsi->bits);
  879. clear_bit(CFHSI_AWAKE, &cfhsi->bits);
  880. /* Create work thread. */
  881. cfhsi->wq = create_singlethread_workqueue(pdev->name);
  882. if (!cfhsi->wq) {
  883. dev_err(&ndev->dev, "%s: Failed to create work queue.\n",
  884. __func__);
  885. res = -ENODEV;
  886. goto err_create_wq;
  887. }
  888. /* Initialize wait queues. */
  889. init_waitqueue_head(&cfhsi->wake_up_wait);
  890. init_waitqueue_head(&cfhsi->wake_down_wait);
  891. init_waitqueue_head(&cfhsi->flush_fifo_wait);
  892. /* Setup the inactivity timer. */
  893. init_timer(&cfhsi->timer);
  894. cfhsi->timer.data = (unsigned long)cfhsi;
  895. cfhsi->timer.function = cfhsi_inactivity_tout;
  896. /* Setup the slowpath RX timer. */
  897. init_timer(&cfhsi->rx_slowpath_timer);
  898. cfhsi->rx_slowpath_timer.data = (unsigned long)cfhsi;
  899. cfhsi->rx_slowpath_timer.function = cfhsi_rx_slowpath;
  900. /* Add CAIF HSI device to list. */
  901. spin_lock(&cfhsi_list_lock);
  902. list_add_tail(&cfhsi->list, &cfhsi_list);
  903. spin_unlock(&cfhsi_list_lock);
  904. /* Activate HSI interface. */
  905. res = cfhsi->dev->cfhsi_up(cfhsi->dev);
  906. if (res) {
  907. dev_err(&cfhsi->ndev->dev,
  908. "%s: can't activate HSI interface: %d.\n",
  909. __func__, res);
  910. goto err_activate;
  911. }
  912. /* Flush FIFO */
  913. res = cfhsi_flush_fifo(cfhsi);
  914. if (res) {
  915. dev_err(&ndev->dev, "%s: Can't flush FIFO: %d.\n",
  916. __func__, res);
  917. goto err_net_reg;
  918. }
  919. /* Register network device. */
  920. res = register_netdev(ndev);
  921. if (res) {
  922. dev_err(&ndev->dev, "%s: Registration error: %d.\n",
  923. __func__, res);
  924. goto err_net_reg;
  925. }
  926. netif_stop_queue(ndev);
  927. return res;
  928. err_net_reg:
  929. cfhsi->dev->cfhsi_down(cfhsi->dev);
  930. err_activate:
  931. destroy_workqueue(cfhsi->wq);
  932. err_create_wq:
  933. kfree(cfhsi->rx_buf);
  934. err_alloc_rx:
  935. kfree(cfhsi->tx_buf);
  936. err_alloc_tx:
  937. free_netdev(ndev);
  938. return res;
  939. }
  940. static void cfhsi_shutdown(struct cfhsi *cfhsi)
  941. {
  942. u8 *tx_buf, *rx_buf;
  943. /* Stop TXing */
  944. netif_tx_stop_all_queues(cfhsi->ndev);
  945. /* going to shutdown driver */
  946. set_bit(CFHSI_SHUTDOWN, &cfhsi->bits);
  947. /* Flush workqueue */
  948. flush_workqueue(cfhsi->wq);
  949. /* Delete timers if pending */
  950. del_timer_sync(&cfhsi->timer);
  951. del_timer_sync(&cfhsi->rx_slowpath_timer);
  952. /* Cancel pending RX request (if any) */
  953. cfhsi->dev->cfhsi_rx_cancel(cfhsi->dev);
  954. /* Destroy workqueue */
  955. destroy_workqueue(cfhsi->wq);
  956. /* Store bufferes: will be freed later. */
  957. tx_buf = cfhsi->tx_buf;
  958. rx_buf = cfhsi->rx_buf;
  959. /* Flush transmit queues. */
  960. cfhsi_abort_tx(cfhsi);
  961. /* Deactivate interface */
  962. cfhsi->dev->cfhsi_down(cfhsi->dev);
  963. /* Finally unregister the network device. */
  964. unregister_netdev(cfhsi->ndev);
  965. /* Free buffers. */
  966. kfree(tx_buf);
  967. kfree(rx_buf);
  968. }
  969. int cfhsi_remove(struct platform_device *pdev)
  970. {
  971. struct list_head *list_node;
  972. struct list_head *n;
  973. struct cfhsi *cfhsi = NULL;
  974. struct cfhsi_dev *dev;
  975. dev = (struct cfhsi_dev *)pdev->dev.platform_data;
  976. spin_lock(&cfhsi_list_lock);
  977. list_for_each_safe(list_node, n, &cfhsi_list) {
  978. cfhsi = list_entry(list_node, struct cfhsi, list);
  979. /* Find the corresponding device. */
  980. if (cfhsi->dev == dev) {
  981. /* Remove from list. */
  982. list_del(list_node);
  983. spin_unlock(&cfhsi_list_lock);
  984. /* Shutdown driver. */
  985. cfhsi_shutdown(cfhsi);
  986. return 0;
  987. }
  988. }
  989. spin_unlock(&cfhsi_list_lock);
  990. return -ENODEV;
  991. }
  992. struct platform_driver cfhsi_plat_drv = {
  993. .probe = cfhsi_probe,
  994. .remove = cfhsi_remove,
  995. .driver = {
  996. .name = "cfhsi",
  997. .owner = THIS_MODULE,
  998. },
  999. };
  1000. static void __exit cfhsi_exit_module(void)
  1001. {
  1002. struct list_head *list_node;
  1003. struct list_head *n;
  1004. struct cfhsi *cfhsi = NULL;
  1005. spin_lock(&cfhsi_list_lock);
  1006. list_for_each_safe(list_node, n, &cfhsi_list) {
  1007. cfhsi = list_entry(list_node, struct cfhsi, list);
  1008. /* Remove from list. */
  1009. list_del(list_node);
  1010. spin_unlock(&cfhsi_list_lock);
  1011. /* Shutdown driver. */
  1012. cfhsi_shutdown(cfhsi);
  1013. spin_lock(&cfhsi_list_lock);
  1014. }
  1015. spin_unlock(&cfhsi_list_lock);
  1016. /* Unregister platform driver. */
  1017. platform_driver_unregister(&cfhsi_plat_drv);
  1018. }
  1019. static int __init cfhsi_init_module(void)
  1020. {
  1021. int result;
  1022. /* Initialize spin lock. */
  1023. spin_lock_init(&cfhsi_list_lock);
  1024. /* Register platform driver. */
  1025. result = platform_driver_register(&cfhsi_plat_drv);
  1026. if (result) {
  1027. printk(KERN_ERR "Could not register platform HSI driver: %d.\n",
  1028. result);
  1029. goto err_dev_register;
  1030. }
  1031. return result;
  1032. err_dev_register:
  1033. return result;
  1034. }
  1035. module_init(cfhsi_init_module);
  1036. module_exit(cfhsi_exit_module);