]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - drivers/usb/gadget/f_acm.c
usb: gadget: f_acm: convert to new function interface with backwards compatibility
[karo-tx-linux.git] / drivers / usb / gadget / f_acm.c
index 549174466c2160d40003ee5787a0b9e7adaa6fca..3ea7dc89b43de1e624c24840a1eb16ccd07550fe 100644 (file)
@@ -16,7 +16,9 @@
 
 #include <linux/slab.h>
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/device.h>
+#include <linux/err.h>
 
 #include "u_serial.h"
 #include "gadget_chips.h"
@@ -608,6 +610,22 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
        int                     status;
        struct usb_ep           *ep;
 
+       /* REVISIT might want instance-specific strings to help
+        * distinguish instances ...
+        */
+
+       /* maybe allocate device-global string IDs, and patch descriptors */
+       if (acm_string_defs[0].id == 0) {
+               status = usb_string_ids_tab(c->cdev, acm_string_defs);
+               if (status < 0)
+                       return status;
+               acm_control_interface_desc.iInterface =
+                       acm_string_defs[ACM_CTRL_IDX].id;
+               acm_data_interface_desc.iInterface =
+                       acm_string_defs[ACM_DATA_IDX].id;
+               acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
+       }
+
        /* allocate instance-specific interface IDs, and patch descriptors */
        status = usb_interface_id(c, f);
        if (status < 0)
@@ -700,24 +718,44 @@ fail:
        return status;
 }
 
+static struct f_acm *acm_alloc_basic_func(void)
+{
+       struct f_acm    *acm;
+
+       acm = kzalloc(sizeof(*acm), GFP_KERNEL);
+       if (!acm)
+               return NULL;
+
+       spin_lock_init(&acm->lock);
+
+       acm->port.connect = acm_connect;
+       acm->port.disconnect = acm_disconnect;
+       acm->port.send_break = acm_send_break;
+
+       acm->port.func.name = "acm";
+       acm->port.func.strings = acm_strings;
+       /* descriptors are per-instance copies */
+       acm->port.func.bind = acm_bind;
+       acm->port.func.set_alt = acm_set_alt;
+       acm->port.func.setup = acm_setup;
+       acm->port.func.disable = acm_disable;
+
+       return acm;
+}
+
+#ifdef USB_FACM_INCLUDED
 static void
-acm_unbind(struct usb_configuration *c, struct usb_function *f)
+acm_old_unbind(struct usb_configuration *c, struct usb_function *f)
 {
        struct f_acm            *acm = func_to_acm(f);
 
        acm_string_defs[0].id = 0;
        usb_free_all_descriptors(f);
-       gs_free_req(acm->notify, acm->notify_req);
+       if (acm->notify_req)
+               gs_free_req(acm->notify, acm->notify_req);
        kfree(acm);
 }
 
-/* Some controllers can't support CDC ACM ... */
-static inline bool can_support_cdc(struct usb_configuration *c)
-{
-       /* everything else is *probably* fine ... */
-       return true;
-}
-
 /**
  * acm_bind_config - add a CDC ACM function to a configuration
  * @c: the configuration to support the CDC ACM instance
@@ -726,58 +764,80 @@ static inline bool can_support_cdc(struct usb_configuration *c)
  *
  * Returns zero on success, else negative errno.
  *
- * Caller must have called @gserial_setup() with enough ports to
- * handle all the ones it binds.  Caller is also responsible
- * for calling @gserial_cleanup() before module unload.
  */
 int acm_bind_config(struct usb_configuration *c, u8 port_num)
 {
        struct f_acm    *acm;
        int             status;
 
-       if (!can_support_cdc(c))
-               return -EINVAL;
-
-       /* REVISIT might want instance-specific strings to help
-        * distinguish instances ...
-        */
-
-       /* maybe allocate device-global string IDs, and patch descriptors */
-       if (acm_string_defs[0].id == 0) {
-               status = usb_string_ids_tab(c->cdev, acm_string_defs);
-               if (status < 0)
-                       return status;
-               acm_control_interface_desc.iInterface =
-                       acm_string_defs[ACM_CTRL_IDX].id;
-               acm_data_interface_desc.iInterface =
-                       acm_string_defs[ACM_DATA_IDX].id;
-               acm_iad_descriptor.iFunction = acm_string_defs[ACM_IAD_IDX].id;
-       }
-
        /* allocate and initialize one new instance */
-       acm = kzalloc(sizeof *acm, GFP_KERNEL);
+       acm = acm_alloc_basic_func();
        if (!acm)
                return -ENOMEM;
 
-       spin_lock_init(&acm->lock);
-
        acm->port_num = port_num;
-
-       acm->port.connect = acm_connect;
-       acm->port.disconnect = acm_disconnect;
-       acm->port.send_break = acm_send_break;
-
-       acm->port.func.name = "acm";
-       acm->port.func.strings = acm_strings;
-       /* descriptors are per-instance copies */
-       acm->port.func.bind = acm_bind;
-       acm->port.func.unbind = acm_unbind;
-       acm->port.func.set_alt = acm_set_alt;
-       acm->port.func.setup = acm_setup;
-       acm->port.func.disable = acm_disable;
+       acm->port.func.unbind = acm_old_unbind;
 
        status = usb_add_function(c, &acm->port.func);
        if (status)
                kfree(acm);
        return status;
 }
+
+#else
+
+static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
+{
+       struct f_acm            *acm = func_to_acm(f);
+
+       acm_string_defs[0].id = 0;
+       usb_free_all_descriptors(f);
+       if (acm->notify_req)
+               gs_free_req(acm->notify, acm->notify_req);
+}
+
+static void acm_free_func(struct usb_function *f)
+{
+       struct f_acm            *acm = func_to_acm(f);
+
+       kfree(acm);
+}
+
+static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
+{
+       struct f_serial_opts *opts;
+       struct f_acm *acm;
+
+       acm = acm_alloc_basic_func();
+       if (!acm)
+               return ERR_PTR(-ENOMEM);
+
+       opts = container_of(fi, struct f_serial_opts, func_inst);
+       acm->port_num = opts->port_num;
+       acm->port.func.unbind = acm_unbind;
+       acm->port.func.free_func = acm_free_func;
+
+       return &acm->port.func;
+}
+
+static void acm_free_instance(struct usb_function_instance *fi)
+{
+       struct f_serial_opts *opts;
+
+       opts = container_of(fi, struct f_serial_opts, func_inst);
+       kfree(opts);
+}
+
+static struct usb_function_instance *acm_alloc_instance(void)
+{
+       struct f_serial_opts *opts;
+
+       opts = kzalloc(sizeof(*opts), GFP_KERNEL);
+       if (!opts)
+               return ERR_PTR(-ENOMEM);
+       opts->func_inst.free_func_inst = acm_free_instance;
+       return &opts->func_inst;
+}
+DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
+MODULE_LICENSE("GPL");
+#endif