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