printer.c 40 KB

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