pdaudiocf.c 10 KB

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