oxfw-scs1x.c 10 KB

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