network.c 13 KB

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