pdaudiocf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Driver for Sound Core PDAudioCF soundcard
  3. *
  4. * Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <sound/driver.h>
  21. #include <sound/core.h>
  22. #include <linux/slab.h>
  23. #include <linux/moduleparam.h>
  24. #include <pcmcia/version.h>
  25. #include <pcmcia/ciscode.h>
  26. #include <pcmcia/cisreg.h>
  27. #include "pdaudiocf.h"
  28. #include <sound/initval.h>
  29. #include <linux/init.h>
  30. /*
  31. */
  32. #define CARD_NAME "PDAudio-CF"
  33. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  34. MODULE_DESCRIPTION("Sound Core " CARD_NAME);
  35. MODULE_LICENSE("GPL");
  36. MODULE_SUPPORTED_DEVICE("{{Sound Core," CARD_NAME "}}");
  37. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  38. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  39. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  40. module_param_array(index, int, NULL, 0444);
  41. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  42. module_param_array(id, charp, NULL, 0444);
  43. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  44. module_param_array(enable, bool, NULL, 0444);
  45. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  46. /*
  47. */
  48. static dev_info_t dev_info = "snd-pdaudiocf";
  49. static snd_card_t *card_list[SNDRV_CARDS];
  50. static dev_link_t *dev_list;
  51. /*
  52. * prototypes
  53. */
  54. static void pdacf_config(dev_link_t *link);
  55. static int pdacf_event(event_t event, int priority, event_callback_args_t *args);
  56. static void snd_pdacf_detach(dev_link_t *link);
  57. static void pdacf_release(dev_link_t *link)
  58. {
  59. if (link->state & DEV_CONFIG) {
  60. /* release cs resources */
  61. pcmcia_release_configuration(link->handle);
  62. pcmcia_release_io(link->handle, &link->io);
  63. pcmcia_release_irq(link->handle, &link->irq);
  64. link->state &= ~DEV_CONFIG;
  65. }
  66. }
  67. /*
  68. * destructor
  69. */
  70. static int snd_pdacf_free(pdacf_t *pdacf)
  71. {
  72. dev_link_t *link = &pdacf->link;
  73. pdacf_release(link);
  74. /* Break the link with Card Services */
  75. if (link->handle)
  76. pcmcia_deregister_client(link->handle);
  77. card_list[pdacf->index] = NULL;
  78. pdacf->card = NULL;
  79. kfree(pdacf);
  80. return 0;
  81. }
  82. static int snd_pdacf_dev_free(snd_device_t *device)
  83. {
  84. pdacf_t *chip = device->device_data;
  85. return snd_pdacf_free(chip);
  86. }
  87. /*
  88. * snd_pdacf_attach - attach callback for cs
  89. */
  90. static dev_link_t *snd_pdacf_attach(void)
  91. {
  92. client_reg_t client_reg; /* Register with cardmgr */
  93. dev_link_t *link; /* Info for cardmgr */
  94. int i, ret;
  95. pdacf_t *pdacf;
  96. snd_card_t *card;
  97. static snd_device_ops_t ops = {
  98. .dev_free = snd_pdacf_dev_free,
  99. };
  100. snd_printdd(KERN_DEBUG "pdacf_attach called\n");
  101. /* find an empty slot from the card list */
  102. for (i = 0; i < SNDRV_CARDS; i++) {
  103. if (! card_list[i])
  104. break;
  105. }
  106. if (i >= SNDRV_CARDS) {
  107. snd_printk(KERN_ERR "pdacf: too many cards found\n");
  108. return NULL;
  109. }
  110. if (! enable[i])
  111. return NULL; /* disabled explicitly */
  112. /* ok, create a card instance */
  113. card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
  114. if (card == NULL) {
  115. snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
  116. return NULL;
  117. }
  118. pdacf = snd_pdacf_create(card);
  119. if (! pdacf)
  120. return NULL;
  121. if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops) < 0) {
  122. kfree(pdacf);
  123. snd_card_free(card);
  124. return NULL;
  125. }
  126. pdacf->index = i;
  127. card_list[i] = card;
  128. link = &pdacf->link;
  129. link->priv = pdacf;
  130. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  131. link->io.NumPorts1 = 16;
  132. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT | IRQ_FORCED_PULSE;
  133. // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  134. link->irq.IRQInfo1 = 0 /* | IRQ_LEVEL_ID */;
  135. link->irq.Handler = pdacf_interrupt;
  136. link->irq.Instance = pdacf;
  137. link->conf.Attributes = CONF_ENABLE_IRQ;
  138. link->conf.IntType = INT_MEMORY_AND_IO;
  139. link->conf.ConfigIndex = 1;
  140. link->conf.Present = PRESENT_OPTION;
  141. /* Chain drivers */
  142. link->next = dev_list;
  143. dev_list = link;
  144. /* Register with Card Services */
  145. client_reg.dev_info = &dev_info;
  146. client_reg.Version = 0x0210;
  147. client_reg.event_callback_args.client_data = link;
  148. ret = pcmcia_register_client(&link->handle, &client_reg);
  149. if (ret != CS_SUCCESS) {
  150. cs_error(link->handle, RegisterClient, ret);
  151. snd_pdacf_detach(link);
  152. return NULL;
  153. }
  154. return link;
  155. }
  156. /**
  157. * snd_pdacf_assign_resources - initialize the hardware and card instance.
  158. * @port: i/o port for the card
  159. * @irq: irq number for the card
  160. *
  161. * this function assigns the specified port and irq, boot the card,
  162. * create pcm and control instances, and initialize the rest hardware.
  163. *
  164. * returns 0 if successful, or a negative error code.
  165. */
  166. static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
  167. {
  168. int err;
  169. snd_card_t *card = pdacf->card;
  170. snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
  171. pdacf->port = port;
  172. pdacf->irq = irq;
  173. pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
  174. err = snd_pdacf_ak4117_create(pdacf);
  175. if (err < 0)
  176. return err;
  177. strcpy(card->driver, "PDAudio-CF");
  178. sprintf(card->shortname, "Core Sound %s", card->driver);
  179. sprintf(card->longname, "%s at 0x%x, irq %i",
  180. card->shortname, port, irq);
  181. err = snd_pdacf_pcm_new(pdacf);
  182. if (err < 0)
  183. return err;
  184. snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
  185. if ((err = snd_card_register(card)) < 0)
  186. return err;
  187. return 0;
  188. }
  189. /*
  190. * snd_pdacf_detach - detach callback for cs
  191. */
  192. static void snd_pdacf_detach(dev_link_t *link)
  193. {
  194. pdacf_t *chip = link->priv;
  195. snd_printdd(KERN_DEBUG "pdacf_detach called\n");
  196. /* Remove the interface data from the linked list */
  197. {
  198. dev_link_t **linkp;
  199. /* Locate device structure */
  200. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  201. if (*linkp == link)
  202. break;
  203. if (*linkp)
  204. *linkp = link->next;
  205. }
  206. if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
  207. snd_pdacf_powerdown(chip);
  208. chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
  209. snd_card_disconnect(chip->card);
  210. snd_card_free_in_thread(chip->card);
  211. }
  212. /*
  213. * configuration callback
  214. */
  215. #define CS_CHECK(fn, ret) \
  216. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  217. static void pdacf_config(dev_link_t *link)
  218. {
  219. client_handle_t handle = link->handle;
  220. pdacf_t *pdacf = link->priv;
  221. tuple_t tuple;
  222. cisparse_t *parse = NULL;
  223. config_info_t conf;
  224. u_short buf[32];
  225. int last_fn, last_ret;
  226. snd_printdd(KERN_DEBUG "pdacf_config called\n");
  227. parse = kmalloc(sizeof(*parse), GFP_KERNEL);
  228. if (! parse) {
  229. snd_printk(KERN_ERR "pdacf_config: cannot allocate\n");
  230. return;
  231. }
  232. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  233. tuple.Attributes = 0;
  234. tuple.TupleData = (cisdata_t *)buf;
  235. tuple.TupleDataMax = sizeof(buf);
  236. tuple.TupleOffset = 0;
  237. tuple.DesiredTuple = CISTPL_CONFIG;
  238. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  239. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  240. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  241. link->conf.ConfigBase = parse->config.base;
  242. link->conf.ConfigIndex = 0x5;
  243. kfree(parse);
  244. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  245. link->conf.Vcc = conf.Vcc;
  246. /* Configure card */
  247. link->state |= DEV_CONFIG;
  248. CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
  249. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  250. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  251. if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
  252. goto failed;
  253. link->dev = &pdacf->node;
  254. link->state &= ~DEV_CONFIG_PENDING;
  255. return;
  256. cs_failed:
  257. cs_error(link->handle, last_fn, last_ret);
  258. failed:
  259. pcmcia_release_configuration(link->handle);
  260. pcmcia_release_io(link->handle, &link->io);
  261. pcmcia_release_irq(link->handle, &link->irq);
  262. }
  263. /*
  264. * event callback
  265. */
  266. static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
  267. {
  268. dev_link_t *link = args->client_data;
  269. pdacf_t *chip = link->priv;
  270. switch (event) {
  271. case CS_EVENT_CARD_REMOVAL:
  272. snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
  273. link->state &= ~DEV_PRESENT;
  274. if (link->state & DEV_CONFIG) {
  275. chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
  276. }
  277. break;
  278. case CS_EVENT_CARD_INSERTION:
  279. snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
  280. link->state |= DEV_PRESENT;
  281. pdacf_config(link);
  282. break;
  283. #ifdef CONFIG_PM
  284. case CS_EVENT_PM_SUSPEND:
  285. snd_printdd(KERN_DEBUG "SUSPEND\n");
  286. link->state |= DEV_SUSPEND;
  287. if (chip) {
  288. snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
  289. snd_pdacf_suspend(chip->card, PMSG_SUSPEND);
  290. }
  291. /* Fall through... */
  292. case CS_EVENT_RESET_PHYSICAL:
  293. snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
  294. if (link->state & DEV_CONFIG)
  295. pcmcia_release_configuration(link->handle);
  296. break;
  297. case CS_EVENT_PM_RESUME:
  298. snd_printdd(KERN_DEBUG "RESUME\n");
  299. link->state &= ~DEV_SUSPEND;
  300. /* Fall through... */
  301. case CS_EVENT_CARD_RESET:
  302. snd_printdd(KERN_DEBUG "CARD_RESET\n");
  303. if (DEV_OK(link)) {
  304. snd_printdd(KERN_DEBUG "requestconfig...\n");
  305. pcmcia_request_configuration(link->handle, &link->conf);
  306. if (chip) {
  307. snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
  308. snd_pdacf_resume(chip->card);
  309. }
  310. }
  311. snd_printdd(KERN_DEBUG "resume done!\n");
  312. break;
  313. #endif
  314. }
  315. return 0;
  316. }
  317. /*
  318. * Module entry points
  319. */
  320. static struct pcmcia_device_id snd_pdacf_ids[] = {
  321. PCMCIA_DEVICE_MANF_CARD(0x015d, 0x4c45),
  322. PCMCIA_DEVICE_NULL
  323. };
  324. MODULE_DEVICE_TABLE(pcmcia, snd_pdacf_ids);
  325. static struct pcmcia_driver pdacf_cs_driver = {
  326. .owner = THIS_MODULE,
  327. .drv = {
  328. .name = "snd-pdaudiocf",
  329. },
  330. .attach = snd_pdacf_attach,
  331. .event = pdacf_event,
  332. .detach = snd_pdacf_detach,
  333. .id_table = snd_pdacf_ids,
  334. };
  335. static int __init init_pdacf(void)
  336. {
  337. return pcmcia_register_driver(&pdacf_cs_driver);
  338. }
  339. static void __exit exit_pdacf(void)
  340. {
  341. pcmcia_unregister_driver(&pdacf_cs_driver);
  342. BUG_ON(dev_list != NULL);
  343. }
  344. module_init(init_pdacf);
  345. module_exit(exit_pdacf);