printer.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/ioport.h>
  25. #include <linux/sched.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/timer.h>
  31. #include <linux/list.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/utsname.h>
  34. #include <linux/device.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/fs.h>
  37. #include <linux/poll.h>
  38. #include <linux/types.h>
  39. #include <linux/ctype.h>
  40. #include <linux/cdev.h>
  41. #include <asm/byteorder.h>
  42. #include <linux/io.h>
  43. #include <linux/irq.h>
  44. #include <asm/system.h>
  45. #include <linux/uaccess.h>
  46. #include <asm/unaligned.h>
  47. #include <linux/usb/ch9.h>
  48. #include <linux/usb/gadget.h>
  49. #include <linux/usb/g_printer.h>
  50. #include "gadget_chips.h"
  51. /*
  52. * Kbuild is not very cooperative with respect to linking separately
  53. * compiled library objects into one module. So for now we won't use
  54. * separate compilation ... ensuring init/exit sections work to shrink
  55. * the runtime footprint, and giving us at least some parts of what
  56. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  57. */
  58. #include "usbstring.c"
  59. #include "config.c"
  60. #include "epautoconf.c"
  61. /*-------------------------------------------------------------------------*/
  62. #define DRIVER_DESC "Printer Gadget"
  63. #define DRIVER_VERSION "2007 OCT 06"
  64. static DEFINE_MUTEX(printer_mutex);
  65. static const char shortname [] = "printer";
  66. static const char driver_desc [] = DRIVER_DESC;
  67. static dev_t g_printer_devno;
  68. static struct class *usb_gadget_class;
  69. /*-------------------------------------------------------------------------*/
  70. struct printer_dev {
  71. spinlock_t lock; /* lock this structure */
  72. /* lock buffer lists during read/write calls */
  73. struct mutex lock_printer_io;
  74. struct usb_gadget *gadget;
  75. struct usb_request *req; /* for control responses */
  76. u8 config;
  77. s8 interface;
  78. struct usb_ep *in_ep, *out_ep;
  79. struct list_head rx_reqs; /* List of free RX structs */
  80. struct list_head rx_reqs_active; /* List of Active RX xfers */
  81. struct list_head rx_buffers; /* List of completed xfers */
  82. /* wait until there is data to be read. */
  83. wait_queue_head_t rx_wait;
  84. struct list_head tx_reqs; /* List of free TX structs */
  85. struct list_head tx_reqs_active; /* List of Active TX xfers */
  86. /* Wait until there are write buffers available to use. */
  87. wait_queue_head_t tx_wait;
  88. /* Wait until all write buffers have been sent. */
  89. wait_queue_head_t tx_flush_wait;
  90. struct usb_request *current_rx_req;
  91. size_t current_rx_bytes;
  92. u8 *current_rx_buf;
  93. u8 printer_status;
  94. u8 reset_printer;
  95. struct cdev printer_cdev;
  96. struct device *pdev;
  97. u8 printer_cdev_open;
  98. wait_queue_head_t wait;
  99. };
  100. static struct printer_dev usb_printer_gadget;
  101. /*-------------------------------------------------------------------------*/
  102. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  103. * Instead: allocate your own, using normal USB-IF procedures.
  104. */
  105. /* Thanks to NetChip Technologies for donating this product ID.
  106. */
  107. #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
  108. #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
  109. /* Some systems will want different product identifiers published in the
  110. * device descriptor, either numbers or strings or both. These string
  111. * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  112. */
  113. static ushort idVendor;
  114. module_param(idVendor, ushort, S_IRUGO);
  115. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  116. static ushort idProduct;
  117. module_param(idProduct, ushort, S_IRUGO);
  118. MODULE_PARM_DESC(idProduct, "USB Product ID");
  119. static ushort bcdDevice;
  120. module_param(bcdDevice, ushort, S_IRUGO);
  121. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  122. static char *iManufacturer;
  123. module_param(iManufacturer, charp, S_IRUGO);
  124. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  125. static char *iProduct;
  126. module_param(iProduct, charp, S_IRUGO);
  127. MODULE_PARM_DESC(iProduct, "USB Product string");
  128. static char *iSerialNum;
  129. module_param(iSerialNum, charp, S_IRUGO);
  130. MODULE_PARM_DESC(iSerialNum, "1");
  131. static char *iPNPstring;
  132. module_param(iPNPstring, charp, S_IRUGO);
  133. MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
  134. /* Number of requests to allocate per endpoint, not used for ep0. */
  135. static unsigned qlen = 10;
  136. module_param(qlen, uint, S_IRUGO|S_IWUSR);
  137. #define QLEN qlen
  138. #ifdef CONFIG_USB_GADGET_DUALSPEED
  139. #define DEVSPEED USB_SPEED_HIGH
  140. #else /* full speed (low speed doesn't do bulk) */
  141. #define DEVSPEED USB_SPEED_FULL
  142. #endif
  143. /*-------------------------------------------------------------------------*/
  144. #define xprintk(d, level, fmt, args...) \
  145. printk(level "%s: " fmt, DRIVER_DESC, ## args)
  146. #ifdef DEBUG
  147. #define DBG(dev, fmt, args...) \
  148. xprintk(dev, KERN_DEBUG, fmt, ## args)
  149. #else
  150. #define DBG(dev, fmt, args...) \
  151. do { } while (0)
  152. #endif /* DEBUG */
  153. #ifdef VERBOSE
  154. #define VDBG(dev, fmt, args...) \
  155. xprintk(dev, KERN_DEBUG, fmt, ## args)
  156. #else
  157. #define VDBG(dev, fmt, args...) \
  158. do { } while (0)
  159. #endif /* VERBOSE */
  160. #define ERROR(dev, fmt, args...) \
  161. xprintk(dev, KERN_ERR, fmt, ## args)
  162. #define WARNING(dev, fmt, args...) \
  163. xprintk(dev, KERN_WARNING, fmt, ## args)
  164. #define INFO(dev, fmt, args...) \
  165. xprintk(dev, KERN_INFO, fmt, ## args)
  166. /*-------------------------------------------------------------------------*/
  167. /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
  168. * ep0 implementation: descriptors, config management, setup().
  169. * also optional class-specific notification interrupt transfer.
  170. */
  171. /*
  172. * DESCRIPTORS ... most are static, but strings and (full) configuration
  173. * descriptors are built on demand.
  174. */
  175. #define STRING_MANUFACTURER 1
  176. #define STRING_PRODUCT 2
  177. #define STRING_SERIALNUM 3
  178. /* holds our biggest descriptor */
  179. #define USB_DESC_BUFSIZE 256
  180. #define USB_BUFSIZE 8192
  181. /* This device advertises one configuration. */
  182. #define DEV_CONFIG_VALUE 1
  183. #define PRINTER_INTERFACE 0
  184. static struct usb_device_descriptor device_desc = {
  185. .bLength = sizeof device_desc,
  186. .bDescriptorType = USB_DT_DEVICE,
  187. .bcdUSB = cpu_to_le16(0x0200),
  188. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  189. .bDeviceSubClass = 0,
  190. .bDeviceProtocol = 0,
  191. .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
  192. .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
  193. .iManufacturer = STRING_MANUFACTURER,
  194. .iProduct = STRING_PRODUCT,
  195. .iSerialNumber = STRING_SERIALNUM,
  196. .bNumConfigurations = 1
  197. };
  198. static struct usb_otg_descriptor otg_desc = {
  199. .bLength = sizeof otg_desc,
  200. .bDescriptorType = USB_DT_OTG,
  201. .bmAttributes = USB_OTG_SRP
  202. };
  203. static struct usb_config_descriptor config_desc = {
  204. .bLength = sizeof config_desc,
  205. .bDescriptorType = USB_DT_CONFIG,
  206. /* compute wTotalLength on the fly */
  207. .bNumInterfaces = 1,
  208. .bConfigurationValue = DEV_CONFIG_VALUE,
  209. .iConfiguration = 0,
  210. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  211. .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  212. };
  213. static struct usb_interface_descriptor intf_desc = {
  214. .bLength = sizeof intf_desc,
  215. .bDescriptorType = USB_DT_INTERFACE,
  216. .bInterfaceNumber = PRINTER_INTERFACE,
  217. .bNumEndpoints = 2,
  218. .bInterfaceClass = USB_CLASS_PRINTER,
  219. .bInterfaceSubClass = 1, /* Printer Sub-Class */
  220. .bInterfaceProtocol = 2, /* Bi-Directional */
  221. .iInterface = 0
  222. };
  223. static struct usb_endpoint_descriptor fs_ep_in_desc = {
  224. .bLength = USB_DT_ENDPOINT_SIZE,
  225. .bDescriptorType = USB_DT_ENDPOINT,
  226. .bEndpointAddress = USB_DIR_IN,
  227. .bmAttributes = USB_ENDPOINT_XFER_BULK
  228. };
  229. static struct usb_endpoint_descriptor fs_ep_out_desc = {
  230. .bLength = USB_DT_ENDPOINT_SIZE,
  231. .bDescriptorType = USB_DT_ENDPOINT,
  232. .bEndpointAddress = USB_DIR_OUT,
  233. .bmAttributes = USB_ENDPOINT_XFER_BULK
  234. };
  235. static const struct usb_descriptor_header *fs_printer_function [11] = {
  236. (struct usb_descriptor_header *) &otg_desc,
  237. (struct usb_descriptor_header *) &intf_desc,
  238. (struct usb_descriptor_header *) &fs_ep_in_desc,
  239. (struct usb_descriptor_header *) &fs_ep_out_desc,
  240. NULL
  241. };
  242. #ifdef CONFIG_USB_GADGET_DUALSPEED
  243. /*
  244. * usb 2.0 devices need to expose both high speed and full speed
  245. * descriptors, unless they only run at full speed.
  246. */
  247. static struct usb_endpoint_descriptor hs_ep_in_desc = {
  248. .bLength = USB_DT_ENDPOINT_SIZE,
  249. .bDescriptorType = USB_DT_ENDPOINT,
  250. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  251. .wMaxPacketSize = cpu_to_le16(512)
  252. };
  253. static struct usb_endpoint_descriptor hs_ep_out_desc = {
  254. .bLength = USB_DT_ENDPOINT_SIZE,
  255. .bDescriptorType = USB_DT_ENDPOINT,
  256. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  257. .wMaxPacketSize = cpu_to_le16(512)
  258. };
  259. static struct usb_qualifier_descriptor dev_qualifier = {
  260. .bLength = sizeof dev_qualifier,
  261. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  262. .bcdUSB = cpu_to_le16(0x0200),
  263. .bDeviceClass = USB_CLASS_PRINTER,
  264. .bNumConfigurations = 1
  265. };
  266. static const struct usb_descriptor_header *hs_printer_function [11] = {
  267. (struct usb_descriptor_header *) &otg_desc,
  268. (struct usb_descriptor_header *) &intf_desc,
  269. (struct usb_descriptor_header *) &hs_ep_in_desc,
  270. (struct usb_descriptor_header *) &hs_ep_out_desc,
  271. NULL
  272. };
  273. /* maxpacket and other transfer characteristics vary by speed. */
  274. #define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
  275. #else
  276. /* if there's no high speed support, maxpacket doesn't change. */
  277. #define ep_desc(g, hs, fs) (((void)(g)), (fs))
  278. #endif /* !CONFIG_USB_GADGET_DUALSPEED */
  279. /*-------------------------------------------------------------------------*/
  280. /* descriptors that are built on-demand */
  281. static char manufacturer [50];
  282. static char product_desc [40] = DRIVER_DESC;
  283. static char serial_num [40] = "1";
  284. static char pnp_string [1024] =
  285. "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
  286. /* static strings, in UTF-8 */
  287. static struct usb_string strings [] = {
  288. { STRING_MANUFACTURER, manufacturer, },
  289. { STRING_PRODUCT, product_desc, },
  290. { STRING_SERIALNUM, serial_num, },
  291. { } /* end of list */
  292. };
  293. static struct usb_gadget_strings stringtab = {
  294. .language = 0x0409, /* en-us */
  295. .strings = strings,
  296. };
  297. /*-------------------------------------------------------------------------*/
  298. static struct usb_request *
  299. printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
  300. {
  301. struct usb_request *req;
  302. req = usb_ep_alloc_request(ep, gfp_flags);
  303. if (req != NULL) {
  304. req->length = len;
  305. req->buf = kmalloc(len, gfp_flags);
  306. if (req->buf == NULL) {
  307. usb_ep_free_request(ep, req);
  308. return NULL;
  309. }
  310. }
  311. return req;
  312. }
  313. static void
  314. printer_req_free(struct usb_ep *ep, struct usb_request *req)
  315. {
  316. if (ep != NULL && req != NULL) {
  317. kfree(req->buf);
  318. usb_ep_free_request(ep, req);
  319. }
  320. }
  321. /*-------------------------------------------------------------------------*/
  322. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  323. {
  324. struct printer_dev *dev = ep->driver_data;
  325. int status = req->status;
  326. unsigned long flags;
  327. spin_lock_irqsave(&dev->lock, flags);
  328. list_del_init(&req->list); /* Remode from Active List */
  329. switch (status) {
  330. /* normal completion */
  331. case 0:
  332. if (req->actual > 0) {
  333. list_add_tail(&req->list, &dev->rx_buffers);
  334. DBG(dev, "G_Printer : rx length %d\n", req->actual);
  335. } else {
  336. list_add(&req->list, &dev->rx_reqs);
  337. }
  338. break;
  339. /* software-driven interface shutdown */
  340. case -ECONNRESET: /* unlink */
  341. case -ESHUTDOWN: /* disconnect etc */
  342. VDBG(dev, "rx shutdown, code %d\n", status);
  343. list_add(&req->list, &dev->rx_reqs);
  344. break;
  345. /* for hardware automagic (such as pxa) */
  346. case -ECONNABORTED: /* endpoint reset */
  347. DBG(dev, "rx %s reset\n", ep->name);
  348. list_add(&req->list, &dev->rx_reqs);
  349. break;
  350. /* data overrun */
  351. case -EOVERFLOW:
  352. /* FALLTHROUGH */
  353. default:
  354. DBG(dev, "rx status %d\n", status);
  355. list_add(&req->list, &dev->rx_reqs);
  356. break;
  357. }
  358. wake_up_interruptible(&dev->rx_wait);
  359. spin_unlock_irqrestore(&dev->lock, flags);
  360. }
  361. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  362. {
  363. struct printer_dev *dev = ep->driver_data;
  364. switch (req->status) {
  365. default:
  366. VDBG(dev, "tx err %d\n", req->status);
  367. /* FALLTHROUGH */
  368. case -ECONNRESET: /* unlink */
  369. case -ESHUTDOWN: /* disconnect etc */
  370. break;
  371. case 0:
  372. break;
  373. }
  374. spin_lock(&dev->lock);
  375. /* Take the request struct off the active list and put it on the
  376. * free list.
  377. */
  378. list_del_init(&req->list);
  379. list_add(&req->list, &dev->tx_reqs);
  380. wake_up_interruptible(&dev->tx_wait);
  381. if (likely(list_empty(&dev->tx_reqs_active)))
  382. wake_up_interruptible(&dev->tx_flush_wait);
  383. spin_unlock(&dev->lock);
  384. }
  385. /*-------------------------------------------------------------------------*/
  386. static int
  387. printer_open(struct inode *inode, struct file *fd)
  388. {
  389. struct printer_dev *dev;
  390. unsigned long flags;
  391. int ret = -EBUSY;
  392. mutex_lock(&printer_mutex);
  393. dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
  394. spin_lock_irqsave(&dev->lock, flags);
  395. if (!dev->printer_cdev_open) {
  396. dev->printer_cdev_open = 1;
  397. fd->private_data = dev;
  398. ret = 0;
  399. /* Change the printer status to show that it's on-line. */
  400. dev->printer_status |= PRINTER_SELECTED;
  401. }
  402. spin_unlock_irqrestore(&dev->lock, flags);
  403. DBG(dev, "printer_open returned %x\n", ret);
  404. mutex_unlock(&printer_mutex);
  405. return ret;
  406. }
  407. static int
  408. printer_close(struct inode *inode, struct file *fd)
  409. {
  410. struct printer_dev *dev = fd->private_data;
  411. unsigned long flags;
  412. spin_lock_irqsave(&dev->lock, flags);
  413. dev->printer_cdev_open = 0;
  414. fd->private_data = NULL;
  415. /* Change printer status to show that the printer is off-line. */
  416. dev->printer_status &= ~PRINTER_SELECTED;
  417. spin_unlock_irqrestore(&dev->lock, flags);
  418. DBG(dev, "printer_close\n");
  419. return 0;
  420. }
  421. /* This function must be called with interrupts turned off. */
  422. static void
  423. setup_rx_reqs(struct printer_dev *dev)
  424. {
  425. struct usb_request *req;
  426. while (likely(!list_empty(&dev->rx_reqs))) {
  427. int error;
  428. req = container_of(dev->rx_reqs.next,
  429. struct usb_request, list);
  430. list_del_init(&req->list);
  431. /* The USB Host sends us whatever amount of data it wants to
  432. * so we always set the length field to the full USB_BUFSIZE.
  433. * If the amount of data is more than the read() caller asked
  434. * for it will be stored in the request buffer until it is
  435. * asked for by read().
  436. */
  437. req->length = USB_BUFSIZE;
  438. req->complete = rx_complete;
  439. error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
  440. if (error) {
  441. DBG(dev, "rx submit --> %d\n", error);
  442. list_add(&req->list, &dev->rx_reqs);
  443. break;
  444. } else {
  445. list_add(&req->list, &dev->rx_reqs_active);
  446. }
  447. }
  448. }
  449. static ssize_t
  450. printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  451. {
  452. struct printer_dev *dev = fd->private_data;
  453. unsigned long flags;
  454. size_t size;
  455. size_t bytes_copied;
  456. struct usb_request *req;
  457. /* This is a pointer to the current USB rx request. */
  458. struct usb_request *current_rx_req;
  459. /* This is the number of bytes in the current rx buffer. */
  460. size_t current_rx_bytes;
  461. /* This is a pointer to the current rx buffer. */
  462. u8 *current_rx_buf;
  463. if (len == 0)
  464. return -EINVAL;
  465. DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
  466. mutex_lock(&dev->lock_printer_io);
  467. spin_lock_irqsave(&dev->lock, flags);
  468. /* We will use this flag later to check if a printer reset happened
  469. * after we turn interrupts back on.
  470. */
  471. dev->reset_printer = 0;
  472. setup_rx_reqs(dev);
  473. bytes_copied = 0;
  474. current_rx_req = dev->current_rx_req;
  475. current_rx_bytes = dev->current_rx_bytes;
  476. current_rx_buf = dev->current_rx_buf;
  477. dev->current_rx_req = NULL;
  478. dev->current_rx_bytes = 0;
  479. dev->current_rx_buf = NULL;
  480. /* Check if there is any data in the read buffers. Please note that
  481. * current_rx_bytes is the number of bytes in the current rx buffer.
  482. * If it is zero then check if there are any other rx_buffers that
  483. * are on the completed list. We are only out of data if all rx
  484. * buffers are empty.
  485. */
  486. if ((current_rx_bytes == 0) &&
  487. (likely(list_empty(&dev->rx_buffers)))) {
  488. /* Turn interrupts back on before sleeping. */
  489. spin_unlock_irqrestore(&dev->lock, flags);
  490. /*
  491. * If no data is available check if this is a NON-Blocking
  492. * call or not.
  493. */
  494. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  495. mutex_unlock(&dev->lock_printer_io);
  496. return -EAGAIN;
  497. }
  498. /* Sleep until data is available */
  499. wait_event_interruptible(dev->rx_wait,
  500. (likely(!list_empty(&dev->rx_buffers))));
  501. spin_lock_irqsave(&dev->lock, flags);
  502. }
  503. /* We have data to return then copy it to the caller's buffer.*/
  504. while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
  505. && len) {
  506. if (current_rx_bytes == 0) {
  507. req = container_of(dev->rx_buffers.next,
  508. struct usb_request, list);
  509. list_del_init(&req->list);
  510. if (req->actual && req->buf) {
  511. current_rx_req = req;
  512. current_rx_bytes = req->actual;
  513. current_rx_buf = req->buf;
  514. } else {
  515. list_add(&req->list, &dev->rx_reqs);
  516. continue;
  517. }
  518. }
  519. /* Don't leave irqs off while doing memory copies */
  520. spin_unlock_irqrestore(&dev->lock, flags);
  521. if (len > current_rx_bytes)
  522. size = current_rx_bytes;
  523. else
  524. size = len;
  525. size -= copy_to_user(buf, current_rx_buf, size);
  526. bytes_copied += size;
  527. len -= size;
  528. buf += size;
  529. spin_lock_irqsave(&dev->lock, flags);
  530. /* We've disconnected or reset so return. */
  531. if (dev->reset_printer) {
  532. list_add(&current_rx_req->list, &dev->rx_reqs);
  533. spin_unlock_irqrestore(&dev->lock, flags);
  534. mutex_unlock(&dev->lock_printer_io);
  535. return -EAGAIN;
  536. }
  537. /* If we not returning all the data left in this RX request
  538. * buffer then adjust the amount of data left in the buffer.
  539. * Othewise if we are done with this RX request buffer then
  540. * requeue it to get any incoming data from the USB host.
  541. */
  542. if (size < current_rx_bytes) {
  543. current_rx_bytes -= size;
  544. current_rx_buf += size;
  545. } else {
  546. list_add(&current_rx_req->list, &dev->rx_reqs);
  547. current_rx_bytes = 0;
  548. current_rx_buf = NULL;
  549. current_rx_req = NULL;
  550. }
  551. }
  552. dev->current_rx_req = current_rx_req;
  553. dev->current_rx_bytes = current_rx_bytes;
  554. dev->current_rx_buf = current_rx_buf;
  555. spin_unlock_irqrestore(&dev->lock, flags);
  556. mutex_unlock(&dev->lock_printer_io);
  557. DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
  558. if (bytes_copied)
  559. return bytes_copied;
  560. else
  561. return -EAGAIN;
  562. }
  563. static ssize_t
  564. printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  565. {
  566. struct printer_dev *dev = fd->private_data;
  567. unsigned long flags;
  568. size_t size; /* Amount of data in a TX request. */
  569. size_t bytes_copied = 0;
  570. struct usb_request *req;
  571. DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
  572. if (len == 0)
  573. return -EINVAL;
  574. mutex_lock(&dev->lock_printer_io);
  575. spin_lock_irqsave(&dev->lock, flags);
  576. /* Check if a printer reset happens while we have interrupts on */
  577. dev->reset_printer = 0;
  578. /* Check if there is any available write buffers */
  579. if (likely(list_empty(&dev->tx_reqs))) {
  580. /* Turn interrupts back on before sleeping. */
  581. spin_unlock_irqrestore(&dev->lock, flags);
  582. /*
  583. * If write buffers are available check if this is
  584. * a NON-Blocking call or not.
  585. */
  586. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  587. mutex_unlock(&dev->lock_printer_io);
  588. return -EAGAIN;
  589. }
  590. /* Sleep until a write buffer is available */
  591. wait_event_interruptible(dev->tx_wait,
  592. (likely(!list_empty(&dev->tx_reqs))));
  593. spin_lock_irqsave(&dev->lock, flags);
  594. }
  595. while (likely(!list_empty(&dev->tx_reqs)) && len) {
  596. if (len > USB_BUFSIZE)
  597. size = USB_BUFSIZE;
  598. else
  599. size = len;
  600. req = container_of(dev->tx_reqs.next, struct usb_request,
  601. list);
  602. list_del_init(&req->list);
  603. req->complete = tx_complete;
  604. req->length = size;
  605. /* Check if we need to send a zero length packet. */
  606. if (len > size)
  607. /* They will be more TX requests so no yet. */
  608. req->zero = 0;
  609. else
  610. /* If the data amount is not a multple of the
  611. * maxpacket size then send a zero length packet.
  612. */
  613. req->zero = ((len % dev->in_ep->maxpacket) == 0);
  614. /* Don't leave irqs off while doing memory copies */
  615. spin_unlock_irqrestore(&dev->lock, flags);
  616. if (copy_from_user(req->buf, buf, size)) {
  617. list_add(&req->list, &dev->tx_reqs);
  618. mutex_unlock(&dev->lock_printer_io);
  619. return bytes_copied;
  620. }
  621. bytes_copied += size;
  622. len -= size;
  623. buf += size;
  624. spin_lock_irqsave(&dev->lock, flags);
  625. /* We've disconnected or reset so free the req and buffer */
  626. if (dev->reset_printer) {
  627. list_add(&req->list, &dev->tx_reqs);
  628. spin_unlock_irqrestore(&dev->lock, flags);
  629. mutex_unlock(&dev->lock_printer_io);
  630. return -EAGAIN;
  631. }
  632. if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
  633. list_add(&req->list, &dev->tx_reqs);
  634. spin_unlock_irqrestore(&dev->lock, flags);
  635. mutex_unlock(&dev->lock_printer_io);
  636. return -EAGAIN;
  637. }
  638. list_add(&req->list, &dev->tx_reqs_active);
  639. }
  640. spin_unlock_irqrestore(&dev->lock, flags);
  641. mutex_unlock(&dev->lock_printer_io);
  642. DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
  643. if (bytes_copied) {
  644. return bytes_copied;
  645. } else {
  646. return -EAGAIN;
  647. }
  648. }
  649. static int
  650. printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
  651. {
  652. struct printer_dev *dev = fd->private_data;
  653. struct inode *inode = fd->f_path.dentry->d_inode;
  654. unsigned long flags;
  655. int tx_list_empty;
  656. mutex_lock(&inode->i_mutex);
  657. spin_lock_irqsave(&dev->lock, flags);
  658. tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
  659. spin_unlock_irqrestore(&dev->lock, flags);
  660. if (!tx_list_empty) {
  661. /* Sleep until all data has been sent */
  662. wait_event_interruptible(dev->tx_flush_wait,
  663. (likely(list_empty(&dev->tx_reqs_active))));
  664. }
  665. mutex_unlock(&inode->i_mutex);
  666. return 0;
  667. }
  668. static unsigned int
  669. printer_poll(struct file *fd, poll_table *wait)
  670. {
  671. struct printer_dev *dev = fd->private_data;
  672. unsigned long flags;
  673. int status = 0;
  674. mutex_lock(&dev->lock_printer_io);
  675. spin_lock_irqsave(&dev->lock, flags);
  676. setup_rx_reqs(dev);
  677. spin_unlock_irqrestore(&dev->lock, flags);
  678. mutex_unlock(&dev->lock_printer_io);
  679. poll_wait(fd, &dev->rx_wait, wait);
  680. poll_wait(fd, &dev->tx_wait, wait);
  681. spin_lock_irqsave(&dev->lock, flags);
  682. if (likely(!list_empty(&dev->tx_reqs)))
  683. status |= POLLOUT | POLLWRNORM;
  684. if (likely(dev->current_rx_bytes) ||
  685. likely(!list_empty(&dev->rx_buffers)))
  686. status |= POLLIN | POLLRDNORM;
  687. spin_unlock_irqrestore(&dev->lock, flags);
  688. return status;
  689. }
  690. static long
  691. printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
  692. {
  693. struct printer_dev *dev = fd->private_data;
  694. unsigned long flags;
  695. int status = 0;
  696. DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
  697. /* handle ioctls */
  698. spin_lock_irqsave(&dev->lock, flags);
  699. switch (code) {
  700. case GADGET_GET_PRINTER_STATUS:
  701. status = (int)dev->printer_status;
  702. break;
  703. case GADGET_SET_PRINTER_STATUS:
  704. dev->printer_status = (u8)arg;
  705. break;
  706. default:
  707. /* could not handle ioctl */
  708. DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
  709. code);
  710. status = -ENOTTY;
  711. }
  712. spin_unlock_irqrestore(&dev->lock, flags);
  713. return status;
  714. }
  715. /* used after endpoint configuration */
  716. static const struct file_operations printer_io_operations = {
  717. .owner = THIS_MODULE,
  718. .open = printer_open,
  719. .read = printer_read,
  720. .write = printer_write,
  721. .fsync = printer_fsync,
  722. .poll = printer_poll,
  723. .unlocked_ioctl = printer_ioctl,
  724. .release = printer_close,
  725. .llseek = noop_llseek,
  726. };
  727. /*-------------------------------------------------------------------------*/
  728. static int
  729. set_printer_interface(struct printer_dev *dev)
  730. {
  731. int result = 0;
  732. dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
  733. dev->in_ep->driver_data = dev;
  734. dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
  735. &fs_ep_out_desc);
  736. dev->out_ep->driver_data = dev;
  737. result = usb_ep_enable(dev->in_ep);
  738. if (result != 0) {
  739. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  740. goto done;
  741. }
  742. result = usb_ep_enable(dev->out_ep);
  743. if (result != 0) {
  744. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  745. goto done;
  746. }
  747. done:
  748. /* on error, disable any endpoints */
  749. if (result != 0) {
  750. (void) usb_ep_disable(dev->in_ep);
  751. (void) usb_ep_disable(dev->out_ep);
  752. dev->in_ep->desc = NULL;
  753. dev->out_ep->desc = NULL;
  754. }
  755. /* caller is responsible for cleanup on error */
  756. return result;
  757. }
  758. static void printer_reset_interface(struct printer_dev *dev)
  759. {
  760. if (dev->interface < 0)
  761. return;
  762. DBG(dev, "%s\n", __func__);
  763. if (dev->in_ep->desc)
  764. usb_ep_disable(dev->in_ep);
  765. if (dev->out_ep->desc)
  766. usb_ep_disable(dev->out_ep);
  767. dev->in_ep->desc = NULL;
  768. dev->out_ep->desc = NULL;
  769. dev->interface = -1;
  770. }
  771. /* change our operational config. must agree with the code
  772. * that returns config descriptors, and altsetting code.
  773. */
  774. static int
  775. printer_set_config(struct printer_dev *dev, unsigned number)
  776. {
  777. int result = 0;
  778. struct usb_gadget *gadget = dev->gadget;
  779. switch (number) {
  780. case DEV_CONFIG_VALUE:
  781. result = 0;
  782. break;
  783. default:
  784. result = -EINVAL;
  785. /* FALL THROUGH */
  786. case 0:
  787. break;
  788. }
  789. if (result) {
  790. usb_gadget_vbus_draw(dev->gadget,
  791. dev->gadget->is_otg ? 8 : 100);
  792. } else {
  793. char *speed;
  794. unsigned power;
  795. power = 2 * config_desc.bMaxPower;
  796. usb_gadget_vbus_draw(dev->gadget, power);
  797. switch (gadget->speed) {
  798. case USB_SPEED_FULL: speed = "full"; break;
  799. #ifdef CONFIG_USB_GADGET_DUALSPEED
  800. case USB_SPEED_HIGH: speed = "high"; break;
  801. #endif
  802. default: speed = "?"; break;
  803. }
  804. dev->config = number;
  805. INFO(dev, "%s speed config #%d: %d mA, %s\n",
  806. speed, number, power, driver_desc);
  807. }
  808. return result;
  809. }
  810. static int
  811. config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
  812. int is_otg)
  813. {
  814. int len;
  815. const struct usb_descriptor_header **function;
  816. #ifdef CONFIG_USB_GADGET_DUALSPEED
  817. int hs = (speed == USB_SPEED_HIGH);
  818. if (type == USB_DT_OTHER_SPEED_CONFIG)
  819. hs = !hs;
  820. if (hs) {
  821. function = hs_printer_function;
  822. } else {
  823. function = fs_printer_function;
  824. }
  825. #else
  826. function = fs_printer_function;
  827. #endif
  828. if (index >= device_desc.bNumConfigurations)
  829. return -EINVAL;
  830. /* for now, don't advertise srp-only devices */
  831. if (!is_otg)
  832. function++;
  833. len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
  834. function);
  835. if (len < 0)
  836. return len;
  837. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  838. return len;
  839. }
  840. /* Change our operational Interface. */
  841. static int
  842. set_interface(struct printer_dev *dev, unsigned number)
  843. {
  844. int result = 0;
  845. /* Free the current interface */
  846. switch (dev->interface) {
  847. case PRINTER_INTERFACE:
  848. printer_reset_interface(dev);
  849. break;
  850. }
  851. switch (number) {
  852. case PRINTER_INTERFACE:
  853. result = set_printer_interface(dev);
  854. if (result) {
  855. printer_reset_interface(dev);
  856. } else {
  857. dev->interface = PRINTER_INTERFACE;
  858. }
  859. break;
  860. default:
  861. result = -EINVAL;
  862. /* FALL THROUGH */
  863. }
  864. if (!result)
  865. INFO(dev, "Using interface %x\n", number);
  866. return result;
  867. }
  868. static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
  869. {
  870. if (req->status || req->actual != req->length)
  871. DBG((struct printer_dev *) ep->driver_data,
  872. "setup complete --> %d, %d/%d\n",
  873. req->status, req->actual, req->length);
  874. }
  875. static void printer_soft_reset(struct printer_dev *dev)
  876. {
  877. struct usb_request *req;
  878. INFO(dev, "Received Printer Reset Request\n");
  879. if (usb_ep_disable(dev->in_ep))
  880. DBG(dev, "Failed to disable USB in_ep\n");
  881. if (usb_ep_disable(dev->out_ep))
  882. DBG(dev, "Failed to disable USB out_ep\n");
  883. if (dev->current_rx_req != NULL) {
  884. list_add(&dev->current_rx_req->list, &dev->rx_reqs);
  885. dev->current_rx_req = NULL;
  886. }
  887. dev->current_rx_bytes = 0;
  888. dev->current_rx_buf = NULL;
  889. dev->reset_printer = 1;
  890. while (likely(!(list_empty(&dev->rx_buffers)))) {
  891. req = container_of(dev->rx_buffers.next, struct usb_request,
  892. list);
  893. list_del_init(&req->list);
  894. list_add(&req->list, &dev->rx_reqs);
  895. }
  896. while (likely(!(list_empty(&dev->rx_reqs_active)))) {
  897. req = container_of(dev->rx_buffers.next, struct usb_request,
  898. list);
  899. list_del_init(&req->list);
  900. list_add(&req->list, &dev->rx_reqs);
  901. }
  902. while (likely(!(list_empty(&dev->tx_reqs_active)))) {
  903. req = container_of(dev->tx_reqs_active.next,
  904. struct usb_request, list);
  905. list_del_init(&req->list);
  906. list_add(&req->list, &dev->tx_reqs);
  907. }
  908. if (usb_ep_enable(dev->in_ep))
  909. DBG(dev, "Failed to enable USB in_ep\n");
  910. if (usb_ep_enable(dev->out_ep))
  911. DBG(dev, "Failed to enable USB out_ep\n");
  912. wake_up_interruptible(&dev->rx_wait);
  913. wake_up_interruptible(&dev->tx_wait);
  914. wake_up_interruptible(&dev->tx_flush_wait);
  915. }
  916. /*-------------------------------------------------------------------------*/
  917. /*
  918. * The setup() callback implements all the ep0 functionality that's not
  919. * handled lower down.
  920. */
  921. static int
  922. printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  923. {
  924. struct printer_dev *dev = get_gadget_data(gadget);
  925. struct usb_request *req = dev->req;
  926. int value = -EOPNOTSUPP;
  927. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  928. u16 wValue = le16_to_cpu(ctrl->wValue);
  929. u16 wLength = le16_to_cpu(ctrl->wLength);
  930. DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
  931. ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
  932. req->complete = printer_setup_complete;
  933. switch (ctrl->bRequestType&USB_TYPE_MASK) {
  934. case USB_TYPE_STANDARD:
  935. switch (ctrl->bRequest) {
  936. case USB_REQ_GET_DESCRIPTOR:
  937. if (ctrl->bRequestType != USB_DIR_IN)
  938. break;
  939. switch (wValue >> 8) {
  940. case USB_DT_DEVICE:
  941. device_desc.bMaxPacketSize0 =
  942. gadget->ep0->maxpacket;
  943. value = min(wLength, (u16) sizeof device_desc);
  944. memcpy(req->buf, &device_desc, value);
  945. break;
  946. #ifdef CONFIG_USB_GADGET_DUALSPEED
  947. case USB_DT_DEVICE_QUALIFIER:
  948. if (!gadget->is_dualspeed)
  949. break;
  950. /*
  951. * assumes ep0 uses the same value for both
  952. * speeds
  953. */
  954. dev_qualifier.bMaxPacketSize0 =
  955. gadget->ep0->maxpacket;
  956. value = min(wLength,
  957. (u16) sizeof dev_qualifier);
  958. memcpy(req->buf, &dev_qualifier, value);
  959. break;
  960. case USB_DT_OTHER_SPEED_CONFIG:
  961. if (!gadget->is_dualspeed)
  962. break;
  963. /* FALLTHROUGH */
  964. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  965. case USB_DT_CONFIG:
  966. value = config_buf(gadget->speed, req->buf,
  967. wValue >> 8,
  968. wValue & 0xff,
  969. gadget->is_otg);
  970. if (value >= 0)
  971. value = min(wLength, (u16) value);
  972. break;
  973. case USB_DT_STRING:
  974. value = usb_gadget_get_string(&stringtab,
  975. wValue & 0xff, req->buf);
  976. if (value >= 0)
  977. value = min(wLength, (u16) value);
  978. break;
  979. }
  980. break;
  981. case USB_REQ_SET_CONFIGURATION:
  982. if (ctrl->bRequestType != 0)
  983. break;
  984. if (gadget->a_hnp_support)
  985. DBG(dev, "HNP available\n");
  986. else if (gadget->a_alt_hnp_support)
  987. DBG(dev, "HNP needs a different root port\n");
  988. value = printer_set_config(dev, wValue);
  989. if (!value)
  990. value = set_interface(dev, PRINTER_INTERFACE);
  991. break;
  992. case USB_REQ_GET_CONFIGURATION:
  993. if (ctrl->bRequestType != USB_DIR_IN)
  994. break;
  995. *(u8 *)req->buf = dev->config;
  996. value = min(wLength, (u16) 1);
  997. break;
  998. case USB_REQ_SET_INTERFACE:
  999. if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
  1000. !dev->config)
  1001. break;
  1002. value = set_interface(dev, PRINTER_INTERFACE);
  1003. break;
  1004. case USB_REQ_GET_INTERFACE:
  1005. if (ctrl->bRequestType !=
  1006. (USB_DIR_IN|USB_RECIP_INTERFACE)
  1007. || !dev->config)
  1008. break;
  1009. *(u8 *)req->buf = dev->interface;
  1010. value = min(wLength, (u16) 1);
  1011. break;
  1012. default:
  1013. goto unknown;
  1014. }
  1015. break;
  1016. case USB_TYPE_CLASS:
  1017. switch (ctrl->bRequest) {
  1018. case 0: /* Get the IEEE-1284 PNP String */
  1019. /* Only one printer interface is supported. */
  1020. if ((wIndex>>8) != PRINTER_INTERFACE)
  1021. break;
  1022. value = (pnp_string[0]<<8)|pnp_string[1];
  1023. memcpy(req->buf, pnp_string, value);
  1024. DBG(dev, "1284 PNP String: %x %s\n", value,
  1025. &pnp_string[2]);
  1026. break;
  1027. case 1: /* Get Port Status */
  1028. /* Only one printer interface is supported. */
  1029. if (wIndex != PRINTER_INTERFACE)
  1030. break;
  1031. *(u8 *)req->buf = dev->printer_status;
  1032. value = min(wLength, (u16) 1);
  1033. break;
  1034. case 2: /* Soft Reset */
  1035. /* Only one printer interface is supported. */
  1036. if (wIndex != PRINTER_INTERFACE)
  1037. break;
  1038. printer_soft_reset(dev);
  1039. value = 0;
  1040. break;
  1041. default:
  1042. goto unknown;
  1043. }
  1044. break;
  1045. default:
  1046. unknown:
  1047. VDBG(dev,
  1048. "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
  1049. ctrl->bRequestType, ctrl->bRequest,
  1050. wValue, wIndex, wLength);
  1051. break;
  1052. }
  1053. /* respond with data transfer before status phase? */
  1054. if (value >= 0) {
  1055. req->length = value;
  1056. req->zero = value < wLength;
  1057. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1058. if (value < 0) {
  1059. DBG(dev, "ep_queue --> %d\n", value);
  1060. req->status = 0;
  1061. printer_setup_complete(gadget->ep0, req);
  1062. }
  1063. }
  1064. /* host either stalls (value < 0) or reports success */
  1065. return value;
  1066. }
  1067. static void
  1068. printer_disconnect(struct usb_gadget *gadget)
  1069. {
  1070. struct printer_dev *dev = get_gadget_data(gadget);
  1071. unsigned long flags;
  1072. DBG(dev, "%s\n", __func__);
  1073. spin_lock_irqsave(&dev->lock, flags);
  1074. printer_reset_interface(dev);
  1075. spin_unlock_irqrestore(&dev->lock, flags);
  1076. }
  1077. static void
  1078. printer_unbind(struct usb_gadget *gadget)
  1079. {
  1080. struct printer_dev *dev = get_gadget_data(gadget);
  1081. struct usb_request *req;
  1082. DBG(dev, "%s\n", __func__);
  1083. /* Remove sysfs files */
  1084. device_destroy(usb_gadget_class, g_printer_devno);
  1085. /* Remove Character Device */
  1086. cdev_del(&dev->printer_cdev);
  1087. /* we must already have been disconnected ... no i/o may be active */
  1088. WARN_ON(!list_empty(&dev->tx_reqs_active));
  1089. WARN_ON(!list_empty(&dev->rx_reqs_active));
  1090. /* Free all memory for this driver. */
  1091. while (!list_empty(&dev->tx_reqs)) {
  1092. req = container_of(dev->tx_reqs.next, struct usb_request,
  1093. list);
  1094. list_del(&req->list);
  1095. printer_req_free(dev->in_ep, req);
  1096. }
  1097. if (dev->current_rx_req != NULL)
  1098. printer_req_free(dev->out_ep, dev->current_rx_req);
  1099. while (!list_empty(&dev->rx_reqs)) {
  1100. req = container_of(dev->rx_reqs.next,
  1101. struct usb_request, list);
  1102. list_del(&req->list);
  1103. printer_req_free(dev->out_ep, req);
  1104. }
  1105. while (!list_empty(&dev->rx_buffers)) {
  1106. req = container_of(dev->rx_buffers.next,
  1107. struct usb_request, list);
  1108. list_del(&req->list);
  1109. printer_req_free(dev->out_ep, req);
  1110. }
  1111. if (dev->req) {
  1112. printer_req_free(gadget->ep0, dev->req);
  1113. dev->req = NULL;
  1114. }
  1115. set_gadget_data(gadget, NULL);
  1116. }
  1117. static int __init
  1118. printer_bind(struct usb_gadget *gadget)
  1119. {
  1120. struct printer_dev *dev;
  1121. struct usb_ep *in_ep, *out_ep;
  1122. int status = -ENOMEM;
  1123. int gcnum;
  1124. size_t len;
  1125. u32 i;
  1126. struct usb_request *req;
  1127. dev = &usb_printer_gadget;
  1128. /* Setup the sysfs files for the printer gadget. */
  1129. dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
  1130. NULL, "g_printer");
  1131. if (IS_ERR(dev->pdev)) {
  1132. ERROR(dev, "Failed to create device: g_printer\n");
  1133. goto fail;
  1134. }
  1135. /*
  1136. * Register a character device as an interface to a user mode
  1137. * program that handles the printer specific functionality.
  1138. */
  1139. cdev_init(&dev->printer_cdev, &printer_io_operations);
  1140. dev->printer_cdev.owner = THIS_MODULE;
  1141. status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
  1142. if (status) {
  1143. ERROR(dev, "Failed to open char device\n");
  1144. goto fail;
  1145. }
  1146. gcnum = usb_gadget_controller_number(gadget);
  1147. if (gcnum >= 0) {
  1148. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  1149. } else {
  1150. dev_warn(&gadget->dev, "controller '%s' not recognized\n",
  1151. gadget->name);
  1152. /* unrecognized, but safe unless bulk is REALLY quirky */
  1153. device_desc.bcdDevice =
  1154. cpu_to_le16(0xFFFF);
  1155. }
  1156. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1157. init_utsname()->sysname, init_utsname()->release,
  1158. gadget->name);
  1159. device_desc.idVendor =
  1160. cpu_to_le16(PRINTER_VENDOR_NUM);
  1161. device_desc.idProduct =
  1162. cpu_to_le16(PRINTER_PRODUCT_NUM);
  1163. /* support optional vendor/distro customization */
  1164. if (idVendor) {
  1165. if (!idProduct) {
  1166. dev_err(&gadget->dev, "idVendor needs idProduct!\n");
  1167. return -ENODEV;
  1168. }
  1169. device_desc.idVendor = cpu_to_le16(idVendor);
  1170. device_desc.idProduct = cpu_to_le16(idProduct);
  1171. if (bcdDevice)
  1172. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1173. }
  1174. if (iManufacturer)
  1175. strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
  1176. if (iProduct)
  1177. strlcpy(product_desc, iProduct, sizeof product_desc);
  1178. if (iSerialNum)
  1179. strlcpy(serial_num, iSerialNum, sizeof serial_num);
  1180. if (iPNPstring)
  1181. strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
  1182. len = strlen(pnp_string);
  1183. pnp_string[0] = (len >> 8) & 0xFF;
  1184. pnp_string[1] = len & 0xFF;
  1185. /* all we really need is bulk IN/OUT */
  1186. usb_ep_autoconfig_reset(gadget);
  1187. in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
  1188. if (!in_ep) {
  1189. autoconf_fail:
  1190. dev_err(&gadget->dev, "can't autoconfigure on %s\n",
  1191. gadget->name);
  1192. return -ENODEV;
  1193. }
  1194. in_ep->driver_data = in_ep; /* claim */
  1195. out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
  1196. if (!out_ep)
  1197. goto autoconf_fail;
  1198. out_ep->driver_data = out_ep; /* claim */
  1199. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1200. /* assumes that all endpoints are dual-speed */
  1201. hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  1202. hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  1203. #endif /* DUALSPEED */
  1204. usb_gadget_set_selfpowered(gadget);
  1205. if (gadget->is_otg) {
  1206. otg_desc.bmAttributes |= USB_OTG_HNP,
  1207. config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1208. }
  1209. spin_lock_init(&dev->lock);
  1210. mutex_init(&dev->lock_printer_io);
  1211. INIT_LIST_HEAD(&dev->tx_reqs);
  1212. INIT_LIST_HEAD(&dev->tx_reqs_active);
  1213. INIT_LIST_HEAD(&dev->rx_reqs);
  1214. INIT_LIST_HEAD(&dev->rx_reqs_active);
  1215. INIT_LIST_HEAD(&dev->rx_buffers);
  1216. init_waitqueue_head(&dev->rx_wait);
  1217. init_waitqueue_head(&dev->tx_wait);
  1218. init_waitqueue_head(&dev->tx_flush_wait);
  1219. dev->config = 0;
  1220. dev->interface = -1;
  1221. dev->printer_cdev_open = 0;
  1222. dev->printer_status = PRINTER_NOT_ERROR;
  1223. dev->current_rx_req = NULL;
  1224. dev->current_rx_bytes = 0;
  1225. dev->current_rx_buf = NULL;
  1226. dev->in_ep = in_ep;
  1227. dev->out_ep = out_ep;
  1228. /* preallocate control message data and buffer */
  1229. dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
  1230. GFP_KERNEL);
  1231. if (!dev->req) {
  1232. status = -ENOMEM;
  1233. goto fail;
  1234. }
  1235. for (i = 0; i < QLEN; i++) {
  1236. req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
  1237. if (!req) {
  1238. while (!list_empty(&dev->tx_reqs)) {
  1239. req = container_of(dev->tx_reqs.next,
  1240. struct usb_request, list);
  1241. list_del(&req->list);
  1242. printer_req_free(dev->in_ep, req);
  1243. }
  1244. return -ENOMEM;
  1245. }
  1246. list_add(&req->list, &dev->tx_reqs);
  1247. }
  1248. for (i = 0; i < QLEN; i++) {
  1249. req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
  1250. if (!req) {
  1251. while (!list_empty(&dev->rx_reqs)) {
  1252. req = container_of(dev->rx_reqs.next,
  1253. struct usb_request, list);
  1254. list_del(&req->list);
  1255. printer_req_free(dev->out_ep, req);
  1256. }
  1257. return -ENOMEM;
  1258. }
  1259. list_add(&req->list, &dev->rx_reqs);
  1260. }
  1261. dev->req->complete = printer_setup_complete;
  1262. /* finish hookup to lower layer ... */
  1263. dev->gadget = gadget;
  1264. set_gadget_data(gadget, dev);
  1265. gadget->ep0->driver_data = dev;
  1266. INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  1267. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
  1268. in_ep->name);
  1269. return 0;
  1270. fail:
  1271. printer_unbind(gadget);
  1272. return status;
  1273. }
  1274. /*-------------------------------------------------------------------------*/
  1275. static struct usb_gadget_driver printer_driver = {
  1276. .speed = DEVSPEED,
  1277. .function = (char *) driver_desc,
  1278. .unbind = printer_unbind,
  1279. .setup = printer_setup,
  1280. .disconnect = printer_disconnect,
  1281. .driver = {
  1282. .name = (char *) shortname,
  1283. .owner = THIS_MODULE,
  1284. },
  1285. };
  1286. MODULE_DESCRIPTION(DRIVER_DESC);
  1287. MODULE_AUTHOR("Craig Nadler");
  1288. MODULE_LICENSE("GPL");
  1289. static int __init
  1290. init(void)
  1291. {
  1292. int status;
  1293. usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
  1294. if (IS_ERR(usb_gadget_class)) {
  1295. status = PTR_ERR(usb_gadget_class);
  1296. ERROR(dev, "unable to create usb_gadget class %d\n", status);
  1297. return status;
  1298. }
  1299. status = alloc_chrdev_region(&g_printer_devno, 0, 1,
  1300. "USB printer gadget");
  1301. if (status) {
  1302. ERROR(dev, "alloc_chrdev_region %d\n", status);
  1303. class_destroy(usb_gadget_class);
  1304. return status;
  1305. }
  1306. status = usb_gadget_probe_driver(&printer_driver, printer_bind);
  1307. if (status) {
  1308. class_destroy(usb_gadget_class);
  1309. unregister_chrdev_region(g_printer_devno, 1);
  1310. DBG(dev, "usb_gadget_probe_driver %x\n", status);
  1311. }
  1312. return status;
  1313. }
  1314. module_init(init);
  1315. static void __exit
  1316. cleanup(void)
  1317. {
  1318. int status;
  1319. mutex_lock(&usb_printer_gadget.lock_printer_io);
  1320. status = usb_gadget_unregister_driver(&printer_driver);
  1321. if (status)
  1322. ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
  1323. unregister_chrdev_region(g_printer_devno, 2);
  1324. class_destroy(usb_gadget_class);
  1325. mutex_unlock(&usb_printer_gadget.lock_printer_io);
  1326. }
  1327. module_exit(cleanup);