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