network.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IPWireless 3G PCMCIA Network Driver
  4. *
  5. * Original code
  6. * by Stephen Blackheath <stephen@blacksapphire.com>,
  7. * Ben Martel <benm@symmetric.co.nz>
  8. *
  9. * Copyrighted as follows:
  10. * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
  11. *
  12. * Various driver changes and rewrites, port to new kernels
  13. * Copyright (C) 2006-2007 Jiri Kosina
  14. *
  15. * Misc code cleanups and updates
  16. * Copyright (C) 2007 David Sterba
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mutex.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/ppp_channel.h>
  23. #include <linux/ppp_defs.h>
  24. #include <linux/slab.h>
  25. #include <linux/ppp-ioctl.h>
  26. #include <linux/skbuff.h>
  27. #include "network.h"
  28. #include "hardware.h"
  29. #include "main.h"
  30. #include "tty.h"
  31. #define MAX_ASSOCIATED_TTYS 2
  32. #define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
  33. struct ipw_network {
  34. /* Hardware context, used for calls to hardware layer. */
  35. struct ipw_hardware *hardware;
  36. /* Context for kernel 'generic_ppp' functionality */
  37. struct ppp_channel *ppp_channel;
  38. /* tty context connected with IPW console */
  39. struct ipw_tty *associated_ttys[NO_OF_IPW_CHANNELS][MAX_ASSOCIATED_TTYS];
  40. /* True if ppp needs waking up once we're ready to xmit */
  41. int ppp_blocked;
  42. /* Number of packets queued up in hardware module. */
  43. int outgoing_packets_queued;
  44. /* Spinlock to avoid interrupts during shutdown */
  45. spinlock_t lock;
  46. struct mutex close_lock;
  47. /* PPP ioctl data, not actually used anywere */
  48. unsigned int flags;
  49. unsigned int rbits;
  50. u32 xaccm[8];
  51. u32 raccm;
  52. int mru;
  53. int shutting_down;
  54. unsigned int ras_control_lines;
  55. struct work_struct work_go_online;
  56. struct work_struct work_go_offline;
  57. };
  58. static void notify_packet_sent(void *callback_data, unsigned int packet_length)
  59. {
  60. struct ipw_network *network = callback_data;
  61. unsigned long flags;
  62. spin_lock_irqsave(&network->lock, flags);
  63. network->outgoing_packets_queued--;
  64. if (network->ppp_channel != NULL) {
  65. if (network->ppp_blocked) {
  66. network->ppp_blocked = 0;
  67. spin_unlock_irqrestore(&network->lock, flags);
  68. ppp_output_wakeup(network->ppp_channel);
  69. if (ipwireless_debug)
  70. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  71. ": ppp unblocked\n");
  72. } else
  73. spin_unlock_irqrestore(&network->lock, flags);
  74. } else
  75. spin_unlock_irqrestore(&network->lock, flags);
  76. }
  77. /*
  78. * Called by the ppp system when it has a packet to send to the hardware.
  79. */
  80. static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel,
  81. struct sk_buff *skb)
  82. {
  83. struct ipw_network *network = ppp_channel->private;
  84. unsigned long flags;
  85. spin_lock_irqsave(&network->lock, flags);
  86. if (network->outgoing_packets_queued < ipwireless_out_queue) {
  87. unsigned char *buf;
  88. static unsigned char header[] = {
  89. PPP_ALLSTATIONS, /* 0xff */
  90. PPP_UI, /* 0x03 */
  91. };
  92. int ret;
  93. network->outgoing_packets_queued++;
  94. spin_unlock_irqrestore(&network->lock, flags);
  95. /*
  96. * If we have the requested amount of headroom in the skb we
  97. * were handed, then we can add the header efficiently.
  98. */
  99. if (skb_headroom(skb) >= 2) {
  100. memcpy(skb_push(skb, 2), header, 2);
  101. ret = ipwireless_send_packet(network->hardware,
  102. IPW_CHANNEL_RAS, skb->data,
  103. skb->len,
  104. notify_packet_sent,
  105. network);
  106. if (ret < 0) {
  107. skb_pull(skb, 2);
  108. return 0;
  109. }
  110. } else {
  111. /* Otherwise (rarely) we do it inefficiently. */
  112. buf = kmalloc(skb->len + 2, GFP_ATOMIC);
  113. if (!buf)
  114. return 0;
  115. memcpy(buf + 2, skb->data, skb->len);
  116. memcpy(buf, header, 2);
  117. ret = ipwireless_send_packet(network->hardware,
  118. IPW_CHANNEL_RAS, buf,
  119. skb->len + 2,
  120. notify_packet_sent,
  121. network);
  122. kfree(buf);
  123. if (ret < 0)
  124. return 0;
  125. }
  126. kfree_skb(skb);
  127. return 1;
  128. } else {
  129. /*
  130. * Otherwise reject the packet, and flag that the ppp system
  131. * needs to be unblocked once we are ready to send.
  132. */
  133. network->ppp_blocked = 1;
  134. spin_unlock_irqrestore(&network->lock, flags);
  135. if (ipwireless_debug)
  136. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
  137. return 0;
  138. }
  139. }
  140. /* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
  141. static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
  142. unsigned int cmd, unsigned long arg)
  143. {
  144. struct ipw_network *network = ppp_channel->private;
  145. int err, val;
  146. u32 accm[8];
  147. int __user *user_arg = (int __user *) arg;
  148. err = -EFAULT;
  149. switch (cmd) {
  150. case PPPIOCGFLAGS:
  151. val = network->flags | network->rbits;
  152. if (put_user(val, user_arg))
  153. break;
  154. err = 0;
  155. break;
  156. case PPPIOCSFLAGS:
  157. if (get_user(val, user_arg))
  158. break;
  159. network->flags = val & ~SC_RCV_BITS;
  160. network->rbits = val & SC_RCV_BITS;
  161. err = 0;
  162. break;
  163. case PPPIOCGASYNCMAP:
  164. if (put_user(network->xaccm[0], user_arg))
  165. break;
  166. err = 0;
  167. break;
  168. case PPPIOCSASYNCMAP:
  169. if (get_user(network->xaccm[0], user_arg))
  170. break;
  171. err = 0;
  172. break;
  173. case PPPIOCGRASYNCMAP:
  174. if (put_user(network->raccm, user_arg))
  175. break;
  176. err = 0;
  177. break;
  178. case PPPIOCSRASYNCMAP:
  179. if (get_user(network->raccm, user_arg))
  180. break;
  181. err = 0;
  182. break;
  183. case PPPIOCGXASYNCMAP:
  184. if (copy_to_user((void __user *) arg, network->xaccm,
  185. sizeof(network->xaccm)))
  186. break;
  187. err = 0;
  188. break;
  189. case PPPIOCSXASYNCMAP:
  190. if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
  191. break;
  192. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  193. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  194. memcpy(network->xaccm, accm, sizeof(network->xaccm));
  195. err = 0;
  196. break;
  197. case PPPIOCGMRU:
  198. if (put_user(network->mru, user_arg))
  199. break;
  200. err = 0;
  201. break;
  202. case PPPIOCSMRU:
  203. if (get_user(val, user_arg))
  204. break;
  205. if (val < PPP_MRU)
  206. val = PPP_MRU;
  207. network->mru = val;
  208. err = 0;
  209. break;
  210. default:
  211. err = -ENOTTY;
  212. }
  213. return err;
  214. }
  215. static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
  216. .start_xmit = ipwireless_ppp_start_xmit,
  217. .ioctl = ipwireless_ppp_ioctl
  218. };
  219. static void do_go_online(struct work_struct *work_go_online)
  220. {
  221. struct ipw_network *network =
  222. container_of(work_go_online, struct ipw_network,
  223. work_go_online);
  224. unsigned long flags;
  225. spin_lock_irqsave(&network->lock, flags);
  226. if (!network->ppp_channel) {
  227. struct ppp_channel *channel;
  228. spin_unlock_irqrestore(&network->lock, flags);
  229. channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
  230. if (!channel) {
  231. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  232. ": unable to allocate PPP channel\n");
  233. return;
  234. }
  235. channel->private = network;
  236. channel->mtu = 16384; /* Wild guess */
  237. channel->hdrlen = 2;
  238. channel->ops = &ipwireless_ppp_channel_ops;
  239. network->flags = 0;
  240. network->rbits = 0;
  241. network->mru = PPP_MRU;
  242. memset(network->xaccm, 0, sizeof(network->xaccm));
  243. network->xaccm[0] = ~0U;
  244. network->xaccm[3] = 0x60000000U;
  245. network->raccm = ~0U;
  246. if (ppp_register_channel(channel) < 0) {
  247. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  248. ": unable to register PPP channel\n");
  249. kfree(channel);
  250. return;
  251. }
  252. spin_lock_irqsave(&network->lock, flags);
  253. network->ppp_channel = channel;
  254. }
  255. spin_unlock_irqrestore(&network->lock, flags);
  256. }
  257. static void do_go_offline(struct work_struct *work_go_offline)
  258. {
  259. struct ipw_network *network =
  260. container_of(work_go_offline, struct ipw_network,
  261. work_go_offline);
  262. unsigned long flags;
  263. mutex_lock(&network->close_lock);
  264. spin_lock_irqsave(&network->lock, flags);
  265. if (network->ppp_channel != NULL) {
  266. struct ppp_channel *channel = network->ppp_channel;
  267. network->ppp_channel = NULL;
  268. spin_unlock_irqrestore(&network->lock, flags);
  269. mutex_unlock(&network->close_lock);
  270. ppp_unregister_channel(channel);
  271. } else {
  272. spin_unlock_irqrestore(&network->lock, flags);
  273. mutex_unlock(&network->close_lock);
  274. }
  275. }
  276. void ipwireless_network_notify_control_line_change(struct ipw_network *network,
  277. unsigned int channel_idx,
  278. unsigned int control_lines,
  279. unsigned int changed_mask)
  280. {
  281. int i;
  282. if (channel_idx == IPW_CHANNEL_RAS)
  283. network->ras_control_lines = control_lines;
  284. for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
  285. struct ipw_tty *tty =
  286. network->associated_ttys[channel_idx][i];
  287. /*
  288. * If it's associated with a tty (other than the RAS channel
  289. * when we're online), then send the data to that tty. The RAS
  290. * channel's data is handled above - it always goes through
  291. * ppp_generic.
  292. */
  293. if (tty)
  294. ipwireless_tty_notify_control_line_change(tty,
  295. channel_idx,
  296. control_lines,
  297. changed_mask);
  298. }
  299. }
  300. /*
  301. * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
  302. * bytes, which are required on sent packet, but not always present on received
  303. * packets
  304. */
  305. static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
  306. unsigned int length)
  307. {
  308. struct sk_buff *skb;
  309. if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
  310. length -= 2;
  311. data += 2;
  312. }
  313. skb = dev_alloc_skb(length + 4);
  314. if (skb == NULL)
  315. return NULL;
  316. skb_reserve(skb, 2);
  317. skb_put_data(skb, data, length);
  318. return skb;
  319. }
  320. void ipwireless_network_packet_received(struct ipw_network *network,
  321. unsigned int channel_idx,
  322. unsigned char *data,
  323. unsigned int length)
  324. {
  325. int i;
  326. unsigned long flags;
  327. for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
  328. struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
  329. if (!tty)
  330. continue;
  331. /*
  332. * If it's associated with a tty (other than the RAS channel
  333. * when we're online), then send the data to that tty. The RAS
  334. * channel's data is handled above - it always goes through
  335. * ppp_generic.
  336. */
  337. if (channel_idx == IPW_CHANNEL_RAS
  338. && (network->ras_control_lines &
  339. IPW_CONTROL_LINE_DCD) != 0
  340. && ipwireless_tty_is_modem(tty)) {
  341. /*
  342. * If data came in on the RAS channel and this tty is
  343. * the modem tty, and we are online, then we send it to
  344. * the PPP layer.
  345. */
  346. mutex_lock(&network->close_lock);
  347. spin_lock_irqsave(&network->lock, flags);
  348. if (network->ppp_channel != NULL) {
  349. struct sk_buff *skb;
  350. spin_unlock_irqrestore(&network->lock,
  351. flags);
  352. /* Send the data to the ppp_generic module. */
  353. skb = ipw_packet_received_skb(data, length);
  354. if (skb)
  355. ppp_input(network->ppp_channel, skb);
  356. } else
  357. spin_unlock_irqrestore(&network->lock,
  358. flags);
  359. mutex_unlock(&network->close_lock);
  360. }
  361. /* Otherwise we send it out the tty. */
  362. else
  363. ipwireless_tty_received(tty, data, length);
  364. }
  365. }
  366. struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
  367. {
  368. struct ipw_network *network =
  369. kzalloc(sizeof(struct ipw_network), GFP_KERNEL);
  370. if (!network)
  371. return NULL;
  372. spin_lock_init(&network->lock);
  373. mutex_init(&network->close_lock);
  374. network->hardware = hw;
  375. INIT_WORK(&network->work_go_online, do_go_online);
  376. INIT_WORK(&network->work_go_offline, do_go_offline);
  377. ipwireless_associate_network(hw, network);
  378. return network;
  379. }
  380. void ipwireless_network_free(struct ipw_network *network)
  381. {
  382. network->shutting_down = 1;
  383. ipwireless_ppp_close(network);
  384. flush_work(&network->work_go_online);
  385. flush_work(&network->work_go_offline);
  386. ipwireless_stop_interrupts(network->hardware);
  387. ipwireless_associate_network(network->hardware, NULL);
  388. kfree(network);
  389. }
  390. void ipwireless_associate_network_tty(struct ipw_network *network,
  391. unsigned int channel_idx,
  392. struct ipw_tty *tty)
  393. {
  394. int i;
  395. for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
  396. if (network->associated_ttys[channel_idx][i] == NULL) {
  397. network->associated_ttys[channel_idx][i] = tty;
  398. break;
  399. }
  400. }
  401. void ipwireless_disassociate_network_ttys(struct ipw_network *network,
  402. unsigned int channel_idx)
  403. {
  404. int i;
  405. for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
  406. network->associated_ttys[channel_idx][i] = NULL;
  407. }
  408. void ipwireless_ppp_open(struct ipw_network *network)
  409. {
  410. if (ipwireless_debug)
  411. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
  412. schedule_work(&network->work_go_online);
  413. }
  414. void ipwireless_ppp_close(struct ipw_network *network)
  415. {
  416. /* Disconnect from the wireless network. */
  417. if (ipwireless_debug)
  418. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n");
  419. schedule_work(&network->work_go_offline);
  420. }
  421. int ipwireless_ppp_channel_index(struct ipw_network *network)
  422. {
  423. int ret = -1;
  424. unsigned long flags;
  425. spin_lock_irqsave(&network->lock, flags);
  426. if (network->ppp_channel != NULL)
  427. ret = ppp_channel_index(network->ppp_channel);
  428. spin_unlock_irqrestore(&network->lock, flags);
  429. return ret;
  430. }
  431. int ipwireless_ppp_unit_number(struct ipw_network *network)
  432. {
  433. int ret = -1;
  434. unsigned long flags;
  435. spin_lock_irqsave(&network->lock, flags);
  436. if (network->ppp_channel != NULL)
  437. ret = ppp_unit_number(network->ppp_channel);
  438. spin_unlock_irqrestore(&network->lock, flags);
  439. return ret;
  440. }
  441. int ipwireless_ppp_mru(const struct ipw_network *network)
  442. {
  443. return network->mru;
  444. }