visor.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this driver
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/spinlock.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #include "visor.h"
  29. /*
  30. * Version Information
  31. */
  32. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  33. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  34. /* function prototypes for a handspring visor */
  35. static int visor_open (struct usb_serial_port *port, struct file *filp);
  36. static void visor_close (struct usb_serial_port *port, struct file *filp);
  37. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  38. static int visor_write_room (struct usb_serial_port *port);
  39. static int visor_chars_in_buffer (struct usb_serial_port *port);
  40. static void visor_throttle (struct usb_serial_port *port);
  41. static void visor_unthrottle (struct usb_serial_port *port);
  42. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id);
  43. static int visor_calc_num_ports(struct usb_serial *serial);
  44. static void visor_shutdown (struct usb_serial *serial);
  45. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  46. static void visor_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
  47. static void visor_write_bulk_callback (struct urb *urb);
  48. static void visor_read_bulk_callback (struct urb *urb);
  49. static void visor_read_int_callback (struct urb *urb);
  50. static int clie_3_5_startup (struct usb_serial *serial);
  51. static int treo_attach (struct usb_serial *serial);
  52. static int clie_5_attach (struct usb_serial *serial);
  53. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id);
  54. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id);
  55. /* Parameters that may be passed into the module. */
  56. static int debug;
  57. static __u16 vendor;
  58. static __u16 product;
  59. static struct usb_device_id id_table [] = {
  60. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  61. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  62. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  63. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  64. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  65. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  66. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  67. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  68. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  69. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  70. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  71. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  72. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  73. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  74. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  75. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  76. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  77. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  78. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  79. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  80. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  81. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  82. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  83. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  84. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  85. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  86. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  87. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  88. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID),
  89. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  90. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  91. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  92. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  93. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  94. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  95. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  96. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  97. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  98. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  99. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  100. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  101. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  102. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  103. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  104. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  105. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  106. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  107. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  108. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  109. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  110. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  111. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  112. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  113. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  114. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  115. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  116. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  117. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  118. { }, /* optional parameter entry */
  119. { } /* Terminating entry */
  120. };
  121. static struct usb_device_id clie_id_5_table [] = {
  122. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  123. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  124. { }, /* optional parameter entry */
  125. { } /* Terminating entry */
  126. };
  127. static struct usb_device_id clie_id_3_5_table [] = {
  128. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  129. { } /* Terminating entry */
  130. };
  131. static struct usb_device_id id_table_combined [] = {
  132. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  133. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  134. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  135. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  136. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  137. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  148. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  156. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  157. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  158. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  159. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  160. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  161. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  162. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  163. { }, /* optional parameter entry */
  164. { } /* Terminating entry */
  165. };
  166. MODULE_DEVICE_TABLE (usb, id_table_combined);
  167. static struct usb_driver visor_driver = {
  168. .name = "visor",
  169. .probe = usb_serial_probe,
  170. .disconnect = usb_serial_disconnect,
  171. .id_table = id_table_combined,
  172. .no_dynamic_id = 1,
  173. };
  174. /* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
  175. static struct usb_serial_driver handspring_device = {
  176. .driver = {
  177. .owner = THIS_MODULE,
  178. .name = "visor",
  179. },
  180. .description = "Handspring Visor / Palm OS",
  181. .usb_driver = &visor_driver,
  182. .id_table = id_table,
  183. .num_interrupt_in = NUM_DONT_CARE,
  184. .num_bulk_in = 2,
  185. .num_bulk_out = 2,
  186. .num_ports = 2,
  187. .open = visor_open,
  188. .close = visor_close,
  189. .throttle = visor_throttle,
  190. .unthrottle = visor_unthrottle,
  191. .attach = treo_attach,
  192. .probe = visor_probe,
  193. .calc_num_ports = visor_calc_num_ports,
  194. .shutdown = visor_shutdown,
  195. .ioctl = visor_ioctl,
  196. .set_termios = visor_set_termios,
  197. .write = visor_write,
  198. .write_room = visor_write_room,
  199. .chars_in_buffer = visor_chars_in_buffer,
  200. .write_bulk_callback = visor_write_bulk_callback,
  201. .read_bulk_callback = visor_read_bulk_callback,
  202. .read_int_callback = visor_read_int_callback,
  203. };
  204. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  205. static struct usb_serial_driver clie_5_device = {
  206. .driver = {
  207. .owner = THIS_MODULE,
  208. .name = "clie_5",
  209. },
  210. .description = "Sony Clie 5.0",
  211. .usb_driver = &visor_driver,
  212. .id_table = clie_id_5_table,
  213. .num_interrupt_in = NUM_DONT_CARE,
  214. .num_bulk_in = 2,
  215. .num_bulk_out = 2,
  216. .num_ports = 2,
  217. .open = visor_open,
  218. .close = visor_close,
  219. .throttle = visor_throttle,
  220. .unthrottle = visor_unthrottle,
  221. .attach = clie_5_attach,
  222. .probe = visor_probe,
  223. .calc_num_ports = visor_calc_num_ports,
  224. .shutdown = visor_shutdown,
  225. .ioctl = visor_ioctl,
  226. .set_termios = visor_set_termios,
  227. .write = visor_write,
  228. .write_room = visor_write_room,
  229. .chars_in_buffer = visor_chars_in_buffer,
  230. .write_bulk_callback = visor_write_bulk_callback,
  231. .read_bulk_callback = visor_read_bulk_callback,
  232. .read_int_callback = visor_read_int_callback,
  233. };
  234. /* device info for the Sony Clie OS version 3.5 */
  235. static struct usb_serial_driver clie_3_5_device = {
  236. .driver = {
  237. .owner = THIS_MODULE,
  238. .name = "clie_3.5",
  239. },
  240. .description = "Sony Clie 3.5",
  241. .usb_driver = &visor_driver,
  242. .id_table = clie_id_3_5_table,
  243. .num_interrupt_in = 0,
  244. .num_bulk_in = 1,
  245. .num_bulk_out = 1,
  246. .num_ports = 1,
  247. .open = visor_open,
  248. .close = visor_close,
  249. .throttle = visor_throttle,
  250. .unthrottle = visor_unthrottle,
  251. .attach = clie_3_5_startup,
  252. .ioctl = visor_ioctl,
  253. .set_termios = visor_set_termios,
  254. .write = visor_write,
  255. .write_room = visor_write_room,
  256. .chars_in_buffer = visor_chars_in_buffer,
  257. .write_bulk_callback = visor_write_bulk_callback,
  258. .read_bulk_callback = visor_read_bulk_callback,
  259. };
  260. struct visor_private {
  261. spinlock_t lock;
  262. int bytes_in;
  263. int bytes_out;
  264. int outstanding_urbs;
  265. int throttled;
  266. };
  267. /* number of outstanding urbs to prevent userspace DoS from happening */
  268. #define URB_UPPER_LIMIT 42
  269. static int stats;
  270. /******************************************************************************
  271. * Handspring Visor specific driver functions
  272. ******************************************************************************/
  273. static int visor_open (struct usb_serial_port *port, struct file *filp)
  274. {
  275. struct usb_serial *serial = port->serial;
  276. struct visor_private *priv = usb_get_serial_port_data(port);
  277. unsigned long flags;
  278. int result = 0;
  279. dbg("%s - port %d", __FUNCTION__, port->number);
  280. if (!port->read_urb) {
  281. /* this is needed for some brain dead Sony devices */
  282. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  283. return -ENODEV;
  284. }
  285. spin_lock_irqsave(&priv->lock, flags);
  286. priv->bytes_in = 0;
  287. priv->bytes_out = 0;
  288. priv->throttled = 0;
  289. spin_unlock_irqrestore(&priv->lock, flags);
  290. /*
  291. * Force low_latency on so that our tty_push actually forces the data
  292. * through, otherwise it is scheduled, and with high data rates (like
  293. * with OHCI) data can get lost.
  294. */
  295. if (port->tty)
  296. port->tty->low_latency = 1;
  297. /* Start reading from the device */
  298. usb_fill_bulk_urb (port->read_urb, serial->dev,
  299. usb_rcvbulkpipe (serial->dev,
  300. port->bulk_in_endpointAddress),
  301. port->read_urb->transfer_buffer,
  302. port->read_urb->transfer_buffer_length,
  303. visor_read_bulk_callback, port);
  304. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  305. if (result) {
  306. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n",
  307. __FUNCTION__, result);
  308. goto exit;
  309. }
  310. if (port->interrupt_in_urb) {
  311. dbg("%s - adding interrupt input for treo", __FUNCTION__);
  312. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  313. if (result)
  314. dev_err(&port->dev, "%s - failed submitting interrupt urb, error %d\n",
  315. __FUNCTION__, result);
  316. }
  317. exit:
  318. return result;
  319. }
  320. static void visor_close (struct usb_serial_port *port, struct file * filp)
  321. {
  322. struct visor_private *priv = usb_get_serial_port_data(port);
  323. unsigned char *transfer_buffer;
  324. dbg("%s - port %d", __FUNCTION__, port->number);
  325. /* shutdown our urbs */
  326. usb_kill_urb(port->read_urb);
  327. usb_kill_urb(port->interrupt_in_urb);
  328. /* Try to send shutdown message, if the device is gone, this will just fail. */
  329. transfer_buffer = kmalloc (0x12, GFP_KERNEL);
  330. if (transfer_buffer) {
  331. usb_control_msg (port->serial->dev,
  332. usb_rcvctrlpipe(port->serial->dev, 0),
  333. VISOR_CLOSE_NOTIFICATION, 0xc2,
  334. 0x0000, 0x0000,
  335. transfer_buffer, 0x12, 300);
  336. kfree (transfer_buffer);
  337. }
  338. if (stats)
  339. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  340. priv->bytes_in, priv->bytes_out);
  341. }
  342. static int visor_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  343. {
  344. struct visor_private *priv = usb_get_serial_port_data(port);
  345. struct usb_serial *serial = port->serial;
  346. struct urb *urb;
  347. unsigned char *buffer;
  348. unsigned long flags;
  349. int status;
  350. dbg("%s - port %d", __FUNCTION__, port->number);
  351. spin_lock_irqsave(&priv->lock, flags);
  352. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  353. spin_unlock_irqrestore(&priv->lock, flags);
  354. dbg("%s - write limit hit\n", __FUNCTION__);
  355. return 0;
  356. }
  357. spin_unlock_irqrestore(&priv->lock, flags);
  358. buffer = kmalloc (count, GFP_ATOMIC);
  359. if (!buffer) {
  360. dev_err(&port->dev, "out of memory\n");
  361. return -ENOMEM;
  362. }
  363. urb = usb_alloc_urb(0, GFP_ATOMIC);
  364. if (!urb) {
  365. dev_err(&port->dev, "no more free urbs\n");
  366. kfree (buffer);
  367. return -ENOMEM;
  368. }
  369. memcpy (buffer, buf, count);
  370. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  371. usb_fill_bulk_urb (urb, serial->dev,
  372. usb_sndbulkpipe (serial->dev,
  373. port->bulk_out_endpointAddress),
  374. buffer, count,
  375. visor_write_bulk_callback, port);
  376. /* send it down the pipe */
  377. status = usb_submit_urb(urb, GFP_ATOMIC);
  378. if (status) {
  379. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  380. __FUNCTION__, status);
  381. count = status;
  382. kfree (buffer);
  383. } else {
  384. spin_lock_irqsave(&priv->lock, flags);
  385. ++priv->outstanding_urbs;
  386. priv->bytes_out += count;
  387. spin_unlock_irqrestore(&priv->lock, flags);
  388. }
  389. /* we are done with this urb, so let the host driver
  390. * really free it when it is finished with it */
  391. usb_free_urb (urb);
  392. return count;
  393. }
  394. static int visor_write_room (struct usb_serial_port *port)
  395. {
  396. struct visor_private *priv = usb_get_serial_port_data(port);
  397. unsigned long flags;
  398. dbg("%s - port %d", __FUNCTION__, port->number);
  399. /*
  400. * We really can take anything the user throws at us
  401. * but let's pick a nice big number to tell the tty
  402. * layer that we have lots of free space, unless we don't.
  403. */
  404. spin_lock_irqsave(&priv->lock, flags);
  405. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  406. spin_unlock_irqrestore(&priv->lock, flags);
  407. dbg("%s - write limit hit\n", __FUNCTION__);
  408. return 0;
  409. }
  410. spin_unlock_irqrestore(&priv->lock, flags);
  411. return 2048;
  412. }
  413. static int visor_chars_in_buffer (struct usb_serial_port *port)
  414. {
  415. dbg("%s - port %d", __FUNCTION__, port->number);
  416. /*
  417. * We can't really account for how much data we
  418. * have sent out, but hasn't made it through to the
  419. * device, so just tell the tty layer that everything
  420. * is flushed.
  421. */
  422. return 0;
  423. }
  424. static void visor_write_bulk_callback (struct urb *urb)
  425. {
  426. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  427. struct visor_private *priv = usb_get_serial_port_data(port);
  428. unsigned long flags;
  429. /* free up the transfer buffer, as usb_free_urb() does not do this */
  430. kfree (urb->transfer_buffer);
  431. dbg("%s - port %d", __FUNCTION__, port->number);
  432. if (urb->status)
  433. dbg("%s - nonzero write bulk status received: %d",
  434. __FUNCTION__, urb->status);
  435. spin_lock_irqsave(&priv->lock, flags);
  436. --priv->outstanding_urbs;
  437. spin_unlock_irqrestore(&priv->lock, flags);
  438. usb_serial_port_softint(port);
  439. }
  440. static void visor_read_bulk_callback (struct urb *urb)
  441. {
  442. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  443. struct visor_private *priv = usb_get_serial_port_data(port);
  444. unsigned char *data = urb->transfer_buffer;
  445. struct tty_struct *tty;
  446. unsigned long flags;
  447. int throttled;
  448. int result;
  449. dbg("%s - port %d", __FUNCTION__, port->number);
  450. if (urb->status) {
  451. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  452. return;
  453. }
  454. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  455. tty = port->tty;
  456. if (tty && urb->actual_length) {
  457. tty_buffer_request_room(tty, urb->actual_length);
  458. tty_insert_flip_string(tty, data, urb->actual_length);
  459. tty_flip_buffer_push(tty);
  460. }
  461. spin_lock_irqsave(&priv->lock, flags);
  462. priv->bytes_in += urb->actual_length;
  463. throttled = priv->throttled;
  464. spin_unlock_irqrestore(&priv->lock, flags);
  465. /* Continue trying to always read if we should */
  466. if (!throttled) {
  467. usb_fill_bulk_urb (port->read_urb, port->serial->dev,
  468. usb_rcvbulkpipe(port->serial->dev,
  469. port->bulk_in_endpointAddress),
  470. port->read_urb->transfer_buffer,
  471. port->read_urb->transfer_buffer_length,
  472. visor_read_bulk_callback, port);
  473. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  474. if (result)
  475. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  476. }
  477. return;
  478. }
  479. static void visor_read_int_callback (struct urb *urb)
  480. {
  481. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  482. int result;
  483. switch (urb->status) {
  484. case 0:
  485. /* success */
  486. break;
  487. case -ECONNRESET:
  488. case -ENOENT:
  489. case -ESHUTDOWN:
  490. /* this urb is terminated, clean up */
  491. dbg("%s - urb shutting down with status: %d",
  492. __FUNCTION__, urb->status);
  493. return;
  494. default:
  495. dbg("%s - nonzero urb status received: %d",
  496. __FUNCTION__, urb->status);
  497. goto exit;
  498. }
  499. /*
  500. * This information is still unknown what it can be used for.
  501. * If anyone has an idea, please let the author know...
  502. *
  503. * Rumor has it this endpoint is used to notify when data
  504. * is ready to be read from the bulk ones.
  505. */
  506. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  507. urb->actual_length, urb->transfer_buffer);
  508. exit:
  509. result = usb_submit_urb (urb, GFP_ATOMIC);
  510. if (result)
  511. dev_err(&urb->dev->dev, "%s - Error %d submitting interrupt urb\n",
  512. __FUNCTION__, result);
  513. }
  514. static void visor_throttle (struct usb_serial_port *port)
  515. {
  516. struct visor_private *priv = usb_get_serial_port_data(port);
  517. unsigned long flags;
  518. dbg("%s - port %d", __FUNCTION__, port->number);
  519. spin_lock_irqsave(&priv->lock, flags);
  520. priv->throttled = 1;
  521. spin_unlock_irqrestore(&priv->lock, flags);
  522. }
  523. static void visor_unthrottle (struct usb_serial_port *port)
  524. {
  525. struct visor_private *priv = usb_get_serial_port_data(port);
  526. unsigned long flags;
  527. int result;
  528. dbg("%s - port %d", __FUNCTION__, port->number);
  529. spin_lock_irqsave(&priv->lock, flags);
  530. priv->throttled = 0;
  531. spin_unlock_irqrestore(&priv->lock, flags);
  532. port->read_urb->dev = port->serial->dev;
  533. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  534. if (result)
  535. dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
  536. }
  537. static int palm_os_3_probe (struct usb_serial *serial, const struct usb_device_id *id)
  538. {
  539. struct device *dev = &serial->dev->dev;
  540. struct visor_connection_info *connection_info;
  541. unsigned char *transfer_buffer;
  542. char *string;
  543. int retval = 0;
  544. int i;
  545. int num_ports = 0;
  546. dbg("%s", __FUNCTION__);
  547. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  548. if (!transfer_buffer) {
  549. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  550. sizeof(*connection_info));
  551. return -ENOMEM;
  552. }
  553. /* send a get connection info request */
  554. retval = usb_control_msg (serial->dev,
  555. usb_rcvctrlpipe(serial->dev, 0),
  556. VISOR_GET_CONNECTION_INFORMATION,
  557. 0xc2, 0x0000, 0x0000, transfer_buffer,
  558. sizeof(*connection_info), 300);
  559. if (retval < 0) {
  560. dev_err(dev, "%s - error %d getting connection information\n",
  561. __FUNCTION__, retval);
  562. goto exit;
  563. }
  564. if (retval == sizeof(*connection_info)) {
  565. connection_info = (struct visor_connection_info *)transfer_buffer;
  566. num_ports = le16_to_cpu(connection_info->num_ports);
  567. for (i = 0; i < num_ports; ++i) {
  568. switch (connection_info->connections[i].port_function_id) {
  569. case VISOR_FUNCTION_GENERIC:
  570. string = "Generic";
  571. break;
  572. case VISOR_FUNCTION_DEBUGGER:
  573. string = "Debugger";
  574. break;
  575. case VISOR_FUNCTION_HOTSYNC:
  576. string = "HotSync";
  577. break;
  578. case VISOR_FUNCTION_CONSOLE:
  579. string = "Console";
  580. break;
  581. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  582. string = "Remote File System";
  583. break;
  584. default:
  585. string = "unknown";
  586. break;
  587. }
  588. dev_info(dev, "%s: port %d, is for %s use\n",
  589. serial->type->description,
  590. connection_info->connections[i].port, string);
  591. }
  592. }
  593. /*
  594. * Handle devices that report invalid stuff here.
  595. */
  596. if (num_ports == 0 || num_ports > 2) {
  597. dev_warn (dev, "%s: No valid connect info available\n",
  598. serial->type->description);
  599. num_ports = 2;
  600. }
  601. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  602. num_ports);
  603. /*
  604. * save off our num_ports info so that we can use it in the
  605. * calc_num_ports callback
  606. */
  607. usb_set_serial_data(serial, (void *)(long)num_ports);
  608. /* ask for the number of bytes available, but ignore the response as it is broken */
  609. retval = usb_control_msg (serial->dev,
  610. usb_rcvctrlpipe(serial->dev, 0),
  611. VISOR_REQUEST_BYTES_AVAILABLE,
  612. 0xc2, 0x0000, 0x0005, transfer_buffer,
  613. 0x02, 300);
  614. if (retval < 0)
  615. dev_err(dev, "%s - error %d getting bytes available request\n",
  616. __FUNCTION__, retval);
  617. retval = 0;
  618. exit:
  619. kfree (transfer_buffer);
  620. return retval;
  621. }
  622. static int palm_os_4_probe (struct usb_serial *serial, const struct usb_device_id *id)
  623. {
  624. struct device *dev = &serial->dev->dev;
  625. struct palm_ext_connection_info *connection_info;
  626. unsigned char *transfer_buffer;
  627. int retval;
  628. dbg("%s", __FUNCTION__);
  629. transfer_buffer = kmalloc (sizeof (*connection_info), GFP_KERNEL);
  630. if (!transfer_buffer) {
  631. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __FUNCTION__,
  632. sizeof(*connection_info));
  633. return -ENOMEM;
  634. }
  635. retval = usb_control_msg (serial->dev,
  636. usb_rcvctrlpipe(serial->dev, 0),
  637. PALM_GET_EXT_CONNECTION_INFORMATION,
  638. 0xc2, 0x0000, 0x0000, transfer_buffer,
  639. sizeof (*connection_info), 300);
  640. if (retval < 0)
  641. dev_err(dev, "%s - error %d getting connection info\n",
  642. __FUNCTION__, retval);
  643. else
  644. usb_serial_debug_data(debug, &serial->dev->dev, __FUNCTION__,
  645. retval, transfer_buffer);
  646. kfree (transfer_buffer);
  647. return 0;
  648. }
  649. static int visor_probe (struct usb_serial *serial, const struct usb_device_id *id)
  650. {
  651. int retval = 0;
  652. int (*startup) (struct usb_serial *serial, const struct usb_device_id *id);
  653. dbg("%s", __FUNCTION__);
  654. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  655. err("active config #%d != 1 ??",
  656. serial->dev->actconfig->desc.bConfigurationValue);
  657. return -ENODEV;
  658. }
  659. if (id->driver_info) {
  660. startup = (void *)id->driver_info;
  661. retval = startup(serial, id);
  662. }
  663. return retval;
  664. }
  665. static int visor_calc_num_ports (struct usb_serial *serial)
  666. {
  667. int num_ports = (int)(long)(usb_get_serial_data(serial));
  668. if (num_ports)
  669. usb_set_serial_data(serial, NULL);
  670. return num_ports;
  671. }
  672. static int generic_startup(struct usb_serial *serial)
  673. {
  674. struct usb_serial_port **ports = serial->port;
  675. struct visor_private *priv;
  676. int i;
  677. for (i = 0; i < serial->num_ports; ++i) {
  678. priv = kzalloc (sizeof(*priv), GFP_KERNEL);
  679. if (!priv) {
  680. while (i-- != 0) {
  681. priv = usb_get_serial_port_data(ports[i]);
  682. usb_set_serial_port_data(ports[i], NULL);
  683. kfree(priv);
  684. }
  685. return -ENOMEM;
  686. }
  687. spin_lock_init(&priv->lock);
  688. usb_set_serial_port_data(ports[i], priv);
  689. }
  690. return 0;
  691. }
  692. static int clie_3_5_startup (struct usb_serial *serial)
  693. {
  694. struct device *dev = &serial->dev->dev;
  695. int result;
  696. u8 data;
  697. dbg("%s", __FUNCTION__);
  698. /*
  699. * Note that PEG-300 series devices expect the following two calls.
  700. */
  701. /* get the config number */
  702. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  703. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  704. 0, 0, &data, 1, 3000);
  705. if (result < 0) {
  706. dev_err(dev, "%s: get config number failed: %d\n", __FUNCTION__, result);
  707. return result;
  708. }
  709. if (result != 1) {
  710. dev_err(dev, "%s: get config number bad return length: %d\n", __FUNCTION__, result);
  711. return -EIO;
  712. }
  713. /* get the interface number */
  714. result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  715. USB_REQ_GET_INTERFACE,
  716. USB_DIR_IN | USB_RECIP_INTERFACE,
  717. 0, 0, &data, 1, 3000);
  718. if (result < 0) {
  719. dev_err(dev, "%s: get interface number failed: %d\n", __FUNCTION__, result);
  720. return result;
  721. }
  722. if (result != 1) {
  723. dev_err(dev, "%s: get interface number bad return length: %d\n", __FUNCTION__, result);
  724. return -EIO;
  725. }
  726. return generic_startup(serial);
  727. }
  728. static int treo_attach (struct usb_serial *serial)
  729. {
  730. struct usb_serial_port *swap_port;
  731. /* Only do this endpoint hack for the Handspring devices with
  732. * interrupt in endpoints, which for now are the Treo devices. */
  733. if (!((le16_to_cpu(serial->dev->descriptor.idVendor) == HANDSPRING_VENDOR_ID) ||
  734. (le16_to_cpu(serial->dev->descriptor.idVendor) == KYOCERA_VENDOR_ID)) ||
  735. (serial->num_interrupt_in == 0))
  736. goto generic_startup;
  737. dbg("%s", __FUNCTION__);
  738. /*
  739. * It appears that Treos and Kyoceras want to use the
  740. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  741. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  742. * Note that swapping the bulk out endpoints would break lots of
  743. * apps that want to communicate on the second port.
  744. */
  745. #define COPY_PORT(dest, src) \
  746. dest->read_urb = src->read_urb; \
  747. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress; \
  748. dest->bulk_in_buffer = src->bulk_in_buffer; \
  749. dest->interrupt_in_urb = src->interrupt_in_urb; \
  750. dest->interrupt_in_endpointAddress = src->interrupt_in_endpointAddress; \
  751. dest->interrupt_in_buffer = src->interrupt_in_buffer;
  752. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  753. if (!swap_port)
  754. return -ENOMEM;
  755. COPY_PORT(swap_port, serial->port[0]);
  756. COPY_PORT(serial->port[0], serial->port[1]);
  757. COPY_PORT(serial->port[1], swap_port);
  758. kfree(swap_port);
  759. generic_startup:
  760. return generic_startup(serial);
  761. }
  762. static int clie_5_attach (struct usb_serial *serial)
  763. {
  764. dbg("%s", __FUNCTION__);
  765. /* TH55 registers 2 ports.
  766. Communication in from the UX50/TH55 uses bulk_in_endpointAddress from port 0
  767. Communication out to the UX50/TH55 uses bulk_out_endpointAddress from port 1
  768. Lets do a quick and dirty mapping
  769. */
  770. /* some sanity check */
  771. if (serial->num_ports < 2)
  772. return -1;
  773. /* port 0 now uses the modified endpoint Address */
  774. serial->port[0]->bulk_out_endpointAddress = serial->port[1]->bulk_out_endpointAddress;
  775. return generic_startup(serial);
  776. }
  777. static void visor_shutdown (struct usb_serial *serial)
  778. {
  779. struct visor_private *priv;
  780. int i;
  781. dbg("%s", __FUNCTION__);
  782. for (i = 0; i < serial->num_ports; i++) {
  783. priv = usb_get_serial_port_data(serial->port[i]);
  784. if (priv) {
  785. usb_set_serial_port_data(serial->port[i], NULL);
  786. kfree(priv);
  787. }
  788. }
  789. }
  790. static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  791. {
  792. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  793. return -ENOIOCTLCMD;
  794. }
  795. /* This function is all nice and good, but we don't change anything based on it :) */
  796. static void visor_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
  797. {
  798. unsigned int cflag;
  799. dbg("%s - port %d", __FUNCTION__, port->number);
  800. if ((!port->tty) || (!port->tty->termios)) {
  801. dbg("%s - no tty structures", __FUNCTION__);
  802. return;
  803. }
  804. cflag = port->tty->termios->c_cflag;
  805. /* check that they really want us to change something */
  806. if (old_termios) {
  807. if ((cflag == old_termios->c_cflag) &&
  808. (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
  809. dbg("%s - nothing to change...", __FUNCTION__);
  810. return;
  811. }
  812. }
  813. /* get the byte size */
  814. switch (cflag & CSIZE) {
  815. case CS5: dbg("%s - data bits = 5", __FUNCTION__); break;
  816. case CS6: dbg("%s - data bits = 6", __FUNCTION__); break;
  817. case CS7: dbg("%s - data bits = 7", __FUNCTION__); break;
  818. default:
  819. case CS8: dbg("%s - data bits = 8", __FUNCTION__); break;
  820. }
  821. /* determine the parity */
  822. if (cflag & PARENB)
  823. if (cflag & PARODD)
  824. dbg("%s - parity = odd", __FUNCTION__);
  825. else
  826. dbg("%s - parity = even", __FUNCTION__);
  827. else
  828. dbg("%s - parity = none", __FUNCTION__);
  829. /* figure out the stop bits requested */
  830. if (cflag & CSTOPB)
  831. dbg("%s - stop bits = 2", __FUNCTION__);
  832. else
  833. dbg("%s - stop bits = 1", __FUNCTION__);
  834. /* figure out the flow control settings */
  835. if (cflag & CRTSCTS)
  836. dbg("%s - RTS/CTS is enabled", __FUNCTION__);
  837. else
  838. dbg("%s - RTS/CTS is disabled", __FUNCTION__);
  839. /* determine software flow control */
  840. if (I_IXOFF(port->tty))
  841. dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  842. __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
  843. else
  844. dbg("%s - XON/XOFF is disabled", __FUNCTION__);
  845. /* get the baud rate wanted */
  846. dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
  847. return;
  848. }
  849. static int __init visor_init (void)
  850. {
  851. int i, retval;
  852. /* Only if parameters were passed to us */
  853. if ((vendor>0) && (product>0)) {
  854. struct usb_device_id usb_dev_temp[]=
  855. {{USB_DEVICE(vendor, product),
  856. .driver_info = (kernel_ulong_t)&palm_os_4_probe }};
  857. /* Find the last entry in id_table */
  858. for (i=0; ; i++) {
  859. if (id_table[i].idVendor==0) {
  860. id_table[i] = usb_dev_temp[0];
  861. break;
  862. }
  863. }
  864. /* Find the last entry in id_table_combined */
  865. for (i=0; ; i++) {
  866. if (id_table_combined[i].idVendor==0) {
  867. id_table_combined[i] = usb_dev_temp[0];
  868. break;
  869. }
  870. }
  871. info("Untested USB device specified at time of module insertion");
  872. info("Warning: This is not guaranteed to work");
  873. info("Using a newer kernel is preferred to this method");
  874. info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
  875. vendor, product);
  876. }
  877. retval = usb_serial_register(&handspring_device);
  878. if (retval)
  879. goto failed_handspring_register;
  880. retval = usb_serial_register(&clie_3_5_device);
  881. if (retval)
  882. goto failed_clie_3_5_register;
  883. retval = usb_serial_register(&clie_5_device);
  884. if (retval)
  885. goto failed_clie_5_register;
  886. retval = usb_register(&visor_driver);
  887. if (retval)
  888. goto failed_usb_register;
  889. info(DRIVER_DESC);
  890. return 0;
  891. failed_usb_register:
  892. usb_serial_deregister(&clie_5_device);
  893. failed_clie_5_register:
  894. usb_serial_deregister(&clie_3_5_device);
  895. failed_clie_3_5_register:
  896. usb_serial_deregister(&handspring_device);
  897. failed_handspring_register:
  898. return retval;
  899. }
  900. static void __exit visor_exit (void)
  901. {
  902. usb_deregister (&visor_driver);
  903. usb_serial_deregister (&handspring_device);
  904. usb_serial_deregister (&clie_3_5_device);
  905. usb_serial_deregister (&clie_5_device);
  906. }
  907. module_init(visor_init);
  908. module_exit(visor_exit);
  909. MODULE_AUTHOR( DRIVER_AUTHOR );
  910. MODULE_DESCRIPTION( DRIVER_DESC );
  911. MODULE_LICENSE("GPL");
  912. module_param(debug, bool, S_IRUGO | S_IWUSR);
  913. MODULE_PARM_DESC(debug, "Debug enabled or not");
  914. module_param(stats, bool, S_IRUGO | S_IWUSR);
  915. MODULE_PARM_DESC(stats, "Enables statistics or not");
  916. module_param(vendor, ushort, 0);
  917. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  918. module_param(product, ushort, 0);
  919. MODULE_PARM_DESC(product, "User specified product ID");