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