oxfw.c 11 KB

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