Linux Audio

Check our new training course

Loading...
v3.15
 
   1/*     
   2 *   ALSA Driver for Ego Systems Inc. (ESI) Miditerminal 4140
   3 *   Copyright (c) 2006 by Matthias König <mk@phasorlab.de>
   4 *
   5 *   This program is free software; you can redistribute it and/or modify 
   6 *   it under the terms of the GNU General Public License as published by 
   7 *   the Free Software Foundation; either version 2 of the License, or 
   8 *   (at your option) any later version. 
   9 *
  10 *   This program is distributed in the hope that it will be useful, 
  11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 *   GNU General Public License for more details.
  14 *
  15 *   You should have received a copy of the GNU General Public License
  16 *   along with this program; if not, write to the Free Software
  17 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18 *
  19 */
  20
  21#include <linux/init.h>
  22#include <linux/platform_device.h>
  23#include <linux/parport.h>
  24#include <linux/spinlock.h>
  25#include <linux/module.h>
  26#include <linux/delay.h>
  27#include <linux/slab.h>
  28#include <sound/core.h>
  29#include <sound/initval.h>
  30#include <sound/rawmidi.h>
  31#include <sound/control.h>
  32
  33#define CARD_NAME "Miditerminal 4140"
  34#define DRIVER_NAME "MTS64"
  35#define PLATFORM_DRIVER "snd_mts64"
  36
  37static int index[SNDRV_CARDS]  = SNDRV_DEFAULT_IDX;
  38static char *id[SNDRV_CARDS]   = SNDRV_DEFAULT_STR;
  39static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  40
  41static struct platform_device *platform_devices[SNDRV_CARDS]; 
  42static int device_count;
  43
  44module_param_array(index, int, NULL, S_IRUGO);
  45MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  46module_param_array(id, charp, NULL, S_IRUGO);
  47MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  48module_param_array(enable, bool, NULL, S_IRUGO);
  49MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  50
  51MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
  52MODULE_DESCRIPTION("ESI Miditerminal 4140");
  53MODULE_LICENSE("GPL");
  54MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
  55
  56/*********************************************************************
  57 * Chip specific
  58 *********************************************************************/
  59#define MTS64_NUM_INPUT_PORTS 5
  60#define MTS64_NUM_OUTPUT_PORTS 4
  61#define MTS64_SMPTE_SUBSTREAM 4
  62
  63struct mts64 {
  64	spinlock_t lock;
  65	struct snd_card *card;
  66	struct snd_rawmidi *rmidi;
  67	struct pardevice *pardev;
  68	int pardev_claimed;
  69
  70	int open_count;
  71	int current_midi_output_port;
  72	int current_midi_input_port;
  73	u8 mode[MTS64_NUM_INPUT_PORTS];
  74	struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
  75	int smpte_switch;
  76	u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
  77	u8 fps;
  78};
  79
  80static int snd_mts64_free(struct mts64 *mts)
  81{
  82	kfree(mts);
  83	return 0;
  84}
  85
  86static int snd_mts64_create(struct snd_card *card,
  87			    struct pardevice *pardev,
  88			    struct mts64 **rchip)
  89{
  90	struct mts64 *mts;
  91
  92	*rchip = NULL;
  93
  94	mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
  95	if (mts == NULL) 
  96		return -ENOMEM;
  97
  98	/* Init chip specific data */
  99	spin_lock_init(&mts->lock);
 100	mts->card = card;
 101	mts->pardev = pardev;
 102	mts->current_midi_output_port = -1;
 103	mts->current_midi_input_port = -1;
 104
 105	*rchip = mts;
 106
 107	return 0;
 108}
 109
 110/*********************************************************************
 111 * HW register related constants
 112 *********************************************************************/
 113
 114/* Status Bits */
 115#define MTS64_STAT_BSY             0x80
 116#define MTS64_STAT_BIT_SET         0x20  /* readout process, bit is set */
 117#define MTS64_STAT_PORT            0x10  /* read byte is a port number */
 118
 119/* Control Bits */
 120#define MTS64_CTL_READOUT          0x08  /* enable readout */
 121#define MTS64_CTL_WRITE_CMD        0x06  
 122#define MTS64_CTL_WRITE_DATA       0x02  
 123#define MTS64_CTL_STROBE           0x01  
 124
 125/* Command */
 126#define MTS64_CMD_RESET            0xfe
 127#define MTS64_CMD_PROBE            0x8f  /* Used in probing procedure */
 128#define MTS64_CMD_SMPTE_SET_TIME   0xe8
 129#define MTS64_CMD_SMPTE_SET_FPS    0xee
 130#define MTS64_CMD_SMPTE_STOP       0xef
 131#define MTS64_CMD_SMPTE_FPS_24     0xe3
 132#define MTS64_CMD_SMPTE_FPS_25     0xe2
 133#define MTS64_CMD_SMPTE_FPS_2997   0xe4 
 134#define MTS64_CMD_SMPTE_FPS_30D    0xe1
 135#define MTS64_CMD_SMPTE_FPS_30     0xe0
 136#define MTS64_CMD_COM_OPEN         0xf8  /* setting the communication mode */
 137#define MTS64_CMD_COM_CLOSE1       0xff  /* clearing communication mode */
 138#define MTS64_CMD_COM_CLOSE2       0xf5
 139
 140/*********************************************************************
 141 * Hardware specific functions
 142 *********************************************************************/
 143static void mts64_enable_readout(struct parport *p);
 144static void mts64_disable_readout(struct parport *p);
 145static int mts64_device_ready(struct parport *p);
 146static int mts64_device_init(struct parport *p);
 147static int mts64_device_open(struct mts64 *mts);
 148static int mts64_device_close(struct mts64 *mts);
 149static u8 mts64_map_midi_input(u8 c);
 150static int mts64_probe(struct parport *p);
 151static u16 mts64_read(struct parport *p);
 152static u8 mts64_read_char(struct parport *p);
 153static void mts64_smpte_start(struct parport *p,
 154			      u8 hours, u8 minutes,
 155			      u8 seconds, u8 frames,
 156			      u8 idx);
 157static void mts64_smpte_stop(struct parport *p);
 158static void mts64_write_command(struct parport *p, u8 c);
 159static void mts64_write_data(struct parport *p, u8 c);
 160static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
 161
 162
 163/*  Enables the readout procedure
 164 *
 165 *  Before we can read a midi byte from the device, we have to set
 166 *  bit 3 of control port.
 167 */
 168static void mts64_enable_readout(struct parport *p)
 169{
 170	u8 c;
 171
 172	c = parport_read_control(p);
 173	c |= MTS64_CTL_READOUT;
 174	parport_write_control(p, c); 
 175}
 176
 177/*  Disables readout 
 178 *
 179 *  Readout is disabled by clearing bit 3 of control
 180 */
 181static void mts64_disable_readout(struct parport *p)
 182{
 183	u8 c;
 184
 185	c = parport_read_control(p);
 186	c &= ~MTS64_CTL_READOUT;
 187	parport_write_control(p, c);
 188}
 189
 190/*  waits for device ready
 191 *
 192 *  Checks if BUSY (Bit 7 of status) is clear
 193 *  1 device ready
 194 *  0 failure
 195 */
 196static int mts64_device_ready(struct parport *p)
 197{
 198	int i;
 199	u8 c;
 200
 201	for (i = 0; i < 0xffff; ++i) {
 202		c = parport_read_status(p);
 203		c &= MTS64_STAT_BSY;
 204		if (c != 0) 
 205			return 1;
 206	} 
 207
 208	return 0;
 209}
 210
 211/*  Init device (LED blinking startup magic)
 212 *
 213 *  Returns:
 214 *  0 init ok
 215 *  -EIO failure
 216 */
 217static int mts64_device_init(struct parport *p)
 218{
 219	int i;
 220
 221	mts64_write_command(p, MTS64_CMD_RESET);
 222
 223	for (i = 0; i < 64; ++i) {
 224		msleep(100);
 225
 226		if (mts64_probe(p) == 0) {
 227			/* success */
 228			mts64_disable_readout(p);
 229			return 0;
 230		}
 231	}
 232	mts64_disable_readout(p);
 233
 234	return -EIO;
 235}
 236
 237/* 
 238 *  Opens the device (set communication mode)
 239 */
 240static int mts64_device_open(struct mts64 *mts)
 241{
 242	int i;
 243	struct parport *p = mts->pardev->port;
 244
 245	for (i = 0; i < 5; ++i)
 246		mts64_write_command(p, MTS64_CMD_COM_OPEN);
 247
 248	return 0;
 249}
 250
 251/*  
 252 *  Close device (clear communication mode)
 253 */
 254static int mts64_device_close(struct mts64 *mts)
 255{
 256	int i;
 257	struct parport *p = mts->pardev->port;
 258
 259	for (i = 0; i < 5; ++i) {
 260		mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
 261		mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
 262	}
 263
 264	return 0;
 265}
 266
 267/*  map hardware port to substream number
 268 * 
 269 *  When reading a byte from the device, the device tells us
 270 *  on what port the byte is. This HW port has to be mapped to
 271 *  the midiport (substream number).
 272 *  substream 0-3 are Midiports 1-4
 273 *  substream 4 is SMPTE Timecode
 274 *  The mapping is done by the table:
 275 *  HW | 0 | 1 | 2 | 3 | 4 
 276 *  SW | 0 | 1 | 4 | 2 | 3
 277 */
 278static u8 mts64_map_midi_input(u8 c)
 279{
 280	static u8 map[] = { 0, 1, 4, 2, 3 };
 281
 282	return map[c];
 283}
 284
 285
 286/*  Probe parport for device
 287 *
 288 *  Do we have a Miditerminal 4140 on parport? 
 289 *  Returns:
 290 *  0       device found
 291 *  -ENODEV no device
 292 */
 293static int mts64_probe(struct parport *p)
 294{
 295	u8 c;
 296
 297	mts64_smpte_stop(p);
 298	mts64_write_command(p, MTS64_CMD_PROBE);
 299
 300	msleep(50);
 301	
 302	c = mts64_read(p);
 303
 304	c &= 0x00ff;
 305	if (c != MTS64_CMD_PROBE) 
 306		return -ENODEV;
 307	else 
 308		return 0;
 309
 310}
 311
 312/*  Read byte incl. status from device
 313 *
 314 *  Returns:
 315 *  data in lower 8 bits and status in upper 8 bits
 316 */
 317static u16 mts64_read(struct parport *p)
 318{
 319	u8 data, status;
 320
 321	mts64_device_ready(p);
 322	mts64_enable_readout(p);
 323	status = parport_read_status(p);
 324	data = mts64_read_char(p);
 325	mts64_disable_readout(p);
 326
 327	return (status << 8) | data;
 328}
 329
 330/*  Read a byte from device
 331 *
 332 *  Note, that readout mode has to be enabled.
 333 *  readout procedure is as follows: 
 334 *  - Write number of the Bit to read to DATA
 335 *  - Read STATUS
 336 *  - Bit 5 of STATUS indicates if Bit is set
 337 *
 338 *  Returns:
 339 *  Byte read from device
 340 */
 341static u8 mts64_read_char(struct parport *p)
 342{
 343	u8 c = 0;
 344	u8 status;
 345	u8 i;
 346
 347	for (i = 0; i < 8; ++i) {
 348		parport_write_data(p, i);
 349		c >>= 1;
 350		status = parport_read_status(p);
 351		if (status & MTS64_STAT_BIT_SET) 
 352			c |= 0x80;
 353	}
 354	
 355	return c;
 356}
 357
 358/*  Starts SMPTE Timecode generation
 359 *
 360 *  The device creates SMPTE Timecode by hardware.
 361 *  0 24 fps
 362 *  1 25 fps
 363 *  2 29.97 fps
 364 *  3 30 fps (Drop-frame)
 365 *  4 30 fps
 366 */
 367static void mts64_smpte_start(struct parport *p,
 368			      u8 hours, u8 minutes,
 369			      u8 seconds, u8 frames,
 370			      u8 idx)
 371{
 372	static u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24, 
 373			     MTS64_CMD_SMPTE_FPS_25,
 374			     MTS64_CMD_SMPTE_FPS_2997, 
 375			     MTS64_CMD_SMPTE_FPS_30D,
 376			     MTS64_CMD_SMPTE_FPS_30    };
 377
 378	mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
 379	mts64_write_command(p, frames);
 380	mts64_write_command(p, seconds);
 381	mts64_write_command(p, minutes);
 382	mts64_write_command(p, hours);
 383
 384	mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
 385	mts64_write_command(p, fps[idx]);
 386}
 387
 388/*  Stops SMPTE Timecode generation
 389 */
 390static void mts64_smpte_stop(struct parport *p)
 391{
 392	mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
 393}
 394
 395/*  Write a command byte to device
 396 */
 397static void mts64_write_command(struct parport *p, u8 c)
 398{
 399	mts64_device_ready(p);
 400
 401	parport_write_data(p, c);
 402
 403	parport_write_control(p, MTS64_CTL_WRITE_CMD);
 404	parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
 405	parport_write_control(p, MTS64_CTL_WRITE_CMD);
 406}
 407
 408/*  Write a data byte to device 
 409 */
 410static void mts64_write_data(struct parport *p, u8 c)
 411{
 412	mts64_device_ready(p);
 413
 414	parport_write_data(p, c);
 415
 416	parport_write_control(p, MTS64_CTL_WRITE_DATA);
 417	parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
 418	parport_write_control(p, MTS64_CTL_WRITE_DATA);
 419}
 420
 421/*  Write a MIDI byte to midiport
 422 *
 423 *  midiport ranges from 0-3 and maps to Ports 1-4
 424 *  assumptions: communication mode is on
 425 */
 426static void mts64_write_midi(struct mts64 *mts, u8 c,
 427			     int midiport)
 428{
 429	struct parport *p = mts->pardev->port;
 430
 431	/* check current midiport */
 432	if (mts->current_midi_output_port != midiport)
 433		mts64_write_command(p, midiport);
 434
 435	/* write midi byte */
 436	mts64_write_data(p, c);
 437}
 438
 439/*********************************************************************
 440 * Control elements
 441 *********************************************************************/
 442
 443/* SMPTE Switch */
 444#define snd_mts64_ctl_smpte_switch_info		snd_ctl_boolean_mono_info
 445
 446static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
 447					  struct snd_ctl_elem_value *uctl)
 448{
 449	struct mts64 *mts = snd_kcontrol_chip(kctl);
 450
 451	spin_lock_irq(&mts->lock);
 452	uctl->value.integer.value[0] = mts->smpte_switch;
 453	spin_unlock_irq(&mts->lock);
 454
 455	return 0;
 456}
 457
 458/* smpte_switch is not accessed from IRQ handler, so we just need
 459   to protect the HW access */
 460static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
 461					  struct snd_ctl_elem_value *uctl)
 462{
 463	struct mts64 *mts = snd_kcontrol_chip(kctl);
 464	int changed = 0;
 465	int val = !!uctl->value.integer.value[0];
 466
 467	spin_lock_irq(&mts->lock);
 468	if (mts->smpte_switch == val)
 469		goto __out;
 470
 471	changed = 1;
 472	mts->smpte_switch = val;
 473	if (mts->smpte_switch) {
 474		mts64_smpte_start(mts->pardev->port,
 475				  mts->time[0], mts->time[1],
 476				  mts->time[2], mts->time[3],
 477				  mts->fps);
 478	} else {
 479		mts64_smpte_stop(mts->pardev->port);
 480	}
 481__out:
 482	spin_unlock_irq(&mts->lock);
 483	return changed;
 484}
 485
 486static struct snd_kcontrol_new mts64_ctl_smpte_switch = {
 487	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 488	.name  = "SMPTE Playback Switch",
 489	.index = 0,
 490	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 491	.private_value = 0,
 492	.info = snd_mts64_ctl_smpte_switch_info,
 493	.get  = snd_mts64_ctl_smpte_switch_get,
 494	.put  = snd_mts64_ctl_smpte_switch_put
 495};
 496
 497/* Time */
 498static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
 499					   struct snd_ctl_elem_info *uinfo)
 500{
 501	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 502	uinfo->count = 1;
 503	uinfo->value.integer.min = 0;
 504	uinfo->value.integer.max = 23;
 505	return 0;
 506}
 507
 508static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
 509					   struct snd_ctl_elem_info *uinfo)
 510{
 511	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 512	uinfo->count = 1;
 513	uinfo->value.integer.min = 0;
 514	uinfo->value.integer.max = 99;
 515	return 0;
 516}
 517
 518static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
 519					 struct snd_ctl_elem_info *uinfo)
 520{
 521	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 522	uinfo->count = 1;
 523	uinfo->value.integer.min = 0;
 524	uinfo->value.integer.max = 59;
 525	return 0;
 526}
 527
 528static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
 529					struct snd_ctl_elem_value *uctl)
 530{
 531	struct mts64 *mts = snd_kcontrol_chip(kctl);
 532	int idx = kctl->private_value;
 533
 534	spin_lock_irq(&mts->lock);
 535	uctl->value.integer.value[0] = mts->time[idx];
 536	spin_unlock_irq(&mts->lock);
 537
 538	return 0;
 539}
 540
 541static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
 542					struct snd_ctl_elem_value *uctl)
 543{
 544	struct mts64 *mts = snd_kcontrol_chip(kctl);
 545	int idx = kctl->private_value;
 546	unsigned int time = uctl->value.integer.value[0] % 60;
 547	int changed = 0;
 548
 549	spin_lock_irq(&mts->lock);
 550	if (mts->time[idx] != time) {
 551		changed = 1;
 552		mts->time[idx] = time;
 553	}
 554	spin_unlock_irq(&mts->lock);
 555
 556	return changed;
 557}
 558
 559static struct snd_kcontrol_new mts64_ctl_smpte_time_hours = {
 560	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 561	.name  = "SMPTE Time Hours",
 562	.index = 0,
 563	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 564	.private_value = 0,
 565	.info = snd_mts64_ctl_smpte_time_h_info,
 566	.get  = snd_mts64_ctl_smpte_time_get,
 567	.put  = snd_mts64_ctl_smpte_time_put
 568};
 569
 570static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes = {
 571	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 572	.name  = "SMPTE Time Minutes",
 573	.index = 0,
 574	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 575	.private_value = 1,
 576	.info = snd_mts64_ctl_smpte_time_info,
 577	.get  = snd_mts64_ctl_smpte_time_get,
 578	.put  = snd_mts64_ctl_smpte_time_put
 579};
 580
 581static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds = {
 582	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 583	.name  = "SMPTE Time Seconds",
 584	.index = 0,
 585	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 586	.private_value = 2,
 587	.info = snd_mts64_ctl_smpte_time_info,
 588	.get  = snd_mts64_ctl_smpte_time_get,
 589	.put  = snd_mts64_ctl_smpte_time_put
 590};
 591
 592static struct snd_kcontrol_new mts64_ctl_smpte_time_frames = {
 593	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 594	.name  = "SMPTE Time Frames",
 595	.index = 0,
 596	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 597	.private_value = 3,
 598	.info = snd_mts64_ctl_smpte_time_f_info,
 599	.get  = snd_mts64_ctl_smpte_time_get,
 600	.put  = snd_mts64_ctl_smpte_time_put
 601};
 602
 603/* FPS */
 604static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
 605					struct snd_ctl_elem_info *uinfo)
 606{
 607	static char *texts[5] = { "24",
 608				  "25",
 609				  "29.97",
 610				  "30D",
 611				  "30"    };
 612
 613	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 614	uinfo->count = 1;
 615	uinfo->value.enumerated.items = 5;
 616	if (uinfo->value.enumerated.item > 4)
 617		uinfo->value.enumerated.item = 4;
 618	strcpy(uinfo->value.enumerated.name,
 619	       texts[uinfo->value.enumerated.item]);
 620	
 621	return 0;
 622}
 623
 624static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
 625				       struct snd_ctl_elem_value *uctl)
 626{
 627	struct mts64 *mts = snd_kcontrol_chip(kctl);
 628
 629	spin_lock_irq(&mts->lock);
 630	uctl->value.enumerated.item[0] = mts->fps;
 631	spin_unlock_irq(&mts->lock);
 632
 633	return 0;
 634}
 635
 636static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
 637				       struct snd_ctl_elem_value *uctl)
 638{
 639	struct mts64 *mts = snd_kcontrol_chip(kctl);
 640	int changed = 0;
 641
 642	if (uctl->value.enumerated.item[0] >= 5)
 643		return -EINVAL;
 644	spin_lock_irq(&mts->lock);
 645	if (mts->fps != uctl->value.enumerated.item[0]) {
 646		changed = 1;
 647		mts->fps = uctl->value.enumerated.item[0];
 648	}
 649	spin_unlock_irq(&mts->lock);
 650
 651	return changed;
 652}
 653
 654static struct snd_kcontrol_new mts64_ctl_smpte_fps = {
 655	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 656	.name  = "SMPTE Fps",
 657	.index = 0,
 658	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 659	.private_value = 0,
 660	.info  = snd_mts64_ctl_smpte_fps_info,
 661	.get   = snd_mts64_ctl_smpte_fps_get,
 662	.put   = snd_mts64_ctl_smpte_fps_put
 663};
 664
 665
 666static int snd_mts64_ctl_create(struct snd_card *card,
 667				struct mts64 *mts)
 668{
 669	int err, i;
 670	static struct snd_kcontrol_new *control[] = {
 671		&mts64_ctl_smpte_switch,
 672		&mts64_ctl_smpte_time_hours,
 673		&mts64_ctl_smpte_time_minutes,
 674		&mts64_ctl_smpte_time_seconds,
 675		&mts64_ctl_smpte_time_frames,
 676		&mts64_ctl_smpte_fps,
 677	        NULL  };
 678
 679	for (i = 0; control[i]; ++i) {
 680		err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
 681		if (err < 0) {
 682			snd_printd("Cannot create control: %s\n", 
 683				   control[i]->name);
 684			return err;
 685		}
 686	}
 687
 688	return 0;
 689}
 690
 691/*********************************************************************
 692 * Rawmidi
 693 *********************************************************************/
 694#define MTS64_MODE_INPUT_TRIGGERED 0x01
 695
 696static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
 697{
 698	struct mts64 *mts = substream->rmidi->private_data;
 699
 700	if (mts->open_count == 0) {
 701		/* We don't need a spinlock here, because this is just called 
 702		   if the device has not been opened before. 
 703		   So there aren't any IRQs from the device */
 704		mts64_device_open(mts);
 705
 706		msleep(50);
 707	}
 708	++(mts->open_count);
 709
 710	return 0;
 711}
 712
 713static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
 714{
 715	struct mts64 *mts = substream->rmidi->private_data;
 716	unsigned long flags;
 717
 718	--(mts->open_count);
 719	if (mts->open_count == 0) {
 720		/* We need the spinlock_irqsave here because we can still
 721		   have IRQs at this point */
 722		spin_lock_irqsave(&mts->lock, flags);
 723		mts64_device_close(mts);
 724		spin_unlock_irqrestore(&mts->lock, flags);
 725
 726		msleep(500);
 727
 728	} else if (mts->open_count < 0)
 729		mts->open_count = 0;
 730
 731	return 0;
 732}
 733
 734static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
 735					     int up)
 736{
 737	struct mts64 *mts = substream->rmidi->private_data;
 738	u8 data;
 739	unsigned long flags;
 740
 741	spin_lock_irqsave(&mts->lock, flags);
 742	while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
 743		mts64_write_midi(mts, data, substream->number+1);
 744		snd_rawmidi_transmit_ack(substream, 1);
 745	}
 746	spin_unlock_irqrestore(&mts->lock, flags);
 747}
 748
 749static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
 750					    int up)
 751{
 752	struct mts64 *mts = substream->rmidi->private_data;
 753	unsigned long flags;
 754
 755	spin_lock_irqsave(&mts->lock, flags);
 756	if (up)
 757		mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
 758	else
 759 		mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
 760	
 761	spin_unlock_irqrestore(&mts->lock, flags);
 762}
 763
 764static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
 765	.open    = snd_mts64_rawmidi_open,
 766	.close   = snd_mts64_rawmidi_close,
 767	.trigger = snd_mts64_rawmidi_output_trigger
 768};
 769
 770static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
 771	.open    = snd_mts64_rawmidi_open,
 772	.close   = snd_mts64_rawmidi_close,
 773	.trigger = snd_mts64_rawmidi_input_trigger
 774};
 775
 776/* Create and initialize the rawmidi component */
 777static int snd_mts64_rawmidi_create(struct snd_card *card)
 778{
 779	struct mts64 *mts = card->private_data;
 780	struct snd_rawmidi *rmidi;
 781	struct snd_rawmidi_substream *substream;
 782	struct list_head *list;
 783	int err;
 784	
 785	err = snd_rawmidi_new(card, CARD_NAME, 0, 
 786			      MTS64_NUM_OUTPUT_PORTS, 
 787			      MTS64_NUM_INPUT_PORTS, 
 788			      &rmidi);
 789	if (err < 0) 
 790		return err;
 791
 792	rmidi->private_data = mts;
 793	strcpy(rmidi->name, CARD_NAME);
 794	rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
 795		            SNDRV_RAWMIDI_INFO_INPUT |
 796                            SNDRV_RAWMIDI_INFO_DUPLEX;
 797
 798	mts->rmidi = rmidi;
 799
 800	/* register rawmidi ops */
 801	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 
 802			    &snd_mts64_rawmidi_output_ops);
 803	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 
 804			    &snd_mts64_rawmidi_input_ops);
 805
 806	/* name substreams */
 807	/* output */
 808	list_for_each(list, 
 809		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
 810		substream = list_entry(list, struct snd_rawmidi_substream, list);
 811		sprintf(substream->name,
 812			"Miditerminal %d", substream->number+1);
 813	}
 814	/* input */
 815	list_for_each(list, 
 816		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
 817		substream = list_entry(list, struct snd_rawmidi_substream, list);
 818		mts->midi_input_substream[substream->number] = substream;
 819		switch(substream->number) {
 820		case MTS64_SMPTE_SUBSTREAM:
 821			strcpy(substream->name, "Miditerminal SMPTE");
 822			break;
 823		default:
 824			sprintf(substream->name,
 825				"Miditerminal %d", substream->number+1);
 826		}
 827	}
 828
 829	/* controls */
 830	err = snd_mts64_ctl_create(card, mts);
 831
 832	return err;
 833}
 834
 835/*********************************************************************
 836 * parport stuff
 837 *********************************************************************/
 838static void snd_mts64_interrupt(void *private)
 839{
 840	struct mts64 *mts = ((struct snd_card*)private)->private_data;
 841	u16 ret;
 842	u8 status, data;
 843	struct snd_rawmidi_substream *substream;
 844
 845	spin_lock(&mts->lock);
 846	ret = mts64_read(mts->pardev->port);
 847	data = ret & 0x00ff;
 848	status = ret >> 8;
 849
 850	if (status & MTS64_STAT_PORT) {
 851		mts->current_midi_input_port = mts64_map_midi_input(data);
 852	} else {
 853		if (mts->current_midi_input_port == -1) 
 854			goto __out;
 855		substream = mts->midi_input_substream[mts->current_midi_input_port];
 856		if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
 857			snd_rawmidi_receive(substream, &data, 1);
 858	}
 859__out:
 860	spin_unlock(&mts->lock);
 861}
 862
 863static int snd_mts64_probe_port(struct parport *p)
 864{
 865	struct pardevice *pardev;
 866	int res;
 867
 868	pardev = parport_register_device(p, DRIVER_NAME,
 869					 NULL, NULL, NULL,
 870					 0, NULL);
 871	if (!pardev)
 872		return -EIO;
 873	
 874	if (parport_claim(pardev)) {
 875		parport_unregister_device(pardev);
 876		return -EIO;
 877	}
 878
 879	res = mts64_probe(p);
 880
 881	parport_release(pardev);
 882	parport_unregister_device(pardev);
 883
 884	return res;
 885}
 886
 887static void snd_mts64_attach(struct parport *p)
 888{
 889	struct platform_device *device;
 890
 891	device = platform_device_alloc(PLATFORM_DRIVER, device_count);
 892	if (!device)
 893		return;
 894
 895	/* Temporary assignment to forward the parport */
 896	platform_set_drvdata(device, p);
 897
 898	if (platform_device_add(device) < 0) {
 899		platform_device_put(device);
 900		return;
 901	}
 902
 903	/* Since we dont get the return value of probe
 904	 * We need to check if device probing succeeded or not */
 905	if (!platform_get_drvdata(device)) {
 906		platform_device_unregister(device);
 907		return;
 908	}
 909
 910	/* register device in global table */
 911	platform_devices[device_count] = device;
 912	device_count++;
 913}
 914
 915static void snd_mts64_detach(struct parport *p)
 916{
 917	/* nothing to do here */
 918}
 919
 
 
 
 
 
 
 
 
 920static struct parport_driver mts64_parport_driver = {
 921	.name   = "mts64",
 922	.attach = snd_mts64_attach,
 923	.detach = snd_mts64_detach
 
 
 924};
 925
 926/*********************************************************************
 927 * platform stuff
 928 *********************************************************************/
 929static void snd_mts64_card_private_free(struct snd_card *card)
 930{
 931	struct mts64 *mts = card->private_data;
 932	struct pardevice *pardev = mts->pardev;
 933
 934	if (pardev) {
 935		if (mts->pardev_claimed)
 936			parport_release(pardev);
 937		parport_unregister_device(pardev);
 938	}
 939
 940	snd_mts64_free(mts);
 941}
 942
 943static int snd_mts64_probe(struct platform_device *pdev)
 944{
 945	struct pardevice *pardev;
 946	struct parport *p;
 947	int dev = pdev->id;
 948	struct snd_card *card = NULL;
 949	struct mts64 *mts = NULL;
 950	int err;
 
 
 
 
 
 
 951
 952	p = platform_get_drvdata(pdev);
 953	platform_set_drvdata(pdev, NULL);
 954
 955	if (dev >= SNDRV_CARDS)
 956		return -ENODEV;
 957	if (!enable[dev]) 
 958		return -ENOENT;
 959	if ((err = snd_mts64_probe_port(p)) < 0)
 960		return err;
 961
 962	err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
 963			   0, &card);
 964	if (err < 0) {
 965		snd_printd("Cannot create card\n");
 966		return err;
 967	}
 968	strcpy(card->driver, DRIVER_NAME);
 969	strcpy(card->shortname, "ESI " CARD_NAME);
 970	sprintf(card->longname,  "%s at 0x%lx, irq %i", 
 971		card->shortname, p->base, p->irq);
 972
 973	pardev = parport_register_device(p,                   /* port */
 974					 DRIVER_NAME,         /* name */
 975					 NULL,                /* preempt */
 976					 NULL,                /* wakeup */
 977					 snd_mts64_interrupt, /* ISR */
 978					 PARPORT_DEV_EXCL,    /* flags */
 979					 (void *)card);       /* private */
 980	if (pardev == NULL) {
 981		snd_printd("Cannot register pardevice\n");
 982		err = -EIO;
 983		goto __err;
 984	}
 985
 
 
 
 
 
 
 
 986	if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
 987		snd_printd("Cannot create main component\n");
 988		parport_unregister_device(pardev);
 989		goto __err;
 990	}
 991	card->private_data = mts;
 992	card->private_free = snd_mts64_card_private_free;
 
 
 
 
 
 
 993	
 994	if ((err = snd_mts64_rawmidi_create(card)) < 0) {
 995		snd_printd("Creating Rawmidi component failed\n");
 996		goto __err;
 997	}
 998
 999	/* claim parport */
