main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "hardware.h"
  18. #include "network.h"
  19. #include "main.h"
  20. #include "tty.h"
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/device_id.h>
  30. #include <pcmcia/ss.h>
  31. #include <pcmcia/ds.h>
  32. #include <pcmcia/cs.h>
  33. static 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. int ret = pcmcia_reset_card(link->socket);
  61. if (ret != 0)
  62. cs_error(link, ResetCard, ret);
  63. }
  64. static void signalled_reboot_callback(void *callback_data)
  65. {
  66. struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
  67. /* Delegate to process context. */
  68. schedule_work(&ipw->work_reboot);
  69. }
  70. static int ipwireless_ioprobe(struct pcmcia_device *p_dev,
  71. cistpl_cftable_entry_t *cfg,
  72. cistpl_cftable_entry_t *dflt,
  73. unsigned int vcc,
  74. void *priv_data)
  75. {
  76. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  77. p_dev->io.BasePort1 = cfg->io.win[0].base;
  78. p_dev->io.NumPorts1 = cfg->io.win[0].len;
  79. p_dev->io.IOAddrLines = 16;
  80. p_dev->irq.IRQInfo1 = cfg->irq.IRQInfo1;
  81. /* 0x40 causes it to generate level mode interrupts. */
  82. /* 0x04 enables IREQ pin. */
  83. p_dev->conf.ConfigIndex = cfg->index | 0x44;
  84. return pcmcia_request_io(p_dev, &p_dev->io);
  85. }
  86. static int config_ipwireless(struct ipw_dev *ipw)
  87. {
  88. struct pcmcia_device *link = ipw->link;
  89. int ret = 0;
  90. tuple_t tuple;
  91. unsigned short buf[64];
  92. cisparse_t parse;
  93. memreq_t memreq_attr_memory;
  94. memreq_t memreq_common_memory;
  95. ipw->is_v2_card = 0;
  96. tuple.Attributes = 0;
  97. tuple.TupleData = (cisdata_t *) buf;
  98. tuple.TupleDataMax = sizeof(buf);
  99. tuple.TupleOffset = 0;
  100. ret = pcmcia_loop_config(link, ipwireless_ioprobe, NULL);
  101. if (ret != 0) {
  102. cs_error(link, RequestIO, ret);
  103. goto exit0;
  104. }
  105. link->conf.Attributes = CONF_ENABLE_IRQ;
  106. link->conf.IntType = INT_MEMORY_AND_IO;
  107. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
  108. link->irq.Handler = ipwireless_interrupt;
  109. link->irq.Instance = ipw->hardware;
  110. request_region(link->io.BasePort1, link->io.NumPorts1,
  111. IPWIRELESS_PCCARD_NAME);
  112. /* memory settings */
  113. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  114. ret = pcmcia_get_first_tuple(link, &tuple);
  115. if (ret != 0) {
  116. cs_error(link, GetFirstTuple, ret);
  117. goto exit1;
  118. }
  119. ret = pcmcia_get_tuple_data(link, &tuple);
  120. if (ret != 0) {
  121. cs_error(link, GetTupleData, ret);
  122. goto exit1;
  123. }
  124. ret = pcmcia_parse_tuple(&tuple, &parse);
  125. if (ret != 0) {
  126. cs_error(link, ParseTuple, ret);
  127. goto exit1;
  128. }
  129. if (parse.cftable_entry.mem.nwin > 0) {
  130. ipw->request_common_memory.Attributes =
  131. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  132. ipw->request_common_memory.Base =
  133. parse.cftable_entry.mem.win[0].host_addr;
  134. ipw->request_common_memory.Size = parse.cftable_entry.mem.win[0].len;
  135. if (ipw->request_common_memory.Size < 0x1000)
  136. ipw->request_common_memory.Size = 0x1000;
  137. ipw->request_common_memory.AccessSpeed = 0;
  138. ret = pcmcia_request_window(&link, &ipw->request_common_memory,
  139. &ipw->handle_common_memory);
  140. if (ret != 0) {
  141. cs_error(link, RequestWindow, ret);
  142. goto exit1;
  143. }
  144. memreq_common_memory.CardOffset =
  145. parse.cftable_entry.mem.win[0].card_addr;
  146. memreq_common_memory.Page = 0;
  147. ret = pcmcia_map_mem_page(ipw->handle_common_memory,
  148. &memreq_common_memory);
  149. if (ret != 0) {
  150. cs_error(link, MapMemPage, ret);
  151. goto exit1;
  152. }
  153. ipw->is_v2_card =
  154. parse.cftable_entry.mem.win[0].len == 0x100;
  155. ipw->common_memory = ioremap(ipw->request_common_memory.Base,
  156. ipw->request_common_memory.Size);
  157. request_mem_region(ipw->request_common_memory.Base,
  158. ipw->request_common_memory.Size, IPWIRELESS_PCCARD_NAME);
  159. ipw->request_attr_memory.Attributes =
  160. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
  161. ipw->request_attr_memory.Base = 0;
  162. ipw->request_attr_memory.Size = 0; /* this used to be 0x1000 */
  163. ipw->request_attr_memory.AccessSpeed = 0;
  164. ret = pcmcia_request_window(&link, &ipw->request_attr_memory,
  165. &ipw->handle_attr_memory);
  166. if (ret != 0) {
  167. cs_error(link, RequestWindow, ret);
  168. goto exit2;
  169. }
  170. memreq_attr_memory.CardOffset = 0;
  171. memreq_attr_memory.Page = 0;
  172. ret = pcmcia_map_mem_page(ipw->handle_attr_memory,
  173. &memreq_attr_memory);
  174. if (ret != 0) {
  175. cs_error(link, MapMemPage, ret);
  176. goto exit2;
  177. }
  178. ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
  179. ipw->request_attr_memory.Size);
  180. request_mem_region(ipw->request_attr_memory.Base, ipw->request_attr_memory.Size,
  181. IPWIRELESS_PCCARD_NAME);
  182. }
  183. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  184. ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
  185. ipw->attr_memory, ipw->common_memory,
  186. ipw->is_v2_card, signalled_reboot_callback,
  187. ipw);
  188. ret = pcmcia_request_irq(link, &link->irq);
  189. if (ret != 0) {
  190. cs_error(link, RequestIRQ, ret);
  191. goto exit3;
  192. }
  193. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  194. ipw->is_v2_card ? "V2/V3" : "V1");
  195. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  196. ": I/O ports 0x%04x-0x%04x, irq %d\n",
  197. (unsigned int) link->io.BasePort1,
  198. (unsigned int) (link->io.BasePort1 +
  199. link->io.NumPorts1 - 1),
  200. (unsigned int) link->irq.AssignedIRQ);
  201. if (ipw->attr_memory && ipw->common_memory)
  202. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  203. ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
  204. ipw->request_attr_memory.Base,
  205. ipw->request_attr_memory.Base
  206. + ipw->request_attr_memory.Size - 1,
  207. ipw->request_common_memory.Base,
  208. ipw->request_common_memory.Base
  209. + ipw->request_common_memory.Size - 1);
  210. ipw->network = ipwireless_network_create(ipw->hardware);
  211. if (!ipw->network)
  212. goto exit3;
  213. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
  214. ipw->nodes);
  215. if (!ipw->tty)
  216. goto exit3;
  217. ipwireless_init_hardware_v2_v3(ipw->hardware);
  218. /*
  219. * Do the RequestConfiguration last, because it enables interrupts.
  220. * Then we don't get any interrupts before we're ready for them.
  221. */
  222. ret = pcmcia_request_configuration(link, &link->conf);
  223. if (ret != 0) {
  224. cs_error(link, RequestConfiguration, ret);
  225. goto exit4;
  226. }
  227. link->dev_node = &ipw->nodes[0];
  228. return 0;
  229. exit4:
  230. pcmcia_disable_device(link);
  231. exit3:
  232. if (ipw->attr_memory) {
  233. release_mem_region(ipw->request_attr_memory.Base,
  234. ipw->request_attr_memory.Size);
  235. iounmap(ipw->attr_memory);
  236. pcmcia_release_window(ipw->handle_attr_memory);
  237. pcmcia_disable_device(link);
  238. }
  239. exit2:
  240. if (ipw->common_memory) {
  241. release_mem_region(ipw->request_common_memory.Base,
  242. ipw->request_common_memory.Size);
  243. iounmap(ipw->common_memory);
  244. pcmcia_release_window(ipw->handle_common_memory);
  245. }
  246. exit1:
  247. pcmcia_disable_device(link);
  248. exit0:
  249. return -1;
  250. }
  251. static void release_ipwireless(struct ipw_dev *ipw)
  252. {
  253. pcmcia_disable_device(ipw->link);
  254. if (ipw->common_memory) {
  255. release_mem_region(ipw->request_common_memory.Base,
  256. ipw->request_common_memory.Size);
  257. iounmap(ipw->common_memory);
  258. }
  259. if (ipw->attr_memory) {
  260. release_mem_region(ipw->request_attr_memory.Base,
  261. ipw->request_attr_memory.Size);
  262. iounmap(ipw->attr_memory);
  263. }
  264. if (ipw->common_memory)
  265. pcmcia_release_window(ipw->handle_common_memory);
  266. if (ipw->attr_memory)
  267. pcmcia_release_window(ipw->handle_attr_memory);
  268. /* Break the link with Card Services */
  269. pcmcia_disable_device(ipw->link);
  270. }
  271. /*
  272. * ipwireless_attach() creates an "instance" of the driver, allocating
  273. * local data structures for one device (one interface). The device
  274. * is registered with Card Services.
  275. *
  276. * The pcmcia_device structure is initialized, but we don't actually
  277. * configure the card at this point -- we wait until we receive a
  278. * card insertion event.
  279. */
  280. static int ipwireless_attach(struct pcmcia_device *link)
  281. {
  282. struct ipw_dev *ipw;
  283. int ret;
  284. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  285. if (!ipw)
  286. return -ENOMEM;
  287. ipw->link = link;
  288. link->priv = ipw;
  289. link->irq.Instance = ipw;
  290. /* Link this device into our device list. */
  291. link->dev_node = &ipw->nodes[0];
  292. ipw->hardware = ipwireless_hardware_create();
  293. if (!ipw->hardware) {
  294. kfree(ipw);
  295. return -ENOMEM;
  296. }
  297. /* RegisterClient will call config_ipwireless */
  298. ret = config_ipwireless(ipw);
  299. if (ret != 0) {
  300. cs_error(link, RegisterClient, ret);
  301. ipwireless_detach(link);
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. /*
  307. * This deletes a driver "instance". The device is de-registered with
  308. * Card Services. If it has been released, all local data structures
  309. * are freed. Otherwise, the structures will be freed when the device
  310. * is released.
  311. */
  312. static void ipwireless_detach(struct pcmcia_device *link)
  313. {
  314. struct ipw_dev *ipw = link->priv;
  315. release_ipwireless(ipw);
  316. if (ipw->tty != NULL)
  317. ipwireless_tty_free(ipw->tty);
  318. if (ipw->network != NULL)
  319. ipwireless_network_free(ipw->network);
  320. if (ipw->hardware != NULL)
  321. ipwireless_hardware_free(ipw->hardware);
  322. kfree(ipw);
  323. }
  324. static struct pcmcia_driver me = {
  325. .owner = THIS_MODULE,
  326. .probe = ipwireless_attach,
  327. .remove = ipwireless_detach,
  328. .drv = { .name = IPWIRELESS_PCCARD_NAME },
  329. .id_table = ipw_ids
  330. };
  331. /*
  332. * Module insertion : initialisation of the module.
  333. * Register the card with cardmgr...
  334. */
  335. static int __init init_ipwireless(void)
  336. {
  337. int ret;
  338. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  339. IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
  340. ret = ipwireless_tty_init();
  341. if (ret != 0)
  342. return ret;
  343. ret = pcmcia_register_driver(&me);
  344. if (ret != 0)
  345. ipwireless_tty_release();
  346. return ret;
  347. }
  348. /*
  349. * Module removal
  350. */
  351. static void __exit exit_ipwireless(void)
  352. {
  353. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  354. IPWIRELESS_PCMCIA_VERSION " removed\n");
  355. pcmcia_unregister_driver(&me);
  356. ipwireless_tty_release();
  357. }
  358. module_init(init_ipwireless);
  359. module_exit(exit_ipwireless);
  360. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  361. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  362. MODULE_LICENSE("GPL");