printer.c 34 KB

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