vxpocket.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Driver for Digigram VXpocket V2/440 soundcards
  3. *
  4. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  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 <linux/init.h>
  22. #include <linux/moduleparam.h>
  23. #include <sound/core.h>
  24. #include "vxpocket.h"
  25. #include <pcmcia/ciscode.h>
  26. #include <pcmcia/cisreg.h>
  27. #include <sound/initval.h>
  28. /*
  29. */
  30. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  31. MODULE_DESCRIPTION("Digigram VXPocket");
  32. MODULE_LICENSE("GPL");
  33. MODULE_SUPPORTED_DEVICE("{{Digigram,VXPocket},{Digigram,VXPocket440}}");
  34. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  35. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  36. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  37. static int ibl[SNDRV_CARDS];
  38. module_param_array(index, int, NULL, 0444);
  39. MODULE_PARM_DESC(index, "Index value for VXPocket soundcard.");
  40. module_param_array(id, charp, NULL, 0444);
  41. MODULE_PARM_DESC(id, "ID string for VXPocket soundcard.");
  42. module_param_array(enable, bool, NULL, 0444);
  43. MODULE_PARM_DESC(enable, "Enable VXPocket soundcard.");
  44. module_param_array(ibl, int, NULL, 0444);
  45. MODULE_PARM_DESC(ibl, "Capture IBL size for VXPocket soundcard.");
  46. /*
  47. */
  48. static unsigned int card_alloc;
  49. static dev_link_t *dev_list; /* Linked list of devices */
  50. static dev_info_t dev_info = "snd-vxpocket";
  51. static int vxpocket_event(event_t event, int priority, event_callback_args_t *args);
  52. /*
  53. */
  54. static void vxpocket_release(dev_link_t *link)
  55. {
  56. if (link->state & DEV_CONFIG) {
  57. /* release cs resources */
  58. pcmcia_release_configuration(link->handle);
  59. pcmcia_release_io(link->handle, &link->io);
  60. pcmcia_release_irq(link->handle, &link->irq);
  61. link->state &= ~DEV_CONFIG;
  62. }
  63. if (link->handle) {
  64. /* Break the link with Card Services */
  65. pcmcia_deregister_client(link->handle);
  66. link->handle = NULL;
  67. }
  68. }
  69. /*
  70. * destructor, called from snd_card_free_in_thread()
  71. */
  72. static int snd_vxpocket_dev_free(snd_device_t *device)
  73. {
  74. vx_core_t *chip = device->device_data;
  75. snd_vx_free_firmware(chip);
  76. kfree(chip);
  77. return 0;
  78. }
  79. /*
  80. * Hardware information
  81. */
  82. /* VX-pocket V2
  83. *
  84. * 1 DSP, 1 sync UER
  85. * 1 programmable clock (NIY)
  86. * 1 stereo analog input (line/micro)
  87. * 1 stereo analog output
  88. * Only output levels can be modified
  89. */
  90. static struct snd_vx_hardware vxpocket_hw = {
  91. .name = "VXPocket",
  92. .type = VX_TYPE_VXPOCKET,
  93. /* hardware specs */
  94. .num_codecs = 1,
  95. .num_ins = 1,
  96. .num_outs = 1,
  97. .output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
  98. };
  99. /* VX-pocket 440
  100. *
  101. * 1 DSP, 1 sync UER, 1 sync World Clock (NIY)
  102. * SMPTE (NIY)
  103. * 2 stereo analog input (line/micro)
  104. * 2 stereo analog output
  105. * Only output levels can be modified
  106. * UER, but only for the first two inputs and outputs.
  107. */
  108. static struct snd_vx_hardware vxp440_hw = {
  109. .name = "VXPocket440",
  110. .type = VX_TYPE_VXP440,
  111. /* hardware specs */
  112. .num_codecs = 2,
  113. .num_ins = 2,
  114. .num_outs = 2,
  115. .output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
  116. };
  117. /*
  118. * create vxpocket instance
  119. */
  120. static struct snd_vxpocket *snd_vxpocket_new(snd_card_t *card, int ibl)
  121. {
  122. client_reg_t client_reg; /* Register with cardmgr */
  123. dev_link_t *link; /* Info for cardmgr */
  124. vx_core_t *chip;
  125. struct snd_vxpocket *vxp;
  126. int ret;
  127. static snd_device_ops_t ops = {
  128. .dev_free = snd_vxpocket_dev_free,
  129. };
  130. chip = snd_vx_create(card, &vxpocket_hw, &snd_vxpocket_ops,
  131. sizeof(struct snd_vxpocket) - sizeof(vx_core_t));
  132. if (! chip)
  133. return NULL;
  134. if (snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops) < 0) {
  135. kfree(chip);
  136. return NULL;
  137. }
  138. chip->ibl.size = ibl;
  139. vxp = (struct snd_vxpocket *)chip;
  140. link = &vxp->link;
  141. link->priv = chip;
  142. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  143. link->io.NumPorts1 = 16;
  144. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  145. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  146. link->irq.Handler = &snd_vx_irq_handler;
  147. link->irq.Instance = chip;
  148. link->conf.Attributes = CONF_ENABLE_IRQ;
  149. link->conf.Vcc = 50;
  150. link->conf.IntType = INT_MEMORY_AND_IO;
  151. link->conf.ConfigIndex = 1;
  152. link->conf.Present = PRESENT_OPTION;
  153. /* Register with Card Services */
  154. memset(&client_reg, 0, sizeof(client_reg));
  155. client_reg.dev_info = &dev_info;
  156. client_reg.EventMask =
  157. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL
  158. #ifdef CONFIG_PM
  159. | CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET
  160. | CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME
  161. #endif
  162. ;
  163. client_reg.event_handler = &vxpocket_event;
  164. client_reg.Version = 0x0210;
  165. client_reg.event_callback_args.client_data = link;
  166. ret = pcmcia_register_client(&link->handle, &client_reg);
  167. if (ret != CS_SUCCESS) {
  168. cs_error(link->handle, RegisterClient, ret);
  169. return NULL;
  170. }
  171. return vxp;
  172. }
  173. /**
  174. * snd_vxpocket_assign_resources - initialize the hardware and card instance.
  175. * @port: i/o port for the card
  176. * @irq: irq number for the card
  177. *
  178. * this function assigns the specified port and irq, boot the card,
  179. * create pcm and control instances, and initialize the rest hardware.
  180. *
  181. * returns 0 if successful, or a negative error code.
  182. */
  183. static int snd_vxpocket_assign_resources(vx_core_t *chip, int port, int irq)
  184. {
  185. int err;
  186. snd_card_t *card = chip->card;
  187. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  188. snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
  189. vxp->port = port;
  190. sprintf(card->shortname, "Digigram %s", card->driver);
  191. sprintf(card->longname, "%s at 0x%x, irq %i",
  192. card->shortname, port, irq);
  193. chip->irq = irq;
  194. if ((err = snd_vx_setup_firmware(chip)) < 0)
  195. return err;
  196. return 0;
  197. }
  198. /*
  199. * configuration callback
  200. */
  201. #define CS_CHECK(fn, ret) \
  202. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  203. static void vxpocket_config(dev_link_t *link)
  204. {
  205. client_handle_t handle = link->handle;
  206. vx_core_t *chip = link->priv;
  207. struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  208. tuple_t tuple;
  209. cisparse_t *parse;
  210. u_short buf[32];
  211. int last_fn, last_ret;
  212. snd_printdd(KERN_DEBUG "vxpocket_config called\n");
  213. parse = kmalloc(sizeof(*parse), GFP_KERNEL);
  214. if (! parse) {
  215. snd_printk(KERN_ERR "vx: cannot allocate\n");
  216. return;
  217. }
  218. tuple.Attributes = 0;
  219. tuple.TupleData = (cisdata_t *)buf;
  220. tuple.TupleDataMax = sizeof(buf);
  221. tuple.TupleOffset = 0;
  222. tuple.DesiredTuple = CISTPL_CONFIG;
  223. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  224. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  225. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  226. link->conf.ConfigBase = parse->config.base;
  227. link->conf.Present = parse->config.rmask[0];
  228. /* redefine hardware record according to the VERSION1 string */
  229. tuple.DesiredTuple = CISTPL_VERS_1;
  230. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  231. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  232. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, parse));
  233. if (! strcmp(parse->version_1.str + parse->version_1.ofs[1], "VX-POCKET")) {
  234. snd_printdd("VX-pocket is detected\n");
  235. } else {
  236. snd_printdd("VX-pocket 440 is detected\n");
  237. /* overwrite the hardware information */
  238. chip->hw = &vxp440_hw;
  239. chip->type = vxp440_hw.type;
  240. strcpy(chip->card->driver, vxp440_hw.name);
  241. }
  242. /* Configure card */
  243. link->state |= DEV_CONFIG;
  244. CS_CHECK(RequestIO, pcmcia_request_io(handle, &link->io));
  245. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  246. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  247. chip->dev = &handle_to_dev(link->handle);
  248. snd_card_set_dev(chip->card, chip->dev);
  249. if (snd_vxpocket_assign_resources(chip, link->io.BasePort1, link->irq.AssignedIRQ) < 0)
  250. goto failed;
  251. link->dev = &vxp->node;
  252. link->state &= ~DEV_CONFIG_PENDING;
  253. kfree(parse);
  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. link->state &= ~DEV_CONFIG;
  262. kfree(parse);
  263. }
  264. /*
  265. * event callback
  266. */
  267. static int vxpocket_event(event_t event, int priority, event_callback_args_t *args)
  268. {
  269. dev_link_t *link = args->client_data;
  270. vx_core_t *chip = link->priv;
  271. switch (event) {
  272. case CS_EVENT_CARD_REMOVAL:
  273. snd_printdd(KERN_DEBUG "CARD_REMOVAL..\n");
  274. link->state &= ~DEV_PRESENT;
  275. if (link->state & DEV_CONFIG)
  276. chip->chip_status |= VX_STAT_IS_STALE;
  277. break;
  278. case CS_EVENT_CARD_INSERTION:
  279. snd_printdd(KERN_DEBUG "CARD_INSERTION..\n");
  280. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  281. vxpocket_config(link);
  282. break;
  283. #ifdef CONFIG_PM
  284. case CS_EVENT_PM_SUSPEND:
  285. snd_printdd(KERN_DEBUG "SUSPEND\n");
  286. link->state |= DEV_SUSPEND;
  287. if (chip && chip->card->pm_suspend) {
  288. snd_printdd(KERN_DEBUG "snd_vx_suspend calling\n");
  289. chip->card->pm_suspend(chip->card, PMSG_SUSPEND);
  290. }
  291. /* Fall through... */
  292. case CS_EVENT_RESET_PHYSICAL:
  293. snd_printdd(KERN_DEBUG "RESET_PHYSICAL\n");
  294. if (link->state & DEV_CONFIG)
  295. pcmcia_release_configuration(link->handle);
  296. break;
  297. case CS_EVENT_PM_RESUME:
  298. snd_printdd(KERN_DEBUG "RESUME\n");
  299. link->state &= ~DEV_SUSPEND;
  300. /* Fall through... */
  301. case CS_EVENT_CARD_RESET:
  302. snd_printdd(KERN_DEBUG "CARD_RESET\n");
  303. if (DEV_OK(link)) {
  304. //struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
  305. snd_printdd(KERN_DEBUG "requestconfig...\n");
  306. pcmcia_request_configuration(link->handle, &link->conf);
  307. if (chip && chip->card->pm_resume) {
  308. snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
  309. chip->card->pm_resume(chip->card);
  310. }
  311. }
  312. snd_printdd(KERN_DEBUG "resume done!\n");
  313. break;
  314. #endif
  315. }
  316. return 0;
  317. }
  318. /*
  319. */
  320. static dev_link_t *vxpocket_attach(void)
  321. {
  322. snd_card_t *card;
  323. struct snd_vxpocket *vxp;
  324. int i;
  325. /* find an empty slot from the card list */
  326. for (i = 0; i < SNDRV_CARDS; i++) {
  327. if (! card_alloc & (1 << i))
  328. break;
  329. }
  330. if (i >= SNDRV_CARDS) {
  331. snd_printk(KERN_ERR "vxpocket: too many cards found\n");
  332. return NULL;
  333. }
  334. if (! enable[i])
  335. return NULL; /* disabled explicitly */
  336. /* ok, create a card instance */
  337. card = snd_card_new(index[i], id[i], THIS_MODULE, 0);
  338. if (card == NULL) {
  339. snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
  340. return NULL;
  341. }
  342. vxp = snd_vxpocket_new(card, ibl[i]);
  343. if (! vxp) {
  344. snd_card_free(card);
  345. return NULL;
  346. }
  347. vxp->index = i;
  348. card_alloc |= 1 << i;
  349. /* Chain drivers */
  350. vxp->link.next = dev_list;
  351. dev_list = &vxp->link;
  352. return &vxp->link;
  353. }
  354. static void vxpocket_detach(dev_link_t *link)
  355. {
  356. struct snd_vxpocket *vxp;
  357. vx_core_t *chip;
  358. dev_link_t **linkp;
  359. if (! link)
  360. return;
  361. vxp = link->priv;
  362. chip = (vx_core_t *)vxp;
  363. card_alloc &= ~(1 << vxp->index);
  364. /* Remove the interface data from the linked list */
  365. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  366. if (*linkp == link) {
  367. *linkp = link->next;
  368. break;
  369. }
  370. chip->chip_status |= VX_STAT_IS_STALE; /* to be sure */
  371. snd_card_disconnect(chip->card);
  372. vxpocket_release(link);
  373. snd_card_free_in_thread(chip->card);
  374. }
  375. /*
  376. * Module entry points
  377. */
  378. static struct pcmcia_device_id vxp_ids[] = {
  379. PCMCIA_DEVICE_MANF_CARD(0x01f1, 0x0100),
  380. PCMCIA_DEVICE_NULL
  381. };
  382. MODULE_DEVICE_TABLE(pcmcia, vxp_ids);
  383. static struct pcmcia_driver vxp_cs_driver = {
  384. .owner = THIS_MODULE,
  385. .drv = {
  386. .name = "snd-vxpocket",
  387. },
  388. .attach = vxpocket_attach,
  389. .detach = vxpocket_detach,
  390. .event = vxpocket_event,
  391. .id_table = vxp_ids,
  392. };
  393. static int __init init_vxpocket(void)
  394. {
  395. return pcmcia_register_driver(&vxp_cs_driver);
  396. }
  397. static void __exit exit_vxpocket(void)
  398. {
  399. pcmcia_unregister_driver(&vxp_cs_driver);
  400. BUG_ON(dev_list != NULL);
  401. }
  402. module_init(init_vxpocket);
  403. module_exit(exit_vxpocket);