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