oxfw-scs1x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * oxfw-scs1x.c - a part of driver for OXFW970/971 based devices
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  6. * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  7. */
  8. #include "oxfw.h"
  9. #define HSS1394_ADDRESS 0xc007dedadadaULL
  10. #define HSS1394_MAX_PACKET_SIZE 64
  11. #define HSS1394_TAG_USER_DATA 0x00
  12. #define HSS1394_TAG_CHANGE_ADDRESS 0xf1
  13. struct fw_scs1x {
  14. struct fw_address_handler hss_handler;
  15. u8 input_escape_count;
  16. struct snd_rawmidi_substream *input;
  17. /* For MIDI playback. */
  18. struct snd_rawmidi_substream *output;
  19. bool output_idle;
  20. u8 output_status;
  21. u8 output_bytes;
  22. bool output_escaped;
  23. bool output_escape_high_nibble;
  24. struct work_struct work;
  25. wait_queue_head_t idle_wait;
  26. u8 buffer[HSS1394_MAX_PACKET_SIZE];
  27. bool transaction_running;
  28. struct fw_transaction transaction;
  29. unsigned int transaction_bytes;
  30. bool error;
  31. struct fw_device *fw_dev;
  32. };
  33. static const u8 sysex_escape_prefix[] = {
  34. 0xf0, /* SysEx begin */
  35. 0x00, 0x01, 0x60, /* Stanton DJ */
  36. 0x48, 0x53, 0x53, /* "HSS" */
  37. };
  38. static void midi_input_escaped_byte(struct snd_rawmidi_substream *stream,
  39. u8 byte)
  40. {
  41. u8 nibbles[2];
  42. nibbles[0] = byte >> 4;
  43. nibbles[1] = byte & 0x0f;
  44. snd_rawmidi_receive(stream, nibbles, 2);
  45. }
  46. static void midi_input_byte(struct fw_scs1x *scs,
  47. struct snd_rawmidi_substream *stream, u8 byte)
  48. {
  49. const u8 eox = 0xf7;
  50. if (scs->input_escape_count > 0) {
  51. midi_input_escaped_byte(stream, byte);
  52. scs->input_escape_count--;
  53. if (scs->input_escape_count == 0)
  54. snd_rawmidi_receive(stream, &eox, sizeof(eox));
  55. } else if (byte == 0xf9) {
  56. snd_rawmidi_receive(stream, sysex_escape_prefix,
  57. ARRAY_SIZE(sysex_escape_prefix));
  58. midi_input_escaped_byte(stream, 0x00);
  59. midi_input_escaped_byte(stream, 0xf9);
  60. scs->input_escape_count = 3;
  61. } else {
  62. snd_rawmidi_receive(stream, &byte, 1);
  63. }
  64. }
  65. static void midi_input_packet(struct fw_scs1x *scs,
  66. struct snd_rawmidi_substream *stream,
  67. const u8 *data, unsigned int bytes)
  68. {
  69. unsigned int i;
  70. const u8 eox = 0xf7;
  71. if (data[0] == HSS1394_TAG_USER_DATA) {
  72. for (i = 1; i < bytes; ++i)
  73. midi_input_byte(scs, stream, data[i]);
  74. } else {
  75. snd_rawmidi_receive(stream, sysex_escape_prefix,
  76. ARRAY_SIZE(sysex_escape_prefix));
  77. for (i = 0; i < bytes; ++i)
  78. midi_input_escaped_byte(stream, data[i]);
  79. snd_rawmidi_receive(stream, &eox, sizeof(eox));
  80. }
  81. }
  82. static void handle_hss(struct fw_card *card, struct fw_request *request,
  83. int tcode, int destination, int source, int generation,
  84. unsigned long long offset, void *data, size_t length,
  85. void *callback_data)
  86. {
  87. struct fw_scs1x *scs = callback_data;
  88. struct snd_rawmidi_substream *stream;
  89. int rcode;
  90. if (offset != scs->hss_handler.offset) {
  91. rcode = RCODE_ADDRESS_ERROR;
  92. goto end;
  93. }
  94. if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
  95. tcode != TCODE_WRITE_BLOCK_REQUEST) {
  96. rcode = RCODE_TYPE_ERROR;
  97. goto end;
  98. }
  99. if (length >= 1) {
  100. stream = READ_ONCE(scs->input);
  101. if (stream)
  102. midi_input_packet(scs, stream, data, length);
  103. }
  104. rcode = RCODE_COMPLETE;
  105. end:
  106. fw_send_response(card, request, rcode);
  107. }
  108. static void scs_write_callback(struct fw_card *card, int rcode,
  109. void *data, size_t length, void *callback_data)
  110. {
  111. struct fw_scs1x *scs = callback_data;
  112. if (!rcode_is_permanent_error(rcode)) {
  113. /* Don't retry for this data. */
  114. if (rcode == RCODE_COMPLETE)
  115. scs->transaction_bytes = 0;
  116. } else {
  117. scs->error = true;
  118. }
  119. scs->transaction_running = false;
  120. schedule_work(&scs->work);
  121. }
  122. static bool is_valid_running_status(u8 status)
  123. {
  124. return status >= 0x80 && status <= 0xef;
  125. }
  126. static bool is_one_byte_cmd(u8 status)
  127. {
  128. return status == 0xf6 ||
  129. status >= 0xf8;
  130. }
  131. static bool is_two_bytes_cmd(u8 status)
  132. {
  133. return (status >= 0xc0 && status <= 0xdf) ||
  134. status == 0xf1 ||
  135. status == 0xf3;
  136. }
  137. static bool is_three_bytes_cmd(u8 status)
  138. {
  139. return (status >= 0x80 && status <= 0xbf) ||
  140. (status >= 0xe0 && status <= 0xef) ||
  141. status == 0xf2;
  142. }
  143. static bool is_invalid_cmd(u8 status)
  144. {
  145. return status == 0xf4 ||
  146. status == 0xf5 ||
  147. status == 0xf9 ||
  148. status == 0xfd;
  149. }
  150. static void scs_output_work(struct work_struct *work)
  151. {
  152. struct fw_scs1x *scs = container_of(work, struct fw_scs1x, work);
  153. struct snd_rawmidi_substream *stream;
  154. unsigned int i;
  155. u8 byte;
  156. int generation;
  157. if (scs->transaction_running)
  158. return;
  159. stream = READ_ONCE(scs->output);
  160. if (!stream || scs->error) {
  161. scs->output_idle = true;
  162. wake_up(&scs->idle_wait);
  163. return;
  164. }
  165. if (scs->transaction_bytes > 0)
  166. goto retry;
  167. i = scs->output_bytes;
  168. for (;;) {
  169. if (snd_rawmidi_transmit(stream, &byte, 1) != 1) {
  170. scs->output_bytes = i;
  171. scs->output_idle = true;
  172. wake_up(&scs->idle_wait);
  173. return;
  174. }
  175. /*
  176. * Convert from real MIDI to what I think the device expects (no
  177. * running status, one command per packet, unescaped SysExs).
  178. */
  179. if (scs->output_escaped && byte < 0x80) {
  180. if (scs->output_escape_high_nibble) {
  181. if (i < HSS1394_MAX_PACKET_SIZE) {
  182. scs->buffer[i] = byte << 4;
  183. scs->output_escape_high_nibble = false;
  184. }
  185. } else {
  186. scs->buffer[i++] |= byte & 0x0f;
  187. scs->output_escape_high_nibble = true;
  188. }
  189. } else if (byte < 0x80) {
  190. if (i == 1) {
  191. if (!is_valid_running_status(
  192. scs->output_status))
  193. continue;
  194. scs->buffer[0] = HSS1394_TAG_USER_DATA;
  195. scs->buffer[i++] = scs->output_status;
  196. }
  197. scs->buffer[i++] = byte;
  198. if ((i == 3 && is_two_bytes_cmd(scs->output_status)) ||
  199. (i == 4 && is_three_bytes_cmd(scs->output_status)))
  200. break;
  201. if (i == 1 + ARRAY_SIZE(sysex_escape_prefix) &&
  202. !memcmp(scs->buffer + 1, sysex_escape_prefix,
  203. ARRAY_SIZE(sysex_escape_prefix))) {
  204. scs->output_escaped = true;
  205. scs->output_escape_high_nibble = true;
  206. i = 0;
  207. }
  208. if (i >= HSS1394_MAX_PACKET_SIZE)
  209. i = 1;
  210. } else if (byte == 0xf7) {
  211. if (scs->output_escaped) {
  212. if (i >= 1 && scs->output_escape_high_nibble &&
  213. scs->buffer[0] !=
  214. HSS1394_TAG_CHANGE_ADDRESS)
  215. break;
  216. } else {
  217. if (i > 1 && scs->output_status == 0xf0) {
  218. scs->buffer[i++] = 0xf7;
  219. break;
  220. }
  221. }
  222. i = 1;
  223. scs->output_escaped = false;
  224. } else if (!is_invalid_cmd(byte) && byte < 0xf8) {
  225. i = 1;
  226. scs->buffer[0] = HSS1394_TAG_USER_DATA;
  227. scs->buffer[i++] = byte;
  228. scs->output_status = byte;
  229. scs->output_escaped = false;
  230. if (is_one_byte_cmd(byte))
  231. break;
  232. }
  233. }
  234. scs->output_bytes = 1;
  235. scs->output_escaped = false;
  236. scs->transaction_bytes = i;
  237. retry:
  238. scs->transaction_running = true;
  239. generation = scs->fw_dev->generation;
  240. smp_rmb(); /* node_id vs. generation */
  241. fw_send_request(scs->fw_dev->card, &scs->transaction,
  242. TCODE_WRITE_BLOCK_REQUEST, scs->fw_dev->node_id,
  243. generation, scs->fw_dev->max_speed, HSS1394_ADDRESS,
  244. scs->buffer, scs->transaction_bytes,
  245. scs_write_callback, scs);
  246. }
  247. static int midi_capture_open(struct snd_rawmidi_substream *stream)
  248. {
  249. return 0;
  250. }
  251. static int midi_capture_close(struct snd_rawmidi_substream *stream)
  252. {
  253. return 0;
  254. }
  255. static void midi_capture_trigger(struct snd_rawmidi_substream *stream, int up)
  256. {
  257. struct fw_scs1x *scs = stream->rmidi->private_data;
  258. if (up) {
  259. scs->input_escape_count = 0;
  260. WRITE_ONCE(scs->input, stream);
  261. } else {
  262. WRITE_ONCE(scs->input, NULL);
  263. }
  264. }
  265. static int midi_playback_open(struct snd_rawmidi_substream *stream)
  266. {
  267. return 0;
  268. }
  269. static int midi_playback_close(struct snd_rawmidi_substream *stream)
  270. {
  271. return 0;
  272. }
  273. static void midi_playback_trigger(struct snd_rawmidi_substream *stream, int up)
  274. {
  275. struct fw_scs1x *scs = stream->rmidi->private_data;
  276. if (up) {
  277. scs->output_status = 0;
  278. scs->output_bytes = 1;
  279. scs->output_escaped = false;
  280. scs->output_idle = false;
  281. scs->transaction_bytes = 0;
  282. scs->error = false;
  283. WRITE_ONCE(scs->output, stream);
  284. schedule_work(&scs->work);
  285. } else {
  286. WRITE_ONCE(scs->output, NULL);
  287. }
  288. }
  289. static void midi_playback_drain(struct snd_rawmidi_substream *stream)
  290. {
  291. struct fw_scs1x *scs = stream->rmidi->private_data;
  292. wait_event(scs->idle_wait, scs->output_idle);
  293. }
  294. static int register_address(struct snd_oxfw *oxfw)
  295. {
  296. struct fw_scs1x *scs = oxfw->spec;
  297. __be64 data;
  298. data = cpu_to_be64(((u64)HSS1394_TAG_CHANGE_ADDRESS << 56) |
  299. scs->hss_handler.offset);
  300. return snd_fw_transaction(oxfw->unit, TCODE_WRITE_BLOCK_REQUEST,
  301. HSS1394_ADDRESS, &data, sizeof(data), 0);
  302. }
  303. static void remove_scs1x(struct snd_rawmidi *rmidi)
  304. {
  305. struct fw_scs1x *scs = rmidi->private_data;
  306. fw_core_remove_address_handler(&scs->hss_handler);
  307. }
  308. void snd_oxfw_scs1x_update(struct snd_oxfw *oxfw)
  309. {
  310. register_address(oxfw);
  311. }
  312. int snd_oxfw_scs1x_add(struct snd_oxfw *oxfw)
  313. {
  314. static const struct snd_rawmidi_ops midi_capture_ops = {
  315. .open = midi_capture_open,
  316. .close = midi_capture_close,
  317. .trigger = midi_capture_trigger,
  318. };
  319. static const struct snd_rawmidi_ops midi_playback_ops = {
  320. .open = midi_playback_open,
  321. .close = midi_playback_close,
  322. .trigger = midi_playback_trigger,
  323. .drain = midi_playback_drain,
  324. };
  325. struct snd_rawmidi *rmidi;
  326. struct fw_scs1x *scs;
  327. int err;
  328. scs = devm_kzalloc(&oxfw->card->card_dev, sizeof(struct fw_scs1x),
  329. GFP_KERNEL);
  330. if (!scs)
  331. return -ENOMEM;
  332. scs->fw_dev = fw_parent_device(oxfw->unit);
  333. oxfw->spec = scs;
  334. /* Allocate own handler for imcoming asynchronous transaction. */
  335. scs->hss_handler.length = HSS1394_MAX_PACKET_SIZE;
  336. scs->hss_handler.address_callback = handle_hss;
  337. scs->hss_handler.callback_data = scs;
  338. err = fw_core_add_address_handler(&scs->hss_handler,
  339. &fw_high_memory_region);
  340. if (err < 0)
  341. return err;
  342. err = register_address(oxfw);
  343. if (err < 0)
  344. goto err_allocated;
  345. /* Use unique name for backward compatibility to scs1x module. */
  346. err = snd_rawmidi_new(oxfw->card, "SCS.1x", 0, 1, 1, &rmidi);
  347. if (err < 0)
  348. goto err_allocated;
  349. rmidi->private_data = scs;
  350. rmidi->private_free = remove_scs1x;
  351. snprintf(rmidi->name, sizeof(rmidi->name),
  352. "%s MIDI", oxfw->card->shortname);
  353. rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
  354. SNDRV_RAWMIDI_INFO_OUTPUT |
  355. SNDRV_RAWMIDI_INFO_DUPLEX;
  356. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  357. &midi_capture_ops);
  358. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  359. &midi_playback_ops);
  360. INIT_WORK(&scs->work, scs_output_work);
  361. init_waitqueue_head(&scs->idle_wait);
  362. scs->output_idle = true;
  363. return 0;
  364. err_allocated:
  365. fw_core_remove_address_handler(&scs->hss_handler);
  366. return err;
  367. }