printer.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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. if (req->actual > 0) {
  323. list_add_tail(&req->list, &dev->rx_buffers);
  324. DBG(dev, "G_Printer : rx length %d\n", req->actual);
  325. } else {
  326. list_add(&req->list, &dev->rx_reqs);
  327. }
  328. break;
  329. /* software-driven interface shutdown */
  330. case -ECONNRESET: /* unlink */
  331. case -ESHUTDOWN: /* disconnect etc */
  332. VDBG(dev, "rx shutdown, code %d\n", status);
  333. list_add(&req->list, &dev->rx_reqs);
  334. break;
  335. /* for hardware automagic (such as pxa) */
  336. case -ECONNABORTED: /* endpoint reset */
  337. DBG(dev, "rx %s reset\n", ep->name);
  338. list_add(&req->list, &dev->rx_reqs);
  339. break;
  340. /* data overrun */
  341. case -EOVERFLOW:
  342. /* FALLTHROUGH */
  343. default:
  344. DBG(dev, "rx status %d\n", status);
  345. list_add(&req->list, &dev->rx_reqs);
  346. break;
  347. }
  348. wake_up_interruptible(&dev->rx_wait);
  349. spin_unlock_irqrestore(&dev->lock, flags);
  350. }
  351. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  352. {
  353. struct printer_dev *dev = ep->driver_data;
  354. switch (req->status) {
  355. default:
  356. VDBG(dev, "tx err %d\n", req->status);
  357. /* FALLTHROUGH */
  358. case -ECONNRESET: /* unlink */
  359. case -ESHUTDOWN: /* disconnect etc */
  360. break;
  361. case 0:
  362. break;
  363. }
  364. spin_lock(&dev->lock);
  365. /* Take the request struct off the active list and put it on the
  366. * free list.
  367. */
  368. list_del_init(&req->list);
  369. list_add(&req->list, &dev->tx_reqs);
  370. wake_up_interruptible(&dev->tx_wait);
  371. if (likely(list_empty(&dev->tx_reqs_active)))
  372. wake_up_interruptible(&dev->tx_flush_wait);
  373. spin_unlock(&dev->lock);
  374. }
  375. /*-------------------------------------------------------------------------*/
  376. static int
  377. printer_open(struct inode *inode, struct file *fd)
  378. {
  379. struct printer_dev *dev;
  380. unsigned long flags;
  381. int ret = -EBUSY;
  382. lock_kernel();
  383. dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
  384. spin_lock_irqsave(&dev->lock, flags);
  385. if (!dev->printer_cdev_open) {
  386. dev->printer_cdev_open = 1;
  387. fd->private_data = dev;
  388. ret = 0;
  389. /* Change the printer status to show that it's on-line. */
  390. dev->printer_status |= PRINTER_SELECTED;
  391. }
  392. spin_unlock_irqrestore(&dev->lock, flags);
  393. DBG(dev, "printer_open returned %x\n", ret);
  394. unlock_kernel();
  395. return ret;
  396. }
  397. static int
  398. printer_close(struct inode *inode, struct file *fd)
  399. {
  400. struct printer_dev *dev = fd->private_data;
  401. unsigned long flags;
  402. spin_lock_irqsave(&dev->lock, flags);
  403. dev->printer_cdev_open = 0;
  404. fd->private_data = NULL;
  405. /* Change printer status to show that the printer is off-line. */
  406. dev->printer_status &= ~PRINTER_SELECTED;
  407. spin_unlock_irqrestore(&dev->lock, flags);
  408. DBG(dev, "printer_close\n");
  409. return 0;
  410. }
  411. /* This function must be called with interrupts turned off. */
  412. static void
  413. setup_rx_reqs(struct printer_dev *dev)
  414. {
  415. struct usb_request *req;
  416. while (likely(!list_empty(&dev->rx_reqs))) {
  417. int error;
  418. req = container_of(dev->rx_reqs.next,
  419. struct usb_request, list);
  420. list_del_init(&req->list);
  421. /* The USB Host sends us whatever amount of data it wants to
  422. * so we always set the length field to the full USB_BUFSIZE.
  423. * If the amount of data is more than the read() caller asked
  424. * for it will be stored in the request buffer until it is
  425. * asked for by read().
  426. */
  427. req->length = USB_BUFSIZE;
  428. req->complete = rx_complete;
  429. error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
  430. if (error) {
  431. DBG(dev, "rx submit --> %d\n", error);
  432. list_add(&req->list, &dev->rx_reqs);
  433. break;
  434. } else {
  435. list_add(&req->list, &dev->rx_reqs_active);
  436. }
  437. }
  438. }
  439. static ssize_t
  440. printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  441. {
  442. struct printer_dev *dev = fd->private_data;
  443. unsigned long flags;
  444. size_t size;
  445. size_t bytes_copied;
  446. struct usb_request *req;
  447. /* This is a pointer to the current USB rx request. */
  448. struct usb_request *current_rx_req;
  449. /* This is the number of bytes in the current rx buffer. */
  450. size_t current_rx_bytes;
  451. /* This is a pointer to the current rx buffer. */
  452. u8 *current_rx_buf;
  453. if (len == 0)
  454. return -EINVAL;
  455. DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
  456. spin_lock(&dev->lock_printer_io);
  457. spin_lock_irqsave(&dev->lock, flags);
  458. /* We will use this flag later to check if a printer reset happened
  459. * after we turn interrupts back on.
  460. */
  461. dev->reset_printer = 0;
  462. setup_rx_reqs(dev);
  463. bytes_copied = 0;
  464. current_rx_req = dev->current_rx_req;
  465. current_rx_bytes = dev->current_rx_bytes;
  466. current_rx_buf = dev->current_rx_buf;
  467. dev->current_rx_req = NULL;
  468. dev->current_rx_bytes = 0;
  469. dev->current_rx_buf = NULL;
  470. /* Check if there is any data in the read buffers. Please note that
  471. * current_rx_bytes is the number of bytes in the current rx buffer.
  472. * If it is zero then check if there are any other rx_buffers that
  473. * are on the completed list. We are only out of data if all rx
  474. * buffers are empty.
  475. */
  476. if ((current_rx_bytes == 0) &&
  477. (likely(list_empty(&dev->rx_buffers)))) {
  478. /* Turn interrupts back on before sleeping. */
  479. spin_unlock_irqrestore(&dev->lock, flags);
  480. /*
  481. * If no data is available check if this is a NON-Blocking
  482. * call or not.
  483. */
  484. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  485. spin_unlock(&dev->lock_printer_io);
  486. return -EAGAIN;
  487. }
  488. /* Sleep until data is available */
  489. wait_event_interruptible(dev->rx_wait,
  490. (likely(!list_empty(&dev->rx_buffers))));
  491. spin_lock_irqsave(&dev->lock, flags);
  492. }
  493. /* We have data to return then copy it to the caller's buffer.*/
  494. while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
  495. && len) {
  496. if (current_rx_bytes == 0) {
  497. req = container_of(dev->rx_buffers.next,
  498. struct usb_request, list);
  499. list_del_init(&req->list);
  500. if (req->actual && req->buf) {
  501. current_rx_req = req;
  502. current_rx_bytes = req->actual;
  503. current_rx_buf = req->buf;
  504. } else {
  505. list_add(&req->list, &dev->rx_reqs);
  506. continue;
  507. }
  508. }
  509. /* Don't leave irqs off while doing memory copies */
  510. spin_unlock_irqrestore(&dev->lock, flags);
  511. if (len > current_rx_bytes)
  512. size = current_rx_bytes;
  513. else
  514. size = len;
  515. size -= copy_to_user(buf, current_rx_buf, size);
  516. bytes_copied += size;
  517. len -= size;
  518. buf += size;
  519. spin_lock_irqsave(&dev->lock, flags);
  520. /* We've disconnected or reset so return. */
  521. if (dev->reset_printer) {
  522. list_add(&current_rx_req->list, &dev->rx_reqs);
  523. spin_unlock_irqrestore(&dev->lock, flags);
  524. spin_unlock(&dev->lock_printer_io);
  525. return -EAGAIN;
  526. }
  527. /* If we not returning all the data left in this RX request
  528. * buffer then adjust the amount of data left in the buffer.
  529. * Othewise if we are done with this RX request buffer then
  530. * requeue it to get any incoming data from the USB host.
  531. */
  532. if (size < current_rx_bytes) {
  533. current_rx_bytes -= size;
  534. current_rx_buf += size;
  535. } else {
  536. list_add(&current_rx_req->list, &dev->rx_reqs);
  537. current_rx_bytes = 0;
  538. current_rx_buf = NULL;
  539. current_rx_req = NULL;
  540. }
  541. }
  542. dev->current_rx_req = current_rx_req;
  543. dev->current_rx_bytes = current_rx_bytes;
  544. dev->current_rx_buf = current_rx_buf;
  545. spin_unlock_irqrestore(&dev->lock, flags);
  546. spin_unlock(&dev->lock_printer_io);
  547. DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
  548. if (bytes_copied)
  549. return bytes_copied;
  550. else
  551. return -EAGAIN;
  552. }
  553. static ssize_t
  554. printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  555. {
  556. struct printer_dev *dev = fd->private_data;
  557. unsigned long flags;
  558. size_t size; /* Amount of data in a TX request. */
  559. size_t bytes_copied = 0;
  560. struct usb_request *req;
  561. DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
  562. if (len == 0)
  563. return -EINVAL;
  564. spin_lock(&dev->lock_printer_io);
  565. spin_lock_irqsave(&dev->lock, flags);
  566. /* Check if a printer reset happens while we have interrupts on */
  567. dev->reset_printer = 0;
  568. /* Check if there is any available write buffers */
  569. if (likely(list_empty(&dev->tx_reqs))) {
  570. /* Turn interrupts back on before sleeping. */
  571. spin_unlock_irqrestore(&dev->lock, flags);
  572. /*
  573. * If write buffers are available check if this is
  574. * a NON-Blocking call or not.
  575. */
  576. if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
  577. spin_unlock(&dev->lock_printer_io);
  578. return -EAGAIN;
  579. }
  580. /* Sleep until a write buffer is available */
  581. wait_event_interruptible(dev->tx_wait,
  582. (likely(!list_empty(&dev->tx_reqs))));
  583. spin_lock_irqsave(&dev->lock, flags);
  584. }
  585. while (likely(!list_empty(&dev->tx_reqs)) && len) {
  586. if (len > USB_BUFSIZE)
  587. size = USB_BUFSIZE;
  588. else
  589. size = len;
  590. req = container_of(dev->tx_reqs.next, struct usb_request,
  591. list);
  592. list_del_init(&req->list);
  593. req->complete = tx_complete;
  594. req->length = size;
  595. /* Check if we need to send a zero length packet. */
  596. if (len > size)
  597. /* They will be more TX requests so no yet. */
  598. req->zero = 0;
  599. else
  600. /* If the data amount is not a multple of the
  601. * maxpacket size then send a zero length packet.
  602. */
  603. req->zero = ((len % dev->in_ep->maxpacket) == 0);
  604. /* Don't leave irqs off while doing memory copies */
  605. spin_unlock_irqrestore(&dev->lock, flags);
  606. if (copy_from_user(req->buf, buf, size)) {
  607. list_add(&req->list, &dev->tx_reqs);
  608. spin_unlock(&dev->lock_printer_io);
  609. return bytes_copied;
  610. }
  611. bytes_copied += size;
  612. len -= size;
  613. buf += size;
  614. spin_lock_irqsave(&dev->lock, flags);
  615. /* We've disconnected or reset so free the req and buffer */
  616. if (dev->reset_printer) {
  617. list_add(&req->list, &dev->tx_reqs);
  618. spin_unlock_irqrestore(&dev->lock, flags);
  619. spin_unlock(&dev->lock_printer_io);
  620. return -EAGAIN;
  621. }
  622. if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
  623. list_add(&req->list, &dev->tx_reqs);
  624. spin_unlock_irqrestore(&dev->lock, flags);
  625. spin_unlock(&dev->lock_printer_io);
  626. return -EAGAIN;
  627. }
  628. list_add(&req->list, &dev->tx_reqs_active);
  629. }
  630. spin_unlock_irqrestore(&dev->lock, flags);
  631. spin_unlock(&dev->lock_printer_io);
  632. DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
  633. if (bytes_copied) {
  634. return bytes_copied;
  635. } else {
  636. return -EAGAIN;
  637. }
  638. }
  639. static int
  640. printer_fsync(struct file *fd, struct dentry *dentry, int datasync)
  641. {
  642. struct printer_dev *dev = fd->private_data;
  643. unsigned long flags;
  644. int tx_list_empty;
  645. spin_lock_irqsave(&dev->lock, flags);
  646. tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
  647. spin_unlock_irqrestore(&dev->lock, flags);
  648. if (!tx_list_empty) {
  649. /* Sleep until all data has been sent */
  650. wait_event_interruptible(dev->tx_flush_wait,
  651. (likely(list_empty(&dev->tx_reqs_active))));
  652. }
  653. return 0;
  654. }
  655. static unsigned int
  656. printer_poll(struct file *fd, poll_table *wait)
  657. {
  658. struct printer_dev *dev = fd->private_data;
  659. unsigned long flags;
  660. int status = 0;
  661. spin_lock(&dev->lock_printer_io);
  662. spin_lock_irqsave(&dev->lock, flags);
  663. setup_rx_reqs(dev);
  664. spin_unlock_irqrestore(&dev->lock, flags);
  665. spin_unlock(&dev->lock_printer_io);
  666. poll_wait(fd, &dev->rx_wait, wait);
  667. poll_wait(fd, &dev->tx_wait, wait);
  668. spin_lock_irqsave(&dev->lock, flags);
  669. if (likely(!list_empty(&dev->tx_reqs)))
  670. status |= POLLOUT | POLLWRNORM;
  671. if (likely(dev->current_rx_bytes) ||
  672. likely(!list_empty(&dev->rx_buffers)))
  673. status |= POLLIN | POLLRDNORM;
  674. spin_unlock_irqrestore(&dev->lock, flags);
  675. return status;
  676. }
  677. static int
  678. printer_ioctl(struct inode *inode, struct file *fd, unsigned int code,
  679. unsigned long arg)
  680. {
  681. struct printer_dev *dev = fd->private_data;
  682. unsigned long flags;
  683. int status = 0;
  684. DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
  685. /* handle ioctls */
  686. spin_lock_irqsave(&dev->lock, flags);
  687. switch (code) {
  688. case GADGET_GET_PRINTER_STATUS:
  689. status = (int)dev->printer_status;
  690. break;
  691. case GADGET_SET_PRINTER_STATUS:
  692. dev->printer_status = (u8)arg;
  693. break;
  694. default:
  695. /* could not handle ioctl */
  696. DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
  697. code);
  698. status = -ENOTTY;
  699. }
  700. spin_unlock_irqrestore(&dev->lock, flags);
  701. return status;
  702. }
  703. /* used after endpoint configuration */
  704. static struct file_operations printer_io_operations = {
  705. .owner = THIS_MODULE,
  706. .open = printer_open,
  707. .read = printer_read,
  708. .write = printer_write,
  709. .fsync = printer_fsync,
  710. .poll = printer_poll,
  711. .ioctl = printer_ioctl,
  712. .release = printer_close
  713. };
  714. /*-------------------------------------------------------------------------*/
  715. static int
  716. set_printer_interface(struct printer_dev *dev)
  717. {
  718. int result = 0;
  719. dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
  720. dev->in_ep->driver_data = dev;
  721. dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
  722. dev->out_ep->driver_data = dev;
  723. result = usb_ep_enable(dev->in_ep, dev->in);
  724. if (result != 0) {
  725. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  726. goto done;
  727. }
  728. result = usb_ep_enable(dev->out_ep, dev->out);
  729. if (result != 0) {
  730. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  731. goto done;
  732. }
  733. done:
  734. /* on error, disable any endpoints */
  735. if (result != 0) {
  736. (void) usb_ep_disable(dev->in_ep);
  737. (void) usb_ep_disable(dev->out_ep);
  738. dev->in = NULL;
  739. dev->out = NULL;
  740. }
  741. /* caller is responsible for cleanup on error */
  742. return result;
  743. }
  744. static void printer_reset_interface(struct printer_dev *dev)
  745. {
  746. if (dev->interface < 0)
  747. return;
  748. DBG(dev, "%s\n", __func__);
  749. if (dev->in)
  750. usb_ep_disable(dev->in_ep);
  751. if (dev->out)
  752. usb_ep_disable(dev->out_ep);
  753. dev->interface = -1;
  754. }
  755. /* change our operational config. must agree with the code
  756. * that returns config descriptors, and altsetting code.
  757. */
  758. static int
  759. printer_set_config(struct printer_dev *dev, unsigned number)
  760. {
  761. int result = 0;
  762. struct usb_gadget *gadget = dev->gadget;
  763. if (gadget_is_sa1100(gadget) && dev->config) {
  764. /* tx fifo is full, but we can't clear it...*/
  765. INFO(dev, "can't change configurations\n");
  766. return -ESPIPE;
  767. }
  768. switch (number) {
  769. case DEV_CONFIG_VALUE:
  770. result = 0;
  771. break;
  772. default:
  773. result = -EINVAL;
  774. /* FALL THROUGH */
  775. case 0:
  776. break;
  777. }
  778. if (result) {
  779. usb_gadget_vbus_draw(dev->gadget,
  780. dev->gadget->is_otg ? 8 : 100);
  781. } else {
  782. char *speed;
  783. unsigned power;
  784. power = 2 * config_desc.bMaxPower;
  785. usb_gadget_vbus_draw(dev->gadget, power);
  786. switch (gadget->speed) {
  787. case USB_SPEED_FULL: speed = "full"; break;
  788. #ifdef CONFIG_USB_GADGET_DUALSPEED
  789. case USB_SPEED_HIGH: speed = "high"; break;
  790. #endif
  791. default: speed = "?"; break;
  792. }
  793. dev->config = number;
  794. INFO(dev, "%s speed config #%d: %d mA, %s\n",
  795. speed, number, power, driver_desc);
  796. }
  797. return result;
  798. }
  799. static int
  800. config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
  801. int is_otg)
  802. {
  803. int len;
  804. const struct usb_descriptor_header **function;
  805. #ifdef CONFIG_USB_GADGET_DUALSPEED
  806. int hs = (speed == USB_SPEED_HIGH);
  807. if (type == USB_DT_OTHER_SPEED_CONFIG)
  808. hs = !hs;
  809. if (hs) {
  810. function = hs_printer_function;
  811. } else {
  812. function = fs_printer_function;
  813. }
  814. #else
  815. function = fs_printer_function;
  816. #endif
  817. if (index >= device_desc.bNumConfigurations)
  818. return -EINVAL;
  819. /* for now, don't advertise srp-only devices */
  820. if (!is_otg)
  821. function++;
  822. len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
  823. function);
  824. if (len < 0)
  825. return len;
  826. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  827. return len;
  828. }
  829. /* Change our operational Interface. */
  830. static int
  831. set_interface(struct printer_dev *dev, unsigned number)
  832. {
  833. int result = 0;
  834. if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
  835. /* tx fifo is full, but we can't clear it...*/
  836. INFO(dev, "can't change interfaces\n");
  837. return -ESPIPE;
  838. }
  839. /* Free the current interface */
  840. switch (dev->interface) {
  841. case PRINTER_INTERFACE:
  842. printer_reset_interface(dev);
  843. break;
  844. }
  845. switch (number) {
  846. case PRINTER_INTERFACE:
  847. result = set_printer_interface(dev);
  848. if (result) {
  849. printer_reset_interface(dev);
  850. } else {
  851. dev->interface = PRINTER_INTERFACE;
  852. }
  853. break;
  854. default:
  855. result = -EINVAL;
  856. /* FALL THROUGH */
  857. }
  858. if (!result)
  859. INFO(dev, "Using interface %x\n", number);
  860. return result;
  861. }
  862. static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
  863. {
  864. if (req->status || req->actual != req->length)
  865. DBG((struct printer_dev *) ep->driver_data,
  866. "setup complete --> %d, %d/%d\n",
  867. req->status, req->actual, req->length);
  868. }
  869. static void printer_soft_reset(struct printer_dev *dev)
  870. {
  871. struct usb_request *req;
  872. INFO(dev, "Received Printer Reset Request\n");
  873. if (usb_ep_disable(dev->in_ep))
  874. DBG(dev, "Failed to disable USB in_ep\n");
  875. if (usb_ep_disable(dev->out_ep))
  876. DBG(dev, "Failed to disable USB out_ep\n");
  877. if (dev->current_rx_req != NULL) {
  878. list_add(&dev->current_rx_req->list, &dev->rx_reqs);
  879. dev->current_rx_req = NULL;
  880. }
  881. dev->current_rx_bytes = 0;
  882. dev->current_rx_buf = NULL;
  883. dev->reset_printer = 1;
  884. while (likely(!(list_empty(&dev->rx_buffers)))) {
  885. req = container_of(dev->rx_buffers.next, struct usb_request,
  886. list);
  887. list_del_init(&req->list);
  888. list_add(&req->list, &dev->rx_reqs);
  889. }
  890. while (likely(!(list_empty(&dev->rx_reqs_active)))) {
  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->tx_reqs_active)))) {
  897. req = container_of(dev->tx_reqs_active.next,
  898. struct usb_request, list);
  899. list_del_init(&req->list);
  900. list_add(&req->list, &dev->tx_reqs);
  901. }
  902. if (usb_ep_enable(dev->in_ep, dev->in))
  903. DBG(dev, "Failed to enable USB in_ep\n");
  904. if (usb_ep_enable(dev->out_ep, dev->out))
  905. DBG(dev, "Failed to enable USB out_ep\n");
  906. wake_up_interruptible(&dev->rx_wait);
  907. wake_up_interruptible(&dev->tx_wait);
  908. wake_up_interruptible(&dev->tx_flush_wait);
  909. }
  910. /*-------------------------------------------------------------------------*/
  911. /*
  912. * The setup() callback implements all the ep0 functionality that's not
  913. * handled lower down.
  914. */
  915. static int
  916. printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  917. {
  918. struct printer_dev *dev = get_gadget_data(gadget);
  919. struct usb_request *req = dev->req;
  920. int value = -EOPNOTSUPP;
  921. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  922. u16 wValue = le16_to_cpu(ctrl->wValue);
  923. u16 wLength = le16_to_cpu(ctrl->wLength);
  924. DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
  925. ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
  926. req->complete = printer_setup_complete;
  927. switch (ctrl->bRequestType&USB_TYPE_MASK) {
  928. case USB_TYPE_STANDARD:
  929. switch (ctrl->bRequest) {
  930. case USB_REQ_GET_DESCRIPTOR:
  931. if (ctrl->bRequestType != USB_DIR_IN)
  932. break;
  933. switch (wValue >> 8) {
  934. case USB_DT_DEVICE:
  935. value = min(wLength, (u16) sizeof device_desc);
  936. memcpy(req->buf, &device_desc, value);
  937. break;
  938. #ifdef CONFIG_USB_GADGET_DUALSPEED
  939. case USB_DT_DEVICE_QUALIFIER:
  940. if (!gadget->is_dualspeed)
  941. break;
  942. value = min(wLength,
  943. (u16) sizeof dev_qualifier);
  944. memcpy(req->buf, &dev_qualifier, value);
  945. break;
  946. case USB_DT_OTHER_SPEED_CONFIG:
  947. if (!gadget->is_dualspeed)
  948. break;
  949. /* FALLTHROUGH */
  950. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  951. case USB_DT_CONFIG:
  952. value = config_buf(gadget->speed, req->buf,
  953. wValue >> 8,
  954. wValue & 0xff,
  955. gadget->is_otg);
  956. if (value >= 0)
  957. value = min(wLength, (u16) value);
  958. break;
  959. case USB_DT_STRING:
  960. value = usb_gadget_get_string(&stringtab,
  961. wValue & 0xff, req->buf);
  962. if (value >= 0)
  963. value = min(wLength, (u16) value);
  964. break;
  965. }
  966. break;
  967. case USB_REQ_SET_CONFIGURATION:
  968. if (ctrl->bRequestType != 0)
  969. break;
  970. if (gadget->a_hnp_support)
  971. DBG(dev, "HNP available\n");
  972. else if (gadget->a_alt_hnp_support)
  973. DBG(dev, "HNP needs a different root port\n");
  974. value = printer_set_config(dev, wValue);
  975. break;
  976. case USB_REQ_GET_CONFIGURATION:
  977. if (ctrl->bRequestType != USB_DIR_IN)
  978. break;
  979. *(u8 *)req->buf = dev->config;
  980. value = min(wLength, (u16) 1);
  981. break;
  982. case USB_REQ_SET_INTERFACE:
  983. if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
  984. !dev->config)
  985. break;
  986. value = set_interface(dev, PRINTER_INTERFACE);
  987. break;
  988. case USB_REQ_GET_INTERFACE:
  989. if (ctrl->bRequestType !=
  990. (USB_DIR_IN|USB_RECIP_INTERFACE)
  991. || !dev->config)
  992. break;
  993. *(u8 *)req->buf = dev->interface;
  994. value = min(wLength, (u16) 1);
  995. break;
  996. default:
  997. goto unknown;
  998. }
  999. break;
  1000. case USB_TYPE_CLASS:
  1001. switch (ctrl->bRequest) {
  1002. case 0: /* Get the IEEE-1284 PNP String */
  1003. /* Only one printer interface is supported. */
  1004. if ((wIndex>>8) != PRINTER_INTERFACE)
  1005. break;
  1006. value = (pnp_string[0]<<8)|pnp_string[1];
  1007. memcpy(req->buf, pnp_string, value);
  1008. DBG(dev, "1284 PNP String: %x %s\n", value,
  1009. &pnp_string[2]);
  1010. break;
  1011. case 1: /* Get Port Status */
  1012. /* Only one printer interface is supported. */
  1013. if (wIndex != PRINTER_INTERFACE)
  1014. break;
  1015. *(u8 *)req->buf = dev->printer_status;
  1016. value = min(wLength, (u16) 1);
  1017. break;
  1018. case 2: /* Soft Reset */
  1019. /* Only one printer interface is supported. */
  1020. if (wIndex != PRINTER_INTERFACE)
  1021. break;
  1022. printer_soft_reset(dev);
  1023. value = 0;
  1024. break;
  1025. default:
  1026. goto unknown;
  1027. }
  1028. break;
  1029. default:
  1030. unknown:
  1031. VDBG(dev,
  1032. "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
  1033. ctrl->bRequestType, ctrl->bRequest,
  1034. wValue, wIndex, wLength);
  1035. break;
  1036. }
  1037. /* respond with data transfer before status phase? */
  1038. if (value >= 0) {
  1039. req->length = value;
  1040. req->zero = value < wLength
  1041. && (value % gadget->ep0->maxpacket) == 0;
  1042. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1043. if (value < 0) {
  1044. DBG(dev, "ep_queue --> %d\n", value);
  1045. req->status = 0;
  1046. printer_setup_complete(gadget->ep0, req);
  1047. }
  1048. }
  1049. /* host either stalls (value < 0) or reports success */
  1050. return value;
  1051. }
  1052. static void
  1053. printer_disconnect(struct usb_gadget *gadget)
  1054. {
  1055. struct printer_dev *dev = get_gadget_data(gadget);
  1056. unsigned long flags;
  1057. DBG(dev, "%s\n", __func__);
  1058. spin_lock_irqsave(&dev->lock, flags);
  1059. printer_reset_interface(dev);
  1060. spin_unlock_irqrestore(&dev->lock, flags);
  1061. }
  1062. static void
  1063. printer_unbind(struct usb_gadget *gadget)
  1064. {
  1065. struct printer_dev *dev = get_gadget_data(gadget);
  1066. struct usb_request *req;
  1067. DBG(dev, "%s\n", __func__);
  1068. /* Remove sysfs files */
  1069. device_destroy(usb_gadget_class, g_printer_devno);
  1070. /* Remove Character Device */
  1071. cdev_del(&dev->printer_cdev);
  1072. /* we must already have been disconnected ... no i/o may be active */
  1073. WARN_ON(!list_empty(&dev->tx_reqs_active));
  1074. WARN_ON(!list_empty(&dev->rx_reqs_active));
  1075. /* Free all memory for this driver. */
  1076. while (!list_empty(&dev->tx_reqs)) {
  1077. req = container_of(dev->tx_reqs.next, struct usb_request,
  1078. list);
  1079. list_del(&req->list);
  1080. printer_req_free(dev->in_ep, req);
  1081. }
  1082. if (dev->current_rx_req != NULL)
  1083. printer_req_free(dev->out_ep, dev->current_rx_req);
  1084. while (!list_empty(&dev->rx_reqs)) {
  1085. req = container_of(dev->rx_reqs.next,
  1086. struct usb_request, list);
  1087. list_del(&req->list);
  1088. printer_req_free(dev->out_ep, req);
  1089. }
  1090. while (!list_empty(&dev->rx_buffers)) {
  1091. req = container_of(dev->rx_buffers.next,
  1092. struct usb_request, list);
  1093. list_del(&req->list);
  1094. printer_req_free(dev->out_ep, req);
  1095. }
  1096. if (dev->req) {
  1097. printer_req_free(gadget->ep0, dev->req);
  1098. dev->req = NULL;
  1099. }
  1100. set_gadget_data(gadget, NULL);
  1101. }
  1102. static int __init
  1103. printer_bind(struct usb_gadget *gadget)
  1104. {
  1105. struct printer_dev *dev;
  1106. struct usb_ep *in_ep, *out_ep;
  1107. int status = -ENOMEM;
  1108. int gcnum;
  1109. size_t len;
  1110. u32 i;
  1111. struct usb_request *req;
  1112. dev = &usb_printer_gadget;
  1113. /* Setup the sysfs files for the printer gadget. */
  1114. dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
  1115. "g_printer");
  1116. if (IS_ERR(dev->pdev)) {
  1117. ERROR(dev, "Failed to create device: g_printer\n");
  1118. goto fail;
  1119. }
  1120. /*
  1121. * Register a character device as an interface to a user mode
  1122. * program that handles the printer specific functionality.
  1123. */
  1124. cdev_init(&dev->printer_cdev, &printer_io_operations);
  1125. dev->printer_cdev.owner = THIS_MODULE;
  1126. status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
  1127. if (status) {
  1128. ERROR(dev, "Failed to open char device\n");
  1129. goto fail;
  1130. }
  1131. if (gadget_is_sa1100(gadget)) {
  1132. /* hardware can't write zero length packets. */
  1133. ERROR(dev, "SA1100 controller is unsupport by this driver\n");
  1134. goto fail;
  1135. }
  1136. gcnum = usb_gadget_controller_number(gadget);
  1137. if (gcnum >= 0) {
  1138. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  1139. } else {
  1140. dev_warn(&gadget->dev, "controller '%s' not recognized\n",
  1141. gadget->name);
  1142. /* unrecognized, but safe unless bulk is REALLY quirky */
  1143. device_desc.bcdDevice =
  1144. __constant_cpu_to_le16(0xFFFF);
  1145. }
  1146. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1147. init_utsname()->sysname, init_utsname()->release,
  1148. gadget->name);
  1149. device_desc.idVendor =
  1150. __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
  1151. device_desc.idProduct =
  1152. __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
  1153. /* support optional vendor/distro customization */
  1154. if (idVendor) {
  1155. if (!idProduct) {
  1156. dev_err(&gadget->dev, "idVendor needs idProduct!\n");
  1157. return -ENODEV;
  1158. }
  1159. device_desc.idVendor = cpu_to_le16(idVendor);
  1160. device_desc.idProduct = cpu_to_le16(idProduct);
  1161. if (bcdDevice)
  1162. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1163. }
  1164. if (iManufacturer)
  1165. strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
  1166. if (iProduct)
  1167. strlcpy(product_desc, iProduct, sizeof product_desc);
  1168. if (iSerialNum)
  1169. strlcpy(serial_num, iSerialNum, sizeof serial_num);
  1170. if (iPNPstring)
  1171. strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
  1172. len = strlen(pnp_string);
  1173. pnp_string[0] = (len >> 8) & 0xFF;
  1174. pnp_string[1] = len & 0xFF;
  1175. /* all we really need is bulk IN/OUT */
  1176. usb_ep_autoconfig_reset(gadget);
  1177. in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
  1178. if (!in_ep) {
  1179. autoconf_fail:
  1180. dev_err(&gadget->dev, "can't autoconfigure on %s\n",
  1181. gadget->name);
  1182. return -ENODEV;
  1183. }
  1184. in_ep->driver_data = in_ep; /* claim */
  1185. out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
  1186. if (!out_ep)
  1187. goto autoconf_fail;
  1188. out_ep->driver_data = out_ep; /* claim */
  1189. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1190. /* assumes ep0 uses the same value for both speeds ... */
  1191. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1192. /* and that all endpoints are dual-speed */
  1193. hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  1194. hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  1195. #endif /* DUALSPEED */
  1196. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1197. usb_gadget_set_selfpowered(gadget);
  1198. if (gadget->is_otg) {
  1199. otg_desc.bmAttributes |= USB_OTG_HNP,
  1200. config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1201. config_desc.bMaxPower = 4;
  1202. }
  1203. spin_lock_init(&dev->lock);
  1204. spin_lock_init(&dev->lock_printer_io);
  1205. INIT_LIST_HEAD(&dev->tx_reqs);
  1206. INIT_LIST_HEAD(&dev->tx_reqs_active);
  1207. INIT_LIST_HEAD(&dev->rx_reqs);
  1208. INIT_LIST_HEAD(&dev->rx_reqs_active);
  1209. INIT_LIST_HEAD(&dev->rx_buffers);
  1210. init_waitqueue_head(&dev->rx_wait);
  1211. init_waitqueue_head(&dev->tx_wait);
  1212. init_waitqueue_head(&dev->tx_flush_wait);
  1213. dev->config = 0;
  1214. dev->interface = -1;
  1215. dev->printer_cdev_open = 0;
  1216. dev->printer_status = PRINTER_NOT_ERROR;
  1217. dev->current_rx_req = NULL;
  1218. dev->current_rx_bytes = 0;
  1219. dev->current_rx_buf = NULL;
  1220. dev->in_ep = in_ep;
  1221. dev->out_ep = out_ep;
  1222. /* preallocate control message data and buffer */
  1223. dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
  1224. GFP_KERNEL);
  1225. if (!dev->req) {
  1226. status = -ENOMEM;
  1227. goto fail;
  1228. }
  1229. for (i = 0; i < QLEN; i++) {
  1230. req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
  1231. if (!req) {
  1232. while (!list_empty(&dev->tx_reqs)) {
  1233. req = container_of(dev->tx_reqs.next,
  1234. struct usb_request, list);
  1235. list_del(&req->list);
  1236. printer_req_free(dev->in_ep, req);
  1237. }
  1238. return -ENOMEM;
  1239. }
  1240. list_add(&req->list, &dev->tx_reqs);
  1241. }
  1242. for (i = 0; i < QLEN; i++) {
  1243. req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
  1244. if (!req) {
  1245. while (!list_empty(&dev->rx_reqs)) {
  1246. req = container_of(dev->rx_reqs.next,
  1247. struct usb_request, list);
  1248. list_del(&req->list);
  1249. printer_req_free(dev->out_ep, req);
  1250. }
  1251. return -ENOMEM;
  1252. }
  1253. list_add(&req->list, &dev->rx_reqs);
  1254. }
  1255. dev->req->complete = printer_setup_complete;
  1256. /* finish hookup to lower layer ... */
  1257. dev->gadget = gadget;
  1258. set_gadget_data(gadget, dev);
  1259. gadget->ep0->driver_data = dev;
  1260. INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  1261. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
  1262. in_ep->name);
  1263. return 0;
  1264. fail:
  1265. printer_unbind(gadget);
  1266. return status;
  1267. }
  1268. /*-------------------------------------------------------------------------*/
  1269. static struct usb_gadget_driver printer_driver = {
  1270. .speed = DEVSPEED,
  1271. .function = (char *) driver_desc,
  1272. .bind = printer_bind,
  1273. .unbind = printer_unbind,
  1274. .setup = printer_setup,
  1275. .disconnect = printer_disconnect,
  1276. .driver = {
  1277. .name = (char *) shortname,
  1278. .owner = THIS_MODULE,
  1279. },
  1280. };
  1281. MODULE_DESCRIPTION(DRIVER_DESC);
  1282. MODULE_AUTHOR("Craig Nadler");
  1283. MODULE_LICENSE("GPL");
  1284. static int __init
  1285. init(void)
  1286. {
  1287. int status;
  1288. usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
  1289. if (IS_ERR(usb_gadget_class)) {
  1290. status = PTR_ERR(usb_gadget_class);
  1291. ERROR(dev, "unable to create usb_gadget class %d\n", status);
  1292. return status;
  1293. }
  1294. status = alloc_chrdev_region(&g_printer_devno, 0, 1,
  1295. "USB printer gadget");
  1296. if (status) {
  1297. ERROR(dev, "alloc_chrdev_region %d\n", status);
  1298. class_destroy(usb_gadget_class);
  1299. return status;
  1300. }
  1301. status = usb_gadget_register_driver(&printer_driver);
  1302. if (status) {
  1303. class_destroy(usb_gadget_class);
  1304. unregister_chrdev_region(g_printer_devno, 1);
  1305. DBG(dev, "usb_gadget_register_driver %x\n", status);
  1306. }
  1307. return status;
  1308. }
  1309. module_init(init);
  1310. static void __exit
  1311. cleanup(void)
  1312. {
  1313. int status;
  1314. spin_lock(&usb_printer_gadget.lock_printer_io);
  1315. class_destroy(usb_gadget_class);
  1316. unregister_chrdev_region(g_printer_devno, 2);
  1317. status = usb_gadget_unregister_driver(&printer_driver);
  1318. if (status)
  1319. ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
  1320. spin_unlock(&usb_printer_gadget.lock_printer_io);
  1321. }
  1322. module_exit(cleanup);