printer.c 34 KB

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