printer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * printer.c -- Printer gadget driver
  3. *
  4. * Copyright (C) 2003-2005 David Brownell
  5. * Copyright (C) 2006 Craig W. Nadler
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/delay.h>
  15. #include <linux/ioport.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/mutex.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/list.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/device.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/fs.h>
  27. #include <linux/poll.h>
  28. #include <linux/types.h>
  29. #include <linux/ctype.h>
  30. #include <linux/cdev.h>
  31. #include <asm/byteorder.h>
  32. #include <linux/io.h>
  33. #include <linux/irq.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/unaligned.h>
  36. #include <linux/usb/ch9.h>
  37. #include <linux/usb/gadget.h>
  38. #include <linux/usb/g_printer.h>
  39. #include "gadget_chips.h"
  40. /*
  41. * Kbuild is not very cooperative with respect to linking separately
  42. * compiled library objects into one module. So for now we won't use
  43. * separate compilation ... ensuring init/exit sections work to shrink
  44. * the runtime footprint, and giving us at least some parts of what
  45. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  46. */
  47. #include "composite.c"
  48. /*-------------------------------------------------------------------------*/
  49. USB_GADGET_COMPOSITE_OPTIONS();
  50. #define DRIVER_DESC "Printer Gadget"
  51. #define DRIVER_VERSION "2007 OCT 06"
  52. static DEFINE_MUTEX(printer_mutex);
  53. static const char shortname [] = "printer";
  54. static const char driver_desc [] = DRIVER_DESC;
  55. static dev_t g_printer_devno;
  56. static struct class *usb_gadget_class;
  57. /*-------------------------------------------------------------------------*/
  58. struct printer_dev {
  59. spinlock_t lock; /* lock this structure */
  60. /* lock buffer lists during read/write calls */
  61. struct mutex lock_printer_io;
  62. struct usb_gadget *gadget;
  63. s8 interface;
  64. struct usb_ep *in_ep, *out_ep;
  65. struct list_head rx_reqs; /* List of free RX structs */
  66. struct list_head rx_reqs_active; /* List of Active RX xfers */
  67. struct list_head rx_buffers; /* List of completed xfers */
  68. /* wait until there is data to be read. */
  69. wait_queue_head_t rx_wait;
  70. struct list_head tx_reqs; /* List of free TX structs */
  71. struct list_head tx_reqs_active; /* List of Active TX xfers */
  72. /* Wait until there are write buffers available to use. */
  73. wait_queue_head_t tx_wait;
  74. /* Wait until all write buffers have been sent. */
  75. wait_queue_head_t tx_flush_wait;
  76. struct usb_request *current_rx_req;
  77. size_t current_rx_bytes;
  78. u8 *current_rx_buf;
  79. u8 printer_status;
  80. u8 reset_printer;
  81. struct cdev printer_cdev;
  82. struct device *pdev;
  83. u8 printer_cdev_open;
  84. wait_queue_head_t wait;
  85. struct usb_function function;
  86. };
  87. static struct printer_dev usb_printer_gadget;
  88. /*-------------------------------------------------------------------------*/
  89. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  90. * Instead: allocate your own, using normal USB-IF procedures.
  91. */
  92. /* Thanks to NetChip Technologies for donating this product ID.
  93. */
  94. #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
  95. #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
  96. /* Some systems will want different product identifiers published in the
  97. * device descriptor, either numbers or strings or both. These string
  98. * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  99. */
  100. module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
  101. MODULE_PARM_DESC(iSerialNum, "1");
  102. static char *iPNPstring;
  103. module_param(iPNPstring, charp, S_IRUGO);
  104. MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
  105. /* Number of requests to allocate per endpoint, not used for ep0. */
  106. static unsigned qlen = 10;
  107. module_param(qlen, uint, S_IRUGO|S_IWUSR);
  108. #define QLEN qlen
  109. /*-------------------------------------------------------------------------*/
  110. /*
  111. * DESCRIPTORS ... most are static, but strings and (full) configuration
  112. * descriptors are built on demand.
  113. */
  114. /* holds our biggest descriptor */
  115. #define USB_DESC_BUFSIZE 256
  116. #define USB_BUFSIZE 8192
  117. static struct usb_device_descriptor device_desc = {
  118. .bLength = sizeof device_desc,
  119. .bDescriptorType = USB_DT_DEVICE,
  120. .bcdUSB = cpu_to_le16(0x0200),
  121. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  122. .bDeviceSubClass = 0,
  123. .bDeviceProtocol = 0,
  124. .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
  125. .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
  126. .bNumConfigurations = 1
  127. };
  128. static struct usb_interface_descriptor intf_desc = {
  129. .bLength = sizeof intf_desc,
  130. .bDescriptorType = USB_DT_INTERFACE,
  131. .bNumEndpoints = 2,
  132. .bInterfaceClass = USB_CLASS_PRINTER,
  133. .bInterfaceSubClass = 1, /* Printer Sub-Class */
  134. .bInterfaceProtocol = 2, /* Bi-Directional */
  135. .iInterface = 0
  136. };
  137. static struct usb_endpoint_descriptor fs_ep_in_desc = {
  138. .bLength = USB_DT_ENDPOINT_SIZE,
  139. .bDescriptorType = USB_DT_ENDPOINT,
  140. .bEndpointAddress = USB_DIR_IN,
  141. .bmAttributes = USB_ENDPOINT_XFER_BULK
  142. };
  143. static struct usb_endpoint_descriptor fs_ep_out_desc = {
  144. .bLength = USB_DT_ENDPOINT_SIZE,
  145. .bDescriptorType = USB_DT_ENDPOINT,
  146. .bEndpointAddress = USB_DIR_OUT,
  147. .bmAttributes = USB_ENDPOINT_XFER_BULK
  148. };
  149. static struct usb_descriptor_header *fs_printer_function[] = {
  150. (struct usb_descriptor_header *) &intf_desc,
  151. (struct usb_descriptor_header *) &fs_ep_in_desc,
  152. (struct usb_descriptor_header *) &fs_ep_out_desc,
  153. NULL
  154. };
  155. /*
  156. * usb 2.0 devices need to expose both high speed and full speed
  157. * descriptors, unless they only run at full speed.
  158. */
  159. static struct usb_endpoint_descriptor hs_ep_in_desc = {
  160. .bLength = USB_DT_ENDPOINT_SIZE,
  161. .bDescriptorType = USB_DT_ENDPOINT,
  162. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  163. .wMaxPacketSize = cpu_to_le16(512)
  164. };
  165. static struct usb_endpoint_descriptor hs_ep_out_desc = {
  166. .bLength = USB_DT_ENDPOINT_SIZE,
  167. .bDescriptorType = USB_DT_ENDPOINT,
  168. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  169. .wMaxPacketSize = cpu_to_le16(512)
  170. };
  171. static struct usb_qualifier_descriptor dev_qualifier = {
  172. .bLength = sizeof dev_qualifier,
  173. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  174. .bcdUSB = cpu_to_le16(0x0200),
  175. .bDeviceClass = USB_CLASS_PRINTER,
  176. .bNumConfigurations = 1
  177. };
  178. static struct usb_descriptor_header *hs_printer_function[] = {
  179. (struct usb_descriptor_header *) &intf_desc,
  180. (struct usb_descriptor_header *) &hs_ep_in_desc,
  181. (struct usb_descriptor_header *) &hs_ep_out_desc,
  182. NULL
  183. };
  184. static struct usb_otg_descriptor otg_descriptor = {
  185. .bLength = sizeof otg_descriptor,
  186. .bDescriptorType = USB_DT_OTG,
  187. .bmAttributes = USB_OTG_SRP,
  188. };
  189. static const struct usb_descriptor_header *otg_desc[] = {
  190. (struct usb_descriptor_header *) &otg_descriptor,
  191. NULL,
  192. };
  193. /* maxpacket and other transfer characteristics vary by speed. */
  194. #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
  195. /*-------------------------------------------------------------------------*/
  196. /* descriptors that are built on-demand */
  197. static char product_desc [40] = DRIVER_DESC;
  198. static char serial_num [40] = "1";
  199. static char pnp_string [1024] =
  200. "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
  201. /* static strings, in UTF-8 */
  202. static struct usb_string strings [] = {
  203. [USB_GADGET_MANUFACTURER_IDX].s = "",
  204. [USB_GADGET_PRODUCT_IDX].s = product_desc,
  205. [USB_GADGET_SERIAL_IDX].s = serial_num,
  206. { } /* end of list */
  207. };
  208. static struct usb_gadget_strings stringtab_dev = {
  209. .language = 0x0409, /* en-us */
  210. .strings = strings,
  211. };
  212. static struct usb_gadget_strings *dev_strings[] = {
  213. &stringtab_dev,
  214. NULL,
  215. };
  216. /*-------------------------------------------------------------------------*/
  217. static struct usb_request *
  218. printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
  219. {
  220. struct usb_request *req;
  221. req = usb_ep_alloc_request(ep, gfp_flags);
  222. if (req != NULL) {
  223. req->length = len;
  224. req->buf = kmalloc(len, gfp_flags);
  225. if (req->buf == NULL) {
  226. usb_ep_free_request(ep, req);
  227. return NULL;
  228. }
  229. }
  230. return req;
  231. }
  232. static void
  233. printer_req_free(struct usb_ep *ep, struct usb_request *req)
  234. {
  235. if (ep != NULL && req != NULL) {
  236. kfree(req->buf);
  237. usb_ep_free_request(ep, req);
  238. }
  239. }
  240. /*-------------------------------------------------------------------------*/
  241. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  242. {
  243. struct printer_dev *dev = ep->driver_data;
  244. int status = req->status;
  245. unsigned long flags;
  246. spin_lock_irqsave(&dev->lock, flags);
  247. list_del_init(&req->list); /* Remode from Active List */
  248. switch (status) {
  249. /* normal completion */
  250. case 0:
  251. if (req->actual > 0) {
  252. list_add_tail(&req->list, &dev->rx_buffers);
  253. DBG(dev, "G_Printer : rx length %d\n", req->actual);
  254. } else {
  255. list_add(&req->list, &dev->rx_reqs);
  256. }
  257. break;
  258. /* software-driven interface shutdown */
  259. case -ECONNRESET: /* unlink */
  260. case -ESHUTDOWN: /* disconnect etc */
  261. VDBG(dev, "rx shutdown, code %d\n", status);
  262. list_add(&req->list, &dev->rx_reqs);
  263. break;
  264. /* for hardware automagic (such as pxa) */
  265. case -ECONNABORTED: /* endpoint reset */
  266. DBG(dev, "rx %s reset\n", ep->name);
  267. list_add(&req->list, &dev->rx_reqs);
  268. break;
  269. /* data overrun */
  270. case -EOVERFLOW:
  271. /* FALLTHROUGH */
  272. default:
  273. DBG(dev, "rx status %d\n", status);
  274. list_add(&req->list, &dev->rx_reqs);
  275. break;
  276. }
  277. wake_up_interruptible(&dev->rx_wait);
  278. spin_unlock_irqrestore(&dev->lock, flags);
  279. }
  280. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  281. {
  282. struct printer_dev *dev = ep->driver_data;
  283. switch (req->status) {
  284. default:
  285. VDBG(dev, "tx err %d\n", req->status);
  286. /* FALLTHROUGH */
  287. case -ECONNRESET: /* unlink */
  288. case -ESHUTDOWN: /* disconnect etc */
  289. break;
  290. case 0:
  291. break;
  292. }
  293. spin_lock(&dev->lock);
  294. /* Take the request struct off the active list and put it on the
  295. * free list.
  296. */
  297. list_del_init(&req->list);
  298. list_add(&req->list, &dev->tx_reqs);
  299. wake_up_interruptible(&dev->tx_wait);
  300. if (likely(list_empty(&dev->tx_reqs_active)))
  301. wake_up_interruptible(&dev->tx_flush_wait);
  302. spin_unlock(&dev->lock);
  303. }
  304. /*-------------------------------------------------------------------------*/
  305. static int
  306. printer_open(struct inode *inode, struct file *fd)
  307. {
  308. struct printer_dev *dev;
  309. unsigned long flags;
  310. int ret = -EBUSY;
  311. mutex_lock(&printer_mutex);
  312. dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
  313. spin_lock_irqsave(&dev->lock, flags);
  314. if (!dev->printer_cdev_open) {
  315. dev->printer_cdev_open = 1;
  316. fd->private_data = dev;
  317. ret = 0;
  318. /* Change the printer status to show that it's on-line. */
  319. dev->printer_status |= PRINTER_SELECTED;
  320. }
  321. spin_unlock_irqrestore(&dev->lock, flags);
  322. DBG(dev, "printer_open returned %x\n", ret);
  323. mutex_unlock(&printer_mutex);
  324. return ret;
  325. }
  326. static int
  327. printer_close(struct inode *inode, struct file *fd)
  328. {
  329. struct printer_dev *dev = fd->private_data;
  330. unsigned long flags;
  331. spin_lock_irqsave(&dev->lock, flags);
  332. dev->printer_cdev_open = 0;
  333. fd->private_data = NULL;
  334. /* Change printer status to show that the printer is off-line. */
  335. dev->printer_status &= ~PRINTER_SELECTED;
  336. spin_unlock_irqrestore(&dev->lock, flags);
  337. DBG(dev, "printer_close\n");
  338. return 0;
  339. }
  340. /* This function must be called with interrupts turned off. */
  341. static void
  342. setup_rx_reqs(struct printer_dev *dev)
  343. {
  344. struct usb_request *req;
  345. while (likely(!list_empty(&dev->rx_reqs))) {
  346. int error;
  347. req = container_of(dev->rx_reqs.next,
  348. struct usb_request, list);
  349. list_del_init(&req->list);
  350. /* The USB Host sends us whatever amount of data it wants to
  351. * so we always set the length field to the full USB_BUFSIZE.
  352. * If the amount of data is more than the read() caller asked
  353. * for it will be stored in the request buffer until it is
  354. * asked for by read().
  355. */
  356. req->length = USB_BUFSIZE;
  357. req->complete = rx_complete;
  358. error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
  359. if (error) {
  360. DBG(dev, "rx submit --> %d\n", error);
  361. list_add(&req->list, &dev->rx_reqs);
  362. break;
  363. } else {
  364. list_add(&req->list, &dev->rx_reqs_active);
  365. }
  366. }
  367. }
  368. static ssize_t
  369. printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  370. {
  371. struct printer_dev *dev = fd->private_data;
  372. unsigned long flags;
  373. size_t size;
  374. size_t bytes_copied;
  375. struct usb_request *req;
  376. /* This is a pointer to the current USB rx request. */
  377. struct usb_request *current_rx_req;
  378. /* This is the number of bytes in the current rx buffer. */
  379. size_t current_rx_bytes;
  380. /* This is a pointer to the current rx buffer. */
  381. u8 *current_rx_buf;
  382. if (len == 0)
  383. return -EINVAL;
  384. DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
  385. mutex_lock(&dev->lock_printer_io);
  386. spin_lock_irqsave(&dev->lock, flags);
  387. /* We will use this flag later to check if a printer reset happened
  388. * after we turn interrupts back on.
  389. */
  390. dev->reset_printer = 0;
  391. setup_rx_reqs(dev);
  392. bytes_copied = 0;
  393. current_rx_req = dev->current_rx_req;
  394. current_rx_bytes = dev->current_rx_bytes;
  395. current_rx_buf = dev->current_rx_buf;
  396. dev->current_rx_req = NULL;
  397. dev->current_rx_bytes = 0;
  398. dev->current_rx_buf = NULL;
  399. /* Check if there is any data in the read buffers. Please note that
  400. * current_rx_bytes is the number of bytes in the current rx buffer.
  401. * If it is zero then check if there are any other rx_buffers that
  402. * are on the completed list. We are only out of data if all rx
  403. * buffers are empty.
  404. */
  405. if ((current_rx_bytes == 0) &&
  406. (likely(list_empty(&dev->rx_buffers)))) {
  407. /* Turn interrupts back on before sleeping. */
  408. spin_unlock_irqrestore(&dev->lock, flags);
  409. /*
  410. * If no data is available check if this is a NON-Blocking
  411. * call or not.
  412. */
  413. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  414. mutex_unlock(&dev->lock_printer_io);
  415. return -EAGAIN;
  416. }
  417. /* Sleep until data is available */
  418. wait_event_interruptible(dev->rx_wait,
  419. (likely(!list_empty(&dev->rx_buffers))));
  420. spin_lock_irqsave(&dev->lock, flags);
  421. }
  422. /* We have data to return then copy it to the caller's buffer.*/
  423. while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
  424. && len) {
  425. if (current_rx_bytes == 0) {
  426. req = container_of(dev->rx_buffers.next,
  427. struct usb_request, list);
  428. list_del_init(&req->list);
  429. if (req->actual && req->buf) {
  430. current_rx_req = req;
  431. current_rx_bytes = req->actual;
  432. current_rx_buf = req->buf;
  433. } else {
  434. list_add(&req->list, &dev->rx_reqs);
  435. continue;
  436. }
  437. }
  438. /* Don't leave irqs off while doing memory copies */
  439. spin_unlock_irqrestore(&dev->lock, flags);
  440. if (len > current_rx_bytes)
  441. size = current_rx_bytes;
  442. else
  443. size = len;
  444. size -= copy_to_user(buf, current_rx_buf, size);
  445. bytes_copied += size;
  446. len -= size;
  447. buf += size;
  448. spin_lock_irqsave(&dev->lock, flags);
  449. /* We've disconnected or reset so return. */
  450. if (dev->reset_printer) {
  451. list_add(&current_rx_req->list, &dev->rx_reqs);
  452. spin_unlock_irqrestore(&dev->lock, flags);
  453. mutex_unlock(&dev->lock_printer_io);
  454. return -EAGAIN;
  455. }
  456. /* If we not returning all the data left in this RX request
  457. * buffer then adjust the amount of data left in the buffer.
  458. * Othewise if we are done with this RX request buffer then
  459. * requeue it to get any incoming data from the USB host.
  460. */
  461. if (size < current_rx_bytes) {
  462. current_rx_bytes -= size;
  463. current_rx_buf += size;
  464. } else {
  465. list_add(&current_rx_req->list, &dev->rx_reqs);
  466. current_rx_bytes = 0;
  467. current_rx_buf = NULL;
  468. current_rx_req = NULL;
  469. }
  470. }
  471. dev->current_rx_req = current_rx_req;
  472. dev->current_rx_bytes = current_rx_bytes;
  473. dev->current_rx_buf = current_rx_buf;
  474. spin_unlock_irqrestore(&dev->lock, flags);
  475. mutex_unlock(&dev->lock_printer_io);
  476. DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
  477. if (bytes_copied)
  478. return bytes_copied;
  479. else
  480. return -EAGAIN;
  481. }
  482. static ssize_t
  483. printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  484. {
  485. struct printer_dev *dev = fd->private_data;
  486. unsigned long flags;
  487. size_t size; /* Amount of data in a TX request. */
  488. size_t bytes_copied = 0;
  489. struct usb_request *req;
  490. DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
  491. if (len == 0)
  492. return -EINVAL;
  493. mutex_lock(&dev->lock_printer_io);
  494. spin_lock_irqsave(&dev->lock, flags);
  495. /* Check if a printer reset happens while we have interrupts on */
  496. dev->reset_printer = 0;
  497. /* Check if there is any available write buffers */
  498. if (likely(list_empty(&dev->tx_reqs))) {
  499. /* Turn interrupts back on before sleeping. */
  500. spin_unlock_irqrestore(&dev->lock, flags);
  501. /*
  502. * If write buffers are available check if this is
  503. * a NON-Blocking call or not.
  504. */
  505. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  506. mutex_unlock(&dev->lock_printer_io);
  507. return -EAGAIN;
  508. }
  509. /* Sleep until a write buffer is available */
  510. wait_event_interruptible(dev->tx_wait,
  511. (likely(!list_empty(&dev->tx_reqs))));
  512. spin_lock_irqsave(&dev->lock, flags);
  513. }
  514. while (likely(!list_empty(&dev->tx_reqs)) && len) {
  515. if (len > USB_BUFSIZE)
  516. size = USB_BUFSIZE;
  517. else
  518. size = len;
  519. req = container_of(dev->tx_reqs.next, struct usb_request,
  520. list);
  521. list_del_init(&req->list);
  522. req->complete = tx_complete;
  523. req->length = size;
  524. /* Check if we need to send a zero length packet. */
  525. if (len > size)
  526. /* They will be more TX requests so no yet. */
  527. req->zero = 0;
  528. else
  529. /* If the data amount is not a multple of the
  530. * maxpacket size then send a zero length packet.
  531. */
  532. req->zero = ((len % dev->in_ep->maxpacket) == 0);
  533. /* Don't leave irqs off while doing memory copies */
  534. spin_unlock_irqrestore(&dev->lock, flags);
  535. if (copy_from_user(req->buf, buf, size)) {
  536. list_add(&req->list, &dev->tx_reqs);
  537. mutex_unlock(&dev->lock_printer_io);
  538. return bytes_copied;
  539. }
  540. bytes_copied += size;
  541. len -= size;
  542. buf += size;
  543. spin_lock_irqsave(&dev->lock, flags);
  544. /* We've disconnected or reset so free the req and buffer */
  545. if (dev->reset_printer) {
  546. list_add(&req->list, &dev->tx_reqs);
  547. spin_unlock_irqrestore(&dev->lock, flags);
  548. mutex_unlock(&dev->lock_printer_io);
  549. return -EAGAIN;
  550. }
  551. if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
  552. list_add(&req->list, &dev->tx_reqs);
  553. spin_unlock_irqrestore(&dev->lock, flags);
  554. mutex_unlock(&dev->lock_printer_io);
  555. return -EAGAIN;
  556. }
  557. list_add(&req->list, &dev->tx_reqs_active);
  558. }
  559. spin_unlock_irqrestore(&dev->lock, flags);
  560. mutex_unlock(&dev->lock_printer_io);
  561. DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
  562. if (bytes_copied) {
  563. return bytes_copied;
  564. } else {
  565. return -EAGAIN;
  566. }
  567. }
  568. static int
  569. printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
  570. {
  571. struct printer_dev *dev = fd->private_data;
  572. struct inode *inode = fd->f_path.dentry->d_inode;
  573. unsigned long flags;
  574. int tx_list_empty;
  575. mutex_lock(&inode->i_mutex);
  576. spin_lock_irqsave(&dev->lock, flags);
  577. tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
  578. spin_unlock_irqrestore(&dev->lock, flags);
  579. if (!tx_list_empty) {
  580. /* Sleep until all data has been sent */
  581. wait_event_interruptible(dev->tx_flush_wait,
  582. (likely(list_empty(&dev->tx_reqs_active))));
  583. }
  584. mutex_unlock(&inode->i_mutex);
  585. return 0;
  586. }
  587. static unsigned int
  588. printer_poll(struct file *fd, poll_table *wait)
  589. {
  590. struct printer_dev *dev = fd->private_data;
  591. unsigned long flags;
  592. int status = 0;
  593. mutex_lock(&dev->lock_printer_io);
  594. spin_lock_irqsave(&dev->lock, flags);
  595. setup_rx_reqs(dev);
  596. spin_unlock_irqrestore(&dev->lock, flags);
  597. mutex_unlock(&dev->lock_printer_io);
  598. poll_wait(fd, &dev->rx_wait, wait);
  599. poll_wait(fd, &dev->tx_wait, wait);
  600. spin_lock_irqsave(&dev->lock, flags);
  601. if (likely(!list_empty(&dev->tx_reqs)))
  602. status |= POLLOUT | POLLWRNORM;
  603. if (likely(dev->current_rx_bytes) ||
  604. likely(!list_empty(&dev->rx_buffers)))
  605. status |= POLLIN | POLLRDNORM;
  606. spin_unlock_irqrestore(&dev->lock, flags);
  607. return status;
  608. }
  609. static long
  610. printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
  611. {
  612. struct printer_dev *dev = fd->private_data;
  613. unsigned long flags;
  614. int status = 0;
  615. DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
  616. /* handle ioctls */
  617. spin_lock_irqsave(&dev->lock, flags);
  618. switch (code) {
  619. case GADGET_GET_PRINTER_STATUS:
  620. status = (int)dev->printer_status;
  621. break;
  622. case GADGET_SET_PRINTER_STATUS:
  623. dev->printer_status = (u8)arg;
  624. break;
  625. default:
  626. /* could not handle ioctl */
  627. DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
  628. code);
  629. status = -ENOTTY;
  630. }
  631. spin_unlock_irqrestore(&dev->lock, flags);
  632. return status;
  633. }
  634. /* used after endpoint configuration */
  635. static const struct file_operations printer_io_operations = {
  636. .owner = THIS_MODULE,
  637. .open = printer_open,
  638. .read = printer_read,
  639. .write = printer_write,
  640. .fsync = printer_fsync,
  641. .poll = printer_poll,
  642. .unlocked_ioctl = printer_ioctl,
  643. .release = printer_close,
  644. .llseek = noop_llseek,
  645. };
  646. /*-------------------------------------------------------------------------*/
  647. static int
  648. set_printer_interface(struct printer_dev *dev)
  649. {
  650. int result = 0;
  651. dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
  652. dev->in_ep->driver_data = dev;
  653. dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
  654. &fs_ep_out_desc);
  655. dev->out_ep->driver_data = dev;
  656. result = usb_ep_enable(dev->in_ep);
  657. if (result != 0) {
  658. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  659. goto done;
  660. }
  661. result = usb_ep_enable(dev->out_ep);
  662. if (result != 0) {
  663. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  664. goto done;
  665. }
  666. done:
  667. /* on error, disable any endpoints */
  668. if (result != 0) {
  669. (void) usb_ep_disable(dev->in_ep);
  670. (void) usb_ep_disable(dev->out_ep);
  671. dev->in_ep->desc = NULL;
  672. dev->out_ep->desc = NULL;
  673. }
  674. /* caller is responsible for cleanup on error */
  675. return result;
  676. }
  677. static void printer_reset_interface(struct printer_dev *dev)
  678. {
  679. if (dev->interface < 0)
  680. return;
  681. DBG(dev, "%s\n", __func__);
  682. if (dev->in_ep->desc)
  683. usb_ep_disable(dev->in_ep);
  684. if (dev->out_ep->desc)
  685. usb_ep_disable(dev->out_ep);
  686. dev->in_ep->desc = NULL;
  687. dev->out_ep->desc = NULL;
  688. dev->interface = -1;
  689. }
  690. /* Change our operational Interface. */
  691. static int set_interface(struct printer_dev *dev, unsigned number)
  692. {
  693. int result = 0;
  694. /* Free the current interface */
  695. printer_reset_interface(dev);
  696. result = set_printer_interface(dev);
  697. if (result)
  698. printer_reset_interface(dev);
  699. else
  700. dev->interface = number;
  701. if (!result)
  702. INFO(dev, "Using interface %x\n", number);
  703. return result;
  704. }
  705. static void printer_soft_reset(struct printer_dev *dev)
  706. {
  707. struct usb_request *req;
  708. INFO(dev, "Received Printer Reset Request\n");
  709. if (usb_ep_disable(dev->in_ep))
  710. DBG(dev, "Failed to disable USB in_ep\n");
  711. if (usb_ep_disable(dev->out_ep))
  712. DBG(dev, "Failed to disable USB out_ep\n");
  713. if (dev->current_rx_req != NULL) {
  714. list_add(&dev->current_rx_req->list, &dev->rx_reqs);
  715. dev->current_rx_req = NULL;
  716. }
  717. dev->current_rx_bytes = 0;
  718. dev->current_rx_buf = NULL;
  719. dev->reset_printer = 1;
  720. while (likely(!(list_empty(&dev->rx_buffers)))) {
  721. req = container_of(dev->rx_buffers.next, struct usb_request,
  722. list);
  723. list_del_init(&req->list);
  724. list_add(&req->list, &dev->rx_reqs);
  725. }
  726. while (likely(!(list_empty(&dev->rx_reqs_active)))) {
  727. req = container_of(dev->rx_buffers.next, struct usb_request,
  728. list);
  729. list_del_init(&req->list);
  730. list_add(&req->list, &dev->rx_reqs);
  731. }
  732. while (likely(!(list_empty(&dev->tx_reqs_active)))) {
  733. req = container_of(dev->tx_reqs_active.next,
  734. struct usb_request, list);
  735. list_del_init(&req->list);
  736. list_add(&req->list, &dev->tx_reqs);
  737. }
  738. if (usb_ep_enable(dev->in_ep))
  739. DBG(dev, "Failed to enable USB in_ep\n");
  740. if (usb_ep_enable(dev->out_ep))
  741. DBG(dev, "Failed to enable USB out_ep\n");
  742. wake_up_interruptible(&dev->rx_wait);
  743. wake_up_interruptible(&dev->tx_wait);
  744. wake_up_interruptible(&dev->tx_flush_wait);
  745. }
  746. /*-------------------------------------------------------------------------*/
  747. /*
  748. * The setup() callback implements all the ep0 functionality that's not
  749. * handled lower down.
  750. */
  751. static int printer_func_setup(struct usb_function *f,
  752. const struct usb_ctrlrequest *ctrl)
  753. {
  754. struct printer_dev *dev = container_of(f, struct printer_dev, function);
  755. struct usb_composite_dev *cdev = f->config->cdev;
  756. struct usb_request *req = cdev->req;
  757. int value = -EOPNOTSUPP;
  758. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  759. u16 wValue = le16_to_cpu(ctrl->wValue);
  760. u16 wLength = le16_to_cpu(ctrl->wLength);
  761. DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
  762. ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
  763. switch (ctrl->bRequestType&USB_TYPE_MASK) {
  764. case USB_TYPE_CLASS:
  765. switch (ctrl->bRequest) {
  766. case 0: /* Get the IEEE-1284 PNP String */
  767. /* Only one printer interface is supported. */
  768. if ((wIndex>>8) != dev->interface)
  769. break;
  770. value = (pnp_string[0]<<8)|pnp_string[1];
  771. memcpy(req->buf, pnp_string, value);
  772. DBG(dev, "1284 PNP String: %x %s\n", value,
  773. &pnp_string[2]);
  774. break;
  775. case 1: /* Get Port Status */
  776. /* Only one printer interface is supported. */
  777. if (wIndex != dev->interface)
  778. break;
  779. *(u8 *)req->buf = dev->printer_status;
  780. value = min(wLength, (u16) 1);
  781. break;
  782. case 2: /* Soft Reset */
  783. /* Only one printer interface is supported. */
  784. if (wIndex != dev->interface)
  785. break;
  786. printer_soft_reset(dev);
  787. value = 0;
  788. break;
  789. default:
  790. goto unknown;
  791. }
  792. break;
  793. default:
  794. unknown:
  795. VDBG(dev,
  796. "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
  797. ctrl->bRequestType, ctrl->bRequest,
  798. wValue, wIndex, wLength);
  799. break;
  800. }
  801. /* host either stalls (value < 0) or reports success */
  802. return value;
  803. }
  804. static int __init printer_func_bind(struct usb_configuration *c,
  805. struct usb_function *f)
  806. {
  807. struct printer_dev *dev = container_of(f, struct printer_dev, function);
  808. struct usb_composite_dev *cdev = c->cdev;
  809. struct usb_ep *in_ep, *out_ep;
  810. int id;
  811. id = usb_interface_id(c, f);
  812. if (id < 0)
  813. return id;
  814. intf_desc.bInterfaceNumber = id;
  815. /* all we really need is bulk IN/OUT */
  816. in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
  817. if (!in_ep) {
  818. autoconf_fail:
  819. dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
  820. cdev->gadget->name);
  821. return -ENODEV;
  822. }
  823. in_ep->driver_data = in_ep; /* claim */
  824. out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
  825. if (!out_ep)
  826. goto autoconf_fail;
  827. out_ep->driver_data = out_ep; /* claim */
  828. /* assumes that all endpoints are dual-speed */
  829. hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  830. hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  831. dev->in_ep = in_ep;
  832. dev->out_ep = out_ep;
  833. return 0;
  834. }
  835. static void printer_func_unbind(struct usb_configuration *c,
  836. struct usb_function *f)
  837. {
  838. }
  839. static int printer_func_set_alt(struct usb_function *f,
  840. unsigned intf, unsigned alt)
  841. {
  842. struct printer_dev *dev = container_of(f, struct printer_dev, function);
  843. int ret = -ENOTSUPP;
  844. if (!alt)
  845. ret = set_interface(dev, intf);
  846. return ret;
  847. }
  848. static void printer_func_disable(struct usb_function *f)
  849. {
  850. struct printer_dev *dev = container_of(f, struct printer_dev, function);
  851. unsigned long flags;
  852. DBG(dev, "%s\n", __func__);
  853. spin_lock_irqsave(&dev->lock, flags);
  854. printer_reset_interface(dev);
  855. spin_unlock_irqrestore(&dev->lock, flags);
  856. }
  857. static void printer_cfg_unbind(struct usb_configuration *c)
  858. {
  859. struct printer_dev *dev;
  860. struct usb_request *req;
  861. dev = &usb_printer_gadget;
  862. DBG(dev, "%s\n", __func__);
  863. /* Remove sysfs files */
  864. device_destroy(usb_gadget_class, g_printer_devno);
  865. /* Remove Character Device */
  866. cdev_del(&dev->printer_cdev);
  867. /* we must already have been disconnected ... no i/o may be active */
  868. WARN_ON(!list_empty(&dev->tx_reqs_active));
  869. WARN_ON(!list_empty(&dev->rx_reqs_active));
  870. /* Free all memory for this driver. */
  871. while (!list_empty(&dev->tx_reqs)) {
  872. req = container_of(dev->tx_reqs.next, struct usb_request,
  873. list);
  874. list_del(&req->list);
  875. printer_req_free(dev->in_ep, req);
  876. }
  877. if (dev->current_rx_req != NULL)
  878. printer_req_free(dev->out_ep, dev->current_rx_req);
  879. while (!list_empty(&dev->rx_reqs)) {
  880. req = container_of(dev->rx_reqs.next,
  881. struct usb_request, list);
  882. list_del(&req->list);
  883. printer_req_free(dev->out_ep, req);
  884. }
  885. while (!list_empty(&dev->rx_buffers)) {
  886. req = container_of(dev->rx_buffers.next,
  887. struct usb_request, list);
  888. list_del(&req->list);
  889. printer_req_free(dev->out_ep, req);
  890. }
  891. }
  892. static struct usb_configuration printer_cfg_driver = {
  893. .label = "printer",
  894. .unbind = printer_cfg_unbind,
  895. .bConfigurationValue = 1,
  896. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  897. };
  898. static int __init printer_bind_config(struct usb_configuration *c)
  899. {
  900. struct usb_gadget *gadget = c->cdev->gadget;
  901. struct printer_dev *dev;
  902. int status = -ENOMEM;
  903. int gcnum;
  904. size_t len;
  905. u32 i;
  906. struct usb_request *req;
  907. usb_ep_autoconfig_reset(gadget);
  908. dev = &usb_printer_gadget;
  909. dev->function.name = shortname;
  910. dev->function.descriptors = fs_printer_function;
  911. dev->function.hs_descriptors = hs_printer_function;
  912. dev->function.bind = printer_func_bind;
  913. dev->function.setup = printer_func_setup;
  914. dev->function.unbind = printer_func_unbind;
  915. dev->function.set_alt = printer_func_set_alt;
  916. dev->function.disable = printer_func_disable;
  917. status = usb_add_function(c, &dev->function);
  918. if (status)
  919. return status;
  920. /* Setup the sysfs files for the printer gadget. */
  921. dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
  922. NULL, "g_printer");
  923. if (IS_ERR(dev->pdev)) {
  924. ERROR(dev, "Failed to create device: g_printer\n");
  925. goto fail;
  926. }
  927. /*
  928. * Register a character device as an interface to a user mode
  929. * program that handles the printer specific functionality.
  930. */
  931. cdev_init(&dev->printer_cdev, &printer_io_operations);
  932. dev->printer_cdev.owner = THIS_MODULE;
  933. status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
  934. if (status) {
  935. ERROR(dev, "Failed to open char device\n");
  936. goto fail;
  937. }
  938. gcnum = usb_gadget_controller_number(gadget);
  939. if (gcnum >= 0) {
  940. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  941. } else {
  942. dev_warn(&gadget->dev, "controller '%s' not recognized\n",
  943. gadget->name);
  944. /* unrecognized, but safe unless bulk is REALLY quirky */
  945. device_desc.bcdDevice =
  946. cpu_to_le16(0xFFFF);
  947. }
  948. if (iPNPstring)
  949. strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
  950. len = strlen(pnp_string);
  951. pnp_string[0] = (len >> 8) & 0xFF;
  952. pnp_string[1] = len & 0xFF;
  953. usb_gadget_set_selfpowered(gadget);
  954. if (gadget->is_otg) {
  955. otg_descriptor.bmAttributes |= USB_OTG_HNP;
  956. printer_cfg_driver.descriptors = otg_desc;
  957. printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  958. }
  959. spin_lock_init(&dev->lock);
  960. mutex_init(&dev->lock_printer_io);
  961. INIT_LIST_HEAD(&dev->tx_reqs);
  962. INIT_LIST_HEAD(&dev->tx_reqs_active);
  963. INIT_LIST_HEAD(&dev->rx_reqs);
  964. INIT_LIST_HEAD(&dev->rx_reqs_active);
  965. INIT_LIST_HEAD(&dev->rx_buffers);
  966. init_waitqueue_head(&dev->rx_wait);
  967. init_waitqueue_head(&dev->tx_wait);
  968. init_waitqueue_head(&dev->tx_flush_wait);
  969. dev->interface = -1;
  970. dev->printer_cdev_open = 0;
  971. dev->printer_status = PRINTER_NOT_ERROR;
  972. dev->current_rx_req = NULL;
  973. dev->current_rx_bytes = 0;
  974. dev->current_rx_buf = NULL;
  975. for (i = 0; i < QLEN; i++) {
  976. req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
  977. if (!req) {
  978. while (!list_empty(&dev->tx_reqs)) {
  979. req = container_of(dev->tx_reqs.next,
  980. struct usb_request, list);
  981. list_del(&req->list);
  982. printer_req_free(dev->in_ep, req);
  983. }
  984. return -ENOMEM;
  985. }
  986. list_add(&req->list, &dev->tx_reqs);
  987. }
  988. for (i = 0; i < QLEN; i++) {
  989. req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
  990. if (!req) {
  991. while (!list_empty(&dev->rx_reqs)) {
  992. req = container_of(dev->rx_reqs.next,
  993. struct usb_request, list);
  994. list_del(&req->list);
  995. printer_req_free(dev->out_ep, req);
  996. }
  997. return -ENOMEM;
  998. }
  999. list_add(&req->list, &dev->rx_reqs);
  1000. }
  1001. /* finish hookup to lower layer ... */
  1002. dev->gadget = gadget;
  1003. INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  1004. return 0;
  1005. fail:
  1006. printer_cfg_unbind(c);
  1007. return status;
  1008. }
  1009. static int printer_unbind(struct usb_composite_dev *cdev)
  1010. {
  1011. return 0;
  1012. }
  1013. static int __init printer_bind(struct usb_composite_dev *cdev)
  1014. {
  1015. int ret;
  1016. ret = usb_string_ids_tab(cdev, strings);
  1017. if (ret < 0)
  1018. return ret;
  1019. device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
  1020. device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
  1021. device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
  1022. ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
  1023. if (ret)
  1024. return ret;
  1025. usb_composite_overwrite_options(cdev, &coverwrite);
  1026. return ret;
  1027. }
  1028. static __refdata struct usb_composite_driver printer_driver = {
  1029. .name = shortname,
  1030. .dev = &device_desc,
  1031. .strings = dev_strings,
  1032. .max_speed = USB_SPEED_HIGH,
  1033. .bind = printer_bind,
  1034. .unbind = printer_unbind,
  1035. };
  1036. static int __init
  1037. init(void)
  1038. {
  1039. int status;
  1040. usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
  1041. if (IS_ERR(usb_gadget_class)) {
  1042. status = PTR_ERR(usb_gadget_class);
  1043. pr_err("unable to create usb_gadget class %d\n", status);
  1044. return status;
  1045. }
  1046. status = alloc_chrdev_region(&g_printer_devno, 0, 1,
  1047. "USB printer gadget");
  1048. if (status) {
  1049. pr_err("alloc_chrdev_region %d\n", status);
  1050. class_destroy(usb_gadget_class);
  1051. return status;
  1052. }
  1053. status = usb_composite_probe(&printer_driver);
  1054. if (status) {
  1055. class_destroy(usb_gadget_class);
  1056. unregister_chrdev_region(g_printer_devno, 1);
  1057. pr_err("usb_gadget_probe_driver %x\n", status);
  1058. }
  1059. return status;
  1060. }
  1061. module_init(init);
  1062. static void __exit
  1063. cleanup(void)
  1064. {
  1065. mutex_lock(&usb_printer_gadget.lock_printer_io);
  1066. usb_composite_unregister(&printer_driver);
  1067. unregister_chrdev_region(g_printer_devno, 1);
  1068. class_destroy(usb_gadget_class);
  1069. mutex_unlock(&usb_printer_gadget.lock_printer_io);
  1070. }
  1071. module_exit(cleanup);
  1072. MODULE_DESCRIPTION(DRIVER_DESC);
  1073. MODULE_AUTHOR("Craig Nadler");
  1074. MODULE_LICENSE("GPL");