1000	if (parport_claim(pardev)) {
1001		snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
1002		err = -EIO;
1003		goto __err;
1004	}
1005	mts->pardev_claimed = 1;
1006
1007	/* init device */
1008	if ((err = mts64_device_init(p)) < 0)
1009		goto __err;
1010
1011	platform_set_drvdata(pdev, card);
1012
1013	/* At this point card will be usable */
1014	if ((err = snd_card_register(card)) < 0) {
1015		snd_printd("Cannot register card\n");
1016		goto __err;
1017	}
1018
1019	snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
1020	return 0;
1021
 
 
 
 
1022__err:
1023	snd_card_free(card);
1024	return err;
1025}
1026
1027static int snd_mts64_remove(struct platform_device *pdev)
1028{
1029	struct snd_card *card = platform_get_drvdata(pdev);
1030
1031	if (card)
1032		snd_card_free(card);
1033
1034	return 0;
1035}
1036
1037
1038static struct platform_driver snd_mts64_driver = {
1039	.probe  = snd_mts64_probe,
1040	.remove = snd_mts64_remove,
1041	.driver = {
1042		.name = PLATFORM_DRIVER,
1043		.owner	= THIS_MODULE,
1044	}
1045};
1046
1047/*********************************************************************
1048 * module init stuff
1049 *********************************************************************/
1050static void snd_mts64_unregister_all(void)
1051{
1052	int i;
1053
1054	for (i = 0; i < SNDRV_CARDS; ++i) {
1055		if (platform_devices[i]) {
1056			platform_device_unregister(platform_devices[i]);
1057			platform_devices[i] = NULL;
1058		}
1059	}		
1060	platform_driver_unregister(&snd_mts64_driver);
1061	parport_unregister_driver(&mts64_parport_driver);
1062}
1063
1064static int __init snd_mts64_module_init(void)
1065{
1066	int err;
1067
1068	if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1069		return err;
1070
1071	if (parport_register_driver(&mts64_parport_driver) != 0) {
1072		platform_driver_unregister(&snd_mts64_driver);
1073		return -EIO;
1074	}
1075
1076	if (device_count == 0) {
1077		snd_mts64_unregister_all();
1078		return -ENODEV;
1079	}
1080
1081	return 0;
1082}
1083
1084static void __exit snd_mts64_module_exit(void)
1085{
1086	snd_mts64_unregister_all();
1087}
1088
1089module_init(snd_mts64_module_init);
1090module_exit(snd_mts64_module_exit);
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*     
   3 *   ALSA Driver for Ego Systems Inc. (ESI) Miditerminal 4140
   4 *   Copyright (c) 2006 by Matthias König <mk@phasorlab.de>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   5 */
   6
   7#include <linux/init.h>
   8#include <linux/platform_device.h>
   9#include <linux/parport.h>
  10#include <linux/spinlock.h>
  11#include <linux/module.h>
  12#include <linux/delay.h>
  13#include <linux/slab.h>
  14#include <sound/core.h>
  15#include <sound/initval.h>
  16#include <sound/rawmidi.h>
  17#include <sound/control.h>
  18
  19#define CARD_NAME "Miditerminal 4140"
  20#define DRIVER_NAME "MTS64"
  21#define PLATFORM_DRIVER "snd_mts64"
  22
  23static int index[SNDRV_CARDS]  = SNDRV_DEFAULT_IDX;
  24static char *id[SNDRV_CARDS]   = SNDRV_DEFAULT_STR;
  25static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  26
  27static struct platform_device *platform_devices[SNDRV_CARDS]; 
  28static int device_count;
  29
  30module_param_array(index, int, NULL, 0444);
  31MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  32module_param_array(id, charp, NULL, 0444);
  33MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  34module_param_array(enable, bool, NULL, 0444);
  35MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  36
  37MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
  38MODULE_DESCRIPTION("ESI Miditerminal 4140");
  39MODULE_LICENSE("GPL");
  40MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
  41
  42/*********************************************************************
  43 * Chip specific
  44 *********************************************************************/
  45#define MTS64_NUM_INPUT_PORTS 5
  46#define MTS64_NUM_OUTPUT_PORTS 4
  47#define MTS64_SMPTE_SUBSTREAM 4
  48
  49struct mts64 {
  50	spinlock_t lock;
  51	struct snd_card *card;
  52	struct snd_rawmidi *rmidi;
  53	struct pardevice *pardev;
 
 
  54	int open_count;
  55	int current_midi_output_port;
  56	int current_midi_input_port;
  57	u8 mode[MTS64_NUM_INPUT_PORTS];
  58	struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
  59	int smpte_switch;
  60	u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
  61	u8 fps;
  62};
  63
  64static int snd_mts64_free(struct mts64 *mts)
  65{
  66	kfree(mts);
  67	return 0;
  68}
  69
  70static int snd_mts64_create(struct snd_card *card,
  71			    struct pardevice *pardev,
  72			    struct mts64 **rchip)
  73{
  74	struct mts64 *mts;
  75
  76	*rchip = NULL;
  77
  78	mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
  79	if (mts == NULL) 
  80		return -ENOMEM;
  81
  82	/* Init chip specific data */
  83	spin_lock_init(&mts->lock);
  84	mts->card = card;
  85	mts->pardev = pardev;
  86	mts->current_midi_output_port = -1;
  87	mts->current_midi_input_port = -1;
  88
  89	*rchip = mts;
  90
  91	return 0;
  92}
  93
  94/*********************************************************************
  95 * HW register related constants
  96 *********************************************************************/
  97
  98/* Status Bits */
  99#define MTS64_STAT_BSY             0x80
 100#define MTS64_STAT_BIT_SET         0x20  /* readout process, bit is set */
 101#define MTS64_STAT_PORT            0x10  /* read byte is a port number */
 102
 103/* Control Bits */
 104#define MTS64_CTL_READOUT          0x08  /* enable readout */
 105#define MTS64_CTL_WRITE_CMD        0x06  
 106#define MTS64_CTL_WRITE_DATA       0x02  
 107#define MTS64_CTL_STROBE           0x01  
 108
 109/* Command */
 110#define MTS64_CMD_RESET            0xfe
 111#define MTS64_CMD_PROBE            0x8f  /* Used in probing procedure */
 112#define MTS64_CMD_SMPTE_SET_TIME   0xe8
 113#define MTS64_CMD_SMPTE_SET_FPS    0xee
 114#define MTS64_CMD_SMPTE_STOP       0xef
 115#define MTS64_CMD_SMPTE_FPS_24     0xe3
 116#define MTS64_CMD_SMPTE_FPS_25     0xe2
 117#define MTS64_CMD_SMPTE_FPS_2997   0xe4 
 118#define MTS64_CMD_SMPTE_FPS_30D    0xe1
 119#define MTS64_CMD_SMPTE_FPS_30     0xe0
 120#define MTS64_CMD_COM_OPEN         0xf8  /* setting the communication mode */
 121#define MTS64_CMD_COM_CLOSE1       0xff  /* clearing communication mode */
 122#define MTS64_CMD_COM_CLOSE2       0xf5
 123
 124/*********************************************************************
 125 * Hardware specific functions
 126 *********************************************************************/
 127static void mts64_enable_readout(struct parport *p);
 128static void mts64_disable_readout(struct parport *p);
 129static int mts64_device_ready(struct parport *p);
 130static int mts64_device_init(struct parport *p);
 131static int mts64_device_open(struct mts64 *mts);
 132static int mts64_device_close(struct mts64 *mts);
 133static u8 mts64_map_midi_input(u8 c);
 134static int mts64_probe(struct parport *p);
 135static u16 mts64_read(struct parport *p);
 136static u8 mts64_read_char(struct parport *p);
 137static void mts64_smpte_start(struct parport *p,
 138			      u8 hours, u8 minutes,
 139			      u8 seconds, u8 frames,
 140			      u8 idx);
 141static void mts64_smpte_stop(struct parport *p);
 142static void mts64_write_command(struct parport *p, u8 c);
 143static void mts64_write_data(struct parport *p, u8 c);
 144static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
 145
 146
 147/*  Enables the readout procedure
 148 *
 149 *  Before we can read a midi byte from the device, we have to set
 150 *  bit 3 of control port.
 151 */
 152static void mts64_enable_readout(struct parport *p)
 153{
 154	u8 c;
 155
 156	c = parport_read_control(p);
 157	c |= MTS64_CTL_READOUT;
 158	parport_write_control(p, c); 
 159}
 160
 161/*  Disables readout 
 162 *
 163 *  Readout is disabled by clearing bit 3 of control
 164 */
 165static void mts64_disable_readout(struct parport *p)
 166{
 167	u8 c;
 168
 169	c = parport_read_control(p);
 170	c &= ~MTS64_CTL_READOUT;
 171	parport_write_control(p, c);
 172}
 173
 174/*  waits for device ready
 175 *
 176 *  Checks if BUSY (Bit 7 of status) is clear
 177 *  1 device ready
 178 *  0 failure
 179 */
 180static int mts64_device_ready(struct parport *p)
 181{
 182	int i;
 183	u8 c;
 184
 185	for (i = 0; i < 0xffff; ++i) {
 186		c = parport_read_status(p);
 187		c &= MTS64_STAT_BSY;
 188		if (c != 0) 
 189			return 1;
 190	} 
 191
 192	return 0;
 193}
 194
 195/*  Init device (LED blinking startup magic)
 196 *
 197 *  Returns:
 198 *  0 init ok
 199 *  -EIO failure
 200 */
 201static int mts64_device_init(struct parport *p)
 202{
 203	int i;
 204
 205	mts64_write_command(p, MTS64_CMD_RESET);
 206
 207	for (i = 0; i < 64; ++i) {
 208		msleep(100);
 209
 210		if (mts64_probe(p) == 0) {
 211			/* success */
 212			mts64_disable_readout(p);
 213			return 0;
 214		}
 215	}
 216	mts64_disable_readout(p);
 217
 218	return -EIO;
 219}
 220
 221/* 
 222 *  Opens the device (set communication mode)
 223 */
 224static int mts64_device_open(struct mts64 *mts)
 225{
 226	int i;
 227	struct parport *p = mts->pardev->port;
 228
 229	for (i = 0; i < 5; ++i)
 230		mts64_write_command(p, MTS64_CMD_COM_OPEN);
 231
 232	return 0;
 233}
 234
 235/*  
 236 *  Close device (clear communication mode)
 237 */
 238static int mts64_device_close(struct mts64 *mts)
 239{
 240	int i;
 241	struct parport *p = mts->pardev->port;
 242
 243	for (i = 0; i < 5; ++i) {
 244		mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
 245		mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
 246	}
 247
 248	return 0;
 249}
 250
 251/*  map hardware port to substream number
 252 * 
 253 *  When reading a byte from the device, the device tells us
 254 *  on what port the byte is. This HW port has to be mapped to
 255 *  the midiport (substream number).
 256 *  substream 0-3 are Midiports 1-4
 257 *  substream 4 is SMPTE Timecode
 258 *  The mapping is done by the table:
 259 *  HW | 0 | 1 | 2 | 3 | 4 
 260 *  SW | 0 | 1 | 4 | 2 | 3
 261 */
 262static u8 mts64_map_midi_input(u8 c)
 263{
 264	static const u8 map[] = { 0, 1, 4, 2, 3 };
 265
 266	return map[c];
 267}
 268
 269
 270/*  Probe parport for device
 271 *
 272 *  Do we have a Miditerminal 4140 on parport? 
 273 *  Returns:
 274 *  0       device found
 275 *  -ENODEV no device
 276 */
 277static int mts64_probe(struct parport *p)
 278{
 279	u8 c;
 280
 281	mts64_smpte_stop(p);
 282	mts64_write_command(p, MTS64_CMD_PROBE);
 283
 284	msleep(50);
 285	
 286	c = mts64_read(p);
 287
 288	c &= 0x00ff;
 289	if (c != MTS64_CMD_PROBE) 
 290		return -ENODEV;
 291	else 
 292		return 0;
 293
 294}
 295
 296/*  Read byte incl. status from device
 297 *
 298 *  Returns:
 299 *  data in lower 8 bits and status in upper 8 bits
 300 */
 301static u16 mts64_read(struct parport *p)
 302{
 303	u8 data, status;
 304
 305	mts64_device_ready(p);
 306	mts64_enable_readout(p);
 307	status = parport_read_status(p);
 308	data = mts64_read_char(p);
 309	mts64_disable_readout(p);
 310
 311	return (status << 8) | data;
 312}
 313
 314/*  Read a byte from device
 315 *
 316 *  Note, that readout mode has to be enabled.
 317 *  readout procedure is as follows: 
 318 *  - Write number of the Bit to read to DATA
 319 *  - Read STATUS
 320 *  - Bit 5 of STATUS indicates if Bit is set
 321 *
 322 *  Returns:
 323 *  Byte read from device
 324 */
 325static u8 mts64_read_char(struct parport *p)
 326{
 327	u8 c = 0;
 328	u8 status;
 329	u8 i;
 330
 331	for (i = 0; i < 8; ++i) {
 332		parport_write_data(p, i);
 333		c >>= 1;
 334		status = parport_read_status(p);
 335		if (status & MTS64_STAT_BIT_SET) 
 336			c |= 0x80;
 337	}
 338	
 339	return c;
 340}
 341
 342/*  Starts SMPTE Timecode generation
 343 *
 344 *  The device creates SMPTE Timecode by hardware.
 345 *  0 24 fps
 346 *  1 25 fps
 347 *  2 29.97 fps
 348 *  3 30 fps (Drop-frame)
 349 *  4 30 fps
 350 */
 351static void mts64_smpte_start(struct parport *p,
 352			      u8 hours, u8 minutes,
 353			      u8 seconds, u8 frames,
 354			      u8 idx)
 355{
 356	static const u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24,
 357			     MTS64_CMD_SMPTE_FPS_25,
 358			     MTS64_CMD_SMPTE_FPS_2997, 
 359			     MTS64_CMD_SMPTE_FPS_30D,
 360			     MTS64_CMD_SMPTE_FPS_30    };
 361
 362	mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
 363	mts64_write_command(p, frames);
 364	mts64_write_command(p, seconds);
 365	mts64_write_command(p, minutes);
 366	mts64_write_command(p, hours);
 367
 368	mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
 369	mts64_write_command(p, fps[idx]);
 370}
 371
 372/*  Stops SMPTE Timecode generation
 373 */
 374static void mts64_smpte_stop(struct parport *p)
 375{
 376	mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
 377}
 378
 379/*  Write a command byte to device
 380 */
 381static void mts64_write_command(struct parport *p, u8 c)
 382{
 383	mts64_device_ready(p);
 384
 385	parport_write_data(p, c);
 386
 387	parport_write_control(p, MTS64_CTL_WRITE_CMD);
 388	parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
 389	parport_write_control(p, MTS64_CTL_WRITE_CMD);
 390}
 391
 392/*  Write a data byte to device 
 393 */
 394static void mts64_write_data(struct parport *p, u8 c)
 395{
 396	mts64_device_ready(p);
 397
 398	parport_write_data(p, c);
 399
 400	parport_write_control(p, MTS64_CTL_WRITE_DATA);
 401	parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
 402	parport_write_control(p, MTS64_CTL_WRITE_DATA);
 403}
 404
 405/*  Write a MIDI byte to midiport
 406 *
 407 *  midiport ranges from 0-3 and maps to Ports 1-4
 408 *  assumptions: communication mode is on
 409 */
 410static void mts64_write_midi(struct mts64 *mts, u8 c,
 411			     int midiport)
 412{
 413	struct parport *p = mts->pardev->port;
 414
 415	/* check current midiport */
 416	if (mts->current_midi_output_port != midiport)
 417		mts64_write_command(p, midiport);
 418
 419	/* write midi byte */
 420	mts64_write_data(p, c);
 421}
 422
 423/*********************************************************************
 424 * Control elements
 425 *********************************************************************/
 426
 427/* SMPTE Switch */
 428#define snd_mts64_ctl_smpte_switch_info		snd_ctl_boolean_mono_info
 429
 430static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
 431					  struct snd_ctl_elem_value *uctl)
 432{
 433	struct mts64 *mts = snd_kcontrol_chip(kctl);
 434
 435	spin_lock_irq(&mts->lock);
 436	uctl->value.integer.value[0] = mts->smpte_switch;
 437	spin_unlock_irq(&mts->lock);
 438
 439	return 0;
 440}
 441
 442/* smpte_switch is not accessed from IRQ handler, so we just need
 443   to protect the HW access */
 444static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
 445					  struct snd_ctl_elem_value *uctl)
 446{
 447	struct mts64 *mts = snd_kcontrol_chip(kctl);
 448	int changed = 0;
 449	int val = !!uctl->value.integer.value[0];
 450
 451	spin_lock_irq(&mts->lock);
 452	if (mts->smpte_switch == val)
 453		goto __out;
 454
 455	changed = 1;
 456	mts->smpte_switch = val;
 457	if (mts->smpte_switch) {
 458		mts64_smpte_start(mts->pardev->port,
 459				  mts->time[0], mts->time[1],
 460				  mts->time[2], mts->time[3],
 461				  mts->fps);
 462	} else {
 463		mts64_smpte_stop(mts->pardev->port);
 464	}
 465__out:
 466	spin_unlock_irq(&mts->lock);
 467	return changed;
 468}
 469
 470static const struct snd_kcontrol_new mts64_ctl_smpte_switch = {
 471	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 472	.name  = "SMPTE Playback Switch",
 473	.index = 0,
 474	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 475	.private_value = 0,
 476	.info = snd_mts64_ctl_smpte_switch_info,
 477	.get  = snd_mts64_ctl_smpte_switch_get,
 478	.put  = snd_mts64_ctl_smpte_switch_put
 479};
 480
 481/* Time */
 482static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
 483					   struct snd_ctl_elem_info *uinfo)
 484{
 485	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 486	uinfo->count = 1;
 487	uinfo->value.integer.min = 0;
 488	uinfo->value.integer.max = 23;
 489	return 0;
 490}
 491
 492static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
 493					   struct snd_ctl_elem_info *uinfo)
 494{
 495	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 496	uinfo->count = 1;
 497	uinfo->value.integer.min = 0;
 498	uinfo->value.integer.max = 99;
 499	return 0;
 500}
 501
 502static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
 503					 struct snd_ctl_elem_info *uinfo)
 504{
 505	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 506	uinfo->count = 1;
 507	uinfo->value.integer.min = 0;
 508	uinfo->value.integer.max = 59;
 509	return 0;
 510}
 511
 512static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
 513					struct snd_ctl_elem_value *uctl)
 514{
 515	struct mts64 *mts = snd_kcontrol_chip(kctl);
 516	int idx = kctl->private_value;
 517
 518	spin_lock_irq(&mts->lock);
 519	uctl->value.integer.value[0] = mts->time[idx];
 520	spin_unlock_irq(&mts->lock);
 521
 522	return 0;
 523}
 524
 525static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
 526					struct snd_ctl_elem_value *uctl)
 527{
 528	struct mts64 *mts = snd_kcontrol_chip(kctl);
 529	int idx = kctl->private_value;
 530	unsigned int time = uctl->value.integer.value[0] % 60;
 531	int changed = 0;
 532
 533	spin_lock_irq(&mts->lock);
 534	if (mts->time[idx] != time) {
 535		changed = 1;
 536		mts->time[idx] = time;
 537	}
 538	spin_unlock_irq(&mts->lock);
 539
 540	return changed;
 541}
 542
 543static const struct snd_kcontrol_new mts64_ctl_smpte_time_hours = {
 544	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 545	.name  = "SMPTE Time Hours",
 546	.index = 0,
 547	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 548	.private_value = 0,
 549	.info = snd_mts64_ctl_smpte_time_h_info,
 550	.get  = snd_mts64_ctl_smpte_time_get,
 551	.put  = snd_mts64_ctl_smpte_time_put
 552};
 553
 554static const struct snd_kcontrol_new mts64_ctl_smpte_time_minutes = {
 555	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 556	.name  = "SMPTE Time Minutes",
 557	.index = 0,
 558	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 559	.private_value = 1,
 560	.info = snd_mts64_ctl_smpte_time_info,
 561	.get  = snd_mts64_ctl_smpte_time_get,
 562	.put  = snd_mts64_ctl_smpte_time_put
 563};
 564
 565static const struct snd_kcontrol_new mts64_ctl_smpte_time_seconds = {
 566	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 567	.name  = "SMPTE Time Seconds",
 568	.index = 0,
 569	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 570	.private_value = 2,
 571	.info = snd_mts64_ctl_smpte_time_info,
 572	.get  = snd_mts64_ctl_smpte_time_get,
 573	.put  = snd_mts64_ctl_smpte_time_put
 574};
 575
 576static const struct snd_kcontrol_new mts64_ctl_smpte_time_frames = {
 577	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 578	.name  = "SMPTE Time Frames",
 579	.index = 0,
 580	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 581	.private_value = 3,
 582	.info = snd_mts64_ctl_smpte_time_f_info,
 583	.get  = snd_mts64_ctl_smpte_time_get,
 584	.put  = snd_mts64_ctl_smpte_time_put
 585};
 586
 587/* FPS */
 588static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
 589					struct snd_ctl_elem_info *uinfo)
 590{
 591	static const char * const texts[5] = {
 592		"24", "25", "29.97", "30D", "30"
 593	};
 
 
 594
 595	return snd_ctl_enum_info(uinfo, 1, 5, texts);
 
 
 
 
 
 
 
 
 596}
 597
 598static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
 599				       struct snd_ctl_elem_value *uctl)
 600{
 601	struct mts64 *mts = snd_kcontrol_chip(kctl);
 602
 603	spin_lock_irq(&mts->lock);
 604	uctl->value.enumerated.item[0] = mts->fps;
 605	spin_unlock_irq(&mts->lock);
 606
 607	return 0;
 608}
 609
 610static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
 611				       struct snd_ctl_elem_value *uctl)
 612{
 613	struct mts64 *mts = snd_kcontrol_chip(kctl);
 614	int changed = 0;
 615
 616	if (uctl->value.enumerated.item[0] >= 5)
 617		return -EINVAL;
 618	spin_lock_irq(&mts->lock);
 619	if (mts->fps != uctl->value.enumerated.item[0]) {
 620		changed = 1;
 621		mts->fps = uctl->value.enumerated.item[0];
 622	}
 623	spin_unlock_irq(&mts->lock);
 624
 625	return changed;
 626}
 627
 628static const struct snd_kcontrol_new mts64_ctl_smpte_fps = {
 629	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 630	.name  = "SMPTE Fps",
 631	.index = 0,
 632	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 633	.private_value = 0,
 634	.info  = snd_mts64_ctl_smpte_fps_info,
 635	.get   = snd_mts64_ctl_smpte_fps_get,
 636	.put   = snd_mts64_ctl_smpte_fps_put
 637};
 638
 639
 640static int snd_mts64_ctl_create(struct snd_card *card,
 641				struct mts64 *mts)
 642{
 643	int err, i;
 644	static const struct snd_kcontrol_new *control[] = {
 645		&mts64_ctl_smpte_switch,
 646		&mts64_ctl_smpte_time_hours,
 647		&mts64_ctl_smpte_time_minutes,
 648		&mts64_ctl_smpte_time_seconds,
 649		&mts64_ctl_smpte_time_frames,
 650		&mts64_ctl_smpte_fps,
 651	        NULL  };
 652
 653	for (i = 0; control[i]; ++i) {
 654		err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
 655		if (err < 0) {
 656			snd_printd("Cannot create control: %s\n", 
 657				   control[i]->name);
 658			return err;
 659		}
 660	}
 661
 662	return 0;
 663}
 664
 665/*********************************************************************
 666 * Rawmidi
 667 *********************************************************************/
 668#define MTS64_MODE_INPUT_TRIGGERED 0x01
 669
 670static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
 671{
 672	struct mts64 *mts = substream->rmidi->private_data;
 673
 674	if (mts->open_count == 0) {
 675		/* We don't need a spinlock here, because this is just called 
 676		   if the device has not been opened before. 
 677		   So there aren't any IRQs from the device */
 678		mts64_device_open(mts);
 679
 680		msleep(50);
 681	}
 682	++(mts->open_count);
 683
 684	return 0;
 685}
 686
 687static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
 688{
 689	struct mts64 *mts = substream->rmidi->private_data;
 690	unsigned long flags;
 691
 692	--(mts->open_count);
 693	if (mts->open_count == 0) {
 694		/* We need the spinlock_irqsave here because we can still
 695		   have IRQs at this point */
 696		spin_lock_irqsave(&mts->lock, flags);
 697		mts64_device_close(mts);
 698		spin_unlock_irqrestore(&mts->lock, flags);
 699
 700		msleep(500);
 701
 702	} else if (mts->open_count < 0)
 703		mts->open_count = 0;
 704
 705	return 0;
 706}
 707
 708static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
 709					     int up)
 710{
 711	struct mts64 *mts = substream->rmidi->private_data;
 712	u8 data;
 713	unsigned long flags;
 714
 715	spin_lock_irqsave(&mts->lock, flags);
 716	while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
 717		mts64_write_midi(mts, data, substream->number+1);
 718		snd_rawmidi_transmit_ack(substream, 1);
 719	}
 720	spin_unlock_irqrestore(&mts->lock, flags);
 721}
 722
 723static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
 724					    int up)
 725{
 726	struct mts64 *mts = substream->rmidi->private_data;
 727	unsigned long flags;
 728
 729	spin_lock_irqsave(&mts->lock, flags);
 730	if (up)
 731		mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
 732	else
 733 		mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
 734	
 735	spin_unlock_irqrestore(&mts->lock, flags);
 736}
 737
 738static const struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
 739	.open    = snd_mts64_rawmidi_open,
 740	.close   = snd_mts64_rawmidi_close,
 741	.trigger = snd_mts64_rawmidi_output_trigger
 742};
 743
 744static const struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
 745	.open    = snd_mts64_rawmidi_open,
 746	.close   = snd_mts64_rawmidi_close,
 747	.trigger = snd_mts64_rawmidi_input_trigger
 748};
 749
 750/* Create and initialize the rawmidi component */
 751static int snd_mts64_rawmidi_create(struct snd_card *card)
 752{
 753	struct mts64 *mts = card->private_data;
 754	struct snd_rawmidi *rmidi;
 755	struct snd_rawmidi_substream *substream;
 756	struct list_head *list;
 757	int err;
 758	
 759	err = snd_rawmidi_new(card, CARD_NAME, 0, 
 760			      MTS64_NUM_OUTPUT_PORTS, 
 761			      MTS64_NUM_INPUT_PORTS, 
 762			      &rmidi);
 763	if (err < 0) 
 764		return err;
 765
 766	rmidi->private_data = mts;
 767	strcpy(rmidi->name, CARD_NAME);
 768	rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
 769		            SNDRV_RAWMIDI_INFO_INPUT |
 770                            SNDRV_RAWMIDI_INFO_DUPLEX;
 771
 772	mts->rmidi = rmidi;
 773
 774	/* register rawmidi ops */
 775	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 
 776			    &snd_mts64_rawmidi_output_ops);
 777	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 
 778			    &snd_mts64_rawmidi_input_ops);
 779
 780	/* name substreams */
 781	/* output */
 782	list_for_each(list, 
 783		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
 784		substream = list_entry(list, struct snd_rawmidi_substream, list);
 785		sprintf(substream->name,
 786			"Miditerminal %d", substream->number+1);
 787	}
 788	/* input */
 789	list_for_each(list, 
 790		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
 791		substream = list_entry(list, struct snd_rawmidi_substream, list);
 792		mts->midi_input_substream[substream->number] = substream;
 793		switch(substream->number) {
 794		case MTS64_SMPTE_SUBSTREAM:
 795			strcpy(substream->name, "Miditerminal SMPTE");
 796			break;
 797		default:
 798			sprintf(substream->name,
 799				"Miditerminal %d", substream->number+1);
 800		}
 801	}
 802
 803	/* controls */
 804	err = snd_mts64_ctl_create(card, mts);
 805
 806	return err;
 807}
 808
 809/*********************************************************************
 810 * parport stuff
 811 *********************************************************************/
 812static void snd_mts64_interrupt(void *private)
 813{
 814	struct mts64 *mts = ((struct snd_card*)private)->private_data;
 815	u16 ret;
 816	u8 status, data;
 817	struct snd_rawmidi_substream *substream;
 818
 819	spin_lock(&mts->lock);
 820	ret = mts64_read(mts->pardev->port);
 821	data = ret & 0x00ff;
 822	status = ret >> 8;
 823
 824	if (status & MTS64_STAT_PORT) {
 825		mts->current_midi_input_port = mts64_map_midi_input(data);
 826	} else {
 827		if (mts->current_midi_input_port == -1) 
 828			goto __out;
 829		substream = mts->midi_input_substream[mts->current_midi_input_port];
 830		if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
 831			snd_rawmidi_receive(substream, &data, 1);
 832	}
 833__out:
 834	spin_unlock(&mts->lock);
 835}
 836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 837static void snd_mts64_attach(struct parport *p)
 838{
 839	struct platform_device *device;
 840
 841	device = platform_device_alloc(PLATFORM_DRIVER, device_count);
 842	if (!device)
 843		return;
 844
 845	/* Temporary assignment to forward the parport */
 846	platform_set_drvdata(device, p);
 847
 848	if (platform_device_add(device) < 0) {
 849		platform_device_put(device);
 850		return;
 851	}
 852
 853	/* Since we dont get the return value of probe
 854	 * We need to check if device probing succeeded or not */
 855	if (!platform_get_drvdata(device)) {
 856		platform_device_unregister(device);
 857		return;
 858	}
 859
 860	/* register device in global table */
 861	platform_devices[device_count] = device;
 862	device_count++;
 863}
 864
 865static void snd_mts64_detach(struct parport *p)
 866{
 867	/* nothing to do here */
 868}
 869
 870static int snd_mts64_dev_probe(struct pardevice *pardev)
 871{
 872	if (strcmp(pardev->name, DRIVER_NAME))
 873		return -ENODEV;
 874
 875	return 0;
 876}
 877
 878static struct parport_driver mts64_parport_driver = {
 879	.name		= "mts64",
 880	.probe		= snd_mts64_dev_probe,
 881	.match_port	= snd_mts64_attach,
 882	.detach		= snd_mts64_detach,
 883	.devmodel	= true,
 884};
 885
 886/*********************************************************************
 887 * platform stuff
 888 *********************************************************************/
 889static void snd_mts64_card_private_free(struct snd_card *card)
 890{
 891	struct mts64 *mts = card->private_data;
 892	struct pardevice *pardev = mts->pardev;
 893
 894	if (pardev) {
 895		parport_release(pardev);
 
 896		parport_unregister_device(pardev);
 897	}
 898
 899	snd_mts64_free(mts);
 900}
 901
 902static int snd_mts64_probe(struct platform_device *pdev)
 903{
 904	struct pardevice *pardev;
 905	struct parport *p;
 906	int dev = pdev->id;
 907	struct snd_card *card = NULL;
 908	struct mts64 *mts = NULL;
 909	int err;
 910	struct pardev_cb mts64_cb = {
 911		.preempt = NULL,
 912		.wakeup = NULL,
 913		.irq_func = snd_mts64_interrupt,	/* ISR */
 914		.flags = PARPORT_DEV_EXCL,		/* flags */
 915	};
 916
 917	p = platform_get_drvdata(pdev);
 918	platform_set_drvdata(pdev, NULL);
 919
 920	if (dev >= SNDRV_CARDS)
 921		return -ENODEV;
 922	if (!enable[dev]) 
 923		return -ENOENT;
 
 
 924
 925	err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
 926			   0, &card);
 927	if (err < 0) {
 928		snd_printd("Cannot create card\n");
 929		return err;
 930	}
 931	strcpy(card->driver, DRIVER_NAME);
 932	strcpy(card->shortname, "ESI " CARD_NAME);
 933	sprintf(card->longname,  "%s at 0x%lx, irq %i", 
 934		card->shortname, p->base, p->irq);
 935
 936	mts64_cb.private = card;			 /* private */
 937	pardev = parport_register_dev_model(p,		 /* port */
 938					    DRIVER_NAME, /* name */
 939					    &mts64_cb,	 /* callbacks */
 940					    pdev->id);	 /* device number */
 941	if (!pardev) {
 
 
 942		snd_printd("Cannot register pardevice\n");
 943		err = -EIO;
 944		goto __err;
 945	}
 946
 947	/* claim parport */
 948	if (parport_claim(pardev)) {
 949		snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
 950		err = -EIO;
 951		goto free_pardev;
 952	}
 953
 954	if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
 955		snd_printd("Cannot create main component\n");
 956		goto release_pardev;
 
 957	}
 958	card->private_data = mts;
 959	card->private_free = snd_mts64_card_private_free;
 960
 961	err = mts64_probe(p);
 962	if (err) {
 963		err = -EIO;
 964		goto __err;
 965	}
 966	
 967	if ((err = snd_mts64_rawmidi_create(card)) < 0) {
 968		snd_printd("Creating Rawmidi component failed\n");
 969		goto __err;
 970	}
 971
 
 
 
 
 
 
 
 
 972	/* init device */
 973	if ((err = mts64_device_init(p)) < 0)
 974		goto __err;
 975
 976	platform_set_drvdata(pdev, card);
 977
 978	/* At this point card will be usable */
 979	if ((err = snd_card_register(card)) < 0) {
 980		snd_printd("Cannot register card\n");
 981		goto __err;
 982	}
 983
 984	snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
 985	return 0;
 986
 987release_pardev:
 988	parport_release(pardev);
 989free_pardev:
 990	parport_unregister_device(pardev);
 991__err:
 992	snd_card_free(card);
 993	return err;
 994}
 995
 996static int snd_mts64_remove(struct platform_device *pdev)
 997{
 998	struct snd_card *card = platform_get_drvdata(pdev);
 999
1000	if (card)
1001		snd_card_free(card);
1002
1003	return 0;
1004}
1005
 
