uss720.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*****************************************************************************/
  2. /*
  3. * uss720.c -- USS720 USB Parport Cable.
  4. *
  5. * Copyright (C) 1999, 2005, 2010
  6. * Thomas Sailer (t.sailer@alumni.ethz.ch)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Based on parport_pc.c
  23. *
  24. * History:
  25. * 0.1 04.08.1999 Created
  26. * 0.2 07.08.1999 Some fixes mainly suggested by Tim Waugh
  27. * Interrupt handling currently disabled because
  28. * usb_request_irq crashes somewhere within ohci.c
  29. * for no apparent reason (that is for me, anyway)
  30. * ECP currently untested
  31. * 0.3 10.08.1999 fixing merge errors
  32. * 0.4 13.08.1999 Added Vendor/Product ID of Brad Hard's cable
  33. * 0.5 20.09.1999 usb_control_msg wrapper used
  34. * Nov01.2000 usb_device_table support by Adam J. Richter
  35. * 08.04.2001 Identify version on module load. gb
  36. * 0.6 02.09.2005 Fix "scheduling in interrupt" problem by making save/restore
  37. * context asynchronous
  38. *
  39. */
  40. /*****************************************************************************/
  41. #include <linux/module.h>
  42. #include <linux/socket.h>
  43. #include <linux/parport.h>
  44. #include <linux/init.h>
  45. #include <linux/usb.h>
  46. #include <linux/delay.h>
  47. #include <linux/completion.h>
  48. #include <linux/kref.h>
  49. #include <linux/slab.h>
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v0.6"
  54. #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
  55. #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
  56. /* --------------------------------------------------------------------- */
  57. struct parport_uss720_private {
  58. struct usb_device *usbdev;
  59. struct parport *pp;
  60. struct kref ref_count;
  61. __u8 reg[7]; /* USB registers */
  62. struct list_head asynclist;
  63. spinlock_t asynclock;
  64. };
  65. struct uss720_async_request {
  66. struct parport_uss720_private *priv;
  67. struct kref ref_count;
  68. struct list_head asynclist;
  69. struct completion compl;
  70. struct urb *urb;
  71. struct usb_ctrlrequest *dr;
  72. __u8 reg[7];
  73. };
  74. /* --------------------------------------------------------------------- */
  75. static void destroy_priv(struct kref *kref)
  76. {
  77. struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
  78. dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
  79. usb_put_dev(priv->usbdev);
  80. kfree(priv);
  81. }
  82. static void destroy_async(struct kref *kref)
  83. {
  84. struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
  85. struct parport_uss720_private *priv = rq->priv;
  86. unsigned long flags;
  87. if (likely(rq->urb))
  88. usb_free_urb(rq->urb);
  89. kfree(rq->dr);
  90. spin_lock_irqsave(&priv->asynclock, flags);
  91. list_del_init(&rq->asynclist);
  92. spin_unlock_irqrestore(&priv->asynclock, flags);
  93. kfree(rq);
  94. kref_put(&priv->ref_count, destroy_priv);
  95. }
  96. /* --------------------------------------------------------------------- */
  97. static void async_complete(struct urb *urb)
  98. {
  99. struct uss720_async_request *rq;
  100. struct parport *pp;
  101. struct parport_uss720_private *priv;
  102. int status = urb->status;
  103. rq = urb->context;
  104. priv = rq->priv;
  105. pp = priv->pp;
  106. if (status) {
  107. dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
  108. status);
  109. } else if (rq->dr->bRequest == 3) {
  110. memcpy(priv->reg, rq->reg, sizeof(priv->reg));
  111. #if 0
  112. dev_dbg(&priv->usbdev->dev,
  113. "async_complete regs %02x %02x %02x %02x %02x %02x %02x\n",
  114. (unsigned int)priv->reg[0], (unsigned int)priv->reg[1],
  115. (unsigned int)priv->reg[2], (unsigned int)priv->reg[3],
  116. (unsigned int)priv->reg[4], (unsigned int)priv->reg[5],
  117. (unsigned int)priv->reg[6]);
  118. #endif
  119. /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
  120. if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
  121. parport_generic_irq(pp);
  122. }
  123. complete(&rq->compl);
  124. kref_put(&rq->ref_count, destroy_async);
  125. }
  126. static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
  127. __u8 request, __u8 requesttype, __u16 value, __u16 index,
  128. gfp_t mem_flags)
  129. {
  130. struct usb_device *usbdev;
  131. struct uss720_async_request *rq;
  132. unsigned long flags;
  133. int ret;
  134. if (!priv)
  135. return NULL;
  136. usbdev = priv->usbdev;
  137. if (!usbdev)
  138. return NULL;
  139. rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
  140. if (!rq) {
  141. dev_err(&usbdev->dev, "submit_async_request out of memory\n");
  142. return NULL;
  143. }
  144. kref_init(&rq->ref_count);
  145. INIT_LIST_HEAD(&rq->asynclist);
  146. init_completion(&rq->compl);
  147. kref_get(&priv->ref_count);
  148. rq->priv = priv;
  149. rq->urb = usb_alloc_urb(0, mem_flags);
  150. if (!rq->urb) {
  151. kref_put(&rq->ref_count, destroy_async);
  152. dev_err(&usbdev->dev, "submit_async_request out of memory\n");
  153. return NULL;
  154. }
  155. rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
  156. if (!rq->dr) {
  157. kref_put(&rq->ref_count, destroy_async);
  158. return NULL;
  159. }
  160. rq->dr->bRequestType = requesttype;
  161. rq->dr->bRequest = request;
  162. rq->dr->wValue = cpu_to_le16(value);
  163. rq->dr->wIndex = cpu_to_le16(index);
  164. rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
  165. usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
  166. (unsigned char *)rq->dr,
  167. (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
  168. /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
  169. spin_lock_irqsave(&priv->asynclock, flags);
  170. list_add_tail(&rq->asynclist, &priv->asynclist);
  171. spin_unlock_irqrestore(&priv->asynclock, flags);
  172. kref_get(&rq->ref_count);
  173. ret = usb_submit_urb(rq->urb, mem_flags);
  174. if (!ret)
  175. return rq;
  176. destroy_async(&rq->ref_count);
  177. dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
  178. return NULL;
  179. }
  180. static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
  181. {
  182. struct uss720_async_request *rq;
  183. unsigned long flags;
  184. unsigned int ret = 0;
  185. spin_lock_irqsave(&priv->asynclock, flags);
  186. list_for_each_entry(rq, &priv->asynclist, asynclist) {
  187. usb_unlink_urb(rq->urb);
  188. ret++;
  189. }
  190. spin_unlock_irqrestore(&priv->asynclock, flags);
  191. return ret;
  192. }
  193. /* --------------------------------------------------------------------- */
  194. static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
  195. {
  196. struct parport_uss720_private *priv;
  197. struct uss720_async_request *rq;
  198. static const unsigned char regindex[9] = {
  199. 4, 0, 1, 5, 5, 0, 2, 3, 6
  200. };
  201. int ret;
  202. if (!pp)
  203. return -EIO;
  204. priv = pp->private_data;
  205. rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
  206. if (!rq) {
  207. dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
  208. (unsigned int)reg);
  209. return -EIO;
  210. }
  211. if (!val) {
  212. kref_put(&rq->ref_count, destroy_async);
  213. return 0;
  214. }
  215. if (wait_for_completion_timeout(&rq->compl, HZ)) {
  216. ret = rq->urb->status;
  217. *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
  218. if (ret)
  219. printk(KERN_WARNING "get_1284_register: "
  220. "usb error %d\n", ret);
  221. kref_put(&rq->ref_count, destroy_async);
  222. return ret;
  223. }
  224. printk(KERN_WARNING "get_1284_register timeout\n");
  225. kill_all_async_requests_priv(priv);
  226. return -EIO;
  227. }
  228. static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
  229. {
  230. struct parport_uss720_private *priv;
  231. struct uss720_async_request *rq;
  232. if (!pp)
  233. return -EIO;
  234. priv = pp->private_data;
  235. rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
  236. if (!rq) {
  237. dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
  238. (unsigned int)reg, (unsigned int)val);
  239. return -EIO;
  240. }
  241. kref_put(&rq->ref_count, destroy_async);
  242. return 0;
  243. }
  244. /* --------------------------------------------------------------------- */
  245. /* ECR modes */
  246. #define ECR_SPP 00
  247. #define ECR_PS2 01
  248. #define ECR_PPF 02
  249. #define ECR_ECP 03
  250. #define ECR_EPP 04
  251. /* Safely change the mode bits in the ECR */
  252. static int change_mode(struct parport *pp, int m)
  253. {
  254. struct parport_uss720_private *priv = pp->private_data;
  255. int mode;
  256. __u8 reg;
  257. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  258. return -EIO;
  259. /* Bits <7:5> contain the mode. */
  260. mode = (priv->reg[2] >> 5) & 0x7;
  261. if (mode == m)
  262. return 0;
  263. /* We have to go through mode 000 or 001 */
  264. if (mode > ECR_PS2 && m > ECR_PS2)
  265. if (change_mode(pp, ECR_PS2))
  266. return -EIO;
  267. if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
  268. /* This mode resets the FIFO, so we may
  269. * have to wait for it to drain first. */
  270. unsigned long expire = jiffies + pp->physport->cad->timeout;
  271. switch (mode) {
  272. case ECR_PPF: /* Parallel Port FIFO mode */
  273. case ECR_ECP: /* ECP Parallel Port mode */
  274. /* Poll slowly. */
  275. for (;;) {
  276. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  277. return -EIO;
  278. if (priv->reg[2] & 0x01)
  279. break;
  280. if (time_after_eq (jiffies, expire))
  281. /* The FIFO is stuck. */
  282. return -EBUSY;
  283. msleep_interruptible(10);
  284. if (signal_pending (current))
  285. break;
  286. }
  287. }
  288. }
  289. /* Set the mode. */
  290. if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
  291. return -EIO;
  292. if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
  293. return -EIO;
  294. return 0;
  295. }
  296. /*
  297. * Clear TIMEOUT BIT in EPP MODE
  298. */
  299. static int clear_epp_timeout(struct parport *pp)
  300. {
  301. unsigned char stat;
  302. if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
  303. return 1;
  304. return stat & 1;
  305. }
  306. /*
  307. * Access functions.
  308. */
  309. #if 0
  310. static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
  311. {
  312. struct parport *pp = (struct parport *)dev_id;
  313. struct parport_uss720_private *priv = pp->private_data;
  314. if (usbstatus != 0 || len < 4 || !buffer)
  315. return 1;
  316. memcpy(priv->reg, buffer, 4);
  317. /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
  318. if (priv->reg[2] & priv->reg[1] & 0x10)
  319. parport_generic_irq(pp);
  320. return 1;
  321. }
  322. #endif
  323. static void parport_uss720_write_data(struct parport *pp, unsigned char d)
  324. {
  325. set_1284_register(pp, 0, d, GFP_KERNEL);
  326. }
  327. static unsigned char parport_uss720_read_data(struct parport *pp)
  328. {
  329. unsigned char ret;
  330. if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
  331. return 0;
  332. return ret;
  333. }
  334. static void parport_uss720_write_control(struct parport *pp, unsigned char d)
  335. {
  336. struct parport_uss720_private *priv = pp->private_data;
  337. d = (d & 0xf) | (priv->reg[1] & 0xf0);
  338. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  339. return;
  340. priv->reg[1] = d;
  341. }
  342. static unsigned char parport_uss720_read_control(struct parport *pp)
  343. {
  344. struct parport_uss720_private *priv = pp->private_data;
  345. return priv->reg[1] & 0xf; /* Use soft copy */
  346. }
  347. static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
  348. {
  349. struct parport_uss720_private *priv = pp->private_data;
  350. unsigned char d;
  351. mask &= 0x0f;
  352. val &= 0x0f;
  353. d = (priv->reg[1] & (~mask)) ^ val;
  354. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  355. return 0;
  356. priv->reg[1] = d;
  357. return d & 0xf;
  358. }
  359. static unsigned char parport_uss720_read_status(struct parport *pp)
  360. {
  361. unsigned char ret;
  362. if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
  363. return 0;
  364. return ret & 0xf8;
  365. }
  366. static void parport_uss720_disable_irq(struct parport *pp)
  367. {
  368. struct parport_uss720_private *priv = pp->private_data;
  369. unsigned char d;
  370. d = priv->reg[1] & ~0x10;
  371. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  372. return;
  373. priv->reg[1] = d;
  374. }
  375. static void parport_uss720_enable_irq(struct parport *pp)
  376. {
  377. struct parport_uss720_private *priv = pp->private_data;
  378. unsigned char d;
  379. d = priv->reg[1] | 0x10;
  380. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  381. return;
  382. priv->reg[1] = d;
  383. }
  384. static void parport_uss720_data_forward (struct parport *pp)
  385. {
  386. struct parport_uss720_private *priv = pp->private_data;
  387. unsigned char d;
  388. d = priv->reg[1] & ~0x20;
  389. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  390. return;
  391. priv->reg[1] = d;
  392. }
  393. static void parport_uss720_data_reverse (struct parport *pp)
  394. {
  395. struct parport_uss720_private *priv = pp->private_data;
  396. unsigned char d;
  397. d = priv->reg[1] | 0x20;
  398. if (set_1284_register(pp, 2, d, GFP_KERNEL))
  399. return;
  400. priv->reg[1] = d;
  401. }
  402. static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
  403. {
  404. s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
  405. s->u.pc.ecr = 0x24;
  406. }
  407. static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
  408. {
  409. struct parport_uss720_private *priv = pp->private_data;
  410. #if 0
  411. if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
  412. return;
  413. #endif
  414. s->u.pc.ctr = priv->reg[1];
  415. s->u.pc.ecr = priv->reg[2];
  416. }
  417. static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
  418. {
  419. struct parport_uss720_private *priv = pp->private_data;
  420. set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
  421. set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
  422. get_1284_register(pp, 2, NULL, GFP_ATOMIC);
  423. priv->reg[1] = s->u.pc.ctr;
  424. priv->reg[2] = s->u.pc.ecr;
  425. }
  426. static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
  427. {
  428. struct parport_uss720_private *priv = pp->private_data;
  429. size_t got = 0;
  430. if (change_mode(pp, ECR_EPP))
  431. return 0;
  432. for (; got < length; got++) {
  433. if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  434. break;
  435. buf++;
  436. if (priv->reg[0] & 0x01) {
  437. clear_epp_timeout(pp);
  438. break;
  439. }
  440. }
  441. change_mode(pp, ECR_PS2);
  442. return got;
  443. }
  444. static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
  445. {
  446. #if 0
  447. struct parport_uss720_private *priv = pp->private_data;
  448. size_t written = 0;
  449. if (change_mode(pp, ECR_EPP))
  450. return 0;
  451. for (; written < length; written++) {
  452. if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
  453. break;
  454. ((char*)buf)++;
  455. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  456. break;
  457. if (priv->reg[0] & 0x01) {
  458. clear_epp_timeout(pp);
  459. break;
  460. }
  461. }
  462. change_mode(pp, ECR_PS2);
  463. return written;
  464. #else
  465. struct parport_uss720_private *priv = pp->private_data;
  466. struct usb_device *usbdev = priv->usbdev;
  467. int rlen;
  468. int i;
  469. if (!usbdev)
  470. return 0;
  471. if (change_mode(pp, ECR_EPP))
  472. return 0;
  473. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
  474. if (i)
  475. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
  476. change_mode(pp, ECR_PS2);
  477. return rlen;
  478. #endif
  479. }
  480. static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
  481. {
  482. struct parport_uss720_private *priv = pp->private_data;
  483. size_t got = 0;
  484. if (change_mode(pp, ECR_EPP))
  485. return 0;
  486. for (; got < length; got++) {
  487. if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
  488. break;
  489. buf++;
  490. if (priv->reg[0] & 0x01) {
  491. clear_epp_timeout(pp);
  492. break;
  493. }
  494. }
  495. change_mode(pp, ECR_PS2);
  496. return got;
  497. }
  498. static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
  499. {
  500. struct parport_uss720_private *priv = pp->private_data;
  501. size_t written = 0;
  502. if (change_mode(pp, ECR_EPP))
  503. return 0;
  504. for (; written < length; written++) {
  505. if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
  506. break;
  507. buf++;
  508. if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
  509. break;
  510. if (priv->reg[0] & 0x01) {
  511. clear_epp_timeout(pp);
  512. break;
  513. }
  514. }
  515. change_mode(pp, ECR_PS2);
  516. return written;
  517. }
  518. static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
  519. {
  520. struct parport_uss720_private *priv = pp->private_data;
  521. struct usb_device *usbdev = priv->usbdev;
  522. int rlen;
  523. int i;
  524. if (!usbdev)
  525. return 0;
  526. if (change_mode(pp, ECR_ECP))
  527. return 0;
  528. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  529. if (i)
  530. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  531. change_mode(pp, ECR_PS2);
  532. return rlen;
  533. }
  534. static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
  535. {
  536. struct parport_uss720_private *priv = pp->private_data;
  537. struct usb_device *usbdev = priv->usbdev;
  538. int rlen;
  539. int i;
  540. if (!usbdev)
  541. return 0;
  542. if (change_mode(pp, ECR_ECP))
  543. return 0;
  544. i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
  545. if (i)
  546. printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  547. change_mode(pp, ECR_PS2);
  548. return rlen;
  549. }
  550. static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
  551. {
  552. size_t written = 0;
  553. if (change_mode(pp, ECR_ECP))
  554. return 0;
  555. for (; written < len; written++) {
  556. if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
  557. break;
  558. buffer++;
  559. }
  560. change_mode(pp, ECR_PS2);
  561. return written;
  562. }
  563. static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
  564. {
  565. struct parport_uss720_private *priv = pp->private_data;
  566. struct usb_device *usbdev = priv->usbdev;
  567. int rlen;
  568. int i;
  569. if (!usbdev)
  570. return 0;
  571. if (change_mode(pp, ECR_PPF))
  572. return 0;
  573. i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
  574. if (i)
  575. printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
  576. change_mode(pp, ECR_PS2);
  577. return rlen;
  578. }
  579. /* --------------------------------------------------------------------- */
  580. static struct parport_operations parport_uss720_ops =
  581. {
  582. .owner = THIS_MODULE,
  583. .write_data = parport_uss720_write_data,
  584. .read_data = parport_uss720_read_data,
  585. .write_control = parport_uss720_write_control,
  586. .read_control = parport_uss720_read_control,
  587. .frob_control = parport_uss720_frob_control,
  588. .read_status = parport_uss720_read_status,
  589. .enable_irq = parport_uss720_enable_irq,
  590. .disable_irq = parport_uss720_disable_irq,
  591. .data_forward = parport_uss720_data_forward,
  592. .data_reverse = parport_uss720_data_reverse,
  593. .init_state = parport_uss720_init_state,
  594. .save_state = parport_uss720_save_state,
  595. .restore_state = parport_uss720_restore_state,
  596. .epp_write_data = parport_uss720_epp_write_data,
  597. .epp_read_data = parport_uss720_epp_read_data,
  598. .epp_write_addr = parport_uss720_epp_write_addr,
  599. .epp_read_addr = parport_uss720_epp_read_addr,
  600. .ecp_write_data = parport_uss720_ecp_write_data,
  601. .ecp_read_data = parport_uss720_ecp_read_data,
  602. .ecp_write_addr = parport_uss720_ecp_write_addr,
  603. .compat_write_data = parport_uss720_write_compat,
  604. .nibble_read_data = parport_ieee1284_read_nibble,
  605. .byte_read_data = parport_ieee1284_read_byte,
  606. };
  607. /* --------------------------------------------------------------------- */
  608. static int uss720_probe(struct usb_interface *intf,
  609. const struct usb_device_id *id)
  610. {
  611. struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
  612. struct usb_host_interface *interface;
  613. struct usb_host_endpoint *endpoint;
  614. struct parport_uss720_private *priv;
  615. struct parport *pp;
  616. unsigned char reg;
  617. int i;
  618. dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
  619. le16_to_cpu(usbdev->descriptor.idVendor),
  620. le16_to_cpu(usbdev->descriptor.idProduct));
  621. /* our known interfaces have 3 alternate settings */
  622. if (intf->num_altsetting != 3) {
  623. usb_put_dev(usbdev);
  624. return -ENODEV;
  625. }
  626. i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
  627. dev_dbg(&intf->dev, "set interface result %d\n", i);
  628. interface = intf->cur_altsetting;
  629. /*
  630. * Allocate parport interface
  631. */
  632. if (!(priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL))) {
  633. usb_put_dev(usbdev);
  634. return -ENOMEM;
  635. }
  636. priv->pp = NULL;
  637. priv->usbdev = usbdev;
  638. kref_init(&priv->ref_count);
  639. spin_lock_init(&priv->asynclock);
  640. INIT_LIST_HEAD(&priv->asynclist);
  641. if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
  642. printk(KERN_WARNING "uss720: could not register parport\n");
  643. goto probe_abort;
  644. }
  645. priv->pp = pp;
  646. pp->private_data = priv;
  647. pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
  648. /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
  649. set_1284_register(pp, 7, 0x00, GFP_KERNEL);
  650. set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
  651. set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
  652. /* debugging */
  653. get_1284_register(pp, 0, &reg, GFP_KERNEL);
  654. dev_dbg(&intf->dev, "reg: %02x %02x %02x %02x %02x %02x %02x\n",
  655. priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3],
  656. priv->reg[4], priv->reg[5], priv->reg[6]);
  657. endpoint = &interface->endpoint[2];
  658. dev_dbg(&intf->dev, "epaddr %d interval %d\n",
  659. endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
  660. parport_announce_port(pp);
  661. usb_set_intfdata(intf, pp);
  662. return 0;
  663. probe_abort:
  664. kill_all_async_requests_priv(priv);
  665. kref_put(&priv->ref_count, destroy_priv);
  666. return -ENODEV;
  667. }
  668. static void uss720_disconnect(struct usb_interface *intf)
  669. {
  670. struct parport *pp = usb_get_intfdata(intf);
  671. struct parport_uss720_private *priv;
  672. struct usb_device *usbdev;
  673. dev_dbg(&intf->dev, "disconnect\n");
  674. usb_set_intfdata(intf, NULL);
  675. if (pp) {
  676. priv = pp->private_data;
  677. usbdev = priv->usbdev;
  678. priv->usbdev = NULL;
  679. priv->pp = NULL;
  680. dev_dbg(&intf->dev, "parport_remove_port\n");
  681. parport_remove_port(pp);
  682. parport_put_port(pp);
  683. kill_all_async_requests_priv(priv);
  684. kref_put(&priv->ref_count, destroy_priv);
  685. }
  686. dev_dbg(&intf->dev, "disconnect done\n");
  687. }
  688. /* table of cables that work through this driver */
  689. static const struct usb_device_id uss720_table[] = {
  690. { USB_DEVICE(0x047e, 0x1001) },
  691. { USB_DEVICE(0x0557, 0x2001) },
  692. { USB_DEVICE(0x0729, 0x1284) },
  693. { USB_DEVICE(0x1293, 0x0002) },
  694. { USB_DEVICE(0x050d, 0x0002) },
  695. { } /* Terminating entry */
  696. };
  697. MODULE_DEVICE_TABLE (usb, uss720_table);
  698. static struct usb_driver uss720_driver = {
  699. .name = "uss720",
  700. .probe = uss720_probe,
  701. .disconnect = uss720_disconnect,
  702. .id_table = uss720_table,
  703. };
  704. /* --------------------------------------------------------------------- */
  705. MODULE_AUTHOR(DRIVER_AUTHOR);
  706. MODULE_DESCRIPTION(DRIVER_DESC);
  707. MODULE_LICENSE("GPL");
  708. static int __init uss720_init(void)
  709. {
  710. int retval;
  711. retval = usb_register(&uss720_driver);
  712. if (retval)
  713. goto out;
  714. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  715. DRIVER_DESC "\n");
  716. printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
  717. "driver to allow nonstandard\n");
  718. printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
  719. "USS720 usb to parallel cables\n");
  720. printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
  721. "printer, use usblp instead\n");
  722. out:
  723. return retval;
  724. }
  725. static void __exit uss720_cleanup(void)
  726. {
  727. usb_deregister(&uss720_driver);
  728. }
  729. module_init(uss720_init);
  730. module_exit(uss720_cleanup);
  731. /* --------------------------------------------------------------------- */