sja1000.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * sja1000.c - Philips SJA1000 network device driver
  3. *
  4. * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
  5. * 38106 Braunschweig, GERMANY
  6. *
  7. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of Volkswagen nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * Alternatively, provided that this notice is retained in full, this
  23. * software may be distributed under the terms of the GNU General
  24. * Public License ("GPL") version 2, in which case the provisions of the
  25. * GPL apply INSTEAD OF those given above.
  26. *
  27. * The provided data structures and external interfaces from this code
  28. * are not restricted to be used by modules with a GPL compatible license.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41. * DAMAGE.
  42. *
  43. */
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <linux/kernel.h>
  47. #include <linux/sched.h>
  48. #include <linux/types.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/ptrace.h>
  52. #include <linux/string.h>
  53. #include <linux/errno.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/if_arp.h>
  56. #include <linux/if_ether.h>
  57. #include <linux/skbuff.h>
  58. #include <linux/delay.h>
  59. #include <linux/can/dev.h>
  60. #include <linux/can/error.h>
  61. #include <linux/can/led.h>
  62. #include "sja1000.h"
  63. #define DRV_NAME "sja1000"
  64. MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  65. MODULE_LICENSE("Dual BSD/GPL");
  66. MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
  67. static const struct can_bittiming_const sja1000_bittiming_const = {
  68. .name = DRV_NAME,
  69. .tseg1_min = 1,
  70. .tseg1_max = 16,
  71. .tseg2_min = 1,
  72. .tseg2_max = 8,
  73. .sjw_max = 4,
  74. .brp_min = 1,
  75. .brp_max = 64,
  76. .brp_inc = 1,
  77. };
  78. static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
  79. {
  80. unsigned long flags;
  81. /*
  82. * The command register needs some locking and time to settle
  83. * the write_reg() operation - especially on SMP systems.
  84. */
  85. spin_lock_irqsave(&priv->cmdreg_lock, flags);
  86. priv->write_reg(priv, SJA1000_CMR, val);
  87. priv->read_reg(priv, SJA1000_SR);
  88. spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
  89. }
  90. static int sja1000_is_absent(struct sja1000_priv *priv)
  91. {
  92. return (priv->read_reg(priv, SJA1000_MOD) == 0xFF);
  93. }
  94. static int sja1000_probe_chip(struct net_device *dev)
  95. {
  96. struct sja1000_priv *priv = netdev_priv(dev);
  97. if (priv->reg_base && sja1000_is_absent(priv)) {
  98. netdev_err(dev, "probing failed\n");
  99. return 0;
  100. }
  101. return -1;
  102. }
  103. static void set_reset_mode(struct net_device *dev)
  104. {
  105. struct sja1000_priv *priv = netdev_priv(dev);
  106. unsigned char status = priv->read_reg(priv, SJA1000_MOD);
  107. int i;
  108. /* disable interrupts */
  109. priv->write_reg(priv, SJA1000_IER, IRQ_OFF);
  110. for (i = 0; i < 100; i++) {
  111. /* check reset bit */
  112. if (status & MOD_RM) {
  113. priv->can.state = CAN_STATE_STOPPED;
  114. return;
  115. }
  116. /* reset chip */
  117. priv->write_reg(priv, SJA1000_MOD, MOD_RM);
  118. udelay(10);
  119. status = priv->read_reg(priv, SJA1000_MOD);
  120. }
  121. netdev_err(dev, "setting SJA1000 into reset mode failed!\n");
  122. }
  123. static void set_normal_mode(struct net_device *dev)
  124. {
  125. struct sja1000_priv *priv = netdev_priv(dev);
  126. unsigned char status = priv->read_reg(priv, SJA1000_MOD);
  127. u8 mod_reg_val = 0x00;
  128. int i;
  129. for (i = 0; i < 100; i++) {
  130. /* check reset bit */
  131. if ((status & MOD_RM) == 0) {
  132. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  133. /* enable interrupts */
  134. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  135. priv->write_reg(priv, SJA1000_IER, IRQ_ALL);
  136. else
  137. priv->write_reg(priv, SJA1000_IER,
  138. IRQ_ALL & ~IRQ_BEI);
  139. return;
  140. }
  141. /* set chip to normal mode */
  142. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  143. mod_reg_val |= MOD_LOM;
  144. if (priv->can.ctrlmode & CAN_CTRLMODE_PRESUME_ACK)
  145. mod_reg_val |= MOD_STM;
  146. priv->write_reg(priv, SJA1000_MOD, mod_reg_val);
  147. udelay(10);
  148. status = priv->read_reg(priv, SJA1000_MOD);
  149. }
  150. netdev_err(dev, "setting SJA1000 into normal mode failed!\n");
  151. }
  152. /*
  153. * initialize SJA1000 chip:
  154. * - reset chip
  155. * - set output mode
  156. * - set baudrate
  157. * - enable interrupts
  158. * - start operating mode
  159. */
  160. static void chipset_init(struct net_device *dev)
  161. {
  162. struct sja1000_priv *priv = netdev_priv(dev);
  163. /* set clock divider and output control register */
  164. priv->write_reg(priv, SJA1000_CDR, priv->cdr | CDR_PELICAN);
  165. /* set acceptance filter (accept all) */
  166. priv->write_reg(priv, SJA1000_ACCC0, 0x00);
  167. priv->write_reg(priv, SJA1000_ACCC1, 0x00);
  168. priv->write_reg(priv, SJA1000_ACCC2, 0x00);
  169. priv->write_reg(priv, SJA1000_ACCC3, 0x00);
  170. priv->write_reg(priv, SJA1000_ACCM0, 0xFF);
  171. priv->write_reg(priv, SJA1000_ACCM1, 0xFF);
  172. priv->write_reg(priv, SJA1000_ACCM2, 0xFF);
  173. priv->write_reg(priv, SJA1000_ACCM3, 0xFF);
  174. priv->write_reg(priv, SJA1000_OCR, priv->ocr | OCR_MODE_NORMAL);
  175. }
  176. static void sja1000_start(struct net_device *dev)
  177. {
  178. struct sja1000_priv *priv = netdev_priv(dev);
  179. /* leave reset mode */
  180. if (priv->can.state != CAN_STATE_STOPPED)
  181. set_reset_mode(dev);
  182. /* Initialize chip if uninitialized at this stage */
  183. if (!(priv->read_reg(priv, SJA1000_CDR) & CDR_PELICAN))
  184. chipset_init(dev);
  185. /* Clear error counters and error code capture */
  186. priv->write_reg(priv, SJA1000_TXERR, 0x0);
  187. priv->write_reg(priv, SJA1000_RXERR, 0x0);
  188. priv->read_reg(priv, SJA1000_ECC);
  189. /* clear interrupt flags */
  190. priv->read_reg(priv, SJA1000_IR);
  191. /* leave reset mode */
  192. set_normal_mode(dev);
  193. }
  194. static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
  195. {
  196. switch (mode) {
  197. case CAN_MODE_START:
  198. sja1000_start(dev);
  199. if (netif_queue_stopped(dev))
  200. netif_wake_queue(dev);
  201. break;
  202. default:
  203. return -EOPNOTSUPP;
  204. }
  205. return 0;
  206. }
  207. static int sja1000_set_bittiming(struct net_device *dev)
  208. {
  209. struct sja1000_priv *priv = netdev_priv(dev);
  210. struct can_bittiming *bt = &priv->can.bittiming;
  211. u8 btr0, btr1;
  212. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  213. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  214. (((bt->phase_seg2 - 1) & 0x7) << 4);
  215. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  216. btr1 |= 0x80;
  217. netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
  218. priv->write_reg(priv, SJA1000_BTR0, btr0);
  219. priv->write_reg(priv, SJA1000_BTR1, btr1);
  220. return 0;
  221. }
  222. static int sja1000_get_berr_counter(const struct net_device *dev,
  223. struct can_berr_counter *bec)
  224. {
  225. struct sja1000_priv *priv = netdev_priv(dev);
  226. bec->txerr = priv->read_reg(priv, SJA1000_TXERR);
  227. bec->rxerr = priv->read_reg(priv, SJA1000_RXERR);
  228. return 0;
  229. }
  230. /*
  231. * transmit a CAN message
  232. * message layout in the sk_buff should be like this:
  233. * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
  234. * [ can-id ] [flags] [len] [can data (up to 8 bytes]
  235. */
  236. static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
  237. struct net_device *dev)
  238. {
  239. struct sja1000_priv *priv = netdev_priv(dev);
  240. struct can_frame *cf = (struct can_frame *)skb->data;
  241. uint8_t fi;
  242. uint8_t dlc;
  243. canid_t id;
  244. uint8_t dreg;
  245. u8 cmd_reg_val = 0x00;
  246. int i;
  247. if (can_dropped_invalid_skb(dev, skb))
  248. return NETDEV_TX_OK;
  249. netif_stop_queue(dev);
  250. fi = dlc = cf->can_dlc;
  251. id = cf->can_id;
  252. if (id & CAN_RTR_FLAG)
  253. fi |= SJA1000_FI_RTR;
  254. if (id & CAN_EFF_FLAG) {
  255. fi |= SJA1000_FI_FF;
  256. dreg = SJA1000_EFF_BUF;
  257. priv->write_reg(priv, SJA1000_FI, fi);
  258. priv->write_reg(priv, SJA1000_ID1, (id & 0x1fe00000) >> 21);
  259. priv->write_reg(priv, SJA1000_ID2, (id & 0x001fe000) >> 13);
  260. priv->write_reg(priv, SJA1000_ID3, (id & 0x00001fe0) >> 5);
  261. priv->write_reg(priv, SJA1000_ID4, (id & 0x0000001f) << 3);
  262. } else {
  263. dreg = SJA1000_SFF_BUF;
  264. priv->write_reg(priv, SJA1000_FI, fi);
  265. priv->write_reg(priv, SJA1000_ID1, (id & 0x000007f8) >> 3);
  266. priv->write_reg(priv, SJA1000_ID2, (id & 0x00000007) << 5);
  267. }
  268. for (i = 0; i < dlc; i++)
  269. priv->write_reg(priv, dreg++, cf->data[i]);
  270. can_put_echo_skb(skb, dev, 0);
  271. if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  272. cmd_reg_val |= CMD_AT;
  273. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  274. cmd_reg_val |= CMD_SRR;
  275. else
  276. cmd_reg_val |= CMD_TR;
  277. sja1000_write_cmdreg(priv, cmd_reg_val);
  278. return NETDEV_TX_OK;
  279. }
  280. static void sja1000_rx(struct net_device *dev)
  281. {
  282. struct sja1000_priv *priv = netdev_priv(dev);
  283. struct net_device_stats *stats = &dev->stats;
  284. struct can_frame *cf;
  285. struct sk_buff *skb;
  286. uint8_t fi;
  287. uint8_t dreg;
  288. canid_t id;
  289. int i;
  290. /* create zero'ed CAN frame buffer */
  291. skb = alloc_can_skb(dev, &cf);
  292. if (skb == NULL)
  293. return;
  294. fi = priv->read_reg(priv, SJA1000_FI);
  295. if (fi & SJA1000_FI_FF) {
  296. /* extended frame format (EFF) */
  297. dreg = SJA1000_EFF_BUF;
  298. id = (priv->read_reg(priv, SJA1000_ID1) << 21)
  299. | (priv->read_reg(priv, SJA1000_ID2) << 13)
  300. | (priv->read_reg(priv, SJA1000_ID3) << 5)
  301. | (priv->read_reg(priv, SJA1000_ID4) >> 3);
  302. id |= CAN_EFF_FLAG;
  303. } else {
  304. /* standard frame format (SFF) */
  305. dreg = SJA1000_SFF_BUF;
  306. id = (priv->read_reg(priv, SJA1000_ID1) << 3)
  307. | (priv->read_reg(priv, SJA1000_ID2) >> 5);
  308. }
  309. cf->can_dlc = get_can_dlc(fi & 0x0F);
  310. if (fi & SJA1000_FI_RTR) {
  311. id |= CAN_RTR_FLAG;
  312. } else {
  313. for (i = 0; i < cf->can_dlc; i++)
  314. cf->data[i] = priv->read_reg(priv, dreg++);
  315. }
  316. cf->can_id = id;
  317. /* release receive buffer */
  318. sja1000_write_cmdreg(priv, CMD_RRB);
  319. stats->rx_packets++;
  320. stats->rx_bytes += cf->can_dlc;
  321. netif_rx(skb);
  322. can_led_event(dev, CAN_LED_EVENT_RX);
  323. }
  324. static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
  325. {
  326. struct sja1000_priv *priv = netdev_priv(dev);
  327. struct net_device_stats *stats = &dev->stats;
  328. struct can_frame *cf;
  329. struct sk_buff *skb;
  330. enum can_state state = priv->can.state;
  331. enum can_state rx_state, tx_state;
  332. unsigned int rxerr, txerr;
  333. uint8_t ecc, alc;
  334. skb = alloc_can_err_skb(dev, &cf);
  335. if (skb == NULL)
  336. return -ENOMEM;
  337. txerr = priv->read_reg(priv, SJA1000_TXERR);
  338. rxerr = priv->read_reg(priv, SJA1000_RXERR);
  339. cf->data[6] = txerr;
  340. cf->data[7] = rxerr;
  341. if (isrc & IRQ_DOI) {
  342. /* data overrun interrupt */
  343. netdev_dbg(dev, "data overrun interrupt\n");
  344. cf->can_id |= CAN_ERR_CRTL;
  345. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  346. stats->rx_over_errors++;
  347. stats->rx_errors++;
  348. sja1000_write_cmdreg(priv, CMD_CDO); /* clear bit */
  349. }
  350. if (isrc & IRQ_EI) {
  351. /* error warning interrupt */
  352. netdev_dbg(dev, "error warning interrupt\n");
  353. if (status & SR_BS)
  354. state = CAN_STATE_BUS_OFF;
  355. else if (status & SR_ES)
  356. state = CAN_STATE_ERROR_WARNING;
  357. else
  358. state = CAN_STATE_ERROR_ACTIVE;
  359. }
  360. if (isrc & IRQ_BEI) {
  361. /* bus error interrupt */
  362. priv->can.can_stats.bus_error++;
  363. stats->rx_errors++;
  364. ecc = priv->read_reg(priv, SJA1000_ECC);
  365. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  366. /* set error type */
  367. switch (ecc & ECC_MASK) {
  368. case ECC_BIT:
  369. cf->data[2] |= CAN_ERR_PROT_BIT;
  370. break;
  371. case ECC_FORM:
  372. cf->data[2] |= CAN_ERR_PROT_FORM;
  373. break;
  374. case ECC_STUFF:
  375. cf->data[2] |= CAN_ERR_PROT_STUFF;
  376. break;
  377. default:
  378. break;
  379. }
  380. /* set error location */
  381. cf->data[3] = ecc & ECC_SEG;
  382. /* Error occurred during transmission? */
  383. if ((ecc & ECC_DIR) == 0)
  384. cf->data[2] |= CAN_ERR_PROT_TX;
  385. }
  386. if (isrc & IRQ_EPI) {
  387. /* error passive interrupt */
  388. netdev_dbg(dev, "error passive interrupt\n");
  389. if (state == CAN_STATE_ERROR_PASSIVE)
  390. state = CAN_STATE_ERROR_WARNING;
  391. else
  392. state = CAN_STATE_ERROR_PASSIVE;
  393. }
  394. if (isrc & IRQ_ALI) {
  395. /* arbitration lost interrupt */
  396. netdev_dbg(dev, "arbitration lost interrupt\n");
  397. alc = priv->read_reg(priv, SJA1000_ALC);
  398. priv->can.can_stats.arbitration_lost++;
  399. stats->tx_errors++;
  400. cf->can_id |= CAN_ERR_LOSTARB;
  401. cf->data[0] = alc & 0x1f;
  402. }
  403. if (state != priv->can.state) {
  404. tx_state = txerr >= rxerr ? state : 0;
  405. rx_state = txerr <= rxerr ? state : 0;
  406. can_change_state(dev, cf, tx_state, rx_state);
  407. if(state == CAN_STATE_BUS_OFF)
  408. can_bus_off(dev);
  409. }
  410. stats->rx_packets++;
  411. stats->rx_bytes += cf->can_dlc;
  412. netif_rx(skb);
  413. return 0;
  414. }
  415. irqreturn_t sja1000_interrupt(int irq, void *dev_id)
  416. {
  417. struct net_device *dev = (struct net_device *)dev_id;
  418. struct sja1000_priv *priv = netdev_priv(dev);
  419. struct net_device_stats *stats = &dev->stats;
  420. uint8_t isrc, status;
  421. int n = 0;
  422. if (priv->pre_irq)
  423. priv->pre_irq(priv);
  424. /* Shared interrupts and IRQ off? */
  425. if (priv->read_reg(priv, SJA1000_IER) == IRQ_OFF)
  426. goto out;
  427. while ((isrc = priv->read_reg(priv, SJA1000_IR)) &&
  428. (n < SJA1000_MAX_IRQ)) {
  429. status = priv->read_reg(priv, SJA1000_SR);
  430. /* check for absent controller due to hw unplug */
  431. if (status == 0xFF && sja1000_is_absent(priv))
  432. goto out;
  433. if (isrc & IRQ_WUI)
  434. netdev_warn(dev, "wakeup interrupt\n");
  435. if (isrc & IRQ_TI) {
  436. /* transmission buffer released */
  437. if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
  438. !(status & SR_TCS)) {
  439. stats->tx_errors++;
  440. can_free_echo_skb(dev, 0);
  441. } else {
  442. /* transmission complete */
  443. stats->tx_bytes +=
  444. priv->read_reg(priv, SJA1000_FI) & 0xf;
  445. stats->tx_packets++;
  446. can_get_echo_skb(dev, 0);
  447. }
  448. netif_wake_queue(dev);
  449. can_led_event(dev, CAN_LED_EVENT_TX);
  450. }
  451. if (isrc & IRQ_RI) {
  452. /* receive interrupt */
  453. while (status & SR_RBS) {
  454. sja1000_rx(dev);
  455. status = priv->read_reg(priv, SJA1000_SR);
  456. /* check for absent controller */
  457. if (status == 0xFF && sja1000_is_absent(priv))
  458. goto out;
  459. }
  460. }
  461. if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
  462. /* error interrupt */
  463. if (sja1000_err(dev, isrc, status))
  464. break;
  465. }
  466. n++;
  467. }
  468. out:
  469. if (priv->post_irq)
  470. priv->post_irq(priv);
  471. if (n >= SJA1000_MAX_IRQ)
  472. netdev_dbg(dev, "%d messages handled in ISR", n);
  473. return (n) ? IRQ_HANDLED : IRQ_NONE;
  474. }
  475. EXPORT_SYMBOL_GPL(sja1000_interrupt);
  476. static int sja1000_open(struct net_device *dev)
  477. {
  478. struct sja1000_priv *priv = netdev_priv(dev);
  479. int err;
  480. /* set chip into reset mode */
  481. set_reset_mode(dev);
  482. /* common open */
  483. err = open_candev(dev);
  484. if (err)
  485. return err;
  486. /* register interrupt handler, if not done by the device driver */
  487. if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
  488. err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
  489. dev->name, (void *)dev);
  490. if (err) {
  491. close_candev(dev);
  492. return -EAGAIN;
  493. }
  494. }
  495. /* init and start chi */
  496. sja1000_start(dev);
  497. can_led_event(dev, CAN_LED_EVENT_OPEN);
  498. netif_start_queue(dev);
  499. return 0;
  500. }
  501. static int sja1000_close(struct net_device *dev)
  502. {
  503. struct sja1000_priv *priv = netdev_priv(dev);
  504. netif_stop_queue(dev);
  505. set_reset_mode(dev);
  506. if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
  507. free_irq(dev->irq, (void *)dev);
  508. close_candev(dev);
  509. can_led_event(dev, CAN_LED_EVENT_STOP);
  510. return 0;
  511. }
  512. struct net_device *alloc_sja1000dev(int sizeof_priv)
  513. {
  514. struct net_device *dev;
  515. struct sja1000_priv *priv;
  516. dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
  517. SJA1000_ECHO_SKB_MAX);
  518. if (!dev)
  519. return NULL;
  520. priv = netdev_priv(dev);
  521. priv->dev = dev;
  522. priv->can.bittiming_const = &sja1000_bittiming_const;
  523. priv->can.do_set_bittiming = sja1000_set_bittiming;
  524. priv->can.do_set_mode = sja1000_set_mode;
  525. priv->can.do_get_berr_counter = sja1000_get_berr_counter;
  526. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  527. CAN_CTRLMODE_LISTENONLY |
  528. CAN_CTRLMODE_3_SAMPLES |
  529. CAN_CTRLMODE_ONE_SHOT |
  530. CAN_CTRLMODE_BERR_REPORTING |
  531. CAN_CTRLMODE_PRESUME_ACK;
  532. spin_lock_init(&priv->cmdreg_lock);
  533. if (sizeof_priv)
  534. priv->priv = (void *)priv + sizeof(struct sja1000_priv);
  535. return dev;
  536. }
  537. EXPORT_SYMBOL_GPL(alloc_sja1000dev);
  538. void free_sja1000dev(struct net_device *dev)
  539. {
  540. free_candev(dev);
  541. }
  542. EXPORT_SYMBOL_GPL(free_sja1000dev);
  543. static const struct net_device_ops sja1000_netdev_ops = {
  544. .ndo_open = sja1000_open,
  545. .ndo_stop = sja1000_close,
  546. .ndo_start_xmit = sja1000_start_xmit,
  547. .ndo_change_mtu = can_change_mtu,
  548. };
  549. int register_sja1000dev(struct net_device *dev)
  550. {
  551. int ret;
  552. if (!sja1000_probe_chip(dev))
  553. return -ENODEV;
  554. dev->flags |= IFF_ECHO; /* we support local echo */
  555. dev->netdev_ops = &sja1000_netdev_ops;
  556. set_reset_mode(dev);
  557. chipset_init(dev);
  558. ret = register_candev(dev);
  559. if (!ret)
  560. devm_can_led_init(dev);
  561. return ret;
  562. }
  563. EXPORT_SYMBOL_GPL(register_sja1000dev);
  564. void unregister_sja1000dev(struct net_device *dev)
  565. {
  566. set_reset_mode(dev);
  567. unregister_candev(dev);
  568. }
  569. EXPORT_SYMBOL_GPL(unregister_sja1000dev);
  570. static __init int sja1000_init(void)
  571. {
  572. printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
  573. return 0;
  574. }
  575. module_init(sja1000_init);
  576. static __exit void sja1000_exit(void)
  577. {
  578. printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
  579. }
  580. module_exit(sja1000_exit);