1006static struct platform_driver snd_mts64_driver = {
1007	.probe  = snd_mts64_probe,
1008	.remove = snd_mts64_remove,
1009	.driver = {
1010		.name = PLATFORM_DRIVER,
 
1011	}
1012};
1013
1014/*********************************************************************
1015 * module init stuff
1016 *********************************************************************/
1017static void snd_mts64_unregister_all(void)
1018{
1019	int i;
1020
1021	for (i = 0; i < SNDRV_CARDS; ++i) {
1022		if (platform_devices[i]) {
1023			platform_device_unregister(platform_devices[i]);
1024			platform_devices[i] = NULL;
1025		}
1026	}		
1027	platform_driver_unregister(&snd_mts64_driver);
1028	parport_unregister_driver(&mts64_parport_driver);
1029}
1030
1031static int __init snd_mts64_module_init(void)
1032{
1033	int err;
1034
1035	if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1036		return err;
1037
1038	if (parport_register_driver(&mts64_parport_driver) != 0) {
1039		platform_driver_unregister(&snd_mts64_driver);
1040		return -EIO;
1041	}
1042
1043	if (device_count == 0) {
1044		snd_mts64_unregister_all();
1045		return -ENODEV;
1046	}
1047
1048	return 0;
1049}
1050
1051static void __exit snd_mts64_module_exit(void)
1052{
1053	snd_mts64_unregister_all();
1054}
1055
1056module_init(snd_mts64_module_init);
1057module_exit(snd_mts64_module_exit);