slcan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * slcan.c - serial line CAN interface driver (using tty line discipline)
  3. *
  4. * This file is derived from linux/drivers/net/slip/slip.c
  5. *
  6. * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
  7. * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
  8. * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see http://www.gnu.org/licenses/gpl.html
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  34. * DAMAGE.
  35. *
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/bitops.h>
  41. #include <linux/string.h>
  42. #include <linux/tty.h>
  43. #include <linux/errno.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/rtnetlink.h>
  47. #include <linux/if_arp.h>
  48. #include <linux/if_ether.h>
  49. #include <linux/sched.h>
  50. #include <linux/delay.h>
  51. #include <linux/init.h>
  52. #include <linux/kernel.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/can.h>
  55. #include <linux/can/skb.h>
  56. MODULE_ALIAS_LDISC(N_SLCAN);
  57. MODULE_DESCRIPTION("serial line CAN interface");
  58. MODULE_LICENSE("GPL");
  59. MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
  60. #define SLCAN_MAGIC 0x53CA
  61. static int maxdev = 10; /* MAX number of SLCAN channels;
  62. This can be overridden with
  63. insmod slcan.ko maxdev=nnn */
  64. module_param(maxdev, int, 0);
  65. MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
  66. /* maximum rx buffer len: extended CAN frame with timestamp */
  67. #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
  68. #define SLC_CMD_LEN 1
  69. #define SLC_SFF_ID_LEN 3
  70. #define SLC_EFF_ID_LEN 8
  71. struct slcan {
  72. int magic;
  73. /* Various fields. */
  74. struct tty_struct *tty; /* ptr to TTY structure */
  75. struct net_device *dev; /* easy for intr handling */
  76. spinlock_t lock;
  77. struct work_struct tx_work; /* Flushes transmit buffer */
  78. /* These are pointers to the malloc()ed frame buffers. */
  79. unsigned char rbuff[SLC_MTU]; /* receiver buffer */
  80. int rcount; /* received chars counter */
  81. unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
  82. unsigned char *xhead; /* pointer to next XMIT byte */
  83. int xleft; /* bytes left in XMIT queue */
  84. unsigned long flags; /* Flag values/ mode etc */
  85. #define SLF_INUSE 0 /* Channel in use */
  86. #define SLF_ERROR 1 /* Parity, etc. error */
  87. };
  88. static struct net_device **slcan_devs;
  89. /************************************************************************
  90. * SLCAN ENCAPSULATION FORMAT *
  91. ************************************************************************/
  92. /*
  93. * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
  94. * frame format) a data length code (can_dlc) which can be from 0 to 8
  95. * and up to <can_dlc> data bytes as payload.
  96. * Additionally a CAN frame may become a remote transmission frame if the
  97. * RTR-bit is set. This causes another ECU to send a CAN frame with the
  98. * given can_id.
  99. *
  100. * The SLCAN ASCII representation of these different frame types is:
  101. * <type> <id> <dlc> <data>*
  102. *
  103. * Extended frames (29 bit) are defined by capital characters in the type.
  104. * RTR frames are defined as 'r' types - normal frames have 't' type:
  105. * t => 11 bit data frame
  106. * r => 11 bit RTR frame
  107. * T => 29 bit data frame
  108. * R => 29 bit RTR frame
  109. *
  110. * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
  111. * The <dlc> is a one byte ASCII number ('0' - '8')
  112. * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
  113. *
  114. * Examples:
  115. *
  116. * t1230 : can_id 0x123, can_dlc 0, no data
  117. * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
  118. * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
  119. * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
  120. *
  121. */
  122. /************************************************************************
  123. * STANDARD SLCAN DECAPSULATION *
  124. ************************************************************************/
  125. /* Send one completely decapsulated can_frame to the network layer */
  126. static void slc_bump(struct slcan *sl)
  127. {
  128. struct sk_buff *skb;
  129. struct can_frame cf;
  130. int i, tmp;
  131. u32 tmpid;
  132. char *cmd = sl->rbuff;
  133. memset(&cf, 0, sizeof(cf));
  134. switch (*cmd) {
  135. case 'r':
  136. cf.can_id = CAN_RTR_FLAG;
  137. /* fallthrough */
  138. case 't':
  139. /* store dlc ASCII value and terminate SFF CAN ID string */
  140. cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN];
  141. sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0;
  142. /* point to payload data behind the dlc */
  143. cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1;
  144. break;
  145. case 'R':
  146. cf.can_id = CAN_RTR_FLAG;
  147. /* fallthrough */
  148. case 'T':
  149. cf.can_id |= CAN_EFF_FLAG;
  150. /* store dlc ASCII value and terminate EFF CAN ID string */
  151. cf.can_dlc = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN];
  152. sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0;
  153. /* point to payload data behind the dlc */
  154. cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1;
  155. break;
  156. default:
  157. return;
  158. }
  159. if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid))
  160. return;
  161. cf.can_id |= tmpid;
  162. /* get can_dlc from sanitized ASCII value */
  163. if (cf.can_dlc >= '0' && cf.can_dlc < '9')
  164. cf.can_dlc -= '0';
  165. else
  166. return;
  167. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  168. if (!(cf.can_id & CAN_RTR_FLAG)) {
  169. for (i = 0; i < cf.can_dlc; i++) {
  170. tmp = hex_to_bin(*cmd++);
  171. if (tmp < 0)
  172. return;
  173. cf.data[i] = (tmp << 4);
  174. tmp = hex_to_bin(*cmd++);
  175. if (tmp < 0)
  176. return;
  177. cf.data[i] |= tmp;
  178. }
  179. }
  180. skb = dev_alloc_skb(sizeof(struct can_frame) +
  181. sizeof(struct can_skb_priv));
  182. if (!skb)
  183. return;
  184. skb->dev = sl->dev;
  185. skb->protocol = htons(ETH_P_CAN);
  186. skb->pkt_type = PACKET_BROADCAST;
  187. skb->ip_summed = CHECKSUM_UNNECESSARY;
  188. can_skb_reserve(skb);
  189. can_skb_prv(skb)->ifindex = sl->dev->ifindex;
  190. can_skb_prv(skb)->skbcnt = 0;
  191. skb_put_data(skb, &cf, sizeof(struct can_frame));
  192. sl->dev->stats.rx_packets++;
  193. sl->dev->stats.rx_bytes += cf.can_dlc;
  194. netif_rx_ni(skb);
  195. }
  196. /* parse tty input stream */
  197. static void slcan_unesc(struct slcan *sl, unsigned char s)
  198. {
  199. if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
  200. if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
  201. (sl->rcount > 4)) {
  202. slc_bump(sl);
  203. }
  204. sl->rcount = 0;
  205. } else {
  206. if (!test_bit(SLF_ERROR, &sl->flags)) {
  207. if (sl->rcount < SLC_MTU) {
  208. sl->rbuff[sl->rcount++] = s;
  209. return;
  210. } else {
  211. sl->dev->stats.rx_over_errors++;
  212. set_bit(SLF_ERROR, &sl->flags);
  213. }
  214. }
  215. }
  216. }
  217. /************************************************************************
  218. * STANDARD SLCAN ENCAPSULATION *
  219. ************************************************************************/
  220. /* Encapsulate one can_frame and stuff into a TTY queue. */
  221. static void slc_encaps(struct slcan *sl, struct can_frame *cf)
  222. {
  223. int actual, i;
  224. unsigned char *pos;
  225. unsigned char *endpos;
  226. canid_t id = cf->can_id;
  227. pos = sl->xbuff;
  228. if (cf->can_id & CAN_RTR_FLAG)
  229. *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */
  230. else
  231. *pos = 'T'; /* becomes 't' in standard frame format (SSF) */
  232. /* determine number of chars for the CAN-identifier */
  233. if (cf->can_id & CAN_EFF_FLAG) {
  234. id &= CAN_EFF_MASK;
  235. endpos = pos + SLC_EFF_ID_LEN;
  236. } else {
  237. *pos |= 0x20; /* convert R/T to lower case for SFF */
  238. id &= CAN_SFF_MASK;
  239. endpos = pos + SLC_SFF_ID_LEN;
  240. }
  241. /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
  242. pos++;
  243. while (endpos >= pos) {
  244. *endpos-- = hex_asc_upper[id & 0xf];
  245. id >>= 4;
  246. }
  247. pos += (cf->can_id & CAN_EFF_FLAG) ? SLC_EFF_ID_LEN : SLC_SFF_ID_LEN;
  248. *pos++ = cf->can_dlc + '0';
  249. /* RTR frames may have a dlc > 0 but they never have any data bytes */
  250. if (!(cf->can_id & CAN_RTR_FLAG)) {
  251. for (i = 0; i < cf->can_dlc; i++)
  252. pos = hex_byte_pack_upper(pos, cf->data[i]);
  253. }
  254. *pos++ = '\r';
  255. /* Order of next two lines is *very* important.
  256. * When we are sending a little amount of data,
  257. * the transfer may be completed inside the ops->write()
  258. * routine, because it's running with interrupts enabled.
  259. * In this case we *never* got WRITE_WAKEUP event,
  260. * if we did not request it before write operation.
  261. * 14 Oct 1994 Dmitry Gorodchanin.
  262. */
  263. set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  264. actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff);
  265. sl->xleft = (pos - sl->xbuff) - actual;
  266. sl->xhead = sl->xbuff + actual;
  267. sl->dev->stats.tx_bytes += cf->can_dlc;
  268. }
  269. /* Write out any remaining transmit buffer. Scheduled when tty is writable */
  270. static void slcan_transmit(struct work_struct *work)
  271. {
  272. struct slcan *sl = container_of(work, struct slcan, tx_work);
  273. int actual;
  274. spin_lock_bh(&sl->lock);
  275. /* First make sure we're connected. */
  276. if (!sl->tty || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) {
  277. spin_unlock_bh(&sl->lock);
  278. return;
  279. }
  280. if (sl->xleft <= 0) {
  281. /* Now serial buffer is almost free & we can start
  282. * transmission of another packet */
  283. sl->dev->stats.tx_packets++;
  284. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  285. spin_unlock_bh(&sl->lock);
  286. netif_wake_queue(sl->dev);
  287. return;
  288. }
  289. actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft);
  290. sl->xleft -= actual;
  291. sl->xhead += actual;
  292. spin_unlock_bh(&sl->lock);
  293. }
  294. /*
  295. * Called by the driver when there's room for more data.
  296. * Schedule the transmit.
  297. */
  298. static void slcan_write_wakeup(struct tty_struct *tty)
  299. {
  300. struct slcan *sl;
  301. rcu_read_lock();
  302. sl = rcu_dereference(tty->disc_data);
  303. if (!sl)
  304. goto out;
  305. schedule_work(&sl->tx_work);
  306. out:
  307. rcu_read_unlock();
  308. }
  309. /* Send a can_frame to a TTY queue. */
  310. static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
  311. {
  312. struct slcan *sl = netdev_priv(dev);
  313. if (skb->len != CAN_MTU)
  314. goto out;
  315. spin_lock(&sl->lock);
  316. if (!netif_running(dev)) {
  317. spin_unlock(&sl->lock);
  318. printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
  319. goto out;
  320. }
  321. if (sl->tty == NULL) {
  322. spin_unlock(&sl->lock);
  323. goto out;
  324. }
  325. netif_stop_queue(sl->dev);
  326. slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
  327. spin_unlock(&sl->lock);
  328. out:
  329. kfree_skb(skb);
  330. return NETDEV_TX_OK;
  331. }
  332. /******************************************
  333. * Routines looking at netdevice side.
  334. ******************************************/
  335. /* Netdevice UP -> DOWN routine */
  336. static int slc_close(struct net_device *dev)
  337. {
  338. struct slcan *sl = netdev_priv(dev);
  339. spin_lock_bh(&sl->lock);
  340. if (sl->tty) {
  341. /* TTY discipline is running. */
  342. clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
  343. }
  344. netif_stop_queue(dev);
  345. sl->rcount = 0;
  346. sl->xleft = 0;
  347. spin_unlock_bh(&sl->lock);
  348. return 0;
  349. }
  350. /* Netdevice DOWN -> UP routine */
  351. static int slc_open(struct net_device *dev)
  352. {
  353. struct slcan *sl = netdev_priv(dev);
  354. if (sl->tty == NULL)
  355. return -ENODEV;
  356. sl->flags &= (1 << SLF_INUSE);
  357. netif_start_queue(dev);
  358. return 0;
  359. }
  360. /* Hook the destructor so we can free slcan devs at the right point in time */
  361. static void slc_free_netdev(struct net_device *dev)
  362. {
  363. int i = dev->base_addr;
  364. slcan_devs[i] = NULL;
  365. }
  366. static int slcan_change_mtu(struct net_device *dev, int new_mtu)
  367. {
  368. return -EINVAL;
  369. }
  370. static const struct net_device_ops slc_netdev_ops = {
  371. .ndo_open = slc_open,
  372. .ndo_stop = slc_close,
  373. .ndo_start_xmit = slc_xmit,
  374. .ndo_change_mtu = slcan_change_mtu,
  375. };
  376. static void slc_setup(struct net_device *dev)
  377. {
  378. dev->netdev_ops = &slc_netdev_ops;
  379. dev->needs_free_netdev = true;
  380. dev->priv_destructor = slc_free_netdev;
  381. dev->hard_header_len = 0;
  382. dev->addr_len = 0;
  383. dev->tx_queue_len = 10;
  384. dev->mtu = CAN_MTU;
  385. dev->type = ARPHRD_CAN;
  386. /* New-style flags. */
  387. dev->flags = IFF_NOARP;
  388. dev->features = NETIF_F_HW_CSUM;
  389. }
  390. /******************************************
  391. Routines looking at TTY side.
  392. ******************************************/
  393. /*
  394. * Handle the 'receiver data ready' interrupt.
  395. * This function is called by the 'tty_io' module in the kernel when
  396. * a block of SLCAN data has been received, which can now be decapsulated
  397. * and sent on to some IP layer for further processing. This will not
  398. * be re-entered while running but other ldisc functions may be called
  399. * in parallel
  400. */
  401. static void slcan_receive_buf(struct tty_struct *tty,
  402. const unsigned char *cp, char *fp, int count)
  403. {
  404. struct slcan *sl = (struct slcan *) tty->disc_data;
  405. if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
  406. return;
  407. /* Read the characters out of the buffer */
  408. while (count--) {
  409. if (fp && *fp++) {
  410. if (!test_and_set_bit(SLF_ERROR, &sl->flags))
  411. sl->dev->stats.rx_errors++;
  412. cp++;
  413. continue;
  414. }
  415. slcan_unesc(sl, *cp++);
  416. }
  417. }
  418. /************************************
  419. * slcan_open helper routines.
  420. ************************************/
  421. /* Collect hanged up channels */
  422. static void slc_sync(void)
  423. {
  424. int i;
  425. struct net_device *dev;
  426. struct slcan *sl;
  427. for (i = 0; i < maxdev; i++) {
  428. dev = slcan_devs[i];
  429. if (dev == NULL)
  430. break;
  431. sl = netdev_priv(dev);
  432. if (sl->tty)
  433. continue;
  434. if (dev->flags & IFF_UP)
  435. dev_close(dev);
  436. }
  437. }
  438. /* Find a free SLCAN channel, and link in this `tty' line. */
  439. static struct slcan *slc_alloc(void)
  440. {
  441. int i;
  442. char name[IFNAMSIZ];
  443. struct net_device *dev = NULL;
  444. struct slcan *sl;
  445. for (i = 0; i < maxdev; i++) {
  446. dev = slcan_devs[i];
  447. if (dev == NULL)
  448. break;
  449. }
  450. /* Sorry, too many, all slots in use */
  451. if (i >= maxdev)
  452. return NULL;
  453. sprintf(name, "slcan%d", i);
  454. dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, slc_setup);
  455. if (!dev)
  456. return NULL;
  457. dev->base_addr = i;
  458. sl = netdev_priv(dev);
  459. /* Initialize channel control data */
  460. sl->magic = SLCAN_MAGIC;
  461. sl->dev = dev;
  462. spin_lock_init(&sl->lock);
  463. INIT_WORK(&sl->tx_work, slcan_transmit);
  464. slcan_devs[i] = dev;
  465. return sl;
  466. }
  467. /*
  468. * Open the high-level part of the SLCAN channel.
  469. * This function is called by the TTY module when the
  470. * SLCAN line discipline is called for. Because we are
  471. * sure the tty line exists, we only have to link it to
  472. * a free SLCAN channel...
  473. *
  474. * Called in process context serialized from other ldisc calls.
  475. */
  476. static int slcan_open(struct tty_struct *tty)
  477. {
  478. struct slcan *sl;
  479. int err;
  480. if (!capable(CAP_NET_ADMIN))
  481. return -EPERM;
  482. if (tty->ops->write == NULL)
  483. return -EOPNOTSUPP;
  484. /* RTnetlink lock is misused here to serialize concurrent
  485. opens of slcan channels. There are better ways, but it is
  486. the simplest one.
  487. */
  488. rtnl_lock();
  489. /* Collect hanged up channels. */
  490. slc_sync();
  491. sl = tty->disc_data;
  492. err = -EEXIST;
  493. /* First make sure we're not already connected. */
  494. if (sl && sl->magic == SLCAN_MAGIC)
  495. goto err_exit;
  496. /* OK. Find a free SLCAN channel to use. */
  497. err = -ENFILE;
  498. sl = slc_alloc();
  499. if (sl == NULL)
  500. goto err_exit;
  501. sl->tty = tty;
  502. tty->disc_data = sl;
  503. if (!test_bit(SLF_INUSE, &sl->flags)) {
  504. /* Perform the low-level SLCAN initialization. */
  505. sl->rcount = 0;
  506. sl->xleft = 0;
  507. set_bit(SLF_INUSE, &sl->flags);
  508. err = register_netdevice(sl->dev);
  509. if (err)
  510. goto err_free_chan;
  511. }
  512. /* Done. We have linked the TTY line to a channel. */
  513. rtnl_unlock();
  514. tty->receive_room = 65536; /* We don't flow control */
  515. /* TTY layer expects 0 on success */
  516. return 0;
  517. err_free_chan:
  518. sl->tty = NULL;
  519. tty->disc_data = NULL;
  520. clear_bit(SLF_INUSE, &sl->flags);
  521. slc_free_netdev(sl->dev);
  522. /* do not call free_netdev before rtnl_unlock */
  523. rtnl_unlock();
  524. free_netdev(sl->dev);
  525. return err;
  526. err_exit:
  527. rtnl_unlock();
  528. /* Count references from TTY module */
  529. return err;
  530. }
  531. /*
  532. * Close down a SLCAN channel.
  533. * This means flushing out any pending queues, and then returning. This
  534. * call is serialized against other ldisc functions.
  535. *
  536. * We also use this method for a hangup event.
  537. */
  538. static void slcan_close(struct tty_struct *tty)
  539. {
  540. struct slcan *sl = (struct slcan *) tty->disc_data;
  541. /* First make sure we're connected. */
  542. if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
  543. return;
  544. spin_lock_bh(&sl->lock);
  545. rcu_assign_pointer(tty->disc_data, NULL);
  546. sl->tty = NULL;
  547. spin_unlock_bh(&sl->lock);
  548. synchronize_rcu();
  549. flush_work(&sl->tx_work);
  550. /* Flush network side */
  551. unregister_netdev(sl->dev);
  552. /* This will complete via sl_free_netdev */
  553. }
  554. static int slcan_hangup(struct tty_struct *tty)
  555. {
  556. slcan_close(tty);
  557. return 0;
  558. }
  559. /* Perform I/O control on an active SLCAN channel. */
  560. static int slcan_ioctl(struct tty_struct *tty, struct file *file,
  561. unsigned int cmd, unsigned long arg)
  562. {
  563. struct slcan *sl = (struct slcan *) tty->disc_data;
  564. unsigned int tmp;
  565. /* First make sure we're connected. */
  566. if (!sl || sl->magic != SLCAN_MAGIC)
  567. return -EINVAL;
  568. switch (cmd) {
  569. case SIOCGIFNAME:
  570. tmp = strlen(sl->dev->name) + 1;
  571. if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
  572. return -EFAULT;
  573. return 0;
  574. case SIOCSIFHWADDR:
  575. return -EINVAL;
  576. default:
  577. return tty_mode_ioctl(tty, file, cmd, arg);
  578. }
  579. }
  580. static struct tty_ldisc_ops slc_ldisc = {
  581. .owner = THIS_MODULE,
  582. .magic = TTY_LDISC_MAGIC,
  583. .name = "slcan",
  584. .open = slcan_open,
  585. .close = slcan_close,
  586. .hangup = slcan_hangup,
  587. .ioctl = slcan_ioctl,
  588. .receive_buf = slcan_receive_buf,
  589. .write_wakeup = slcan_write_wakeup,
  590. };
  591. static int __init slcan_init(void)
  592. {
  593. int status;
  594. if (maxdev < 4)
  595. maxdev = 4; /* Sanity */
  596. pr_info("slcan: serial line CAN interface driver\n");
  597. pr_info("slcan: %d dynamic interface channels.\n", maxdev);
  598. slcan_devs = kcalloc(maxdev, sizeof(struct net_device *), GFP_KERNEL);
  599. if (!slcan_devs)
  600. return -ENOMEM;
  601. /* Fill in our line protocol discipline, and register it */
  602. status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
  603. if (status) {
  604. printk(KERN_ERR "slcan: can't register line discipline\n");
  605. kfree(slcan_devs);
  606. }
  607. return status;
  608. }
  609. static void __exit slcan_exit(void)
  610. {
  611. int i;
  612. struct net_device *dev;
  613. struct slcan *sl;
  614. unsigned long timeout = jiffies + HZ;
  615. int busy = 0;
  616. if (slcan_devs == NULL)
  617. return;
  618. /* First of all: check for active disciplines and hangup them.
  619. */
  620. do {
  621. if (busy)
  622. msleep_interruptible(100);
  623. busy = 0;
  624. for (i = 0; i < maxdev; i++) {
  625. dev = slcan_devs[i];
  626. if (!dev)
  627. continue;
  628. sl = netdev_priv(dev);
  629. spin_lock_bh(&sl->lock);
  630. if (sl->tty) {
  631. busy++;
  632. tty_hangup(sl->tty);
  633. }
  634. spin_unlock_bh(&sl->lock);
  635. }
  636. } while (busy && time_before(jiffies, timeout));
  637. /* FIXME: hangup is async so we should wait when doing this second
  638. phase */
  639. for (i = 0; i < maxdev; i++) {
  640. dev = slcan_devs[i];
  641. if (!dev)
  642. continue;
  643. slcan_devs[i] = NULL;
  644. sl = netdev_priv(dev);
  645. if (sl->tty) {
  646. printk(KERN_ERR "%s: tty discipline still running\n",
  647. dev->name);
  648. }
  649. unregister_netdev(dev);
  650. }
  651. kfree(slcan_devs);
  652. slcan_devs = NULL;
  653. i = tty_unregister_ldisc(N_SLCAN);
  654. if (i)
  655. printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
  656. }
  657. module_init(slcan_init);
  658. module_exit(slcan_exit);