vx_entry.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 int vxpocket_event(event_t event, int priority, event_callback_args_t *args);
  35. static void vxpocket_release(dev_link_t *link)
  36. {
  37. if (link->state & DEV_CONFIG) {
  38. /* release cs resources */
  39. pcmcia_release_configuration(link->handle);
  40. pcmcia_release_io(link->handle, &link->io);
  41. pcmcia_release_irq(link->handle, &link->irq);
  42. link->state &= ~DEV_CONFIG;
  43. }
  44. }
  45. /*
  46. * destructor
  47. */
  48. static int snd_vxpocket_free(vx_core_t *chip)
  49. {
  50. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  51. struct snd_vxp_entry *hw;
  52. dev_link_t *link = &vxp->link;
  53. vxpocket_release(link);
  54. /* Break the link with Card Services */
  55. if (link->handle)
  56. pcmcia_deregister_client(link->handle);
  57. hw = vxp->hw_entry;
  58. if (hw)
  59. hw->card_list[vxp->index] = NULL;
  60. chip->card = NULL;
  61. kfree(chip->dev);
  62. snd_vx_free_firmware(chip);
  63. kfree(chip);
  64. return 0;
  65. }
  66. static int snd_vxpocket_dev_free(snd_device_t *device)
  67. {
  68. vx_core_t *chip = device->device_data;
  69. return snd_vxpocket_free(chip);
  70. }
  71. /*
  72. * snd_vxpocket_attach - attach callback for cs
  73. * @hw: the hardware information
  74. */
  75. dev_link_t *snd_vxpocket_attach(struct snd_vxp_entry *hw)
  76. {
  77. client_reg_t client_reg; /* Register with cardmgr */
  78. dev_link_t *link; /* Info for cardmgr */
  79. int i, ret;
  80. vx_core_t *chip;
  81. struct snd_vxpocket *vxp;
  82. snd_card_t *card;
  83. static snd_device_ops_t ops = {
  84. .dev_free = snd_vxpocket_dev_free,
  85. };
  86. snd_printdd(KERN_DEBUG "vxpocket_attach called\n");
  87. /* find an empty slot from the card list */
  88. for (i = 0; i < SNDRV_CARDS; i++) {
  89. if (! hw->card_list[i])
  90. break;
  91. }
  92. if (i >= SNDRV_CARDS) {
  93. snd_printk(KERN_ERR "vxpocket: too many cards found\n");
  94. return NULL;
  95. }
  96. if (! hw->enable_table[i])
  97. return NULL; /* disabled explicitly */
  98. /* ok, create a card instance */
  99. card = snd_card_new(hw->index_table[i], hw->id_table[i], THIS_MODULE, 0);
  100. if (card == NULL) {
  101. snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
  102. return NULL;
  103. }
  104. chip = snd_vx_create(card, hw->hardware, hw->ops,
  105. sizeof(struct snd_vxpocket) - sizeof(vx_core_t));
  106. if (! chip)
  107. return NULL;
  108. #ifdef SND_VX_FW_LOADER
  109. /* fake a device here since pcmcia doesn't give a valid device... */
  110. chip->dev = kcalloc(1, sizeof(*chip->dev), GFP_KERNEL);
  111. if (! chip->dev) {
  112. snd_printk(KERN_ERR "vxp: can't malloc chip->dev\n");
  113. kfree(chip);
  114. snd_card_free(card);
  115. return NULL;
  116. }
  117. device_initialize(chip->dev);
  118. sprintf(chip->dev->bus_id, "vxpocket%d", i);
  119. #endif
  120. if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops) < 0) {
  121. kfree(chip);
  122. snd_card_free(card);
  123. return NULL;
  124. }
  125. vxp = (struct snd_vxpocket *)chip;
  126. vxp->index = i;
  127. vxp->hw_entry = hw;
  128. chip->ibl.size = hw->ibl[i];
  129. hw->card_list[i] = chip;
  130. link = &vxp->link;
  131. link->priv = chip;
  132. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  133. link->io.NumPorts1 = 16;
  134. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  135. // link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
  136. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  137. link->irq.Handler = &snd_vx_irq_handler;
  138. link->irq.Instance = chip;
  139. link->conf.Attributes = CONF_ENABLE_IRQ;
  140. link->conf.Vcc = 50;
  141. link->conf.IntType = INT_MEMORY_AND_IO;
  142. link->conf.ConfigIndex = 1;
  143. link->conf.Present = PRESENT_OPTION;
  144. /* Register with Card Services */
  145. memset(&client_reg, 0, sizeof(client_reg));
  146. client_reg.dev_info = hw->dev_info;
  147. client_reg.EventMask =
  148. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
  149. #ifdef CONFIG_PM
  150. | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
  151. | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
  152. #endif
  153. ;
  154. client_reg.event_handler = &vxpocket_event;
  155. client_reg.Version = 0x0210;
  156. client_reg.event_callback_args.client_data = link;
  157. ret = pcmcia_register_client(&link->handle, &client_reg);
  158. if (ret != CS_SUCCESS) {
  159. cs_error(link->handle, RegisterClient, ret);
  160. snd_card_free(card);
  161. return NULL;
  162. }
  163. /* Chain drivers */
  164. link->next = hw->dev_list;
  165. hw->dev_list = link;
  166. /* snd_card_set_pm_callback(card, snd_vxpocket_suspend, snd_vxpocket_resume, chip); */
  167. return link;
  168. }
  169. /**
  170. * snd_vxpocket_assign_resources - initialize the hardware and card instance.
  171. * @port: i/o port for the card
  172. * @irq: irq number for the card
  173. *
  174. * this function assigns the specified port and irq, boot the card,
  175. * create pcm and control instances, and initialize the rest hardware.
  176. *
  177. * returns 0 if successful, or a negative error code.
  178. */
  179. static int snd_vxpocket_assign_resources(vx_core_t *chip, int port, int irq)
  180. {
  181. int err;
  182. snd_card_t *card = chip->card;
  183. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  184. snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
  185. vxp->port = port;
  186. sprintf(card->shortname, "Digigram %s", card->driver);
  187. sprintf(card->longname, "%s at 0x%x, irq %i",
  188. card->shortname, port, irq);
  189. chip->irq = irq;
  190. if ((err = snd_vx_setup_firmware(chip)) < 0)
  191. return err;
  192. return 0;
  193. }
  194. /*
  195. * snd_vxpocket_detach - detach callback for cs
  196. * @hw: the hardware information
  197. */
  198. void snd_vxpocket_detach(struct snd_vxp_entry *hw, dev_link_t *link)
  199. {
  200. vx_core_t *chip;
  201. if (! link)
  202. return;
  203. chip = link->priv;
  204. snd_printdd(KERN_DEBUG "vxpocket_detach called\n");
  205. /* Remove the interface data from the linked list */
  206. if (hw) {
  207. dev_link_t **linkp;
  208. /* Locate device structure */
  209. for (linkp = &hw->dev_list; *linkp; linkp = &(*linkp)->next)
  210. if (*linkp == link) {
  211. *linkp = link->next;
  212. break;
  213. }
  214. }
  215. chip->chip_status |= VX_STAT_IS_STALE; /* to be sure */
  216. snd_card_disconnect(chip->card);
  217. snd_card_free_in_thread(chip->card);
  218. }
  219. /*
  220. * configuration callback
  221. */
  222. #define CS_CHECK(fn, ret) \
  223. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  224. static void vxpocket_config(dev_link_t *link)
  225. {
  226. client_handle_t handle = link->handle;
  227. vx_core_t *chip = link->priv;
  228. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  229. tuple_t tuple;
  230. cisparse_t *parse = NULL;
  231. u_short buf[32];
  232. int last_fn, last_ret;
  233. snd_printdd(KERN_DEBUG "vxpocket_config called\n");
  234. parse = kmalloc(sizeof(*parse), GFP_KERNEL);
  235. if (! parse) {
  236. snd_printk(KERN_ERR "vx: cannot allocate\n");
  237. return;
  238. }
  239. tuple.Attributes = 0;
  240. tuple.TupleData = (cisdata_t *)buf;
  241. tuple.TupleDataMax = sizeof(buf);
  242. tuple.TupleOffset = 0;
  243. tuple.DesiredTuple = CISTPL_CONFIG;
  244. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  245. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  246. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  247. link->conf.ConfigBase = parse->config.base;
  248. link->conf.Present = parse->config.rmask[0];
  249. /* Configure card */
  250. link->state |= DEV_CONFIG;
  251. CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
  252. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  253. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  254. if (snd_vxpocket_assign_resources(chip, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
  255. goto failed;
  256. link->dev = &vxp->node;
  257. link->state &= ~DEV_CONFIG_PENDING;
  258. kfree(parse);
  259. return;
  260. cs_failed:
  261. cs_error(link->handle, last_fn, last_ret);
  262. failed:
  263. pcmcia_release_configuration(link->handle);
  264. pcmcia_release_io(link->handle, &link->io);
  265. pcmcia_release_irq(link->handle, &link->irq);
  266. link->state &= ~DEV_CONFIG;
  267. kfree(parse);
  268. }
  269. /*
  270. * event callback
  271. */
  272. static int vxpocket_event(event_t event, int priority, event_callback_args_t *args)
  273. {
  274. dev_link_t *link = args->client_data;
  275. vx_core_t *chip = link->priv;
  276. switch (event) {
  277. case CS_EVENT_CARD_REMOVAL:
  278. snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
  279. link->state &= ~DEV_PRESENT;
  280. if (link->state & DEV_CONFIG) {
  281. chip->chip_status |= VX_STAT_IS_STALE;
  282. }
  283. break;
  284. case CS_EVENT_CARD_INSERTION:
  285. snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
  286. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  287. vxpocket_config(link);
  288. break;
  289. #ifdef CONFIG_PM
  290. case CS_EVENT_PM_SUSPEND:
  291. snd_printdd(KERN_DEBUG "SUSPEND\n");
  292. link->state |= DEV_SUSPEND;
  293. if (chip && chip->card->pm_suspend) {
  294. snd_printdd(KERN_DEBUG "snd_vx_suspend calling\n");
  295. chip->card->pm_suspend(chip->card, PMSG_SUSPEND);
  296. }
  297. /* Fall through... */
  298. case CS_EVENT_RESET_PHYSICAL:
  299. snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
  300. if (link->state & DEV_CONFIG)
  301. pcmcia_release_configuration(link->handle);
  302. break;
  303. case CS_EVENT_PM_RESUME:
  304. snd_printdd(KERN_DEBUG "RESUME\n");
  305. link->state &= ~DEV_SUSPEND;
  306. /* Fall through... */
  307. case CS_EVENT_CARD_RESET:
  308. snd_printdd(KERN_DEBUG "CARD_RESET\n");
  309. if (DEV_OK(link)) {
  310. //struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  311. snd_printdd(KERN_DEBUG "requestconfig...\n");
  312. pcmcia_request_configuration(link->handle, &link->conf);
  313. if (chip && chip->card->pm_resume) {
  314. snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
  315. chip->card->pm_resume(chip->card);
  316. }
  317. }
  318. snd_printdd(KERN_DEBUG "resume done!\n");
  319. break;
  320. #endif
  321. }
  322. return 0;
  323. }
  324. /*
  325. * exported stuffs
  326. */
  327. EXPORT_SYMBOL(snd_vxpocket_ops);
  328. EXPORT_SYMBOL(snd_vxpocket_attach);
  329. EXPORT_SYMBOL(snd_vxpocket_detach);