visor.c 29 KB

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