Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
0004  *
0005  *   Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
0006  *                      Karsten Wiese <fzu@wemgehoertderstaat.de>
0007 */
0008 
0009 #include <linux/moduleparam.h>
0010 #include <linux/device.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/init.h>
0014 #include <linux/gfp.h>
0015 #include <linux/usb.h>
0016 #include <sound/initval.h>
0017 #include <sound/core.h>
0018 #include <sound/pcm.h>
0019 
0020 #include "device.h"
0021 #include "audio.h"
0022 #include "midi.h"
0023 #include "control.h"
0024 #include "input.h"
0025 
0026 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
0027 MODULE_DESCRIPTION("caiaq USB audio");
0028 MODULE_LICENSE("GPL");
0029 
0030 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
0031 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
0032 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
0033 
0034 module_param_array(index, int, NULL, 0444);
0035 MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
0036 module_param_array(id, charp, NULL, 0444);
0037 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
0038 module_param_array(enable, bool, NULL, 0444);
0039 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
0040 
0041 enum {
0042     SAMPLERATE_44100    = 0,
0043     SAMPLERATE_48000    = 1,
0044     SAMPLERATE_96000    = 2,
0045     SAMPLERATE_192000   = 3,
0046     SAMPLERATE_88200    = 4,
0047     SAMPLERATE_INVALID  = 0xff
0048 };
0049 
0050 enum {
0051     DEPTH_NONE  = 0,
0052     DEPTH_16    = 1,
0053     DEPTH_24    = 2,
0054     DEPTH_32    = 3
0055 };
0056 
0057 static const struct usb_device_id snd_usb_id_table[] = {
0058     {
0059         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0060         .idVendor = USB_VID_NATIVEINSTRUMENTS,
0061         .idProduct =    USB_PID_RIGKONTROL2
0062     },
0063     {
0064         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0065         .idVendor = USB_VID_NATIVEINSTRUMENTS,
0066         .idProduct =    USB_PID_RIGKONTROL3
0067     },
0068     {
0069         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0070         .idVendor = USB_VID_NATIVEINSTRUMENTS,
0071         .idProduct =    USB_PID_KORECONTROLLER
0072     },
0073     {
0074         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0075         .idVendor = USB_VID_NATIVEINSTRUMENTS,
0076         .idProduct =    USB_PID_KORECONTROLLER2
0077     },
0078     {
0079         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0080         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0081         .idProduct =    USB_PID_AK1
0082     },
0083     {
0084         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0085         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0086         .idProduct =    USB_PID_AUDIO8DJ
0087     },
0088     {
0089         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0090         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0091         .idProduct =    USB_PID_SESSIONIO
0092     },
0093     {
0094         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0095         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0096         .idProduct =    USB_PID_GUITARRIGMOBILE
0097     },
0098     {
0099         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0100         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0101         .idProduct =    USB_PID_AUDIO4DJ
0102     },
0103     {
0104         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0105         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0106         .idProduct =    USB_PID_AUDIO2DJ
0107     },
0108     {
0109         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0110         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0111         .idProduct =    USB_PID_TRAKTORKONTROLX1
0112     },
0113     {
0114         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0115         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0116         .idProduct =    USB_PID_TRAKTORKONTROLS4
0117     },
0118     {
0119         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0120         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0121         .idProduct =    USB_PID_TRAKTORAUDIO2
0122     },
0123     {
0124         .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
0125         .idVendor =     USB_VID_NATIVEINSTRUMENTS,
0126         .idProduct =    USB_PID_MASCHINECONTROLLER
0127     },
0128     { /* terminator */ }
0129 };
0130 
0131 static void usb_ep1_command_reply_dispatch (struct urb* urb)
0132 {
0133     int ret;
0134     struct device *dev = &urb->dev->dev;
0135     struct snd_usb_caiaqdev *cdev = urb->context;
0136     unsigned char *buf = urb->transfer_buffer;
0137 
0138     if (urb->status || !cdev) {
0139         dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
0140         return;
0141     }
0142 
0143     switch(buf[0]) {
0144     case EP1_CMD_GET_DEVICE_INFO:
0145         memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec));
0146         cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version);
0147         dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, "
0148             "MIDI: %d in, %d out, data alignment %d\n",
0149             cdev->spec.fw_version,
0150             cdev->spec.num_analog_audio_in,
0151             cdev->spec.num_analog_audio_out,
0152             cdev->spec.num_midi_in,
0153             cdev->spec.num_midi_out,
0154             cdev->spec.data_alignment);
0155 
0156         cdev->spec_received++;
0157         wake_up(&cdev->ep1_wait_queue);
0158         break;
0159     case EP1_CMD_AUDIO_PARAMS:
0160         cdev->audio_parm_answer = buf[1];
0161         wake_up(&cdev->ep1_wait_queue);
0162         break;
0163     case EP1_CMD_MIDI_READ:
0164         snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
0165         break;
0166     case EP1_CMD_READ_IO:
0167         if (cdev->chip.usb_id ==
0168             USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
0169             if (urb->actual_length > sizeof(cdev->control_state))
0170                 urb->actual_length = sizeof(cdev->control_state);
0171             memcpy(cdev->control_state, buf + 1, urb->actual_length);
0172             wake_up(&cdev->ep1_wait_queue);
0173             break;
0174         }
0175 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
0176         fallthrough;
0177     case EP1_CMD_READ_ERP:
0178     case EP1_CMD_READ_ANALOG:
0179         snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
0180 #endif
0181         break;
0182     }
0183 
0184     cdev->ep1_in_urb.actual_length = 0;
0185     ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
0186     if (ret < 0)
0187         dev_err(dev, "unable to submit urb. OOM!?\n");
0188 }
0189 
0190 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
0191                    unsigned char command,
0192                    const unsigned char *buffer,
0193                    int len)
0194 {
0195     int actual_len;
0196     struct usb_device *usb_dev = cdev->chip.dev;
0197 
0198     if (!usb_dev)
0199         return -EIO;
0200 
0201     if (len > EP1_BUFSIZE - 1)
0202         len = EP1_BUFSIZE - 1;
0203 
0204     if (buffer && len > 0)
0205         memcpy(cdev->ep1_out_buf+1, buffer, len);
0206 
0207     cdev->ep1_out_buf[0] = command;
0208     return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
0209                cdev->ep1_out_buf, len+1, &actual_len, 200);
0210 }
0211 
0212 int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
0213                    unsigned char command,
0214                    unsigned char bank,
0215                    const unsigned char *buffer,
0216                    int len)
0217 {
0218     int actual_len;
0219     struct usb_device *usb_dev = cdev->chip.dev;
0220 
0221     if (!usb_dev)
0222         return -EIO;
0223 
0224     if (len > EP1_BUFSIZE - 2)
0225         len = EP1_BUFSIZE - 2;
0226 
0227     if (buffer && len > 0)
0228         memcpy(cdev->ep1_out_buf+2, buffer, len);
0229 
0230     cdev->ep1_out_buf[0] = command;
0231     cdev->ep1_out_buf[1] = bank;
0232 
0233     return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
0234                cdev->ep1_out_buf, len+2, &actual_len, 200);
0235 }
0236 
0237 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
0238                     int rate, int depth, int bpp)
0239 {
0240     int ret;
0241     char tmp[5];
0242     struct device *dev = caiaqdev_to_dev(cdev);
0243 
0244     switch (rate) {
0245     case 44100: tmp[0] = SAMPLERATE_44100;   break;
0246     case 48000: tmp[0] = SAMPLERATE_48000;   break;
0247     case 88200: tmp[0] = SAMPLERATE_88200;   break;
0248     case 96000: tmp[0] = SAMPLERATE_96000;   break;
0249     case 192000:    tmp[0] = SAMPLERATE_192000;  break;
0250     default:    return -EINVAL;
0251     }
0252 
0253     switch (depth) {
0254     case 16:    tmp[1] = DEPTH_16;   break;
0255     case 24:    tmp[1] = DEPTH_24;   break;
0256     default:    return -EINVAL;
0257     }
0258 
0259     tmp[2] = bpp & 0xff;
0260     tmp[3] = bpp >> 8;
0261     tmp[4] = 1; /* packets per microframe */
0262 
0263     dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
0264         rate, depth, bpp);
0265 
0266     cdev->audio_parm_answer = -1;
0267     ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
0268                      tmp, sizeof(tmp));
0269 
0270     if (ret)
0271         return ret;
0272 
0273     if (!wait_event_timeout(cdev->ep1_wait_queue,
0274         cdev->audio_parm_answer >= 0, HZ))
0275         return -EPIPE;
0276 
0277     if (cdev->audio_parm_answer != 1)
0278         dev_dbg(dev, "unable to set the device's audio params\n");
0279     else
0280         cdev->bpp = bpp;
0281 
0282     return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
0283 }
0284 
0285 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
0286                    int digital, int analog, int erp)
0287 {
0288     char tmp[3] = { digital, analog, erp };
0289     return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
0290                       tmp, sizeof(tmp));
0291 }
0292 
0293 static void setup_card(struct snd_usb_caiaqdev *cdev)
0294 {
0295     int ret;
0296     char val[4];
0297     struct device *dev = caiaqdev_to_dev(cdev);
0298 
0299     /* device-specific startup specials */
0300     switch (cdev->chip.usb_id) {
0301     case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
0302         /* RigKontrol2 - display centered dash ('-') */
0303         val[0] = 0x00;
0304         val[1] = 0x00;
0305         val[2] = 0x01;
0306         snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
0307         break;
0308     case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
0309         /* RigKontrol2 - display two centered dashes ('--') */
0310         val[0] = 0x00;
0311         val[1] = 0x40;
0312         val[2] = 0x40;
0313         val[3] = 0x00;
0314         snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
0315         break;
0316     case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
0317         /* Audio Kontrol 1 - make USB-LED stop blinking */
0318         val[0] = 0x00;
0319         snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
0320         break;
0321     case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
0322         /* Audio 8 DJ - trigger read of current settings */
0323         cdev->control_state[0] = 0xff;
0324         snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0);
0325         snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0);
0326 
0327         if (!wait_event_timeout(cdev->ep1_wait_queue,
0328                     cdev->control_state[0] != 0xff, HZ))
0329             return;
0330 
0331         /* fix up some defaults */
0332         if ((cdev->control_state[1] != 2) ||
0333             (cdev->control_state[2] != 3) ||
0334             (cdev->control_state[4] != 2)) {
0335             cdev->control_state[1] = 2;
0336             cdev->control_state[2] = 3;
0337             cdev->control_state[4] = 2;
0338             snd_usb_caiaq_send_command(cdev,
0339                 EP1_CMD_WRITE_IO, cdev->control_state, 6);
0340         }
0341 
0342         break;
0343     }
0344 
0345     if (cdev->spec.num_analog_audio_out +
0346         cdev->spec.num_analog_audio_in +
0347         cdev->spec.num_digital_audio_out +
0348         cdev->spec.num_digital_audio_in > 0) {
0349         ret = snd_usb_caiaq_audio_init(cdev);
0350         if (ret < 0)
0351             dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
0352     }
0353 
0354     if (cdev->spec.num_midi_in +
0355         cdev->spec.num_midi_out > 0) {
0356         ret = snd_usb_caiaq_midi_init(cdev);
0357         if (ret < 0)
0358             dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
0359     }
0360 
0361 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
0362     ret = snd_usb_caiaq_input_init(cdev);
0363     if (ret < 0)
0364         dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
0365 #endif
0366 
0367     /* finally, register the card and all its sub-instances */
0368     ret = snd_card_register(cdev->chip.card);
0369     if (ret < 0) {
0370         dev_err(dev, "snd_card_register() returned %d\n", ret);
0371         snd_card_free(cdev->chip.card);
0372     }
0373 
0374     ret = snd_usb_caiaq_control_init(cdev);
0375     if (ret < 0)
0376         dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
0377 }
0378 
0379 static int create_card(struct usb_device *usb_dev,
0380                struct usb_interface *intf,
0381                struct snd_card **cardp)
0382 {
0383     int devnum;
0384     int err;
0385     struct snd_card *card;
0386     struct snd_usb_caiaqdev *cdev;
0387 
0388     for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
0389         if (enable[devnum])
0390             break;
0391 
0392     if (devnum >= SNDRV_CARDS)
0393         return -ENODEV;
0394 
0395     err = snd_card_new(&intf->dev,
0396                index[devnum], id[devnum], THIS_MODULE,
0397                sizeof(struct snd_usb_caiaqdev), &card);
0398     if (err < 0)
0399         return err;
0400 
0401     cdev = caiaqdev(card);
0402     cdev->chip.dev = usb_dev;
0403     cdev->chip.card = card;
0404     cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
0405                   le16_to_cpu(usb_dev->descriptor.idProduct));
0406     spin_lock_init(&cdev->spinlock);
0407 
0408     *cardp = card;
0409     return 0;
0410 }
0411 
0412 static int init_card(struct snd_usb_caiaqdev *cdev)
0413 {
0414     char *c, usbpath[32];
0415     struct usb_device *usb_dev = cdev->chip.dev;
0416     struct snd_card *card = cdev->chip.card;
0417     struct device *dev = caiaqdev_to_dev(cdev);
0418     int err, len;
0419 
0420     if (usb_set_interface(usb_dev, 0, 1) != 0) {
0421         dev_err(dev, "can't set alt interface.\n");
0422         return -EIO;
0423     }
0424 
0425     usb_init_urb(&cdev->ep1_in_urb);
0426     usb_init_urb(&cdev->midi_out_urb);
0427 
0428     usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev,
0429               usb_rcvbulkpipe(usb_dev, 0x1),
0430               cdev->ep1_in_buf, EP1_BUFSIZE,
0431               usb_ep1_command_reply_dispatch, cdev);
0432 
0433     usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev,
0434               usb_sndbulkpipe(usb_dev, 0x1),
0435               cdev->midi_out_buf, EP1_BUFSIZE,
0436               snd_usb_caiaq_midi_output_done, cdev);
0437 
0438     /* sanity checks of EPs before actually submitting */
0439     if (usb_urb_ep_type_check(&cdev->ep1_in_urb) ||
0440         usb_urb_ep_type_check(&cdev->midi_out_urb)) {
0441         dev_err(dev, "invalid EPs\n");
0442         return -EINVAL;
0443     }
0444 
0445     init_waitqueue_head(&cdev->ep1_wait_queue);
0446     init_waitqueue_head(&cdev->prepare_wait_queue);
0447 
0448     if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
0449         return -EIO;
0450 
0451     err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
0452     if (err)
0453         goto err_kill_urb;
0454 
0455     if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) {
0456         err = -ENODEV;
0457         goto err_kill_urb;
0458     }
0459 
0460     usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
0461            cdev->vendor_name, CAIAQ_USB_STR_LEN);
0462 
0463     usb_string(usb_dev, usb_dev->descriptor.iProduct,
0464            cdev->product_name, CAIAQ_USB_STR_LEN);
0465 
0466     strscpy(card->driver, MODNAME, sizeof(card->driver));
0467     strscpy(card->shortname, cdev->product_name, sizeof(card->shortname));
0468     strscpy(card->mixername, cdev->product_name, sizeof(card->mixername));
0469 
0470     /* if the id was not passed as module option, fill it with a shortened
0471      * version of the product string which does not contain any
0472      * whitespaces */
0473 
0474     if (*card->id == '\0') {
0475         char id[sizeof(card->id)];
0476 
0477         memset(id, 0, sizeof(id));
0478 
0479         for (c = card->shortname, len = 0;
0480             *c && len < sizeof(card->id); c++)
0481             if (*c != ' ')
0482                 id[len++] = *c;
0483 
0484         snd_card_set_id(card, id);
0485     }
0486 
0487     usb_make_path(usb_dev, usbpath, sizeof(usbpath));
0488     snprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
0489                cdev->vendor_name, cdev->product_name, usbpath);
0490 
0491     setup_card(cdev);
0492     return 0;
0493 
0494  err_kill_urb:
0495     usb_kill_urb(&cdev->ep1_in_urb);
0496     return err;
0497 }
0498 
0499 static int snd_probe(struct usb_interface *intf,
0500              const struct usb_device_id *id)
0501 {
0502     int ret;
0503     struct snd_card *card = NULL;
0504     struct usb_device *usb_dev = interface_to_usbdev(intf);
0505 
0506     ret = create_card(usb_dev, intf, &card);
0507 
0508     if (ret < 0)
0509         return ret;
0510 
0511     usb_set_intfdata(intf, card);
0512     ret = init_card(caiaqdev(card));
0513     if (ret < 0) {
0514         dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
0515         snd_card_free(card);
0516         return ret;
0517     }
0518 
0519     return 0;
0520 }
0521 
0522 static void snd_disconnect(struct usb_interface *intf)
0523 {
0524     struct snd_card *card = usb_get_intfdata(intf);
0525     struct device *dev = intf->usb_dev;
0526     struct snd_usb_caiaqdev *cdev;
0527 
0528     if (!card)
0529         return;
0530 
0531     cdev = caiaqdev(card);
0532     dev_dbg(dev, "%s(%p)\n", __func__, intf);
0533 
0534     snd_card_disconnect(card);
0535 
0536 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
0537     snd_usb_caiaq_input_free(cdev);
0538 #endif
0539     snd_usb_caiaq_audio_free(cdev);
0540 
0541     usb_kill_urb(&cdev->ep1_in_urb);
0542     usb_kill_urb(&cdev->midi_out_urb);
0543 
0544     snd_card_free(card);
0545     usb_reset_device(interface_to_usbdev(intf));
0546 }
0547 
0548 
0549 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
0550 static struct usb_driver snd_usb_driver = {
0551     .name       = MODNAME,
0552     .probe      = snd_probe,
0553     .disconnect = snd_disconnect,
0554     .id_table   = snd_usb_id_table,
0555 };
0556 
0557 module_usb_driver(snd_usb_driver);