softing_main.c 21 KB

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