pdaudiocf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.EventMask =
  147. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
  148. #ifdef CONFIG_PM
  149. | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
  150. | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
  151. #endif
  152. ;
  153. client_reg.event_handler = &pdacf_event;
  154. client_reg.Version = 0x0210;
  155. client_reg.event_callback_args.client_data = link;
  156. ret = pcmcia_register_client(&link->handle, &client_reg);
  157. if (ret != CS_SUCCESS) {
  158. cs_error(link->handle, RegisterClient, ret);
  159. snd_pdacf_detach(link);
  160. return NULL;
  161. }
  162. return link;
  163. }
  164. /**
  165. * snd_pdacf_assign_resources - initialize the hardware and card instance.
  166. * @port: i/o port for the card
  167. * @irq: irq number for the card
  168. *
  169. * this function assigns the specified port and irq, boot the card,
  170. * create pcm and control instances, and initialize the rest hardware.
  171. *
  172. * returns 0 if successful, or a negative error code.
  173. */
  174. static int snd_pdacf_assign_resources(pdacf_t *pdacf, int port, int irq)
  175. {
  176. int err;
  177. snd_card_t *card = pdacf->card;
  178. snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
  179. pdacf->port = port;
  180. pdacf->irq = irq;
  181. pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
  182. err = snd_pdacf_ak4117_create(pdacf);
  183. if (err < 0)
  184. return err;
  185. strcpy(card->driver, "PDAudio-CF");
  186. sprintf(card->shortname, "Core Sound %s", card->driver);
  187. sprintf(card->longname, "%s at 0x%x, irq %i",
  188. card->shortname, port, irq);
  189. err = snd_pdacf_pcm_new(pdacf);
  190. if (err < 0)
  191. return err;
  192. snd_card_set_pm_callback(card, snd_pdacf_suspend, snd_pdacf_resume, pdacf);
  193. if ((err = snd_card_register(card)) < 0)
  194. return err;
  195. return 0;
  196. }
  197. /*
  198. * snd_pdacf_detach - detach callback for cs
  199. */
  200. static void snd_pdacf_detach(dev_link_t *link)
  201. {
  202. pdacf_t *chip = link->priv;
  203. snd_printdd(KERN_DEBUG "pdacf_detach called\n");
  204. /* Remove the interface data from the linked list */
  205. {
  206. dev_link_t **linkp;
  207. /* Locate device structure */
  208. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  209. if (*linkp == link)
  210. break;
  211. if (*linkp)
  212. *linkp = link->next;
  213. }
  214. if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
  215. snd_pdacf_powerdown(chip);
  216. chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
  217. snd_card_disconnect(chip->card);
  218. snd_card_free_in_thread(chip->card);
  219. }
  220. /*
  221. * configuration callback
  222. */
  223. #define CS_CHECK(fn, ret) \
  224. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  225. static void pdacf_config(dev_link_t *link)
  226. {
  227. client_handle_t handle = link->handle;
  228. pdacf_t *pdacf = link->priv;
  229. tuple_t tuple;
  230. cisparse_t *parse = NULL;
  231. config_info_t conf;
  232. u_short buf[32];
  233. int last_fn, last_ret;
  234. snd_printdd(KERN_DEBUG "pdacf_config called\n");
  235. parse = kmalloc(sizeof(*parse), GFP_KERNEL);
  236. if (! parse) {
  237. snd_printk(KERN_ERR "pdacf_config: cannot allocate\n");
  238. return;
  239. }
  240. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  241. tuple.Attributes = 0;
  242. tuple.TupleData = (cisdata_t *)buf;
  243. tuple.TupleDataMax = sizeof(buf);
  244. tuple.TupleOffset = 0;
  245. tuple.DesiredTuple = CISTPL_CONFIG;
  246. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  247. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  248. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  249. link->conf.ConfigBase = parse->config.base;
  250. link->conf.ConfigIndex = 0x5;
  251. kfree(parse);
  252. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  253. link->conf.Vcc = conf.Vcc;
  254. /* Configure card */
  255. link->state |= DEV_CONFIG;
  256. CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
  257. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  258. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  259. if (snd_pdacf_assign_resources(pdacf, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
  260. goto failed;
  261. link->dev = &pdacf->node;
  262. link->state &= ~DEV_CONFIG_PENDING;
  263. return;
  264. cs_failed:
  265. cs_error(link->handle, last_fn, last_ret);
  266. failed:
  267. pcmcia_release_configuration(link->handle);
  268. pcmcia_release_io(link->handle, &link->io);
  269. pcmcia_release_irq(link->handle, &link->irq);
  270. }
  271. /*
  272. * event callback
  273. */
  274. static int pdacf_event(event_t event, int priority, event_callback_args_t *args)
  275. {
  276. dev_link_t *link = args->client_data;
  277. pdacf_t *chip = link->priv;
  278. switch (event) {
  279. case CS_EVENT_CARD_REMOVAL:
  280. snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
  281. link->state &= ~DEV_PRESENT;
  282. if (link->state & DEV_CONFIG) {
  283. chip->chip_status |= PDAUDIOCF_STAT_IS_STALE;
  284. }
  285. break;
  286. case CS_EVENT_CARD_INSERTION:
  287. snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
  288. link->state |= DEV_PRESENT;
  289. pdacf_config(link);
  290. break;
  291. #ifdef CONFIG_PM
  292. case CS_EVENT_PM_SUSPEND:
  293. snd_printdd(KERN_DEBUG "SUSPEND\n");
  294. link->state |= DEV_SUSPEND;
  295. if (chip) {
  296. snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
  297. snd_pdacf_suspend(chip->card, PMSG_SUSPEND);
  298. }
  299. /* Fall through... */
  300. case CS_EVENT_RESET_PHYSICAL:
  301. snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
  302. if (link->state & DEV_CONFIG)
  303. pcmcia_release_configuration(link->handle);
  304. break;
  305. case CS_EVENT_PM_RESUME:
  306. snd_printdd(KERN_DEBUG "RESUME\n");
  307. link->state &= ~DEV_SUSPEND;
  308. /* Fall through... */
  309. case CS_EVENT_CARD_RESET:
  310. snd_printdd(KERN_DEBUG "CARD_RESET\n");
  311. if (DEV_OK(link)) {
  312. snd_printdd(KERN_DEBUG "requestconfig...\n");
  313. pcmcia_request_configuration(link->handle, &link->conf);
  314. if (chip) {
  315. snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
  316. snd_pdacf_resume(chip->card);
  317. }
  318. }
  319. snd_printdd(KERN_DEBUG "resume done!\n");
  320. break;
  321. #endif
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Module entry points
  327. */
  328. static struct pcmcia_device_id snd_pdacf_ids[] = {
  329. PCMCIA_DEVICE_MANF_CARD(0x015d, 0x4c45),
  330. PCMCIA_DEVICE_NULL
  331. };
  332. MODULE_DEVICE_TABLE(pcmcia, snd_pdacf_ids);
  333. static struct pcmcia_driver pdacf_cs_driver = {
  334. .owner = THIS_MODULE,
  335. .drv = {
  336. .name = "snd-pdaudiocf",
  337. },
  338. .attach = snd_pdacf_attach,
  339. .detach = snd_pdacf_detach,
  340. .id_table = snd_pdacf_ids,
  341. };
  342. static int __init init_pdacf(void)
  343. {
  344. return pcmcia_register_driver(&pdacf_cs_driver);
  345. }
  346. static void __exit exit_pdacf(void)
  347. {
  348. pcmcia_unregister_driver(&pdacf_cs_driver);
  349. BUG_ON(dev_list != NULL);
  350. }
  351. module_init(init_pdacf);
  352. module_exit(exit_pdacf);