166c1d43123f824a08dd377d08de119ea02b302a
[firefly-linux-kernel-4.4.55.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_DIGI00X           0x000002
17
18 static int name_card(struct snd_dg00x *dg00x)
19 {
20         struct fw_device *fw_dev = fw_parent_device(dg00x->unit);
21         char name[32] = {0};
22         char *model;
23         int err;
24
25         err = fw_csr_string(dg00x->unit->directory, CSR_MODEL, name,
26                             sizeof(name));
27         if (err < 0)
28                 return err;
29
30         model = skip_spaces(name);
31
32         strcpy(dg00x->card->driver, "Digi00x");
33         strcpy(dg00x->card->shortname, model);
34         strcpy(dg00x->card->mixername, model);
35         snprintf(dg00x->card->longname, sizeof(dg00x->card->longname),
36                  "Digidesign %s, GUID %08x%08x at %s, S%d", model,
37                  cpu_to_be32(fw_dev->config_rom[3]),
38                  cpu_to_be32(fw_dev->config_rom[4]),
39                  dev_name(&dg00x->unit->device), 100 << fw_dev->max_speed);
40
41         return 0;
42 }
43
44 static void dg00x_card_free(struct snd_card *card)
45 {
46         struct snd_dg00x *dg00x = card->private_data;
47
48         snd_dg00x_stream_destroy_duplex(dg00x);
49
50         fw_unit_put(dg00x->unit);
51
52         mutex_destroy(&dg00x->mutex);
53 }
54
55 static int snd_dg00x_probe(struct fw_unit *unit,
56                            const struct ieee1394_device_id *entry)
57 {
58         struct snd_card *card;
59         struct snd_dg00x *dg00x;
60         int err;
61
62         /* create card */
63         err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
64                            sizeof(struct snd_dg00x), &card);
65         if (err < 0)
66                 return err;
67         card->private_free = dg00x_card_free;
68
69         /* initialize myself */
70         dg00x = card->private_data;
71         dg00x->card = card;
72         dg00x->unit = fw_unit_get(unit);
73
74         mutex_init(&dg00x->mutex);
75
76         err = name_card(dg00x);
77         if (err < 0)
78                 goto error;
79
80         err = snd_dg00x_stream_init_duplex(dg00x);
81         if (err < 0)
82                 goto error;
83
84         snd_dg00x_proc_init(dg00x);
85
86         err = snd_dg00x_create_pcm_devices(dg00x);
87         if (err < 0)
88                 goto error;
89
90         err = snd_card_register(card);
91         if (err < 0)
92                 goto error;
93
94         dev_set_drvdata(&unit->device, dg00x);
95
96         return err;
97 error:
98         snd_card_free(card);
99         return err;
100 }
101
102 static void snd_dg00x_update(struct fw_unit *unit)
103 {
104         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
105
106         mutex_lock(&dg00x->mutex);
107         snd_dg00x_stream_update_duplex(dg00x);
108         mutex_unlock(&dg00x->mutex);
109 }
110
111 static void snd_dg00x_remove(struct fw_unit *unit)
112 {
113         struct snd_dg00x *dg00x = dev_get_drvdata(&unit->device);
114
115         /* No need to wait for releasing card object in this context. */
116         snd_card_free_when_closed(dg00x->card);
117 }
118
119 static const struct ieee1394_device_id snd_dg00x_id_table[] = {
120         /* Both of 002/003 use the same ID. */
121         {
122                 .match_flags = IEEE1394_MATCH_VENDOR_ID |
123                                IEEE1394_MATCH_MODEL_ID,
124                 .vendor_id = VENDOR_DIGIDESIGN,
125                 .model_id = MODEL_DIGI00X,
126         },
127         {}
128 };
129 MODULE_DEVICE_TABLE(ieee1394, snd_dg00x_id_table);
130
131 static struct fw_driver dg00x_driver = {
132         .driver = {
133                 .owner = THIS_MODULE,
134                 .name = "snd-firewire-digi00x",
135                 .bus = &fw_bus_type,
136         },
137         .probe    = snd_dg00x_probe,
138         .update   = snd_dg00x_update,
139         .remove   = snd_dg00x_remove,
140         .id_table = snd_dg00x_id_table,
141 };
142
143 static int __init snd_dg00x_init(void)
144 {
145         return driver_register(&dg00x_driver.driver);
146 }
147
148 static void __exit snd_dg00x_exit(void)
149 {
150         driver_unregister(&dg00x_driver.driver);
151 }
152
153 module_init(snd_dg00x_init);
154 module_exit(snd_dg00x_exit);