vx_entry.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Driver for Digigram VXpocket soundcards
  3. *
  4. * PCMCIA entry part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/driver.h>
  23. #include <sound/core.h>
  24. #include "vxpocket.h"
  25. #include <pcmcia/ciscode.h>
  26. #include <pcmcia/cisreg.h>
  27. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  28. MODULE_DESCRIPTION("Common routines for Digigram PCMCIA VX drivers");
  29. MODULE_LICENSE("GPL");
  30. /*
  31. * prototypes
  32. */
  33. static void vxpocket_config(dev_link_t *link);
  34. static void vxpocket_release(dev_link_t *link)
  35. {
  36. if (link->state & DEV_CONFIG) {
  37. /* release cs resources */
  38. pcmcia_release_configuration(link->handle);
  39. pcmcia_release_io(link->handle, &link->io);
  40. pcmcia_release_irq(link->handle, &link->irq);
  41. link->state &= ~DEV_CONFIG;
  42. }
  43. }
  44. /*
  45. * destructor
  46. */
  47. static int snd_vxpocket_free(vx_core_t *chip)
  48. {
  49. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  50. struct snd_vxp_entry *hw;
  51. dev_link_t *link = &vxp->link;
  52. vxpocket_release(link);
  53. /* Break the link with Card Services */
  54. if (link->handle)
  55. pcmcia_deregister_client(link->handle);
  56. hw = vxp->hw_entry;
  57. if (hw)
  58. hw->card_list[vxp->index] = NULL;
  59. chip->card = NULL;
  60. kfree(chip->dev);
  61. snd_vx_free_firmware(chip);
  62. kfree(chip);
  63. return 0;
  64. }
  65. static int snd_vxpocket_dev_free(snd_device_t *device)
  66. {
  67. vx_core_t *chip = device->device_data;
  68. return snd_vxpocket_free(chip);
  69. }
  70. /*
  71. * snd_vxpocket_attach - attach callback for cs
  72. * @hw: the hardware information
  73. */
  74. dev_link_t *snd_vxpocket_attach(struct snd_vxp_entry *hw)
  75. {
  76. client_reg_t client_reg; /* Register with cardmgr */
  77. dev_link_t *link; /* Info for cardmgr */
  78. int i, ret;
  79. vx_core_t *chip;
  80. struct snd_vxpocket *vxp;
  81. snd_card_t *card;
  82. static snd_device_ops_t ops = {
  83. .dev_free = snd_vxpocket_dev_free,
  84. };
  85. snd_printdd(KERN_DEBUG "vxpocket_attach called\n");
  86. /* find an empty slot from the card list */
  87. for (i = 0; i < SNDRV_CARDS; i++) {
  88. if (! hw->card_list[i])
  89. break;
  90. }
  91. if (i >= SNDRV_CARDS) {
  92. snd_printk(KERN_ERR "vxpocket: too many cards found\n");
  93. return NULL;
  94. }
  95. if (! hw->enable_table[i])
  96. return NULL; /* disabled explicitly */
  97. /* ok, create a card instance */
  98. card = snd_card_new(hw->index_table[i], hw->id_table[i], THIS_MODULE, 0);
  99. if (card == NULL) {
  100. snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
  101. return NULL;
  102. }
  103. chip = snd_vx_create(card, hw->hardware, hw->ops,
  104. sizeof(struct snd_vxpocket) - sizeof(vx_core_t));
  105. if (! chip)
  106. return NULL;
  107. #ifdef SND_VX_FW_LOADER
  108. /* fake a device here since pcmcia doesn't give a valid device... */
  109. chip->dev = kcalloc(1, sizeof(*chip->dev), GFP_KERNEL);
  110. if (! chip->dev) {
  111. snd_printk(KERN_ERR "vxp: can't malloc chip->dev\n");
  112. kfree(chip);
  113. snd_card_free(card);
  114. return NULL;
  115. }
  116. device_initialize(chip->dev);
  117. sprintf(chip->dev->bus_id, "vxpocket%d", i);
  118. #endif
  119. if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops) < 0) {
  120. kfree(chip);
  121. snd_card_free(card);
  122. return NULL;
  123. }
  124. vxp = (struct snd_vxpocket *)chip;
  125. vxp->index = i;
  126. vxp->hw_entry = hw;
  127. chip->ibl.size = hw->ibl[i];
  128. hw->card_list[i] = chip;
  129. link = &vxp->link;
  130. link->priv = chip;
  131. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  132. link->io.NumPorts1 = 16;
  133. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  134. // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  135. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  136. link->irq.Handler = &snd_vx_irq_handler;
  137. link->irq.Instance = chip;
  138. link->conf.Attributes = CONF_ENABLE_IRQ;
  139. link->conf.Vcc = 50;
  140. link->conf.IntType = INT_MEMORY_AND_IO;
  141. link->conf.ConfigIndex = 1;
  142. link->conf.Present = PRESENT_OPTION;
  143. /* Register with Card Services */
  144. memset(&client_reg, 0, sizeof(client_reg));
  145. client_reg.dev_info = hw->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_card_free(card);
  152. return NULL;
  153. }
  154. /* Chain drivers */
  155. link->next = hw->dev_list;
  156. hw->dev_list = link;
  157. /* snd_card_set_pm_callback(card, snd_vxpocket_suspend, snd_vxpocket_resume, chip); */
  158. return link;
  159. }
  160. /**
  161. * snd_vxpocket_assign_resources - initialize the hardware and card instance.
  162. * @port: i/o port for the card
  163. * @irq: irq number for the card
  164. *
  165. * this function assigns the specified port and irq, boot the card,
  166. * create pcm and control instances, and initialize the rest hardware.
  167. *
  168. * returns 0 if successful, or a negative error code.
  169. */
  170. static int snd_vxpocket_assign_resources(vx_core_t *chip, int port, int irq)
  171. {
  172. int err;
  173. snd_card_t *card = chip->card;
  174. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  175. snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
  176. vxp->port = port;
  177. sprintf(card->shortname, "Digigram %s", card->driver);
  178. sprintf(card->longname, "%s at 0x%x, irq %i",
  179. card->shortname, port, irq);
  180. chip->irq = irq;
  181. if ((err = snd_vx_setup_firmware(chip)) < 0)
  182. return err;
  183. return 0;
  184. }
  185. /*
  186. * snd_vxpocket_detach - detach callback for cs
  187. * @hw: the hardware information
  188. */
  189. void snd_vxpocket_detach(struct snd_vxp_entry *hw, dev_link_t *link)
  190. {
  191. vx_core_t *chip;
  192. if (! link)
  193. return;
  194. chip = link->priv;
  195. snd_printdd(KERN_DEBUG "vxpocket_detach called\n");
  196. /* Remove the interface data from the linked list */
  197. if (hw) {
  198. dev_link_t **linkp;
  199. /* Locate device structure */
  200. for (linkp = &hw->dev_list; *linkp; linkp = &(*linkp)->next)
  201. if (*linkp == link) {
  202. *linkp = link->next;
  203. break;
  204. }
  205. }
  206. chip->chip_status |= VX_STAT_IS_STALE; /* to be sure */
  207. snd_card_disconnect(chip->card);
  208. snd_card_free_in_thread(chip->card);
  209. }
  210. /*
  211. * configuration callback
  212. */
  213. #define CS_CHECK(fn, ret) \
  214. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  215. static void vxpocket_config(dev_link_t *link)
  216. {
  217. client_handle_t handle = link->handle;
  218. vx_core_t *chip = link->priv;
  219. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  220. tuple_t tuple;
  221. cisparse_t *parse = NULL;
  222. u_short buf[32];
  223. int last_fn, last_ret;
  224. snd_printdd(KERN_DEBUG "vxpocket_config called\n");
  225. parse = kmalloc(sizeof(*parse), GFP_KERNEL);
  226. if (! parse) {
  227. snd_printk(KERN_ERR "vx: cannot allocate\n");
  228. return;
  229. }
  230. tuple.Attributes = 0;
  231. tuple.TupleData = (cisdata_t *)buf;
  232. tuple.TupleDataMax = sizeof(buf);
  233. tuple.TupleOffset = 0;
  234. tuple.DesiredTuple = CISTPL_CONFIG;
  235. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  236. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  237. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  238. link->conf.ConfigBase = parse->config.base;
  239. link->conf.Present = parse->config.rmask[0];
  240. /* Configure card */
  241. link->state |= DEV_CONFIG;
  242. CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
  243. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  244. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  245. if (snd_vxpocket_assign_resources(chip, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
  246. goto failed;
  247. link->dev = &vxp->node;
  248. link->state &= ~DEV_CONFIG_PENDING;
  249. kfree(parse);
  250. return;
  251. cs_failed:
  252. cs_error(link->handle, last_fn, last_ret);
  253. failed:
  254. pcmcia_release_configuration(link->handle);
  255. pcmcia_release_io(link->handle, &link->io);
  256. pcmcia_release_irq(link->handle, &link->irq);
  257. link->state &= ~DEV_CONFIG;
  258. kfree(parse);
  259. }
  260. /*
  261. * event callback
  262. */
  263. int vxpocket_event(event_t event, int priority, event_callback_args_t *args)
  264. {
  265. dev_link_t *link = args->client_data;
  266. vx_core_t *chip = link->priv;
  267. switch (event) {
  268. case CS_EVENT_CARD_REMOVAL:
  269. snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
  270. link->state &= ~DEV_PRESENT;
  271. if (link->state & DEV_CONFIG) {
  272. chip->chip_status |= VX_STAT_IS_STALE;
  273. }
  274. break;
  275. case CS_EVENT_CARD_INSERTION:
  276. snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
  277. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  278. vxpocket_config(link);
  279. break;
  280. #ifdef CONFIG_PM
  281. case CS_EVENT_PM_SUSPEND:
  282. snd_printdd(KERN_DEBUG "SUSPEND\n");
  283. link->state |= DEV_SUSPEND;
  284. if (chip && chip->card->pm_suspend) {
  285. snd_printdd(KERN_DEBUG "snd_vx_suspend calling\n");
  286. chip->card->pm_suspend(chip->card, PMSG_SUSPEND);
  287. }
  288. /* Fall through... */
  289. case CS_EVENT_RESET_PHYSICAL:
  290. snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
  291. if (link->state & DEV_CONFIG)
  292. pcmcia_release_configuration(link->handle);
  293. break;
  294. case CS_EVENT_PM_RESUME:
  295. snd_printdd(KERN_DEBUG "RESUME\n");
  296. link->state &= ~DEV_SUSPEND;
  297. /* Fall through... */
  298. case CS_EVENT_CARD_RESET:
  299. snd_printdd(KERN_DEBUG "CARD_RESET\n");
  300. if (DEV_OK(link)) {
  301. //struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  302. snd_printdd(KERN_DEBUG "requestconfig...\n");
  303. pcmcia_request_configuration(link->handle, &link->conf);
  304. if (chip && chip->card->pm_resume) {
  305. snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
  306. chip->card->pm_resume(chip->card);
  307. }
  308. }
  309. snd_printdd(KERN_DEBUG "resume done!\n");
  310. break;
  311. #endif
  312. }
  313. return 0;
  314. }
  315. /*
  316. * exported stuffs
  317. */
  318. EXPORT_SYMBOL(snd_vxpocket_ops);
  319. EXPORT_SYMBOL(snd_vxpocket_attach);
  320. EXPORT_SYMBOL(vxpocket_event);
  321. EXPORT_SYMBOL(snd_vxpocket_detach);