network.c 12 KB

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