├── .github
└── FUNDING.yml
├── .gitignore
├── COPYING
├── Makefile
├── README.md
├── main.c
├── scarlett2-firmware.c
├── scarlett2-firmware.h
├── scarlett2-ioctls.c
├── scarlett2-ioctls.h
└── scarlett2.h
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | liberapay: gdb
2 | custom: 'https://www.paypal.me/gdbau'
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.deps
2 | /firmware
3 | *.o
4 | scarlett2
5 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright © 2007 Free Software Foundation, Inc.
5 |
6 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for software and other kinds of works.
11 |
12 | The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.
13 |
14 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
15 |
16 | To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.
17 |
18 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
19 |
20 | Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it.
21 |
22 | For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions.
23 |
24 | Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users.
25 |
26 | Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free.
27 |
28 | The precise terms and conditions for copying, distribution and modification follow.
29 |
30 | TERMS AND CONDITIONS
31 |
32 | 0. Definitions.
33 |
34 | “This License” refers to version 3 of the GNU General Public License.
35 |
36 | “Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
37 |
38 | “The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.
39 |
40 | To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work.
41 |
42 | A “covered work” means either the unmodified Program or a work based on the Program.
43 |
44 | To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
45 |
46 | To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
47 |
48 | An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
49 |
50 | 1. Source Code.
51 | The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
52 |
53 | A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
54 |
55 | The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
56 |
57 | The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
58 |
59 | The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
60 |
61 | The Corresponding Source for a work in source code form is that same work.
62 |
63 | 2. Basic Permissions.
64 | All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
65 |
66 | You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
67 |
68 | Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
69 |
70 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
71 | No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
72 |
73 | When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
74 |
75 | 4. Conveying Verbatim Copies.
76 | You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
77 |
78 | You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
79 |
80 | 5. Conveying Modified Source Versions.
81 | You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
82 |
83 | a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
84 |
85 | b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”.
86 |
87 | c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
88 |
89 | d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
90 |
91 | A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
92 |
93 | 6. Conveying Non-Source Forms.
94 | You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
95 |
96 | a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
97 |
98 | b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
99 |
100 | c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
101 |
102 | d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
103 |
104 | e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
105 |
106 | A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
107 |
108 | A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
109 |
110 | “Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
111 |
112 | If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
113 |
114 | The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
115 |
116 | Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
117 |
118 | 7. Additional Terms.
119 | “Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
120 |
121 | When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
122 |
123 | Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
124 |
125 | a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
126 |
127 | b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
128 |
129 | c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
130 |
131 | d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
132 |
133 | e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
134 |
135 | f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
136 |
137 | All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
138 |
139 | If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
140 |
141 | Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
142 |
143 | 8. Termination.
144 | You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
145 |
146 | However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
147 |
148 | Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
149 |
150 | Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
151 |
152 | 9. Acceptance Not Required for Having Copies.
153 | You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
154 |
155 | 10. Automatic Licensing of Downstream Recipients.
156 | Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
157 |
158 | An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
159 |
160 | You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
161 |
162 | 11. Patents.
163 | A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”.
164 |
165 | A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
166 |
167 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
168 |
169 | In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
170 |
171 | If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
172 |
173 | If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
174 |
175 | A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
176 |
177 | Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
178 |
179 | 12. No Surrender of Others' Freedom.
180 | If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
181 |
182 | 13. Use with the GNU Affero General Public License.
183 | Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
184 |
185 | 14. Revised Versions of this License.
186 | The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
187 |
188 | Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
189 |
190 | If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
191 |
192 | Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
193 |
194 | 15. Disclaimer of Warranty.
195 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
196 |
197 | 16. Limitation of Liability.
198 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
199 |
200 | 17. Interpretation of Sections 15 and 16.
201 | If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
202 |
203 | END OF TERMS AND CONDITIONS
204 |
205 | How to Apply These Terms to Your New Programs
206 |
207 | If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
208 |
209 | To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
210 |
211 |
212 | Copyright (C)
213 |
214 | This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
215 |
216 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
217 |
218 | You should have received a copy of the GNU General Public License along with this program. If not, see .
219 |
220 | Also add information on how to contact you by electronic and paper mail.
221 |
222 | If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
223 |
224 | Copyright (C)
225 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
226 | This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
227 |
228 | The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”.
229 |
230 | You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see .
231 |
232 | The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read .
233 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # SPDX-FileCopyrightText: 2022 Geoffrey D. Bennett
2 | # SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | # Credit to Tom Tromey and Paul D. Smith:
5 | # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
6 |
7 | VERSION := $(shell \
8 | git describe --abbrev=4 --dirty --always --tags 2>/dev/null || \
9 | echo $${APP_VERSION:-Unknown} \
10 | )
11 |
12 | DEPDIR := .deps
13 | DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d
14 |
15 | CFLAGS := -Wall -Werror -ggdb -fno-omit-frame-pointer -O2 -D_FORTIFY_SOURCE=2
16 | CFLAGS += -DVERSION=\"$(VERSION)\"
17 |
18 | PKG_CONFIG=pkg-config
19 |
20 | CFLAGS += $(shell $(PKG_CONFIG) --cflags alsa)
21 |
22 | LDFLAGS += $(shell $(PKG_CONFIG) --libs alsa)
23 | LDFLAGS += -lm -lcrypto
24 |
25 | COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c
26 |
27 | %.o: %.c
28 | %.o: %.c Makefile $(DEPDIR)/%.d | $(DEPDIR)
29 | $(COMPILE.c) $(OUTPUT_OPTION) $<
30 |
31 | SRCS := $(sort $(wildcard *.c))
32 | OBJS := $(patsubst %.c,%.o,$(SRCS))
33 | TARGET := scarlett2
34 |
35 | all: $(TARGET)
36 |
37 | clean:
38 | rm -f $(TARGET) $(OBJS)
39 |
40 | depclean:
41 | rm -rf $(DEPDIR)
42 |
43 | $(DEPDIR): ; @mkdir -p $@
44 |
45 | DEPFILES := $(SRCS:%.c=$(DEPDIR)/%.d)
46 | $(DEPFILES):
47 |
48 | include $(wildcard $(DEPFILES))
49 |
50 | $(TARGET): $(OBJS)
51 | cc -o $(TARGET) $(OBJS) ${LDFLAGS}
52 |
53 | ifeq ($(PREFIX),)
54 | PREFIX := /usr/local
55 | endif
56 |
57 | BINDIR := $(DESTDIR)$(PREFIX)/bin
58 |
59 | install: all
60 | install -d $(BINDIR)
61 | install -m 755 $(TARGET) $(BINDIR)
62 |
63 | uninstall:
64 | rm -f $(BINDIR)/$(TARGET)
65 |
66 | help:
67 | @echo "scarlett2"
68 | @echo
69 | @echo "This Makefile knows about:"
70 | @echo " make"
71 | @echo " make install"
72 | @echo " make uninstall"
73 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Scarlett2 Firmware Management Tool
2 |
3 | ## Overview
4 |
5 | This command-line tool provides firmware management for Focusrite
6 | audio interfaces using the Scarlett2 USB protocol, which includes
7 | these series:
8 | - Scarlett 2nd, 3rd, and 4th Gen
9 | - Clarett USB and Clarett+
10 | - Vocaster
11 |
12 | Available operations:
13 | - `reboot` — reboot the device
14 | - `reset-config` — reset to default configuration
15 | - `erase-firmware` — reset the device to factory firmware
16 | - `update` — update the device's firmware
17 |
18 | ## Requirements
19 |
20 | To use this utility, you'll need the firmware itself, plus a kernel
21 | with a recent-enough version of the `snd_usb_audio` module.
22 |
23 | ### Linux Kernel
24 |
25 | - Linux Kernel 6.8 includes the Scarlett2 driver with firmware update
26 | support as part of the `snd_usb_audio` module for all supported
27 | interfaces except Vocaster (which should arrive in 6.10)
28 |
29 | OR
30 |
31 | - The latest version of the Scarlett2 driver (with firmware update and
32 | Vocaster support) is available at
33 | https://github.com/geoffreybennett/scarlett-gen2/releases — this is
34 | a replacement `snd_usb_audio` kernel module which you can build
35 | against any running kernel 5.15–6.8
36 |
37 | ### Device Firmware
38 |
39 | Device firmware should be downloaded from
40 | https://github.com/geoffreybennett/scarlett2-firmware and placed in
41 | `/usr/lib/firmware/scarlett2` or a subdirectory `firmware` relative to
42 | the binary.
43 |
44 | ## Demo
45 |
46 | ```
47 | [g@fedora ~]$ scarlett2
48 | Found 1 supported device:
49 | card1: Scarlett 3rd Gen Solo (firmware version 1605)
50 |
51 | [g@fedora ~]$ scarlett2 erase-firmware
52 | Selected device Scarlett 3rd Gen Solo
53 | Resetting to default configuration...
54 | Erase progress: Done!
55 | Erasing upgrade firmware...
56 | Erase progress: Done!
57 | Rebooting interface...
58 |
59 | [g@fedora ~]$ scarlett2
60 | Found 1 supported device:
61 | card1: Scarlett 3rd Gen Solo (firmware 1535, update to 1605 available)
62 |
63 | [g@fedora ~]$ scarlett2 update
64 | Selected device Scarlett 3rd Gen Solo
65 | Found firmware version 1605 for Scarlett 3rd Gen Solo:
66 | /usr/lib/firmware/scarlett2/scarlett2-1235-8211-1605.bin
67 | Updating Scarlett 3rd Gen Solo from firmware version 1535 to 1605
68 | Resetting to default configuration...
69 | Erase progress: Done!
70 | Erasing upgrade firmware...
71 | Erase progress: Done!
72 | Firmware write progress: Done!
73 | Rebooting interface...
74 |
75 | [g@fedora ~]$ scarlett2
76 | Found 1 supported device:
77 | card1: Scarlett 3rd Gen Solo (firmware version 1605)
78 |
79 | [g@fedora ~]$ scarlett2 list-all
80 | USB Product ID, Product Name, and Firmware versions available (* = connected)
81 | 8203 Scarlett 2nd Gen 6i6 1583, 1076
82 | 8204 Scarlett 2nd Gen 18i8 1583, 1331
83 | 8201 Scarlett 2nd Gen 18i20 1653, 1083
84 | *8211 Scarlett 3rd Gen Solo 1605, 1552 (running: 1605)
85 | 8210 Scarlett 3rd Gen 2i2 1605, 1552
86 | 8212 Scarlett 3rd Gen 4i4 1605, 1552
87 | 8213 Scarlett 3rd Gen 8i6 1605, 1552
88 | 8214 Scarlett 3rd Gen 18i8 1605, 1552
89 | 8215 Scarlett 3rd Gen 18i20 1644, 1563
90 | 8216 Vocaster One 1769
91 | 8217 Vocaster Two 1769
92 | 8218 Scarlett 4th Gen Solo 2115, 2096, 2082
93 | 8219 Scarlett 4th Gen 2i2 2115, 2100, 2096, 2082
94 | 821a Scarlett 4th Gen 4i4 2089, 2082
95 | 8206 Clarett USB 2Pre 1552
96 | 8207 Clarett USB 4Pre 1552
97 | 8208 Clarett USB 8Pre 1552
98 | 820a Clarett+ 2Pre 1993
99 | 820b Clarett+ 4Pre 1955
100 | 820c Clarett+ 8Pre 1955
101 | ```
102 |
103 | ## Build
104 |
105 | On Fedora, you'll need these packages:
106 |
107 | ```
108 | sudo dnf -y install alsa-lib-devel openssl-devel
109 | ```
110 |
111 | On OpenSUSE:
112 |
113 | ```
114 | sudo zypper in alsa-devel libopenssl-devel
115 | ```
116 |
117 | On Ubuntu:
118 |
119 | ```
120 | sudo apt -y install make gcc pkg-config libasound2-dev libssl-dev
121 | ```
122 |
123 | To build & install:
124 |
125 | ```
126 | make
127 | sudo make install
128 | ```
129 |
130 | ## Usage
131 |
132 | Usually, just `scarlett2 update` is all you need.
133 |
134 | Run `scarlett2 help` and `scarlett2 about` for more information.
135 |
136 | ## See Also
137 |
138 | The [ALSA Scarlett2 Control
139 | Panel](https://github.com/geoffreybennett/alsa-scarlett-gui/)
140 | (`alsa-scarlett-gui`), a Gtk4 GUI for these interfaces.
141 |
142 | ## Donations
143 |
144 | This program, the kernel driver, and the GUI are Free Software,
145 | developed using my personal resources, over hundreds of hours.
146 |
147 | If you find it useful, please consider a donation to show your
148 | appreciation:
149 |
150 | - https://liberapay.com/gdb
151 | - https://www.paypal.me/gdbau
152 |
153 | ## License
154 |
155 | Copyright 2023-2024 Geoffrey D. Bennett
156 |
157 | This program is free software: you can redistribute it and/or modify
158 | it under the terms of the GNU General Public License as published by
159 | the Free Software Foundation, either version 3 of the License, or (at
160 | your option) any later version.
161 |
162 | This program is distributed in the hope that it will be useful, but
163 | WITHOUT ANY WARRANTY; without even the implied warranty of
164 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
165 | General Public License for more details.
166 |
167 | You should have received a copy of the GNU General Public License
168 | along with this program. If not, see https://www.gnu.org/licenses/.
169 |
170 | ## Disclaimer Third Parties
171 |
172 | Focusrite, Scarlett, Clarett, and Vocaster are trademarks or
173 | registered trademarks of Focusrite Audio Engineering Limited in
174 | England, USA, and/or other countries. Use of these trademarks does not
175 | imply any affiliation or endorsement of this software.
176 |
--------------------------------------------------------------------------------
/main.c:
--------------------------------------------------------------------------------
1 | // SPDX-FileCopyrightText: 2023-2024 Geoffrey D. Bennett
2 | // SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include "scarlett2-firmware.h"
9 | #include "scarlett2-ioctls.h"
10 | #include "scarlett2.h"
11 |
12 | #define REQUIRED_HWDEP_VERSION_MAJOR 1
13 |
14 | // USD IDs are 4:4 characters + newline/null
15 | #define USBID_SIZE 10
16 |
17 | // Focusrite USB VID
18 | #define VENDOR_VID 0x1235
19 | #define VENDOR_PREFIX "1235:"
20 | #define VENDOR_PREFIX_LEN 5
21 |
22 | // System-wide firmware directory
23 | #define SYSTEM_FIRMWARE_DIR "/usr/lib/firmware/scarlett2"
24 |
25 | // Relative-to-executable firmware directory
26 | #define FIRMWARE_DIR "firmware"
27 |
28 | // Supported devices
29 | struct scarlett2_device {
30 | int pid;
31 | const char *name;
32 | };
33 |
34 | struct scarlett2_device scarlett2_supported[] = {
35 | { 0x8203, "Scarlett 2nd Gen 6i6" },
36 | { 0x8204, "Scarlett 2nd Gen 18i8" },
37 | { 0x8201, "Scarlett 2nd Gen 18i20" },
38 | { 0x8211, "Scarlett 3rd Gen Solo" },
39 | { 0x8210, "Scarlett 3rd Gen 2i2" },
40 | { 0x8212, "Scarlett 3rd Gen 4i4" },
41 | { 0x8213, "Scarlett 3rd Gen 8i6" },
42 | { 0x8214, "Scarlett 3rd Gen 18i8" },
43 | { 0x8215, "Scarlett 3rd Gen 18i20" },
44 | { 0x8216, "Vocaster One" },
45 | { 0x8217, "Vocaster Two" },
46 | { 0x8218, "Scarlett 4th Gen Solo" },
47 | { 0x8219, "Scarlett 4th Gen 2i2" },
48 | { 0x821a, "Scarlett 4th Gen 4i4" },
49 | { 0x8206, "Clarett USB 2Pre" },
50 | { 0x8207, "Clarett USB 4Pre" },
51 | { 0x8208, "Clarett USB 8Pre" },
52 | { 0x820a, "Clarett+ 2Pre" },
53 | { 0x820b, "Clarett+ 4Pre" },
54 | { 0x820c, "Clarett+ 8Pre" },
55 | { 0, NULL }
56 | };
57 |
58 | // found cards
59 | struct sound_card {
60 | int card_num;
61 | char card_name[32];
62 | char alsa_name[32];
63 | int pid;
64 | const char *product_name;
65 | int firmware_version;
66 | };
67 |
68 | // list of found cards
69 | struct sound_card *found_cards = NULL;
70 | int found_cards_count = 0;
71 |
72 | // found firmware
73 | struct found_firmware {
74 | char *fn;
75 | struct scarlett2_firmware_header *firmware;
76 | };
77 |
78 | // list of found firmware
79 | struct found_firmware *found_firmwares = NULL;
80 | int found_firmwares_count = 0;
81 |
82 | // the name we were called as
83 | const char *program_name;
84 |
85 | // command-line parameters
86 | const char *command = NULL;
87 | int selected_card_num = -1;
88 | struct sound_card *selected_card = NULL;
89 | int selected_firmware_version = 0;
90 | struct scarlett2_firmware_file *selected_firmware = NULL;
91 |
92 | // the open card & ioctl protocol version
93 | snd_hwdep_t *hwdep = NULL;
94 | int protocol_version = 0;
95 |
96 | // validate the VID and return the PID
97 | static int check_usb_id(const char *card_name) {
98 | char proc_path[256];
99 | char usbid[USBID_SIZE];
100 |
101 | // check if /proc/asound/ exists
102 | sprintf(proc_path, "/proc/asound/%s", card_name);
103 | if (access(proc_path, F_OK) != 0) {
104 | fprintf(stderr, "Missing %s (CONFIG_SND_PROC_FS not set?)\n", proc_path);
105 | return 0;
106 | }
107 |
108 | // attempt to open /proc/asound//usbid
109 | strcat(proc_path, "/usbid");
110 | FILE *f = fopen(proc_path, "r");
111 | if (!f)
112 | return 0;
113 |
114 | int ret = fread(usbid, 1, sizeof(usbid), f);
115 |
116 | fclose(f);
117 |
118 | if (ret != sizeof(usbid))
119 | return 0;
120 |
121 | usbid[USBID_SIZE - 1] = 0;
122 |
123 | if (strncmp(usbid, VENDOR_PREFIX, VENDOR_PREFIX_LEN) != 0)
124 | return 0;
125 |
126 | char *endptr;
127 | errno = 0;
128 | int pid = strtol(usbid + VENDOR_PREFIX_LEN, &endptr, 16);
129 | if (errno != 0 || *endptr != '\0')
130 | return 0;
131 |
132 | return pid;
133 | }
134 |
135 | static struct scarlett2_device *get_device_for_pid(int pid) {
136 | for (int i = 0; scarlett2_supported[i].name; i++)
137 | if (scarlett2_supported[i].pid == pid)
138 | return &scarlett2_supported[i];
139 |
140 | return NULL;
141 | }
142 |
143 | static int get_firmware_version(const char *alsa_name) {
144 | int err;
145 | snd_ctl_t* ctl_handle;
146 | snd_ctl_elem_id_t* id;
147 | snd_ctl_elem_value_t* control;
148 |
149 | // Open the control interface for the specified sound card
150 | if ((err = snd_ctl_open(&ctl_handle, alsa_name, 0)) < 0) {
151 | fprintf(
152 | stderr,
153 | "Unable to open control interface for card %s: %s\n",
154 | alsa_name,
155 | snd_strerror(errno)
156 | );
157 |
158 | return -1;
159 | }
160 |
161 | snd_ctl_elem_id_alloca(&id);
162 | snd_ctl_elem_value_alloca(&control);
163 |
164 | // Set the control we're interested in
165 | snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_CARD);
166 | snd_ctl_elem_id_set_name(id, "Firmware Version");
167 |
168 | snd_ctl_elem_value_set_id(control, id);
169 |
170 | // Read the control value
171 | if (snd_ctl_elem_read(ctl_handle, control) < 0) {
172 | fprintf(
173 | stderr,
174 | "Found supported Scarlett2 device at %s, but cannot read the\n"
175 | "Firmware Version ALSA control (need a newer kernel version or\n"
176 | "the updated snd-usb-audio Scarlett2 protocol driver).\n\n",
177 | alsa_name
178 | );
179 |
180 | snd_ctl_close(ctl_handle);
181 | return -1;
182 | }
183 |
184 | int version = snd_ctl_elem_value_get_integer(control, 0);
185 |
186 | snd_ctl_close(ctl_handle);
187 |
188 | return version;
189 | }
190 |
191 | static void enum_cards(void) {
192 | int card_num = -1;
193 |
194 | if (snd_card_next(&card_num) < 0 || card_num < 0)
195 | return;
196 |
197 | while (card_num >= 0) {
198 | char card_name[32];
199 | snprintf(card_name, sizeof(card_name), "card%d", card_num);
200 |
201 | int pid = check_usb_id(card_name);
202 | if (!pid)
203 | goto next;
204 |
205 | struct scarlett2_device *dev = get_device_for_pid(pid);
206 | if (!dev)
207 | goto next;
208 |
209 | found_cards_count++;
210 | found_cards = realloc(
211 | found_cards,
212 | sizeof(*found_cards) * found_cards_count
213 | );
214 | if (!found_cards) {
215 | perror("realloc");
216 | exit(EXIT_FAILURE);
217 | }
218 |
219 | struct sound_card *sc = &found_cards[found_cards_count - 1];
220 | sc->card_num = card_num;
221 | strcpy(sc->card_name, card_name);
222 | snprintf(sc->alsa_name, sizeof(sc->alsa_name), "hw:%d", card_num);
223 | sc->pid = pid;
224 | sc->product_name = dev->name;
225 | sc->firmware_version = get_firmware_version(sc->alsa_name);
226 |
227 | next:
228 | if (snd_card_next(&card_num) < 0)
229 | break;
230 | }
231 | }
232 |
233 | static void check_card_selection(void) {
234 | if (!found_cards_count) {
235 | fprintf(stderr, "No supported devices found\n");
236 | exit(EXIT_FAILURE);
237 | }
238 | if (selected_card_num == -1) {
239 | if (found_cards_count > 1) {
240 | fprintf(stderr, "Error: more than one supported device found\n");
241 | fprintf(
242 | stderr,
243 | "Use '%s list' and '%s -c ...' to select a device\n",
244 | program_name,
245 | program_name
246 | );
247 | exit(EXIT_FAILURE);
248 | }
249 | selected_card_num = found_cards[0].card_num;
250 | }
251 |
252 | for (int i = 0; i < found_cards_count; i++)
253 | if (found_cards[i].card_num == selected_card_num) {
254 | selected_card = &found_cards[i];
255 | printf("Selected device %s\n", selected_card->product_name);
256 | return;
257 | }
258 |
259 | fprintf(stderr, "Error: selected card %d not found\n", selected_card_num);
260 | fprintf(
261 | stderr,
262 | "Use '%s list' to list supported devices\n",
263 | program_name
264 | );
265 | exit(EXIT_FAILURE);
266 | }
267 |
268 | static void add_found_firmware(
269 | char *fn,
270 | struct scarlett2_firmware_header *firmware
271 | ) {
272 | if (firmware->usb_vid != VENDOR_VID)
273 | return;
274 |
275 | for (int i = 0; i < found_firmwares_count; i++) {
276 | struct found_firmware *found_firmware = &found_firmwares[i];
277 |
278 | // already have this firmware under a different name?
279 | if (found_firmware->firmware->usb_vid == firmware->usb_vid &&
280 | found_firmware->firmware->usb_pid == firmware->usb_pid &&
281 | found_firmware->firmware->firmware_version ==
282 | firmware->firmware_version)
283 | return;
284 | }
285 |
286 | found_firmwares_count++;
287 | found_firmwares = realloc(
288 | found_firmwares,
289 | sizeof(*found_firmwares) * found_firmwares_count
290 | );
291 | if (!found_firmwares) {
292 | perror("realloc");
293 | exit(EXIT_FAILURE);
294 | }
295 |
296 | struct found_firmware *fw = &found_firmwares[found_firmwares_count - 1];
297 | fw->fn = fn;
298 | fw->firmware = firmware;
299 | }
300 |
301 | static void enum_firmware_dir(const char *dirname) {
302 | DIR *dir;
303 | struct dirent *entry;
304 |
305 | if ((dir = opendir(dirname)) == NULL) {
306 | if (errno == ENOENT)
307 | return;
308 | fprintf(stderr, "Unable to opendir %s: %s\n", dirname, strerror(errno));
309 | return;
310 | }
311 |
312 | while ((entry = readdir(dir)) != NULL) {
313 |
314 | // Check if the file is a .bin file
315 | if (!strstr(entry->d_name, ".bin"))
316 | continue;
317 |
318 | // Construct full path
319 | size_t path_len = strlen(dirname) + strlen(entry->d_name) + 2;
320 | char *full_path = malloc(path_len);
321 |
322 | if (!full_path) {
323 | perror("malloc");
324 | return;
325 | }
326 |
327 | snprintf(full_path, path_len, "%s/%s", dirname, entry->d_name);
328 |
329 | // Parse the firmware file
330 | struct scarlett2_firmware_header *firmware =
331 | scarlett2_read_firmware_header(full_path);
332 | if (firmware) {
333 | add_found_firmware(full_path, firmware);
334 | } else {
335 | fprintf(stderr, "Failed to read firmware file: %s\n", full_path);
336 | free(full_path);
337 | }
338 | }
339 |
340 | closedir(dir);
341 | }
342 |
343 | static char *get_exec_dir(void) {
344 | char *path = malloc(PATH_MAX);
345 |
346 | if (!path) {
347 | perror("malloc");
348 | return NULL;
349 | }
350 |
351 | ssize_t len = readlink("/proc/self/exe", path, PATH_MAX);
352 | if (len == -1) {
353 | perror("readlink");
354 | free(path);
355 | return NULL;
356 | }
357 |
358 | path[len] = '\0';
359 |
360 | char *last_slash = strrchr(path, '/');
361 | if (!last_slash) {
362 | fprintf(stderr, "Unable to find last slash in path: %s\n", path);
363 | free(path);
364 | return NULL;
365 | }
366 |
367 | *last_slash = '\0';
368 |
369 | return path;
370 | }
371 |
372 | static char *get_firmware_exec_dir(void) {
373 | char *exec_dir = get_exec_dir();
374 | if (!exec_dir)
375 | return NULL;
376 |
377 | char *firmware_dir = malloc(strlen(exec_dir) + strlen(FIRMWARE_DIR) + 2);
378 | if (!firmware_dir) {
379 | perror("malloc");
380 | free(exec_dir);
381 | return NULL;
382 | }
383 |
384 | snprintf(firmware_dir, strlen(exec_dir) + strlen(FIRMWARE_DIR) + 2,
385 | "%s/%s", exec_dir, FIRMWARE_DIR);
386 |
387 | free(exec_dir);
388 |
389 | return firmware_dir;
390 | }
391 |
392 | static int found_firmware_cmp(const void *p1, const void *p2) {
393 | const struct found_firmware *ff1 = p1;
394 | const struct found_firmware *ff2 = p2;
395 | struct scarlett2_firmware_header *f1 = ff1->firmware;
396 | struct scarlett2_firmware_header *f2 = ff2->firmware;
397 | struct scarlett2_device *d1 = get_device_for_pid(f1->usb_pid);
398 | struct scarlett2_device *d2 = get_device_for_pid(f2->usb_pid);
399 |
400 | // compare location in scarlett2_supported array
401 | if (d1 < d2)
402 | return -1;
403 | if (d1 > d2)
404 | return 1;
405 |
406 | // compare firmware version, newest first
407 | if (f1->firmware_version < f2->firmware_version)
408 | return 1;
409 | if (f1->firmware_version > f2->firmware_version)
410 | return -1;
411 |
412 | return 0;
413 | }
414 |
415 | static void enum_firmwares(void) {
416 |
417 | /* look for firmware files in the exec-relative firmware directory */
418 | char *firmware_dir = get_firmware_exec_dir();
419 | if (firmware_dir) {
420 | enum_firmware_dir(firmware_dir);
421 | free(firmware_dir);
422 | }
423 |
424 | /* look for firmware files in the system firmware directory */
425 | enum_firmware_dir(SYSTEM_FIRMWARE_DIR);
426 |
427 | qsort(
428 | found_firmwares,
429 | found_firmwares_count,
430 | sizeof(*found_firmwares),
431 | found_firmware_cmp
432 | );
433 | }
434 |
435 | static struct found_firmware *get_latest_firmware(int pid) {
436 | for (int i = 0; i < found_firmwares_count; i++) {
437 | struct found_firmware *found_firmware = &found_firmwares[i];
438 |
439 | // found_firmwares is sorted by version descending so first is
440 | // latest
441 | if (found_firmware->firmware->usb_pid == pid)
442 | return found_firmware;
443 | }
444 |
445 | return NULL;
446 | }
447 |
448 | static struct found_firmware *get_firmware_for_version(int pid, int version) {
449 | for (int i = 0; i < found_firmwares_count; i++) {
450 | struct found_firmware *found_firmware = &found_firmwares[i];
451 |
452 | if (found_firmware->firmware->usb_pid == pid &&
453 | found_firmware->firmware->firmware_version == version)
454 | return found_firmware;
455 | }
456 |
457 | return NULL;
458 | }
459 |
460 | static void check_firmware_selection(void) {
461 |
462 | struct found_firmware *ff;
463 |
464 | // no firmware version specified, use latest
465 | if (!selected_firmware_version) {
466 | ff = get_latest_firmware(selected_card->pid);
467 |
468 | if (!ff) {
469 | fprintf(
470 | stderr,
471 | "No firmware available for %s\n",
472 | selected_card->product_name
473 | );
474 | exit(EXIT_FAILURE);
475 | }
476 |
477 | // check if latest firmware is newer
478 | if (selected_card->firmware_version >= ff->firmware->firmware_version) {
479 | fprintf(
480 | stderr,
481 | "Firmware %d for %s is already up to date\n",
482 | selected_card->firmware_version,
483 | selected_card->product_name
484 | );
485 | exit(EXIT_FAILURE);
486 | }
487 |
488 | // firmware version specified, check if it's available
489 | } else {
490 | ff = get_firmware_for_version(
491 | selected_card->pid,
492 | selected_firmware_version
493 | );
494 |
495 | if (!ff) {
496 | fprintf(
497 | stderr,
498 | "No firmware version %d available for %s\n",
499 | selected_firmware_version,
500 | selected_card->product_name
501 | );
502 | exit(EXIT_FAILURE);
503 | }
504 | }
505 |
506 | // read the firmware file
507 | selected_firmware = scarlett2_read_firmware_file(ff->fn);
508 |
509 | if (!selected_firmware) {
510 | fprintf(stderr, "Unable to load firmware\n");
511 | exit(EXIT_FAILURE);
512 | }
513 |
514 | // double-check the PID
515 | if (selected_firmware->header.usb_pid != selected_card->pid) {
516 | fprintf(
517 | stderr,
518 | "Firmware file is for a different device (PID %04x != %04x)\n",
519 | selected_firmware->header.usb_pid,
520 | selected_card->pid
521 | );
522 | exit(EXIT_FAILURE);
523 | }
524 |
525 | // display the firmware version and filename
526 | printf(
527 | "Found firmware version %d for %s:\n"
528 | " %s\n",
529 | selected_firmware->header.firmware_version,
530 | selected_card->product_name,
531 | ff->fn
532 | );
533 | }
534 |
535 | static void usage(void) {
536 | printf(
537 | "Scarlett2 Firmware Management Tool Version %s\n"
538 | "(Scarlett 2nd, 3rd, and 4th Gen, Clarett USB, and Clarett+ series)\n"
539 | "\n"
540 | "Usage: %s [options] [command]\n"
541 | "\n"
542 | "Commonly-used commands:\n"
543 | " -h, help Display this information\n"
544 | " -l, list List currently connected devices and\n"
545 | " if a firmware update is available\n"
546 | " -u, update Update firmware on the device\n"
547 | " about Display more information\n"
548 | "\n"
549 | "Lesser-used commands:\n"
550 | " list-all List all supported products and\n"
551 | " available firmware versions\n"
552 | " reboot Reboot device\n"
553 | " reset-config Reset to default configuration\n"
554 | " erase-firmware Reset device to factory firmware\n"
555 | "\n"
556 | "Lesser-used options:\n"
557 | " -c NUM, --card NUM Select a specific device\n"
558 | " (only needed if more than one connected)\n"
559 | " --fw-ver NUM Select a specific firmware version\n"
560 | "\n"
561 | "Support: https://github.com/geoffreybennett/scarlett2\n"
562 | "Configuration GUI: https://github.com/geoffreybennett/alsa-scarlett-gui\n"
563 | "\n",
564 | VERSION,
565 | program_name
566 | );
567 | }
568 |
569 | static void short_help(void) {
570 | fprintf(stderr, "Use '%s help' for help\n", program_name);
571 | exit(EXIT_FAILURE);
572 | }
573 |
574 | static void about(void) {
575 | printf(
576 | "Scarlett2 Firmware Management Tool Version %s\n"
577 | "\n"
578 | "ABOUT\n"
579 | "-----\n"
580 | "\n"
581 | "Provides firmware management for Focusrite(R) interfaces using the\n"
582 | "Scarlett2 USB protocol, including Scarlett 2nd, 3rd, 4th Gen, Clarett\n"
583 | "USB, Clarett+, and Vocaster series.\n"
584 | "\n"
585 | "REQUIREMENTS\n"
586 | "------------\n"
587 | "\n"
588 | "Requires Linux kernel 6.8 or later, or a backported version of the\n"
589 | "Scarlett2 USB protocol driver from\n"
590 | " https://github.com/geoffreybennett/scarlett-gen2/\n"
591 | "\n"
592 | "Requires device firmware to be placed in:\n"
593 | " %s or\n"
594 | " %s\n"
595 | "\n"
596 | "Obtain firmware from:\n"
597 | " https://github.com/geoffreybennett/scarlett2-firmware\n"
598 | "\n"
599 | "COPYRIGHT AND LEGAL INFORMATION\n"
600 | "-------------------------------\n"
601 | "\n"
602 | "Copyright 2023-2024 Geoffrey D. Bennett \n"
603 | "License: GPL-3.0-or-later\n"
604 | "\n"
605 | "This program is free software: you can redistribute it and/or modify\n"
606 | "it under the terms of the GNU General Public License as published by\n"
607 | "the Free Software Foundation, either version 3 of the License, or (at\n"
608 | "your option) any later version.\n"
609 | "\n"
610 | "This program is distributed in the hope that it will be useful, but\n"
611 | "WITHOUT ANY WARRANTY; without even the implied warranty of\n"
612 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
613 | "General Public License for more details.\n"
614 | "\n"
615 | "You should have received a copy of the GNU General Public License\n"
616 | "along with this program. If not, see https://www.gnu.org/licenses/\n"
617 | "\n"
618 | "Focusrite, Scarlett, Clarett, and Vocaster are trademarks or\n"
619 | "registered trademarks of Focusrite Audio Engineering Limited in\n"
620 | "England, USA, and/or other countries. Use of these trademarks does not\n"
621 | "imply any affiliation or endorsement of this software.\n"
622 | "\n"
623 | "SUPPORT AND ADDITIONAL SOFTWARE\n"
624 | "-------------------------------\n"
625 | "\n"
626 | "For support, please open an issue on GitHub:\n"
627 | " https://github.com/geoffreybennett/scarlett2\n"
628 | "\n"
629 | "GUI control panel available at:\n"
630 | " https://github.com/geoffreybennett/alsa-scarlett-gui\n"
631 | "\n"
632 | "DONATIONS\n"
633 | "---------\n"
634 | "\n"
635 | "This software was developed over hundreds of hours using my personal\n"
636 | "resources. If you find it useful, please consider a donation to show\n"
637 | "your appreciation:\n"
638 | " https://liberapay.com/gdb\n"
639 | " https://www.paypal.me/gdbau\n"
640 | "\n",
641 | VERSION, SYSTEM_FIRMWARE_DIR, get_firmware_exec_dir()
642 | );
643 | exit(0);
644 | }
645 |
646 | static void parse_args(int argc, char *argv[]) {
647 | for (int i = 1; i < argc; i++) {
648 |
649 | char *arg = argv[i];
650 |
651 | // -c, --card
652 | if (strncmp(arg, "-c", 2) == 0 ||
653 | strcmp(arg, "--card") == 0 ||
654 | strncmp(arg, "--card=", 7) == 0) {
655 |
656 | char *card_num_str;
657 |
658 | // support -cN
659 | if (strncmp(arg, "-c", 2) == 0 && strlen(arg) > 2) {
660 | card_num_str = arg + 2;
661 |
662 | // and --card=N
663 | } else if (strncmp(arg, "--card=", 7) == 0) {
664 | card_num_str = arg + 7;
665 |
666 | // and -c N, --card N
667 | } else {
668 | if (i + 1 >= argc) {
669 | fprintf(
670 | stderr, "Missing argument for %s (requires a card number)\n", arg
671 | );
672 | exit(EXIT_FAILURE);
673 | }
674 |
675 | card_num_str = argv[++i];
676 | }
677 |
678 | // make sure -c, --card hasn't already been seen
679 | if (selected_card_num != -1) {
680 | fprintf(stderr, "Error: multiple cards specified\n");
681 | exit(EXIT_FAILURE);
682 | }
683 |
684 | // parse N
685 | char *endptr;
686 | errno = 0;
687 | selected_card_num = strtol(card_num_str, &endptr, 10);
688 | if (errno != 0 || *endptr != '\0' || selected_card_num < 0) {
689 | fprintf(
690 | stderr,
691 | "Invalid argument '%s' (should be a card number)\n",
692 | card_num_str
693 | );
694 | exit(EXIT_FAILURE);
695 | }
696 |
697 | // --fw-ver
698 | } else if (strcmp(arg, "--fw-ver") == 0 ||
699 | strncmp(arg, "--fw-ver=", 9) == 0) {
700 |
701 | char *fw_ver_str;
702 |
703 | // support --fw-ver=N
704 | if (strncmp(arg, "--fw-ver=", 9) == 0) {
705 | fw_ver_str = arg + 9;
706 |
707 | } else {
708 | if (i + 1 >= argc) {
709 | fprintf(
710 | stderr,
711 | "Missing argument for %s (requires a firmware version number)\n",
712 | arg
713 | );
714 | exit(EXIT_FAILURE);
715 | }
716 |
717 | fw_ver_str = argv[++i];
718 | }
719 |
720 | // make sure --fw-ver hasn't already been seen
721 | if (selected_firmware_version) {
722 | fprintf(stderr, "Error: multiple firmware versions specified\n");
723 | exit(EXIT_FAILURE);
724 | }
725 |
726 | // parse N
727 | char *endptr;
728 | errno = 0;
729 | selected_firmware_version = strtol(fw_ver_str, &endptr, 10);
730 | if (errno != 0 || *endptr != '\0' || selected_firmware_version <= 0) {
731 | fprintf(
732 | stderr,
733 | "Invalid argument '%s' (should be a firmware version number)\n",
734 | fw_ver_str
735 | );
736 | exit(EXIT_FAILURE);
737 | }
738 |
739 | // short-form commands
740 | } else if (arg[0] == '-') {
741 | char *short_command = NULL;
742 |
743 | if (strlen(arg) == 2)
744 | switch (arg[1]) {
745 | case 'h': short_command = "help"; break;
746 | case 'l': short_command = "list"; break;
747 | case 'u': short_command = "update"; break;
748 | }
749 |
750 | if (!short_command) {
751 | fprintf(stderr, "Unknown argument: %s\n", arg);
752 | short_help();
753 | }
754 |
755 | if (command) {
756 | fprintf(
757 | stderr,
758 | "Cannot specify %s when a command has already been specified\n",
759 | arg
760 | );
761 | short_help();
762 | }
763 |
764 | command = short_command;
765 |
766 | // command
767 | } else if (!command) {
768 | command = arg;
769 |
770 | // command already specified
771 | } else {
772 | fprintf(
773 | stderr,
774 | "Cannot specify '%s' when a command has already been specified\n",
775 | arg
776 | );
777 | short_help();
778 | }
779 | }
780 |
781 | // check if a card was specified but no command
782 | if (!command && selected_card_num != -1) {
783 | fprintf(stderr, "No command specified\n");
784 | short_help();
785 | }
786 | }
787 |
788 | static void list_cards(void) {
789 | if (!found_cards) {
790 | printf("No supported devices found.\n");
791 | return;
792 | }
793 |
794 | printf(
795 | "Found %d supported device%s:\n",
796 | found_cards_count,
797 | found_cards_count > 1 ? "s" : ""
798 | );
799 | for (int i = 0; i < found_cards_count; i++) {
800 | struct sound_card *sc = &found_cards[i];
801 | struct found_firmware *ff = get_latest_firmware(sc->pid);
802 | int upgrade_version = 0;
803 |
804 | if (ff) {
805 | struct scarlett2_firmware_header *firmware = ff->firmware;
806 | if (firmware->firmware_version > sc->firmware_version)
807 | upgrade_version = firmware->firmware_version;
808 | }
809 |
810 | if (upgrade_version) {
811 | printf(
812 | " %s: %s (firmware %d, update to %d available)\n",
813 | sc->card_name,
814 | sc->product_name,
815 | sc->firmware_version,
816 | upgrade_version
817 | );
818 | } else {
819 | printf(
820 | " %s: %s (firmware version %d)\n",
821 | sc->card_name,
822 | sc->product_name,
823 | sc->firmware_version
824 | );
825 | }
826 | }
827 | }
828 |
829 | static int is_connected(int pid) {
830 | for (int i = 0; i < found_cards_count; i++)
831 | if (found_cards[i].pid == pid)
832 | return 1;
833 |
834 | return 0;
835 | }
836 |
837 | // list all supported products and available firmware versions
838 | static void list_all(void) {
839 | struct scarlett2_device *dev;
840 |
841 | // no firmware found in either directory
842 | if (!found_firmwares_count) {
843 | printf("No firmware found.\n\n");
844 | printf(
845 | "Scarlett2 firmware files should be placed in:\n"
846 | " %s or\n"
847 | " %s\n\n",
848 | SYSTEM_FIRMWARE_DIR,
849 | get_firmware_exec_dir()
850 | );
851 | }
852 |
853 | printf(
854 | "USB Product ID, Product Name, and Firmware versions available "
855 | "(* = connected)\n"
856 | );
857 |
858 | // explain the '*' prefix
859 | for (dev = scarlett2_supported; dev->name; dev++) {
860 | printf(
861 | "%c%04x %-25s",
862 | is_connected(dev->pid) ? '*' : ' ',
863 | dev->pid,
864 | dev->name
865 | );
866 |
867 | int first = 1;
868 | for (int i = 0; i < found_firmwares_count; i++) {
869 | struct found_firmware *found_firmware = &found_firmwares[i];
870 | struct scarlett2_firmware_header *firmware = found_firmware->firmware;
871 |
872 | if (firmware->usb_pid != dev->pid)
873 | continue;
874 |
875 | if (!first)
876 | printf(", ");
877 |
878 | printf("%d", firmware->firmware_version);
879 | first = 0;
880 | }
881 |
882 | // display the running firmware version if connected
883 | if (is_connected(dev->pid)) {
884 | printf(" (running: ");
885 |
886 | int first = 1;
887 | for (int i = 0; i < found_cards_count; i++) {
888 | struct sound_card *sc = &found_cards[i];
889 |
890 | if (sc->pid != dev->pid)
891 | continue;
892 |
893 | if (!first)
894 | printf(", ");
895 |
896 | printf("%d", sc->firmware_version);
897 | first = 0;
898 | }
899 |
900 | printf(")");
901 | }
902 |
903 | printf("\n");
904 | }
905 | }
906 |
907 | // open the device
908 | static void open_card(char *alsa_name) {
909 | int err;
910 |
911 | if (hwdep)
912 | return;
913 |
914 | err = scarlett2_open_card(alsa_name, &hwdep);
915 | if (err < 0) {
916 | fprintf(
917 | stderr,
918 | "Unable to open card %s: %s\n",
919 | alsa_name,
920 | snd_strerror(errno)
921 | );
922 | exit(EXIT_FAILURE);
923 | }
924 |
925 | err = scarlett2_get_protocol_version(hwdep);
926 | if (err < 0) {
927 | fprintf(
928 | stderr,
929 | "Unable to get protocol version on card %s: %s\n",
930 | alsa_name,
931 | snd_strerror(errno)
932 | );
933 | exit(EXIT_FAILURE);
934 | }
935 | protocol_version = err;
936 | if (SCARLETT2_HWDEP_VERSION_MAJOR(protocol_version) !=
937 | REQUIRED_HWDEP_VERSION_MAJOR) {
938 | fprintf(
939 | stderr,
940 | "Unsupported hwdep protocol version %d.%d.%d on card %s\n",
941 | SCARLETT2_HWDEP_VERSION_MAJOR(protocol_version),
942 | SCARLETT2_HWDEP_VERSION_MINOR(protocol_version),
943 | SCARLETT2_HWDEP_VERSION_SUBMINOR(protocol_version),
944 | alsa_name
945 | );
946 | exit(EXIT_FAILURE);
947 | }
948 | }
949 |
950 | static void reboot_card(void) {
951 | open_card(selected_card->alsa_name);
952 |
953 | printf("Rebooting interface...\n");
954 |
955 | int err = scarlett2_reboot(hwdep);
956 | if (err < 0) {
957 | fprintf(
958 | stderr,
959 | "Unable to reboot card %s: %s\n",
960 | selected_card->alsa_name,
961 | snd_strerror(errno)
962 | );
963 | exit(EXIT_FAILURE);
964 | }
965 | }
966 |
967 | static void monitor_erase_progress(void) {
968 | int last_progress = 0;
969 | int progress = 0;
970 | for (int i = 0; i < 10; i++) {
971 | progress = scarlett2_get_erase_progress(hwdep);
972 | if (progress < 0) {
973 | fprintf(
974 | stderr,
975 | "Unable to get erase progress on card %s: %s\n",
976 | selected_card->alsa_name,
977 | snd_strerror(progress)
978 | );
979 | exit(EXIT_FAILURE);
980 | }
981 |
982 | if (progress == 255)
983 | break;
984 |
985 | if (progress > last_progress) {
986 | printf("\rErase progress: %d%%", progress);
987 | fflush(stdout);
988 | last_progress = progress;
989 | i = 0;
990 | } else if (progress < last_progress) {
991 | fprintf(
992 | stderr,
993 | "\nErase progress went backwards! (%d%% -> %d%%)\n",
994 | last_progress,
995 | progress
996 | );
997 | exit(EXIT_FAILURE);
998 | }
999 | usleep(50000);
1000 | }
1001 |
1002 | if (progress != 255) {
1003 | fprintf(
1004 | stderr,
1005 | "\nUnable to get erase progress on card %s: timed out\n",
1006 | selected_card->alsa_name
1007 | );
1008 | exit(EXIT_FAILURE);
1009 | } else {
1010 | printf("\rErase progress: Done!\n");
1011 | }
1012 | }
1013 |
1014 | static void reset_config(void) {
1015 | open_card(selected_card->alsa_name);
1016 |
1017 | printf("Resetting to default configuration...\n");
1018 |
1019 | // send request to erase config
1020 | int err = scarlett2_erase_config(hwdep);
1021 | if (err < 0) {
1022 | fprintf(
1023 | stderr,
1024 | "Unable to reset configuration on card %s: %s\n",
1025 | selected_card->alsa_name,
1026 | snd_strerror(errno)
1027 | );
1028 | exit(EXIT_FAILURE);
1029 | }
1030 |
1031 | monitor_erase_progress();
1032 | }
1033 |
1034 | static void erase_firmware(void) {
1035 | open_card(selected_card->alsa_name);
1036 |
1037 | printf("Erasing upgrade firmware...\n");
1038 |
1039 | // send request to erase firmware
1040 | int err = scarlett2_erase_firmware(hwdep);
1041 | if (err < 0) {
1042 | fprintf(
1043 | stderr,
1044 | "Unable to erase upgrade firmware on card %s: %s\n",
1045 | selected_card->alsa_name,
1046 | snd_strerror(errno)
1047 | );
1048 | exit(EXIT_FAILURE);
1049 | }
1050 |
1051 | monitor_erase_progress();
1052 | }
1053 |
1054 | static void update_firmware(void) {
1055 | open_card(selected_card->alsa_name);
1056 |
1057 | // write the firmware
1058 | size_t offset = 0;
1059 | size_t len = selected_firmware->header.firmware_length;
1060 | unsigned char *buf = selected_firmware->firmware_data;
1061 |
1062 | while (offset < len) {
1063 | int err = snd_hwdep_write(hwdep, buf + offset, len - offset);
1064 | if (err < 0) {
1065 | fprintf(
1066 | stderr,
1067 | "Unable to write firmware to card %s: %s\n",
1068 | selected_card->alsa_name,
1069 | snd_strerror(errno)
1070 | );
1071 | exit(EXIT_FAILURE);
1072 | }
1073 |
1074 | if (!err) {
1075 | fprintf(
1076 | stderr,
1077 | "Unable to write firmware to card %s: offset %lu (len %lu) "
1078 | "returned 0\n",
1079 | selected_card->alsa_name,
1080 | offset,
1081 | len
1082 | );
1083 | exit(EXIT_FAILURE);
1084 | }
1085 |
1086 | offset += err;
1087 |
1088 | int progress = (offset * 100) / len;
1089 | printf("\rFirmware write progress: %d%%", progress);
1090 | fflush(stdout);
1091 | }
1092 |
1093 | printf("\rFirmware write progress: Done!\n");
1094 | }
1095 |
1096 | int main(int argc, char *argv[]) {
1097 | program_name = argv[0];
1098 |
1099 | parse_args(argc, argv);
1100 |
1101 | if (!command)
1102 | command = "list";
1103 |
1104 | if (!strcmp(command, "help")) {
1105 | usage();
1106 | } else if (!strcmp(command, "about")) {
1107 | about();
1108 | } else if (!strcmp(command, "list")) {
1109 | enum_cards();
1110 | enum_firmwares();
1111 | list_cards();
1112 | } else if (!strcmp(command, "list-all")) {
1113 | enum_cards();
1114 | enum_firmwares();
1115 | list_all();
1116 | } else if (!strcmp(command, "reboot")) {
1117 | enum_cards();
1118 | check_card_selection();
1119 | reboot_card();
1120 | } else if (!strcmp(command, "reset-config")) {
1121 | enum_cards();
1122 | check_card_selection();
1123 | reset_config();
1124 | reboot_card();
1125 | } else if (!strcmp(command, "erase-firmware")) {
1126 | enum_cards();
1127 | check_card_selection();
1128 | reset_config();
1129 | erase_firmware();
1130 | reboot_card();
1131 | } else if (!strcmp(command, "update")) {
1132 | enum_cards();
1133 | enum_firmwares();
1134 | check_card_selection();
1135 | check_firmware_selection();
1136 |
1137 | printf(
1138 | "Updating %s from firmware version %d to %d\n",
1139 | selected_card->product_name,
1140 | selected_card->firmware_version,
1141 | selected_firmware->header.firmware_version
1142 | );
1143 |
1144 | reset_config();
1145 | erase_firmware();
1146 | update_firmware();
1147 | reboot_card();
1148 | } else {
1149 | fprintf(stderr, "Unknown command: %s\n\n", command);
1150 | short_help();
1151 | }
1152 |
1153 | return 0;
1154 | }
1155 |
--------------------------------------------------------------------------------
/scarlett2-firmware.c:
--------------------------------------------------------------------------------
1 | // SPDX-FileCopyrightText: 2023 Geoffrey D. Bennett
2 | // SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | #include "scarlett2-firmware.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | static int verify_sha256(
12 | const unsigned char *data,
13 | size_t length,
14 | const unsigned char *expected_hash
15 | ) {
16 | unsigned char computed_hash[SHA256_DIGEST_LENGTH];
17 | SHA256(data, length, computed_hash);
18 | return memcmp(computed_hash, expected_hash, SHA256_DIGEST_LENGTH) == 0;
19 | }
20 |
21 | static struct scarlett2_firmware_file *read_header(FILE *file) {
22 | struct scarlett2_firmware_file *firmware = calloc(
23 | 1, sizeof(struct scarlett2_firmware_file)
24 | );
25 | if (!firmware) {
26 | perror("Failed to allocate memory for firmware structure");
27 | goto error;
28 | }
29 |
30 | size_t read_count = fread(
31 | &firmware->header, sizeof(struct scarlett2_firmware_header), 1, file
32 | );
33 |
34 | if (read_count != 1) {
35 | if (feof(file))
36 | fprintf(stderr, "Unexpected end of file\n");
37 | else
38 | perror("Failed to read header");
39 | goto error;
40 | }
41 |
42 | if (strncmp(firmware->header.magic, MAGIC_STRING, 8) != 0) {
43 | fprintf(stderr, "Invalid magic number\n");
44 | goto error;
45 | }
46 |
47 | firmware->header.usb_vid = ntohs(firmware->header.usb_vid);
48 | firmware->header.usb_pid = ntohs(firmware->header.usb_pid);
49 | firmware->header.firmware_version = ntohl(firmware->header.firmware_version);
50 | firmware->header.firmware_length = ntohl(firmware->header.firmware_length);
51 |
52 | return firmware;
53 |
54 | error:
55 | free(firmware);
56 | return NULL;
57 | }
58 |
59 | struct scarlett2_firmware_header *scarlett2_read_firmware_header(
60 | const char *fn
61 | ) {
62 | FILE *file = fopen(fn, "rb");
63 | if (!file) {
64 | perror("fopen");
65 | fprintf(stderr, "Unable to open %s\n", fn);
66 | return NULL;
67 | }
68 |
69 | struct scarlett2_firmware_file *firmware = read_header(file);
70 | if (!firmware) {
71 | fprintf(stderr, "Error reading firmware header from %s\n", fn);
72 | return NULL;
73 | }
74 |
75 | fclose(file);
76 |
77 | return realloc(firmware, sizeof(struct scarlett2_firmware_header));
78 | }
79 |
80 | struct scarlett2_firmware_file *scarlett2_read_firmware_file(const char *fn) {
81 | FILE *file = fopen(fn, "rb");
82 | if (!file) {
83 | perror("fopen");
84 | fprintf(stderr, "Unable to open %s\n", fn);
85 | return NULL;
86 | }
87 |
88 | struct scarlett2_firmware_file *firmware = read_header(file);
89 | if (!firmware) {
90 | fprintf(stderr, "Error reading firmware header from %s\n", fn);
91 | return NULL;
92 | }
93 |
94 | firmware->firmware_data = malloc(firmware->header.firmware_length);
95 | if (!firmware->firmware_data) {
96 | perror("Failed to allocate memory for firmware data");
97 | goto error;
98 | }
99 |
100 | size_t read_count = fread(
101 | firmware->firmware_data, 1, firmware->header.firmware_length, file
102 | );
103 |
104 | if (read_count != firmware->header.firmware_length) {
105 | if (feof(file))
106 | fprintf(stderr, "Unexpected end of file\n");
107 | else
108 | perror("Failed to read firmware data");
109 | fprintf(stderr, "Error reading firmware data from %s\n", fn);
110 | goto error;
111 | }
112 |
113 | if (!verify_sha256(
114 | firmware->firmware_data,
115 | firmware->header.firmware_length,
116 | firmware->header.sha256
117 | )) {
118 | fprintf(stderr, "Corrupt firmware (failed checksum) in %s\n", fn);
119 | goto error;
120 | }
121 |
122 | fclose(file);
123 | return firmware;
124 |
125 | error:
126 | scarlett2_free_firmware_file(firmware);
127 | fclose(file);
128 | return NULL;
129 | }
130 |
131 | void scarlett2_free_firmware_header(struct scarlett2_firmware_header *firmware) {
132 | if (firmware)
133 | free(firmware);
134 | }
135 |
136 | void scarlett2_free_firmware_file(struct scarlett2_firmware_file *firmware) {
137 | if (firmware) {
138 | free(firmware->firmware_data);
139 | free(firmware);
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/scarlett2-firmware.h:
--------------------------------------------------------------------------------
1 | // SPDX-FileCopyrightText: 2023 Geoffrey D. Bennett
2 | // SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | #ifndef SCARLETT2_FIRMWARE_H
5 | #define SCARLETT2_FIRMWARE_H
6 |
7 | #include
8 |
9 | #define MAGIC_STRING "SCARLETT"
10 |
11 | struct scarlett2_firmware_header {
12 | char magic[8]; // "SCARLETT"
13 | uint16_t usb_vid; // Big-endian
14 | uint16_t usb_pid; // Big-endian
15 | uint32_t firmware_version; // Big-endian
16 | uint32_t firmware_length; // Big-endian
17 | uint8_t sha256[32];
18 | } __attribute__((packed));
19 |
20 | struct scarlett2_firmware_file {
21 | struct scarlett2_firmware_header header;
22 | uint8_t *firmware_data;
23 | };
24 |
25 | struct scarlett2_firmware_header *scarlett2_read_firmware_header(
26 | const char *fn
27 | );
28 |
29 | void scarlett2_free_firmware_header(
30 | struct scarlett2_firmware_header *firmware
31 | );
32 |
33 | struct scarlett2_firmware_file *scarlett2_read_firmware_file(
34 | const char *fn
35 | );
36 |
37 | void scarlett2_free_firmware_file(
38 | struct scarlett2_firmware_file *firmware
39 | );
40 |
41 | #endif // SCARLETT2_FIRMWARE_H
42 |
--------------------------------------------------------------------------------
/scarlett2-ioctls.c:
--------------------------------------------------------------------------------
1 | // SPDX-FileCopyrightText: 2023 Geoffrey D. Bennett
2 | // SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | #include
5 | #include
6 |
7 | #include "scarlett2.h"
8 |
9 | #include "scarlett2-ioctls.h"
10 |
11 | int scarlett2_open_card(char *alsa_name, snd_hwdep_t **hwdep) {
12 | return snd_hwdep_open(hwdep, alsa_name, SND_HWDEP_OPEN_DUPLEX);
13 | }
14 |
15 | int scarlett2_get_protocol_version(snd_hwdep_t *hwdep) {
16 | int version = 0;
17 | int err = snd_hwdep_ioctl(hwdep, SCARLETT2_IOCTL_PVERSION, &version);
18 |
19 | if (err < 0)
20 | return err;
21 | return version;
22 | }
23 |
24 | int scarlett2_close(snd_hwdep_t *hwdep) {
25 | return snd_hwdep_close(hwdep);
26 | }
27 |
28 | int scarlett2_reboot(snd_hwdep_t *hwdep) {
29 | return snd_hwdep_ioctl(hwdep, SCARLETT2_IOCTL_REBOOT, 0);
30 | }
31 |
32 | static int scarlett2_select_flash_segment(snd_hwdep_t *hwdep, int segment) {
33 | return snd_hwdep_ioctl(hwdep, SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT, &segment);
34 | }
35 |
36 | static int scarlett2_erase_flash_segment(snd_hwdep_t *hwdep) {
37 | return snd_hwdep_ioctl(hwdep, SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT, 0);
38 | }
39 |
40 | int scarlett2_erase_config(snd_hwdep_t *hwdep) {
41 | int err;
42 |
43 | err = scarlett2_select_flash_segment(hwdep, SCARLETT2_SEGMENT_ID_SETTINGS);
44 | if (err < 0)
45 | return err;
46 | return scarlett2_erase_flash_segment(hwdep);
47 | }
48 |
49 | int scarlett2_erase_firmware(snd_hwdep_t *hwdep) {
50 | int err;
51 |
52 | err = scarlett2_select_flash_segment(hwdep, SCARLETT2_SEGMENT_ID_FIRMWARE);
53 | if (err < 0)
54 | return err;
55 | return scarlett2_erase_flash_segment(hwdep);
56 | }
57 |
58 | int scarlett2_get_erase_progress(snd_hwdep_t *hwdep) {
59 | struct scarlett2_flash_segment_erase_progress progress;
60 |
61 | int err = snd_hwdep_ioctl(
62 | hwdep, SCARLETT2_IOCTL_GET_ERASE_PROGRESS, &progress
63 | );
64 | if (err < 0)
65 | return err;
66 |
67 | // translate progress from [1..num_blocks, 255] to [[0..100), 255]]
68 | if (progress.num_blocks == 0 ||
69 | progress.progress == 0 ||
70 | progress.progress == 255)
71 | return progress.progress;
72 |
73 | return (progress.progress - 1) * 100 / progress.num_blocks;
74 | }
75 |
--------------------------------------------------------------------------------
/scarlett2-ioctls.h:
--------------------------------------------------------------------------------
1 | // SPDX-FileCopyrightText: 2023 Geoffrey D. Bennett
2 | // SPDX-License-Identifier: GPL-3.0-or-later
3 |
4 | #ifndef SCARLETT2_IOCTLS_H
5 | #define SCARLETT2_IOCTLS_H
6 |
7 | #include
8 |
9 | int scarlett2_open_card(char *alsa_name, snd_hwdep_t **hwdep);
10 | int scarlett2_get_protocol_version(snd_hwdep_t *hwdep);
11 | int scarlett2_lock(snd_hwdep_t *hwdep);
12 | int scarlett2_unlock(snd_hwdep_t *hwdep);
13 | int scarlett2_close(snd_hwdep_t *hwdep);
14 |
15 | int scarlett2_reboot(snd_hwdep_t *hwdep);
16 | int scarlett2_erase_config(snd_hwdep_t *hwdep);
17 | int scarlett2_erase_firmware(snd_hwdep_t *hwdep);
18 | int scarlett2_get_erase_progress(snd_hwdep_t *hwdep);
19 | int scarlett2_write_firmware(
20 | snd_hwdep_t *hwdep,
21 | off_t offset,
22 | unsigned char *buf,
23 | size_t buf_len
24 | );
25 |
26 | #endif // SCARLETT2_IOCTLS_H
27 |
--------------------------------------------------------------------------------
/scarlett2.h:
--------------------------------------------------------------------------------
1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 | /*
3 | * Focusrite Scarlett 2 Protocol Driver for ALSA
4 | * (including Scarlett 2nd Gen, 3rd Gen, Clarett USB, and Clarett+
5 | * series products)
6 | *
7 | * Copyright (c) 2023 by Geoffrey D. Bennett
8 | */
9 | #ifndef __UAPI_SOUND_SCARLETT2_H
10 | #define __UAPI_SOUND_SCARLETT2_H
11 |
12 | #include
13 | #include
14 |
15 | #define SCARLETT2_HWDEP_MAJOR 1
16 | #define SCARLETT2_HWDEP_MINOR 0
17 | #define SCARLETT2_HWDEP_SUBMINOR 0
18 |
19 | #define SCARLETT2_HWDEP_VERSION \
20 | ((SCARLETT2_HWDEP_MAJOR << 16) | \
21 | (SCARLETT2_HWDEP_MINOR << 8) | \
22 | SCARLETT2_HWDEP_SUBMINOR)
23 |
24 | #define SCARLETT2_HWDEP_VERSION_MAJOR(v) (((v) >> 16) & 0xFF)
25 | #define SCARLETT2_HWDEP_VERSION_MINOR(v) (((v) >> 8) & 0xFF)
26 | #define SCARLETT2_HWDEP_VERSION_SUBMINOR(v) ((v) & 0xFF)
27 |
28 | /* Get protocol version */
29 | #define SCARLETT2_IOCTL_PVERSION _IOR('S', 0x60, int)
30 |
31 | /* Reboot */
32 | #define SCARLETT2_IOCTL_REBOOT _IO('S', 0x61)
33 |
34 | /* Select flash segment */
35 | #define SCARLETT2_SEGMENT_ID_SETTINGS 0
36 | #define SCARLETT2_SEGMENT_ID_FIRMWARE 1
37 | #define SCARLETT2_SEGMENT_ID_COUNT 2
38 |
39 | #define SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT _IOW('S', 0x62, int)
40 |
41 | /* Erase selected flash segment */
42 | #define SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT _IO('S', 0x63)
43 |
44 | /* Get selected flash segment erase progress
45 | * 1 through to num_blocks, or 255 for complete
46 | */
47 | struct scarlett2_flash_segment_erase_progress {
48 | unsigned char progress;
49 | unsigned char num_blocks;
50 | };
51 | #define SCARLETT2_IOCTL_GET_ERASE_PROGRESS \
52 | _IOR('S', 0x64, struct scarlett2_flash_segment_erase_progress)
53 |
54 | #endif /* __UAPI_SOUND_SCARLETT2_H */
55 |
--------------------------------------------------------------------------------