printer.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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 WARNING(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 long
  678. printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
  679. {
  680. struct printer_dev *dev = fd->private_data;
  681. unsigned long flags;
  682. int status = 0;
  683. DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
  684. /* handle ioctls */
  685. spin_lock_irqsave(&dev->lock, flags);
  686. switch (code) {
  687. case GADGET_GET_PRINTER_STATUS:
  688. status = (int)dev->printer_status;
  689. break;
  690. case GADGET_SET_PRINTER_STATUS:
  691. dev->printer_status = (u8)arg;
  692. break;
  693. default:
  694. /* could not handle ioctl */
  695. DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
  696. code);
  697. status = -ENOTTY;
  698. }
  699. spin_unlock_irqrestore(&dev->lock, flags);
  700. return status;
  701. }
  702. /* used after endpoint configuration */
  703. static struct file_operations printer_io_operations = {
  704. .owner = THIS_MODULE,
  705. .open = printer_open,
  706. .read = printer_read,
  707. .write = printer_write,
  708. .fsync = printer_fsync,
  709. .poll = printer_poll,
  710. .unlocked_ioctl = printer_ioctl,
  711. .release = printer_close
  712. };
  713. /*-------------------------------------------------------------------------*/
  714. static int
  715. set_printer_interface(struct printer_dev *dev)
  716. {
  717. int result = 0;
  718. dev->in = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
  719. dev->in_ep->driver_data = dev;
  720. dev->out = ep_desc(dev->gadget, &hs_ep_out_desc, &fs_ep_out_desc);
  721. dev->out_ep->driver_data = dev;
  722. result = usb_ep_enable(dev->in_ep, dev->in);
  723. if (result != 0) {
  724. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  725. goto done;
  726. }
  727. result = usb_ep_enable(dev->out_ep, dev->out);
  728. if (result != 0) {
  729. DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
  730. goto done;
  731. }
  732. done:
  733. /* on error, disable any endpoints */
  734. if (result != 0) {
  735. (void) usb_ep_disable(dev->in_ep);
  736. (void) usb_ep_disable(dev->out_ep);
  737. dev->in = NULL;
  738. dev->out = NULL;
  739. }
  740. /* caller is responsible for cleanup on error */
  741. return result;
  742. }
  743. static void printer_reset_interface(struct printer_dev *dev)
  744. {
  745. if (dev->interface < 0)
  746. return;
  747. DBG(dev, "%s\n", __func__);
  748. if (dev->in)
  749. usb_ep_disable(dev->in_ep);
  750. if (dev->out)
  751. usb_ep_disable(dev->out_ep);
  752. dev->interface = -1;
  753. }
  754. /* change our operational config. must agree with the code
  755. * that returns config descriptors, and altsetting code.
  756. */
  757. static int
  758. printer_set_config(struct printer_dev *dev, unsigned number)
  759. {
  760. int result = 0;
  761. struct usb_gadget *gadget = dev->gadget;
  762. if (gadget_is_sa1100(gadget) && dev->config) {
  763. /* tx fifo is full, but we can't clear it...*/
  764. INFO(dev, "can't change configurations\n");
  765. return -ESPIPE;
  766. }
  767. switch (number) {
  768. case DEV_CONFIG_VALUE:
  769. result = 0;
  770. break;
  771. default:
  772. result = -EINVAL;
  773. /* FALL THROUGH */
  774. case 0:
  775. break;
  776. }
  777. if (result) {
  778. usb_gadget_vbus_draw(dev->gadget,
  779. dev->gadget->is_otg ? 8 : 100);
  780. } else {
  781. char *speed;
  782. unsigned power;
  783. power = 2 * config_desc.bMaxPower;
  784. usb_gadget_vbus_draw(dev->gadget, power);
  785. switch (gadget->speed) {
  786. case USB_SPEED_FULL: speed = "full"; break;
  787. #ifdef CONFIG_USB_GADGET_DUALSPEED
  788. case USB_SPEED_HIGH: speed = "high"; break;
  789. #endif
  790. default: speed = "?"; break;
  791. }
  792. dev->config = number;
  793. INFO(dev, "%s speed config #%d: %d mA, %s\n",
  794. speed, number, power, driver_desc);
  795. }
  796. return result;
  797. }
  798. static int
  799. config_buf(enum usb_device_speed speed, u8 *buf, u8 type, unsigned index,
  800. int is_otg)
  801. {
  802. int len;
  803. const struct usb_descriptor_header **function;
  804. #ifdef CONFIG_USB_GADGET_DUALSPEED
  805. int hs = (speed == USB_SPEED_HIGH);
  806. if (type == USB_DT_OTHER_SPEED_CONFIG)
  807. hs = !hs;
  808. if (hs) {
  809. function = hs_printer_function;
  810. } else {
  811. function = fs_printer_function;
  812. }
  813. #else
  814. function = fs_printer_function;
  815. #endif
  816. if (index >= device_desc.bNumConfigurations)
  817. return -EINVAL;
  818. /* for now, don't advertise srp-only devices */
  819. if (!is_otg)
  820. function++;
  821. len = usb_gadget_config_buf(&config_desc, buf, USB_DESC_BUFSIZE,
  822. function);
  823. if (len < 0)
  824. return len;
  825. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  826. return len;
  827. }
  828. /* Change our operational Interface. */
  829. static int
  830. set_interface(struct printer_dev *dev, unsigned number)
  831. {
  832. int result = 0;
  833. if (gadget_is_sa1100(dev->gadget) && dev->interface < 0) {
  834. /* tx fifo is full, but we can't clear it...*/
  835. INFO(dev, "can't change interfaces\n");
  836. return -ESPIPE;
  837. }
  838. /* Free the current interface */
  839. switch (dev->interface) {
  840. case PRINTER_INTERFACE:
  841. printer_reset_interface(dev);
  842. break;
  843. }
  844. switch (number) {
  845. case PRINTER_INTERFACE:
  846. result = set_printer_interface(dev);
  847. if (result) {
  848. printer_reset_interface(dev);
  849. } else {
  850. dev->interface = PRINTER_INTERFACE;
  851. }
  852. break;
  853. default:
  854. result = -EINVAL;
  855. /* FALL THROUGH */
  856. }
  857. if (!result)
  858. INFO(dev, "Using interface %x\n", number);
  859. return result;
  860. }
  861. static void printer_setup_complete(struct usb_ep *ep, struct usb_request *req)
  862. {
  863. if (req->status || req->actual != req->length)
  864. DBG((struct printer_dev *) ep->driver_data,
  865. "setup complete --> %d, %d/%d\n",
  866. req->status, req->actual, req->length);
  867. }
  868. static void printer_soft_reset(struct printer_dev *dev)
  869. {
  870. struct usb_request *req;
  871. INFO(dev, "Received Printer Reset Request\n");
  872. if (usb_ep_disable(dev->in_ep))
  873. DBG(dev, "Failed to disable USB in_ep\n");
  874. if (usb_ep_disable(dev->out_ep))
  875. DBG(dev, "Failed to disable USB out_ep\n");
  876. if (dev->current_rx_req != NULL) {
  877. list_add(&dev->current_rx_req->list, &dev->rx_reqs);
  878. dev->current_rx_req = NULL;
  879. }
  880. dev->current_rx_bytes = 0;
  881. dev->current_rx_buf = NULL;
  882. dev->reset_printer = 1;
  883. while (likely(!(list_empty(&dev->rx_buffers)))) {
  884. req = container_of(dev->rx_buffers.next, struct usb_request,
  885. list);
  886. list_del_init(&req->list);
  887. list_add(&req->list, &dev->rx_reqs);
  888. }
  889. while (likely(!(list_empty(&dev->rx_reqs_active)))) {
  890. req = container_of(dev->rx_buffers.next, struct usb_request,
  891. list);
  892. list_del_init(&req->list);
  893. list_add(&req->list, &dev->rx_reqs);
  894. }
  895. while (likely(!(list_empty(&dev->tx_reqs_active)))) {
  896. req = container_of(dev->tx_reqs_active.next,
  897. struct usb_request, list);
  898. list_del_init(&req->list);
  899. list_add(&req->list, &dev->tx_reqs);
  900. }
  901. if (usb_ep_enable(dev->in_ep, dev->in))
  902. DBG(dev, "Failed to enable USB in_ep\n");
  903. if (usb_ep_enable(dev->out_ep, dev->out))
  904. DBG(dev, "Failed to enable USB out_ep\n");
  905. wake_up_interruptible(&dev->rx_wait);
  906. wake_up_interruptible(&dev->tx_wait);
  907. wake_up_interruptible(&dev->tx_flush_wait);
  908. }
  909. /*-------------------------------------------------------------------------*/
  910. /*
  911. * The setup() callback implements all the ep0 functionality that's not
  912. * handled lower down.
  913. */
  914. static int
  915. printer_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  916. {
  917. struct printer_dev *dev = get_gadget_data(gadget);
  918. struct usb_request *req = dev->req;
  919. int value = -EOPNOTSUPP;
  920. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  921. u16 wValue = le16_to_cpu(ctrl->wValue);
  922. u16 wLength = le16_to_cpu(ctrl->wLength);
  923. DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
  924. ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
  925. req->complete = printer_setup_complete;
  926. switch (ctrl->bRequestType&USB_TYPE_MASK) {
  927. case USB_TYPE_STANDARD:
  928. switch (ctrl->bRequest) {
  929. case USB_REQ_GET_DESCRIPTOR:
  930. if (ctrl->bRequestType != USB_DIR_IN)
  931. break;
  932. switch (wValue >> 8) {
  933. case USB_DT_DEVICE:
  934. value = min(wLength, (u16) sizeof device_desc);
  935. memcpy(req->buf, &device_desc, value);
  936. break;
  937. #ifdef CONFIG_USB_GADGET_DUALSPEED
  938. case USB_DT_DEVICE_QUALIFIER:
  939. if (!gadget->is_dualspeed)
  940. break;
  941. value = min(wLength,
  942. (u16) sizeof dev_qualifier);
  943. memcpy(req->buf, &dev_qualifier, value);
  944. break;
  945. case USB_DT_OTHER_SPEED_CONFIG:
  946. if (!gadget->is_dualspeed)
  947. break;
  948. /* FALLTHROUGH */
  949. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  950. case USB_DT_CONFIG:
  951. value = config_buf(gadget->speed, req->buf,
  952. wValue >> 8,
  953. wValue & 0xff,
  954. gadget->is_otg);
  955. if (value >= 0)
  956. value = min(wLength, (u16) value);
  957. break;
  958. case USB_DT_STRING:
  959. value = usb_gadget_get_string(&stringtab,
  960. wValue & 0xff, req->buf);
  961. if (value >= 0)
  962. value = min(wLength, (u16) value);
  963. break;
  964. }
  965. break;
  966. case USB_REQ_SET_CONFIGURATION:
  967. if (ctrl->bRequestType != 0)
  968. break;
  969. if (gadget->a_hnp_support)
  970. DBG(dev, "HNP available\n");
  971. else if (gadget->a_alt_hnp_support)
  972. DBG(dev, "HNP needs a different root port\n");
  973. value = printer_set_config(dev, wValue);
  974. break;
  975. case USB_REQ_GET_CONFIGURATION:
  976. if (ctrl->bRequestType != USB_DIR_IN)
  977. break;
  978. *(u8 *)req->buf = dev->config;
  979. value = min(wLength, (u16) 1);
  980. break;
  981. case USB_REQ_SET_INTERFACE:
  982. if (ctrl->bRequestType != USB_RECIP_INTERFACE ||
  983. !dev->config)
  984. break;
  985. value = set_interface(dev, PRINTER_INTERFACE);
  986. break;
  987. case USB_REQ_GET_INTERFACE:
  988. if (ctrl->bRequestType !=
  989. (USB_DIR_IN|USB_RECIP_INTERFACE)
  990. || !dev->config)
  991. break;
  992. *(u8 *)req->buf = dev->interface;
  993. value = min(wLength, (u16) 1);
  994. break;
  995. default:
  996. goto unknown;
  997. }
  998. break;
  999. case USB_TYPE_CLASS:
  1000. switch (ctrl->bRequest) {
  1001. case 0: /* Get the IEEE-1284 PNP String */
  1002. /* Only one printer interface is supported. */
  1003. if ((wIndex>>8) != PRINTER_INTERFACE)
  1004. break;
  1005. value = (pnp_string[0]<<8)|pnp_string[1];
  1006. memcpy(req->buf, pnp_string, value);
  1007. DBG(dev, "1284 PNP String: %x %s\n", value,
  1008. &pnp_string[2]);
  1009. break;
  1010. case 1: /* Get Port Status */
  1011. /* Only one printer interface is supported. */
  1012. if (wIndex != PRINTER_INTERFACE)
  1013. break;
  1014. *(u8 *)req->buf = dev->printer_status;
  1015. value = min(wLength, (u16) 1);
  1016. break;
  1017. case 2: /* Soft Reset */
  1018. /* Only one printer interface is supported. */
  1019. if (wIndex != PRINTER_INTERFACE)
  1020. break;
  1021. printer_soft_reset(dev);
  1022. value = 0;
  1023. break;
  1024. default:
  1025. goto unknown;
  1026. }
  1027. break;
  1028. default:
  1029. unknown:
  1030. VDBG(dev,
  1031. "unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
  1032. ctrl->bRequestType, ctrl->bRequest,
  1033. wValue, wIndex, wLength);
  1034. break;
  1035. }
  1036. /* respond with data transfer before status phase? */
  1037. if (value >= 0) {
  1038. req->length = value;
  1039. req->zero = value < wLength
  1040. && (value % gadget->ep0->maxpacket) == 0;
  1041. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1042. if (value < 0) {
  1043. DBG(dev, "ep_queue --> %d\n", value);
  1044. req->status = 0;
  1045. printer_setup_complete(gadget->ep0, req);
  1046. }
  1047. }
  1048. /* host either stalls (value < 0) or reports success */
  1049. return value;
  1050. }
  1051. static void
  1052. printer_disconnect(struct usb_gadget *gadget)
  1053. {
  1054. struct printer_dev *dev = get_gadget_data(gadget);
  1055. unsigned long flags;
  1056. DBG(dev, "%s\n", __func__);
  1057. spin_lock_irqsave(&dev->lock, flags);
  1058. printer_reset_interface(dev);
  1059. spin_unlock_irqrestore(&dev->lock, flags);
  1060. }
  1061. static void
  1062. printer_unbind(struct usb_gadget *gadget)
  1063. {
  1064. struct printer_dev *dev = get_gadget_data(gadget);
  1065. struct usb_request *req;
  1066. DBG(dev, "%s\n", __func__);
  1067. /* Remove sysfs files */
  1068. device_destroy(usb_gadget_class, g_printer_devno);
  1069. /* Remove Character Device */
  1070. cdev_del(&dev->printer_cdev);
  1071. /* we must already have been disconnected ... no i/o may be active */
  1072. WARN_ON(!list_empty(&dev->tx_reqs_active));
  1073. WARN_ON(!list_empty(&dev->rx_reqs_active));
  1074. /* Free all memory for this driver. */
  1075. while (!list_empty(&dev->tx_reqs)) {
  1076. req = container_of(dev->tx_reqs.next, struct usb_request,
  1077. list);
  1078. list_del(&req->list);
  1079. printer_req_free(dev->in_ep, req);
  1080. }
  1081. if (dev->current_rx_req != NULL)
  1082. printer_req_free(dev->out_ep, dev->current_rx_req);
  1083. while (!list_empty(&dev->rx_reqs)) {
  1084. req = container_of(dev->rx_reqs.next,
  1085. struct usb_request, list);
  1086. list_del(&req->list);
  1087. printer_req_free(dev->out_ep, req);
  1088. }
  1089. while (!list_empty(&dev->rx_buffers)) {
  1090. req = container_of(dev->rx_buffers.next,
  1091. struct usb_request, list);
  1092. list_del(&req->list);
  1093. printer_req_free(dev->out_ep, req);
  1094. }
  1095. if (dev->req) {
  1096. printer_req_free(gadget->ep0, dev->req);
  1097. dev->req = NULL;
  1098. }
  1099. set_gadget_data(gadget, NULL);
  1100. }
  1101. static int __init
  1102. printer_bind(struct usb_gadget *gadget)
  1103. {
  1104. struct printer_dev *dev;
  1105. struct usb_ep *in_ep, *out_ep;
  1106. int status = -ENOMEM;
  1107. int gcnum;
  1108. size_t len;
  1109. u32 i;
  1110. struct usb_request *req;
  1111. dev = &usb_printer_gadget;
  1112. /* Setup the sysfs files for the printer gadget. */
  1113. dev->pdev = device_create_drvdata(usb_gadget_class, NULL,
  1114. g_printer_devno, NULL, "g_printer");
  1115. if (IS_ERR(dev->pdev)) {
  1116. ERROR(dev, "Failed to create device: g_printer\n");
  1117. goto fail;
  1118. }
  1119. /*
  1120. * Register a character device as an interface to a user mode
  1121. * program that handles the printer specific functionality.
  1122. */
  1123. cdev_init(&dev->printer_cdev, &printer_io_operations);
  1124. dev->printer_cdev.owner = THIS_MODULE;
  1125. status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
  1126. if (status) {
  1127. ERROR(dev, "Failed to open char device\n");
  1128. goto fail;
  1129. }
  1130. if (gadget_is_sa1100(gadget)) {
  1131. /* hardware can't write zero length packets. */
  1132. ERROR(dev, "SA1100 controller is unsupport by this driver\n");
  1133. goto fail;
  1134. }
  1135. gcnum = usb_gadget_controller_number(gadget);
  1136. if (gcnum >= 0) {
  1137. device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
  1138. } else {
  1139. dev_warn(&gadget->dev, "controller '%s' not recognized\n",
  1140. gadget->name);
  1141. /* unrecognized, but safe unless bulk is REALLY quirky */
  1142. device_desc.bcdDevice =
  1143. __constant_cpu_to_le16(0xFFFF);
  1144. }
  1145. snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
  1146. init_utsname()->sysname, init_utsname()->release,
  1147. gadget->name);
  1148. device_desc.idVendor =
  1149. __constant_cpu_to_le16(PRINTER_VENDOR_NUM);
  1150. device_desc.idProduct =
  1151. __constant_cpu_to_le16(PRINTER_PRODUCT_NUM);
  1152. /* support optional vendor/distro customization */
  1153. if (idVendor) {
  1154. if (!idProduct) {
  1155. dev_err(&gadget->dev, "idVendor needs idProduct!\n");
  1156. return -ENODEV;
  1157. }
  1158. device_desc.idVendor = cpu_to_le16(idVendor);
  1159. device_desc.idProduct = cpu_to_le16(idProduct);
  1160. if (bcdDevice)
  1161. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1162. }
  1163. if (iManufacturer)
  1164. strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
  1165. if (iProduct)
  1166. strlcpy(product_desc, iProduct, sizeof product_desc);
  1167. if (iSerialNum)
  1168. strlcpy(serial_num, iSerialNum, sizeof serial_num);
  1169. if (iPNPstring)
  1170. strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
  1171. len = strlen(pnp_string);
  1172. pnp_string[0] = (len >> 8) & 0xFF;
  1173. pnp_string[1] = len & 0xFF;
  1174. /* all we really need is bulk IN/OUT */
  1175. usb_ep_autoconfig_reset(gadget);
  1176. in_ep = usb_ep_autoconfig(gadget, &fs_ep_in_desc);
  1177. if (!in_ep) {
  1178. autoconf_fail:
  1179. dev_err(&gadget->dev, "can't autoconfigure on %s\n",
  1180. gadget->name);
  1181. return -ENODEV;
  1182. }
  1183. in_ep->driver_data = in_ep; /* claim */
  1184. out_ep = usb_ep_autoconfig(gadget, &fs_ep_out_desc);
  1185. if (!out_ep)
  1186. goto autoconf_fail;
  1187. out_ep->driver_data = out_ep; /* claim */
  1188. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1189. /* assumes ep0 uses the same value for both speeds ... */
  1190. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1191. /* and that all endpoints are dual-speed */
  1192. hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
  1193. hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
  1194. #endif /* DUALSPEED */
  1195. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1196. usb_gadget_set_selfpowered(gadget);
  1197. if (gadget->is_otg) {
  1198. otg_desc.bmAttributes |= USB_OTG_HNP,
  1199. config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1200. config_desc.bMaxPower = 4;
  1201. }
  1202. spin_lock_init(&dev->lock);
  1203. spin_lock_init(&dev->lock_printer_io);
  1204. INIT_LIST_HEAD(&dev->tx_reqs);
  1205. INIT_LIST_HEAD(&dev->tx_reqs_active);
  1206. INIT_LIST_HEAD(&dev->rx_reqs);
  1207. INIT_LIST_HEAD(&dev->rx_reqs_active);
  1208. INIT_LIST_HEAD(&dev->rx_buffers);
  1209. init_waitqueue_head(&dev->rx_wait);
  1210. init_waitqueue_head(&dev->tx_wait);
  1211. init_waitqueue_head(&dev->tx_flush_wait);
  1212. dev->config = 0;
  1213. dev->interface = -1;
  1214. dev->printer_cdev_open = 0;
  1215. dev->printer_status = PRINTER_NOT_ERROR;
  1216. dev->current_rx_req = NULL;
  1217. dev->current_rx_bytes = 0;
  1218. dev->current_rx_buf = NULL;
  1219. dev->in_ep = in_ep;
  1220. dev->out_ep = out_ep;
  1221. /* preallocate control message data and buffer */
  1222. dev->req = printer_req_alloc(gadget->ep0, USB_DESC_BUFSIZE,
  1223. GFP_KERNEL);
  1224. if (!dev->req) {
  1225. status = -ENOMEM;
  1226. goto fail;
  1227. }
  1228. for (i = 0; i < QLEN; i++) {
  1229. req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
  1230. if (!req) {
  1231. while (!list_empty(&dev->tx_reqs)) {
  1232. req = container_of(dev->tx_reqs.next,
  1233. struct usb_request, list);
  1234. list_del(&req->list);
  1235. printer_req_free(dev->in_ep, req);
  1236. }
  1237. return -ENOMEM;
  1238. }
  1239. list_add(&req->list, &dev->tx_reqs);
  1240. }
  1241. for (i = 0; i < QLEN; i++) {
  1242. req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
  1243. if (!req) {
  1244. while (!list_empty(&dev->rx_reqs)) {
  1245. req = container_of(dev->rx_reqs.next,
  1246. struct usb_request, list);
  1247. list_del(&req->list);
  1248. printer_req_free(dev->out_ep, req);
  1249. }
  1250. return -ENOMEM;
  1251. }
  1252. list_add(&req->list, &dev->rx_reqs);
  1253. }
  1254. dev->req->complete = printer_setup_complete;
  1255. /* finish hookup to lower layer ... */
  1256. dev->gadget = gadget;
  1257. set_gadget_data(gadget, dev);
  1258. gadget->ep0->driver_data = dev;
  1259. INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  1260. INFO(dev, "using %s, OUT %s IN %s\n", gadget->name, out_ep->name,
  1261. in_ep->name);
  1262. return 0;
  1263. fail:
  1264. printer_unbind(gadget);
  1265. return status;
  1266. }
  1267. /*-------------------------------------------------------------------------*/
  1268. static struct usb_gadget_driver printer_driver = {
  1269. .speed = DEVSPEED,
  1270. .function = (char *) driver_desc,
  1271. .bind = printer_bind,
  1272. .unbind = printer_unbind,
  1273. .setup = printer_setup,
  1274. .disconnect = printer_disconnect,
  1275. .driver = {
  1276. .name = (char *) shortname,
  1277. .owner = THIS_MODULE,
  1278. },
  1279. };
  1280. MODULE_DESCRIPTION(DRIVER_DESC);
  1281. MODULE_AUTHOR("Craig Nadler");
  1282. MODULE_LICENSE("GPL");
  1283. static int __init
  1284. init(void)
  1285. {
  1286. int status;
  1287. usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
  1288. if (IS_ERR(usb_gadget_class)) {
  1289. status = PTR_ERR(usb_gadget_class);
  1290. ERROR(dev, "unable to create usb_gadget class %d\n", status);
  1291. return status;
  1292. }
  1293. status = alloc_chrdev_region(&g_printer_devno, 0, 1,
  1294. "USB printer gadget");
  1295. if (status) {
  1296. ERROR(dev, "alloc_chrdev_region %d\n", status);
  1297. class_destroy(usb_gadget_class);
  1298. return status;
  1299. }
  1300. status = usb_gadget_register_driver(&printer_driver);
  1301. if (status) {
  1302. class_destroy(usb_gadget_class);
  1303. unregister_chrdev_region(g_printer_devno, 1);
  1304. DBG(dev, "usb_gadget_register_driver %x\n", status);
  1305. }
  1306. return status;
  1307. }
  1308. module_init(init);
  1309. static void __exit
  1310. cleanup(void)
  1311. {
  1312. int status;
  1313. spin_lock(&usb_printer_gadget.lock_printer_io);
  1314. class_destroy(usb_gadget_class);
  1315. unregister_chrdev_region(g_printer_devno, 2);
  1316. status = usb_gadget_unregister_driver(&printer_driver);
  1317. if (status)
  1318. ERROR(dev, "usb_gadget_unregister_driver %x\n", status);
  1319. spin_unlock(&usb_printer_gadget.lock_printer_io);
  1320. }
  1321. module_exit(cleanup);