• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Gadget Function Driver for Android USB accessories
3  *
4  * Copyright (C) 2011 Google, Inc.
5  * Author: Mike Lockwood <lockwood@android.com>
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 
18 /* #define DEBUG */
19 /* #define VERBOSE_DEBUG */
20 
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/poll.h>
24 #include <linux/delay.h>
25 #include <linux/wait.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/kthread.h>
29 #include <linux/freezer.h>
30 #include <linux/kref.h>
31 
32 #include <linux/types.h>
33 #include <linux/file.h>
34 #include <linux/device.h>
35 #include <linux/miscdevice.h>
36 
37 #include <linux/hid.h>
38 #include <linux/hiddev.h>
39 #include <linux/usb.h>
40 #include <linux/usb/ch9.h>
41 #include <linux/usb/f_accessory.h>
42 
43 #include <linux/configfs.h>
44 #include <linux/usb/composite.h>
45 
46 #define MAX_INST_NAME_LEN        40
47 #define BULK_BUFFER_SIZE    16384
48 #define ACC_STRING_SIZE     256
49 
50 #define PROTOCOL_VERSION    2
51 
52 /* String IDs */
53 #define INTERFACE_STRING_INDEX	0
54 
55 /* number of tx and rx requests to allocate */
56 #define TX_REQ_MAX 4
57 #define RX_REQ_MAX 2
58 
59 struct acc_hid_dev {
60 	struct list_head	list;
61 	struct hid_device *hid;
62 	struct acc_dev *dev;
63 	/* accessory defined ID */
64 	int id;
65 	/* HID report descriptor */
66 	u8 *report_desc;
67 	/* length of HID report descriptor */
68 	int report_desc_len;
69 	/* number of bytes of report_desc we have received so far */
70 	int report_desc_offset;
71 };
72 
73 struct acc_dev {
74 	struct usb_function function;
75 	struct usb_composite_dev *cdev;
76 	spinlock_t lock;
77 	struct acc_dev_ref *ref;
78 
79 	struct usb_ep *ep_in;
80 	struct usb_ep *ep_out;
81 
82 	/* online indicates state of function_set_alt & function_unbind
83 	 * set to 1 when we connect
84 	 */
85 	int online;
86 
87 	/* disconnected indicates state of open & release
88 	 * Set to 1 when we disconnect.
89 	 * Not cleared until our file is closed.
90 	 */
91 	int disconnected;
92 
93 	/* strings sent by the host */
94 	char manufacturer[ACC_STRING_SIZE];
95 	char model[ACC_STRING_SIZE];
96 	char description[ACC_STRING_SIZE];
97 	char version[ACC_STRING_SIZE];
98 	char uri[ACC_STRING_SIZE];
99 	char serial[ACC_STRING_SIZE];
100 
101 	/* for acc_complete_set_string */
102 	int string_index;
103 
104 	/* set to 1 if we have a pending start request */
105 	int start_requested;
106 
107 	int audio_mode;
108 
109 	/* synchronize access to our device file */
110 	atomic_t open_excl;
111 
112 	struct list_head tx_idle;
113 
114 	wait_queue_head_t read_wq;
115 	wait_queue_head_t write_wq;
116 	struct usb_request *rx_req[RX_REQ_MAX];
117 	int rx_done;
118 
119 	/* delayed work for handling ACCESSORY_START */
120 	struct delayed_work start_work;
121 
122 	/* worker for registering and unregistering hid devices */
123 	struct work_struct hid_work;
124 
125 	/* list of active HID devices */
126 	struct list_head	hid_list;
127 
128 	/* list of new HID devices to register */
129 	struct list_head	new_hid_list;
130 
131 	/* list of dead HID devices to unregister */
132 	struct list_head	dead_hid_list;
133 };
134 
135 static struct usb_interface_descriptor acc_interface_desc = {
136 	.bLength                = USB_DT_INTERFACE_SIZE,
137 	.bDescriptorType        = USB_DT_INTERFACE,
138 	.bInterfaceNumber       = 0,
139 	.bNumEndpoints          = 2,
140 	.bInterfaceClass        = USB_CLASS_VENDOR_SPEC,
141 	.bInterfaceSubClass     = USB_SUBCLASS_VENDOR_SPEC,
142 	.bInterfaceProtocol     = 0,
143 };
144 
145 static struct usb_endpoint_descriptor acc_highspeed_in_desc = {
146 	.bLength                = USB_DT_ENDPOINT_SIZE,
147 	.bDescriptorType        = USB_DT_ENDPOINT,
148 	.bEndpointAddress       = USB_DIR_IN,
149 	.bmAttributes           = USB_ENDPOINT_XFER_BULK,
150 	.wMaxPacketSize         = __constant_cpu_to_le16(512),
151 };
152 
153 static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
154 	.bLength                = USB_DT_ENDPOINT_SIZE,
155 	.bDescriptorType        = USB_DT_ENDPOINT,
156 	.bEndpointAddress       = USB_DIR_OUT,
157 	.bmAttributes           = USB_ENDPOINT_XFER_BULK,
158 	.wMaxPacketSize         = __constant_cpu_to_le16(512),
159 };
160 
161 static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
162 	.bLength                = USB_DT_ENDPOINT_SIZE,
163 	.bDescriptorType        = USB_DT_ENDPOINT,
164 	.bEndpointAddress       = USB_DIR_IN,
165 	.bmAttributes           = USB_ENDPOINT_XFER_BULK,
166 };
167 
168 static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
169 	.bLength                = USB_DT_ENDPOINT_SIZE,
170 	.bDescriptorType        = USB_DT_ENDPOINT,
171 	.bEndpointAddress       = USB_DIR_OUT,
172 	.bmAttributes           = USB_ENDPOINT_XFER_BULK,
173 };
174 
175 static struct usb_descriptor_header *fs_acc_descs[] = {
176 	(struct usb_descriptor_header *) &acc_interface_desc,
177 	(struct usb_descriptor_header *) &acc_fullspeed_in_desc,
178 	(struct usb_descriptor_header *) &acc_fullspeed_out_desc,
179 	NULL,
180 };
181 
182 static struct usb_descriptor_header *hs_acc_descs[] = {
183 	(struct usb_descriptor_header *) &acc_interface_desc,
184 	(struct usb_descriptor_header *) &acc_highspeed_in_desc,
185 	(struct usb_descriptor_header *) &acc_highspeed_out_desc,
186 	NULL,
187 };
188 
189 static struct usb_string acc_string_defs[] = {
190 	[INTERFACE_STRING_INDEX].s	= "Android Accessory Interface",
191 	{  },	/* end of list */
192 };
193 
194 static struct usb_gadget_strings acc_string_table = {
195 	.language		= 0x0409,	/* en-US */
196 	.strings		= acc_string_defs,
197 };
198 
199 static struct usb_gadget_strings *acc_strings[] = {
200 	&acc_string_table,
201 	NULL,
202 };
203 
204 struct acc_dev_ref {
205 	struct kref	kref;
206 	struct acc_dev	*acc_dev;
207 };
208 
209 static struct acc_dev_ref _acc_dev_ref = {
210 	.kref = KREF_INIT(0),
211 };
212 
213 struct acc_instance {
214 	struct usb_function_instance func_inst;
215 	const char *name;
216 };
217 
get_acc_dev(void)218 static struct acc_dev *get_acc_dev(void)
219 {
220 	struct acc_dev_ref *ref = &_acc_dev_ref;
221 
222 	return kref_get_unless_zero(&ref->kref) ? ref->acc_dev : NULL;
223 }
224 
__put_acc_dev(struct kref * kref)225 static void __put_acc_dev(struct kref *kref)
226 {
227 	struct acc_dev_ref *ref = container_of(kref, struct acc_dev_ref, kref);
228 	struct acc_dev *dev = ref->acc_dev;
229 
230 	/* Cancel any async work */
231 	cancel_delayed_work_sync(&dev->start_work);
232 	cancel_work_sync(&dev->hid_work);
233 
234 	ref->acc_dev = NULL;
235 	kfree(dev);
236 }
237 
put_acc_dev(struct acc_dev * dev)238 static void put_acc_dev(struct acc_dev *dev)
239 {
240 	struct acc_dev_ref *ref = dev->ref;
241 
242 	WARN_ON(ref->acc_dev != dev);
243 	kref_put(&ref->kref, __put_acc_dev);
244 }
245 
func_to_dev(struct usb_function * f)246 static inline struct acc_dev *func_to_dev(struct usb_function *f)
247 {
248 	return container_of(f, struct acc_dev, function);
249 }
250 
acc_request_new(struct usb_ep * ep,int buffer_size)251 static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
252 {
253 	struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
254 
255 	if (!req)
256 		return NULL;
257 
258 	/* now allocate buffers for the requests */
259 	req->buf = kmalloc(buffer_size, GFP_KERNEL);
260 	if (!req->buf) {
261 		usb_ep_free_request(ep, req);
262 		return NULL;
263 	}
264 
265 	return req;
266 }
267 
acc_request_free(struct usb_request * req,struct usb_ep * ep)268 static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
269 {
270 	if (req) {
271 		kfree(req->buf);
272 		usb_ep_free_request(ep, req);
273 	}
274 }
275 
276 /* add a request to the tail of a list */
req_put(struct acc_dev * dev,struct list_head * head,struct usb_request * req)277 static void req_put(struct acc_dev *dev, struct list_head *head,
278 		struct usb_request *req)
279 {
280 	unsigned long flags;
281 
282 	spin_lock_irqsave(&dev->lock, flags);
283 	list_add_tail(&req->list, head);
284 	spin_unlock_irqrestore(&dev->lock, flags);
285 }
286 
287 /* remove a request from the head of a list */
req_get(struct acc_dev * dev,struct list_head * head)288 static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
289 {
290 	unsigned long flags;
291 	struct usb_request *req;
292 
293 	spin_lock_irqsave(&dev->lock, flags);
294 	if (list_empty(head)) {
295 		req = 0;
296 	} else {
297 		req = list_first_entry(head, struct usb_request, list);
298 		list_del(&req->list);
299 	}
300 	spin_unlock_irqrestore(&dev->lock, flags);
301 	return req;
302 }
303 
acc_set_disconnected(struct acc_dev * dev)304 static void acc_set_disconnected(struct acc_dev *dev)
305 {
306 	dev->disconnected = 1;
307 }
308 
acc_complete_in(struct usb_ep * ep,struct usb_request * req)309 static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
310 {
311 	struct acc_dev *dev = get_acc_dev();
312 
313 	if (!dev)
314 		return;
315 
316 	if (req->status == -ESHUTDOWN) {
317 		pr_debug("acc_complete_in set disconnected");
318 		acc_set_disconnected(dev);
319 	}
320 
321 	req_put(dev, &dev->tx_idle, req);
322 
323 	wake_up(&dev->write_wq);
324 	put_acc_dev(dev);
325 }
326 
acc_complete_out(struct usb_ep * ep,struct usb_request * req)327 static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
328 {
329 	struct acc_dev *dev = get_acc_dev();
330 
331 	if (!dev)
332 		return;
333 
334 	dev->rx_done = 1;
335 	if (req->status == -ESHUTDOWN) {
336 		pr_debug("acc_complete_out set disconnected");
337 		acc_set_disconnected(dev);
338 	}
339 
340 	wake_up(&dev->read_wq);
341 	put_acc_dev(dev);
342 }
343 
acc_complete_set_string(struct usb_ep * ep,struct usb_request * req)344 static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
345 {
346 	struct acc_dev	*dev = ep->driver_data;
347 	char *string_dest = NULL;
348 	int length = req->actual;
349 
350 	if (req->status != 0) {
351 		pr_err("acc_complete_set_string, err %d\n", req->status);
352 		return;
353 	}
354 
355 	switch (dev->string_index) {
356 	case ACCESSORY_STRING_MANUFACTURER:
357 		string_dest = dev->manufacturer;
358 		break;
359 	case ACCESSORY_STRING_MODEL:
360 		string_dest = dev->model;
361 		break;
362 	case ACCESSORY_STRING_DESCRIPTION:
363 		string_dest = dev->description;
364 		break;
365 	case ACCESSORY_STRING_VERSION:
366 		string_dest = dev->version;
367 		break;
368 	case ACCESSORY_STRING_URI:
369 		string_dest = dev->uri;
370 		break;
371 	case ACCESSORY_STRING_SERIAL:
372 		string_dest = dev->serial;
373 		break;
374 	}
375 	if (string_dest) {
376 		unsigned long flags;
377 
378 		if (length >= ACC_STRING_SIZE)
379 			length = ACC_STRING_SIZE - 1;
380 
381 		spin_lock_irqsave(&dev->lock, flags);
382 		memcpy(string_dest, req->buf, length);
383 		/* ensure zero termination */
384 		string_dest[length] = 0;
385 		spin_unlock_irqrestore(&dev->lock, flags);
386 	} else {
387 		pr_err("unknown accessory string index %d\n",
388 			dev->string_index);
389 	}
390 }
391 
acc_complete_set_hid_report_desc(struct usb_ep * ep,struct usb_request * req)392 static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
393 		struct usb_request *req)
394 {
395 	struct acc_hid_dev *hid = req->context;
396 	struct acc_dev *dev = hid->dev;
397 	int length = req->actual;
398 
399 	if (req->status != 0) {
400 		pr_err("acc_complete_set_hid_report_desc, err %d\n",
401 			req->status);
402 		return;
403 	}
404 
405 	memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
406 	hid->report_desc_offset += length;
407 	if (hid->report_desc_offset == hid->report_desc_len) {
408 		/* After we have received the entire report descriptor
409 		 * we schedule work to initialize the HID device
410 		 */
411 		schedule_work(&dev->hid_work);
412 	}
413 }
414 
acc_complete_send_hid_event(struct usb_ep * ep,struct usb_request * req)415 static void acc_complete_send_hid_event(struct usb_ep *ep,
416 		struct usb_request *req)
417 {
418 	struct acc_hid_dev *hid = req->context;
419 	int length = req->actual;
420 
421 	if (req->status != 0) {
422 		pr_err("acc_complete_send_hid_event, err %d\n", req->status);
423 		return;
424 	}
425 
426 	hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
427 }
428 
acc_hid_parse(struct hid_device * hid)429 static int acc_hid_parse(struct hid_device *hid)
430 {
431 	struct acc_hid_dev *hdev = hid->driver_data;
432 
433 	hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
434 	return 0;
435 }
436 
acc_hid_start(struct hid_device * hid)437 static int acc_hid_start(struct hid_device *hid)
438 {
439 	return 0;
440 }
441 
acc_hid_stop(struct hid_device * hid)442 static void acc_hid_stop(struct hid_device *hid)
443 {
444 }
445 
acc_hid_open(struct hid_device * hid)446 static int acc_hid_open(struct hid_device *hid)
447 {
448 	return 0;
449 }
450 
acc_hid_close(struct hid_device * hid)451 static void acc_hid_close(struct hid_device *hid)
452 {
453 }
454 
acc_hid_raw_request(struct hid_device * hid,unsigned char reportnum,__u8 * buf,size_t len,unsigned char rtype,int reqtype)455 static int acc_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
456 	__u8 *buf, size_t len, unsigned char rtype, int reqtype)
457 {
458 	return 0;
459 }
460 
461 static struct hid_ll_driver acc_hid_ll_driver = {
462 	.parse = acc_hid_parse,
463 	.start = acc_hid_start,
464 	.stop = acc_hid_stop,
465 	.open = acc_hid_open,
466 	.close = acc_hid_close,
467 	.raw_request = acc_hid_raw_request,
468 };
469 
acc_hid_new(struct acc_dev * dev,int id,int desc_len)470 static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
471 		int id, int desc_len)
472 {
473 	struct acc_hid_dev *hdev;
474 
475 	hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
476 	if (!hdev)
477 		return NULL;
478 	hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
479 	if (!hdev->report_desc) {
480 		kfree(hdev);
481 		return NULL;
482 	}
483 	hdev->dev = dev;
484 	hdev->id = id;
485 	hdev->report_desc_len = desc_len;
486 
487 	return hdev;
488 }
489 
acc_hid_get(struct list_head * list,int id)490 static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
491 {
492 	struct acc_hid_dev *hid;
493 
494 	list_for_each_entry(hid, list, list) {
495 		if (hid->id == id)
496 			return hid;
497 	}
498 	return NULL;
499 }
500 
acc_register_hid(struct acc_dev * dev,int id,int desc_length)501 static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
502 {
503 	struct acc_hid_dev *hid;
504 	unsigned long flags;
505 
506 	/* report descriptor length must be > 0 */
507 	if (desc_length <= 0)
508 		return -EINVAL;
509 
510 	spin_lock_irqsave(&dev->lock, flags);
511 	/* replace HID if one already exists with this ID */
512 	hid = acc_hid_get(&dev->hid_list, id);
513 	if (!hid)
514 		hid = acc_hid_get(&dev->new_hid_list, id);
515 	if (hid)
516 		list_move(&hid->list, &dev->dead_hid_list);
517 
518 	hid = acc_hid_new(dev, id, desc_length);
519 	if (!hid) {
520 		spin_unlock_irqrestore(&dev->lock, flags);
521 		return -ENOMEM;
522 	}
523 
524 	list_add(&hid->list, &dev->new_hid_list);
525 	spin_unlock_irqrestore(&dev->lock, flags);
526 
527 	/* schedule work to register the HID device */
528 	schedule_work(&dev->hid_work);
529 	return 0;
530 }
531 
acc_unregister_hid(struct acc_dev * dev,int id)532 static int acc_unregister_hid(struct acc_dev *dev, int id)
533 {
534 	struct acc_hid_dev *hid;
535 	unsigned long flags;
536 
537 	spin_lock_irqsave(&dev->lock, flags);
538 	hid = acc_hid_get(&dev->hid_list, id);
539 	if (!hid)
540 		hid = acc_hid_get(&dev->new_hid_list, id);
541 	if (!hid) {
542 		spin_unlock_irqrestore(&dev->lock, flags);
543 		return -EINVAL;
544 	}
545 
546 	list_move(&hid->list, &dev->dead_hid_list);
547 	spin_unlock_irqrestore(&dev->lock, flags);
548 
549 	schedule_work(&dev->hid_work);
550 	return 0;
551 }
552 
create_bulk_endpoints(struct acc_dev * dev,struct usb_endpoint_descriptor * in_desc,struct usb_endpoint_descriptor * out_desc)553 static int create_bulk_endpoints(struct acc_dev *dev,
554 				struct usb_endpoint_descriptor *in_desc,
555 				struct usb_endpoint_descriptor *out_desc)
556 {
557 	struct usb_composite_dev *cdev = dev->cdev;
558 	struct usb_request *req;
559 	struct usb_ep *ep;
560 	int i;
561 
562 	DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
563 
564 	ep = usb_ep_autoconfig(cdev->gadget, in_desc);
565 	if (!ep) {
566 		DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
567 		return -ENODEV;
568 	}
569 	DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
570 	ep->driver_data = dev;		/* claim the endpoint */
571 	dev->ep_in = ep;
572 
573 	ep = usb_ep_autoconfig(cdev->gadget, out_desc);
574 	if (!ep) {
575 		DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
576 		return -ENODEV;
577 	}
578 	DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
579 	ep->driver_data = dev;		/* claim the endpoint */
580 	dev->ep_out = ep;
581 
582 	/* now allocate requests for our endpoints */
583 	for (i = 0; i < TX_REQ_MAX; i++) {
584 		req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
585 		if (!req)
586 			goto fail;
587 		req->complete = acc_complete_in;
588 		req_put(dev, &dev->tx_idle, req);
589 	}
590 	for (i = 0; i < RX_REQ_MAX; i++) {
591 		req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
592 		if (!req)
593 			goto fail;
594 		req->complete = acc_complete_out;
595 		dev->rx_req[i] = req;
596 	}
597 
598 	return 0;
599 
600 fail:
601 	pr_err("acc_bind() could not allocate requests\n");
602 	while ((req = req_get(dev, &dev->tx_idle)))
603 		acc_request_free(req, dev->ep_in);
604 	for (i = 0; i < RX_REQ_MAX; i++)
605 		acc_request_free(dev->rx_req[i], dev->ep_out);
606 	return -1;
607 }
608 
acc_read(struct file * fp,char __user * buf,size_t count,loff_t * pos)609 static ssize_t acc_read(struct file *fp, char __user *buf,
610 	size_t count, loff_t *pos)
611 {
612 	struct acc_dev *dev = fp->private_data;
613 	struct usb_request *req;
614 	ssize_t r = count;
615 	ssize_t data_length;
616 	unsigned xfer;
617 	int ret = 0;
618 
619 	pr_debug("acc_read(%zu)\n", count);
620 
621 	if (dev->disconnected) {
622 		pr_debug("acc_read disconnected");
623 		return -ENODEV;
624 	}
625 
626 	if (count > BULK_BUFFER_SIZE)
627 		count = BULK_BUFFER_SIZE;
628 
629 	/* we will block until we're online */
630 	pr_debug("acc_read: waiting for online\n");
631 	ret = wait_event_interruptible(dev->read_wq, dev->online);
632 	if (ret < 0) {
633 		r = ret;
634 		goto done;
635 	}
636 
637 	/*
638 	 * Calculate the data length by considering termination character.
639 	 * Then compansite the difference of rounding up to
640 	 * integer multiple of maxpacket size.
641 	 */
642 	data_length = count;
643 	data_length += dev->ep_out->maxpacket - 1;
644 	data_length -= data_length % dev->ep_out->maxpacket;
645 
646 	if (dev->rx_done) {
647 		// last req cancelled. try to get it.
648 		req = dev->rx_req[0];
649 		goto copy_data;
650 	}
651 
652 requeue_req:
653 	/* queue a request */
654 	req = dev->rx_req[0];
655 	req->length = data_length;
656 	dev->rx_done = 0;
657 	ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
658 	if (ret < 0) {
659 		r = -EIO;
660 		goto done;
661 	} else {
662 		pr_debug("rx %p queue\n", req);
663 	}
664 
665 	/* wait for a request to complete */
666 	ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
667 	if (ret < 0) {
668 		r = ret;
669 		ret = usb_ep_dequeue(dev->ep_out, req);
670 		if (ret != 0) {
671 			// cancel failed. There can be a data already received.
672 			// it will be retrieved in the next read.
673 			pr_debug("acc_read: cancelling failed %d", ret);
674 		}
675 		goto done;
676 	}
677 
678 copy_data:
679 	dev->rx_done = 0;
680 	if (dev->online) {
681 		/* If we got a 0-len packet, throw it back and try again. */
682 		if (req->actual == 0)
683 			goto requeue_req;
684 
685 		pr_debug("rx %p %u\n", req, req->actual);
686 		xfer = (req->actual < count) ? req->actual : count;
687 		r = xfer;
688 		if (copy_to_user(buf, req->buf, xfer))
689 			r = -EFAULT;
690 	} else
691 		r = -EIO;
692 
693 done:
694 	pr_debug("acc_read returning %zd\n", r);
695 	return r;
696 }
697 
acc_write(struct file * fp,const char __user * buf,size_t count,loff_t * pos)698 static ssize_t acc_write(struct file *fp, const char __user *buf,
699 	size_t count, loff_t *pos)
700 {
701 	struct acc_dev *dev = fp->private_data;
702 	struct usb_request *req = 0;
703 	ssize_t r = count;
704 	unsigned xfer;
705 	int ret;
706 
707 	pr_debug("acc_write(%zu)\n", count);
708 
709 	if (!dev->online || dev->disconnected) {
710 		pr_debug("acc_write disconnected or not online");
711 		return -ENODEV;
712 	}
713 
714 	while (count > 0) {
715 		if (!dev->online) {
716 			pr_debug("acc_write dev->error\n");
717 			r = -EIO;
718 			break;
719 		}
720 
721 		/* get an idle tx request to use */
722 		req = 0;
723 		ret = wait_event_interruptible(dev->write_wq,
724 			((req = req_get(dev, &dev->tx_idle)) || !dev->online));
725 		if (!req) {
726 			r = ret;
727 			break;
728 		}
729 
730 		if (count > BULK_BUFFER_SIZE) {
731 			xfer = BULK_BUFFER_SIZE;
732 			/* ZLP, They will be more TX requests so not yet. */
733 			req->zero = 0;
734 		} else {
735 			xfer = count;
736 			/*
737 			 * If the data length is a multple of the
738 			 * maxpacket size then send a zero length packet(ZLP).
739 			 */
740 			req->zero = ((xfer % dev->ep_in->maxpacket) == 0);
741 		}
742 		if (copy_from_user(req->buf, buf, xfer)) {
743 			r = -EFAULT;
744 			break;
745 		}
746 
747 		req->length = xfer;
748 		ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
749 		if (ret < 0) {
750 			pr_debug("acc_write: xfer error %d\n", ret);
751 			r = -EIO;
752 			break;
753 		}
754 
755 		buf += xfer;
756 		count -= xfer;
757 
758 		/* zero this so we don't try to free it on error exit */
759 		req = 0;
760 	}
761 
762 	if (req)
763 		req_put(dev, &dev->tx_idle, req);
764 
765 	pr_debug("acc_write returning %zd\n", r);
766 	return r;
767 }
768 
acc_ioctl(struct file * fp,unsigned code,unsigned long value)769 static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
770 {
771 	struct acc_dev *dev = fp->private_data;
772 	char *src = NULL;
773 	int ret;
774 
775 	switch (code) {
776 	case ACCESSORY_GET_STRING_MANUFACTURER:
777 		src = dev->manufacturer;
778 		break;
779 	case ACCESSORY_GET_STRING_MODEL:
780 		src = dev->model;
781 		break;
782 	case ACCESSORY_GET_STRING_DESCRIPTION:
783 		src = dev->description;
784 		break;
785 	case ACCESSORY_GET_STRING_VERSION:
786 		src = dev->version;
787 		break;
788 	case ACCESSORY_GET_STRING_URI:
789 		src = dev->uri;
790 		break;
791 	case ACCESSORY_GET_STRING_SERIAL:
792 		src = dev->serial;
793 		break;
794 	case ACCESSORY_IS_START_REQUESTED:
795 		return dev->start_requested;
796 	case ACCESSORY_GET_AUDIO_MODE:
797 		return dev->audio_mode;
798 	}
799 	if (!src)
800 		return -EINVAL;
801 
802 	ret = strlen(src) + 1;
803 	if (copy_to_user((void __user *)value, src, ret))
804 		ret = -EFAULT;
805 	return ret;
806 }
807 
acc_open(struct inode * ip,struct file * fp)808 static int acc_open(struct inode *ip, struct file *fp)
809 {
810 	struct acc_dev *dev = get_acc_dev();
811 
812 	if (!dev)
813 		return -ENODEV;
814 
815 	if (atomic_xchg(&dev->open_excl, 1)) {
816 		put_acc_dev(dev);
817 		return -EBUSY;
818 	}
819 
820 	dev->disconnected = 0;
821 	fp->private_data = dev;
822 	return 0;
823 }
824 
acc_release(struct inode * ip,struct file * fp)825 static int acc_release(struct inode *ip, struct file *fp)
826 {
827 	struct acc_dev *dev = fp->private_data;
828 
829 	if (!dev)
830 		return -ENOENT;
831 
832 	/* indicate that we are disconnected
833 	 * still could be online so don't touch online flag
834 	 */
835 	dev->disconnected = 1;
836 
837 	fp->private_data = NULL;
838 	WARN_ON(!atomic_xchg(&dev->open_excl, 0));
839 	put_acc_dev(dev);
840 	return 0;
841 }
842 
843 /* file operations for /dev/usb_accessory */
844 static const struct file_operations acc_fops = {
845 	.owner = THIS_MODULE,
846 	.read = acc_read,
847 	.write = acc_write,
848 	.unlocked_ioctl = acc_ioctl,
849 	.open = acc_open,
850 	.release = acc_release,
851 };
852 
acc_hid_probe(struct hid_device * hdev,const struct hid_device_id * id)853 static int acc_hid_probe(struct hid_device *hdev,
854 		const struct hid_device_id *id)
855 {
856 	int ret;
857 
858 	ret = hid_parse(hdev);
859 	if (ret)
860 		return ret;
861 	return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
862 }
863 
864 static struct miscdevice acc_device = {
865 	.minor = MISC_DYNAMIC_MINOR,
866 	.name = "usb_accessory",
867 	.fops = &acc_fops,
868 };
869 
870 static const struct hid_device_id acc_hid_table[] = {
871 	{ HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
872 	{ }
873 };
874 
875 static struct hid_driver acc_hid_driver = {
876 	.name = "USB accessory",
877 	.id_table = acc_hid_table,
878 	.probe = acc_hid_probe,
879 };
880 
acc_complete_setup_noop(struct usb_ep * ep,struct usb_request * req)881 static void acc_complete_setup_noop(struct usb_ep *ep, struct usb_request *req)
882 {
883 	/*
884 	 * Default no-op function when nothing needs to be done for the
885 	 * setup request
886 	 */
887 }
888 
acc_ctrlrequest(struct usb_composite_dev * cdev,const struct usb_ctrlrequest * ctrl)889 int acc_ctrlrequest(struct usb_composite_dev *cdev,
890 				const struct usb_ctrlrequest *ctrl)
891 {
892 	struct acc_dev	*dev = get_acc_dev();
893 	int	value = -EOPNOTSUPP;
894 	struct acc_hid_dev *hid;
895 	int offset;
896 	u8 b_requestType = ctrl->bRequestType;
897 	u8 b_request = ctrl->bRequest;
898 	u16	w_index = le16_to_cpu(ctrl->wIndex);
899 	u16	w_value = le16_to_cpu(ctrl->wValue);
900 	u16	w_length = le16_to_cpu(ctrl->wLength);
901 	unsigned long flags;
902 
903 	/*
904 	 * If instance is not created which is the case in power off charging
905 	 * mode, dev will be NULL. Hence return error if it is the case.
906 	 */
907 	if (!dev)
908 		return -ENODEV;
909 
910 	if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
911 		if (b_request == ACCESSORY_START) {
912 			dev->start_requested = 1;
913 			schedule_delayed_work(
914 				&dev->start_work, msecs_to_jiffies(10));
915 			value = 0;
916 			cdev->req->complete = acc_complete_setup_noop;
917 		} else if (b_request == ACCESSORY_SEND_STRING) {
918 			dev->string_index = w_index;
919 			cdev->gadget->ep0->driver_data = dev;
920 			cdev->req->complete = acc_complete_set_string;
921 			value = w_length;
922 		} else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
923 				w_index == 0 && w_length == 0) {
924 			dev->audio_mode = w_value;
925 			cdev->req->complete = acc_complete_setup_noop;
926 			value = 0;
927 		} else if (b_request == ACCESSORY_REGISTER_HID) {
928 			cdev->req->complete = acc_complete_setup_noop;
929 			value = acc_register_hid(dev, w_value, w_index);
930 		} else if (b_request == ACCESSORY_UNREGISTER_HID) {
931 			cdev->req->complete = acc_complete_setup_noop;
932 			value = acc_unregister_hid(dev, w_value);
933 		} else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
934 			spin_lock_irqsave(&dev->lock, flags);
935 			hid = acc_hid_get(&dev->new_hid_list, w_value);
936 			spin_unlock_irqrestore(&dev->lock, flags);
937 			if (!hid) {
938 				value = -EINVAL;
939 				goto err;
940 			}
941 			offset = w_index;
942 			if (offset != hid->report_desc_offset
943 				|| offset + w_length > hid->report_desc_len) {
944 				value = -EINVAL;
945 				goto err;
946 			}
947 			cdev->req->context = hid;
948 			cdev->req->complete = acc_complete_set_hid_report_desc;
949 			value = w_length;
950 		} else if (b_request == ACCESSORY_SEND_HID_EVENT) {
951 			spin_lock_irqsave(&dev->lock, flags);
952 			hid = acc_hid_get(&dev->hid_list, w_value);
953 			spin_unlock_irqrestore(&dev->lock, flags);
954 			if (!hid) {
955 				value = -EINVAL;
956 				goto err;
957 			}
958 			cdev->req->context = hid;
959 			cdev->req->complete = acc_complete_send_hid_event;
960 			value = w_length;
961 		}
962 	} else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
963 		if (b_request == ACCESSORY_GET_PROTOCOL) {
964 			*((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
965 			value = sizeof(u16);
966 			cdev->req->complete = acc_complete_setup_noop;
967 			/* clear any string left over from a previous session */
968 			memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
969 			memset(dev->model, 0, sizeof(dev->model));
970 			memset(dev->description, 0, sizeof(dev->description));
971 			memset(dev->version, 0, sizeof(dev->version));
972 			memset(dev->uri, 0, sizeof(dev->uri));
973 			memset(dev->serial, 0, sizeof(dev->serial));
974 			dev->start_requested = 0;
975 			dev->audio_mode = 0;
976 		}
977 	}
978 
979 	if (value >= 0) {
980 		cdev->req->zero = 0;
981 		cdev->req->length = value;
982 		value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
983 		if (value < 0)
984 			ERROR(cdev, "%s setup response queue error\n",
985 				__func__);
986 	}
987 
988 err:
989 	if (value == -EOPNOTSUPP)
990 		VDBG(cdev,
991 			"unknown class-specific control req "
992 			"%02x.%02x v%04x i%04x l%u\n",
993 			ctrl->bRequestType, ctrl->bRequest,
994 			w_value, w_index, w_length);
995 	put_acc_dev(dev);
996 	return value;
997 }
998 EXPORT_SYMBOL_GPL(acc_ctrlrequest);
999 
1000 static int
__acc_function_bind(struct usb_configuration * c,struct usb_function * f,bool configfs)1001 __acc_function_bind(struct usb_configuration *c,
1002 			struct usb_function *f, bool configfs)
1003 {
1004 	struct usb_composite_dev *cdev = c->cdev;
1005 	struct acc_dev	*dev = func_to_dev(f);
1006 	int			id;
1007 	int			ret;
1008 
1009 	DBG(cdev, "acc_function_bind dev: %p\n", dev);
1010 
1011 	if (configfs) {
1012 		if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
1013 			ret = usb_string_id(c->cdev);
1014 			if (ret < 0)
1015 				return ret;
1016 			acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
1017 			acc_interface_desc.iInterface = ret;
1018 		}
1019 		dev->cdev = c->cdev;
1020 	}
1021 	ret = hid_register_driver(&acc_hid_driver);
1022 	if (ret)
1023 		return ret;
1024 
1025 	dev->start_requested = 0;
1026 
1027 	/* allocate interface ID(s) */
1028 	id = usb_interface_id(c, f);
1029 	if (id < 0)
1030 		return id;
1031 	acc_interface_desc.bInterfaceNumber = id;
1032 
1033 	/* allocate endpoints */
1034 	ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
1035 			&acc_fullspeed_out_desc);
1036 	if (ret)
1037 		return ret;
1038 
1039 	/* support high speed hardware */
1040 	if (gadget_is_dualspeed(c->cdev->gadget)) {
1041 		acc_highspeed_in_desc.bEndpointAddress =
1042 			acc_fullspeed_in_desc.bEndpointAddress;
1043 		acc_highspeed_out_desc.bEndpointAddress =
1044 			acc_fullspeed_out_desc.bEndpointAddress;
1045 	}
1046 
1047 	DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
1048 			gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1049 			f->name, dev->ep_in->name, dev->ep_out->name);
1050 	return 0;
1051 }
1052 
1053 static int
acc_function_bind_configfs(struct usb_configuration * c,struct usb_function * f)1054 acc_function_bind_configfs(struct usb_configuration *c,
1055 			struct usb_function *f) {
1056 	return __acc_function_bind(c, f, true);
1057 }
1058 
1059 static void
kill_all_hid_devices(struct acc_dev * dev)1060 kill_all_hid_devices(struct acc_dev *dev)
1061 {
1062 	struct acc_hid_dev *hid;
1063 	struct list_head *entry, *temp;
1064 	unsigned long flags;
1065 
1066 	spin_lock_irqsave(&dev->lock, flags);
1067 	list_for_each_safe(entry, temp, &dev->hid_list) {
1068 		hid = list_entry(entry, struct acc_hid_dev, list);
1069 		list_del(&hid->list);
1070 		list_add(&hid->list, &dev->dead_hid_list);
1071 	}
1072 	list_for_each_safe(entry, temp, &dev->new_hid_list) {
1073 		hid = list_entry(entry, struct acc_hid_dev, list);
1074 		list_del(&hid->list);
1075 		list_add(&hid->list, &dev->dead_hid_list);
1076 	}
1077 	spin_unlock_irqrestore(&dev->lock, flags);
1078 
1079 	schedule_work(&dev->hid_work);
1080 }
1081 
1082 static void
acc_hid_unbind(struct acc_dev * dev)1083 acc_hid_unbind(struct acc_dev *dev)
1084 {
1085 	hid_unregister_driver(&acc_hid_driver);
1086 	kill_all_hid_devices(dev);
1087 }
1088 
1089 static void
acc_function_unbind(struct usb_configuration * c,struct usb_function * f)1090 acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
1091 {
1092 	struct acc_dev	*dev = func_to_dev(f);
1093 	struct usb_request *req;
1094 	int i;
1095 
1096 	dev->online = 0;		/* clear online flag */
1097 	wake_up(&dev->read_wq);		/* unblock reads on closure */
1098 	wake_up(&dev->write_wq);	/* likewise for writes */
1099 
1100 	while ((req = req_get(dev, &dev->tx_idle)))
1101 		acc_request_free(req, dev->ep_in);
1102 	for (i = 0; i < RX_REQ_MAX; i++)
1103 		acc_request_free(dev->rx_req[i], dev->ep_out);
1104 
1105 	acc_hid_unbind(dev);
1106 }
1107 
acc_start_work(struct work_struct * data)1108 static void acc_start_work(struct work_struct *data)
1109 {
1110 	char *envp[2] = { "ACCESSORY=START", NULL };
1111 
1112 	kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
1113 }
1114 
acc_hid_init(struct acc_hid_dev * hdev)1115 static int acc_hid_init(struct acc_hid_dev *hdev)
1116 {
1117 	struct hid_device *hid;
1118 	int ret;
1119 
1120 	hid = hid_allocate_device();
1121 	if (IS_ERR(hid))
1122 		return PTR_ERR(hid);
1123 
1124 	hid->ll_driver = &acc_hid_ll_driver;
1125 	hid->dev.parent = acc_device.this_device;
1126 
1127 	hid->bus = BUS_USB;
1128 	hid->vendor = HID_ANY_ID;
1129 	hid->product = HID_ANY_ID;
1130 	hid->driver_data = hdev;
1131 	ret = hid_add_device(hid);
1132 	if (ret) {
1133 		pr_err("can't add hid device: %d\n", ret);
1134 		hid_destroy_device(hid);
1135 		return ret;
1136 	}
1137 
1138 	hdev->hid = hid;
1139 	return 0;
1140 }
1141 
acc_hid_delete(struct acc_hid_dev * hid)1142 static void acc_hid_delete(struct acc_hid_dev *hid)
1143 {
1144 	kfree(hid->report_desc);
1145 	kfree(hid);
1146 }
1147 
acc_hid_work(struct work_struct * data)1148 static void acc_hid_work(struct work_struct *data)
1149 {
1150 	struct acc_dev *dev = get_acc_dev();
1151 	struct list_head	*entry, *temp;
1152 	struct acc_hid_dev *hid;
1153 	struct list_head	new_list, dead_list;
1154 	unsigned long flags;
1155 
1156 	if (!dev)
1157 		return;
1158 
1159 	INIT_LIST_HEAD(&new_list);
1160 
1161 	spin_lock_irqsave(&dev->lock, flags);
1162 
1163 	/* copy hids that are ready for initialization to new_list */
1164 	list_for_each_safe(entry, temp, &dev->new_hid_list) {
1165 		hid = list_entry(entry, struct acc_hid_dev, list);
1166 		if (hid->report_desc_offset == hid->report_desc_len)
1167 			list_move(&hid->list, &new_list);
1168 	}
1169 
1170 	if (list_empty(&dev->dead_hid_list)) {
1171 		INIT_LIST_HEAD(&dead_list);
1172 	} else {
1173 		/* move all of dev->dead_hid_list to dead_list */
1174 		dead_list.prev = dev->dead_hid_list.prev;
1175 		dead_list.next = dev->dead_hid_list.next;
1176 		dead_list.next->prev = &dead_list;
1177 		dead_list.prev->next = &dead_list;
1178 		INIT_LIST_HEAD(&dev->dead_hid_list);
1179 	}
1180 
1181 	spin_unlock_irqrestore(&dev->lock, flags);
1182 
1183 	/* register new HID devices */
1184 	list_for_each_safe(entry, temp, &new_list) {
1185 		hid = list_entry(entry, struct acc_hid_dev, list);
1186 		if (acc_hid_init(hid)) {
1187 			pr_err("can't add HID device %p\n", hid);
1188 			acc_hid_delete(hid);
1189 		} else {
1190 			spin_lock_irqsave(&dev->lock, flags);
1191 			list_move(&hid->list, &dev->hid_list);
1192 			spin_unlock_irqrestore(&dev->lock, flags);
1193 		}
1194 	}
1195 
1196 	/* remove dead HID devices */
1197 	list_for_each_safe(entry, temp, &dead_list) {
1198 		hid = list_entry(entry, struct acc_hid_dev, list);
1199 		list_del(&hid->list);
1200 		if (hid->hid)
1201 			hid_destroy_device(hid->hid);
1202 		acc_hid_delete(hid);
1203 	}
1204 
1205 	put_acc_dev(dev);
1206 }
1207 
acc_function_set_alt(struct usb_function * f,unsigned intf,unsigned alt)1208 static int acc_function_set_alt(struct usb_function *f,
1209 		unsigned intf, unsigned alt)
1210 {
1211 	struct acc_dev	*dev = func_to_dev(f);
1212 	struct usb_composite_dev *cdev = f->config->cdev;
1213 	int ret;
1214 
1215 	DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
1216 
1217 	ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
1218 	if (ret)
1219 		return ret;
1220 
1221 	ret = usb_ep_enable(dev->ep_in);
1222 	if (ret)
1223 		return ret;
1224 
1225 	ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
1226 	if (ret)
1227 		return ret;
1228 
1229 	ret = usb_ep_enable(dev->ep_out);
1230 	if (ret) {
1231 		usb_ep_disable(dev->ep_in);
1232 		return ret;
1233 	}
1234 
1235 	dev->online = 1;
1236 	dev->disconnected = 0; /* if online then not disconnected */
1237 
1238 	/* readers may be blocked waiting for us to go online */
1239 	wake_up(&dev->read_wq);
1240 	return 0;
1241 }
1242 
acc_function_disable(struct usb_function * f)1243 static void acc_function_disable(struct usb_function *f)
1244 {
1245 	struct acc_dev	*dev = func_to_dev(f);
1246 	struct usb_composite_dev	*cdev = dev->cdev;
1247 
1248 	DBG(cdev, "acc_function_disable\n");
1249 	acc_set_disconnected(dev); /* this now only sets disconnected */
1250 	dev->online = 0; /* so now need to clear online flag here too */
1251 	usb_ep_disable(dev->ep_in);
1252 	usb_ep_disable(dev->ep_out);
1253 
1254 	/* readers may be blocked waiting for us to go online */
1255 	wake_up(&dev->read_wq);
1256 
1257 	VDBG(cdev, "%s disabled\n", dev->function.name);
1258 }
1259 
acc_setup(void)1260 static int acc_setup(void)
1261 {
1262 	struct acc_dev_ref *ref = &_acc_dev_ref;
1263 	struct acc_dev *dev;
1264 	int ret;
1265 
1266 	if (kref_read(&ref->kref))
1267 		return -EBUSY;
1268 
1269 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1270 	if (!dev)
1271 		return -ENOMEM;
1272 
1273 	spin_lock_init(&dev->lock);
1274 	init_waitqueue_head(&dev->read_wq);
1275 	init_waitqueue_head(&dev->write_wq);
1276 	atomic_set(&dev->open_excl, 0);
1277 	INIT_LIST_HEAD(&dev->tx_idle);
1278 	INIT_LIST_HEAD(&dev->hid_list);
1279 	INIT_LIST_HEAD(&dev->new_hid_list);
1280 	INIT_LIST_HEAD(&dev->dead_hid_list);
1281 	INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
1282 	INIT_WORK(&dev->hid_work, acc_hid_work);
1283 
1284 	dev->ref = ref;
1285 	if (cmpxchg_relaxed(&ref->acc_dev, NULL, dev)) {
1286 		ret = -EBUSY;
1287 		goto err_free_dev;
1288 	}
1289 
1290 	ret = misc_register(&acc_device);
1291 	if (ret)
1292 		goto err_zap_ptr;
1293 
1294 	kref_init(&ref->kref);
1295 	return 0;
1296 
1297 err_zap_ptr:
1298 	ref->acc_dev = NULL;
1299 err_free_dev:
1300 	kfree(dev);
1301 	pr_err("USB accessory gadget driver failed to initialize\n");
1302 	return ret;
1303 }
1304 
acc_disconnect(void)1305 void acc_disconnect(void)
1306 {
1307 	struct acc_dev *dev = get_acc_dev();
1308 
1309 	if (!dev)
1310 		return;
1311 
1312 	/* unregister all HID devices if USB is disconnected */
1313 	kill_all_hid_devices(dev);
1314 	put_acc_dev(dev);
1315 }
1316 EXPORT_SYMBOL_GPL(acc_disconnect);
1317 
acc_cleanup(void)1318 static void acc_cleanup(void)
1319 {
1320 	struct acc_dev *dev = get_acc_dev();
1321 
1322 	misc_deregister(&acc_device);
1323 	put_acc_dev(dev);
1324 	put_acc_dev(dev); /* Pairs with kref_init() in acc_setup() */
1325 }
to_acc_instance(struct config_item * item)1326 static struct acc_instance *to_acc_instance(struct config_item *item)
1327 {
1328 	return container_of(to_config_group(item), struct acc_instance,
1329 		func_inst.group);
1330 }
1331 
acc_attr_release(struct config_item * item)1332 static void acc_attr_release(struct config_item *item)
1333 {
1334 	struct acc_instance *fi_acc = to_acc_instance(item);
1335 
1336 	usb_put_function_instance(&fi_acc->func_inst);
1337 }
1338 
1339 static struct configfs_item_operations acc_item_ops = {
1340 	.release        = acc_attr_release,
1341 };
1342 
1343 static struct config_item_type acc_func_type = {
1344 	.ct_item_ops    = &acc_item_ops,
1345 	.ct_owner       = THIS_MODULE,
1346 };
1347 
to_fi_acc(struct usb_function_instance * fi)1348 static struct acc_instance *to_fi_acc(struct usb_function_instance *fi)
1349 {
1350 	return container_of(fi, struct acc_instance, func_inst);
1351 }
1352 
acc_set_inst_name(struct usb_function_instance * fi,const char * name)1353 static int acc_set_inst_name(struct usb_function_instance *fi, const char *name)
1354 {
1355 	struct acc_instance *fi_acc;
1356 	char *ptr;
1357 	int name_len;
1358 
1359 	name_len = strlen(name) + 1;
1360 	if (name_len > MAX_INST_NAME_LEN)
1361 		return -ENAMETOOLONG;
1362 
1363 	ptr = kstrndup(name, name_len, GFP_KERNEL);
1364 	if (!ptr)
1365 		return -ENOMEM;
1366 
1367 	fi_acc = to_fi_acc(fi);
1368 	fi_acc->name = ptr;
1369 	return 0;
1370 }
1371 
acc_free_inst(struct usb_function_instance * fi)1372 static void acc_free_inst(struct usb_function_instance *fi)
1373 {
1374 	struct acc_instance *fi_acc;
1375 
1376 	fi_acc = to_fi_acc(fi);
1377 	kfree(fi_acc->name);
1378 	acc_cleanup();
1379 }
1380 
acc_alloc_inst(void)1381 static struct usb_function_instance *acc_alloc_inst(void)
1382 {
1383 	struct acc_instance *fi_acc;
1384 	int err;
1385 
1386 	fi_acc = kzalloc(sizeof(*fi_acc), GFP_KERNEL);
1387 	if (!fi_acc)
1388 		return ERR_PTR(-ENOMEM);
1389 	fi_acc->func_inst.set_inst_name = acc_set_inst_name;
1390 	fi_acc->func_inst.free_func_inst = acc_free_inst;
1391 
1392 	err = acc_setup();
1393 	if (err) {
1394 		kfree(fi_acc);
1395 		return ERR_PTR(err);
1396 	}
1397 
1398 	config_group_init_type_name(&fi_acc->func_inst.group,
1399 					"", &acc_func_type);
1400 	return  &fi_acc->func_inst;
1401 }
1402 
acc_free(struct usb_function * f)1403 static void acc_free(struct usb_function *f)
1404 {
1405 	struct acc_dev *dev = func_to_dev(f);
1406 
1407 	put_acc_dev(dev);
1408 }
1409 
acc_ctrlrequest_configfs(struct usb_function * f,const struct usb_ctrlrequest * ctrl)1410 int acc_ctrlrequest_configfs(struct usb_function *f,
1411 			const struct usb_ctrlrequest *ctrl) {
1412 	if (f->config != NULL && f->config->cdev != NULL)
1413 		return acc_ctrlrequest(f->config->cdev, ctrl);
1414 	else
1415 		return -1;
1416 }
1417 
acc_alloc(struct usb_function_instance * fi)1418 static struct usb_function *acc_alloc(struct usb_function_instance *fi)
1419 {
1420 	struct acc_dev *dev = get_acc_dev();
1421 
1422 	dev->function.name = "accessory";
1423 	dev->function.strings = acc_strings,
1424 	dev->function.fs_descriptors = fs_acc_descs;
1425 	dev->function.hs_descriptors = hs_acc_descs;
1426 	dev->function.bind = acc_function_bind_configfs;
1427 	dev->function.unbind = acc_function_unbind;
1428 	dev->function.set_alt = acc_function_set_alt;
1429 	dev->function.disable = acc_function_disable;
1430 	dev->function.free_func = acc_free;
1431 	dev->function.setup = acc_ctrlrequest_configfs;
1432 
1433 	return &dev->function;
1434 }
1435 DECLARE_USB_FUNCTION_INIT(accessory, acc_alloc_inst, acc_alloc);
1436 MODULE_LICENSE("GPL");
1437