File Index
Files and directories
Sign in
FILE

mts64.c

File preview with syntax highlighting.

linux-6.19.14/sound/drivers/mts64.c C
   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 <linux/string.h>
  15#include <sound/core.h>
  16#include <sound/initval.h>
  17#include <sound/rawmidi.h>
  18#include <sound/control.h>
  19
  20#define CARD_NAME "Miditerminal 4140"
  21#define DRIVER_NAME "MTS64"
  22#define PLATFORM_DRIVER "snd_mts64"
  23
  24static int index[SNDRV_CARDS]  = SNDRV_DEFAULT_IDX;
  25static char *id[SNDRV_CARDS]   = SNDRV_DEFAULT_STR;
  26static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  27
  28static struct platform_device *platform_devices[SNDRV_CARDS]; 
  29static int device_count;
  30
  31module_param_array(index, int, NULL, 0444);
  32MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  33module_param_array(id, charp, NULL, 0444);
  34MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  35module_param_array(enable, bool, NULL, 0444);
  36MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  37
  38MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
  39MODULE_DESCRIPTION("ESI Miditerminal 4140");
  40MODULE_LICENSE("GPL");
  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	guard(spinlock_irq)(&mts->lock);
 436	uctl->value.integer.value[0] = mts->smpte_switch;
 437
 438	return 0;
 439}
 440
 441/* smpte_switch is not accessed from IRQ handler, so we just need
 442   to protect the HW access */
 443static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
 444					  struct snd_ctl_elem_value *uctl)
 445{
 446	struct mts64 *mts = snd_kcontrol_chip(kctl);
 447	int val = !!uctl->value.integer.value[0];
 448
 449	guard(spinlock_irq)(&mts->lock);
 450	if (mts->smpte_switch == val)
 451		return 0;
 452
 453	mts->smpte_switch = val;
 454	if (mts->smpte_switch) {
 455		mts64_smpte_start(mts->pardev->port,
 456				  mts->time[0], mts->time[1],
 457				  mts->time[2], mts->time[3],
 458				  mts->fps);
 459	} else {
 460		mts64_smpte_stop(mts->pardev->port);
 461	}
 462	return 1;
 463}
 464
 465static const struct snd_kcontrol_new mts64_ctl_smpte_switch = {
 466	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 467	.name  = "SMPTE Playback Switch",
 468	.index = 0,
 469	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 470	.private_value = 0,
 471	.info = snd_mts64_ctl_smpte_switch_info,
 472	.get  = snd_mts64_ctl_smpte_switch_get,
 473	.put  = snd_mts64_ctl_smpte_switch_put
 474};
 475
 476/* Time */
 477static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
 478					   struct snd_ctl_elem_info *uinfo)
 479{
 480	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 481	uinfo->count = 1;
 482	uinfo->value.integer.min = 0;
 483	uinfo->value.integer.max = 23;
 484	return 0;
 485}
 486
 487static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
 488					   struct snd_ctl_elem_info *uinfo)
 489{
 490	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 491	uinfo->count = 1;
 492	uinfo->value.integer.min = 0;
 493	uinfo->value.integer.max = 99;
 494	return 0;
 495}
 496
 497static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
 498					 struct snd_ctl_elem_info *uinfo)
 499{
 500	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 501	uinfo->count = 1;
 502	uinfo->value.integer.min = 0;
 503	uinfo->value.integer.max = 59;
 504	return 0;
 505}
 506
 507static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
 508					struct snd_ctl_elem_value *uctl)
 509{
 510	struct mts64 *mts = snd_kcontrol_chip(kctl);
 511	int idx = kctl->private_value;
 512
 513	guard(spinlock_irq)(&mts->lock);
 514	uctl->value.integer.value[0] = mts->time[idx];
 515
 516	return 0;
 517}
 518
 519static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
 520					struct snd_ctl_elem_value *uctl)
 521{
 522	struct mts64 *mts = snd_kcontrol_chip(kctl);
 523	int idx = kctl->private_value;
 524	unsigned int time = uctl->value.integer.value[0] % 60;
 525
 526	guard(spinlock_irq)(&mts->lock);
 527	if (mts->time[idx] != time) {
 528		mts->time[idx] = time;
 529		return 1;
 530	}
 531
 532	return 0;
 533}
 534
 535static const struct snd_kcontrol_new mts64_ctl_smpte_time_hours = {
 536	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 537	.name  = "SMPTE Time Hours",
 538	.index = 0,
 539	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 540	.private_value = 0,
 541	.info = snd_mts64_ctl_smpte_time_h_info,
 542	.get  = snd_mts64_ctl_smpte_time_get,
 543	.put  = snd_mts64_ctl_smpte_time_put
 544};
 545
 546static const struct snd_kcontrol_new mts64_ctl_smpte_time_minutes = {
 547	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 548	.name  = "SMPTE Time Minutes",
 549	.index = 0,
 550	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 551	.private_value = 1,
 552	.info = snd_mts64_ctl_smpte_time_info,
 553	.get  = snd_mts64_ctl_smpte_time_get,
 554	.put  = snd_mts64_ctl_smpte_time_put
 555};
 556
 557static const struct snd_kcontrol_new mts64_ctl_smpte_time_seconds = {
 558	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 559	.name  = "SMPTE Time Seconds",
 560	.index = 0,
 561	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 562	.private_value = 2,
 563	.info = snd_mts64_ctl_smpte_time_info,
 564	.get  = snd_mts64_ctl_smpte_time_get,
 565	.put  = snd_mts64_ctl_smpte_time_put
 566};
 567
 568static const struct snd_kcontrol_new mts64_ctl_smpte_time_frames = {
 569	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 570	.name  = "SMPTE Time Frames",
 571	.index = 0,
 572	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 573	.private_value = 3,
 574	.info = snd_mts64_ctl_smpte_time_f_info,
 575	.get  = snd_mts64_ctl_smpte_time_get,
 576	.put  = snd_mts64_ctl_smpte_time_put
 577};
 578
 579/* FPS */
 580static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
 581					struct snd_ctl_elem_info *uinfo)
 582{
 583	static const char * const texts[5] = {
 584		"24", "25", "29.97", "30D", "30"
 585	};
 586
 587	return snd_ctl_enum_info(uinfo, 1, 5, texts);
 588}
 589
 590static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
 591				       struct snd_ctl_elem_value *uctl)
 592{
 593	struct mts64 *mts = snd_kcontrol_chip(kctl);
 594
 595	guard(spinlock_irq)(&mts->lock);
 596	uctl->value.enumerated.item[0] = mts->fps;
 597
 598	return 0;
 599}
 600
 601static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
 602				       struct snd_ctl_elem_value *uctl)
 603{
 604	struct mts64 *mts = snd_kcontrol_chip(kctl);
 605
 606	if (uctl->value.enumerated.item[0] >= 5)
 607		return -EINVAL;
 608	guard(spinlock_irq)(&mts->lock);
 609	if (mts->fps != uctl->value.enumerated.item[0]) {
 610		mts->fps = uctl->value.enumerated.item[0];
 611		return 1;
 612	}
 613
 614	return 0;
 615}
 616
 617static const struct snd_kcontrol_new mts64_ctl_smpte_fps = {
 618	.iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
 619	.name  = "SMPTE Fps",
 620	.index = 0,
 621	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
 622	.private_value = 0,
 623	.info  = snd_mts64_ctl_smpte_fps_info,
 624	.get   = snd_mts64_ctl_smpte_fps_get,
 625	.put   = snd_mts64_ctl_smpte_fps_put
 626};
 627
 628
 629static int snd_mts64_ctl_create(struct snd_card *card,
 630				struct mts64 *mts)
 631{
 632	int err, i;
 633	static const struct snd_kcontrol_new *control[] = {
 634		&mts64_ctl_smpte_switch,
 635		&mts64_ctl_smpte_time_hours,
 636		&mts64_ctl_smpte_time_minutes,
 637		&mts64_ctl_smpte_time_seconds,
 638		&mts64_ctl_smpte_time_frames,
 639		&mts64_ctl_smpte_fps,
 640	        NULL  };
 641
 642	for (i = 0; control[i]; ++i) {
 643		err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
 644		if (err < 0) {
 645			dev_dbg(card->dev, "Cannot create control: %s\n",
 646				control[i]->name);
 647			return err;
 648		}
 649	}
 650
 651	return 0;
 652}
 653
 654/*********************************************************************
 655 * Rawmidi
 656 *********************************************************************/
 657#define MTS64_MODE_INPUT_TRIGGERED 0x01
 658
 659static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
 660{
 661	struct mts64 *mts = substream->rmidi->private_data;
 662
 663	if (mts->open_count == 0) {
 664		/* We don't need a spinlock here, because this is just called 
 665		   if the device has not been opened before. 
 666		   So there aren't any IRQs from the device */
 667		mts64_device_open(mts);
 668
 669		msleep(50);
 670	}
 671	++(mts->open_count);
 672
 673	return 0;
 674}
 675
 676static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
 677{
 678	struct mts64 *mts = substream->rmidi->private_data;
 679
 680	--(mts->open_count);
 681	if (mts->open_count == 0) {
 682		/* We need the spinlock_irqsave here because we can still
 683		   have IRQs at this point */
 684		scoped_guard(spinlock_irqsave, &mts->lock) {
 685			mts64_device_close(mts);
 686		}
 687
 688		msleep(500);
 689
 690	} else if (mts->open_count < 0)
 691		mts->open_count = 0;
 692
 693	return 0;
 694}
 695
 696static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
 697					     int up)
 698{
 699	struct mts64 *mts = substream->rmidi->private_data;
 700	u8 data;
 701
 702	guard(spinlock_irqsave)(&mts->lock);
 703	while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
 704		mts64_write_midi(mts, data, substream->number+1);
 705		snd_rawmidi_transmit_ack(substream, 1);
 706	}
 707}
 708
 709static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
 710					    int up)
 711{
 712	struct mts64 *mts = substream->rmidi->private_data;
 713
 714	guard(spinlock_irqsave)(&mts->lock);
 715	if (up)
 716		mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
 717	else
 718 		mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
 719}
 720
 721static const struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
 722	.open    = snd_mts64_rawmidi_open,
 723	.close   = snd_mts64_rawmidi_close,
 724	.trigger = snd_mts64_rawmidi_output_trigger
 725};
 726
 727static const struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
 728	.open    = snd_mts64_rawmidi_open,
 729	.close   = snd_mts64_rawmidi_close,
 730	.trigger = snd_mts64_rawmidi_input_trigger
 731};
 732
 733/* Create and initialize the rawmidi component */
 734static int snd_mts64_rawmidi_create(struct snd_card *card)
 735{
 736	struct mts64 *mts = card->private_data;
 737	struct snd_rawmidi *rmidi;
 738	struct snd_rawmidi_substream *substream;
 739	struct list_head *list;
 740	int err;
 741	
 742	err = snd_rawmidi_new(card, CARD_NAME, 0, 
 743			      MTS64_NUM_OUTPUT_PORTS, 
 744			      MTS64_NUM_INPUT_PORTS, 
 745			      &rmidi);
 746	if (err < 0) 
 747		return err;
 748
 749	rmidi->private_data = mts;
 750	strscpy(rmidi->name, CARD_NAME);
 751	rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
 752		            SNDRV_RAWMIDI_INFO_INPUT |
 753                            SNDRV_RAWMIDI_INFO_DUPLEX;
 754
 755	mts->rmidi = rmidi;
 756
 757	/* register rawmidi ops */
 758	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 
 759			    &snd_mts64_rawmidi_output_ops);
 760	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 
 761			    &snd_mts64_rawmidi_input_ops);
 762
 763	/* name substreams */
 764	/* output */
 765	list_for_each(list, 
 766		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
 767		substream = list_entry(list, struct snd_rawmidi_substream, list);
 768		sprintf(substream->name,
 769			"Miditerminal %d", substream->number+1);
 770	}
 771	/* input */
 772	list_for_each(list, 
 773		      &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
 774		substream = list_entry(list, struct snd_rawmidi_substream, list);
 775		mts->midi_input_substream[substream->number] = substream;
 776		switch(substream->number) {
 777		case MTS64_SMPTE_SUBSTREAM:
 778			strscpy(substream->name, "Miditerminal SMPTE");
 779			break;
 780		default:
 781			sprintf(substream->name,
 782				"Miditerminal %d", substream->number+1);
 783		}
 784	}
 785
 786	/* controls */
 787	err = snd_mts64_ctl_create(card, mts);
 788
 789	return err;
 790}
 791
 792/*********************************************************************
 793 * parport stuff
 794 *********************************************************************/
 795static void snd_mts64_interrupt(void *private)
 796{
 797	struct mts64 *mts = ((struct snd_card*)private)->private_data;
 798	u16 ret;
 799	u8 status, data;
 800	struct snd_rawmidi_substream *substream;
 801
 802	if (!mts)
 803		return;
 804
 805	guard(spinlock)(&mts->lock);
 806	ret = mts64_read(mts->pardev->port);
 807	data = ret & 0x00ff;
 808	status = ret >> 8;
 809
 810	if (status & MTS64_STAT_PORT) {
 811		mts->current_midi_input_port = mts64_map_midi_input(data);
 812	} else {
 813		if (mts->current_midi_input_port == -1) 
 814			return;
 815		substream = mts->midi_input_substream[mts->current_midi_input_port];
 816		if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
 817			snd_rawmidi_receive(substream, &data, 1);
 818	}
 819}
 820
 821static void snd_mts64_attach(struct parport *p)
 822{
 823	struct platform_device *device;
 824
 825	device = platform_device_alloc(PLATFORM_DRIVER, device_count);
 826	if (!device)
 827		return;
 828
 829	/* Temporary assignment to forward the parport */
 830	platform_set_drvdata(device, p);
 831
 832	if (platform_device_add(device) < 0) {
 833		platform_device_put(device);
 834		return;
 835	}
 836
 837	/* Since we dont get the return value of probe
 838	 * We need to check if device probing succeeded or not */
 839	if (!platform_get_drvdata(device)) {
 840		platform_device_unregister(device);
 841		return;
 842	}
 843
 844	/* register device in global table */
 845	platform_devices[device_count] = device;
 846	device_count++;
 847}
 848
 849static void snd_mts64_detach(struct parport *p)
 850{
 851	/* nothing to do here */
 852}
 853
 854static int snd_mts64_dev_probe(struct pardevice *pardev)
 855{
 856	if (strcmp(pardev->name, DRIVER_NAME))
 857		return -ENODEV;
 858
 859	return 0;
 860}
 861
 862static struct parport_driver mts64_parport_driver = {
 863	.name		= "mts64",
 864	.probe		= snd_mts64_dev_probe,
 865	.match_port	= snd_mts64_attach,
 866	.detach		= snd_mts64_detach,
 867};
 868
 869/*********************************************************************
 870 * platform stuff
 871 *********************************************************************/
 872static void snd_mts64_card_private_free(struct snd_card *card)
 873{
 874	struct mts64 *mts = card->private_data;
 875	struct pardevice *pardev = mts->pardev;
 876
 877	if (pardev) {
 878		parport_release(pardev);
 879		parport_unregister_device(pardev);
 880	}
 881
 882	snd_mts64_free(mts);
 883}
 884
 885static int snd_mts64_probe(struct platform_device *pdev)
 886{
 887	struct pardevice *pardev;
 888	struct parport *p;
 889	int dev = pdev->id;
 890	struct snd_card *card = NULL;
 891	struct mts64 *mts = NULL;
 892	int err;
 893	struct pardev_cb mts64_cb = {
 894		.preempt = NULL,
 895		.wakeup = NULL,
 896		.irq_func = snd_mts64_interrupt,	/* ISR */
 897		.flags = PARPORT_DEV_EXCL,		/* flags */
 898	};
 899
 900	p = platform_get_drvdata(pdev);
 901	platform_set_drvdata(pdev, NULL);
 902
 903	if (dev >= SNDRV_CARDS)
 904		return -ENODEV;
 905	if (!enable[dev]) 
 906		return -ENOENT;
 907
 908	err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
 909			   0, &card);
 910	if (err < 0) {
 911		dev_dbg(&pdev->dev, "Cannot create card\n");
 912		return err;
 913	}
 914	strscpy(card->driver, DRIVER_NAME);
 915	strscpy(card->shortname, "ESI " CARD_NAME);
 916	sprintf(card->longname,  "%s at 0x%lx, irq %i", 
 917		card->shortname, p->base, p->irq);
 918
 919	mts64_cb.private = card;			 /* private */
 920	pardev = parport_register_dev_model(p,		 /* port */
 921					    DRIVER_NAME, /* name */
 922					    &mts64_cb,	 /* callbacks */
 923					    pdev->id);	 /* device number */
 924	if (!pardev) {
 925		dev_dbg(card->dev, "Cannot register pardevice\n");
 926		err = -EIO;
 927		goto __err;
 928	}
 929
 930	/* claim parport */
 931	if (parport_claim(pardev)) {
 932		dev_dbg(card->dev, "Cannot claim parport 0x%lx\n", pardev->port->base);
 933		err = -EIO;
 934		goto free_pardev;
 935	}
 936
 937	err = snd_mts64_create(card, pardev, &mts);
 938	if (err < 0) {
 939		dev_dbg(card->dev, "Cannot create main component\n");
 940		goto release_pardev;
 941	}
 942	card->private_data = mts;
 943	card->private_free = snd_mts64_card_private_free;
 944
 945	err = mts64_probe(p);
 946	if (err) {
 947		err = -EIO;
 948		goto __err;
 949	}
 950	
 951	err = snd_mts64_rawmidi_create(card);
 952	if (err < 0) {
 953		dev_dbg(card->dev, "Creating Rawmidi component failed\n");
 954		goto __err;
 955	}
 956
 957	/* init device */
 958	err = mts64_device_init(p);
 959	if (err < 0)
 960		goto __err;
 961
 962	platform_set_drvdata(pdev, card);
 963
 964	/* At this point card will be usable */
 965	err = snd_card_register(card);
 966	if (err < 0) {
 967		dev_dbg(card->dev, "Cannot register card\n");
 968		goto __err;
 969	}
 970
 971	dev_info(card->dev, "ESI Miditerminal 4140 on 0x%lx\n", p->base);
 972	return 0;
 973
 974release_pardev:
 975	parport_release(pardev);
 976free_pardev:
 977	parport_unregister_device(pardev);
 978__err:
 979	snd_card_free(card);
 980	return err;
 981}
 982
 983static void snd_mts64_remove(struct platform_device *pdev)
 984{
 985	struct snd_card *card = platform_get_drvdata(pdev);
 986
 987	if (card)
 988		snd_card_free(card);
 989}
 990
 991static struct platform_driver snd_mts64_driver = {
 992	.probe  = snd_mts64_probe,
 993	.remove = snd_mts64_remove,
 994	.driver = {
 995		.name = PLATFORM_DRIVER,
 996	}
 997};
 998
 999/*********************************************************************
1000 * module init stuff
1001 *********************************************************************/
1002static void snd_mts64_unregister_all(void)
1003{
1004	int i;
1005
1006	for (i = 0; i < SNDRV_CARDS; ++i) {
1007		if (platform_devices[i]) {
1008			platform_device_unregister(platform_devices[i]);
1009			platform_devices[i] = NULL;
1010		}
1011	}		
1012	platform_driver_unregister(&snd_mts64_driver);
1013	parport_unregister_driver(&mts64_parport_driver);
1014}
1015
1016static int __init snd_mts64_module_init(void)
1017{
1018	int err;
1019
1020	err = platform_driver_register(&snd_mts64_driver);
1021	if (err < 0)
1022		return err;
1023
1024	if (parport_register_driver(&mts64_parport_driver) != 0) {
1025		platform_driver_unregister(&snd_mts64_driver);
1026		return -EIO;
1027	}
1028
1029	if (device_count == 0) {
1030		snd_mts64_unregister_all();
1031		return -ENODEV;
1032	}
1033
1034	return 0;
1035}
1036
1037static void __exit snd_mts64_module_exit(void)
1038{
1039	snd_mts64_unregister_all();
1040}
1041
1042module_init(snd_mts64_module_init);
1043module_exit(snd_mts64_module_exit);
~/ Read-only file preview.