xref: /openbmc/linux/drivers/mfd/dln2.c (revision 132db935)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Diolan DLN-2 USB adapter
4  *
5  * Copyright (c) 2014 Intel Corporation
6  *
7  * Derived from:
8  *  i2c-diolan-u2c.c
9  *  Copyright (c) 2010-2011 Ericsson AB
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <linux/platform_device.h>
20 #include <linux/mfd/core.h>
21 #include <linux/mfd/dln2.h>
22 #include <linux/rculist.h>
23 
24 struct dln2_header {
25 	__le16 size;
26 	__le16 id;
27 	__le16 echo;
28 	__le16 handle;
29 };
30 
31 struct dln2_response {
32 	struct dln2_header hdr;
33 	__le16 result;
34 };
35 
36 #define DLN2_GENERIC_MODULE_ID		0x00
37 #define DLN2_GENERIC_CMD(cmd)		DLN2_CMD(cmd, DLN2_GENERIC_MODULE_ID)
38 #define CMD_GET_DEVICE_VER		DLN2_GENERIC_CMD(0x30)
39 #define CMD_GET_DEVICE_SN		DLN2_GENERIC_CMD(0x31)
40 
41 #define DLN2_HW_ID			0x200
42 #define DLN2_USB_TIMEOUT		200	/* in ms */
43 #define DLN2_MAX_RX_SLOTS		16
44 #define DLN2_MAX_URBS			16
45 #define DLN2_RX_BUF_SIZE		512
46 
47 enum dln2_handle {
48 	DLN2_HANDLE_EVENT = 0,		/* don't change, hardware defined */
49 	DLN2_HANDLE_CTRL,
50 	DLN2_HANDLE_GPIO,
51 	DLN2_HANDLE_I2C,
52 	DLN2_HANDLE_SPI,
53 	DLN2_HANDLES
54 };
55 
56 /*
57  * Receive context used between the receive demultiplexer and the transfer
58  * routine. While sending a request the transfer routine will look for a free
59  * receive context and use it to wait for a response and to receive the URB and
60  * thus the response data.
61  */
62 struct dln2_rx_context {
63 	/* completion used to wait for a response */
64 	struct completion done;
65 
66 	/* if non-NULL the URB contains the response */
67 	struct urb *urb;
68 
69 	/* if true then this context is used to wait for a response */
70 	bool in_use;
71 };
72 
73 /*
74  * Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the
75  * handle header field to identify the module in dln2_dev.mod_rx_slots and then
76  * the echo header field to index the slots field and find the receive context
77  * for a particular request.
78  */
79 struct dln2_mod_rx_slots {
80 	/* RX slots bitmap */
81 	DECLARE_BITMAP(bmap, DLN2_MAX_RX_SLOTS);
82 
83 	/* used to wait for a free RX slot */
84 	wait_queue_head_t wq;
85 
86 	/* used to wait for an RX operation to complete */
87 	struct dln2_rx_context slots[DLN2_MAX_RX_SLOTS];
88 
89 	/* avoid races between alloc/free_rx_slot and dln2_rx_transfer */
90 	spinlock_t lock;
91 };
92 
93 enum dln2_endpoint {
94 	DLN2_EP_OUT	= 0,
95 	DLN2_EP_IN	= 1,
96 };
97 
98 struct dln2_dev {
99 	struct usb_device *usb_dev;
100 	struct usb_interface *interface;
101 	u8 ep_in;
102 	u8 ep_out;
103 
104 	struct urb *rx_urb[DLN2_MAX_URBS];
105 	void *rx_buf[DLN2_MAX_URBS];
106 
107 	struct dln2_mod_rx_slots mod_rx_slots[DLN2_HANDLES];
108 
109 	struct list_head event_cb_list;
110 	spinlock_t event_cb_lock;
111 
112 	bool disconnect;
113 	int active_transfers;
114 	wait_queue_head_t disconnect_wq;
115 	spinlock_t disconnect_lock;
116 };
117 
118 struct dln2_event_cb_entry {
119 	struct list_head list;
120 	u16 id;
121 	struct platform_device *pdev;
122 	dln2_event_cb_t callback;
123 };
124 
125 int dln2_register_event_cb(struct platform_device *pdev, u16 id,
126 			   dln2_event_cb_t event_cb)
127 {
128 	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
129 	struct dln2_event_cb_entry *i, *entry;
130 	unsigned long flags;
131 	int ret = 0;
132 
133 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
134 	if (!entry)
135 		return -ENOMEM;
136 
137 	entry->id = id;
138 	entry->callback = event_cb;
139 	entry->pdev = pdev;
140 
141 	spin_lock_irqsave(&dln2->event_cb_lock, flags);
142 
143 	list_for_each_entry(i, &dln2->event_cb_list, list) {
144 		if (i->id == id) {
145 			ret = -EBUSY;
146 			break;
147 		}
148 	}
149 
150 	if (!ret)
151 		list_add_rcu(&entry->list, &dln2->event_cb_list);
152 
153 	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
154 
155 	if (ret)
156 		kfree(entry);
157 
158 	return ret;
159 }
160 EXPORT_SYMBOL(dln2_register_event_cb);
161 
162 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id)
163 {
164 	struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent);
165 	struct dln2_event_cb_entry *i;
166 	unsigned long flags;
167 	bool found = false;
168 
169 	spin_lock_irqsave(&dln2->event_cb_lock, flags);
170 
171 	list_for_each_entry(i, &dln2->event_cb_list, list) {
172 		if (i->id == id) {
173 			list_del_rcu(&i->list);
174 			found = true;
175 			break;
176 		}
177 	}
178 
179 	spin_unlock_irqrestore(&dln2->event_cb_lock, flags);
180 
181 	if (found) {
182 		synchronize_rcu();
183 		kfree(i);
184 	}
185 }
186 EXPORT_SYMBOL(dln2_unregister_event_cb);
187 
188 /*
189  * Returns true if a valid transfer slot is found. In this case the URB must not
190  * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer
191  * is woke up. It will be resubmitted there.
192  */
193 static bool dln2_transfer_complete(struct dln2_dev *dln2, struct urb *urb,
194 				   u16 handle, u16 rx_slot)
195 {
196 	struct device *dev = &dln2->interface->dev;
197 	struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
198 	struct dln2_rx_context *rxc;
199 	unsigned long flags;
200 	bool valid_slot = false;
201 
202 	if (rx_slot >= DLN2_MAX_RX_SLOTS)
203 		goto out;
204 
205 	rxc = &rxs->slots[rx_slot];
206 
207 	spin_lock_irqsave(&rxs->lock, flags);
208 	if (rxc->in_use && !rxc->urb) {
209 		rxc->urb = urb;
210 		complete(&rxc->done);
211 		valid_slot = true;
212 	}
213 	spin_unlock_irqrestore(&rxs->lock, flags);
214 
215 out:
216 	if (!valid_slot)
217 		dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot);
218 
219 	return valid_slot;
220 }
221 
222 static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo,
223 				     void *data, int len)
224 {
225 	struct dln2_event_cb_entry *i;
226 
227 	rcu_read_lock();
228 
229 	list_for_each_entry_rcu(i, &dln2->event_cb_list, list) {
230 		if (i->id == id) {
231 			i->callback(i->pdev, echo, data, len);
232 			break;
233 		}
234 	}
235 
236 	rcu_read_unlock();
237 }
238 
239 static void dln2_rx(struct urb *urb)
240 {
241 	struct dln2_dev *dln2 = urb->context;
242 	struct dln2_header *hdr = urb->transfer_buffer;
243 	struct device *dev = &dln2->interface->dev;
244 	u16 id, echo, handle, size;
245 	u8 *data;
246 	int len;
247 	int err;
248 
249 	switch (urb->status) {
250 	case 0:
251 		/* success */
252 		break;
253 	case -ECONNRESET:
254 	case -ENOENT:
255 	case -ESHUTDOWN:
256 	case -EPIPE:
257 		/* this urb is terminated, clean up */
258 		dev_dbg(dev, "urb shutting down with status %d\n", urb->status);
259 		return;
260 	default:
261 		dev_dbg(dev, "nonzero urb status received %d\n", urb->status);
262 		goto out;
263 	}
264 
265 	if (urb->actual_length < sizeof(struct dln2_header)) {
266 		dev_err(dev, "short response: %d\n", urb->actual_length);
267 		goto out;
268 	}
269 
270 	handle = le16_to_cpu(hdr->handle);
271 	id = le16_to_cpu(hdr->id);
272 	echo = le16_to_cpu(hdr->echo);
273 	size = le16_to_cpu(hdr->size);
274 
275 	if (size != urb->actual_length) {
276 		dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n",
277 			handle, id, echo, size, urb->actual_length);
278 		goto out;
279 	}
280 
281 	if (handle >= DLN2_HANDLES) {
282 		dev_warn(dev, "invalid handle %d\n", handle);
283 		goto out;
284 	}
285 
286 	data = urb->transfer_buffer + sizeof(struct dln2_header);
287 	len = urb->actual_length - sizeof(struct dln2_header);
288 
289 	if (handle == DLN2_HANDLE_EVENT) {
290 		dln2_run_event_callbacks(dln2, id, echo, data, len);
291 	} else {
292 		/* URB will be re-submitted in _dln2_transfer (free_rx_slot) */
293 		if (dln2_transfer_complete(dln2, urb, handle, echo))
294 			return;
295 	}
296 
297 out:
298 	err = usb_submit_urb(urb, GFP_ATOMIC);
299 	if (err < 0)
300 		dev_err(dev, "failed to resubmit RX URB: %d\n", err);
301 }
302 
303 static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf,
304 			   int *obuf_len, gfp_t gfp)
305 {
306 	int len;
307 	void *buf;
308 	struct dln2_header *hdr;
309 
310 	len = *obuf_len + sizeof(*hdr);
311 	buf = kmalloc(len, gfp);
312 	if (!buf)
313 		return NULL;
314 
315 	hdr = (struct dln2_header *)buf;
316 	hdr->id = cpu_to_le16(cmd);
317 	hdr->size = cpu_to_le16(len);
318 	hdr->echo = cpu_to_le16(echo);
319 	hdr->handle = cpu_to_le16(handle);
320 
321 	memcpy(buf + sizeof(*hdr), obuf, *obuf_len);
322 
323 	*obuf_len = len;
324 
325 	return buf;
326 }
327 
328 static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo,
329 			  const void *obuf, int obuf_len)
330 {
331 	int ret = 0;
332 	int len = obuf_len;
333 	void *buf;
334 	int actual;
335 
336 	buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL);
337 	if (!buf)
338 		return -ENOMEM;
339 
340 	ret = usb_bulk_msg(dln2->usb_dev,
341 			   usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out),
342 			   buf, len, &actual, DLN2_USB_TIMEOUT);
343 
344 	kfree(buf);
345 
346 	return ret;
347 }
348 
349 static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot)
350 {
351 	struct dln2_mod_rx_slots *rxs;
352 	unsigned long flags;
353 
354 	if (dln2->disconnect) {
355 		*slot = -ENODEV;
356 		return true;
357 	}
358 
359 	rxs = &dln2->mod_rx_slots[handle];
360 
361 	spin_lock_irqsave(&rxs->lock, flags);
362 
363 	*slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS);
364 
365 	if (*slot < DLN2_MAX_RX_SLOTS) {
366 		struct dln2_rx_context *rxc = &rxs->slots[*slot];
367 
368 		set_bit(*slot, rxs->bmap);
369 		rxc->in_use = true;
370 	}
371 
372 	spin_unlock_irqrestore(&rxs->lock, flags);
373 
374 	return *slot < DLN2_MAX_RX_SLOTS;
375 }
376 
377 static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle)
378 {
379 	int ret;
380 	int slot;
381 
382 	/*
383 	 * No need to timeout here, the wait is bounded by the timeout in
384 	 * _dln2_transfer.
385 	 */
386 	ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq,
387 				       find_free_slot(dln2, handle, &slot));
388 	if (ret < 0)
389 		return ret;
390 
391 	return slot;
392 }
393 
394 static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot)
395 {
396 	struct dln2_mod_rx_slots *rxs;
397 	struct urb *urb = NULL;
398 	unsigned long flags;
399 	struct dln2_rx_context *rxc;
400 
401 	rxs = &dln2->mod_rx_slots[handle];
402 
403 	spin_lock_irqsave(&rxs->lock, flags);
404 
405 	clear_bit(slot, rxs->bmap);
406 
407 	rxc = &rxs->slots[slot];
408 	rxc->in_use = false;
409 	urb = rxc->urb;
410 	rxc->urb = NULL;
411 	reinit_completion(&rxc->done);
412 
413 	spin_unlock_irqrestore(&rxs->lock, flags);
414 
415 	if (urb) {
416 		int err;
417 		struct device *dev = &dln2->interface->dev;
418 
419 		err = usb_submit_urb(urb, GFP_KERNEL);
420 		if (err < 0)
421 			dev_err(dev, "failed to resubmit RX URB: %d\n", err);
422 	}
423 
424 	wake_up_interruptible(&rxs->wq);
425 }
426 
427 static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd,
428 			  const void *obuf, unsigned obuf_len,
429 			  void *ibuf, unsigned *ibuf_len)
430 {
431 	int ret = 0;
432 	int rx_slot;
433 	struct dln2_response *rsp;
434 	struct dln2_rx_context *rxc;
435 	struct device *dev = &dln2->interface->dev;
436 	const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT);
437 	struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle];
438 	int size;
439 
440 	spin_lock(&dln2->disconnect_lock);
441 	if (!dln2->disconnect)
442 		dln2->active_transfers++;
443 	else
444 		ret = -ENODEV;
445 	spin_unlock(&dln2->disconnect_lock);
446 
447 	if (ret)
448 		return ret;
449 
450 	rx_slot = alloc_rx_slot(dln2, handle);
451 	if (rx_slot < 0) {
452 		ret = rx_slot;
453 		goto out_decr;
454 	}
455 
456 	ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len);
457 	if (ret < 0) {
458 		dev_err(dev, "USB write failed: %d\n", ret);
459 		goto out_free_rx_slot;
460 	}
461 
462 	rxc = &rxs->slots[rx_slot];
463 
464 	ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout);
465 	if (ret <= 0) {
466 		if (!ret)
467 			ret = -ETIMEDOUT;
468 		goto out_free_rx_slot;
469 	} else {
470 		ret = 0;
471 	}
472 
473 	if (dln2->disconnect) {
474 		ret = -ENODEV;
475 		goto out_free_rx_slot;
476 	}
477 
478 	/* if we got here we know that the response header has been checked */
479 	rsp = rxc->urb->transfer_buffer;
480 	size = le16_to_cpu(rsp->hdr.size);
481 
482 	if (size < sizeof(*rsp)) {
483 		ret = -EPROTO;
484 		goto out_free_rx_slot;
485 	}
486 
487 	if (le16_to_cpu(rsp->result) > 0x80) {
488 		dev_dbg(dev, "%d received response with error %d\n",
489 			handle, le16_to_cpu(rsp->result));
490 		ret = -EREMOTEIO;
491 		goto out_free_rx_slot;
492 	}
493 
494 	if (!ibuf)
495 		goto out_free_rx_slot;
496 
497 	if (*ibuf_len > size - sizeof(*rsp))
498 		*ibuf_len = size - sizeof(*rsp);
499 
500 	memcpy(ibuf, rsp + 1, *ibuf_len);
501 
502 out_free_rx_slot:
503 	free_rx_slot(dln2, handle, rx_slot);
504 out_decr:
505 	spin_lock(&dln2->disconnect_lock);
506 	dln2->active_transfers--;
507 	spin_unlock(&dln2->disconnect_lock);
508 	if (dln2->disconnect)
509 		wake_up(&dln2->disconnect_wq);
510 
511 	return ret;
512 }
513 
514 int dln2_transfer(struct platform_device *pdev, u16 cmd,
515 		  const void *obuf, unsigned obuf_len,
516 		  void *ibuf, unsigned *ibuf_len)
517 {
518 	struct dln2_platform_data *dln2_pdata;
519 	struct dln2_dev *dln2;
520 	u16 handle;
521 
522 	dln2 = dev_get_drvdata(pdev->dev.parent);
523 	dln2_pdata = dev_get_platdata(&pdev->dev);
524 	handle = dln2_pdata->handle;
525 
526 	return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf,
527 			      ibuf_len);
528 }
529 EXPORT_SYMBOL(dln2_transfer);
530 
531 static int dln2_check_hw(struct dln2_dev *dln2)
532 {
533 	int ret;
534 	__le32 hw_type;
535 	int len = sizeof(hw_type);
536 
537 	ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER,
538 			     NULL, 0, &hw_type, &len);
539 	if (ret < 0)
540 		return ret;
541 	if (len < sizeof(hw_type))
542 		return -EREMOTEIO;
543 
544 	if (le32_to_cpu(hw_type) != DLN2_HW_ID) {
545 		dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n",
546 			le32_to_cpu(hw_type));
547 		return -ENODEV;
548 	}
549 
550 	return 0;
551 }
552 
553 static int dln2_print_serialno(struct dln2_dev *dln2)
554 {
555 	int ret;
556 	__le32 serial_no;
557 	int len = sizeof(serial_no);
558 	struct device *dev = &dln2->interface->dev;
559 
560 	ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0,
561 			     &serial_no, &len);
562 	if (ret < 0)
563 		return ret;
564 	if (len < sizeof(serial_no))
565 		return -EREMOTEIO;
566 
567 	dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no));
568 
569 	return 0;
570 }
571 
572 static int dln2_hw_init(struct dln2_dev *dln2)
573 {
574 	int ret;
575 
576 	ret = dln2_check_hw(dln2);
577 	if (ret < 0)
578 		return ret;
579 
580 	return dln2_print_serialno(dln2);
581 }
582 
583 static void dln2_free_rx_urbs(struct dln2_dev *dln2)
584 {
585 	int i;
586 
587 	for (i = 0; i < DLN2_MAX_URBS; i++) {
588 		usb_free_urb(dln2->rx_urb[i]);
589 		kfree(dln2->rx_buf[i]);
590 	}
591 }
592 
593 static void dln2_stop_rx_urbs(struct dln2_dev *dln2)
594 {
595 	int i;
596 
597 	for (i = 0; i < DLN2_MAX_URBS; i++)
598 		usb_kill_urb(dln2->rx_urb[i]);
599 }
600 
601 static void dln2_free(struct dln2_dev *dln2)
602 {
603 	dln2_free_rx_urbs(dln2);
604 	usb_put_dev(dln2->usb_dev);
605 	kfree(dln2);
606 }
607 
608 static int dln2_setup_rx_urbs(struct dln2_dev *dln2,
609 			      struct usb_host_interface *hostif)
610 {
611 	int i;
612 	const int rx_max_size = DLN2_RX_BUF_SIZE;
613 
614 	for (i = 0; i < DLN2_MAX_URBS; i++) {
615 		dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL);
616 		if (!dln2->rx_buf[i])
617 			return -ENOMEM;
618 
619 		dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
620 		if (!dln2->rx_urb[i])
621 			return -ENOMEM;
622 
623 		usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev,
624 				  usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in),
625 				  dln2->rx_buf[i], rx_max_size, dln2_rx, dln2);
626 	}
627 
628 	return 0;
629 }
630 
631 static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp)
632 {
633 	struct device *dev = &dln2->interface->dev;
634 	int ret;
635 	int i;
636 
637 	for (i = 0; i < DLN2_MAX_URBS; i++) {
638 		ret = usb_submit_urb(dln2->rx_urb[i], gfp);
639 		if (ret < 0) {
640 			dev_err(dev, "failed to submit RX URB: %d\n", ret);
641 			return ret;
642 		}
643 	}
644 
645 	return 0;
646 }
647 
648 enum {
649 	DLN2_ACPI_MATCH_GPIO	= 0,
650 	DLN2_ACPI_MATCH_I2C	= 1,
651 	DLN2_ACPI_MATCH_SPI	= 2,
652 };
653 
654 static struct dln2_platform_data dln2_pdata_gpio = {
655 	.handle = DLN2_HANDLE_GPIO,
656 };
657 
658 static struct mfd_cell_acpi_match dln2_acpi_match_gpio = {
659 	.adr = DLN2_ACPI_MATCH_GPIO,
660 };
661 
662 /* Only one I2C port seems to be supported on current hardware */
663 static struct dln2_platform_data dln2_pdata_i2c = {
664 	.handle = DLN2_HANDLE_I2C,
665 	.port = 0,
666 };
667 
668 static struct mfd_cell_acpi_match dln2_acpi_match_i2c = {
669 	.adr = DLN2_ACPI_MATCH_I2C,
670 };
671 
672 /* Only one SPI port supported */
673 static struct dln2_platform_data dln2_pdata_spi = {
674 	.handle = DLN2_HANDLE_SPI,
675 	.port = 0,
676 };
677 
678 static struct mfd_cell_acpi_match dln2_acpi_match_spi = {
679 	.adr = DLN2_ACPI_MATCH_SPI,
680 };
681 
682 static const struct mfd_cell dln2_devs[] = {
683 	{
684 		.name = "dln2-gpio",
685 		.acpi_match = &dln2_acpi_match_gpio,
686 		.platform_data = &dln2_pdata_gpio,
687 		.pdata_size = sizeof(struct dln2_platform_data),
688 	},
689 	{
690 		.name = "dln2-i2c",
691 		.acpi_match = &dln2_acpi_match_i2c,
692 		.platform_data = &dln2_pdata_i2c,
693 		.pdata_size = sizeof(struct dln2_platform_data),
694 	},
695 	{
696 		.name = "dln2-spi",
697 		.acpi_match = &dln2_acpi_match_spi,
698 		.platform_data = &dln2_pdata_spi,
699 		.pdata_size = sizeof(struct dln2_platform_data),
700 	},
701 };
702 
703 static void dln2_stop(struct dln2_dev *dln2)
704 {
705 	int i, j;
706 
707 	/* don't allow starting new transfers */
708 	spin_lock(&dln2->disconnect_lock);
709 	dln2->disconnect = true;
710 	spin_unlock(&dln2->disconnect_lock);
711 
712 	/* cancel in progress transfers */
713 	for (i = 0; i < DLN2_HANDLES; i++) {
714 		struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i];
715 		unsigned long flags;
716 
717 		spin_lock_irqsave(&rxs->lock, flags);
718 
719 		/* cancel all response waiters */
720 		for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) {
721 			struct dln2_rx_context *rxc = &rxs->slots[j];
722 
723 			if (rxc->in_use)
724 				complete(&rxc->done);
725 		}
726 
727 		spin_unlock_irqrestore(&rxs->lock, flags);
728 	}
729 
730 	/* wait for transfers to end */
731 	wait_event(dln2->disconnect_wq, !dln2->active_transfers);
732 
733 	dln2_stop_rx_urbs(dln2);
734 }
735 
736 static void dln2_disconnect(struct usb_interface *interface)
737 {
738 	struct dln2_dev *dln2 = usb_get_intfdata(interface);
739 
740 	dln2_stop(dln2);
741 
742 	mfd_remove_devices(&interface->dev);
743 
744 	dln2_free(dln2);
745 }
746 
747 static int dln2_probe(struct usb_interface *interface,
748 		      const struct usb_device_id *usb_id)
749 {
750 	struct usb_host_interface *hostif = interface->cur_altsetting;
751 	struct usb_endpoint_descriptor *epin;
752 	struct usb_endpoint_descriptor *epout;
753 	struct device *dev = &interface->dev;
754 	struct dln2_dev *dln2;
755 	int ret;
756 	int i, j;
757 
758 	if (hostif->desc.bInterfaceNumber != 0 ||
759 	    hostif->desc.bNumEndpoints < 2)
760 		return -ENODEV;
761 
762 	epout = &hostif->endpoint[DLN2_EP_OUT].desc;
763 	if (!usb_endpoint_is_bulk_out(epout))
764 		return -ENODEV;
765 	epin = &hostif->endpoint[DLN2_EP_IN].desc;
766 	if (!usb_endpoint_is_bulk_in(epin))
767 		return -ENODEV;
768 
769 	dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
770 	if (!dln2)
771 		return -ENOMEM;
772 
773 	dln2->ep_out = epout->bEndpointAddress;
774 	dln2->ep_in = epin->bEndpointAddress;
775 	dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
776 	dln2->interface = interface;
777 	usb_set_intfdata(interface, dln2);
778 	init_waitqueue_head(&dln2->disconnect_wq);
779 
780 	for (i = 0; i < DLN2_HANDLES; i++) {
781 		init_waitqueue_head(&dln2->mod_rx_slots[i].wq);
782 		spin_lock_init(&dln2->mod_rx_slots[i].lock);
783 		for (j = 0; j < DLN2_MAX_RX_SLOTS; j++)
784 			init_completion(&dln2->mod_rx_slots[i].slots[j].done);
785 	}
786 
787 	spin_lock_init(&dln2->event_cb_lock);
788 	spin_lock_init(&dln2->disconnect_lock);
789 	INIT_LIST_HEAD(&dln2->event_cb_list);
790 
791 	ret = dln2_setup_rx_urbs(dln2, hostif);
792 	if (ret)
793 		goto out_free;
794 
795 	ret = dln2_start_rx_urbs(dln2, GFP_KERNEL);
796 	if (ret)
797 		goto out_stop_rx;
798 
799 	ret = dln2_hw_init(dln2);
800 	if (ret < 0) {
801 		dev_err(dev, "failed to initialize hardware\n");
802 		goto out_stop_rx;
803 	}
804 
805 	ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs));
806 	if (ret != 0) {
807 		dev_err(dev, "failed to add mfd devices to core\n");
808 		goto out_stop_rx;
809 	}
810 
811 	return 0;
812 
813 out_stop_rx:
814 	dln2_stop_rx_urbs(dln2);
815 
816 out_free:
817 	dln2_free(dln2);
818 
819 	return ret;
820 }
821 
822 static int dln2_suspend(struct usb_interface *iface, pm_message_t message)
823 {
824 	struct dln2_dev *dln2 = usb_get_intfdata(iface);
825 
826 	dln2_stop(dln2);
827 
828 	return 0;
829 }
830 
831 static int dln2_resume(struct usb_interface *iface)
832 {
833 	struct dln2_dev *dln2 = usb_get_intfdata(iface);
834 
835 	dln2->disconnect = false;
836 
837 	return dln2_start_rx_urbs(dln2, GFP_NOIO);
838 }
839 
840 static const struct usb_device_id dln2_table[] = {
841 	{ USB_DEVICE(0xa257, 0x2013) },
842 	{ }
843 };
844 
845 MODULE_DEVICE_TABLE(usb, dln2_table);
846 
847 static struct usb_driver dln2_driver = {
848 	.name = "dln2",
849 	.probe = dln2_probe,
850 	.disconnect = dln2_disconnect,
851 	.id_table = dln2_table,
852 	.suspend = dln2_suspend,
853 	.resume = dln2_resume,
854 };
855 
856 module_usb_driver(dln2_driver);
857 
858 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
859 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter");
860 MODULE_LICENSE("GPL v2");
861