visor.c 29 KB

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