Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * printer.c -- Printer gadget driver
  4 *
  5 * Copyright (C) 2003-2005 David Brownell
  6 * Copyright (C) 2006 Craig W. Nadler
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <asm/byteorder.h>
 12
 13#include <linux/usb/ch9.h>
 14#include <linux/usb/composite.h>
 15#include <linux/usb/gadget.h>
 16#include <linux/usb/g_printer.h>
 17
 18USB_GADGET_COMPOSITE_OPTIONS();
 19
 20#define DRIVER_DESC		"Printer Gadget"
 21#define DRIVER_VERSION		"2015 FEB 17"
 22
 23static const char shortname [] = "printer";
 
 24
 25#include "u_printer.h"
 26
 27/*-------------------------------------------------------------------------*/
 28
 29/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 30 * Instead:  allocate your own, using normal USB-IF procedures.
 31 */
 32
 33/* Thanks to NetChip Technologies for donating this product ID.
 34 */
 35#define PRINTER_VENDOR_NUM	0x0525		/* NetChip */
 36#define PRINTER_PRODUCT_NUM	0xa4a8		/* Linux-USB Printer Gadget */
 37
 38/* Some systems will want different product identifiers published in the
 39 * device descriptor, either numbers or strings or both.  These string
 40 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
 41 */
 42
 43module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
 44MODULE_PARM_DESC(iSerialNum, "1");
 45
 46static char *iPNPstring;
 47module_param(iPNPstring, charp, S_IRUGO);
 48MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
 49
 50/* Number of requests to allocate per endpoint, not used for ep0. */
 51static unsigned qlen = 10;
 52module_param(qlen, uint, S_IRUGO|S_IWUSR);
 53
 54#define QLEN	qlen
 55
 56static struct usb_function_instance *fi_printer;
 57static struct usb_function *f_printer;
 58
 59/*-------------------------------------------------------------------------*/
 60
 61/*
 62 * DESCRIPTORS ... most are static, but strings and (full) configuration
 63 * descriptors are built on demand.
 64 */
 65
 66static struct usb_device_descriptor device_desc = {
 67	.bLength =		sizeof device_desc,
 68	.bDescriptorType =	USB_DT_DEVICE,
 69	/* .bcdUSB = DYNAMIC */
 70	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
 71	.bDeviceSubClass =	0,
 72	.bDeviceProtocol =	0,
 73	.idVendor =		cpu_to_le16(PRINTER_VENDOR_NUM),
 74	.idProduct =		cpu_to_le16(PRINTER_PRODUCT_NUM),
 75	.bNumConfigurations =	1
 76};
 77
 78static const struct usb_descriptor_header *otg_desc[2];
 79
 80/*-------------------------------------------------------------------------*/
 81
 82/* descriptors that are built on-demand */
 83
 84static char				product_desc [40] = DRIVER_DESC;
 85static char				serial_num [40] = "1";
 86static char				*pnp_string =
 87	"MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
 88
 89/* static strings, in UTF-8 */
 90static struct usb_string		strings [] = {
 91	[USB_GADGET_MANUFACTURER_IDX].s = "",
 92	[USB_GADGET_PRODUCT_IDX].s = product_desc,
 93	[USB_GADGET_SERIAL_IDX].s =	serial_num,
 94	{  }		/* end of list */
 95};
 96
 97static struct usb_gadget_strings	stringtab_dev = {
 98	.language	= 0x0409,	/* en-us */
 99	.strings	= strings,
100};
101
102static struct usb_gadget_strings *dev_strings[] = {
103	&stringtab_dev,
104	NULL,
105};
106
107static struct usb_configuration printer_cfg_driver = {
108	.label			= "printer",
109	.bConfigurationValue	= 1,
110	.bmAttributes		= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
111};
112
113static int printer_do_config(struct usb_configuration *c)
114{
115	struct usb_gadget	*gadget = c->cdev->gadget;
116	int			status = 0;
117
118	usb_ep_autoconfig_reset(gadget);
119
120	usb_gadget_set_selfpowered(gadget);
121
122	if (gadget_is_otg(gadget)) {
123		printer_cfg_driver.descriptors = otg_desc;
124		printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
125	}
126
127	f_printer = usb_get_function(fi_printer);
128	if (IS_ERR(f_printer))
129		return PTR_ERR(f_printer);
130
131	status = usb_add_function(c, f_printer);
132	if (status < 0)
133		usb_put_function(f_printer);
134
135	return status;
136}
137
138static int printer_bind(struct usb_composite_dev *cdev)
139{
140	struct f_printer_opts *opts;
141	int ret;
142
143	fi_printer = usb_get_function_instance("printer");
144	if (IS_ERR(fi_printer))
145		return PTR_ERR(fi_printer);
146
147	opts = container_of(fi_printer, struct f_printer_opts, func_inst);
148	opts->minor = 0;
149	opts->q_len = QLEN;
150	if (iPNPstring) {
151		opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
152		if (!opts->pnp_string) {
153			ret = -ENOMEM;
154			goto fail_put_func_inst;
155		}
156		opts->pnp_string_allocated = true;
157		/*
158		 * we don't free this memory in case of error
159		 * as printer cleanup func will do this for us
160		 */
161	} else {
162		opts->pnp_string = pnp_string;
163	}
164
165	ret = usb_string_ids_tab(cdev, strings);
166	if (ret < 0)
167		goto fail_put_func_inst;
168
169	device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
170	device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
171	device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
172
173	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
174		struct usb_descriptor_header *usb_desc;
175
176		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
177		if (!usb_desc) {
178			ret = -ENOMEM;
179			goto fail_put_func_inst;
180		}
181		usb_otg_descriptor_init(cdev->gadget, usb_desc);
182		otg_desc[0] = usb_desc;
183		otg_desc[1] = NULL;
184	}
185
186	ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
187	if (ret)
188		goto fail_free_otg_desc;
189
190	usb_composite_overwrite_options(cdev, &coverwrite);
191	return ret;
192
193fail_free_otg_desc:
194	kfree(otg_desc[0]);
195	otg_desc[0] = NULL;
196fail_put_func_inst:
197	usb_put_function_instance(fi_printer);
198	return ret;
199}
200
201static int printer_unbind(struct usb_composite_dev *cdev)
202{
203	usb_put_function(f_printer);
204	usb_put_function_instance(fi_printer);
205
206	kfree(otg_desc[0]);
207	otg_desc[0] = NULL;
208
209	return 0;
210}
211
212static struct usb_composite_driver printer_driver = {
213	.name           = shortname,
214	.dev            = &device_desc,
215	.strings        = dev_strings,
216	.max_speed      = USB_SPEED_SUPER,
217	.bind		= printer_bind,
218	.unbind		= printer_unbind,
219};
220
221module_usb_composite_driver(printer_driver);
222
223MODULE_DESCRIPTION(DRIVER_DESC);
224MODULE_AUTHOR("Craig Nadler");
225MODULE_LICENSE("GPL");
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * printer.c -- Printer gadget driver
  4 *
  5 * Copyright (C) 2003-2005 David Brownell
  6 * Copyright (C) 2006 Craig W. Nadler
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <asm/byteorder.h>
 12
 13#include <linux/usb/ch9.h>
 14#include <linux/usb/composite.h>
 15#include <linux/usb/gadget.h>
 16#include <linux/usb/g_printer.h>
 17
 18USB_GADGET_COMPOSITE_OPTIONS();
 19
 20#define DRIVER_DESC		"Printer Gadget"
 21#define DRIVER_VERSION		"2015 FEB 17"
 22
 23static const char shortname [] = "printer";
 24static const char driver_desc [] = DRIVER_DESC;
 25
 26#include "u_printer.h"
 27
 28/*-------------------------------------------------------------------------*/
 29
 30/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 31 * Instead:  allocate your own, using normal USB-IF procedures.
 32 */
 33
 34/* Thanks to NetChip Technologies for donating this product ID.
 35 */
 36#define PRINTER_VENDOR_NUM	0x0525		/* NetChip */
 37#define PRINTER_PRODUCT_NUM	0xa4a8		/* Linux-USB Printer Gadget */
 38
 39/* Some systems will want different product identifiers published in the
 40 * device descriptor, either numbers or strings or both.  These string
 41 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
 42 */
 43
 44module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
 45MODULE_PARM_DESC(iSerialNum, "1");
 46
 47static char *iPNPstring;
 48module_param(iPNPstring, charp, S_IRUGO);
 49MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
 50
 51/* Number of requests to allocate per endpoint, not used for ep0. */
 52static unsigned qlen = 10;
 53module_param(qlen, uint, S_IRUGO|S_IWUSR);
 54
 55#define QLEN	qlen
 56
 57static struct usb_function_instance *fi_printer;
 58static struct usb_function *f_printer;
 59
 60/*-------------------------------------------------------------------------*/
 61
 62/*
 63 * DESCRIPTORS ... most are static, but strings and (full) configuration
 64 * descriptors are built on demand.
 65 */
 66
 67static struct usb_device_descriptor device_desc = {
 68	.bLength =		sizeof device_desc,
 69	.bDescriptorType =	USB_DT_DEVICE,
 70	/* .bcdUSB = DYNAMIC */
 71	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
 72	.bDeviceSubClass =	0,
 73	.bDeviceProtocol =	0,
 74	.idVendor =		cpu_to_le16(PRINTER_VENDOR_NUM),
 75	.idProduct =		cpu_to_le16(PRINTER_PRODUCT_NUM),
 76	.bNumConfigurations =	1
 77};
 78
 79static const struct usb_descriptor_header *otg_desc[2];
 80
 81/*-------------------------------------------------------------------------*/
 82
 83/* descriptors that are built on-demand */
 84
 85static char				product_desc [40] = DRIVER_DESC;
 86static char				serial_num [40] = "1";
 87static char				*pnp_string =
 88	"MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
 89
 90/* static strings, in UTF-8 */
 91static struct usb_string		strings [] = {
 92	[USB_GADGET_MANUFACTURER_IDX].s = "",
 93	[USB_GADGET_PRODUCT_IDX].s = product_desc,
 94	[USB_GADGET_SERIAL_IDX].s =	serial_num,
 95	{  }		/* end of list */
 96};
 97
 98static struct usb_gadget_strings	stringtab_dev = {
 99	.language	= 0x0409,	/* en-us */
100	.strings	= strings,
101};
102
103static struct usb_gadget_strings *dev_strings[] = {
104	&stringtab_dev,
105	NULL,
106};
107
108static struct usb_configuration printer_cfg_driver = {
109	.label			= "printer",
110	.bConfigurationValue	= 1,
111	.bmAttributes		= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
112};
113
114static int printer_do_config(struct usb_configuration *c)
115{
116	struct usb_gadget	*gadget = c->cdev->gadget;
117	int			status = 0;
118
119	usb_ep_autoconfig_reset(gadget);
120
121	usb_gadget_set_selfpowered(gadget);
122
123	if (gadget_is_otg(gadget)) {
124		printer_cfg_driver.descriptors = otg_desc;
125		printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
126	}
127
128	f_printer = usb_get_function(fi_printer);
129	if (IS_ERR(f_printer))
130		return PTR_ERR(f_printer);
131
132	status = usb_add_function(c, f_printer);
133	if (status < 0)
134		usb_put_function(f_printer);
135
136	return status;
137}
138
139static int printer_bind(struct usb_composite_dev *cdev)
140{
141	struct f_printer_opts *opts;
142	int ret;
143
144	fi_printer = usb_get_function_instance("printer");
145	if (IS_ERR(fi_printer))
146		return PTR_ERR(fi_printer);
147
148	opts = container_of(fi_printer, struct f_printer_opts, func_inst);
149	opts->minor = 0;
150	opts->q_len = QLEN;
151	if (iPNPstring) {
152		opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
153		if (!opts->pnp_string) {
154			ret = -ENOMEM;
155			goto fail_put_func_inst;
156		}
157		opts->pnp_string_allocated = true;
158		/*
159		 * we don't free this memory in case of error
160		 * as printer cleanup func will do this for us
161		 */
162	} else {
163		opts->pnp_string = pnp_string;
164	}
165
166	ret = usb_string_ids_tab(cdev, strings);
167	if (ret < 0)
168		goto fail_put_func_inst;
169
170	device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
171	device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
172	device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
173
174	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
175		struct usb_descriptor_header *usb_desc;
176
177		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
178		if (!usb_desc) {
179			ret = -ENOMEM;
180			goto fail_put_func_inst;
181		}
182		usb_otg_descriptor_init(cdev->gadget, usb_desc);
183		otg_desc[0] = usb_desc;
184		otg_desc[1] = NULL;
185	}
186
187	ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
188	if (ret)
189		goto fail_free_otg_desc;
190
191	usb_composite_overwrite_options(cdev, &coverwrite);
192	return ret;
193
194fail_free_otg_desc:
195	kfree(otg_desc[0]);
196	otg_desc[0] = NULL;
197fail_put_func_inst:
198	usb_put_function_instance(fi_printer);
199	return ret;
200}
201
202static int printer_unbind(struct usb_composite_dev *cdev)
203{
204	usb_put_function(f_printer);
205	usb_put_function_instance(fi_printer);
206
207	kfree(otg_desc[0]);
208	otg_desc[0] = NULL;
209
210	return 0;
211}
212
213static struct usb_composite_driver printer_driver = {
214	.name           = shortname,
215	.dev            = &device_desc,
216	.strings        = dev_strings,
217	.max_speed      = USB_SPEED_SUPER,
218	.bind		= printer_bind,
219	.unbind		= printer_unbind,
220};
221
222module_usb_composite_driver(printer_driver);
223
224MODULE_DESCRIPTION(DRIVER_DESC);
225MODULE_AUTHOR("Craig Nadler");
226MODULE_LICENSE("GPL");