slcan.c 19 KB

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