visor.c 29 KB

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