oxfw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * oxfw.c - a part of driver for OXFW970/971 based devices
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include "oxfw.h"
  8. #define OXFORD_FIRMWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x50000)
  9. /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
  10. #define OXFORD_HARDWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x90020)
  11. #define OXFORD_HARDWARE_ID_OXFW970 0x39443841
  12. #define OXFORD_HARDWARE_ID_OXFW971 0x39373100
  13. #define VENDOR_LOUD 0x000ff2
  14. #define VENDOR_GRIFFIN 0x001292
  15. #define VENDOR_BEHRINGER 0x001564
  16. #define VENDOR_LACIE 0x00d04b
  17. #define VENDOR_TASCAM 0x00022e
  18. #define OUI_STANTON 0x001260
  19. #define OUI_APOGEE 0x0003db
  20. #define MODEL_SATELLITE 0x00200f
  21. #define SPECIFIER_1394TA 0x00a02d
  22. #define VERSION_AVC 0x010001
  23. MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
  24. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  25. MODULE_LICENSE("GPL v2");
  26. MODULE_ALIAS("snd-firewire-speakers");
  27. MODULE_ALIAS("snd-scs1x");
  28. struct compat_info {
  29. const char *driver_name;
  30. const char *vendor_name;
  31. const char *model_name;
  32. };
  33. static bool detect_loud_models(struct fw_unit *unit)
  34. {
  35. const char *const models[] = {
  36. "Onyxi",
  37. "Onyx-i",
  38. "Onyx 1640i",
  39. "d.Pro",
  40. "Mackie Onyx Satellite",
  41. "Tapco LINK.firewire 4x6",
  42. "U.420"};
  43. char model[32];
  44. int err;
  45. err = fw_csr_string(unit->directory, CSR_MODEL,
  46. model, sizeof(model));
  47. if (err < 0)
  48. return false;
  49. return match_string(models, ARRAY_SIZE(models), model) >= 0;
  50. }
  51. static int name_card(struct snd_oxfw *oxfw)
  52. {
  53. struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
  54. const struct compat_info *info;
  55. char vendor[24];
  56. char model[32];
  57. const char *d, *v, *m;
  58. u32 firmware;
  59. int err;
  60. /* get vendor name from root directory */
  61. err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
  62. vendor, sizeof(vendor));
  63. if (err < 0)
  64. goto end;
  65. /* get model name from unit directory */
  66. err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
  67. model, sizeof(model));
  68. if (err < 0)
  69. goto end;
  70. err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
  71. OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
  72. if (err < 0)
  73. goto end;
  74. be32_to_cpus(&firmware);
  75. /* to apply card definitions */
  76. if (oxfw->entry->vendor_id == VENDOR_GRIFFIN ||
  77. oxfw->entry->vendor_id == VENDOR_LACIE) {
  78. info = (const struct compat_info *)oxfw->entry->driver_data;
  79. d = info->driver_name;
  80. v = info->vendor_name;
  81. m = info->model_name;
  82. } else {
  83. d = "OXFW";
  84. v = vendor;
  85. m = model;
  86. }
  87. strcpy(oxfw->card->driver, d);
  88. strcpy(oxfw->card->mixername, m);
  89. strcpy(oxfw->card->shortname, m);
  90. snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
  91. "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
  92. v, m, firmware >> 20, firmware & 0xffff,
  93. fw_dev->config_rom[3], fw_dev->config_rom[4],
  94. dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
  95. end:
  96. return err;
  97. }
  98. static void oxfw_free(struct snd_oxfw *oxfw)
  99. {
  100. unsigned int i;
  101. snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
  102. if (oxfw->has_output)
  103. snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
  104. fw_unit_put(oxfw->unit);
  105. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
  106. kfree(oxfw->tx_stream_formats[i]);
  107. kfree(oxfw->rx_stream_formats[i]);
  108. }
  109. kfree(oxfw->spec);
  110. mutex_destroy(&oxfw->mutex);
  111. kfree(oxfw);
  112. }
  113. /*
  114. * This module releases the FireWire unit data after all ALSA character devices
  115. * are released by applications. This is for releasing stream data or finishing
  116. * transactions safely. Thus at returning from .remove(), this module still keep
  117. * references for the unit.
  118. */
  119. static void oxfw_card_free(struct snd_card *card)
  120. {
  121. oxfw_free(card->private_data);
  122. }
  123. static int detect_quirks(struct snd_oxfw *oxfw)
  124. {
  125. struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
  126. struct fw_csr_iterator it;
  127. int key, val;
  128. int vendor, model;
  129. /*
  130. * Add ALSA control elements for two models to keep compatibility to
  131. * old firewire-speaker module.
  132. */
  133. if (oxfw->entry->vendor_id == VENDOR_GRIFFIN)
  134. return snd_oxfw_add_spkr(oxfw, false);
  135. if (oxfw->entry->vendor_id == VENDOR_LACIE)
  136. return snd_oxfw_add_spkr(oxfw, true);
  137. /*
  138. * Stanton models supports asynchronous transactions for unique MIDI
  139. * messages.
  140. */
  141. if (oxfw->entry->vendor_id == OUI_STANTON) {
  142. /* No physical MIDI ports. */
  143. oxfw->midi_input_ports = 0;
  144. oxfw->midi_output_ports = 0;
  145. return snd_oxfw_scs1x_add(oxfw);
  146. }
  147. /*
  148. * TASCAM FireOne has physical control and requires a pair of additional
  149. * MIDI ports.
  150. */
  151. if (oxfw->entry->vendor_id == VENDOR_TASCAM) {
  152. oxfw->midi_input_ports++;
  153. oxfw->midi_output_ports++;
  154. return 0;
  155. }
  156. /* Seek from Root Directory of Config ROM. */
  157. vendor = model = 0;
  158. fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
  159. while (fw_csr_iterator_next(&it, &key, &val)) {
  160. if (key == CSR_VENDOR)
  161. vendor = val;
  162. else if (key == CSR_MODEL)
  163. model = val;
  164. }
  165. /*
  166. * Mackie Onyx Satellite with base station has a quirk to report a wrong
  167. * value in 'dbs' field of CIP header against its format information.
  168. */
  169. if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
  170. oxfw->wrong_dbs = true;
  171. return 0;
  172. }
  173. static void do_registration(struct work_struct *work)
  174. {
  175. struct snd_oxfw *oxfw = container_of(work, struct snd_oxfw, dwork.work);
  176. int i;
  177. int err;
  178. if (oxfw->registered)
  179. return;
  180. err = snd_card_new(&oxfw->unit->device, -1, NULL, THIS_MODULE, 0,
  181. &oxfw->card);
  182. if (err < 0)
  183. return;
  184. err = name_card(oxfw);
  185. if (err < 0)
  186. goto error;
  187. err = snd_oxfw_stream_discover(oxfw);
  188. if (err < 0)
  189. goto error;
  190. err = detect_quirks(oxfw);
  191. if (err < 0)
  192. goto error;
  193. err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
  194. if (err < 0)
  195. goto error;
  196. if (oxfw->has_output) {
  197. err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
  198. if (err < 0)
  199. goto error;
  200. }
  201. err = snd_oxfw_create_pcm(oxfw);
  202. if (err < 0)
  203. goto error;
  204. snd_oxfw_proc_init(oxfw);
  205. err = snd_oxfw_create_midi(oxfw);
  206. if (err < 0)
  207. goto error;
  208. err = snd_oxfw_create_hwdep(oxfw);
  209. if (err < 0)
  210. goto error;
  211. err = snd_card_register(oxfw->card);
  212. if (err < 0)
  213. goto error;
  214. /*
  215. * After registered, oxfw instance can be released corresponding to
  216. * releasing the sound card instance.
  217. */
  218. oxfw->card->private_free = oxfw_card_free;
  219. oxfw->card->private_data = oxfw;
  220. oxfw->registered = true;
  221. return;
  222. error:
  223. snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
  224. if (oxfw->has_output)
  225. snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
  226. for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; ++i) {
  227. kfree(oxfw->tx_stream_formats[i]);
  228. oxfw->tx_stream_formats[i] = NULL;
  229. kfree(oxfw->rx_stream_formats[i]);
  230. oxfw->rx_stream_formats[i] = NULL;
  231. }
  232. snd_card_free(oxfw->card);
  233. kfree(oxfw->spec);
  234. oxfw->spec = NULL;
  235. dev_info(&oxfw->unit->device,
  236. "Sound card registration failed: %d\n", err);
  237. }
  238. static int oxfw_probe(struct fw_unit *unit,
  239. const struct ieee1394_device_id *entry)
  240. {
  241. struct snd_oxfw *oxfw;
  242. if (entry->vendor_id == VENDOR_LOUD && !detect_loud_models(unit))
  243. return -ENODEV;
  244. /* Allocate this independent of sound card instance. */
  245. oxfw = kzalloc(sizeof(struct snd_oxfw), GFP_KERNEL);
  246. if (oxfw == NULL)
  247. return -ENOMEM;
  248. oxfw->entry = entry;
  249. oxfw->unit = fw_unit_get(unit);
  250. dev_set_drvdata(&unit->device, oxfw);
  251. mutex_init(&oxfw->mutex);
  252. spin_lock_init(&oxfw->lock);
  253. init_waitqueue_head(&oxfw->hwdep_wait);
  254. /* Allocate and register this sound card later. */
  255. INIT_DEFERRABLE_WORK(&oxfw->dwork, do_registration);
  256. snd_fw_schedule_registration(unit, &oxfw->dwork);
  257. return 0;
  258. }
  259. static void oxfw_bus_reset(struct fw_unit *unit)
  260. {
  261. struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
  262. if (!oxfw->registered)
  263. snd_fw_schedule_registration(unit, &oxfw->dwork);
  264. fcp_bus_reset(oxfw->unit);
  265. if (oxfw->registered) {
  266. mutex_lock(&oxfw->mutex);
  267. snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
  268. if (oxfw->has_output)
  269. snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
  270. mutex_unlock(&oxfw->mutex);
  271. if (oxfw->entry->vendor_id == OUI_STANTON)
  272. snd_oxfw_scs1x_update(oxfw);
  273. }
  274. }
  275. static void oxfw_remove(struct fw_unit *unit)
  276. {
  277. struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
  278. /*
  279. * Confirm to stop the work for registration before the sound card is
  280. * going to be released. The work is not scheduled again because bus
  281. * reset handler is not called anymore.
  282. */
  283. cancel_delayed_work_sync(&oxfw->dwork);
  284. if (oxfw->registered) {
  285. /* No need to wait for releasing card object in this context. */
  286. snd_card_free_when_closed(oxfw->card);
  287. } else {
  288. /* Don't forget this case. */
  289. oxfw_free(oxfw);
  290. }
  291. }
  292. static const struct compat_info griffin_firewave = {
  293. .driver_name = "FireWave",
  294. .vendor_name = "Griffin",
  295. .model_name = "FireWave",
  296. };
  297. static const struct compat_info lacie_speakers = {
  298. .driver_name = "FWSpeakers",
  299. .vendor_name = "LaCie",
  300. .model_name = "FireWire Speakers",
  301. };
  302. static const struct ieee1394_device_id oxfw_id_table[] = {
  303. {
  304. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  305. IEEE1394_MATCH_MODEL_ID |
  306. IEEE1394_MATCH_SPECIFIER_ID |
  307. IEEE1394_MATCH_VERSION,
  308. .vendor_id = VENDOR_GRIFFIN,
  309. .model_id = 0x00f970,
  310. .specifier_id = SPECIFIER_1394TA,
  311. .version = VERSION_AVC,
  312. .driver_data = (kernel_ulong_t)&griffin_firewave,
  313. },
  314. {
  315. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  316. IEEE1394_MATCH_MODEL_ID |
  317. IEEE1394_MATCH_SPECIFIER_ID |
  318. IEEE1394_MATCH_VERSION,
  319. .vendor_id = VENDOR_LACIE,
  320. .model_id = 0x00f970,
  321. .specifier_id = SPECIFIER_1394TA,
  322. .version = VERSION_AVC,
  323. .driver_data = (kernel_ulong_t)&lacie_speakers,
  324. },
  325. /* Behringer,F-Control Audio 202 */
  326. {
  327. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  328. IEEE1394_MATCH_MODEL_ID,
  329. .vendor_id = VENDOR_BEHRINGER,
  330. .model_id = 0x00fc22,
  331. },
  332. /*
  333. * Any Mackie(Loud) models (name string/model id):
  334. * Onyx-i series (former models): 0x081216
  335. * Mackie Onyx Satellite: 0x00200f
  336. * Tapco LINK.firewire 4x6: 0x000460
  337. * d.4 pro: Unknown
  338. * U.420: Unknown
  339. * U.420d: Unknown
  340. */
  341. {
  342. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  343. IEEE1394_MATCH_SPECIFIER_ID |
  344. IEEE1394_MATCH_VERSION,
  345. .vendor_id = VENDOR_LOUD,
  346. .specifier_id = SPECIFIER_1394TA,
  347. .version = VERSION_AVC,
  348. },
  349. /* TASCAM, FireOne */
  350. {
  351. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  352. IEEE1394_MATCH_MODEL_ID,
  353. .vendor_id = VENDOR_TASCAM,
  354. .model_id = 0x800007,
  355. },
  356. /* Stanton, Stanton Controllers & Systems 1 Mixer (SCS.1m) */
  357. {
  358. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  359. IEEE1394_MATCH_MODEL_ID,
  360. .vendor_id = OUI_STANTON,
  361. .model_id = 0x001000,
  362. },
  363. /* Stanton, Stanton Controllers & Systems 1 Deck (SCS.1d) */
  364. {
  365. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  366. IEEE1394_MATCH_MODEL_ID,
  367. .vendor_id = OUI_STANTON,
  368. .model_id = 0x002000,
  369. },
  370. // APOGEE, duet FireWire
  371. {
  372. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  373. IEEE1394_MATCH_MODEL_ID,
  374. .vendor_id = OUI_APOGEE,
  375. .model_id = 0x01dddd,
  376. },
  377. { }
  378. };
  379. MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
  380. static struct fw_driver oxfw_driver = {
  381. .driver = {
  382. .owner = THIS_MODULE,
  383. .name = KBUILD_MODNAME,
  384. .bus = &fw_bus_type,
  385. },
  386. .probe = oxfw_probe,
  387. .update = oxfw_bus_reset,
  388. .remove = oxfw_remove,
  389. .id_table = oxfw_id_table,
  390. };
  391. static int __init snd_oxfw_init(void)
  392. {
  393. return driver_register(&oxfw_driver.driver);
  394. }
  395. static void __exit snd_oxfw_exit(void)
  396. {
  397. driver_unregister(&oxfw_driver.driver);
  398. }
  399. module_init(snd_oxfw_init);
  400. module_exit(snd_oxfw_exit);