dln2.c 18 KB

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