usb-serial.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * USB Serial Converter driver
  3. *
  4. * Copyright (C) 1999 - 2004 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
  6. * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This driver was originally based on the ACM driver by Armin Fuerst (which was
  13. * based on a driver by Brad Keryan)
  14. *
  15. * See Documentation/usb/usb-serial.txt for more information on using this driver
  16. *
  17. * (12/10/2002) gkh
  18. * Split the ports off into their own struct device, and added a
  19. * usb-serial bus driver.
  20. *
  21. * (11/19/2002) gkh
  22. * removed a few #ifdefs for the generic code and cleaned up the failure
  23. * logic in initialization.
  24. *
  25. * (10/02/2002) gkh
  26. * moved the console code to console.c and out of this file.
  27. *
  28. * (06/05/2002) gkh
  29. * moved location of startup() call in serial_probe() until after all
  30. * of the port information and endpoints are initialized. This makes
  31. * things easier for some drivers.
  32. *
  33. * (04/10/2002) gkh
  34. * added serial_read_proc function which creates a
  35. * /proc/tty/driver/usb-serial file.
  36. *
  37. * (03/27/2002) gkh
  38. * Got USB serial console code working properly and merged into the main
  39. * version of the tree. Thanks to Randy Dunlap for the initial version
  40. * of this code, and for pushing me to finish it up.
  41. * The USB serial console works with any usb serial driver device.
  42. *
  43. * (03/21/2002) gkh
  44. * Moved all manipulation of port->open_count into the core. Now the
  45. * individual driver's open and close functions are called only when the
  46. * first open() and last close() is called. Making the drivers a bit
  47. * smaller and simpler.
  48. * Fixed a bug if a driver didn't have the owner field set.
  49. *
  50. * (02/26/2002) gkh
  51. * Moved all locking into the main serial_* functions, instead of having
  52. * the individual drivers have to grab the port semaphore. This should
  53. * reduce races.
  54. * Reworked the MOD_INC logic a bit to always increment and decrement, even
  55. * if the generic driver is being used.
  56. *
  57. * (10/10/2001) gkh
  58. * usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
  59. * help prevent child drivers from accessing the device since it is now
  60. * gone.
  61. *
  62. * (09/13/2001) gkh
  63. * Moved generic driver initialize after we have registered with the USB
  64. * core. Thanks to Randy Dunlap for pointing this problem out.
  65. *
  66. * (07/03/2001) gkh
  67. * Fixed module paramater size. Thanks to John Brockmeyer for the pointer.
  68. * Fixed vendor and product getting defined through the MODULE_PARM macro
  69. * if the Generic driver wasn't compiled in.
  70. * Fixed problem with generic_shutdown() not being called for drivers that
  71. * don't have a shutdown() function.
  72. *
  73. * (06/06/2001) gkh
  74. * added evil hack that is needed for the prolific pl2303 device due to the
  75. * crazy way its endpoints are set up.
  76. *
  77. * (05/30/2001) gkh
  78. * switched from using spinlock to a semaphore, which fixes lots of problems.
  79. *
  80. * (04/08/2001) gb
  81. * Identify version on module load.
  82. *
  83. * 2001_02_05 gkh
  84. * Fixed buffer overflows bug with the generic serial driver. Thanks to
  85. * Todd Squires <squirest@ct0.com> for fixing this.
  86. *
  87. * (01/10/2001) gkh
  88. * Fixed bug where the generic serial adaptor grabbed _any_ device that was
  89. * offered to it.
  90. *
  91. * (12/12/2000) gkh
  92. * Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
  93. * moved them to the serial_open and serial_close functions.
  94. * Also fixed bug with there not being a MOD_DEC for the generic driver
  95. * (thanks to Gary Brubaker for finding this.)
  96. *
  97. * (11/29/2000) gkh
  98. * Small NULL pointer initialization cleanup which saves a bit of disk image
  99. *
  100. * (11/01/2000) Adam J. Richter
  101. * instead of using idVendor/idProduct pairs, usb serial drivers
  102. * now identify their hardware interest with usb_device_id tables,
  103. * which they usually have anyhow for use with MODULE_DEVICE_TABLE.
  104. *
  105. * (10/05/2000) gkh
  106. * Fixed bug with urb->dev not being set properly, now that the usb
  107. * core needs it.
  108. *
  109. * (09/11/2000) gkh
  110. * Removed DEBUG #ifdefs with call to usb_serial_debug_data
  111. *
  112. * (08/28/2000) gkh
  113. * Added port_lock to port structure.
  114. * Added locks for SMP safeness to generic driver
  115. * Fixed the ability to open a generic device's port more than once.
  116. *
  117. * (07/23/2000) gkh
  118. * Added bulk_out_endpointAddress to port structure.
  119. *
  120. * (07/19/2000) gkh, pberger, and borchers
  121. * Modifications to allow usb-serial drivers to be modules.
  122. *
  123. * (07/03/2000) gkh
  124. * Added more debugging to serial_ioctl call
  125. *
  126. * (06/25/2000) gkh
  127. * Changed generic_write_bulk_callback to not call wake_up_interruptible
  128. * directly, but to have port_softint do it at a safer time.
  129. *
  130. * (06/23/2000) gkh
  131. * Cleaned up debugging statements in a quest to find UHCI timeout bug.
  132. *
  133. * (05/22/2000) gkh
  134. * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
  135. * removed from the individual device source files.
  136. *
  137. * (05/03/2000) gkh
  138. * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
  139. *
  140. * (05/02/2000) gkh
  141. * Changed devfs and tty register code to work properly now. This was based on
  142. * the ACM driver changes by Vojtech Pavlik.
  143. *
  144. * (04/27/2000) Ryan VanderBijl
  145. * Put calls to *_paranoia_checks into one function.
  146. *
  147. * (04/23/2000) gkh
  148. * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
  149. * Moved when the startup code printed out the devices that are supported.
  150. *
  151. * (04/19/2000) gkh
  152. * Added driver for ZyXEL omni.net lcd plus ISDN TA
  153. * Made startup info message specify which drivers were compiled in.
  154. *
  155. * (04/03/2000) gkh
  156. * Changed the probe process to remove the module unload races.
  157. * Changed where the tty layer gets initialized to have devfs work nicer.
  158. * Added initial devfs support.
  159. *
  160. * (03/26/2000) gkh
  161. * Split driver up into device specific pieces.
  162. *
  163. * (03/19/2000) gkh
  164. * Fixed oops that could happen when device was removed while a program
  165. * was talking to the device.
  166. * Removed the static urbs and now all urbs are created and destroyed
  167. * dynamically.
  168. * Reworked the internal interface. Now everything is based on the
  169. * usb_serial_port structure instead of the larger usb_serial structure.
  170. * This fixes the bug that a multiport device could not have more than
  171. * one port open at one time.
  172. *
  173. * (03/17/2000) gkh
  174. * Added config option for debugging messages.
  175. * Added patch for keyspan pda from Brian Warner.
  176. *
  177. * (03/06/2000) gkh
  178. * Added the keyspan pda code from Brian Warner <warner@lothar.com>
  179. * Moved a bunch of the port specific stuff into its own structure. This
  180. * is in anticipation of the true multiport devices (there's a bug if you
  181. * try to access more than one port of any multiport device right now)
  182. *
  183. * (02/21/2000) gkh
  184. * Made it so that any serial devices only have to specify which functions
  185. * they want to overload from the generic function calls (great,
  186. * inheritance in C, in a driver, just what I wanted...)
  187. * Added support for set_termios and ioctl function calls. No drivers take
  188. * advantage of this yet.
  189. * Removed the #ifdef MODULE, now there is no module specific code.
  190. * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
  191. * to Miles Lott).
  192. * Small fix to get_free_serial.
  193. *
  194. * (02/14/2000) gkh
  195. * Removed the Belkin and Peracom functionality from the driver due to
  196. * the lack of support from the vendor, and me not wanting people to
  197. * accidenatly buy the device, expecting it to work with Linux.
  198. * Added read_bulk_callback and write_bulk_callback to the type structure
  199. * for the needs of the FTDI and WhiteHEAT driver.
  200. * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
  201. * Ryder.
  202. * Changed the output urb size back to the max endpoint size to make
  203. * the ftdi_sio driver have it easier, and due to the fact that it didn't
  204. * really increase the speed any.
  205. *
  206. * (02/11/2000) gkh
  207. * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
  208. * patch from Miles Lott (milos@insync.net).
  209. * Fixed bug with not restoring the minor range that a device grabs, if
  210. * the startup function fails (thanks Miles for finding this).
  211. *
  212. * (02/05/2000) gkh
  213. * Added initial framework for the Keyspan PDA serial converter so that
  214. * Brian Warner has a place to put his code.
  215. * Made the ezusb specific functions generic enough that different
  216. * devices can use them (whiteheat and keyspan_pda both need them).
  217. * Split out a whole bunch of structure and other stuff to a separate
  218. * usb-serial.h file.
  219. * Made the Visor connection messages a little more understandable, now
  220. * that Miles Lott (milos@insync.net) has gotten the Generic channel to
  221. * work. Also made them always show up in the log file.
  222. *
  223. * (01/25/2000) gkh
  224. * Added initial framework for FTDI serial converter so that Bill Ryder
  225. * has a place to put his code.
  226. * Added the vendor specific info from Handspring. Now we can print out
  227. * informational debug messages as well as understand what is happening.
  228. *
  229. * (01/23/2000) gkh
  230. * Fixed problem of crash when trying to open a port that didn't have a
  231. * device assigned to it. Made the minor node finding a little smarter,
  232. * now it looks to find a continuous space for the new device.
  233. *
  234. * (01/21/2000) gkh
  235. * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
  236. * Fixed get_serial_by_minor which was all messed up for multi port
  237. * devices. Fixed multi port problem for generic devices. Now the number
  238. * of ports is determined by the number of bulk out endpoints for the
  239. * generic device.
  240. *
  241. * (01/19/2000) gkh
  242. * Removed lots of cruft that was around from the old (pre urb) driver
  243. * interface.
  244. * Made the serial_table dynamic. This should save lots of memory when
  245. * the number of minor nodes goes up to 256.
  246. * Added initial support for devices that have more than one port.
  247. * Added more debugging comments for the Visor, and added a needed
  248. * set_configuration call.
  249. *
  250. * (01/17/2000) gkh
  251. * Fixed the WhiteHEAT firmware (my processing tool had a bug)
  252. * and added new debug loader firmware for it.
  253. * Removed the put_char function as it isn't really needed.
  254. * Added visor startup commands as found by the Win98 dump.
  255. *
  256. * (01/13/2000) gkh
  257. * Fixed the vendor id for the generic driver to the one I meant it to be.
  258. *
  259. * (01/12/2000) gkh
  260. * Forget the version numbering...that's pretty useless...
  261. * Made the driver able to be compiled so that the user can select which
  262. * converter they want to use. This allows people who only want the Visor
  263. * support to not pay the memory size price of the WhiteHEAT.
  264. * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
  265. * grabbed the root hub. Not good.
  266. *
  267. * version 0.4.0 (01/10/2000) gkh
  268. * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
  269. * device. Added startup function to allow firmware to be downloaded to
  270. * a device if it needs to be.
  271. * Added firmware download logic to the WhiteHEAT device.
  272. * Started to add #defines to split up the different drivers for potential
  273. * configuration option.
  274. *
  275. * version 0.3.1 (12/30/99) gkh
  276. * Fixed problems with urb for bulk out.
  277. * Added initial support for multiple sets of endpoints. This enables
  278. * the Handspring Visor to be attached successfully. Only the first
  279. * bulk in / bulk out endpoint pair is being used right now.
  280. *
  281. * version 0.3.0 (12/27/99) gkh
  282. * Added initial support for the Handspring Visor based on a patch from
  283. * Miles Lott (milos@sneety.insync.net)
  284. * Cleaned up the code a bunch and converted over to using urbs only.
  285. *
  286. * version 0.2.3 (12/21/99) gkh
  287. * Added initial support for the Connect Tech WhiteHEAT converter.
  288. * Incremented the number of ports in expectation of getting the
  289. * WhiteHEAT to work properly (4 ports per connection).
  290. * Added notification on insertion and removal of what port the
  291. * device is/was connected to (and what kind of device it was).
  292. *
  293. * version 0.2.2 (12/16/99) gkh
  294. * Changed major number to the new allocated number. We're legal now!
  295. *
  296. * version 0.2.1 (12/14/99) gkh
  297. * Fixed bug that happens when device node is opened when there isn't a
  298. * device attached to it. Thanks to marek@webdesign.no for noticing this.
  299. *
  300. * version 0.2.0 (11/10/99) gkh
  301. * Split up internals to make it easier to add different types of serial
  302. * converters to the code.
  303. * Added a "generic" driver that gets it's vendor and product id
  304. * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
  305. * for the idea and sample code (from the usb scanner driver.)
  306. * Cleared up any licensing questions by releasing it under the GNU GPL.
  307. *
  308. * version 0.1.2 (10/25/99) gkh
  309. * Fixed bug in detecting device.
  310. *
  311. * version 0.1.1 (10/05/99) gkh
  312. * Changed the major number to not conflict with anything else.
  313. *
  314. * version 0.1 (09/28/99) gkh
  315. * Can recognize the two different devices and start up a read from
  316. * device when asked to. Writes also work. No control signals yet, this
  317. * all is vendor specific data (i.e. no spec), also no control for
  318. * different baud rates or other bit settings.
  319. * Currently we are using the same devid as the acm driver. This needs
  320. * to change.
  321. *
  322. */
  323. #include <linux/config.h>
  324. #include <linux/kernel.h>
  325. #include <linux/errno.h>
  326. #include <linux/init.h>
  327. #include <linux/slab.h>
  328. #include <linux/tty.h>
  329. #include <linux/tty_driver.h>
  330. #include <linux/tty_flip.h>
  331. #include <linux/module.h>
  332. #include <linux/moduleparam.h>
  333. #include <linux/spinlock.h>
  334. #include <linux/list.h>
  335. #include <linux/smp_lock.h>
  336. #include <asm/uaccess.h>
  337. #include <linux/usb.h>
  338. #include "usb-serial.h"
  339. #include "pl2303.h"
  340. /*
  341. * Version Information
  342. */
  343. #define DRIVER_VERSION "v2.0"
  344. #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
  345. #define DRIVER_DESC "USB Serial Driver core"
  346. /* Driver structure we register with the USB core */
  347. static struct usb_driver usb_serial_driver = {
  348. .owner = THIS_MODULE,
  349. .name = "usbserial",
  350. .probe = usb_serial_probe,
  351. .disconnect = usb_serial_disconnect,
  352. };
  353. /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
  354. the MODULE_DEVICE_TABLE declarations in each serial driver
  355. cause the "hotplug" program to pull in whatever module is necessary
  356. via modprobe, and modprobe will load usbserial because the serial
  357. drivers depend on it.
  358. */
  359. static int debug;
  360. static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
  361. static LIST_HEAD(usb_serial_driver_list);
  362. struct usb_serial *usb_serial_get_by_index(unsigned index)
  363. {
  364. struct usb_serial *serial = serial_table[index];
  365. if (serial)
  366. kref_get(&serial->kref);
  367. return serial;
  368. }
  369. static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
  370. {
  371. unsigned int i, j;
  372. int good_spot;
  373. dbg("%s %d", __FUNCTION__, num_ports);
  374. *minor = 0;
  375. for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
  376. if (serial_table[i])
  377. continue;
  378. good_spot = 1;
  379. for (j = 1; j <= num_ports-1; ++j)
  380. if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
  381. good_spot = 0;
  382. i += j;
  383. break;
  384. }
  385. if (good_spot == 0)
  386. continue;
  387. *minor = i;
  388. dbg("%s - minor base = %d", __FUNCTION__, *minor);
  389. for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
  390. serial_table[i] = serial;
  391. return serial;
  392. }
  393. return NULL;
  394. }
  395. static void return_serial(struct usb_serial *serial)
  396. {
  397. int i;
  398. dbg("%s", __FUNCTION__);
  399. if (serial == NULL)
  400. return;
  401. for (i = 0; i < serial->num_ports; ++i) {
  402. serial_table[serial->minor + i] = NULL;
  403. }
  404. }
  405. static void destroy_serial(struct kref *kref)
  406. {
  407. struct usb_serial *serial;
  408. struct usb_serial_port *port;
  409. int i;
  410. serial = to_usb_serial(kref);
  411. dbg ("%s - %s", __FUNCTION__, serial->type->name);
  412. serial->type->shutdown(serial);
  413. /* return the minor range that this device had */
  414. return_serial(serial);
  415. for (i = 0; i < serial->num_ports; ++i)
  416. serial->port[i]->open_count = 0;
  417. /* the ports are cleaned up and released in port_release() */
  418. for (i = 0; i < serial->num_ports; ++i)
  419. if (serial->port[i]->dev.parent != NULL) {
  420. device_unregister(&serial->port[i]->dev);
  421. serial->port[i] = NULL;
  422. }
  423. /* If this is a "fake" port, we have to clean it up here, as it will
  424. * not get cleaned up in port_release() as it was never registered with
  425. * the driver core */
  426. if (serial->num_ports < serial->num_port_pointers) {
  427. for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
  428. port = serial->port[i];
  429. if (!port)
  430. continue;
  431. usb_kill_urb(port->read_urb);
  432. usb_free_urb(port->read_urb);
  433. usb_kill_urb(port->write_urb);
  434. usb_free_urb(port->write_urb);
  435. usb_kill_urb(port->interrupt_in_urb);
  436. usb_free_urb(port->interrupt_in_urb);
  437. usb_kill_urb(port->interrupt_out_urb);
  438. usb_free_urb(port->interrupt_out_urb);
  439. kfree(port->bulk_in_buffer);
  440. kfree(port->bulk_out_buffer);
  441. kfree(port->interrupt_in_buffer);
  442. kfree(port->interrupt_out_buffer);
  443. }
  444. }
  445. usb_put_dev(serial->dev);
  446. /* free up any memory that we allocated */
  447. kfree (serial);
  448. }
  449. /*****************************************************************************
  450. * Driver tty interface functions
  451. *****************************************************************************/
  452. static int serial_open (struct tty_struct *tty, struct file * filp)
  453. {
  454. struct usb_serial *serial;
  455. struct usb_serial_port *port;
  456. unsigned int portNumber;
  457. int retval;
  458. dbg("%s", __FUNCTION__);
  459. /* get the serial object associated with this tty pointer */
  460. serial = usb_serial_get_by_index(tty->index);
  461. if (!serial) {
  462. tty->driver_data = NULL;
  463. return -ENODEV;
  464. }
  465. portNumber = tty->index - serial->minor;
  466. port = serial->port[portNumber];
  467. ++port->open_count;
  468. if (port->open_count == 1) {
  469. /* set up our port structure making the tty driver
  470. * remember our port object, and us it */
  471. tty->driver_data = port;
  472. port->tty = tty;
  473. /* lock this module before we call it
  474. * this may fail, which means we must bail out,
  475. * safe because we are called with BKL held */
  476. if (!try_module_get(serial->type->owner)) {
  477. retval = -ENODEV;
  478. goto bailout_kref_put;
  479. }
  480. /* only call the device specific open if this
  481. * is the first time the port is opened */
  482. retval = serial->type->open(port, filp);
  483. if (retval)
  484. goto bailout_module_put;
  485. }
  486. return 0;
  487. bailout_module_put:
  488. module_put(serial->type->owner);
  489. bailout_kref_put:
  490. kref_put(&serial->kref, destroy_serial);
  491. port->open_count = 0;
  492. return retval;
  493. }
  494. static void serial_close(struct tty_struct *tty, struct file * filp)
  495. {
  496. struct usb_serial_port *port = tty->driver_data;
  497. if (!port)
  498. return;
  499. dbg("%s - port %d", __FUNCTION__, port->number);
  500. if (port->open_count == 0)
  501. return;
  502. --port->open_count;
  503. if (port->open_count == 0) {
  504. /* only call the device specific close if this
  505. * port is being closed by the last owner */
  506. port->serial->type->close(port, filp);
  507. if (port->tty) {
  508. if (port->tty->driver_data)
  509. port->tty->driver_data = NULL;
  510. port->tty = NULL;
  511. }
  512. module_put(port->serial->type->owner);
  513. }
  514. kref_put(&port->serial->kref, destroy_serial);
  515. }
  516. static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
  517. {
  518. struct usb_serial_port *port = tty->driver_data;
  519. int retval = -EINVAL;
  520. dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
  521. if (!port->open_count) {
  522. dbg("%s - port not opened", __FUNCTION__);
  523. goto exit;
  524. }
  525. /* pass on to the driver specific version of this function */
  526. retval = port->serial->type->write(port, buf, count);
  527. exit:
  528. return retval;
  529. }
  530. static int serial_write_room (struct tty_struct *tty)
  531. {
  532. struct usb_serial_port *port = tty->driver_data;
  533. int retval = -EINVAL;
  534. dbg("%s - port %d", __FUNCTION__, port->number);
  535. if (!port->open_count) {
  536. dbg("%s - port not open", __FUNCTION__);
  537. goto exit;
  538. }
  539. /* pass on to the driver specific version of this function */
  540. retval = port->serial->type->write_room(port);
  541. exit:
  542. return retval;
  543. }
  544. static int serial_chars_in_buffer (struct tty_struct *tty)
  545. {
  546. struct usb_serial_port *port = tty->driver_data;
  547. int retval = -EINVAL;
  548. dbg("%s = port %d", __FUNCTION__, port->number);
  549. if (!port->open_count) {
  550. dbg("%s - port not open", __FUNCTION__);
  551. goto exit;
  552. }
  553. /* pass on to the driver specific version of this function */
  554. retval = port->serial->type->chars_in_buffer(port);
  555. exit:
  556. return retval;
  557. }
  558. static void serial_throttle (struct tty_struct * tty)
  559. {
  560. struct usb_serial_port *port = tty->driver_data;
  561. dbg("%s - port %d", __FUNCTION__, port->number);
  562. if (!port->open_count) {
  563. dbg ("%s - port not open", __FUNCTION__);
  564. return;
  565. }
  566. /* pass on to the driver specific version of this function */
  567. if (port->serial->type->throttle)
  568. port->serial->type->throttle(port);
  569. }
  570. static void serial_unthrottle (struct tty_struct * tty)
  571. {
  572. struct usb_serial_port *port = tty->driver_data;
  573. dbg("%s - port %d", __FUNCTION__, port->number);
  574. if (!port->open_count) {
  575. dbg("%s - port not open", __FUNCTION__);
  576. return;
  577. }
  578. /* pass on to the driver specific version of this function */
  579. if (port->serial->type->unthrottle)
  580. port->serial->type->unthrottle(port);
  581. }
  582. static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
  583. {
  584. struct usb_serial_port *port = tty->driver_data;
  585. int retval = -ENODEV;
  586. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  587. if (!port->open_count) {
  588. dbg ("%s - port not open", __FUNCTION__);
  589. goto exit;
  590. }
  591. /* pass on to the driver specific version of this function if it is available */
  592. if (port->serial->type->ioctl)
  593. retval = port->serial->type->ioctl(port, file, cmd, arg);
  594. else
  595. retval = -ENOIOCTLCMD;
  596. exit:
  597. return retval;
  598. }
  599. static void serial_set_termios (struct tty_struct *tty, struct termios * old)
  600. {
  601. struct usb_serial_port *port = tty->driver_data;
  602. dbg("%s - port %d", __FUNCTION__, port->number);
  603. if (!port->open_count) {
  604. dbg("%s - port not open", __FUNCTION__);
  605. return;
  606. }
  607. /* pass on to the driver specific version of this function if it is available */
  608. if (port->serial->type->set_termios)
  609. port->serial->type->set_termios(port, old);
  610. }
  611. static void serial_break (struct tty_struct *tty, int break_state)
  612. {
  613. struct usb_serial_port *port = tty->driver_data;
  614. dbg("%s - port %d", __FUNCTION__, port->number);
  615. if (!port->open_count) {
  616. dbg("%s - port not open", __FUNCTION__);
  617. return;
  618. }
  619. /* pass on to the driver specific version of this function if it is available */
  620. if (port->serial->type->break_ctl)
  621. port->serial->type->break_ctl(port, break_state);
  622. }
  623. static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
  624. {
  625. struct usb_serial *serial;
  626. int length = 0;
  627. int i;
  628. off_t begin = 0;
  629. char tmp[40];
  630. dbg("%s", __FUNCTION__);
  631. length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
  632. for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
  633. serial = usb_serial_get_by_index(i);
  634. if (serial == NULL)
  635. continue;
  636. length += sprintf (page+length, "%d:", i);
  637. if (serial->type->owner)
  638. length += sprintf (page+length, " module:%s", module_name(serial->type->owner));
  639. length += sprintf (page+length, " name:\"%s\"", serial->type->name);
  640. length += sprintf (page+length, " vendor:%04x product:%04x",
  641. le16_to_cpu(serial->dev->descriptor.idVendor),
  642. le16_to_cpu(serial->dev->descriptor.idProduct));
  643. length += sprintf (page+length, " num_ports:%d", serial->num_ports);
  644. length += sprintf (page+length, " port:%d", i - serial->minor + 1);
  645. usb_make_path(serial->dev, tmp, sizeof(tmp));
  646. length += sprintf (page+length, " path:%s", tmp);
  647. length += sprintf (page+length, "\n");
  648. if ((length + begin) > (off + count))
  649. goto done;
  650. if ((length + begin) < off) {
  651. begin += length;
  652. length = 0;
  653. }
  654. kref_put(&serial->kref, destroy_serial);
  655. }
  656. *eof = 1;
  657. done:
  658. if (off >= (length + begin))
  659. return 0;
  660. *start = page + (off-begin);
  661. return ((count < begin+length-off) ? count : begin+length-off);
  662. }
  663. static int serial_tiocmget (struct tty_struct *tty, struct file *file)
  664. {
  665. struct usb_serial_port *port = tty->driver_data;
  666. dbg("%s - port %d", __FUNCTION__, port->number);
  667. if (!port->open_count) {
  668. dbg("%s - port not open", __FUNCTION__);
  669. goto exit;
  670. }
  671. if (port->serial->type->tiocmget)
  672. return port->serial->type->tiocmget(port, file);
  673. exit:
  674. return -EINVAL;
  675. }
  676. static int serial_tiocmset (struct tty_struct *tty, struct file *file,
  677. unsigned int set, unsigned int clear)
  678. {
  679. struct usb_serial_port *port = tty->driver_data;
  680. dbg("%s - port %d", __FUNCTION__, port->number);
  681. if (!port->open_count) {
  682. dbg("%s - port not open", __FUNCTION__);
  683. goto exit;
  684. }
  685. if (port->serial->type->tiocmset)
  686. return port->serial->type->tiocmset(port, file, set, clear);
  687. exit:
  688. return -EINVAL;
  689. }
  690. void usb_serial_port_softint(void *private)
  691. {
  692. struct usb_serial_port *port = private;
  693. struct tty_struct *tty;
  694. dbg("%s - port %d", __FUNCTION__, port->number);
  695. if (!port)
  696. return;
  697. tty = port->tty;
  698. if (!tty)
  699. return;
  700. tty_wakeup(tty);
  701. }
  702. static void port_release(struct device *dev)
  703. {
  704. struct usb_serial_port *port = to_usb_serial_port(dev);
  705. dbg ("%s - %s", __FUNCTION__, dev->bus_id);
  706. usb_kill_urb(port->read_urb);
  707. usb_free_urb(port->read_urb);
  708. usb_kill_urb(port->write_urb);
  709. usb_free_urb(port->write_urb);
  710. usb_kill_urb(port->interrupt_in_urb);
  711. usb_free_urb(port->interrupt_in_urb);
  712. usb_kill_urb(port->interrupt_out_urb);
  713. usb_free_urb(port->interrupt_out_urb);
  714. kfree(port->bulk_in_buffer);
  715. kfree(port->bulk_out_buffer);
  716. kfree(port->interrupt_in_buffer);
  717. kfree(port->interrupt_out_buffer);
  718. kfree(port);
  719. }
  720. static struct usb_serial * create_serial (struct usb_device *dev,
  721. struct usb_interface *interface,
  722. struct usb_serial_device_type *type)
  723. {
  724. struct usb_serial *serial;
  725. serial = kmalloc (sizeof (*serial), GFP_KERNEL);
  726. if (!serial) {
  727. dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
  728. return NULL;
  729. }
  730. memset (serial, 0, sizeof(*serial));
  731. serial->dev = usb_get_dev(dev);
  732. serial->type = type;
  733. serial->interface = interface;
  734. kref_init(&serial->kref);
  735. return serial;
  736. }
  737. static struct usb_serial_device_type *search_serial_device(struct usb_interface *iface)
  738. {
  739. struct list_head *p;
  740. const struct usb_device_id *id;
  741. struct usb_serial_device_type *t;
  742. /* List trough know devices and see if the usb id matches */
  743. list_for_each(p, &usb_serial_driver_list) {
  744. t = list_entry(p, struct usb_serial_device_type, driver_list);
  745. id = usb_match_id(iface, t->id_table);
  746. if (id != NULL) {
  747. dbg("descriptor matches");
  748. return t;
  749. }
  750. }
  751. return NULL;
  752. }
  753. int usb_serial_probe(struct usb_interface *interface,
  754. const struct usb_device_id *id)
  755. {
  756. struct usb_device *dev = interface_to_usbdev (interface);
  757. struct usb_serial *serial = NULL;
  758. struct usb_serial_port *port;
  759. struct usb_host_interface *iface_desc;
  760. struct usb_endpoint_descriptor *endpoint;
  761. struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
  762. struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
  763. struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
  764. struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
  765. struct usb_serial_device_type *type = NULL;
  766. int retval;
  767. int minor;
  768. int buffer_size;
  769. int i;
  770. int num_interrupt_in = 0;
  771. int num_interrupt_out = 0;
  772. int num_bulk_in = 0;
  773. int num_bulk_out = 0;
  774. int num_ports = 0;
  775. int max_endpoints;
  776. type = search_serial_device(interface);
  777. if (!type) {
  778. dbg("none matched");
  779. return -ENODEV;
  780. }
  781. serial = create_serial (dev, interface, type);
  782. if (!serial) {
  783. dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
  784. return -ENOMEM;
  785. }
  786. /* if this device type has a probe function, call it */
  787. if (type->probe) {
  788. const struct usb_device_id *id;
  789. if (!try_module_get(type->owner)) {
  790. dev_err(&interface->dev, "module get failed, exiting\n");
  791. kfree (serial);
  792. return -EIO;
  793. }
  794. id = usb_match_id(interface, type->id_table);
  795. retval = type->probe(serial, id);
  796. module_put(type->owner);
  797. if (retval) {
  798. dbg ("sub driver rejected device");
  799. kfree (serial);
  800. return retval;
  801. }
  802. }
  803. /* descriptor matches, let's find the endpoints needed */
  804. /* check out the endpoints */
  805. iface_desc = interface->cur_altsetting;
  806. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  807. endpoint = &iface_desc->endpoint[i].desc;
  808. if ((endpoint->bEndpointAddress & 0x80) &&
  809. ((endpoint->bmAttributes & 3) == 0x02)) {
  810. /* we found a bulk in endpoint */
  811. dbg("found bulk in on endpoint %d", i);
  812. bulk_in_endpoint[num_bulk_in] = endpoint;
  813. ++num_bulk_in;
  814. }
  815. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  816. ((endpoint->bmAttributes & 3) == 0x02)) {
  817. /* we found a bulk out endpoint */
  818. dbg("found bulk out on endpoint %d", i);
  819. bulk_out_endpoint[num_bulk_out] = endpoint;
  820. ++num_bulk_out;
  821. }
  822. if ((endpoint->bEndpointAddress & 0x80) &&
  823. ((endpoint->bmAttributes & 3) == 0x03)) {
  824. /* we found a interrupt in endpoint */
  825. dbg("found interrupt in on endpoint %d", i);
  826. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  827. ++num_interrupt_in;
  828. }
  829. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  830. ((endpoint->bmAttributes & 3) == 0x03)) {
  831. /* we found an interrupt out endpoint */
  832. dbg("found interrupt out on endpoint %d", i);
  833. interrupt_out_endpoint[num_interrupt_out] = endpoint;
  834. ++num_interrupt_out;
  835. }
  836. }
  837. #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
  838. /* BEGIN HORRIBLE HACK FOR PL2303 */
  839. /* this is needed due to the looney way its endpoints are set up */
  840. if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
  841. (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
  842. ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
  843. (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
  844. if (interface != dev->actconfig->interface[0]) {
  845. /* check out the endpoints of the other interface*/
  846. iface_desc = dev->actconfig->interface[0]->cur_altsetting;
  847. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  848. endpoint = &iface_desc->endpoint[i].desc;
  849. if ((endpoint->bEndpointAddress & 0x80) &&
  850. ((endpoint->bmAttributes & 3) == 0x03)) {
  851. /* we found a interrupt in endpoint */
  852. dbg("found interrupt in for Prolific device on separate interface");
  853. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  854. ++num_interrupt_in;
  855. }
  856. }
  857. }
  858. /* Now make sure the PL-2303 is configured correctly.
  859. * If not, give up now and hope this hack will work
  860. * properly during a later invocation of usb_serial_probe
  861. */
  862. if (num_bulk_in == 0 || num_bulk_out == 0) {
  863. dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
  864. kfree (serial);
  865. return -ENODEV;
  866. }
  867. }
  868. /* END HORRIBLE HACK FOR PL2303 */
  869. #endif
  870. /* found all that we need */
  871. dev_info(&interface->dev, "%s converter detected\n", type->name);
  872. #ifdef CONFIG_USB_SERIAL_GENERIC
  873. if (type == &usb_serial_generic_device) {
  874. num_ports = num_bulk_out;
  875. if (num_ports == 0) {
  876. dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
  877. kfree (serial);
  878. return -EIO;
  879. }
  880. }
  881. #endif
  882. if (!num_ports) {
  883. /* if this device type has a calc_num_ports function, call it */
  884. if (type->calc_num_ports) {
  885. if (!try_module_get(type->owner)) {
  886. dev_err(&interface->dev, "module get failed, exiting\n");
  887. kfree (serial);
  888. return -EIO;
  889. }
  890. num_ports = type->calc_num_ports (serial);
  891. module_put(type->owner);
  892. }
  893. if (!num_ports)
  894. num_ports = type->num_ports;
  895. }
  896. if (get_free_serial (serial, num_ports, &minor) == NULL) {
  897. dev_err(&interface->dev, "No more free serial devices\n");
  898. kfree (serial);
  899. return -ENOMEM;
  900. }
  901. serial->minor = minor;
  902. serial->num_ports = num_ports;
  903. serial->num_bulk_in = num_bulk_in;
  904. serial->num_bulk_out = num_bulk_out;
  905. serial->num_interrupt_in = num_interrupt_in;
  906. serial->num_interrupt_out = num_interrupt_out;
  907. /* create our ports, we need as many as the max endpoints */
  908. /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
  909. max_endpoints = max(num_bulk_in, num_bulk_out);
  910. max_endpoints = max(max_endpoints, num_interrupt_in);
  911. max_endpoints = max(max_endpoints, num_interrupt_out);
  912. max_endpoints = max(max_endpoints, (int)serial->num_ports);
  913. serial->num_port_pointers = max_endpoints;
  914. dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
  915. for (i = 0; i < max_endpoints; ++i) {
  916. port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
  917. if (!port)
  918. goto probe_error;
  919. memset(port, 0x00, sizeof(struct usb_serial_port));
  920. port->number = i + serial->minor;
  921. port->serial = serial;
  922. spin_lock_init(&port->lock);
  923. INIT_WORK(&port->work, usb_serial_port_softint, port);
  924. serial->port[i] = port;
  925. }
  926. /* set up the endpoint information */
  927. for (i = 0; i < num_bulk_in; ++i) {
  928. endpoint = bulk_in_endpoint[i];
  929. port = serial->port[i];
  930. port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
  931. if (!port->read_urb) {
  932. dev_err(&interface->dev, "No free urbs available\n");
  933. goto probe_error;
  934. }
  935. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  936. port->bulk_in_size = buffer_size;
  937. port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
  938. port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  939. if (!port->bulk_in_buffer) {
  940. dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
  941. goto probe_error;
  942. }
  943. usb_fill_bulk_urb (port->read_urb, dev,
  944. usb_rcvbulkpipe (dev,
  945. endpoint->bEndpointAddress),
  946. port->bulk_in_buffer, buffer_size,
  947. serial->type->read_bulk_callback,
  948. port);
  949. }
  950. for (i = 0; i < num_bulk_out; ++i) {
  951. endpoint = bulk_out_endpoint[i];
  952. port = serial->port[i];
  953. port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  954. if (!port->write_urb) {
  955. dev_err(&interface->dev, "No free urbs available\n");
  956. goto probe_error;
  957. }
  958. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  959. port->bulk_out_size = buffer_size;
  960. port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
  961. port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
  962. if (!port->bulk_out_buffer) {
  963. dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
  964. goto probe_error;
  965. }
  966. usb_fill_bulk_urb (port->write_urb, dev,
  967. usb_sndbulkpipe (dev,
  968. endpoint->bEndpointAddress),
  969. port->bulk_out_buffer, buffer_size,
  970. serial->type->write_bulk_callback,
  971. port);
  972. }
  973. if (serial->type->read_int_callback) {
  974. for (i = 0; i < num_interrupt_in; ++i) {
  975. endpoint = interrupt_in_endpoint[i];
  976. port = serial->port[i];
  977. port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  978. if (!port->interrupt_in_urb) {
  979. dev_err(&interface->dev, "No free urbs available\n");
  980. goto probe_error;
  981. }
  982. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  983. port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
  984. port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  985. if (!port->interrupt_in_buffer) {
  986. dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
  987. goto probe_error;
  988. }
  989. usb_fill_int_urb (port->interrupt_in_urb, dev,
  990. usb_rcvintpipe (dev,
  991. endpoint->bEndpointAddress),
  992. port->interrupt_in_buffer, buffer_size,
  993. serial->type->read_int_callback, port,
  994. endpoint->bInterval);
  995. }
  996. } else if (num_interrupt_in) {
  997. dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
  998. }
  999. if (serial->type->write_int_callback) {
  1000. for (i = 0; i < num_interrupt_out; ++i) {
  1001. endpoint = interrupt_out_endpoint[i];
  1002. port = serial->port[i];
  1003. port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1004. if (!port->interrupt_out_urb) {
  1005. dev_err(&interface->dev, "No free urbs available\n");
  1006. goto probe_error;
  1007. }
  1008. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  1009. port->interrupt_out_size = buffer_size;
  1010. port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
  1011. port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
  1012. if (!port->interrupt_out_buffer) {
  1013. dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
  1014. goto probe_error;
  1015. }
  1016. usb_fill_int_urb (port->interrupt_out_urb, dev,
  1017. usb_sndintpipe (dev,
  1018. endpoint->bEndpointAddress),
  1019. port->interrupt_out_buffer, buffer_size,
  1020. serial->type->write_int_callback, port,
  1021. endpoint->bInterval);
  1022. }
  1023. } else if (num_interrupt_out) {
  1024. dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
  1025. }
  1026. /* if this device type has an attach function, call it */
  1027. if (type->attach) {
  1028. if (!try_module_get(type->owner)) {
  1029. dev_err(&interface->dev, "module get failed, exiting\n");
  1030. goto probe_error;
  1031. }
  1032. retval = type->attach (serial);
  1033. module_put(type->owner);
  1034. if (retval < 0)
  1035. goto probe_error;
  1036. if (retval > 0) {
  1037. /* quietly accept this device, but don't bind to a serial port
  1038. * as it's about to disappear */
  1039. goto exit;
  1040. }
  1041. }
  1042. /* register all of the individual ports with the driver core */
  1043. for (i = 0; i < num_ports; ++i) {
  1044. port = serial->port[i];
  1045. port->dev.parent = &interface->dev;
  1046. port->dev.driver = NULL;
  1047. port->dev.bus = &usb_serial_bus_type;
  1048. port->dev.release = &port_release;
  1049. snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
  1050. dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
  1051. device_register (&port->dev);
  1052. }
  1053. usb_serial_console_init (debug, minor);
  1054. exit:
  1055. /* success */
  1056. usb_set_intfdata (interface, serial);
  1057. return 0;
  1058. probe_error:
  1059. for (i = 0; i < num_bulk_in; ++i) {
  1060. port = serial->port[i];
  1061. if (!port)
  1062. continue;
  1063. if (port->read_urb)
  1064. usb_free_urb (port->read_urb);
  1065. kfree(port->bulk_in_buffer);
  1066. }
  1067. for (i = 0; i < num_bulk_out; ++i) {
  1068. port = serial->port[i];
  1069. if (!port)
  1070. continue;
  1071. if (port->write_urb)
  1072. usb_free_urb (port->write_urb);
  1073. kfree(port->bulk_out_buffer);
  1074. }
  1075. for (i = 0; i < num_interrupt_in; ++i) {
  1076. port = serial->port[i];
  1077. if (!port)
  1078. continue;
  1079. if (port->interrupt_in_urb)
  1080. usb_free_urb (port->interrupt_in_urb);
  1081. kfree(port->interrupt_in_buffer);
  1082. }
  1083. for (i = 0; i < num_interrupt_out; ++i) {
  1084. port = serial->port[i];
  1085. if (!port)
  1086. continue;
  1087. if (port->interrupt_out_urb)
  1088. usb_free_urb (port->interrupt_out_urb);
  1089. kfree(port->interrupt_out_buffer);
  1090. }
  1091. /* return the minor range that this device had */
  1092. return_serial (serial);
  1093. /* free up any memory that we allocated */
  1094. for (i = 0; i < serial->num_port_pointers; ++i)
  1095. kfree(serial->port[i]);
  1096. kfree (serial);
  1097. return -EIO;
  1098. }
  1099. void usb_serial_disconnect(struct usb_interface *interface)
  1100. {
  1101. int i;
  1102. struct usb_serial *serial = usb_get_intfdata (interface);
  1103. struct device *dev = &interface->dev;
  1104. struct usb_serial_port *port;
  1105. dbg ("%s", __FUNCTION__);
  1106. usb_set_intfdata (interface, NULL);
  1107. if (serial) {
  1108. for (i = 0; i < serial->num_ports; ++i) {
  1109. port = serial->port[i];
  1110. if (port && port->tty)
  1111. tty_hangup(port->tty);
  1112. }
  1113. /* let the last holder of this object
  1114. * cause it to be cleaned up */
  1115. kref_put(&serial->kref, destroy_serial);
  1116. }
  1117. dev_info(dev, "device disconnected\n");
  1118. }
  1119. static struct tty_operations serial_ops = {
  1120. .open = serial_open,
  1121. .close = serial_close,
  1122. .write = serial_write,
  1123. .write_room = serial_write_room,
  1124. .ioctl = serial_ioctl,
  1125. .set_termios = serial_set_termios,
  1126. .throttle = serial_throttle,
  1127. .unthrottle = serial_unthrottle,
  1128. .break_ctl = serial_break,
  1129. .chars_in_buffer = serial_chars_in_buffer,
  1130. .read_proc = serial_read_proc,
  1131. .tiocmget = serial_tiocmget,
  1132. .tiocmset = serial_tiocmset,
  1133. };
  1134. struct tty_driver *usb_serial_tty_driver;
  1135. static int __init usb_serial_init(void)
  1136. {
  1137. int i;
  1138. int result;
  1139. usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
  1140. if (!usb_serial_tty_driver)
  1141. return -ENOMEM;
  1142. /* Initialize our global data */
  1143. for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
  1144. serial_table[i] = NULL;
  1145. }
  1146. result = bus_register(&usb_serial_bus_type);
  1147. if (result) {
  1148. err("%s - registering bus driver failed", __FUNCTION__);
  1149. goto exit_bus;
  1150. }
  1151. usb_serial_tty_driver->owner = THIS_MODULE;
  1152. usb_serial_tty_driver->driver_name = "usbserial";
  1153. usb_serial_tty_driver->devfs_name = "usb/tts/";
  1154. usb_serial_tty_driver->name = "ttyUSB";
  1155. usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
  1156. usb_serial_tty_driver->minor_start = 0;
  1157. usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1158. usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  1159. usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
  1160. usb_serial_tty_driver->init_termios = tty_std_termios;
  1161. usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1162. tty_set_operations(usb_serial_tty_driver, &serial_ops);
  1163. result = tty_register_driver(usb_serial_tty_driver);
  1164. if (result) {
  1165. err("%s - tty_register_driver failed", __FUNCTION__);
  1166. goto exit_reg_driver;
  1167. }
  1168. /* register the USB driver */
  1169. result = usb_register(&usb_serial_driver);
  1170. if (result < 0) {
  1171. err("%s - usb_register failed", __FUNCTION__);
  1172. goto exit_tty;
  1173. }
  1174. /* register the generic driver, if we should */
  1175. result = usb_serial_generic_register(debug);
  1176. if (result < 0) {
  1177. err("%s - registering generic driver failed", __FUNCTION__);
  1178. goto exit_generic;
  1179. }
  1180. info(DRIVER_DESC " " DRIVER_VERSION);
  1181. return result;
  1182. exit_generic:
  1183. usb_deregister(&usb_serial_driver);
  1184. exit_tty:
  1185. tty_unregister_driver(usb_serial_tty_driver);
  1186. exit_reg_driver:
  1187. bus_unregister(&usb_serial_bus_type);
  1188. exit_bus:
  1189. err ("%s - returning with error %d", __FUNCTION__, result);
  1190. put_tty_driver(usb_serial_tty_driver);
  1191. return result;
  1192. }
  1193. static void __exit usb_serial_exit(void)
  1194. {
  1195. usb_serial_console_exit();
  1196. usb_serial_generic_deregister();
  1197. usb_deregister(&usb_serial_driver);
  1198. tty_unregister_driver(usb_serial_tty_driver);
  1199. put_tty_driver(usb_serial_tty_driver);
  1200. bus_unregister(&usb_serial_bus_type);
  1201. }
  1202. module_init(usb_serial_init);
  1203. module_exit(usb_serial_exit);
  1204. #define set_to_generic_if_null(type, function) \
  1205. do { \
  1206. if (!type->function) { \
  1207. type->function = usb_serial_generic_##function; \
  1208. dbg("Had to override the " #function \
  1209. " usb serial operation with the generic one.");\
  1210. } \
  1211. } while (0)
  1212. static void fixup_generic(struct usb_serial_device_type *device)
  1213. {
  1214. set_to_generic_if_null(device, open);
  1215. set_to_generic_if_null(device, write);
  1216. set_to_generic_if_null(device, close);
  1217. set_to_generic_if_null(device, write_room);
  1218. set_to_generic_if_null(device, chars_in_buffer);
  1219. set_to_generic_if_null(device, read_bulk_callback);
  1220. set_to_generic_if_null(device, write_bulk_callback);
  1221. set_to_generic_if_null(device, shutdown);
  1222. }
  1223. int usb_serial_register(struct usb_serial_device_type *new_device)
  1224. {
  1225. int retval;
  1226. fixup_generic(new_device);
  1227. /* Add this device to our list of devices */
  1228. list_add(&new_device->driver_list, &usb_serial_driver_list);
  1229. retval = usb_serial_bus_register(new_device);
  1230. if (retval) {
  1231. err("problem %d when registering driver %s", retval, new_device->name);
  1232. list_del(&new_device->driver_list);
  1233. }
  1234. else
  1235. info("USB Serial support registered for %s", new_device->name);
  1236. return retval;
  1237. }
  1238. void usb_serial_deregister(struct usb_serial_device_type *device)
  1239. {
  1240. info("USB Serial deregistering driver %s", device->name);
  1241. list_del(&device->driver_list);
  1242. usb_serial_bus_deregister(device);
  1243. }
  1244. /* If the usb-serial core is built into the core, the usb-serial drivers
  1245. need these symbols to load properly as modules. */
  1246. EXPORT_SYMBOL_GPL(usb_serial_register);
  1247. EXPORT_SYMBOL_GPL(usb_serial_deregister);
  1248. EXPORT_SYMBOL_GPL(usb_serial_probe);
  1249. EXPORT_SYMBOL_GPL(usb_serial_disconnect);
  1250. EXPORT_SYMBOL_GPL(usb_serial_port_softint);
  1251. /* Module information */
  1252. MODULE_AUTHOR( DRIVER_AUTHOR );
  1253. MODULE_DESCRIPTION( DRIVER_DESC );
  1254. MODULE_VERSION( DRIVER_VERSION );
  1255. MODULE_LICENSE("GPL");
  1256. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1257. MODULE_PARM_DESC(debug, "Debug enabled or not");