main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "hardware.h"
  19. #include "network.h"
  20. #include "main.h"
  21. #include "tty.h"
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <pcmcia/cisreg.h>
  30. #include <pcmcia/device_id.h>
  31. #include <pcmcia/ss.h>
  32. #include <pcmcia/ds.h>
  33. static const struct pcmcia_device_id ipw_ids[] = {
  34. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
  35. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
  36. PCMCIA_DEVICE_NULL
  37. };
  38. MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
  39. static void ipwireless_detach(struct pcmcia_device *link);
  40. /*
  41. * Module params
  42. */
  43. /* Debug mode: more verbose, print sent/recv bytes */
  44. int ipwireless_debug;
  45. int ipwireless_loopback;
  46. int ipwireless_out_queue = 10;
  47. module_param_named(debug, ipwireless_debug, int, 0);
  48. module_param_named(loopback, ipwireless_loopback, int, 0);
  49. module_param_named(out_queue, ipwireless_out_queue, int, 0);
  50. MODULE_PARM_DESC(debug, "switch on debug messages [0]");
  51. MODULE_PARM_DESC(loopback,
  52. "debug: enable ras_raw channel [0]");
  53. MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
  54. /* Executes in process context. */
  55. static void signalled_reboot_work(struct work_struct *work_reboot)
  56. {
  57. struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
  58. work_reboot);
  59. struct pcmcia_device *link = ipw->link;
  60. pcmcia_reset_card(link->socket);
  61. }
  62. static void signalled_reboot_callback(void *callback_data)
  63. {
  64. struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
  65. /* Delegate to process context. */
  66. schedule_work(&ipw->work_reboot);
  67. }
  68. static int ipwireless_probe(struct pcmcia_device *p_dev, void *priv_data)
  69. {
  70. struct ipw_dev *ipw = priv_data;
  71. int ret;
  72. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  73. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  74. /* 0x40 causes it to generate level mode interrupts. */
  75. /* 0x04 enables IREQ pin. */
  76. p_dev->config_index |= 0x44;
  77. p_dev->io_lines = 16;
  78. ret = pcmcia_request_io(p_dev);
  79. if (ret)
  80. return ret;
  81. if (!request_region(p_dev->resource[0]->start,
  82. resource_size(p_dev->resource[0]),
  83. IPWIRELESS_PCCARD_NAME)) {
  84. ret = -EBUSY;
  85. goto exit;
  86. }
  87. p_dev->resource[2]->flags |=
  88. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  89. ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
  90. if (ret != 0)
  91. goto exit1;
  92. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2], p_dev->card_addr);
  93. if (ret != 0)
  94. goto exit1;
  95. ipw->is_v2_card = resource_size(p_dev->resource[2]) == 0x100;
  96. ipw->common_memory = ioremap(p_dev->resource[2]->start,
  97. resource_size(p_dev->resource[2]));
  98. if (!ipw->common_memory) {
  99. ret = -ENOMEM;
  100. goto exit1;
  101. }
  102. if (!request_mem_region(p_dev->resource[2]->start,
  103. resource_size(p_dev->resource[2]),
  104. IPWIRELESS_PCCARD_NAME)) {
  105. ret = -EBUSY;
  106. goto exit2;
  107. }
  108. p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
  109. WIN_ENABLE;
  110. p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
  111. ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
  112. if (ret != 0)
  113. goto exit3;
  114. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
  115. if (ret != 0)
  116. goto exit3;
  117. ipw->attr_memory = ioremap(p_dev->resource[3]->start,
  118. resource_size(p_dev->resource[3]));
  119. if (!ipw->attr_memory) {
  120. ret = -ENOMEM;
  121. goto exit3;
  122. }
  123. if (!request_mem_region(p_dev->resource[3]->start,
  124. resource_size(p_dev->resource[3]),
  125. IPWIRELESS_PCCARD_NAME)) {
  126. ret = -EBUSY;
  127. goto exit4;
  128. }
  129. return 0;
  130. exit4:
  131. iounmap(ipw->attr_memory);
  132. exit3:
  133. release_mem_region(p_dev->resource[2]->start,
  134. resource_size(p_dev->resource[2]));
  135. exit2:
  136. iounmap(ipw->common_memory);
  137. exit1:
  138. release_region(p_dev->resource[0]->start,
  139. resource_size(p_dev->resource[0]));
  140. exit:
  141. pcmcia_disable_device(p_dev);
  142. return ret;
  143. }
  144. static int config_ipwireless(struct ipw_dev *ipw)
  145. {
  146. struct pcmcia_device *link = ipw->link;
  147. int ret = 0;
  148. ipw->is_v2_card = 0;
  149. link->config_flags |= CONF_AUTO_SET_IO | CONF_AUTO_SET_IOMEM |
  150. CONF_ENABLE_IRQ;
  151. ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
  152. if (ret != 0)
  153. return ret;
  154. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  155. ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
  156. ipw->attr_memory, ipw->common_memory,
  157. ipw->is_v2_card, signalled_reboot_callback,
  158. ipw);
  159. ret = pcmcia_request_irq(link, ipwireless_interrupt);
  160. if (ret != 0)
  161. goto exit;
  162. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  163. ipw->is_v2_card ? "V2/V3" : "V1");
  164. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  165. ": I/O ports %pR, irq %d\n", link->resource[0],
  166. (unsigned int) link->irq);
  167. if (ipw->attr_memory && ipw->common_memory)
  168. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  169. ": attr memory %pR, common memory %pR\n",
  170. link->resource[3],
  171. link->resource[2]);
  172. ipw->network = ipwireless_network_create(ipw->hardware);
  173. if (!ipw->network)
  174. goto exit;
  175. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
  176. if (!ipw->tty)
  177. goto exit;
  178. ipwireless_init_hardware_v2_v3(ipw->hardware);
  179. /*
  180. * Do the RequestConfiguration last, because it enables interrupts.
  181. * Then we don't get any interrupts before we're ready for them.
  182. */
  183. ret = pcmcia_enable_device(link);
  184. if (ret != 0)
  185. goto exit;
  186. return 0;
  187. exit:
  188. if (ipw->common_memory) {
  189. release_mem_region(link->resource[2]->start,
  190. resource_size(link->resource[2]));
  191. iounmap(ipw->common_memory);
  192. }
  193. if (ipw->attr_memory) {
  194. release_mem_region(link->resource[3]->start,
  195. resource_size(link->resource[3]));
  196. iounmap(ipw->attr_memory);
  197. }
  198. pcmcia_disable_device(link);
  199. return -1;
  200. }
  201. static void release_ipwireless(struct ipw_dev *ipw)
  202. {
  203. release_region(ipw->link->resource[0]->start,
  204. resource_size(ipw->link->resource[0]));
  205. if (ipw->common_memory) {
  206. release_mem_region(ipw->link->resource[2]->start,
  207. resource_size(ipw->link->resource[2]));
  208. iounmap(ipw->common_memory);
  209. }
  210. if (ipw->attr_memory) {
  211. release_mem_region(ipw->link->resource[3]->start,
  212. resource_size(ipw->link->resource[3]));
  213. iounmap(ipw->attr_memory);
  214. }
  215. pcmcia_disable_device(ipw->link);
  216. }
  217. /*
  218. * ipwireless_attach() creates an "instance" of the driver, allocating
  219. * local data structures for one device (one interface). The device
  220. * is registered with Card Services.
  221. *
  222. * The pcmcia_device structure is initialized, but we don't actually
  223. * configure the card at this point -- we wait until we receive a
  224. * card insertion event.
  225. */
  226. static int ipwireless_attach(struct pcmcia_device *link)
  227. {
  228. struct ipw_dev *ipw;
  229. int ret;
  230. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  231. if (!ipw)
  232. return -ENOMEM;
  233. ipw->link = link;
  234. link->priv = ipw;
  235. ipw->hardware = ipwireless_hardware_create();
  236. if (!ipw->hardware) {
  237. kfree(ipw);
  238. return -ENOMEM;
  239. }
  240. /* RegisterClient will call config_ipwireless */
  241. ret = config_ipwireless(ipw);
  242. if (ret != 0) {
  243. ipwireless_detach(link);
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. /*
  249. * This deletes a driver "instance". The device is de-registered with
  250. * Card Services. If it has been released, all local data structures
  251. * are freed. Otherwise, the structures will be freed when the device
  252. * is released.
  253. */
  254. static void ipwireless_detach(struct pcmcia_device *link)
  255. {
  256. struct ipw_dev *ipw = link->priv;
  257. release_ipwireless(ipw);
  258. if (ipw->tty != NULL)
  259. ipwireless_tty_free(ipw->tty);
  260. if (ipw->network != NULL)
  261. ipwireless_network_free(ipw->network);
  262. if (ipw->hardware != NULL)
  263. ipwireless_hardware_free(ipw->hardware);
  264. kfree(ipw);
  265. }
  266. static struct pcmcia_driver me = {
  267. .owner = THIS_MODULE,
  268. .probe = ipwireless_attach,
  269. .remove = ipwireless_detach,
  270. .name = IPWIRELESS_PCCARD_NAME,
  271. .id_table = ipw_ids
  272. };
  273. /*
  274. * Module insertion : initialisation of the module.
  275. * Register the card with cardmgr...
  276. */
  277. static int __init init_ipwireless(void)
  278. {
  279. int ret;
  280. ret = ipwireless_tty_init();
  281. if (ret != 0)
  282. return ret;
  283. ret = pcmcia_register_driver(&me);
  284. if (ret != 0)
  285. ipwireless_tty_release();
  286. return ret;
  287. }
  288. /*
  289. * Module removal
  290. */
  291. static void __exit exit_ipwireless(void)
  292. {
  293. pcmcia_unregister_driver(&me);
  294. ipwireless_tty_release();
  295. }
  296. module_init(init_ipwireless);
  297. module_exit(exit_ipwireless);
  298. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  299. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  300. MODULE_LICENSE("GPL");