oxfw.c 11 KB

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