softing_main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Copyright (C) 2008-2010
  3. *
  4. * - Kurt Van Dijck, EIA Electronics
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/io.h>
  21. #include "softing.h"
  22. #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1)
  23. /*
  24. * test is a specific CAN netdev
  25. * is online (ie. up 'n running, not sleeping, not busoff
  26. */
  27. static inline int canif_is_active(struct net_device *netdev)
  28. {
  29. struct can_priv *can = netdev_priv(netdev);
  30. if (!netif_running(netdev))
  31. return 0;
  32. return (can->state <= CAN_STATE_ERROR_PASSIVE);
  33. }
  34. /* reset DPRAM */
  35. static inline void softing_set_reset_dpram(struct softing *card)
  36. {
  37. if (card->pdat->generation >= 2) {
  38. spin_lock_bh(&card->spin);
  39. iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1,
  40. &card->dpram[DPRAM_V2_RESET]);
  41. spin_unlock_bh(&card->spin);
  42. }
  43. }
  44. static inline void softing_clr_reset_dpram(struct softing *card)
  45. {
  46. if (card->pdat->generation >= 2) {
  47. spin_lock_bh(&card->spin);
  48. iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1,
  49. &card->dpram[DPRAM_V2_RESET]);
  50. spin_unlock_bh(&card->spin);
  51. }
  52. }
  53. /* trigger the tx queue-ing */
  54. static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb,
  55. struct net_device *dev)
  56. {
  57. struct softing_priv *priv = netdev_priv(dev);
  58. struct softing *card = priv->card;
  59. int ret;
  60. uint8_t *ptr;
  61. uint8_t fifo_wr, fifo_rd;
  62. struct can_frame *cf = (struct can_frame *)skb->data;
  63. uint8_t buf[DPRAM_TX_SIZE];
  64. if (can_dropped_invalid_skb(dev, skb))
  65. return NETDEV_TX_OK;
  66. spin_lock(&card->spin);
  67. ret = NETDEV_TX_BUSY;
  68. if (!card->fw.up ||
  69. (card->tx.pending >= TXMAX) ||
  70. (priv->tx.pending >= TX_ECHO_SKB_MAX))
  71. goto xmit_done;
  72. fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]);
  73. fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]);
  74. if (fifo_wr == fifo_rd)
  75. /* fifo full */
  76. goto xmit_done;
  77. memset(buf, 0, sizeof(buf));
  78. ptr = buf;
  79. *ptr = CMD_TX;
  80. if (cf->can_id & CAN_RTR_FLAG)
  81. *ptr |= CMD_RTR;
  82. if (cf->can_id & CAN_EFF_FLAG)
  83. *ptr |= CMD_XTD;
  84. if (priv->index)
  85. *ptr |= CMD_BUS2;
  86. ++ptr;
  87. *ptr++ = cf->can_dlc;
  88. *ptr++ = (cf->can_id >> 0);
  89. *ptr++ = (cf->can_id >> 8);
  90. if (cf->can_id & CAN_EFF_FLAG) {
  91. *ptr++ = (cf->can_id >> 16);
  92. *ptr++ = (cf->can_id >> 24);
  93. } else {
  94. /* increment 1, not 2 as you might think */
  95. ptr += 1;
  96. }
  97. if (!(cf->can_id & CAN_RTR_FLAG))
  98. memcpy(ptr, &cf->data[0], cf->can_dlc);
  99. memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr],
  100. buf, DPRAM_TX_SIZE);
  101. if (++fifo_wr >= DPRAM_TX_CNT)
  102. fifo_wr = 0;
  103. iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]);
  104. card->tx.last_bus = priv->index;
  105. ++card->tx.pending;
  106. ++priv->tx.pending;
  107. can_put_echo_skb(skb, dev, priv->tx.echo_put);
  108. ++priv->tx.echo_put;
  109. if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
  110. priv->tx.echo_put = 0;
  111. /* can_put_echo_skb() saves the skb, safe to return TX_OK */
  112. ret = NETDEV_TX_OK;
  113. xmit_done:
  114. spin_unlock(&card->spin);
  115. if (card->tx.pending >= TXMAX) {
  116. int j;
  117. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  118. if (card->net[j])
  119. netif_stop_queue(card->net[j]);
  120. }
  121. }
  122. if (ret != NETDEV_TX_OK)
  123. netif_stop_queue(dev);
  124. return ret;
  125. }
  126. /*
  127. * shortcut for skb delivery
  128. */
  129. int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg,
  130. ktime_t ktime)
  131. {
  132. struct sk_buff *skb;
  133. struct can_frame *cf;
  134. skb = alloc_can_skb(netdev, &cf);
  135. if (!skb)
  136. return -ENOMEM;
  137. memcpy(cf, msg, sizeof(*msg));
  138. skb->tstamp = ktime;
  139. return netif_rx(skb);
  140. }
  141. /*
  142. * softing_handle_1
  143. * pop 1 entry from the DPRAM queue, and process
  144. */
  145. static int softing_handle_1(struct softing *card)
  146. {
  147. struct net_device *netdev;
  148. struct softing_priv *priv;
  149. ktime_t ktime;
  150. struct can_frame msg;
  151. int cnt = 0, lost_msg;
  152. uint8_t fifo_rd, fifo_wr, cmd;
  153. uint8_t *ptr;
  154. uint32_t tmp_u32;
  155. uint8_t buf[DPRAM_RX_SIZE];
  156. memset(&msg, 0, sizeof(msg));
  157. /* test for lost msgs */
  158. lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]);
  159. if (lost_msg) {
  160. int j;
  161. /* reset condition */
  162. iowrite8(0, &card->dpram[DPRAM_RX_LOST]);
  163. /* prepare msg */
  164. msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
  165. msg.can_dlc = CAN_ERR_DLC;
  166. msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  167. /*
  168. * service to all busses, we don't know which it was applicable
  169. * but only service busses that are online
  170. */
  171. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  172. netdev = card->net[j];
  173. if (!netdev)
  174. continue;
  175. if (!canif_is_active(netdev))
  176. /* a dead bus has no overflows */
  177. continue;
  178. ++netdev->stats.rx_over_errors;
  179. softing_netdev_rx(netdev, &msg, ktime_set(0, 0));
  180. }
  181. /* prepare for other use */
  182. memset(&msg, 0, sizeof(msg));
  183. ++cnt;
  184. }
  185. fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]);
  186. fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]);
  187. if (++fifo_rd >= DPRAM_RX_CNT)
  188. fifo_rd = 0;
  189. if (fifo_wr == fifo_rd)
  190. return cnt;
  191. memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd],
  192. DPRAM_RX_SIZE);
  193. mb();
  194. /* trigger dual port RAM */
  195. iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]);
  196. ptr = buf;
  197. cmd = *ptr++;
  198. if (cmd == 0xff)
  199. /* not quite useful, probably the card has got out */
  200. return 0;
  201. netdev = card->net[0];
  202. if (cmd & CMD_BUS2)
  203. netdev = card->net[1];
  204. priv = netdev_priv(netdev);
  205. if (cmd & CMD_ERR) {
  206. uint8_t can_state, state;
  207. state = *ptr++;
  208. msg.can_id = CAN_ERR_FLAG;
  209. msg.can_dlc = CAN_ERR_DLC;
  210. if (state & SF_MASK_BUSOFF) {
  211. can_state = CAN_STATE_BUS_OFF;
  212. msg.can_id |= CAN_ERR_BUSOFF;
  213. state = STATE_BUSOFF;
  214. } else if (state & SF_MASK_EPASSIVE) {
  215. can_state = CAN_STATE_ERROR_PASSIVE;
  216. msg.can_id |= CAN_ERR_CRTL;
  217. msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
  218. state = STATE_EPASSIVE;
  219. } else {
  220. can_state = CAN_STATE_ERROR_ACTIVE;
  221. msg.can_id |= CAN_ERR_CRTL;
  222. state = STATE_EACTIVE;
  223. }
  224. /* update DPRAM */
  225. iowrite8(state, &card->dpram[priv->index ?
  226. DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]);
  227. /* timestamp */
  228. tmp_u32 = le32_to_cpup((void *)ptr);
  229. ptr += 4;
  230. ktime = softing_raw2ktime(card, tmp_u32);
  231. ++netdev->stats.rx_errors;
  232. /* update internal status */
  233. if (can_state != priv->can.state) {
  234. priv->can.state = can_state;
  235. if (can_state == CAN_STATE_ERROR_PASSIVE)
  236. ++priv->can.can_stats.error_passive;
  237. else if (can_state == CAN_STATE_BUS_OFF) {
  238. /* this calls can_close_cleanup() */
  239. ++priv->can.can_stats.bus_off;
  240. can_bus_off(netdev);
  241. netif_stop_queue(netdev);
  242. }
  243. /* trigger socketcan */
  244. softing_netdev_rx(netdev, &msg, ktime);
  245. }
  246. } else {
  247. if (cmd & CMD_RTR)
  248. msg.can_id |= CAN_RTR_FLAG;
  249. msg.can_dlc = get_can_dlc(*ptr++);
  250. if (cmd & CMD_XTD) {
  251. msg.can_id |= CAN_EFF_FLAG;
  252. msg.can_id |= le32_to_cpup((void *)ptr);
  253. ptr += 4;
  254. } else {
  255. msg.can_id |= le16_to_cpup((void *)ptr);
  256. ptr += 2;
  257. }
  258. /* timestamp */
  259. tmp_u32 = le32_to_cpup((void *)ptr);
  260. ptr += 4;
  261. ktime = softing_raw2ktime(card, tmp_u32);
  262. if (!(msg.can_id & CAN_RTR_FLAG))
  263. memcpy(&msg.data[0], ptr, 8);
  264. ptr += 8;
  265. /* update socket */
  266. if (cmd & CMD_ACK) {
  267. /* acknowledge, was tx msg */
  268. struct sk_buff *skb;
  269. skb = priv->can.echo_skb[priv->tx.echo_get];
  270. if (skb)
  271. skb->tstamp = ktime;
  272. can_get_echo_skb(netdev, priv->tx.echo_get);
  273. ++priv->tx.echo_get;
  274. if (priv->tx.echo_get >= TX_ECHO_SKB_MAX)
  275. priv->tx.echo_get = 0;
  276. if (priv->tx.pending)
  277. --priv->tx.pending;
  278. if (card->tx.pending)
  279. --card->tx.pending;
  280. ++netdev->stats.tx_packets;
  281. if (!(msg.can_id & CAN_RTR_FLAG))
  282. netdev->stats.tx_bytes += msg.can_dlc;
  283. } else {
  284. int ret;
  285. ret = softing_netdev_rx(netdev, &msg, ktime);
  286. if (ret == NET_RX_SUCCESS) {
  287. ++netdev->stats.rx_packets;
  288. if (!(msg.can_id & CAN_RTR_FLAG))
  289. netdev->stats.rx_bytes += msg.can_dlc;
  290. } else {
  291. ++netdev->stats.rx_dropped;
  292. }
  293. }
  294. }
  295. ++cnt;
  296. return cnt;
  297. }
  298. /*
  299. * real interrupt handler
  300. */
  301. static irqreturn_t softing_irq_thread(int irq, void *dev_id)
  302. {
  303. struct softing *card = (struct softing *)dev_id;
  304. struct net_device *netdev;
  305. struct softing_priv *priv;
  306. int j, offset, work_done;
  307. work_done = 0;
  308. spin_lock_bh(&card->spin);
  309. while (softing_handle_1(card) > 0) {
  310. ++card->irq.svc_count;
  311. ++work_done;
  312. }
  313. spin_unlock_bh(&card->spin);
  314. /* resume tx queue's */
  315. offset = card->tx.last_bus;
  316. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  317. if (card->tx.pending >= TXMAX)
  318. break;
  319. netdev = card->net[(j + offset + 1) % card->pdat->nbus];
  320. if (!netdev)
  321. continue;
  322. priv = netdev_priv(netdev);
  323. if (!canif_is_active(netdev))
  324. /* it makes no sense to wake dead busses */
  325. continue;
  326. if (priv->tx.pending >= TX_ECHO_SKB_MAX)
  327. continue;
  328. ++work_done;
  329. netif_wake_queue(netdev);
  330. }
  331. return work_done ? IRQ_HANDLED : IRQ_NONE;
  332. }
  333. /*
  334. * interrupt routines:
  335. * schedule the 'real interrupt handler'
  336. */
  337. static irqreturn_t softing_irq_v2(int irq, void *dev_id)
  338. {
  339. struct softing *card = (struct softing *)dev_id;
  340. uint8_t ir;
  341. ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]);
  342. iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]);
  343. return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE;
  344. }
  345. static irqreturn_t softing_irq_v1(int irq, void *dev_id)
  346. {
  347. struct softing *card = (struct softing *)dev_id;
  348. uint8_t ir;
  349. ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]);
  350. iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]);
  351. return ir ? IRQ_WAKE_THREAD : IRQ_NONE;
  352. }
  353. /*
  354. * netdev/candev inter-operability
  355. */
  356. static int softing_netdev_open(struct net_device *ndev)
  357. {
  358. int ret;
  359. /* check or determine and set bittime */
  360. ret = open_candev(ndev);
  361. if (!ret)
  362. ret = softing_startstop(ndev, 1);
  363. return ret;
  364. }
  365. static int softing_netdev_stop(struct net_device *ndev)
  366. {
  367. int ret;
  368. netif_stop_queue(ndev);
  369. /* softing cycle does close_candev() */
  370. ret = softing_startstop(ndev, 0);
  371. return ret;
  372. }
  373. static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode)
  374. {
  375. int ret;
  376. switch (mode) {
  377. case CAN_MODE_START:
  378. /* softing_startstop does close_candev() */
  379. ret = softing_startstop(ndev, 1);
  380. return ret;
  381. case CAN_MODE_STOP:
  382. case CAN_MODE_SLEEP:
  383. return -EOPNOTSUPP;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Softing device management helpers
  389. */
  390. int softing_enable_irq(struct softing *card, int enable)
  391. {
  392. int ret;
  393. if (!card->irq.nr) {
  394. return 0;
  395. } else if (card->irq.requested && !enable) {
  396. free_irq(card->irq.nr, card);
  397. card->irq.requested = 0;
  398. } else if (!card->irq.requested && enable) {
  399. ret = request_threaded_irq(card->irq.nr,
  400. (card->pdat->generation >= 2) ?
  401. softing_irq_v2 : softing_irq_v1,
  402. softing_irq_thread, IRQF_SHARED,
  403. dev_name(&card->pdev->dev), card);
  404. if (ret) {
  405. dev_alert(&card->pdev->dev,
  406. "request_threaded_irq(%u) failed\n",
  407. card->irq.nr);
  408. return ret;
  409. }
  410. card->irq.requested = 1;
  411. }
  412. return 0;
  413. }
  414. static void softing_card_shutdown(struct softing *card)
  415. {
  416. int fw_up = 0;
  417. if (mutex_lock_interruptible(&card->fw.lock))
  418. /* return -ERESTARTSYS */;
  419. fw_up = card->fw.up;
  420. card->fw.up = 0;
  421. if (card->irq.requested && card->irq.nr) {
  422. free_irq(card->irq.nr, card);
  423. card->irq.requested = 0;
  424. }
  425. if (fw_up) {
  426. if (card->pdat->enable_irq)
  427. card->pdat->enable_irq(card->pdev, 0);
  428. softing_set_reset_dpram(card);
  429. if (card->pdat->reset)
  430. card->pdat->reset(card->pdev, 1);
  431. }
  432. mutex_unlock(&card->fw.lock);
  433. }
  434. static int softing_card_boot(struct softing *card)
  435. {
  436. int ret, j;
  437. static const uint8_t stream[] = {
  438. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
  439. unsigned char back[sizeof(stream)];
  440. if (mutex_lock_interruptible(&card->fw.lock))
  441. return -ERESTARTSYS;
  442. if (card->fw.up) {
  443. mutex_unlock(&card->fw.lock);
  444. return 0;
  445. }
  446. /* reset board */
  447. if (card->pdat->enable_irq)
  448. card->pdat->enable_irq(card->pdev, 1);
  449. /* boot card */
  450. softing_set_reset_dpram(card);
  451. if (card->pdat->reset)
  452. card->pdat->reset(card->pdev, 1);
  453. for (j = 0; (j + sizeof(stream)) < card->dpram_size;
  454. j += sizeof(stream)) {
  455. memcpy_toio(&card->dpram[j], stream, sizeof(stream));
  456. /* flush IO cache */
  457. mb();
  458. memcpy_fromio(back, &card->dpram[j], sizeof(stream));
  459. if (!memcmp(back, stream, sizeof(stream)))
  460. continue;
  461. /* memory is not equal */
  462. dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j);
  463. ret = -EIO;
  464. goto failed;
  465. }
  466. wmb();
  467. /* load boot firmware */
  468. ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram,
  469. card->dpram_size,
  470. card->pdat->boot.offs - card->pdat->boot.addr);
  471. if (ret < 0)
  472. goto failed;
  473. /* load loader firmware */
  474. ret = softing_load_fw(card->pdat->load.fw, card, card->dpram,
  475. card->dpram_size,
  476. card->pdat->load.offs - card->pdat->load.addr);
  477. if (ret < 0)
  478. goto failed;
  479. if (card->pdat->reset)
  480. card->pdat->reset(card->pdev, 0);
  481. softing_clr_reset_dpram(card);
  482. ret = softing_bootloader_command(card, 0, "card boot");
  483. if (ret < 0)
  484. goto failed;
  485. ret = softing_load_app_fw(card->pdat->app.fw, card);
  486. if (ret < 0)
  487. goto failed;
  488. ret = softing_chip_poweron(card);
  489. if (ret < 0)
  490. goto failed;
  491. card->fw.up = 1;
  492. mutex_unlock(&card->fw.lock);
  493. return 0;
  494. failed:
  495. card->fw.up = 0;
  496. if (card->pdat->enable_irq)
  497. card->pdat->enable_irq(card->pdev, 0);
  498. softing_set_reset_dpram(card);
  499. if (card->pdat->reset)
  500. card->pdat->reset(card->pdev, 1);
  501. mutex_unlock(&card->fw.lock);
  502. return ret;
  503. }
  504. /*
  505. * netdev sysfs
  506. */
  507. static ssize_t show_chip(struct device *dev, struct device_attribute *attr,
  508. char *buf)
  509. {
  510. struct net_device *ndev = to_net_dev(dev);
  511. struct softing_priv *priv = netdev2softing(ndev);
  512. return sprintf(buf, "%i\n", priv->chip);
  513. }
  514. static ssize_t show_output(struct device *dev, struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct net_device *ndev = to_net_dev(dev);
  518. struct softing_priv *priv = netdev2softing(ndev);
  519. return sprintf(buf, "0x%02x\n", priv->output);
  520. }
  521. static ssize_t store_output(struct device *dev, struct device_attribute *attr,
  522. const char *buf, size_t count)
  523. {
  524. struct net_device *ndev = to_net_dev(dev);
  525. struct softing_priv *priv = netdev2softing(ndev);
  526. struct softing *card = priv->card;
  527. unsigned long val;
  528. int ret;
  529. ret = kstrtoul(buf, 0, &val);
  530. if (ret < 0)
  531. return ret;
  532. val &= 0xFF;
  533. ret = mutex_lock_interruptible(&card->fw.lock);
  534. if (ret)
  535. return -ERESTARTSYS;
  536. if (netif_running(ndev)) {
  537. mutex_unlock(&card->fw.lock);
  538. return -EBUSY;
  539. }
  540. priv->output = val;
  541. mutex_unlock(&card->fw.lock);
  542. return count;
  543. }
  544. static const DEVICE_ATTR(chip, S_IRUGO, show_chip, NULL);
  545. static const DEVICE_ATTR(output, S_IRUGO | S_IWUSR, show_output, store_output);
  546. static const struct attribute *const netdev_sysfs_attrs[] = {
  547. &dev_attr_chip.attr,
  548. &dev_attr_output.attr,
  549. NULL,
  550. };
  551. static const struct attribute_group netdev_sysfs_group = {
  552. .name = NULL,
  553. .attrs = (struct attribute **)netdev_sysfs_attrs,
  554. };
  555. static const struct net_device_ops softing_netdev_ops = {
  556. .ndo_open = softing_netdev_open,
  557. .ndo_stop = softing_netdev_stop,
  558. .ndo_start_xmit = softing_netdev_start_xmit,
  559. .ndo_change_mtu = can_change_mtu,
  560. };
  561. static const struct can_bittiming_const softing_btr_const = {
  562. .name = "softing",
  563. .tseg1_min = 1,
  564. .tseg1_max = 16,
  565. .tseg2_min = 1,
  566. .tseg2_max = 8,
  567. .sjw_max = 4, /* overruled */
  568. .brp_min = 1,
  569. .brp_max = 32, /* overruled */
  570. .brp_inc = 1,
  571. };
  572. static struct net_device *softing_netdev_create(struct softing *card,
  573. uint16_t chip_id)
  574. {
  575. struct net_device *netdev;
  576. struct softing_priv *priv;
  577. netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
  578. if (!netdev) {
  579. dev_alert(&card->pdev->dev, "alloc_candev failed\n");
  580. return NULL;
  581. }
  582. priv = netdev_priv(netdev);
  583. priv->netdev = netdev;
  584. priv->card = card;
  585. memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
  586. priv->btr_const.brp_max = card->pdat->max_brp;
  587. priv->btr_const.sjw_max = card->pdat->max_sjw;
  588. priv->can.bittiming_const = &priv->btr_const;
  589. priv->can.clock.freq = 8000000;
  590. priv->chip = chip_id;
  591. priv->output = softing_default_output(netdev);
  592. SET_NETDEV_DEV(netdev, &card->pdev->dev);
  593. netdev->flags |= IFF_ECHO;
  594. netdev->netdev_ops = &softing_netdev_ops;
  595. priv->can.do_set_mode = softing_candev_set_mode;
  596. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  597. return netdev;
  598. }
  599. static int softing_netdev_register(struct net_device *netdev)
  600. {
  601. int ret;
  602. ret = register_candev(netdev);
  603. if (ret) {
  604. dev_alert(&netdev->dev, "register failed\n");
  605. return ret;
  606. }
  607. if (sysfs_create_group(&netdev->dev.kobj, &netdev_sysfs_group) < 0)
  608. netdev_alert(netdev, "sysfs group failed\n");
  609. return 0;
  610. }
  611. static void softing_netdev_cleanup(struct net_device *netdev)
  612. {
  613. sysfs_remove_group(&netdev->dev.kobj, &netdev_sysfs_group);
  614. unregister_candev(netdev);
  615. free_candev(netdev);
  616. }
  617. /*
  618. * sysfs for Platform device
  619. */
  620. #define DEV_ATTR_RO(name, member) \
  621. static ssize_t show_##name(struct device *dev, \
  622. struct device_attribute *attr, char *buf) \
  623. { \
  624. struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
  625. return sprintf(buf, "%u\n", card->member); \
  626. } \
  627. static DEVICE_ATTR(name, 0444, show_##name, NULL)
  628. #define DEV_ATTR_RO_STR(name, member) \
  629. static ssize_t show_##name(struct device *dev, \
  630. struct device_attribute *attr, char *buf) \
  631. { \
  632. struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
  633. return sprintf(buf, "%s\n", card->member); \
  634. } \
  635. static DEVICE_ATTR(name, 0444, show_##name, NULL)
  636. DEV_ATTR_RO(serial, id.serial);
  637. DEV_ATTR_RO_STR(firmware, pdat->app.fw);
  638. DEV_ATTR_RO(firmware_version, id.fw_version);
  639. DEV_ATTR_RO_STR(hardware, pdat->name);
  640. DEV_ATTR_RO(hardware_version, id.hw_version);
  641. DEV_ATTR_RO(license, id.license);
  642. static struct attribute *softing_pdev_attrs[] = {
  643. &dev_attr_serial.attr,
  644. &dev_attr_firmware.attr,
  645. &dev_attr_firmware_version.attr,
  646. &dev_attr_hardware.attr,
  647. &dev_attr_hardware_version.attr,
  648. &dev_attr_license.attr,
  649. NULL,
  650. };
  651. static const struct attribute_group softing_pdev_group = {
  652. .name = NULL,
  653. .attrs = softing_pdev_attrs,
  654. };
  655. /*
  656. * platform driver
  657. */
  658. static int softing_pdev_remove(struct platform_device *pdev)
  659. {
  660. struct softing *card = platform_get_drvdata(pdev);
  661. int j;
  662. /* first, disable card*/
  663. softing_card_shutdown(card);
  664. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  665. if (!card->net[j])
  666. continue;
  667. softing_netdev_cleanup(card->net[j]);
  668. card->net[j] = NULL;
  669. }
  670. sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
  671. iounmap(card->dpram);
  672. kfree(card);
  673. return 0;
  674. }
  675. static int softing_pdev_probe(struct platform_device *pdev)
  676. {
  677. const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev);
  678. struct softing *card;
  679. struct net_device *netdev;
  680. struct softing_priv *priv;
  681. struct resource *pres;
  682. int ret;
  683. int j;
  684. if (!pdat) {
  685. dev_warn(&pdev->dev, "no platform data\n");
  686. return -EINVAL;
  687. }
  688. if (pdat->nbus > ARRAY_SIZE(card->net)) {
  689. dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus);
  690. return -EINVAL;
  691. }
  692. card = kzalloc(sizeof(*card), GFP_KERNEL);
  693. if (!card)
  694. return -ENOMEM;
  695. card->pdat = pdat;
  696. card->pdev = pdev;
  697. platform_set_drvdata(pdev, card);
  698. mutex_init(&card->fw.lock);
  699. spin_lock_init(&card->spin);
  700. ret = -EINVAL;
  701. pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  702. if (!pres)
  703. goto platform_resource_failed;
  704. card->dpram_phys = pres->start;
  705. card->dpram_size = resource_size(pres);
  706. card->dpram = ioremap_nocache(card->dpram_phys, card->dpram_size);
  707. if (!card->dpram) {
  708. dev_alert(&card->pdev->dev, "dpram ioremap failed\n");
  709. goto ioremap_failed;
  710. }
  711. pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  712. if (pres)
  713. card->irq.nr = pres->start;
  714. /* reset card */
  715. ret = softing_card_boot(card);
  716. if (ret < 0) {
  717. dev_alert(&pdev->dev, "failed to boot\n");
  718. goto boot_failed;
  719. }
  720. /* only now, the chip's are known */
  721. card->id.freq = card->pdat->freq;
  722. ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group);
  723. if (ret < 0) {
  724. dev_alert(&card->pdev->dev, "sysfs failed\n");
  725. goto sysfs_failed;
  726. }
  727. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  728. card->net[j] = netdev =
  729. softing_netdev_create(card, card->id.chip[j]);
  730. if (!netdev) {
  731. dev_alert(&pdev->dev, "failed to make can[%i]", j);
  732. ret = -ENOMEM;
  733. goto netdev_failed;
  734. }
  735. netdev->dev_id = j;
  736. priv = netdev_priv(card->net[j]);
  737. priv->index = j;
  738. ret = softing_netdev_register(netdev);
  739. if (ret) {
  740. free_candev(netdev);
  741. card->net[j] = NULL;
  742. dev_alert(&card->pdev->dev,
  743. "failed to register can[%i]\n", j);
  744. goto netdev_failed;
  745. }
  746. }
  747. dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name);
  748. return 0;
  749. netdev_failed:
  750. for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
  751. if (!card->net[j])
  752. continue;
  753. softing_netdev_cleanup(card->net[j]);
  754. }
  755. sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
  756. sysfs_failed:
  757. softing_card_shutdown(card);
  758. boot_failed:
  759. iounmap(card->dpram);
  760. ioremap_failed:
  761. platform_resource_failed:
  762. kfree(card);
  763. return ret;
  764. }
  765. static struct platform_driver softing_driver = {
  766. .driver = {
  767. .name = "softing",
  768. },
  769. .probe = softing_pdev_probe,
  770. .remove = softing_pdev_remove,
  771. };
  772. module_platform_driver(softing_driver);
  773. MODULE_ALIAS("platform:softing");
  774. MODULE_DESCRIPTION("Softing DPRAM CAN driver");
  775. MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
  776. MODULE_LICENSE("GPL v2");