network.c 12 KB

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