GNU Linux-libre 4.4.297-gnu1
[releases.git] / sound / firewire / digi00x / digi00x.c
1 /*
2  * digi00x.c - a part of driver for Digidesign Digi 002/003 family
3  *
4  * Copyright (c) 2014-2015 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8
9 #include "digi00x.h"
10
11 MODULE_DESCRIPTION("Digidesign Digi 002/003 family Driver");
12 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
13 MODULE_LICENSE("GPL v2");
14
15 #define VENDOR_DIGIDESIGN       0x00a07e
16 #define MODEL_CONSOLE           0x000001
17 #define MODEL_RACK              0x000002
18 #define SPEC_VERSION            0x000001
19
20 static int name_card(struct snd_dg00x *dg00x)
21 {
22         struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
23         char name[32] = {0};
24         char *model;
25         int err;
26
27         err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
28                             sizeof(name));
29         if (err < 0)
30                 return err;
31
32         model = skip_spaces(name);
33
34         strcpy(dg00x->card->driver, "Digi00x");
35         strcpy(dg00x->card->shortname, model);
36         strcpy(dg00x->card->mixername, model);
37         snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
38                  "Digidesign %s, GUID %08x%08x at %s, S%d", model,
39                  fw_dev->config_rom[3], fw_dev->config_rom[4],
40                  dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
41
42         return 0;
43 }
44
45 static void dg00x_card_free(struct snd_card *card)
46 {
47         struct snd_dg00x *dg00x = card->private_data;
48
49         snd_dg00x_stream_destroy_duplex(dg00x);
50         snd_dg00x_transaction_unregister(dg00x);
51
52         fw_unit_put(dg00x->unit);
53
54         mutex_destroy(&dg00x->mutex);
55 }
56
57 static int snd_dg00x_probe(struct fw_unit *unit,
58                            const struct ieee1394_device_id *entry)
59 {
60         struct snd_card *card;
61         struct snd_dg00x *dg00x;
62         int err;
63
64         /* create card */
65         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
66                            sizeof(struct snd_dg00x), &card);
67         if (err < 0)
68                 return err;
69         card->private_free = dg00x_card_free;
70
71         /* initialize myself */
72         dg00x = card->private_data;
73         dg00x->card = card;
74         dg00x->unit = fw_unit_get(unit);
75
76         mutex_init(&dg00x->mutex);
77         spin_lock_init(&dg00x->lock);
78         init_waitqueue_head(&dg00x->hwdep_wait);
79
80         dg00x->is_console = entry->model_id == MODEL_CONSOLE;
81
82         err = name_card(dg00x);
83         if (err < 0)
84                 goto error;
85
86         err = snd_dg00x_stream_init_duplex(dg00x);
87         if (err < 0)
88                 goto error;
89
90         snd_dg00x_proc_init(dg00x);
91
92         err = snd_dg00x_create_pcm_devices(dg00x);
93         if (err < 0)
94                 goto error;
95
96         err = snd_dg00x_create_midi_devices(dg00x);
97         if (err < 0)
98                 goto error;
99
100         err = snd_dg00x_create_hwdep_device(dg00x);
101         if (err < 0)
102                 goto error;
103
104         err = snd_dg00x_transaction_register(dg00x);
105         if (err < 0)
106                 goto error;
107
108         err = snd_card_register(card);
109         if (err < 0)
110                 goto error;
111
112         dev_set_drvdata(&unit->device, dg00x);
113
114         return err;
115 error:
116         snd_card_free(card);
117         return err;
118 }
119
120 static void snd_dg00x_update(struct fw_unit *unit)
121 {
122         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
123
124         snd_dg00x_transaction_reregister(dg00x);
125
126         mutex_lock(&dg00x->mutex);
127         snd_dg00x_stream_update_duplex(dg00x);
128         mutex_unlock(&dg00x->mutex);
129 }
130
131 static void snd_dg00x_remove(struct fw_unit *unit)
132 {
133         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
134
135         /* No need to wait for releasing card object in this context. */
136         snd_card_free_when_closed(dg00x->card);
137 }
138
139 static const struct ieee1394_device_id snd_dg00x_id_table[] = {
140         /* Both of 002/003 use the same ID. */
141         {
142                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
143                                IEEE1394_MATCH_VERSION |
144                                IEEE1394_MATCH_MODEL_ID,
145                 .vendor_id = VENDOR_DIGIDESIGN,
146                 .version = SPEC_VERSION,
147                 .model_id = MODEL_CONSOLE,
148         },
149         {
150                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
151                                IEEE1394_MATCH_VERSION |
152                                IEEE1394_MATCH_MODEL_ID,
153                 .vendor_id = VENDOR_DIGIDESIGN,
154                 .version = SPEC_VERSION,
155                 .model_id = MODEL_RACK,
156         },
157         {}
158 };
159 MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
160
161 static struct fw_driver dg00x_driver = {
162         .driver = {
163                 .owner = THIS_MODULE,
164                 .name = "snd-firewire-digi00x",
165                 .bus = &fw_bus_type,
166         },
167         .probe    = snd_dg00x_probe,
168         .update   = snd_dg00x_update,
169         .remove   = snd_dg00x_remove,
170         .id_table = snd_dg00x_id_table,
171 };
172
173 static int __init snd_dg00x_init(void)
174 {
175         return driver_register(&dg00x_driver.driver);
176 }
177
178 static void __exit snd_dg00x_exit(void)
179 {
180         driver_unregister(&dg00x_driver.driver);
181 }
182
183 module_init(snd_dg00x_init);
184 module_exit(snd_dg00x_exit);