caif_hsi.c 29 KB

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